From 1fcd3354d3f64caa14e2fec4a0816932d627ca67 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Thu, 21 May 2015 11:02:10 -0400 Subject: [PATCH] Converted windows/events.c to use uthash. --- redo/windows/events.c | 46 ++++++++++++++++++------------------------- 1 file changed, 19 insertions(+), 27 deletions(-) diff --git a/redo/windows/events.c b/redo/windows/events.c index d7d9bc2c..c71f0013 100644 --- a/redo/windows/events.c +++ b/redo/windows/events.c @@ -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;