diff --git a/include/nana/gui/widgets/skeletons/text_editor.hpp b/include/nana/gui/widgets/skeletons/text_editor.hpp index 9fc39e69..41812395 100644 --- a/include/nana/gui/widgets/skeletons/text_editor.hpp +++ b/include/nana/gui/widgets/skeletons/text_editor.hpp @@ -231,8 +231,9 @@ namespace nana{ namespace widgets void move_ns(bool to_north); //Moves up and down void move_left(); void move_right(); - nana::upoint mouse_caret(const point& screen_pos); - nana::upoint caret() const; + upoint mouse_caret(const point& screen_pos); + upoint caret() const; + point caret_screen_pos() const; bool scroll(bool upwards, bool vertical); bool mouse_enter(bool); bool mouse_down(::nana::mouse, const point& screen_pos); diff --git a/include/nana/gui/widgets/textbox.hpp b/include/nana/gui/widgets/textbox.hpp index 1d46d33b..85bac2a6 100644 --- a/include/nana/gui/widgets/textbox.hpp +++ b/include/nana/gui/widgets/textbox.hpp @@ -144,7 +144,10 @@ namespace nana bool saved() const; /// Read the text from a specified line. It returns true for success. - bool getline(std::size_t line_index, nana::string&) const; + bool getline(std::size_t pos, nana::string&) const; + + /// Gets the caret position + bool caret_pos(point& pos, bool text_coordinate) const; /// Appends an string. If `at_caret` is `true`, the string is inserted at the position of caret, otherwise, it is appended at end of the textbox. textbox& append(const nana::string& text, bool at_caret); diff --git a/source/gui/widgets/skeletons/text_editor.cpp b/source/gui/widgets/skeletons/text_editor.cpp index 29893989..e568fbee 100644 --- a/source/gui/widgets/skeletons/text_editor.cpp +++ b/source/gui/widgets/skeletons/text_editor.cpp @@ -2359,7 +2359,7 @@ namespace nana{ namespace widgets } } - nana::upoint text_editor::mouse_caret(const point& scrpos) //From screen position + upoint text_editor::mouse_caret(const point& scrpos) //From screen position { points_.caret = behavior_->screen_to_caret(scrpos); @@ -2370,11 +2370,16 @@ namespace nana{ namespace widgets return points_.caret; } - nana::upoint text_editor::caret() const + upoint text_editor::caret() const { return points_.caret; } + point text_editor::caret_screen_pos() const + { + return behavior_->caret_to_screen(points_.caret); + } + bool text_editor::scroll(bool upwards, bool vert) { if(vert && attributes_.vscroll) diff --git a/source/gui/widgets/textbox.cpp b/source/gui/widgets/textbox.cpp index 5f600f1c..59e51d57 100644 --- a/source/gui/widgets/textbox.cpp +++ b/source/gui/widgets/textbox.cpp @@ -294,6 +294,26 @@ namespace drawerbase { return (editor ? editor->getline(line_index, text) : false); } + /// Gets the caret position + bool textbox::caret_pos(point& pos, bool text_coordinate) const + { + internal_scope_guard lock; + auto editor = get_drawer_trigger().editor(); + + auto scr_pos = editor->caret_screen_pos(); + + if (text_coordinate) + { + auto upos = editor->caret(); + pos.x = static_cast(upos.x); + pos.y = static_cast(upos.y); + } + else + pos = scr_pos; + + return editor->hit_text_area(scr_pos); + } + textbox& textbox::append(const nana::string& text, bool at_caret) { internal_scope_guard lock;