Added uiMainStep() and implemented it on Windows.

This commit is contained in:
Pietro Gagliardi 2016-05-24 22:14:05 -04:00
parent c349edda5e
commit a9e731ed59
2 changed files with 63 additions and 20 deletions

1
ui.h
View File

@ -36,6 +36,7 @@ _UI_EXTERN void uiUninit(void);
_UI_EXTERN void uiFreeInitError(const char *err); _UI_EXTERN void uiFreeInitError(const char *err);
_UI_EXTERN void uiMain(void); _UI_EXTERN void uiMain(void);
_UI_EXTERN int uiMainStep(int wait);
_UI_EXTERN void uiQuit(void); _UI_EXTERN void uiQuit(void);
_UI_EXTERN void uiQueueMain(void (*f)(void *data), void *data); _UI_EXTERN void uiQueueMain(void (*f)(void *data), void *data);

View File

@ -41,31 +41,73 @@ void unregisterMessageFilter(void)
logLastError(L"error unregistering libui message filter"); logLastError(L"error unregistering libui message filter");
} }
// TODO http://blogs.msdn.com/b/oldnewthing/archive/2005/04/08/406509.aspx when adding accelerators, TranslateAccelerators() before IsDialogMessage() // LONGTERM http://blogs.msdn.com/b/oldnewthing/archive/2005/04/08/406509.aspx when adding accelerators, TranslateAccelerators() before IsDialogMessage()
static void processMessage(MSG *msg)
{
HWND active;
// TODO really active? or parentToplevel(msg->hwnd)?
active = GetActiveWindow();
if (active != NULL)
// TODO find documentation that says IsDialogMessage() calls CallMsgFilter() for us, because that's what's happening
if (IsDialogMessage(active, msg) != 0)
return;
TranslateMessage(msg);
DispatchMessageW(msg);
}
static int waitMessage(MSG *msg)
{
int res;
res = GetMessageW(msg, NULL, 0, 0);
if (res < 0) {
logLastError(L"error calling GetMessage()");
return 0; // bail out on error
}
return res != 0; // returns false on WM_QUIT
}
void uiMain(void) void uiMain(void)
{ {
MSG msg; while (uiMainStep(1))
int res; ;
HWND active; }
for (;;) { static int peekMessage(MSG *msg)
res = GetMessageW(&msg, NULL, 0, 0); {
if (res < 0) { BOOL res;
logLastError(L"error calling GetMessage()");
break; // bail out on error res = PeekMessageW(msg, NULL, 0, 0, PM_REMOVE);
} if (res == 0)
if (res == 0) // WM_QUIT return 2; // no message available
break; if (msg->message != WM_QUIT)
// TODO really active? or parentToplevel(msg->hwnd)? return 1; // a message
active = GetActiveWindow(); return 0; // WM_QUIT
if (active != NULL) }
// TODO find documentation that says IsDialogMessage() calls CallMsgFilter() for us, because that's what's happening
if (IsDialogMessage(active, &msg) != 0) int uiMainStep(int wait)
continue; {
TranslateMessage(&msg); MSG msg;
DispatchMessageW(&msg);
if (wait) {
if (!waitMessage(&msg))
return 0;
processMessage(&msg);
return 1;
} }
// don't wait for a message
switch (peekMessage(&msg)) {
case 0: // quit
// TODO PostQuitMessage() again?
return 0;
case 1: // process a message
processMessage(&msg);
// fall out to the case for no message
}
return 1; // no message
} }
void uiQuit(void) void uiQuit(void)