[win32] implemented string clipboard functions

This commit is contained in:
martinfouilleul 2023-03-09 19:44:55 +01:00
parent 359e08b0a8
commit 137a75540b
2 changed files with 69 additions and 17 deletions

View File

@ -2005,6 +2005,12 @@ typedef struct ui_edit_command
} ui_edit_command; } ui_edit_command;
#if OS_WIN64
#define OS_COPY_PASTE_MOD MP_KEYMOD_CTRL
#elif OS_MACOS
#define OS_COPY_PAST_MOD MP_KEYMOD_CMD
#endif
const ui_edit_command UI_EDIT_COMMANDS[] = { const ui_edit_command UI_EDIT_COMMANDS[] = {
//NOTE(martin): move one left //NOTE(martin): move one left
{ {
@ -2097,7 +2103,7 @@ const ui_edit_command UI_EDIT_COMMANDS[] = {
//NOTE(martin): select all //NOTE(martin): select all
{ {
.key = MP_KEY_Q, .key = MP_KEY_Q,
.mods = MP_KEYMOD_CMD, .mods = OS_COPY_PASTE_MOD,
.operation = UI_EDIT_SELECT_ALL, .operation = UI_EDIT_SELECT_ALL,
.move = UI_EDIT_MOVE_NONE .move = UI_EDIT_MOVE_NONE
}, },
@ -2118,21 +2124,21 @@ const ui_edit_command UI_EDIT_COMMANDS[] = {
//NOTE(martin): cut //NOTE(martin): cut
{ {
.key = MP_KEY_X, .key = MP_KEY_X,
.mods = MP_KEYMOD_CMD, .mods = OS_COPY_PASTE_MOD,
.operation = UI_EDIT_CUT, .operation = UI_EDIT_CUT,
.move = UI_EDIT_MOVE_NONE .move = UI_EDIT_MOVE_NONE
}, },
//NOTE(martin): copy //NOTE(martin): copy
{ {
.key = MP_KEY_C, .key = MP_KEY_C,
.mods = MP_KEYMOD_CMD, .mods = OS_COPY_PASTE_MOD,
.operation = UI_EDIT_COPY, .operation = UI_EDIT_COPY,
.move = UI_EDIT_MOVE_NONE .move = UI_EDIT_MOVE_NONE
}, },
//NOTE(martin): paste //NOTE(martin): paste
{ {
.key = MP_KEY_V, .key = MP_KEY_V,
.mods = MP_KEYMOD_CMD, .mods = OS_COPY_PASTE_MOD,
.operation = UI_EDIT_PASTE, .operation = UI_EDIT_PASTE,
.move = UI_EDIT_MOVE_NONE .move = UI_EDIT_MOVE_NONE
} }

View File

@ -477,6 +477,8 @@ LRESULT WinProc(HWND windowHandle, UINT message, WPARAM wParam, LPARAM lParam)
} break; } break;
case WM_CHAR: case WM_CHAR:
{
if((u32)wParam >= 32)
{ {
mp_event event = {0}; mp_event event = {0};
event.window = mp_window_handle_from_ptr(mpWindow); event.window = mp_window_handle_from_ptr(mpWindow);
@ -487,7 +489,7 @@ LRESULT WinProc(HWND windowHandle, UINT message, WPARAM wParam, LPARAM lParam)
mp_queue_event(&event); mp_queue_event(&event);
mp_update_text(event.character.codepoint); mp_update_text(event.character.codepoint);
}
} break; } break;
case WM_DROPFILES: case WM_DROPFILES:
@ -794,18 +796,62 @@ mp_rect mp_window_get_content_rect(mp_window window)
MP_API void mp_clipboard_clear(void) MP_API void mp_clipboard_clear(void)
{ {
//TODO if(OpenClipboard(NULL))
{
EmptyClipboard();
CloseClipboard();
}
} }
MP_API void mp_clipboard_set_string(str8 string) MP_API void mp_clipboard_set_string(str8 string)
{ {
//TODO if(OpenClipboard(NULL))
{
EmptyClipboard();
int wideCount = MultiByteToWideChar(CP_UTF8, 0, string.ptr, string.len, 0, 0);
HANDLE handle = GlobalAlloc(GMEM_MOVEABLE, (wideCount+1)*sizeof(wchar_t));
if(handle)
{
char* memory = GlobalLock(handle);
if(memory)
{
MultiByteToWideChar(CP_UTF8, 0, string.ptr, string.len, (wchar_t*)memory, wideCount);
((wchar_t*)memory)[wideCount] = '\0';
GlobalUnlock(handle);
SetClipboardData(CF_UNICODETEXT, handle);
}
}
CloseClipboard();
}
} }
MP_API str8 mp_clipboard_get_string(mem_arena* arena) MP_API str8 mp_clipboard_get_string(mem_arena* arena)
{ {
//TODO str8 string = {0};
return((str8){0});
if(OpenClipboard(NULL))
{
HANDLE handle = GetClipboardData(CF_UNICODETEXT);
if(handle)
{
char* memory = GlobalLock(handle);
if(memory)
{
u64 size = WideCharToMultiByte(CP_UTF8, 0, (wchar_t*)memory, -1, 0, 0, 0, 0);
if(size)
{
string.ptr = mem_arena_alloc(arena, size);
string.len = size - 1;
WideCharToMultiByte(CP_UTF8, 0, (wchar_t*)memory, -1, string.ptr, size, 0, 0);
GlobalUnlock(handle);
}
}
}
CloseClipboard();
}
return(string);
} }
MP_API str8 mp_clipboard_copy_string(str8 backing) MP_API str8 mp_clipboard_copy_string(str8 backing)