diff --git a/src/gui/value/text.cpp b/src/gui/value/text.cpp index a58c3d7b..04b46447 100644 --- a/src/gui/value/text.cpp +++ b/src/gui/value/text.cpp @@ -224,7 +224,15 @@ void TextValueEditor::onMenu(wxCommandEvent& ev) { void TextValueEditor::draw(RotatedDC& dc) { TextValueViewer::draw(dc); - v.drawSelection(dc, style(), selection_start, selection_end); + if (isCurrent()) { + v.drawSelection(dc, style(), selection_start, selection_end); + + // show caret, onAction() would be a better place + // but it has to be done after the viewer has updated the TextViewer + // we could do that ourselfs, but we need a dc for that + fixSelection(); + showCaret(); + } // DEBUG, TODO: REMOVEME Rotater r(dc, style().getRotation()); /*dc.SetPen(*wxRED_PEN); @@ -269,8 +277,6 @@ void TextValueEditor::onAction(const ValueAction& action, bool undone) { TYPE_CASE(action, TextValueAction) { selection_start = action.selection_start; selection_end = action.selection_end; - fixSelection(); - if (isCurrent()) showCaret(); } } @@ -437,6 +443,8 @@ void TextValueEditor::replaceSelection(const String& replacement, const String& moveSelection(selection_start); return; } + // perform the action + // NOTE: this calls our onAction, invalidating the text viewer and moving the selection around the new text getSet().actions.add(action); // move cursor if (field().move_cursor_with_sort && replacement.size() == 1) { diff --git a/src/render/text/viewer.cpp b/src/render/text/viewer.cpp index 173b468e..dc4f1267 100644 --- a/src/render/text/viewer.cpp +++ b/src/render/text/viewer.cpp @@ -64,13 +64,9 @@ TextViewer::~TextViewer() {} // ----------------------------------------------------------------------------- : Drawing -void TextViewer::draw(RotatedDC& dc, const String& text, const TextStyle& style, Context& ctx, DrawWhat what) { +void TextViewer::draw(RotatedDC& dc, const TextStyle& style, DrawWhat what) { + assert(!lines.empty()); Rotater r(dc, style.getRotation()); - if (lines.empty()) { - // not prepared yet - prepareElements(text, style, ctx); - prepareLines(dc, text, style); - } // Draw the text line by line FOR_EACH(l, lines) { if (l.visible(dc)) { @@ -102,6 +98,14 @@ void TextViewer::Line::drawSelection(RotatedDC& dc, size_t sel_start, size_t sel } } +void TextViewer::prepare(RotatedDC& dc, const String& text, const TextStyle& style, Context& ctx) { + if (lines.empty()) { + // not prepared yet + Rotater r(dc, style.getRotation()); + prepareElements(text, style, ctx); + prepareLines(dc, text, style); + } +} void TextViewer::reset() { elements.clear(); lines.clear(); diff --git a/src/render/text/viewer.hpp b/src/render/text/viewer.hpp index 66844b21..532e04ba 100644 --- a/src/render/text/viewer.hpp +++ b/src/render/text/viewer.hpp @@ -47,10 +47,12 @@ class TextViewer { /** The drawing information is cached, * before calling draw again with different text/style reset() should be called */ - void draw(RotatedDC& dc, const String& text, const TextStyle& style, Context&, DrawWhat); + void draw(RotatedDC& dc, const TextStyle& style, DrawWhat what); /// Draw an indicator for selected text void drawSelection(RotatedDC& dc, const TextStyle& style, size_t sel_start, size_t sel_end); + /// Prepare the text for drawing, if it is not already prepared + void prepare(RotatedDC& dc, const String& text, const TextStyle& style, Context&); /// Reset the cached data, at a new call to draw it will be recalculated void reset(); diff --git a/src/render/value/text.cpp b/src/render/value/text.cpp index 6ea5b665..2ca7bdd0 100644 --- a/src/render/value/text.cpp +++ b/src/render/value/text.cpp @@ -13,7 +13,8 @@ void TextValueViewer::draw(RotatedDC& dc) { drawFieldBorder(dc); - v.draw(dc, value().value(), style(), viewer.getContext(), DRAW_NORMAL); + v.prepare(dc, value().value(), style(), viewer.getContext()); + v.draw(dc, style(), DRAW_NORMAL); } void TextValueViewer::onValueChange() {