Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for GDI and GDI+ #576

Merged
merged 4 commits into from
Sep 16, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion demo/gdi/nuklear_gdi.h
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,8 @@ nk_gdi_draw_text(HDC dc, short x, short y, unsigned short w, unsigned short h,
wsize = MultiByteToWideChar(CP_UTF8, 0, text, len, NULL, 0);
wstr = (WCHAR*)_alloca(wsize * sizeof(wchar_t));
MultiByteToWideChar(CP_UTF8, 0, text, len, wstr, wsize);


SetBkMode(dc, TRANSPARENT); // Transparent Text Background
nyaruku marked this conversation as resolved.
Show resolved Hide resolved
SetBkColor(dc, convert_color(cbg));
SetTextColor(dc, convert_color(cfg));

Expand Down
133 changes: 55 additions & 78 deletions demo/gdip/nuklear_gdip.h
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,7 @@ nk_gdipfont_get_text_width(nk_handle handle, float height, const char *text, int

(void)height;
wsize = MultiByteToWideChar(CP_UTF8, 0, text, len, NULL, 0);
wstr = (WCHAR*)_alloca(wsize * sizeof(wchar_t));
wstr = (WCHAR*)_malloca(wsize * sizeof(wchar_t));
MultiByteToWideChar(CP_UTF8, 0, text, len, wstr, wsize);

GdipMeasureString(gdip.memory, wstr, wsize, font->handle, &layout, gdip.format, &bbox, NULL, NULL);
Expand All @@ -777,93 +777,63 @@ nk_gdipfont_del(GdipFont *font)
}

static void
nk_gdip_clipboard_paste(nk_handle usr, struct nk_text_edit *edit)
nk_gdip_clipboard_paste(nk_handle usr, struct nk_text_edit* edit)
{
HGLOBAL mem;
SIZE_T size;
LPCWSTR wstr;
int utf8size;
char* utf8;
(void)usr;

if (!IsClipboardFormatAvailable(CF_UNICODETEXT) && OpenClipboard(NULL))
return;

mem = (HGLOBAL)GetClipboardData(CF_UNICODETEXT);
if (!mem) {
CloseClipboard();
return;
}

size = GlobalSize(mem) - 1;
if (!size) {
CloseClipboard();
return;
}

wstr = (LPCWSTR)GlobalLock(mem);
if (!wstr) {
CloseClipboard();
return;
}

utf8size = WideCharToMultiByte(CP_UTF8, 0, wstr, (int)(size / sizeof(wchar_t)), NULL, 0, NULL, NULL);
if (!utf8size) {
GlobalUnlock(mem);
CloseClipboard();
return;
}

utf8 = (char*)malloc(utf8size);
if (!utf8) {
GlobalUnlock(mem);
if (IsClipboardFormatAvailable(CF_UNICODETEXT) && OpenClipboard(NULL))
{
HGLOBAL mem = GetClipboardData(CF_UNICODETEXT);
if (mem)
{
SIZE_T size = GlobalSize(mem) - 1;
if (size)
{
LPCWSTR wstr = (LPCWSTR)GlobalLock(mem);
if (wstr)
{
int utf8size = WideCharToMultiByte(CP_UTF8, 0, wstr, (int)(size / sizeof(wchar_t)), NULL, 0, NULL, NULL);
if (utf8size)
{
char* utf8 = (char*)malloc(utf8size);
if (utf8)
{
WideCharToMultiByte(CP_UTF8, 0, wstr, (int)(size / sizeof(wchar_t)), utf8, utf8size, NULL, NULL);
nk_textedit_paste(edit, utf8, utf8size);
free(utf8);
}
}
GlobalUnlock(mem);
}
}
}
CloseClipboard();
return;
}

WideCharToMultiByte(CP_UTF8, 0, wstr, (int)(size / sizeof(wchar_t)), utf8, utf8size, NULL, NULL);
nk_textedit_paste(edit, utf8, utf8size);
free(utf8);
GlobalUnlock(mem);
CloseClipboard();
}

static void
nk_gdip_clipboard_copy(nk_handle usr, const char *text, int len)
nk_gdip_clipboard_copy(nk_handle usr, const char* text, int len)
{
HGLOBAL mem;
wchar_t* wstr;
int wsize;
(void)usr;

if (!OpenClipboard(NULL))
return;

wsize = MultiByteToWideChar(CP_UTF8, 0, text, len, NULL, 0);
if (!wsize) {
CloseClipboard();
return;
}

mem = (HGLOBAL)GlobalAlloc(GMEM_MOVEABLE, (wsize + 1) * sizeof(wchar_t));
if (!mem) {
CloseClipboard();
return;
}

wstr = (wchar_t*)GlobalLock(mem);
if (!wstr) {
GlobalFree(mem);
if (OpenClipboard(NULL))
{
int wsize = MultiByteToWideChar(CP_UTF8, 0, text, len, NULL, 0);
if (wsize)
{
HGLOBAL mem = (HGLOBAL)GlobalAlloc(GMEM_MOVEABLE, (wsize + 1) * sizeof(wchar_t));
if (mem)
{
wchar_t* wstr = (wchar_t*)GlobalLock(mem);
if (wstr)
{
MultiByteToWideChar(CP_UTF8, 0, text, len, wstr, wsize);
wstr[wsize] = 0;
GlobalUnlock(mem);

SetClipboardData(CF_UNICODETEXT, mem);
}
}
}
CloseClipboard();
return;
}

MultiByteToWideChar(CP_UTF8, 0, text, len, wstr, wsize);
wstr[wsize] = 0;
GlobalUnlock(mem);
if (!SetClipboardData(CF_UNICODETEXT, mem))
GlobalFree(mem);
CloseClipboard();
}

NK_API struct nk_context*
Expand Down Expand Up @@ -997,6 +967,13 @@ nk_gdip_handle_event(HWND wnd, UINT msg, WPARAM wparam, LPARAM lparam)
nk_input_key(&gdip.ctx, NK_KEY_SCROLL_UP, down);
return 1;

case 'A':
if (ctrl) {
nk_input_key(&gdip.ctx, NK_KEY_TEXT_SELECT_ALL, down);
return 1;
}
break;

case 'C':
if (ctrl) {
nk_input_key(&gdip.ctx, NK_KEY_COPY, down);
Expand Down