Skip to content

Commit

Permalink
InputText: more tidying up of selection search loop.
Browse files Browse the repository at this point in the history
  • Loading branch information
ocornut committed Sep 16, 2024
1 parent aef07ae commit 7ac50bf
Showing 1 changed file with 16 additions and 24 deletions.
40 changes: 16 additions & 24 deletions imgui_widgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5099,43 +5099,35 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_
ImVec2 cursor_offset, select_start_offset;

{
// Find lines numbers straddling 'cursor' (slot 0) and 'select_start' (slot 1) positions.
int searches_result_line_no[2] = { -1000, -1000 };
const char* searches_input_ptr[2] = { NULL, NULL };
if (render_cursor)
{
searches_input_ptr[0] = text_begin + state->Stb->cursor;
searches_result_line_no[0] = -1;
}
if (render_selection)
{
searches_input_ptr[1] = text_begin + ImMin(state->Stb->select_start, state->Stb->select_end);
searches_result_line_no[1] = -1;
}
// Find lines numbers straddling cursor and selection min position
int cursor_line_no = render_cursor ? -1 : -1000;
int selmin_line_no = render_selection ? -1 : -1000;
const char* cursor_ptr = render_cursor ? text_begin + state->Stb->cursor : NULL;
const char* selmin_ptr = render_selection ? text_begin + ImMin(state->Stb->select_start, state->Stb->select_end) : NULL;

// Count lines and find line number for cursor and selection ends
int line_count = 1;
if (is_multiline)
{
for (const char* s = text_begin; (s = (const char*)memchr(s, '\n', (size_t)(text_end - s))) != NULL; s++)
{
if (searches_result_line_no[0] == -1 && s >= searches_input_ptr[0]) { searches_result_line_no[0] = line_count; }
if (searches_result_line_no[1] == -1 && s >= searches_input_ptr[1]) { searches_result_line_no[1] = line_count; }
if (cursor_line_no == -1 && s >= cursor_ptr) { cursor_line_no = line_count; }
if (selmin_line_no == -1 && s >= selmin_ptr) { selmin_line_no = line_count; }
line_count++;
}
}
if (searches_result_line_no[0] == -1)
searches_result_line_no[0] = line_count;
if (searches_result_line_no[1] == -1)
searches_result_line_no[1] = line_count;
if (cursor_line_no == -1)
cursor_line_no = line_count;
if (selmin_line_no == -1)
selmin_line_no = line_count;

// Calculate 2d position by finding the beginning of the line and measuring distance
cursor_offset.x = InputTextCalcTextSize(&g, ImStrbol(searches_input_ptr[0], text_begin), searches_input_ptr[0]).x;
cursor_offset.y = searches_result_line_no[0] * g.FontSize;
if (searches_result_line_no[1] >= 0)
cursor_offset.x = InputTextCalcTextSize(&g, ImStrbol(cursor_ptr, text_begin), cursor_ptr).x;
cursor_offset.y = cursor_line_no * g.FontSize;
if (selmin_line_no >= 0)
{
select_start_offset.x = InputTextCalcTextSize(&g, ImStrbol(searches_input_ptr[1], text_begin), searches_input_ptr[1]).x;
select_start_offset.y = searches_result_line_no[1] * g.FontSize;
select_start_offset.x = InputTextCalcTextSize(&g, ImStrbol(selmin_ptr, text_begin), selmin_ptr).x;
select_start_offset.y = selmin_line_no * g.FontSize;
}

// Store text height (note that we haven't calculated text width at all, see GitHub issues #383, #1224)
Expand Down

0 comments on commit 7ac50bf

Please sign in to comment.