"Moved" events.c to C++.

This commit is contained in:
Pietro Gagliardi 2016-04-20 22:10:04 -04:00
parent b9384c0e32
commit 87c2c4f3f7
1 changed files with 19 additions and 49 deletions

View File

@ -1,45 +1,37 @@
// 20 may 2015 // 20 may 2015
#include "uipriv_windows.h" #include "uipriv_windows.hpp"
// TODO get rid of the macro magic
// TODO re-add existence checks
// In each of these structures, hwnd is the hash key. // In each of these structures, hwnd is the hash key.
struct commandHandler { struct commandHandler {
HWND hwnd;
BOOL (*handler)(uiControl *, HWND, WORD, LRESULT *); BOOL (*handler)(uiControl *, HWND, WORD, LRESULT *);
uiControl *c; uiControl *c;
UT_hash_handle hh;
}; };
struct notifyHandler { struct notifyHandler {
HWND hwnd;
BOOL (*handler)(uiControl *, HWND, NMHDR *, LRESULT *); BOOL (*handler)(uiControl *, HWND, NMHDR *, LRESULT *);
uiControl *c; uiControl *c;
UT_hash_handle hh;
}; };
struct hscrollHandler { struct hscrollHandler {
HWND hwnd;
BOOL (*handler)(uiControl *, HWND, WORD, LRESULT *); BOOL (*handler)(uiControl *, HWND, WORD, LRESULT *);
uiControl *c; uiControl *c;
UT_hash_handle hh;
}; };
static struct commandHandler *commandHandlers = NULL; static std::map<HWND, struct commandHandler> commandHandlers;
static struct notifyHandler *notifyHandlers = NULL; static std::map<HWND, struct notifyHandler> notifyHandlers;
static struct hscrollHandler *hscrollHandlers = NULL; static std::map<HWND, struct hscrollHandler> hscrollHandlers;
#define REGFN(WM_MESSAGE, message, params) \ #define REGFN(WM_MESSAGE, message, params) \
void uiWindowsRegister ## WM_MESSAGE ## Handler(HWND hwnd, BOOL (*handler)params, uiControl *c) \ void uiWindowsRegister ## WM_MESSAGE ## Handler(HWND hwnd, BOOL (*handler)params, uiControl *c) \
{ \ { \
struct message ## Handler *ch; \ if (message ## Handler.find(hwnd) != ch.end()) \
HASH_FIND_PTR(message ## Handlers, &hwnd, ch); \
if (ch != NULL) \
complain("window handle %p already subscribed with a %s handler in uiWindowsRegister%sHandler()", hwnd, #WM_MESSAGE, #WM_MESSAGE); \ complain("window handle %p already subscribed with a %s handler in uiWindowsRegister%sHandler()", hwnd, #WM_MESSAGE, #WM_MESSAGE); \
ch = uiNew(struct message ## Handler); \ message ## Handler[hwnd].handler = handler; \
ch->hwnd = hwnd; \ message ## Handler[hwnd].c = c; \
ch->handler = handler; \
ch->c = c; \
HASH_ADD_PTR(message ## Handlers, hwnd, ch); \
} }
REGFN(WM_COMMAND, command, (uiControl *, HWND, WORD, LRESULT *)) REGFN(WM_COMMAND, command, (uiControl *, HWND, WORD, LRESULT *))
REGFN(WM_NOTIFY, notify, (uiControl *, HWND, NMHDR *, LRESULT *)) REGFN(WM_NOTIFY, notify, (uiControl *, HWND, NMHDR *, LRESULT *))
@ -48,12 +40,7 @@ REGFN(WM_HSCROLL, hscroll, (uiControl *, HWND, WORD, LRESULT *))
#define UNREGFN(WM_MESSAGE, message) \ #define UNREGFN(WM_MESSAGE, message) \
void uiWindowsUnregister ## WM_MESSAGE ## Handler(HWND hwnd) \ void uiWindowsUnregister ## WM_MESSAGE ## Handler(HWND hwnd) \
{ \ { \
struct message ## Handler *ch; \ message ## Handler.erase(hwnd); \
HASH_FIND_PTR(message ## Handlers, &hwnd, ch); \
if (ch == NULL) \
complain("window handle %p not registered with a %s handler in uiWindowsUnregister%sHandler()", hwnd, #WM_MESSAGE, #WM_MESSAGE); \
HASH_DEL(message ## Handlers, ch); \
uiFree(ch); \
} }
UNREGFN(WM_COMMAND, command) UNREGFN(WM_COMMAND, command)
UNREGFN(WM_NOTIFY, notify) UNREGFN(WM_NOTIFY, notify)
@ -68,9 +55,8 @@ UNREGFN(WM_HSCROLL, hscroll)
/* don't bounce back if to the utility window, in which case act as if the message was ignored */ \ /* don't bounce back if to the utility window, in which case act as if the message was ignored */ \
control = gethwnd; \ control = gethwnd; \
if (control != NULL && IsChild(utilWindow, control) == 0) { \ if (control != NULL && IsChild(utilWindow, control) == 0) { \
HASH_FIND_PTR(message ## Handlers, &control, ch); \ if (message ## Handlers.find(control) != message ## Handlers.end()) \
if (ch != NULL) \ return (*(message ## Handlers[control].handler))(message ## Handlers[control].c, control, arg3, lResult); \
return (*(ch->handler))(ch->c, control, arg3, lResult); \
/* not registered; fall out to return FALSE */ \ /* not registered; fall out to return FALSE */ \
} \ } \
return FALSE; \ return FALSE; \
@ -85,40 +71,24 @@ RUNFN(WM_HSCROLL, hscroll,
(HWND) lParam, (HWND) lParam,
LOWORD(wParam)) LOWORD(wParam))
struct wininichange { static std::map<HWND, bool> wininichanges;
HWND hwnd;
UT_hash_handle hh;
};
static struct wininichange *wininichanges = NULL;
void uiWindowsRegisterReceiveWM_WININICHANGE(HWND hwnd) void uiWindowsRegisterReceiveWM_WININICHANGE(HWND hwnd)
{ {
struct wininichange *ch; if (wininichanges[hwnd])
HASH_FIND_PTR(wininichanges, &hwnd, ch);
if (ch != NULL)
complain("window handle %p already subscribed to receive WM_WINICHANGEs in uiWindowsRegisterReceiveWM_WININICHANGE()", hwnd); complain("window handle %p already subscribed to receive WM_WINICHANGEs in uiWindowsRegisterReceiveWM_WININICHANGE()", hwnd);
ch = uiNew(struct wininichange); wininichanges[hwnd] = true;
ch->hwnd = hwnd;
HASH_ADD_PTR(wininichanges, hwnd, ch);
} }
void uiWindowsUnregisterReceiveWM_WININICHANGE(HWND hwnd) void uiWindowsUnregisterReceiveWM_WININICHANGE(HWND hwnd)
{ {
struct wininichange *ch; wininichanges[hwnd] = false;
HASH_FIND_PTR(wininichanges, &hwnd, ch);
if (ch == NULL)
complain("window handle %p not registered to receive WM_WININICHANGEs in uiWindowsUnregisterReceiveWM_WININICHANGE()", hwnd);
HASH_DEL(wininichanges, ch);
uiFree(ch);
} }
void issueWM_WININICHANGE(WPARAM wParam, LPARAM lParam) void issueWM_WININICHANGE(WPARAM wParam, LPARAM lParam)
{ {
struct wininichange *ch; struct wininichange *ch;
for (ch = wininichanges; ch != NULL; ch = ch->hh.next) for (const auto &iter : wininichanges)
SendMessageW(ch->hwnd, WM_WININICHANGE, wParam, lParam); SendMessageW(iter.first, WM_WININICHANGE, wParam, lParam);
} }