2015-05-14 19:40:41 -05:00
|
|
|
// 27 april 2015
|
|
|
|
#include "uipriv_windows.h"
|
|
|
|
|
|
|
|
#define windowClass L"libui_uiWindowClass"
|
|
|
|
|
2015-08-30 18:32:05 -05:00
|
|
|
struct uiWindow {
|
|
|
|
uiWindowsControl c;
|
2015-05-14 19:40:41 -05:00
|
|
|
HWND hwnd;
|
|
|
|
HMENU menubar;
|
2015-08-30 18:32:05 -05:00
|
|
|
struct child *child;
|
2015-05-14 19:40:41 -05:00
|
|
|
BOOL shownOnce;
|
|
|
|
int (*onClosing)(uiWindow *, void *);
|
|
|
|
void *onClosingData;
|
|
|
|
int margined;
|
|
|
|
};
|
|
|
|
|
2015-08-30 18:32:05 -05:00
|
|
|
static void onDestroy(uiWindow *);
|
|
|
|
|
|
|
|
uiWindowsDefineControlWithOnDestroy(
|
|
|
|
uiWindow, // type name
|
|
|
|
uiWindowType, // type function
|
|
|
|
onDestroy(this); // on destroy
|
|
|
|
)
|
2015-05-29 21:44:48 -05:00
|
|
|
|
2015-05-14 19:40:41 -05:00
|
|
|
static LRESULT CALLBACK windowWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
|
|
|
|
{
|
2015-08-30 18:32:05 -05:00
|
|
|
uiWindow *w;
|
2015-05-14 19:40:41 -05:00
|
|
|
CREATESTRUCTW *cs = (CREATESTRUCTW *) lParam;
|
|
|
|
WINDOWPOS *wp = (WINDOWPOS *) lParam;
|
2015-05-18 08:52:37 -05:00
|
|
|
LRESULT lResult;
|
2015-05-14 19:40:41 -05:00
|
|
|
|
2015-08-30 18:32:05 -05:00
|
|
|
w = uiWindow((void *) GetWindowLongPtrW(hwnd, GWLP_USERDATA));
|
2015-05-14 19:40:41 -05:00
|
|
|
if (w == NULL) {
|
|
|
|
if (uMsg == WM_CREATE)
|
|
|
|
SetWindowLongPtrW(hwnd, GWLP_USERDATA, (LONG_PTR) (cs->lpCreateParams));
|
|
|
|
// fall through to DefWindowProc() anyway
|
|
|
|
return DefWindowProcW(hwnd, uMsg, wParam, lParam);
|
|
|
|
}
|
2015-05-18 08:52:37 -05:00
|
|
|
if (handleParentMessages(hwnd, uMsg, wParam, lParam, &lResult) != FALSE)
|
|
|
|
return lResult;
|
2015-05-14 19:40:41 -05:00
|
|
|
switch (uMsg) {
|
|
|
|
case WM_COMMAND:
|
|
|
|
// not a menu
|
|
|
|
if (lParam != 0)
|
|
|
|
break;
|
|
|
|
if (HIWORD(wParam) != 0)
|
|
|
|
break;
|
|
|
|
runMenuEvent(LOWORD(wParam), uiWindow(w));
|
|
|
|
return 0;
|
|
|
|
case WM_WINDOWPOSCHANGED:
|
|
|
|
if ((wp->flags & SWP_NOSIZE) != 0)
|
|
|
|
break;
|
|
|
|
if (w->child != NULL)
|
2015-05-17 20:15:39 -05:00
|
|
|
uiControlQueueResize(uiControl(w));
|
2015-05-14 19:40:41 -05:00
|
|
|
return 0;
|
2015-06-07 21:06:41 -05:00
|
|
|
case WM_PRINTCLIENT:
|
|
|
|
// we do no special painting; just erase the background
|
|
|
|
// don't worry about the return value; we let DefWindowProcW() handle this message
|
|
|
|
SendMessageW(hwnd, WM_ERASEBKGND, wParam, lParam);
|
|
|
|
return 0;
|
2015-05-14 19:40:41 -05:00
|
|
|
case WM_CLOSE:
|
2015-08-30 18:32:05 -05:00
|
|
|
if ((*(w->onClosing))(w, w->onClosingData))
|
2015-05-14 19:40:41 -05:00
|
|
|
uiControlDestroy(uiControl(w));
|
|
|
|
return 0; // we destroyed it already
|
|
|
|
}
|
|
|
|
return DefWindowProcW(hwnd, uMsg, wParam, lParam);
|
|
|
|
}
|
|
|
|
|
|
|
|
ATOM registerWindowClass(HICON hDefaultIcon, HCURSOR hDefaultCursor)
|
|
|
|
{
|
|
|
|
WNDCLASSW wc;
|
|
|
|
|
|
|
|
ZeroMemory(&wc, sizeof (WNDCLASSW));
|
|
|
|
wc.lpszClassName = windowClass;
|
|
|
|
wc.lpfnWndProc = windowWndProc;
|
|
|
|
wc.hInstance = hInstance;
|
|
|
|
wc.hIcon = hDefaultIcon;
|
|
|
|
wc.hCursor = hDefaultCursor;
|
|
|
|
wc.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1);
|
|
|
|
return RegisterClassW(&wc);
|
|
|
|
}
|
|
|
|
|
|
|
|
void unregisterWindowClass(void)
|
|
|
|
{
|
|
|
|
if (UnregisterClassW(windowClass, hInstance) == 0)
|
|
|
|
logLastError("error unregistering uiWindow window class in unregisterWindowClass()");
|
|
|
|
}
|
|
|
|
|
|
|
|
static int defaultOnClosing(uiWindow *w, void *data)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-30 18:32:05 -05:00
|
|
|
static void onDestroy(uiWindow *w)
|
2015-05-14 19:40:41 -05:00
|
|
|
{
|
|
|
|
// first hide ourselves
|
|
|
|
ShowWindow(w->hwnd, SW_HIDE);
|
|
|
|
// now destroy the child
|
2015-08-30 18:32:05 -05:00
|
|
|
if (w->child != NULL)
|
|
|
|
childDestroy(w->child);
|
2015-05-14 19:40:41 -05:00
|
|
|
// now free the menubar, if any
|
|
|
|
if (w->menubar != NULL)
|
|
|
|
freeMenubar(w->menubar);
|
2015-05-15 14:39:45 -05:00
|
|
|
// and finally destroy ourselves
|
2015-05-22 16:41:36 -05:00
|
|
|
dialogHelperUnregisterWindow(w->hwnd);
|
2015-05-15 14:39:45 -05:00
|
|
|
}
|
|
|
|
|
2015-05-30 20:24:33 -05:00
|
|
|
static void windowCommitShow(uiControl *c)
|
2015-05-15 14:39:45 -05:00
|
|
|
{
|
2015-08-30 18:32:05 -05:00
|
|
|
uiWindow *w = uiWindow(c);
|
2015-05-15 14:39:45 -05:00
|
|
|
|
|
|
|
if (w->shownOnce) {
|
|
|
|
ShowWindow(w->hwnd, SW_SHOW);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
w->shownOnce = TRUE;
|
2015-05-16 10:37:45 -05:00
|
|
|
// make sure the child is the correct size
|
2015-05-17 20:15:39 -05:00
|
|
|
uiControlQueueResize(uiControl(w));
|
2015-05-15 14:39:45 -05:00
|
|
|
ShowWindow(w->hwnd, nCmdShow);
|
|
|
|
if (UpdateWindow(w->hwnd) == 0)
|
|
|
|
logLastError("error calling UpdateWindow() after showing uiWindow for the first time in windowShow()");
|
|
|
|
}
|
|
|
|
|
2015-06-03 16:57:04 -05:00
|
|
|
static void windowContainerUpdateState(uiControl *c)
|
|
|
|
{
|
2015-08-30 18:32:05 -05:00
|
|
|
uiWindow *w = uiWindow(c);
|
2015-06-03 16:57:04 -05:00
|
|
|
|
|
|
|
if (w->child != NULL)
|
2015-08-30 18:32:05 -05:00
|
|
|
childContainerUpdateState(w->child);
|
2015-06-03 16:57:04 -05:00
|
|
|
}
|
2015-05-15 14:39:45 -05:00
|
|
|
|
2015-08-30 18:32:05 -05:00
|
|
|
char *uiWindowTitle(uiWindow *w)
|
2015-05-14 19:40:41 -05:00
|
|
|
{
|
2015-06-03 14:49:44 -05:00
|
|
|
return uiWindowsUtilText(w->hwnd);
|
2015-05-14 19:40:41 -05:00
|
|
|
}
|
|
|
|
|
2015-08-30 18:32:05 -05:00
|
|
|
void uiWindowSetTitle(uiWindow *w, const char *title)
|
2015-05-14 19:40:41 -05:00
|
|
|
{
|
2015-06-03 14:49:44 -05:00
|
|
|
uiWindowsUtilSetText(w->hwnd, title);
|
|
|
|
// don't queue resize; the caption isn't part of what affects layout and sizing of the client area (it'll be ellipsized if too long)
|
2015-05-14 19:40:41 -05:00
|
|
|
}
|
|
|
|
|
2015-08-30 18:32:05 -05:00
|
|
|
void uiWindowOnClosing(uiWindow *ww, int (*f)(uiWindow *, void *), void *data)
|
2015-05-14 19:40:41 -05:00
|
|
|
{
|
|
|
|
w->onClosing = f;
|
|
|
|
w->onClosingData = data;
|
|
|
|
}
|
|
|
|
|
2015-08-30 18:32:05 -05:00
|
|
|
void uiWindowSetChild(uiWindow *w, uiControl *child)
|
2015-05-14 19:40:41 -05:00
|
|
|
{
|
|
|
|
if (w->child != NULL)
|
2015-08-30 18:32:05 -05:00
|
|
|
childRemove(w->child);
|
|
|
|
w->child = newChild(child, uiControl(w), w->hwnd);
|
|
|
|
if (w->child != NULL)
|
2015-05-17 20:15:39 -05:00
|
|
|
uiControlQueueResize(w->child);
|
2015-05-14 19:40:41 -05:00
|
|
|
}
|
|
|
|
|
2015-08-30 18:32:05 -05:00
|
|
|
int uiWindowMargined(uiWindow *w)
|
2015-05-14 19:40:41 -05:00
|
|
|
{
|
|
|
|
return w->margined;
|
|
|
|
}
|
|
|
|
|
2015-08-30 18:32:05 -05:00
|
|
|
void uiWindowSetMargined(uiWindow *w, int margined)
|
2015-05-14 19:40:41 -05:00
|
|
|
{
|
|
|
|
w->margined = margined;
|
2015-05-17 20:15:39 -05:00
|
|
|
uiControlQueueResize(uiControl(w));
|
|
|
|
}
|
|
|
|
|
|
|
|
// from https://msdn.microsoft.com/en-us/library/windows/desktop/dn742486.aspx#sizingandspacing
|
|
|
|
#define windowMargin 7
|
|
|
|
|
2015-05-30 21:18:30 -05:00
|
|
|
static void windowResizeChild(uiWindow *ww)
|
2015-05-17 20:15:39 -05:00
|
|
|
{
|
2015-05-30 21:18:30 -05:00
|
|
|
struct window *w = (struct window *) ww;
|
2015-05-17 20:15:39 -05:00
|
|
|
RECT r;
|
2015-05-18 10:46:50 -05:00
|
|
|
uiSizing *d;
|
2015-05-17 20:15:39 -05:00
|
|
|
|
|
|
|
if (w->child == NULL)
|
|
|
|
return;
|
|
|
|
if (GetClientRect(w->hwnd, &r) == 0)
|
|
|
|
logLastError("error getting uiWindow client rect in windowComputeChildSize()");
|
2015-06-03 16:42:08 -05:00
|
|
|
d = uiControlSizing(uiControl(w));
|
2015-05-17 20:15:39 -05:00
|
|
|
if (w->margined) {
|
2015-06-03 16:42:08 -05:00
|
|
|
r.left += uiWindowsDlgUnitsToX(windowMargin, d->Sys->BaseX);
|
|
|
|
r.top += uiWindowsDlgUnitsToY(windowMargin, d->Sys->BaseY);
|
|
|
|
r.right -= uiWindowsDlgUnitsToX(windowMargin, d->Sys->BaseX);
|
|
|
|
r.bottom -= uiWindowsDlgUnitsToY(windowMargin, d->Sys->BaseY);
|
2015-05-17 20:15:39 -05:00
|
|
|
}
|
2015-05-18 10:46:50 -05:00
|
|
|
uiControlResize(w->child, r.left, r.top, r.right - r.left, r.bottom - r.top, d);
|
|
|
|
uiFreeSizing(d);
|
2015-05-14 19:40:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// see http://blogs.msdn.com/b/oldnewthing/archive/2003/09/11/54885.aspx and http://blogs.msdn.com/b/oldnewthing/archive/2003/09/13/54917.aspx
|
2015-08-30 18:32:05 -05:00
|
|
|
static void setClientSize(uiWindow *w, int width, int height, BOOL hasMenubar, DWORD style, DWORD exstyle)
|
2015-05-14 19:40:41 -05:00
|
|
|
{
|
|
|
|
RECT window;
|
|
|
|
|
|
|
|
window.left = 0;
|
|
|
|
window.top = 0;
|
|
|
|
window.right = width;
|
|
|
|
window.bottom = height;
|
|
|
|
if (AdjustWindowRectEx(&window, style, hasMenubar, exstyle) == 0)
|
|
|
|
logLastError("error getting real window coordinates in setClientSize()");
|
|
|
|
if (hasMenubar) {
|
|
|
|
RECT temp;
|
|
|
|
|
|
|
|
temp = window;
|
|
|
|
temp.bottom = 0x7FFF; // infinite height
|
|
|
|
SendMessageW(w->hwnd, WM_NCCALCSIZE, (WPARAM) FALSE, (LPARAM) (&temp));
|
|
|
|
window.bottom += temp.top;
|
|
|
|
}
|
|
|
|
if (SetWindowPos(w->hwnd, NULL, 0, 0, window.right - window.left, window.bottom - window.top, SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOZORDER) == 0)
|
|
|
|
logLastError("error resizing window in setClientSize()");
|
|
|
|
}
|
|
|
|
|
|
|
|
uiWindow *uiNewWindow(const char *title, int width, int height, int hasMenubar)
|
|
|
|
{
|
2015-08-30 18:32:05 -05:00
|
|
|
uiWindow *w;
|
2015-05-14 19:40:41 -05:00
|
|
|
WCHAR *wtitle;
|
|
|
|
BOOL hasMenubarBOOL;
|
|
|
|
|
2015-08-30 18:32:05 -05:00
|
|
|
w = (uiWindow *) uiNewControl(uiWindowType());
|
2015-05-14 19:40:41 -05:00
|
|
|
|
|
|
|
hasMenubarBOOL = FALSE;
|
|
|
|
if (hasMenubar)
|
|
|
|
hasMenubarBOOL = TRUE;
|
|
|
|
|
|
|
|
#define style WS_OVERLAPPEDWINDOW
|
|
|
|
#define exstyle 0
|
|
|
|
|
|
|
|
wtitle = toUTF16(title);
|
|
|
|
w->hwnd = CreateWindowExW(exstyle,
|
|
|
|
windowClass, wtitle,
|
|
|
|
style,
|
|
|
|
CW_USEDEFAULT, CW_USEDEFAULT,
|
|
|
|
// use the raw width and height for now
|
|
|
|
// this will get CW_USEDEFAULT (hopefully) predicting well
|
|
|
|
// even if it doesn't, we're adjusting it later
|
|
|
|
width, height,
|
|
|
|
NULL, NULL, hInstance, w);
|
|
|
|
if (w->hwnd == NULL)
|
|
|
|
logLastError("error creating window in uiWindow()");
|
|
|
|
uiFree(wtitle);
|
|
|
|
|
2015-05-22 16:41:36 -05:00
|
|
|
dialogHelperRegisterWindow(w->hwnd);
|
|
|
|
|
2015-05-14 19:40:41 -05:00
|
|
|
if (hasMenubar) {
|
|
|
|
w->menubar = makeMenubar();
|
|
|
|
if (SetMenu(w->hwnd, w->menubar) == 0)
|
|
|
|
logLastError("error giving menu to window in uiNewWindow()");
|
|
|
|
}
|
|
|
|
|
|
|
|
// and use the proper size
|
|
|
|
setClientSize(w, width, height, hasMenubarBOOL, style, exstyle);
|
|
|
|
|
2015-08-30 18:32:05 -05:00
|
|
|
uiWindowSetOnClosing(w, defaultOnClosing, NULL);
|
2015-05-14 19:40:41 -05:00
|
|
|
|
2015-08-30 18:32:05 -05:00
|
|
|
uiWindowsFinishNewControl(w, uiWindow);
|
2015-05-30 20:30:40 -05:00
|
|
|
uiControl(w)->CommitShow = windowCommitShow;
|
2015-06-03 16:57:04 -05:00
|
|
|
uiControl(w)->ContainerUpdateState = windowContainerUpdateState;
|
2015-05-14 19:40:41 -05:00
|
|
|
|
2015-08-30 18:32:05 -05:00
|
|
|
return w;
|
2015-05-14 19:40:41 -05:00
|
|
|
}
|