diff --git a/include/nana/gui/widgets/skeletons/text_editor.hpp b/include/nana/gui/widgets/skeletons/text_editor.hpp index 4ebee6ec..22e099e4 100644 --- a/include/nana/gui/widgets/skeletons/text_editor.hpp +++ b/include/nana/gui/widgets/skeletons/text_editor.hpp @@ -219,7 +219,7 @@ namespace nana{ namespace widgets * @param pos the text position * @param reset indicates whether to reset the text position by the pos. If this parameter is true, the text position is set by pos. If the parameter is false, it only moves the UI caret to the specified position. */ - void move_caret(const upoint& pos, bool reset = false); + bool move_caret(const upoint& pos, bool reset = false); void move_caret_end(); void reset_caret_pixels() const; void reset_caret(); diff --git a/source/gui/widgets/skeletons/text_editor.cpp b/source/gui/widgets/skeletons/text_editor.cpp index b7a4cda0..a030471a 100644 --- a/source/gui/widgets/skeletons/text_editor.cpp +++ b/source/gui/widgets/skeletons/text_editor.cpp @@ -341,6 +341,9 @@ namespace nana{ namespace widgets text_ptr = &mask_str; } + if (pos.x > text_ptr->size()) + pos.x = text_ptr->size(); + pos.x = editor_._m_pixels_by_char(*text_ptr, pos.x) + editor_.text_area_.area.x; int pos_y = static_cast((pos.y - editor_.points_.offset.y) * editor_.line_height() + editor_._m_text_top_base()); @@ -1840,18 +1843,18 @@ namespace nana{ namespace widgets //move_caret //Set caret position through text coordinate - void text_editor::move_caret(const upoint& crtpos, bool reset_caret) - { - if (reset_caret) - points_.caret = crtpos; - - if (!API::is_focus_ready(window_)) - return; - + bool text_editor::move_caret(const upoint& crtpos, bool reset_caret) + { const unsigned line_pixels = line_height(); auto pos = this->behavior_->caret_to_screen(crtpos); const int line_bottom = pos.y + static_cast(line_pixels); + if (reset_caret) + points_.caret = this->behavior_->screen_to_caret(pos); + + if (!API::is_focus_ready(window_)) + return false; + bool visible = false; if (hit_text_area(pos) && (line_bottom > text_area_.area.y)) { @@ -1868,6 +1871,16 @@ namespace nana{ namespace widgets caret_->visible(visible); if(visible) caret_->position(pos); + + //Adjust the caret into screen when the caret position is modified by this function + if (reset_caret && (!hit_text_area(pos))) + { + behavior_->adjust_caret_into_screen(); + render(true); + caret_->visible(true); + return true; + } + return false; } void text_editor::move_caret_end() diff --git a/source/gui/widgets/textbox.cpp b/source/gui/widgets/textbox.cpp index 38d0d1c7..76aed3ac 100644 --- a/source/gui/widgets/textbox.cpp +++ b/source/gui/widgets/textbox.cpp @@ -344,8 +344,8 @@ namespace drawerbase { { auto editor = get_drawer_trigger().editor(); internal_scope_guard lock; - if (editor) - editor->move_caret(pos, true); + if (editor && editor->move_caret(pos, true)) + API::refresh_window(handle()); return *this; }