瀏覽代碼

InputText(): always use live input data for display in read-only mode (fix #360)

ocornut 10 年之前
父節點
當前提交
43ff5a41be
共有 2 個文件被更改,包括 24 次插入15 次删除
  1. 12 4
      imgui.cpp
  2. 12 11
      imgui_internal.h

+ 12 - 4
imgui.cpp

@@ -7089,7 +7089,7 @@ static bool InputTextFilterCharacter(unsigned int* p_char, ImGuiInputTextFlags f
 }
 }
 
 
 // Edit a string of text
 // Edit a string of text
-// FIXME: This is rather complex partly because we are doing UTF8 > u16 > UTF8 conversions on the go to more easily handle stb_textedit calls. Ideally we should stay in UTF8 space all the time. 
+// FIXME: This is rather complex partly because we are doing UTF8 > u16 > UTF8 conversions on the go to more easily handle stb_textedit calls. Ideally we should stay in UTF-8 all the time. 
 bool ImGui::InputTextEx(const char* label, char* buf, int buf_size, const ImVec2& size_arg, ImGuiInputTextFlags flags, ImGuiTextEditCallback callback, void* user_data)
 bool ImGui::InputTextEx(const char* label, char* buf, int buf_size, const ImVec2& size_arg, ImGuiInputTextFlags flags, ImGuiTextEditCallback callback, void* user_data)
 {
 {
     ImGuiWindow* window = GetCurrentWindow();
     ImGuiWindow* window = GetCurrentWindow();
@@ -7177,9 +7177,7 @@ bool ImGui::InputTextEx(const char* label, char* buf, int buf_size, const ImVec2
             {
             {
                 // Recycle existing cursor/selection/undo stack but clamp position
                 // Recycle existing cursor/selection/undo stack but clamp position
                 // Note a single mouse click will override the cursor/position immediately by calling stb_textedit_click handler.
                 // Note a single mouse click will override the cursor/position immediately by calling stb_textedit_click handler.
-                edit_state.StbState.cursor = ImMin(edit_state.StbState.cursor, edit_state.CurLenW);
-                edit_state.StbState.select_start = ImMin(edit_state.StbState.select_start, edit_state.CurLenW);
-                edit_state.StbState.select_end = ImMin(edit_state.StbState.select_end, edit_state.CurLenW);
+                edit_state.CursorClamp();
             }
             }
             else
             else
             {
             {
@@ -7210,6 +7208,16 @@ bool ImGui::InputTextEx(const char* label, char* buf, int buf_size, const ImVec2
 
 
     if (g.ActiveId == id)
     if (g.ActiveId == id)
     {
     {
+        if (!is_editable && !g.ActiveIdIsJustActivated)
+        {
+            // When read-only we always use the live data passed to the function
+            edit_state.Text.resize(buf_size+1);
+            const char* buf_end = NULL;
+            edit_state.CurLenW = ImTextStrFromUtf8(edit_state.Text.Data, edit_state.Text.Size, buf, NULL, &buf_end);
+            edit_state.CurLenA = (int)(buf_end - buf);
+            edit_state.CursorClamp();
+        }
+
         edit_state.BufSizeA = buf_size;
         edit_state.BufSizeA = buf_size;
 
 
         // Although we are active we don't prevent mouse from hovering other elements unless we are interacting right now with the widget.
         // Although we are active we don't prevent mouse from hovering other elements unless we are interacting right now with the widget.

+ 12 - 11
imgui_internal.h

@@ -270,24 +270,25 @@ struct IMGUI_API ImGuiSimpleColumns
 // Internal state of the currently focused/edited text input box
 // Internal state of the currently focused/edited text input box
 struct IMGUI_API ImGuiTextEditState
 struct IMGUI_API ImGuiTextEditState
 {
 {
-    ImGuiID             Id;                             // widget id owning the text state
-    ImVector<ImWchar>   Text;                           // edit buffer, we need to persist but can't guarantee the persistence of the user-provided buffer. so we copy into own buffer.
-    ImVector<char>      InitialText;                    // backup of end-user buffer at the time of focus (in UTF-8, unaltered)
+    ImGuiID             Id;                         // widget id owning the text state
+    ImVector<ImWchar>   Text;                       // edit buffer, we need to persist but can't guarantee the persistence of the user-provided buffer. so we copy into own buffer.
+    ImVector<char>      InitialText;                // backup of end-user buffer at the time of focus (in UTF-8, unaltered)
     ImVector<char>      TempTextBuffer;
     ImVector<char>      TempTextBuffer;
-    int                 CurLenA, CurLenW;               // we need to maintain our buffer length in both UTF-8 and wchar format.
-    int                 BufSizeA;                       // end-user buffer size
+    int                 CurLenA, CurLenW;           // we need to maintain our buffer length in both UTF-8 and wchar format.
+    int                 BufSizeA;                   // end-user buffer size
     float               ScrollX;
     float               ScrollX;
     ImGuiStb::STB_TexteditState   StbState;
     ImGuiStb::STB_TexteditState   StbState;
     float               CursorAnim;
     float               CursorAnim;
     bool                CursorFollow;
     bool                CursorFollow;
-    ImVec2              InputCursorScreenPos;           // Cursor position in screen space to be used by IME callback.
+    ImVec2              InputCursorScreenPos;       // Cursor position in screen space to be used by IME callback.
     bool                SelectedAllMouseLock;
     bool                SelectedAllMouseLock;
 
 
-    ImGuiTextEditState()                                { memset(this, 0, sizeof(*this)); }
-    void                CursorAnimReset()               { CursorAnim = -0.30f; }                                   // After a user-input the cursor stays on for a while without blinking
-    bool                HasSelection() const            { return StbState.select_start != StbState.select_end; }
-    void                ClearSelection()                { StbState.select_start = StbState.select_end = StbState.cursor; }
-    void                SelectAll()                     { StbState.select_start = 0; StbState.select_end = CurLenW; StbState.cursor = StbState.select_end; StbState.has_preferred_x = false; }
+    ImGuiTextEditState()                            { memset(this, 0, sizeof(*this)); }
+    void                CursorAnimReset()           { CursorAnim = -0.30f; }                                   // After a user-input the cursor stays on for a while without blinking
+    void                CursorClamp()               { StbState.cursor = ImMin(StbState.cursor, CurLenW); StbState.select_start = ImMin(StbState.select_start, CurLenW); StbState.select_end = ImMin(StbState.select_end, CurLenW); }
+    bool                HasSelection() const        { return StbState.select_start != StbState.select_end; }
+    void                ClearSelection()            { StbState.select_start = StbState.select_end = StbState.cursor; }
+    void                SelectAll()                 { StbState.select_start = 0; StbState.select_end = CurLenW; StbState.cursor = StbState.select_end; StbState.has_preferred_x = false; }
     void                OnKeyPressed(int key);
     void                OnKeyPressed(int key);
 };
 };