Set up the boilerplate for dialog boxes.

This commit is contained in:
Pietro Gagliardi 2015-05-22 17:41:36 -04:00
parent d9f94d4326
commit e7102c0c61
5 changed files with 124 additions and 0 deletions

View File

@ -9,6 +9,7 @@ osCFILES = \
windows/control.c \
windows/datetimepicker.c \
windows/debug.c \
windows/dialoghelper.c \
windows/entry.c \
windows/events.c \
windows/group.c \

110
redo/windows/dialoghelper.c Normal file
View File

@ -0,0 +1,110 @@
// 22 may 2015
#include "uipriv_windows.h"
// see http://stackoverflow.com/questions/25494914/is-there-something-like-cdn-filecancel-analogous-to-cdn-fileok-for-getting-when#comment40420049_25494914
// TODO reconcile this with users being able to enable/disable windows
struct dialogDisableWindow {
HWND hwnd;
uintmax_t n;
UT_hash_handle hh;
};
static struct dialogDisableWindow *windows = NULL;
void dialogHelperRegisterWindow(HWND hwnd)
{
struct dialogDisableWindow *d;
HASH_FIND_PTR(windows, &hwnd, d);
if (d != NULL)
complain("window handle %p already register in dialogHelperRegisterWindow()", hwnd);
d = uiNew(struct dialogDisableWindow);
d->hwnd = hwnd;
HASH_ADD_PTR(windows, hwnd, d);
}
void dialogHelperUnregisterWindow(HWND hwnd)
{
struct dialogDisableWindow *d;
HASH_FIND_PTR(windows, &hwnd, d);
if (d == NULL)
complain("window handle %p not registered in dialogHelperUnregisterWindow()", hwnd);
HASH_DEL(windows, d);
uiFree(d);
}
static void dialogBegin(void)
{
struct dialogDisableWindow *d;
for (d = windows; d != NULL; d = d->hh.next) {
EnableWindow(d->hwnd, FALSE);
d->n++;
}
}
static void dialogEnd(void)
{
struct dialogDisableWindow *d;
for (d = windows; d != NULL; d = d->hh.next) {
d->n--;
if (d->n == 0)
EnableWindow(d->hwnd, TRUE);
}
}
#define dialogHelperClass L"libui_dialogHelperClass"
static LRESULT CALLBACK dialogHelperWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg) {
case WM_CREATE:
dialogBegin();
break;
case WM_ENABLE:
if (wParam != (WPARAM) FALSE) // enabling
dialogEnd();
break;
}
return DefWindowProcW(hwnd, uMsg, wParam, lParam);
}
ATOM initDialogHelper(HICON hDefaultIcon, HCURSOR hDefaultCursor)
{
WNDCLASSW wc;
ZeroMemory(&wc, sizeof (WNDCLASSW));
wc.lpszClassName = dialogHelperClass;
wc.lpfnWndProc = dialogHelperWndProc;
wc.hInstance = hInstance;
wc.hIcon = hDefaultIcon;
wc.hCursor = hDefaultCursor;
wc.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1);
return RegisterClassW(&wc);
}
uintptr_t beginDialogHelper(void)
{
HWND hwnd;
hwnd = CreateWindowExW(0,
dialogHelperClass, L"libui dialog helper",
WS_OVERLAPPEDWINDOW,
0, 0, 100, 100,
NULL, NULL, hInstance, NULL);
if (hwnd == NULL)
logLastError("error creating dialog helper in beginDialogHelper()");
return (uintptr_t) hwnd;
}
void endDialog(uintptr_t handle)
{
HWND hwnd = (HWND) handle;
if (DestroyWindow(hwnd) == 0)
logLastError("error cleaning up after dialog helper in endDialogHelper()");
}

View File

@ -141,6 +141,9 @@ const char *uiInit(uiInitOptions *o)
if (InitCommonControlsEx(&icc) == 0)
return loadLastError("initializing Common Controls");
if (initDialogHelper(hDefaultIcon, hDefaultCursor) == 0)
return loadLastError("initializing the dialog helper");
return NULL;
}

View File

@ -120,3 +120,10 @@ extern void uiWindowsUnregisterWM_HSCROLLHandler(HWND);
extern BOOL runWM_COMMAND(WPARAM, LPARAM, LRESULT *);
extern BOOL runWM_NOTIFY(WPARAM, LPARAM, LRESULT *);
extern BOOL runWM_HSCROLL(WPARAM, LPARAM, LRESULT *);
// dialoghelper.c
extern void dialogHelperRegisterWindow(HWND);
extern void dialogHelperUnregisterWindow(HWND);
extern ATOM initDialogHelper(HICON, HCURSOR);
extern uintptr_t beginDialogHelper(void);
extern void endDialog(uintptr_t);

View File

@ -97,6 +97,7 @@ static void windowDestroy(uiControl *c)
if (w->menubar != NULL)
freeMenubar(w->menubar);
// and finally destroy ourselves
dialogHelperUnregisterWindow(w->hwnd);
if (DestroyWindow(w->hwnd) == 0)
logLastError("error destroying uiWindow in windowDestroy()");
uiFree(w);
@ -351,6 +352,8 @@ uiWindow *uiNewWindow(const char *title, int width, int height, int hasMenubar)
logLastError("error creating window in uiWindow()");
uiFree(wtitle);
dialogHelperRegisterWindow(w->hwnd);
if (hasMenubar) {
w->menubar = makeMenubar();
if (SetMenu(w->hwnd, w->menubar) == 0)