Home key doesn't work if cursor is at the end of the buffer in InputTextMultiline() #588

Closed
opened 2016-04-13 00:47:54 +00:00 by sergof · 4 comments
sergof commented 2016-04-13 00:47:54 +00:00 (Migrated from github.com)

Simple way to reproduce:
In the "Multi-line Text Input" widget of the demo window delete the last line so that the cursor is at the end of "eax", press the home key and see nothing happen.

After looking into this for a bit it seems that the case when the cursor is at the end of the buffer (and pointing at ’\0’) is not handled correctly in the multi line mode (stb_textedit.h lines: 506-516).
Not sure if it's a problem with stb_textedit or the way it's used.

I'd try to fix this myself, but it would probably take to much time to understand how stb_textedit works and how it's used in dear imgui, so sorry for that.

Simple way to reproduce: In the "Multi-line Text Input" widget of the demo window delete the last line so that the cursor is at the end of "eax", press the home key and see nothing happen. After looking into this for a bit it seems that the case when the cursor is at the end of the buffer (and pointing at ’\0’) is not handled correctly in the multi line mode (stb_textedit.h lines: 506-516). Not sure if it's a problem with stb_textedit or the way it's used. I'd try to fix this myself, but it would probably take to much time to understand how stb_textedit works and how it's used in dear imgui, so sorry for that.
ocornut commented 2016-04-13 08:53:14 +00:00 (Migrated from github.com)

Confirmed. I'll look at it eventually. I don't know much about stb_textedit either, have to dig.

Confirmed. I'll look at it eventually. I don't know much about stb_textedit either, have to dig.
johanwendin commented 2016-07-16 13:47:09 +00:00 (Migrated from github.com)

You can get around this by editing stb_textedit.h as a temporary fix.

Search for "case STB_TEXTEDIT_K_LINESTART:"
Change it from:

case STB_TEXTEDIT_K_LINESTART: {
         StbFindState find;
         stb_textedit_clamp(str, state);
         stb_textedit_move_to_first(state);
         stb_textedit_find_charpos(&find, str, state->cursor, state->single_line);
         state->cursor = find.first_char;
         state->has_preferred_x = 0;
         break;
      }

to:

case STB_TEXTEDIT_K_LINESTART: {
         StbFindState find;
         stb_textedit_clamp(str, state);

         int n = STB_TEXTEDIT_STRINGLEN(str);
         if (state->cursor >= n) state->cursor = n-1;

         stb_textedit_move_to_first(state);
         stb_textedit_find_charpos(&find, str, state->cursor, state->single_line);
         state->cursor = find.first_char;
         state->has_preferred_x = 0;
         break;
      }

It's not the prettiest solution, but it should get you by for now. (similar fixes needed for up-arrow as well)

You can get around this by editing stb_textedit.h as a temporary fix. Search for "case STB_TEXTEDIT_K_LINESTART:" Change it from: ``` case STB_TEXTEDIT_K_LINESTART: { StbFindState find; stb_textedit_clamp(str, state); stb_textedit_move_to_first(state); stb_textedit_find_charpos(&find, str, state->cursor, state->single_line); state->cursor = find.first_char; state->has_preferred_x = 0; break; } ``` to: ``` case STB_TEXTEDIT_K_LINESTART: { StbFindState find; stb_textedit_clamp(str, state); int n = STB_TEXTEDIT_STRINGLEN(str); if (state->cursor >= n) state->cursor = n-1; stb_textedit_move_to_first(state); stb_textedit_find_charpos(&find, str, state->cursor, state->single_line); state->cursor = find.first_char; state->has_preferred_x = 0; break; } ``` It's not the prettiest solution, but it should get you by for now. (similar fixes needed for up-arrow as well)
blackhole89 commented 2016-09-03 21:56:59 +00:00 (Migrated from github.com)

It seems like the error in the logic is in stb_textedit_find_charpos here:

   if (n == z) { 
      // if it's at the end, then find the last line -- simpler than trying to
      // explicitly handle this case in the regular code

The home key handler (and some related ones, like up, which is analogously broken) all use this subroutine to locate the line they are in.

edit: The if(single_line)...else branch in there is flawed. I'll make a PR for the fix.

It seems like the error in the logic is in `stb_textedit_find_charpos` here: ``` c if (n == z) { // if it's at the end, then find the last line -- simpler than trying to // explicitly handle this case in the regular code ``` The home key handler (and some related ones, like up, which is analogously broken) all use this subroutine to locate the line they are in. edit: The `if(single_line)`...`else` branch in there is flawed. I'll make a PR for the fix.
ocornut commented 2016-10-16 12:03:22 +00:00 (Migrated from github.com)

Should be fixed now.

Should be fixed now.
Sign in to join this conversation.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
ocornut/imgui#588
No description provided.