From d2701727b9475c923749b6eac7177f5e957d3e38 Mon Sep 17 00:00:00 2001 From: ocornut Date: Fri, 10 Jul 2015 18:17:46 -0600 Subject: [PATCH] InputText: added ImGuiInputTextFlags_NoHorizontalScroll flag. Added HasSelection() helper in ImGuiTextEditCallbackData as a clarification. --- imgui.cpp | 17 ++++++++++++----- imgui.h | 2 ++ 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/imgui.cpp b/imgui.cpp index 284782ea..6034d1fc 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -7419,11 +7419,18 @@ static bool InputTextEx(const char* label, char* buf, int buf_size, const ImVec2 if (edit_state.CursorFollow) { // Horizontal scroll in chunks of quarter width - const float scroll_increment_x = size.x * 0.25f; - if (cursor_offset.x < edit_state.ScrollX) - edit_state.ScrollX = ImMax(0.0f, cursor_offset.x - scroll_increment_x); - else if (cursor_offset.x - size.x >= edit_state.ScrollX) - edit_state.ScrollX = cursor_offset.x - size.x + scroll_increment_x; + if (!(flags & ImGuiInputTextFlags_NoHorizontalScroll)) + { + const float scroll_increment_x = size.x * 0.25f; + if (cursor_offset.x < edit_state.ScrollX) + edit_state.ScrollX = ImMax(0.0f, cursor_offset.x - scroll_increment_x); + else if (cursor_offset.x - size.x >= edit_state.ScrollX) + edit_state.ScrollX = cursor_offset.x - size.x + scroll_increment_x; + } + else + { + edit_state.ScrollX = 0.0f; + } // Vertical scroll if (is_multiline) diff --git a/imgui.h b/imgui.h index 9c5eb404..f174841b 100644 --- a/imgui.h +++ b/imgui.h @@ -461,6 +461,7 @@ enum ImGuiInputTextFlags_ ImGuiInputTextFlags_CallbackCharFilter = 1 << 9, // Call user function to filter character. Modify data->EventChar to replace/filter input, or return 1 to discard character. ImGuiInputTextFlags_AllowTabInput = 1 << 10, // Pressing TAB input a '\t' character into the text field ImGuiInputTextFlags_CtrlEnterForNewLine = 1 << 11, // In multi-line mode, allow exiting edition by pressing Enter. Ctrl+Enter to add new line (by default adds new lines with Enter). + ImGuiInputTextFlags_NoHorizontalScroll = 1 << 12, // Disable following the cursor horizontally // [Internal] ImGuiInputTextFlags_Multiline = 1 << 20 // For internal use by InputTextMultiline() }; @@ -919,6 +920,7 @@ struct ImGuiTextEditCallbackData // NB: calling those function loses selection. void DeleteChars(int pos, int bytes_count); void InsertChars(int pos, const char* text, const char* text_end = NULL); + bool HasSelection() const { return SelectionStart != SelectionEnd; } }; // ImColor() is just a helper that implicity converts to either ImU32 (packed 4x1 byte) or ImVec4 (4x1 float)