diff --git a/include/nana/gui/widgets/skeletons/text_editor.hpp b/include/nana/gui/widgets/skeletons/text_editor.hpp index ea021411..f5408008 100644 --- a/include/nana/gui/widgets/skeletons/text_editor.hpp +++ b/include/nana/gui/widgets/skeletons/text_editor.hpp @@ -151,6 +151,8 @@ namespace nana{ namespace widgets bool selected() const; bool select(bool); + bool get_select_points(nana::upoint&, nana::upoint&) const; + /// Sets the end position of a selected string. void set_end_caret(); @@ -233,7 +235,6 @@ namespace nana{ namespace widgets //@brief: redraw whole line specified by caret pos. //@return: true if caret overs the border bool _m_update_caret_line(std::size_t secondary_before); - bool _m_get_sort_select_points(nana::upoint&, nana::upoint&) const; void _m_offset_y(int y); diff --git a/include/nana/gui/widgets/textbox.hpp b/include/nana/gui/widgets/textbox.hpp index 14b5a9d8..b757260d 100644 --- a/include/nana/gui/widgets/textbox.hpp +++ b/include/nana/gui/widgets/textbox.hpp @@ -184,7 +184,7 @@ namespace nana bool editable() const; textbox& editable(bool); - /// Enables the caret if the textbox current is not editable + /// Enables the caret if the textbox currently is not editable textbox& enable_caret(); void set_accept(std::function); @@ -200,6 +200,12 @@ namespace nana /// Selects/unselects all text. void select(bool); + /// Returns the bounds of a text selection + /** + * @return no selection if pair.first == pair.second. + */ + std::pair selection() const; + void copy() const; ///< Copies the selected text into shared memory, such as clipboard under Windows. void paste(); ///< Pastes the text from shared memory. void del(); diff --git a/source/gui/widgets/skeletons/text_editor.cpp b/source/gui/widgets/skeletons/text_editor.cpp index a662aadf..42d98873 100644 --- a/source/gui/widgets/skeletons/text_editor.cpp +++ b/source/gui/widgets/skeletons/text_editor.cpp @@ -2081,6 +2081,25 @@ namespace nana{ namespace widgets return false; } + bool text_editor::get_select_points(nana::upoint& a, nana::upoint& b) const + { + if (select_.a == select_.b) + return false; + + if (select_.a < select_.b) + { + a = select_.a; + b = select_.b; + } + else + { + a = select_.b; + b = select_.a; + } + + return true; + } + bool text_editor::hit_text_area(const point& pos) const { return _m_text_area().is_hit(pos); @@ -2089,7 +2108,7 @@ namespace nana{ namespace widgets bool text_editor::hit_select_area(nana::upoint pos, bool ignore_when_select_all) const { nana::upoint a, b; - if(_m_get_sort_select_points(a, b)) + if(get_select_points(a, b)) { if (ignore_when_select_all) { @@ -3006,7 +3025,7 @@ namespace nana{ namespace widgets nana::upoint text_editor::_m_erase_select() { nana::upoint a, b; - if (_m_get_sort_select_points(a, b)) + if (get_select_points(a, b)) { auto & textbase = this->textbase(); if(a.y != b.y) @@ -3037,7 +3056,7 @@ namespace nana{ namespace widgets std::wstring text; nana::upoint a, b; - if (_m_get_sort_select_points(a, b)) + if (get_select_points(a, b)) { auto & textbase = this->textbase(); if (a.y != b.y) @@ -3128,7 +3147,7 @@ namespace nana{ namespace widgets bool text_editor::_m_cancel_select(int align) { nana::upoint a, b; - if(_m_get_sort_select_points(a, b)) + if (get_select_points(a, b)) { switch(align) { @@ -3223,7 +3242,7 @@ namespace nana{ namespace widgets const bool at_left = (select_.b < select_.a); nana::upoint a, b; - _m_get_sort_select_points(a, b); + get_select_points(a, b); if (caret.y < a.y || (caret.y == a.y && caret.x < a.x)) {//forward undo_ptr->set_caret_pos(); @@ -3419,7 +3438,7 @@ namespace nana{ namespace widgets const wchar_t *sbegin = nullptr, *send = nullptr; nana::upoint a, b; - if (_m_get_sort_select_points(a, b)) + if (get_select_points(a, b)) { if (a.y < text_coord.y && text_coord.y < b.y) { @@ -3576,25 +3595,6 @@ namespace nana{ namespace widgets return true; } - bool text_editor::_m_get_sort_select_points(nana::upoint& a, nana::upoint& b) const - { - if (select_.a == select_.b) - return false; - - if (select_.a < select_.b) - { - a = select_.a; - b = select_.b; - } - else - { - a = select_.b; - b = select_.a; - } - - return true; - } - void text_editor::_m_offset_y(int y) { points_.offset.y = y; diff --git a/source/gui/widgets/textbox.cpp b/source/gui/widgets/textbox.cpp index a2c07ef4..3e20f7ec 100644 --- a/source/gui/widgets/textbox.cpp +++ b/source/gui/widgets/textbox.cpp @@ -469,6 +469,18 @@ namespace drawerbase { API::update_window(*this); } + std::pair textbox::selection() const + { + std::pair points; + + internal_scope_guard lock; + auto editor = get_drawer_trigger().editor(); + if (editor) + editor->get_select_points(points.first, points.second); + + return points; + } + void textbox::copy() const { internal_scope_guard lock;