Skip to content

Commit

Permalink
InputTextMultine() optimised height calculation for inactive multi-li…
Browse files Browse the repository at this point in the history
…ne edit box (#200)
  • Loading branch information
ocornut committed Jun 19, 2015
1 parent 8666648 commit 2b68a5c
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6514,6 +6514,19 @@ bool ImGui::RadioButton(const char* label, int* v, int v_button)
return pressed;
}

static int InputTextCalcTextLenAndLineCount(const char* text_begin, const char** out_text_end)
{
int line_count = 0;
const char* s = text_begin;
while (char c = *s++) // We are only matching for \n so we can ignore UTF-8 decoding
if (c == '\n')
line_count++;
if (s[-1] != '\n' && s[-1] != '\r')
line_count++;
*out_text_end = s-1;
return line_count;
}

static ImVec2 InputTextCalcTextSizeW(const ImWchar* text_begin, const ImWchar* text_end, const ImWchar** remaining = NULL, ImVec2* out_offset = NULL, bool stop_on_new_line = false)
{
ImFont* font = GImGui->Font;
Expand Down Expand Up @@ -7168,10 +7181,10 @@ static bool InputTextEx(const char* label, char* buf, size_t buf_size, const ImV
else
{
// Render text only
draw_window->DrawList->AddText(g.Font, g.FontSize, render_pos, draw_window->Color(ImGuiCol_Text), buf, NULL, 0.0f, is_multiline ? NULL : &clip_rect);

const char* buf_end = NULL;
if (is_multiline)
text_size = g.Font->CalcTextSizeA(g.FontSize, FLT_MAX, 0.0f, buf);
text_size = ImVec2(size.x, InputTextCalcTextLenAndLineCount(buf, &buf_end) * g.FontSize); // We don't need width
draw_window->DrawList->AddText(g.Font, g.FontSize, render_pos, draw_window->Color(ImGuiCol_Text), buf, buf_end, 0.0f, is_multiline ? NULL : &clip_rect);
}

if (is_multiline)
Expand Down

0 comments on commit 2b68a5c

Please sign in to comment.