Skip to content

Commit

Permalink
InputText() fixed multi-line selection clipping. (#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
ocornut committed Jun 17, 2015
1 parent ec7c183 commit 0795a60
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
14 changes: 8 additions & 6 deletions imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7115,33 +7115,35 @@ static bool InputTextEx(const char* label, char* buf, size_t buf_size, const ImV
const float font_offy_dn = 2.0f;
const ImVec2 render_pos = frame_bb.Min + style.FramePadding;

//const float render_scroll_x = (g.ActiveId == id) ? edit_state.ScrollX : 0.0f;
const float render_scroll_x = (edit_state.Id == id) ? edit_state.ScrollX : 0.0f;
const ImVec4 clip_rect(frame_bb.Min.x, frame_bb.Min.y, frame_bb.Min.x + size.x + style.FramePadding.x*2.0f, frame_bb.Min.y + size.y + style.FramePadding.y*2.0f);

if (g.ActiveId == id)
{
// Draw selection
const int select_begin_idx = edit_state.StbState.select_start;
const int select_end_idx = edit_state.StbState.select_end;
if (select_begin_idx != select_end_idx)
{
ImVec2 rect_pos;
ImWchar* text_selected_begin = edit_state.Text + ImMin(select_begin_idx,select_end_idx);
ImWchar* text_selected_end = edit_state.Text + ImMax(select_begin_idx,select_end_idx);
ImVec2 rect_pos;
CalcTextSizeW(edit_state.Font, edit_state.FontSize, FLT_MAX, edit_state.Text, text_selected_begin, NULL, &rect_pos);

ImU32 font_color = window->Color(ImGuiCol_TextSelectedBg);
for (const ImWchar* p = text_selected_begin; p < text_selected_end; )
{
ImVec2 rect_size = CalcTextSizeW(edit_state.Font, edit_state.FontSize, FLT_MAX, p, text_selected_end, &p, NULL, true);
window->DrawList->AddRectFilled(render_pos + rect_pos + ImVec2(-edit_state.ScrollX, -font_offy_up), render_pos + rect_pos + ImVec2(rect_size.x - edit_state.ScrollX, +font_offy_dn), font_color);
ImRect rect(render_pos + rect_pos + ImVec2(-edit_state.ScrollX, -font_offy_up), render_pos + rect_pos + ImVec2(rect_size.x - edit_state.ScrollX, +font_offy_dn));
rect.Clip(clip_rect);
window->DrawList->AddRectFilled(rect.Min, rect.Max, font_color);
rect_pos.x = 0.0f;
rect_pos.y += g.FontSize;
}
}
}

// FIMXE-WIP-MULTILINE
//const float render_scroll_x = (g.ActiveId == id) ? edit_state.ScrollX : 0.0f;
const float render_scroll_x = (edit_state.Id == id) ? edit_state.ScrollX : 0.0f;
const ImVec4 clip_rect(frame_bb.Min.x, frame_bb.Min.y, frame_bb.Min.x + size.x + style.FramePadding.x*2.0f, frame_bb.Min.y + size.y + style.FramePadding.y*2.0f);
window->DrawList->AddText(g.Font, g.FontSize, render_pos - ImVec2(render_scroll_x, 0.0f), window->Color(ImGuiCol_Text), buf, NULL, 0.0f, &clip_rect);

// Log as text
Expand Down
2 changes: 1 addition & 1 deletion imgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ enum ImGuiInputTextFlags_
ImGuiInputTextFlags_CallbackAlways = 1 << 8, // Call user function every time
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.
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).
// [Internal]
ImGuiInputTextFlags_Multiline = 1 << 20 // For internal use by InputTextMultiline()
};
Expand Down

0 comments on commit 0795a60

Please sign in to comment.