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