# HG changeset patch # User bsmith@81767d24-ef19-dc11-ae90-00e081727c95 # Date 1671843165 0 # Node ID d53fee1980852283e679b33ef92b6e5781603390 # Parent fed0c211298576af185f39415e66e63dd5fe31b2 C++: Add KeyPress, ButtonPress, ButtonRelease and MotionNotify signals to Render. diff -r fed0c2112985 -r d53fee198085 dw.hpp --- a/dw.hpp Fri Dec 23 06:29:48 2022 +0000 +++ b/dw.hpp Sat Dec 24 00:52:45 2022 +0000 @@ -516,13 +516,21 @@ class Render : public Drawable, public Widget { private: - bool ExposeConnected, ConfigureConnected; + bool ExposeConnected, ConfigureConnected, KeyPressConnected, ButtonPressConnected, ButtonReleaseConnected, MotionNotifyConnected; #ifdef DW_LAMBDA std::function _ConnectExpose; std::function _ConnectConfigure; + std::function _ConnectKeyPress; + std::function _ConnectButtonPress; + std::function _ConnectButtonRelease; + std::function _ConnectMotionNotify; #else int (*_ConnectExpose)(DWExpose *); int (*_ConnectConfigure)(int width, int height); + int (*_ConnectKeyPress)(char c, int vk, int state, char *utf8); + int (*_ConnectButtonPress)(int x, int y, int buttonmask); + int (*_ConnectButtonRelease)(int x, int y, int buttonmask); + int (*_ConnectMotionNotify)(int x, int y, int buttonmask); #endif void Setup() { if(IsOverridden(Render::OnExpose, this)) { @@ -533,6 +541,22 @@ dw_signal_connect(hwnd, DW_SIGNAL_CONFIGURE, DW_SIGNAL_FUNC(_OnConfigure), this); ConfigureConnected = true; } + if(IsOverridden(Render::OnKeyPress, this)) { + dw_signal_connect(hwnd, DW_SIGNAL_KEY_PRESS, DW_SIGNAL_FUNC(_OnKeyPress), this); + KeyPressConnected = true; + } + if(IsOverridden(Render::OnButtonPress, this)) { + dw_signal_connect(hwnd, DW_SIGNAL_BUTTON_PRESS, DW_SIGNAL_FUNC(_OnButtonPress), this); + ButtonPressConnected = true; + } + if(IsOverridden(Render::OnButtonRelease, this)) { + dw_signal_connect(hwnd, DW_SIGNAL_BUTTON_RELEASE, DW_SIGNAL_FUNC(_OnButtonRelease), this); + ButtonReleaseConnected = true; + } + if(IsOverridden(Render::OnMotionNotify, this)) { + dw_signal_connect(hwnd, DW_SIGNAL_MOTION_NOTIFY, DW_SIGNAL_FUNC(_OnMotionNotify), this); + MotionNotifyConnected = true; + } } static int _OnExpose(HWND window, DWExpose *exp, void *data) { if(reinterpret_cast(data)->_ConnectExpose) @@ -542,6 +566,22 @@ if(reinterpret_cast(data)->_ConnectConfigure) return reinterpret_cast(data)->_ConnectConfigure(width, height); return reinterpret_cast(data)->OnConfigure(width, height); } + static int _OnKeyPress(HWND window, char c, int vk, int state, void *data, char *utf8) { + if(reinterpret_cast(data)->_ConnectKeyPress) + return reinterpret_cast(data)->_ConnectKeyPress(c, vk, state, utf8); + return reinterpret_cast(data)->OnKeyPress(c, vk, state, utf8); } + static int _OnButtonPress(HWND window, int x, int y, int buttonmask, void *data) { + if(reinterpret_cast(data)->_ConnectButtonPress) + return reinterpret_cast(data)->_ConnectButtonPress(x, y, buttonmask); + return reinterpret_cast(data)->OnButtonPress(x, y, buttonmask); } + static int _OnButtonRelease(HWND window, int x, int y, int buttonmask, void *data) { + if(reinterpret_cast(data)->_ConnectButtonRelease) + return reinterpret_cast(data)->_ConnectButtonRelease(x, y, buttonmask); + return reinterpret_cast(data)->OnButtonRelease(x, y, buttonmask); } + static int _OnMotionNotify(HWND window, int x, int y, int buttonmask, void *data) { + if(reinterpret_cast(data)->_ConnectMotionNotify) + return reinterpret_cast(data)->_ConnectMotionNotify(x, y, buttonmask); + return reinterpret_cast(data)->OnMotionNotify(x, y, buttonmask); } public: // Constructors Render(unsigned long id) { SetHWND(dw_render_new(id)); Setup(); } @@ -589,7 +629,55 @@ dw_signal_connect(hwnd, DW_SIGNAL_CONFIGURE, DW_SIGNAL_FUNC(_OnConfigure), this); ConfigureConnected = true; } - } + } +#ifdef DW_LAMBDA + void ConnectKeyPress(std::function userfunc) +#else + void ConnectKeyPress(int (*userfunc)(char, int, int, char *)) +#endif + { + _ConnectKeyPress = userfunc; + if(!KeyPressConnected) { + dw_signal_connect(hwnd, DW_SIGNAL_KEY_PRESS, DW_SIGNAL_FUNC(_OnKeyPress), this); + KeyPressConnected = true; + } + } +#ifdef DW_LAMBDA + void ConnectButtonPress(std::function userfunc) +#else + void ConnectButtonPress(int (*userfunc)(int, int, int)) +#endif + { + _ConnectButtonPress = userfunc; + if(!KeyPressConnected) { + dw_signal_connect(hwnd, DW_SIGNAL_BUTTON_PRESS, DW_SIGNAL_FUNC(_OnButtonPress), this); + ButtonPressConnected = true; + } + } +#ifdef DW_LAMBDA + void ConnectButtonRelease(std::function userfunc) +#else + void ConnectButtonRelease(int (*userfunc)(int, int, int)) +#endif + { + _ConnectButtonRelease = userfunc; + if(!KeyPressConnected) { + dw_signal_connect(hwnd, DW_SIGNAL_BUTTON_RELEASE, DW_SIGNAL_FUNC(_OnButtonRelease), this); + ButtonReleaseConnected = true; + } + } +#ifdef DW_LAMBDA + void ConnectMotionNotify(std::function userfunc) +#else + void ConnectMotionNotify(int (*userfunc)(int, int, int)) +#endif + { + _ConnectMotionNotify = userfunc; + if(!MotionNotifyConnected) { + dw_signal_connect(hwnd, DW_SIGNAL_MOTION_NOTIFY, DW_SIGNAL_FUNC(_OnMotionNotify), this); + MotionNotifyConnected = true; + } + } protected: // Our signal handler functions to be overriden... // If they are not overridden and an event is generated, remove the unused handler @@ -603,6 +691,26 @@ ConfigureConnected = false; return FALSE; }; + virtual int OnKeyPress(char c, int vk, int state, char *utf8) { + dw_signal_disconnect_by_name(hwnd, DW_SIGNAL_KEY_PRESS); + KeyPressConnected = false; + return FALSE; + }; + virtual int OnButtonPress(char x, int y, int buttonmask) { + dw_signal_disconnect_by_name(hwnd, DW_SIGNAL_BUTTON_PRESS); + ButtonPressConnected = false; + return FALSE; + }; + virtual int OnButtonRelease(char x, int y, int buttonmask) { + dw_signal_disconnect_by_name(hwnd, DW_SIGNAL_BUTTON_RELEASE); + ButtonReleaseConnected = false; + return FALSE; + }; + virtual int OnMotionNotify(char x, int y, int buttonmask) { + dw_signal_disconnect_by_name(hwnd, DW_SIGNAL_MOTION_NOTIFY); + MotionNotifyConnected = false; + return FALSE; + }; }; class Pixmap : public Drawable, public Handle