From 855ab96aaa42ce06e6d38c508440482146241bf9 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sun, 26 May 2019 20:25:47 +1000 Subject: [PATCH] Fix launcher clipboard pasting not respecting caret position, fixes #586 (Thanks igor725) --- src/LWidgets.c | 23 +++++++++++++++++------ src/LWidgets.h | 2 -- src/Platform.c | 20 +++++++++++--------- src/Window.c | 7 ++----- 4 files changed, 30 insertions(+), 22 deletions(-) diff --git a/src/LWidgets.c b/src/LWidgets.c index 7c2eff55f..3efa3c1e7 100644 --- a/src/LWidgets.c +++ b/src/LWidgets.c @@ -455,9 +455,7 @@ void LInput_SetText(struct LInput* w, const String* text_) { w->_TextHeight = size.Height; } -bool LInput_Append(struct LInput* w, char c) { - if (w->TextFilter && !w->TextFilter(c)) return false; - +static CC_NOINLINE bool LInput_AppendRaw(struct LInput* w, char c) { if (c >= ' ' && c <= '~' && c != '&' && w->Text.length < w->Text.capacity) { if (w->CaretPos == -1) { String_Append(&w->Text, c); @@ -465,12 +463,26 @@ bool LInput_Append(struct LInput* w, char c) { String_InsertAt(&w->Text, w->CaretPos, c); w->CaretPos++; } - if (w->TextChanged) w->TextChanged(w); return true; } return false; } +bool LInput_Append(struct LInput* w, char c) { + bool appended = LInput_AppendRaw(w, c); + if (appended && w->TextChanged) w->TextChanged(w); + return appended; +} + +static void LInput_AppendString(struct LInput* w, const String* str) { + int i, appended = 0; + for (i = 0; i < str->length; i++) { + if (LInput_AppendRaw(w, str->buffer[i])) appended++; + } + + if (appended && w->TextChanged) w->TextChanged(w); +} + bool LInput_Backspace(struct LInput* w) { if (w->Text.length == 0 || w->CaretPos == 0) return false; @@ -519,8 +531,7 @@ bool LInput_CopyFromClipboard(struct LInput* w) { if (!text.length) return false; if (w->ClipboardFilter) w->ClipboardFilter(&text); - String_AppendString(&w->Text, &text); - if (w->TextChanged) w->TextChanged(w); + LInput_AppendString(w, &text); return true; } diff --git a/src/LWidgets.h b/src/LWidgets.h index ad3fef00b..e38b8ca6a 100644 --- a/src/LWidgets.h +++ b/src/LWidgets.h @@ -71,8 +71,6 @@ struct LInput { void (*ClipboardFilter)(String* str); /* Callback invoked when the text is changed. Can be NULL. */ void (*TextChanged)(struct LInput* w); - /* Filter that only lets certain characters be entered. Can be NULL. */ - bool (*TextFilter)(char c); /* Specifies the position that characters are inserted/deleted from. */ /* NOTE: -1 to insert/delete characters at end of the text. */ int CaretPos; diff --git a/src/Platform.c b/src/Platform.c index ba01fca22..5eff7b743 100644 --- a/src/Platform.c +++ b/src/Platform.c @@ -842,8 +842,8 @@ struct FontData { FT_StreamRec stream; uint8_t buffer[8192]; /* small buffer to minimise disk I/O */ uint16_t widths[256]; /* cached width of each character glyph */ - FT_Glyph glyphs[256]; /* cached glyphs */ - FT_Glyph shadow_glyphs[256]; /* cached glyphs (for back layer shadow) */ + FT_BitmapGlyph glyphs[256]; /* cached glyphs */ + FT_BitmapGlyph shadow_glyphs[256]; /* cached glyphs (for back layer shadow) */ #ifdef CC_BUILD_OSX char filename[FILENAME_SIZE + 1]; #endif @@ -871,11 +871,11 @@ static void FontData_Free(struct FontData* font) { for (i = 0; i < 256; i++) { if (!font->glyphs[i]) continue; - FT_Done_Glyph(font->glyphs[i]); + FT_Done_Glyph((FT_Glyph)font->glyphs[i]); } for (i = 0; i < 256; i++) { if (!font->shadow_glyphs[i]) continue; - FT_Done_Glyph(font->shadow_glyphs[i]); + FT_Done_Glyph((FT_Glyph)font->shadow_glyphs[i]); } } @@ -1164,10 +1164,11 @@ static void Platform_BlackWhiteGlyph(FT_Bitmap* img, Bitmap* bmp, int x, int y, } int Platform_TextDraw(struct DrawTextArgs* args, Bitmap* bmp, int x, int y, BitmapCol col, bool shadow) { - struct FontData* data = (struct FontData*)args->Font.Handle; - FT_Face face = data->face; - String text = args->Text; - FT_Glyph* glyphs = data->glyphs; + struct FontData* data = (struct FontData*)args->Font.Handle; + FT_BitmapGlyph* glyphs = data->glyphs; + + FT_Face face = data->face; + String text = args->Text; int descender, height, begX = x; /* glyph state */ @@ -1197,7 +1198,8 @@ int Platform_TextDraw(struct DrawTextArgs* args, Bitmap* bmp, int x, int y, Bitm continue; } - FT_Get_Glyph(face->glyph, &glyph); /* TODO: Check error */ + /* due to FT_LOAD_RENDER, glyph is always a bitmap one */ + FT_Get_Glyph(face->glyph, (FT_Glyph)&glyph); /* TODO: Check error */ glyphs[(uint8_t)text.buffer[i]] = glyph; } diff --git a/src/Window.c b/src/Window.c index 3807acc44..b76e3d390 100644 --- a/src/Window.c +++ b/src/Window.c @@ -738,11 +738,8 @@ void Window_DisableRawMouse(void) { static Display* win_display; static int win_screen; -static Window win_rootWin; - -static Window win_handle; +static Window win_rootWin, win_handle; static XVisualInfo win_visual; -static int borderLeft, borderRight, borderTop, borderBottom; static Atom wm_destroy, net_wm_state; static Atom net_wm_state_minimized; @@ -751,7 +748,7 @@ static Atom net_wm_state_maximized_horizontal; static Atom net_wm_state_maximized_vertical; static Atom xa_clipboard, xa_targets, xa_utf8_string, xa_data_sel; -static Atom xa_atom = 4, xa_cardinal = 6; +static Atom xa_atom = 4; static long win_eventMask;