diff options
author | crupest <crupest@outlook.com> | 2018-09-13 23:42:44 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2018-09-13 23:42:44 +0800 |
commit | da54cf6e1dd9ffbf1e57a8a3c19e41a8fcedf5b1 (patch) | |
tree | 59ab635a09bfb4fc219b243c920cd236dfa41010 | |
parent | 42f9f6c0bce5b3bea05edf4e371539fe21ea1c5d (diff) | |
download | cru-da54cf6e1dd9ffbf1e57a8a3c19e41a8fcedf5b1.tar.gz cru-da54cf6e1dd9ffbf1e57a8a3c19e41a8fcedf5b1.tar.bz2 cru-da54cf6e1dd9ffbf1e57a8a3c19e41a8fcedf5b1.zip |
...
-rw-r--r-- | CruUI/ui/control.cpp | 8 | ||||
-rw-r--r-- | CruUI/ui/control.h | 12 | ||||
-rw-r--r-- | CruUI/ui/controls/text_block.cpp | 32 | ||||
-rw-r--r-- | CruUI/ui/controls/text_block.h | 8 | ||||
-rw-r--r-- | CruUI/ui/events/ui_event.h | 24 | ||||
-rw-r--r-- | CruUI/ui/window.cpp | 12 |
6 files changed, 52 insertions, 44 deletions
diff --git a/CruUI/ui/control.cpp b/CruUI/ui/control.cpp index 4d9f0593..c7864dc7 100644 --- a/CruUI/ui/control.cpp +++ b/CruUI/ui/control.cpp @@ -389,21 +389,21 @@ namespace cru { mouse_up_event.Raise(args); } - void Control::OnGetFocus(UiEventArgs & args) + void Control::OnGetFocus(FocusChangeEventArgs& args) { } - void Control::OnLoseFocus(UiEventArgs & args) + void Control::OnLoseFocus(FocusChangeEventArgs& args) { } - void Control::OnGetFocusCore(UiEventArgs & args) + void Control::OnGetFocusCore(FocusChangeEventArgs& args) { OnGetFocus(args); get_focus_event.Raise(args); } - void Control::OnLoseFocusCore(UiEventArgs & args) + void Control::OnLoseFocusCore(FocusChangeEventArgs& args) { OnLoseFocus(args); lose_focus_event.Raise(args); diff --git a/CruUI/ui/control.h b/CruUI/ui/control.h index 0c19f305..2ec4019b 100644 --- a/CruUI/ui/control.h +++ b/CruUI/ui/control.h @@ -176,8 +176,8 @@ namespace cru //Raised when a mouse button is released in the control. events::MouseButtonEvent mouse_up_event; - events::UiEvent get_focus_event; - events::UiEvent lose_focus_event; + events::FocusChangeEvent get_focus_event; + events::FocusChangeEvent lose_focus_event; events::DrawEvent draw_event; @@ -228,11 +228,11 @@ namespace cru //*************** region: focus event *************** - virtual void OnGetFocus(events::UiEventArgs& args); - virtual void OnLoseFocus(events::UiEventArgs& args); + virtual void OnGetFocus(events::FocusChangeEventArgs& args); + virtual void OnLoseFocus(events::FocusChangeEventArgs& args); - virtual void OnGetFocusCore(events::UiEventArgs& args); - virtual void OnLoseFocusCore(events::UiEventArgs& args); + virtual void OnGetFocusCore(events::FocusChangeEventArgs& args); + virtual void OnLoseFocusCore(events::FocusChangeEventArgs& args); //*************** region: layout *************** virtual Size OnMeasure(const Size& available_size); diff --git a/CruUI/ui/controls/text_block.cpp b/CruUI/ui/controls/text_block.cpp index 00850b0b..3d53da7c 100644 --- a/CruUI/ui/controls/text_block.cpp +++ b/CruUI/ui/controls/text_block.cpp @@ -25,11 +25,7 @@ namespace cru } TextBlock::TextBlock(const Microsoft::WRL::ComPtr<IDWriteTextFormat>& init_text_format, - const Microsoft::WRL::ComPtr<ID2D1Brush>& init_brush) : Control(false), - window_deactivated_handler_(new events::UiEvent::EventHandler([this](events::UiEvent::ArgsType& args) - { - is_selecting_ = false; - })) + const Microsoft::WRL::ComPtr<ID2D1Brush>& init_brush) : Control(false) { text_format_ = init_text_format; if (init_brush == nullptr) @@ -63,18 +59,6 @@ namespace cru Repaint(); } - void TextBlock::OnAttachToWindow(Window* window) - { - Control::OnAttachToWindow(window); - window->deactivated_event.AddHandler(window_deactivated_handler_); - } - - void TextBlock::OnDetachToWindow(Window* window) - { - Control::OnDetachToWindow(window); - window->deactivated_event.RemoveHandler(window_deactivated_handler_); - } - void TextBlock::OnSizeChangedCore(events::SizeChangedEventArgs& args) { text_layout_->SetMaxWidth(args.GetNewSize().width); @@ -171,10 +155,18 @@ namespace cru Control::OnMouseUpCore(args); } - void TextBlock::OnLoseFocusCore(events::UiEventArgs& args) + void TextBlock::OnLoseFocusCore(events::FocusChangeEventArgs& args) { - selected_range_ = std::nullopt; - Repaint(); + if (is_selecting_) + { + is_selecting_ = false; + GetWindow()->ReleaseCurrentMouseCapture(); + } + if (!args.IsWindow()) + { + selected_range_ = std::nullopt; + Repaint(); + } Control::OnLoseFocusCore(args); } diff --git a/CruUI/ui/controls/text_block.h b/CruUI/ui/controls/text_block.h index 689607ac..beb662b9 100644 --- a/CruUI/ui/controls/text_block.h +++ b/CruUI/ui/controls/text_block.h @@ -97,9 +97,6 @@ namespace cru } protected: - void OnAttachToWindow(Window* window) override; - void OnDetachToWindow(Window* window) override; - void OnSizeChangedCore(events::SizeChangedEventArgs& args) override final; void OnDraw(ID2D1DeviceContext* device_context) override; @@ -107,7 +104,7 @@ namespace cru void OnMouseMoveCore(events::MouseEventArgs& args) override; void OnMouseUpCore(events::MouseButtonEventArgs& args) override; - void OnLoseFocusCore(events::UiEventArgs& args) override; + void OnLoseFocusCore(events::FocusChangeEventArgs& args) override; Size OnMeasure(const Size& available_size) override; @@ -131,9 +128,6 @@ namespace cru bool is_selecting_ = false; unsigned mouse_down_position_ = 0; std::optional<TextRange> selected_range_ = std::nullopt; - - - events::UiEvent::EventHandlerPtr window_deactivated_handler_; }; } } diff --git a/CruUI/ui/events/ui_event.h b/CruUI/ui/events/ui_event.h index efb0479b..ddf5f99f 100644 --- a/CruUI/ui/events/ui_event.h +++ b/CruUI/ui/events/ui_event.h @@ -168,6 +168,29 @@ namespace cru Size new_size_; }; + class FocusChangeEventArgs : public UiEventArgs + { + public: + FocusChangeEventArgs(Object* sender, Object* original_sender, const bool is_window =false) + : UiEventArgs(sender, original_sender), is_window_(is_window) + { + + } + FocusChangeEventArgs(const FocusChangeEventArgs& other) = default; + FocusChangeEventArgs(FocusChangeEventArgs&& other) = default; + FocusChangeEventArgs& operator=(const FocusChangeEventArgs& other) = default; + FocusChangeEventArgs& operator=(FocusChangeEventArgs&& other) = default; + ~FocusChangeEventArgs() override = default; + + // Return whether the focus change is caused by the window-wide focus change. + bool IsWindow() const + { + return is_window_; + } + + private: + bool is_window_; + }; using UiEvent = Event<UiEventArgs>; using MouseEvent = Event<MouseEventArgs>; @@ -175,6 +198,7 @@ namespace cru using DrawEvent = Event<DrawEventArgs>; using PositionChangedEvent = Event<PositionChangedEventArgs>; using SizeChangedEvent = Event<SizeChangedEventArgs>; + using FocusChangeEvent = Event<FocusChangeEventArgs>; } } } diff --git a/CruUI/ui/window.cpp b/CruUI/ui/window.cpp index d43f2e77..cbb3b694 100644 --- a/CruUI/ui/window.cpp +++ b/CruUI/ui/window.cpp @@ -412,19 +412,19 @@ namespace cru if (!window_focus_) { - ::SetFocus(hwnd_); focus_control_ = control; + ::SetFocus(hwnd_); return true; // event dispatch will be done in window message handling function "OnSetFocusInternal". } if (focus_control_ == control) return true; - DispatchEvent(focus_control_, &Control::OnLoseFocusCore, nullptr); + DispatchEvent(focus_control_, &Control::OnLoseFocusCore, nullptr, false); focus_control_ = control; - DispatchEvent(control, &Control::OnGetFocusCore, nullptr); + DispatchEvent(control, &Control::OnGetFocusCore, nullptr, false); return true; } @@ -511,15 +511,13 @@ namespace cru void Window::OnSetFocusInternal() { window_focus_ = true; - if (focus_control_ != nullptr) - DispatchEvent(focus_control_, &Control::OnGetFocusCore, nullptr); + DispatchEvent(focus_control_, &Control::OnGetFocusCore, nullptr, true); } void Window::OnKillFocusInternal() { window_focus_ = false; - if (focus_control_ != nullptr) - DispatchEvent(focus_control_, &Control::OnLoseFocusCore, nullptr); + DispatchEvent(focus_control_, &Control::OnLoseFocusCore, nullptr, true); } void Window::OnMouseMoveInternal(const POINT point) |