aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2018-09-23 21:00:46 +0800
committercrupest <crupest@outlook.com>2018-09-23 21:00:46 +0800
commit82f42a4103c168abd3605acad8ee4b9b4f00d79d (patch)
tree6db4f99f3dc2f85fba3f8339da53849d04a5cfee
parentad8ea7fc26b3e0807d11965d93c26a6ff51db226 (diff)
downloadcru-82f42a4103c168abd3605acad8ee4b9b4f00d79d.tar.gz
cru-82f42a4103c168abd3605acad8ee4b9b4f00d79d.tar.bz2
cru-82f42a4103c168abd3605acad8ee4b9b4f00d79d.zip
Add native message handler event in Window.
-rw-r--r--CruUI/cru_event.h5
-rw-r--r--CruUI/main.cpp10
-rw-r--r--CruUI/ui/events/ui_event.h42
-rw-r--r--CruUI/ui/window.cpp15
-rw-r--r--CruUI/ui/window.h2
5 files changed, 73 insertions, 1 deletions
diff --git a/CruUI/cru_event.h b/CruUI/cru_event.h
index 3a2dccfa..d0a7eb82 100644
--- a/CruUI/cru_event.h
+++ b/CruUI/cru_event.h
@@ -80,6 +80,11 @@ namespace cru {
(*ptr)(args);
}
+ bool IsNoHandler() const
+ {
+ return handlers_.empty();
+ }
+
private:
std::list<EventHandlerPtr> handlers_;
};
diff --git a/CruUI/main.cpp b/CruUI/main.cpp
index 7f5c1d1c..56d42894 100644
--- a/CruUI/main.cpp
+++ b/CruUI/main.cpp
@@ -5,6 +5,7 @@
#include "ui/controls/toggle_button.h"
#include "ui/controls/button.h"
#include "ui/controls/margin_container.h"
+#include "ui/events/ui_event.h"
using cru::String;
@@ -29,6 +30,15 @@ int APIENTRY wWinMain(
Application application(hInstance);
Window window;
+ window.native_message_event.AddHandler([](cru::ui::events::WindowNativeMessageEventArgs& args)
+ {
+ if (args.GetWindowMessage().msg == WM_PAINT)
+ {
+ OutputDebugStringW(L"Paint!\n");
+ //args.SetResult(0);
+ }
+ });
+
/*
// test1
cru::ui::controls::TextBlock text_block;
diff --git a/CruUI/ui/events/ui_event.h b/CruUI/ui/events/ui_event.h
index 4915d63d..a17067c7 100644
--- a/CruUI/ui/events/ui_event.h
+++ b/CruUI/ui/events/ui_event.h
@@ -215,6 +215,47 @@ namespace cru
bool new_state_;
};
+ struct WindowNativeMessage
+ {
+ HWND hwnd;
+ int msg;
+ WPARAM w_param;
+ LPARAM l_param;
+ };
+
+ class WindowNativeMessageEventArgs : public UiEventArgs
+ {
+ public:
+ WindowNativeMessageEventArgs(Object* sender, Object* original_sender, const WindowNativeMessage& message)
+ : UiEventArgs(sender, original_sender), message_(message), result_(std::nullopt)
+ {
+
+ }
+ WindowNativeMessageEventArgs(const WindowNativeMessageEventArgs& other) = default;
+ WindowNativeMessageEventArgs(WindowNativeMessageEventArgs&& other) = default;
+ WindowNativeMessageEventArgs& operator=(const WindowNativeMessageEventArgs& other) = default;
+ WindowNativeMessageEventArgs& operator=(WindowNativeMessageEventArgs&& other) = default;
+ ~WindowNativeMessageEventArgs() override = default;
+
+ WindowNativeMessage GetWindowMessage() const
+ {
+ return message_;
+ }
+
+ std::optional<LRESULT> GetResult() const
+ {
+ return result_;
+ }
+
+ void SetResult(const std::optional<LRESULT> result)
+ {
+ result_ = result;
+ }
+
+ private:
+ WindowNativeMessage message_;
+ std::optional<LRESULT> result_;
+ };
using UiEvent = Event<UiEventArgs>;
using MouseEvent = Event<MouseEventArgs>;
@@ -224,6 +265,7 @@ namespace cru
using SizeChangedEvent = Event<SizeChangedEventArgs>;
using FocusChangeEvent = Event<FocusChangeEventArgs>;
using ToggleEvent = Event<ToggleEventArgs>;
+ using WindowNativeMessageEvent = Event<WindowNativeMessageEventArgs>;
}
}
} \ No newline at end of file
diff --git a/CruUI/ui/window.cpp b/CruUI/ui/window.cpp
index b20abb61..6ff962b6 100644
--- a/CruUI/ui/window.cpp
+++ b/CruUI/ui/window.cpp
@@ -82,7 +82,7 @@ namespace cru
Vector<Window*> windows;
for (auto [key, value] : window_map_)
windows.push_back(value);
- return std::move(windows);
+ return windows;
}
inline Point PiToDip(const POINT& pi_point)
@@ -201,6 +201,19 @@ namespace cru
}
bool Window::HandleWindowMessage(HWND hwnd, int msg, WPARAM w_param, LPARAM l_param, LRESULT & result) {
+
+ if (!native_message_event.IsNoHandler())
+ {
+ const events::WindowNativeMessage message{hwnd, msg, w_param, l_param};
+ events::WindowNativeMessageEventArgs args(this, this, message);
+ native_message_event.Raise(args);
+ if (args.GetResult().has_value())
+ {
+ result = args.GetResult().value();
+ return true;
+ }
+ }
+
switch (msg) {
case WM_PAINT:
OnPaintInternal();
diff --git a/CruUI/ui/window.h b/CruUI/ui/window.h
index b9e9a184..42ebf477 100644
--- a/CruUI/ui/window.h
+++ b/CruUI/ui/window.h
@@ -194,6 +194,8 @@ namespace cru {
events::UiEvent activated_event;
events::UiEvent deactivated_event;
+ events::WindowNativeMessageEvent native_message_event;
+
private:
//*************** region: native operations ***************