Skip to content

Commit e900571

Browse files
committed
InputText: Fixed issue when activating a ReadOnly field when the underlying value is being modified. (#8242)
1 parent f31d530 commit e900571

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

docs/CHANGELOG.txt

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ Other changes:
4545

4646
- InputText: Fixed a bug where character replacements performed from a callback
4747
were not applied when pasting from clipbard. (#8229)
48+
- InputText: Fixed issue when activating a ReadOnly field when the underlying
49+
value is being modified. (#8242)
4850
- Drags: Added ImGuiSliderFlags_NoSpeedTweaks flag to disable keyboard
4951
modifiers altering the tweak speed. Useful if you want to alter tweak speed
5052
yourself based on your own logic. (#8223)

imgui.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
// Library Version
3030
// (Integer encoded as XYYZZ for use in #if preprocessor conditionals, e.g. '#if IMGUI_VERSION_NUM >= 12345')
3131
#define IMGUI_VERSION "1.91.7 WIP"
32-
#define IMGUI_VERSION_NUM 19161
32+
#define IMGUI_VERSION_NUM 19162
3333
#define IMGUI_HAS_TABLE
3434

3535
/*

imgui_widgets.cpp

+34-7
Original file line numberDiff line numberDiff line change
@@ -4507,12 +4507,13 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_
45074507
const bool init_changed_specs = (state != NULL && state->Stb->single_line != !is_multiline); // state != NULL means its our state.
45084508
const bool init_make_active = (user_clicked || user_scroll_finish || input_requested_by_nav);
45094509
const bool init_state = (init_make_active || user_scroll_active);
4510+
bool readonly_swapped_text_data = false;
45104511
if (init_reload_from_user_buf)
45114512
{
45124513
int new_len = (int)strlen(buf);
45134514
state->WantReloadUserBuf = false;
45144515
InputTextReconcileUndoState(state, state->TextA.Data, state->TextLen, buf, new_len);
4515-
state->TextA.resize(buf_size + 1); // we use +1 to make sure that .Data is always pointing to at least an empty string.
4516+
state->TextA.resize(buf_size + 1); // we use +1 to make sure that .Data is always pointing to at least an empty string.
45164517
state->TextLen = new_len;
45174518
memcpy(state->TextA.Data, buf, state->TextLen + 1);
45184519
state->Stb->select_start = state->ReloadSelectionStart;
@@ -4537,14 +4538,17 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_
45374538
// Preserve cursor position and undo/redo stack if we come back to same widget
45384539
// FIXME: Since we reworked this on 2022/06, may want to differentiate recycle_cursor vs recycle_undostate?
45394540
bool recycle_state = (state->ID == id && !init_changed_specs);
4540-
if (recycle_state && (state->TextLen != buf_len || (strncmp(state->TextA.Data, buf, buf_len) != 0)))
4541+
if (recycle_state && (state->TextLen != buf_len || (state->TextA.Data == NULL || strncmp(state->TextA.Data, buf, buf_len) != 0)))
45414542
recycle_state = false;
45424543

45434544
// Start edition
45444545
state->ID = id;
4545-
state->TextA.resize(buf_size + 1); // we use +1 to make sure that .Data is always pointing to at least an empty string.
45464546
state->TextLen = (int)strlen(buf);
4547-
memcpy(state->TextA.Data, buf, state->TextLen + 1);
4547+
if (!is_readonly)
4548+
{
4549+
state->TextA.resize(buf_size + 1); // we use +1 to make sure that .Data is always pointing to at least an empty string.
4550+
memcpy(state->TextA.Data, buf, state->TextLen + 1);
4551+
}
45484552

45494553
// Find initial scroll position for right alignment
45504554
state->Scroll = ImVec2(0.0f, 0.0f);
@@ -4610,6 +4614,21 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_
46104614
state->Scroll.y = draw_window->Scroll.y;
46114615
}
46124616

4617+
if (g.ActiveId == id && is_readonly)
4618+
{
4619+
// FIXME: Refresh buffer because cursor/selection code uses that data (see repro #8242)
4620+
// The "simple" way would be to copy buf into state->TextA, like in the block above.
4621+
// But because we like to live dangerously, we do a little swap....
4622+
// Removing the swap and only doing a TextA.clear() is a way to identify who's using TextA.Data.
4623+
state->TextLen = (int)strlen(buf);
4624+
state->TextA.clear();
4625+
state->TextA.Data = buf; // Ouch
4626+
state->TextA.Size = state->TextLen + 1;
4627+
readonly_swapped_text_data = true; // Need to always ensure that every code path below lead to this being handled
4628+
//state->TextA.resize(buf_size + 1);
4629+
//memcpy(state->TextA.Data, buf, state->TextLen + 1);
4630+
}
4631+
46134632
// We have an edge case if ActiveId was set through another widget (e.g. widget being swapped), clear id immediately (don't wait until the end of the function)
46144633
if (g.ActiveId == id && state == NULL)
46154634
ClearActiveID();
@@ -5008,10 +5027,11 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_
50085027
callback_data.UserData = callback_user_data;
50095028

50105029
// FIXME-OPT: Undo stack reconcile needs a backup of the data until we rework API, see #7925
5030+
char* callback_buf = is_readonly ? buf : state->TextA.Data;
5031+
50115032
state->CallbackTextBackup.resize(state->TextLen + 1);
5012-
memcpy(state->CallbackTextBackup.Data, state->TextA.Data, state->TextLen + 1);
5033+
memcpy(state->CallbackTextBackup.Data, callback_buf, state->TextLen + 1);
50135034

5014-
char* callback_buf = is_readonly ? buf : state->TextA.Data;
50155035
callback_data.EventKey = event_key;
50165036
callback_data.Buf = callback_buf;
50175037
callback_data.BufTextLen = state->TextLen;
@@ -5142,7 +5162,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_
51425162
// - Measure text height (for scrollbar)
51435163
// We are attempting to do most of that in **one main pass** to minimize the computation cost (non-negligible for large amount of text) + 2nd pass for selection rendering (we could merge them by an extra refactoring effort)
51445164
// FIXME: This should occur on buf_display but we'd need to maintain cursor/select_start/select_end for UTF-8.
5145-
const char* text_begin = state->TextA.Data;
5165+
const char* text_begin = buf_display;
51465166
const char* text_end = text_begin + state->TextLen;
51475167
ImVec2 cursor_offset, select_start_offset;
51485168

@@ -5304,6 +5324,13 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_
53045324
if (is_password && !is_displaying_hint)
53055325
PopFont();
53065326

5327+
if (readonly_swapped_text_data)
5328+
{
5329+
IM_ASSERT(state->TextA.Data == buf);
5330+
state->TextA.Size = 0;
5331+
state->TextA.Data = NULL;
5332+
}
5333+
53075334
if (is_multiline)
53085335
{
53095336
// For focus requests to work on our multiline we need to ensure our child ItemAdd() call specifies the ImGuiItemFlags_Inputable (see #4761, #7870)...

0 commit comments

Comments
 (0)