implement Unicode Copy/Paster for win32
This commit is contained in:
parent
6e6f8ca3f4
commit
fda4708756
117
src/readline.c
117
src/readline.c
@ -120,7 +120,7 @@ static void Insert_character_unicode(word * str, word c, int position)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int Prepend_string(char* dest, char* src, int max)
|
static int Prepend_string(char* dest, const char* src, int max)
|
||||||
// Insert a string at the start of another. Up to MAX characters only
|
// Insert a string at the start of another. Up to MAX characters only
|
||||||
// Returns actual number of chars inserted
|
// Returns actual number of chars inserted
|
||||||
{
|
{
|
||||||
@ -138,7 +138,25 @@ int Prepend_string(char* dest, char* src, int max)
|
|||||||
return sizes;
|
return sizes;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Valid_character(word c, int input_type)
|
static int Prepend_string_unicode(word* dest, const word* src, int max)
|
||||||
|
// Insert a string at the start of another. Up to MAX characters only
|
||||||
|
// Returns actual number of chars inserted
|
||||||
|
{
|
||||||
|
// Insert src before dest
|
||||||
|
int sized = Unicode_strlen(dest);
|
||||||
|
int sizes = Unicode_strlen(src);
|
||||||
|
|
||||||
|
if (sized + sizes >= max)
|
||||||
|
{
|
||||||
|
sizes = max - sized;
|
||||||
|
}
|
||||||
|
|
||||||
|
memmove(dest+sizes, dest, 2*(sized+1));
|
||||||
|
memcpy(dest, src, sizes*2);
|
||||||
|
return sizes;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int Valid_character(word c, int input_type)
|
||||||
// returns 0 = Not allowed
|
// returns 0 = Not allowed
|
||||||
// returns 1 = Allowed
|
// returns 1 = Allowed
|
||||||
// returns 2 = Allowed only once at start of string (for - sign in numbers)
|
// returns 2 = Allowed only once at start of string (for - sign in numbers)
|
||||||
@ -198,10 +216,10 @@ int Valid_character(word c, int input_type)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Cleanup_string(char* str, int input_type)
|
static void Cleanup_string(char* str, int input_type)
|
||||||
{
|
{
|
||||||
int i,j=0;
|
int i,j=0;
|
||||||
|
|
||||||
for(i=0; str[i]!='\0'; i++)
|
for(i=0; str[i]!='\0'; i++)
|
||||||
{
|
{
|
||||||
if (Valid_character((unsigned char)(str[i]), input_type))
|
if (Valid_character((unsigned char)(str[i]), input_type))
|
||||||
@ -213,6 +231,20 @@ void Cleanup_string(char* str, int input_type)
|
|||||||
str[j] = '\0';
|
str[j] = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void Cleanup_string_unicode(word* str, int input_type)
|
||||||
|
{
|
||||||
|
int i,j=0;
|
||||||
|
|
||||||
|
for(i=0; str[i]!=0; i++)
|
||||||
|
{
|
||||||
|
if (Valid_character(str[i], input_type))
|
||||||
|
{
|
||||||
|
str[j]=str[i];
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
str[j] = 0;
|
||||||
|
}
|
||||||
static void Display_whole_string(word x_pos,word y_pos,const char * str,byte position)
|
static void Display_whole_string(word x_pos,word y_pos,const char * str,byte position)
|
||||||
{
|
{
|
||||||
char cursor[2];
|
char cursor[2];
|
||||||
@ -265,30 +297,41 @@ void Init_virtual_keyboard(word y_pos, word keyboard_width, word keyboard_height
|
|||||||
|
|
||||||
// Inspired from http://www.libsdl.org/projects/scrap/
|
// Inspired from http://www.libsdl.org/projects/scrap/
|
||||||
// TODO X11 and others
|
// TODO X11 and others
|
||||||
char* getClipboard()
|
static char* getClipboard(word * * unicode)
|
||||||
{
|
{
|
||||||
#ifdef __WIN32__
|
#ifdef __WIN32__
|
||||||
char* dst = NULL;
|
char* dst = NULL;
|
||||||
SDL_SysWMinfo info;
|
SDL_SysWMinfo info;
|
||||||
HWND SDL_Window;
|
|
||||||
|
|
||||||
SDL_VERSION(&info.version);
|
SDL_VERSION(&info.version);
|
||||||
|
|
||||||
if ( SDL_GetWMInfo(&info) )
|
if ( SDL_GetWMInfo(&info) )
|
||||||
{
|
{
|
||||||
SDL_Window = info.window;
|
if (OpenClipboard(info.window) )
|
||||||
|
|
||||||
if ( IsClipboardFormatAvailable(CF_TEXT) && OpenClipboard(SDL_Window) )
|
|
||||||
{
|
{
|
||||||
HANDLE hMem;
|
HANDLE hMem;
|
||||||
char *src;
|
if ( IsClipboardFormatAvailable(CF_TEXT) )
|
||||||
|
|
||||||
hMem = GetClipboardData(CF_TEXT);
|
|
||||||
if ( hMem != NULL )
|
|
||||||
{
|
{
|
||||||
src = (char *)GlobalLock(hMem);
|
char *src;
|
||||||
dst = strdup(src);
|
|
||||||
GlobalUnlock(hMem);
|
hMem = GetClipboardData(CF_TEXT);
|
||||||
|
if ( hMem != NULL )
|
||||||
|
{
|
||||||
|
src = (char *)GlobalLock(hMem);
|
||||||
|
dst = strdup(src);
|
||||||
|
GlobalUnlock(hMem);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (unicode != NULL && IsClipboardFormatAvailable(CF_UNICODETEXT) )
|
||||||
|
{
|
||||||
|
word * src;
|
||||||
|
hMem = GetClipboardData(CF_UNICODETEXT);
|
||||||
|
if ( hMem != NULL )
|
||||||
|
{
|
||||||
|
src = (word *)GlobalLock(hMem);
|
||||||
|
*unicode = Unicode_strdup(src);
|
||||||
|
GlobalUnlock(hMem);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
CloseClipboard();
|
CloseClipboard();
|
||||||
}
|
}
|
||||||
@ -662,14 +705,39 @@ byte Readline_ex_unicode(word x_pos,word y_pos,char * str,word * str_unicode,byt
|
|||||||
// Handle paste request on CTRL+v
|
// Handle paste request on CTRL+v
|
||||||
if (Key == SHORTCUT_PASTE)
|
if (Key == SHORTCUT_PASTE)
|
||||||
{
|
{
|
||||||
int nb_added;
|
int nb_added = 0;
|
||||||
char* data = getClipboard();
|
if (str_unicode != NULL)
|
||||||
if (data == NULL)
|
{
|
||||||
continue; // No clipboard data
|
word * data_unicode = NULL;
|
||||||
Cleanup_string(data, input_type);
|
char * data = getClipboard(&data_unicode);
|
||||||
// Insert it at the cursor position
|
if (data_unicode != NULL)
|
||||||
nb_added = Prepend_string(str + position, data, max_size - position);//TODO : unicode
|
{
|
||||||
while (nb_added)
|
Cleanup_string_unicode(data_unicode, input_type);
|
||||||
|
nb_added = Prepend_string_unicode(str_unicode + position, data_unicode, max_size - position);
|
||||||
|
free(data_unicode);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
word tmp_unicode[256];
|
||||||
|
if (data == NULL)
|
||||||
|
continue;
|
||||||
|
Cleanup_string(data, input_type);
|
||||||
|
Unicode_char_strlcpy(tmp_unicode, data, sizeof(tmp_unicode)/sizeof(word));
|
||||||
|
nb_added = Prepend_string_unicode(str_unicode + position, tmp_unicode, max_size - position);
|
||||||
|
}
|
||||||
|
free(data);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
char* data = getClipboard(NULL);
|
||||||
|
if (data == NULL)
|
||||||
|
continue; // No clipboard data
|
||||||
|
Cleanup_string(data, input_type);
|
||||||
|
// Insert it at the cursor position
|
||||||
|
nb_added = Prepend_string(str + position, data, max_size - position);
|
||||||
|
free(data);
|
||||||
|
}
|
||||||
|
while (nb_added > 0)
|
||||||
{
|
{
|
||||||
size++;
|
size++;
|
||||||
if (size<max_size)
|
if (size<max_size)
|
||||||
@ -688,7 +756,6 @@ byte Readline_ex_unicode(word x_pos,word y_pos,char * str,word * str_unicode,byt
|
|||||||
}
|
}
|
||||||
nb_added--;
|
nb_added--;
|
||||||
}
|
}
|
||||||
free(data);
|
|
||||||
Hide_cursor();
|
Hide_cursor();
|
||||||
goto affichage;
|
goto affichage;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user