Converted windows/events.c to use uthash.

This commit is contained in:
Pietro Gagliardi 2015-05-21 11:02:10 -04:00
parent 9c3391f3db
commit 1fcd3354d3
1 changed files with 19 additions and 27 deletions

View File

@ -1,78 +1,70 @@
// 20 may 2015 // 20 may 2015
#include "uipriv_windows.h" #include "uipriv_windows.h"
// TODO switch to uthash // In each of these structures, hwnd is the hash key.
struct commandHandler { struct commandHandler {
HWND hwnd; HWND hwnd;
BOOL (*handler)(uiControl *, WORD, LRESULT *); BOOL (*handler)(uiControl *, WORD, LRESULT *);
uiControl *c; uiControl *c;
UT_hash_handle hh;
}; };
struct notifyHandler { struct notifyHandler {
HWND hwnd; HWND hwnd;
BOOL (*handler)(uiControl *, NMHDR *, LRESULT *); BOOL (*handler)(uiControl *, NMHDR *, LRESULT *);
uiControl *c; uiControl *c;
UT_hash_handle hh;
}; };
struct hscrollHandler { struct hscrollHandler {
HWND hwnd; HWND hwnd;
BOOL (*handler)(uiControl *, WORD, LRESULT *); BOOL (*handler)(uiControl *, WORD, LRESULT *);
uiControl *c; uiControl *c;
UT_hash_handle hh;
}; };
struct ptrArray *commandHandlers = NULL; struct commandHandler *commandHandlers = NULL;
struct ptrArray *notifyHandlers = NULL; struct notifyHandler *notifyHandlers = NULL;
struct ptrArray *hscrollHandlers = NULL; struct hscrollHandler *hscrollHandlers = NULL;
void uiWindowsRegisterWM_COMMANDHandler(HWND hwnd, BOOL (*handler)(uiControl *, WORD, LRESULT *), uiControl *c) void uiWindowsRegisterWM_COMMANDHandler(HWND hwnd, BOOL (*handler)(uiControl *, WORD, LRESULT *), uiControl *c)
{ {
struct commandHandler *ch; struct commandHandler *ch;
HASH_FIND_PTR(commandHandlers, &hwnd, ch);
if (ch != NULL)
complain("window handle %p already subscribed to receive WM_COMMANDs in uiWindowsRegisterWM_COMMANDHandler()", hwnd);
ch = uiNew(struct commandHandler); ch = uiNew(struct commandHandler);
ch->hwnd = hwnd; ch->hwnd = hwnd;
ch->handler = handler; ch->handler = handler;
ch->c = c; ch->c = c;
if (commandHandlers == NULL) HASH_ADD_PTR(commandHandlers, hwnd, ch);
commandHandlers = newPtrArray();
ptrArrayAppend(commandHandlers, ch);
} }
void uiWindowsUnregisterWM_COMMANDHandler(HWND hwnd) void uiWindowsUnregisterWM_COMMANDHandler(HWND hwnd)
{ {
struct commandHandler *ch; struct commandHandler *ch;
uintmax_t i;
for (i = 0; i < commandHandlers->len; i++) { HASH_FIND_PTR(commandHandlers, &hwnd, ch);
ch = ptrArrayIndex(commandHandlers, struct commandHandler *, i); if (ch == NULL)
if (ch->hwnd == hwnd) { complain("window handle %p not registered with a WM_COMMAND handler in uiWindowsUnregisterWM_COMMANDHandler()", hwnd);
ptrArrayDelete(commandHandlers, i); HASH_DEL(commandHandlers, ch);
uiFree(ch); uiFree(ch);
if (commandHandlers->len == 0) {
ptrArrayDestroy(commandHandlers);
commandHandlers = NULL;
}
return;
}
}
complain("window handle %p not registered with a WM_COMMAND handler in uiWindowsUnregisterWM_COMMANDHandler()", hwnd);
} }
BOOL runWM_COMMAND(WPARAM wParam, LPARAM lParam, LRESULT *lResult) BOOL runWM_COMMAND(WPARAM wParam, LPARAM lParam, LRESULT *lResult)
{ {
HWND control; HWND control;
struct commandHandler *ch; struct commandHandler *ch;
uintmax_t i;
// bounce back to the control in question // bounce back to the control in question
// 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 = (HWND) lParam; control = (HWND) lParam;
if (control != NULL && IsChild(utilWindow, control) == 0) { if (control != NULL && IsChild(utilWindow, control) == 0) {
for (i = 0; i < commandHandlers->len; i++) { HASH_FIND_PTR(commandHandlers, &control, ch);
ch = ptrArrayIndex(commandHandlers, struct commandHandler *, i); if (ch != NULL)
if (ch->hwnd == control) return (*(ch->handler))(ch->c, HIWORD(wParam), lResult);
return (*(ch->handler))(ch->c, HIWORD(wParam), lResult);
}
// not registered; fall out to return FALSE // not registered; fall out to return FALSE
} }
return FALSE; return FALSE;