From 09b314b94ce7ca31721f9af9f4d09dece2e0f373 Mon Sep 17 00:00:00 2001 From: Jinhao Date: Fri, 23 Oct 2015 00:51:58 +0800 Subject: [PATCH] change textbox event text_position_changed in favor of text_exposed --- .../gui/widgets/skeletons/text_editor.hpp | 3 - .../widgets/skeletons/text_editor_part.hpp | 5 +- include/nana/gui/widgets/textbox.hpp | 39 +++---------- include/nana/paint/detail/image_processor.hpp | 2 +- source/gui/dragger.cpp | 57 +++++++------------ source/gui/widgets/skeletons/text_editor.cpp | 9 +-- source/gui/widgets/textbox.cpp | 26 ++++----- 7 files changed, 47 insertions(+), 94 deletions(-) diff --git a/include/nana/gui/widgets/skeletons/text_editor.hpp b/include/nana/gui/widgets/skeletons/text_editor.hpp index f07187d6..62da6ab3 100644 --- a/include/nana/gui/widgets/skeletons/text_editor.hpp +++ b/include/nana/gui/widgets/skeletons/text_editor.hpp @@ -229,8 +229,6 @@ namespace nana{ namespace widgets /// Returns text position of each line that currently displays on screen const std::vector& text_position() const; - - void set_text_position_changed(std::function&)>); public: void draw_corner(); void render(bool focused); @@ -317,7 +315,6 @@ namespace nana{ namespace widgets mutable ext_renderer_tag ext_renderer_; std::vector text_position_; //position of text from last rendering. - std::function&)> text_position_function_; struct indent_rep { diff --git a/include/nana/gui/widgets/skeletons/text_editor_part.hpp b/include/nana/gui/widgets/skeletons/text_editor_part.hpp index 9a213844..be4ec920 100644 --- a/include/nana/gui/widgets/skeletons/text_editor_part.hpp +++ b/include/nana/gui/widgets/skeletons/text_editor_part.hpp @@ -11,6 +11,9 @@ namespace nana { namespace skeletons { + //forward declaration + class text_editor; + struct text_editor_scheme : public ::nana::widget_colors { @@ -23,7 +26,7 @@ namespace nana public: virtual ~text_editor_event_interface() = default; - virtual void text_position_changed(const std::vector&) = 0; + virtual void text_exposed(const std::vector&) = 0; }; } } diff --git a/include/nana/gui/widgets/textbox.hpp b/include/nana/gui/widgets/textbox.hpp index bca29008..16236081 100644 --- a/include/nana/gui/widgets/textbox.hpp +++ b/include/nana/gui/widgets/textbox.hpp @@ -23,28 +23,11 @@ namespace nana : public event_arg { textbox& widget; - const std::vector* text_position; + const std::vector& text_position; ///< position of characters that the first character of line which are displayed - arg_textbox(textbox&); + arg_textbox(textbox&, const std::vector&); }; - struct arg_textbox_text_position - : public event_arg - { - textbox& widget; - const std::vector& text_position; - - arg_textbox_text_position(textbox&, const std::vector&); - }; - - namespace widgets - { - namespace skeletons - { - class text_editor; - } - } - namespace drawerbase { namespace textbox @@ -54,15 +37,7 @@ namespace nana { basic_event first_change; basic_event text_changed; - basic_event text_position_changed; - }; - - class event_interface - { - public: - virtual ~event_interface() = default; - - virtual void text_position_changed(const std::vector&) = 0; + basic_event text_exposed; }; class event_agent @@ -70,16 +45,17 @@ namespace nana public widgets::skeletons::text_editor_event_interface { public: - event_agent(::nana::textbox&); + event_agent(::nana::textbox&, const std::vector&); private: //Overrides textbase_event_agent_interface void first_change() override; void text_changed() override; private: //Overrides text_editor_event_interface - void text_position_changed(const std::vector&) override; + void text_exposed(const std::vector&) override; private: ::nana::textbox & widget_; + const std::vector& text_position_; }; //class drawer @@ -122,6 +98,7 @@ namespace nana :public widget_object { public: + using text_positions = std::vector; /// The default constructor without creating the widget. textbox(); @@ -220,7 +197,7 @@ namespace nana /// Returns the text position of each line that currently displays on screen. - std::vector text_position() const; + text_positions text_position() const; /// Returns the rectangle of text area rectangle text_area() const; diff --git a/include/nana/paint/detail/image_processor.hpp b/include/nana/paint/detail/image_processor.hpp index ae4355b0..7cdc1658 100644 --- a/include/nana/paint/detail/image_processor.hpp +++ b/include/nana/paint/detail/image_processor.hpp @@ -30,7 +30,7 @@ namespace detail { ///@brief Seek a pixel address by using offset bytes ///@return the specified pixel address - inline pixel_color_t * pixel_at(pixel_color_t * p, std::size_t bytes) //deprecated + inline pixel_color_t * pixel_at(pixel_color_t * p, std::size_t bytes) { return reinterpret_cast(reinterpret_cast(p) + bytes); } diff --git a/source/gui/dragger.cpp b/source/gui/dragger.cpp index 9aeb8b0b..8b865891 100644 --- a/source/gui/dragger.cpp +++ b/source/gui/dragger.cpp @@ -26,13 +26,17 @@ namespace nana event_handle destroy; }; public: - dragger_impl_t() - : dragging_(false) - {} - ~dragger_impl_t() { - _m_clear_triggers(); + //Clear triggers + for (auto & t : triggers_) + { + API::umake_event(t.press); + API::umake_event(t.over); + API::umake_event(t.release); + API::umake_event(t.destroy); + API::capture_window(t.wd, false); + } } void drag_target(window wd, const rectangle& restrict_area, arrange arg) @@ -128,40 +132,23 @@ namespace nana tg.press = events.mouse_down.connect(fn); tg.over = events.mouse_move.connect(fn); tg.release = events.mouse_up.connect(fn); - tg.destroy = events.destroy.connect([this](const arg_destroy& arg){ - _m_destroy(arg.window_handle); + tg.destroy = events.destroy.connect([this](const arg_destroy& arg) + { + for (auto i = triggers_.begin(), end = triggers_.end(); i != end; ++i) + { + if (i->wd == arg.window_handle) + { + triggers_.erase(i); + API::capture_window(arg.window_handle, false); + return; + } + } }); triggers_.push_back(tg); } private: - void _m_clear_triggers() - { - for(auto & t : triggers_) - { - API::umake_event(t.press); - API::umake_event(t.over); - API::umake_event(t.release); - API::umake_event(t.destroy); - API::capture_window(t.wd, false); - } - triggers_.clear(); - } - - void _m_destroy(::nana::window wd) - { - for(auto i = triggers_.begin(), end = triggers_.end(); i != end; ++i) - { - if(i->wd == wd) - { - triggers_.erase(i); - API::capture_window(wd, false); - return; - } - } - } - - void _m_check_restrict_area(nana::point & pos, const nana::size & size, const nana::rectangle& restr_area) + static void _m_check_restrict_area(nana::point & pos, const nana::size & size, const nana::rectangle& restr_area) { if ((pos.x > 0) && (static_cast(size.width) + pos.x > restr_area.right())) pos.x = restr_area.right() - static_cast(size.width); @@ -176,7 +163,7 @@ namespace nana pos.y = restr_area.y; } private: - bool dragging_; + bool dragging_{ false }; nana::point origin_; std::vector targets_; std::vector triggers_; diff --git a/source/gui/widgets/skeletons/text_editor.cpp b/source/gui/widgets/skeletons/text_editor.cpp index 4bf5d580..654e317c 100644 --- a/source/gui/widgets/skeletons/text_editor.cpp +++ b/source/gui/widgets/skeletons/text_editor.cpp @@ -435,7 +435,7 @@ namespace nana{ namespace widgets if (x > lnstr.size()) x = static_cast(lnstr.size()); - unsigned text_w = editor_._m_pixels_by_char(lnstr, x); + auto const text_w = editor_._m_pixels_by_char(lnstr, x); unsigned area_w = editor_._m_text_area().width; @@ -1918,11 +1918,6 @@ namespace nana{ namespace widgets return text_position_; } - void text_editor::set_text_position_changed(std::function&)> fn) - { - text_position_function_.swap(fn); - } - void text_editor::draw_corner() { if(text_area_.vscroll && text_area_.hscroll) @@ -1967,7 +1962,7 @@ namespace nana{ namespace widgets { text_position_.swap(text_pos); if (event_handler_) - event_handler_->text_position_changed(text_position_); + event_handler_->text_exposed(text_position_); } } else //Draw tip string diff --git a/source/gui/widgets/textbox.cpp b/source/gui/widgets/textbox.cpp index 31910ff2..10e07bd7 100644 --- a/source/gui/widgets/textbox.cpp +++ b/source/gui/widgets/textbox.cpp @@ -20,38 +20,32 @@ namespace nana { - arg_textbox::arg_textbox(textbox& wdg) - : widget(wdg) + arg_textbox::arg_textbox(textbox& wdg, const std::vector& text_pos) + : widget(wdg), text_position(text_pos) {} - arg_textbox_text_position::arg_textbox_text_position(textbox& wdg, const std::vector& text_pos) - : widget(wdg), - text_position(text_pos) - { - } - namespace drawerbase { namespace textbox { //class event_agent - event_agent::event_agent(::nana::textbox& wdg) - :widget_(wdg) + event_agent::event_agent(::nana::textbox& wdg, const std::vector& text_pos) + :widget_(wdg), text_position_(text_pos) {} void event_agent::first_change() { - widget_.events().first_change.emit(::nana::arg_textbox{ widget_ }); + widget_.events().first_change.emit(::nana::arg_textbox{ widget_, text_position_ }); } void event_agent::text_changed() { - widget_.events().text_changed.emit(::nana::arg_textbox{ widget_ }); + widget_.events().text_changed.emit(::nana::arg_textbox{ widget_, text_position_ }); } - void event_agent::text_position_changed(const std::vector& text_pos) + void event_agent::text_exposed(const std::vector& text_pos) { - ::nana::arg_textbox_text_position arg(widget_, text_pos); - widget_.events().text_position_changed.emit(arg); + ::nana::arg_textbox arg(widget_, text_pos); + widget_.events().text_exposed.emit(arg); } //end class event_agent @@ -75,7 +69,7 @@ namespace drawerbase { { auto wd = wdg.handle(); widget_ = &wdg; - evt_agent_.reset(new event_agent(static_cast< ::nana::textbox&>(wdg))); + evt_agent_.reset(new event_agent(static_cast<::nana::textbox&>(wdg), editor_->text_position())); auto scheme = API::dev::get_scheme(wdg);