2015-04-06 16:41:33 -05:00
|
|
|
// 6 april 2015
|
2015-04-06 23:26:27 -05:00
|
|
|
#include "uipriv_windows.h"
|
2015-04-06 16:41:33 -05:00
|
|
|
|
2015-04-16 08:44:06 -05:00
|
|
|
struct window {
|
|
|
|
uiWindow w;
|
2015-04-06 16:41:33 -05:00
|
|
|
HWND hwnd;
|
2015-04-12 21:39:36 -05:00
|
|
|
uiParent *content;
|
2015-04-06 16:41:33 -05:00
|
|
|
BOOL shownOnce;
|
|
|
|
int (*onClosing)(uiWindow *, void *);
|
|
|
|
void *onClosingData;
|
2015-04-09 12:51:01 -05:00
|
|
|
int margined;
|
2015-04-06 16:41:33 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
#define uiWindowClass L"uiWindowClass"
|
|
|
|
|
|
|
|
static LRESULT CALLBACK uiWindowWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
|
|
|
|
{
|
2015-04-16 08:44:06 -05:00
|
|
|
struct window *w;
|
2015-04-06 16:41:33 -05:00
|
|
|
CREATESTRUCTW *cs = (CREATESTRUCTW *) lParam;
|
2015-04-07 02:39:47 -05:00
|
|
|
WINDOWPOS *wp = (WINDOWPOS *) lParam;
|
2015-04-12 21:57:05 -05:00
|
|
|
RECT r;
|
|
|
|
HWND contenthwnd;
|
2015-04-06 16:41:33 -05:00
|
|
|
|
2015-04-16 08:44:06 -05:00
|
|
|
w = (struct window *) GetWindowLongPtrW(hwnd, GWLP_USERDATA);
|
2015-04-06 16:41:33 -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);
|
|
|
|
}
|
|
|
|
switch (uMsg) {
|
2015-04-08 19:37:32 -05:00
|
|
|
case WM_WINDOWPOSCHANGED:
|
2015-04-12 21:57:05 -05:00
|
|
|
if ((wp->flags & SWP_NOSIZE) != 0)
|
|
|
|
break;
|
2015-04-12 23:44:27 -05:00
|
|
|
// fall through
|
|
|
|
case msgUpdateChild:
|
2015-04-07 02:39:47 -05:00
|
|
|
if (GetClientRect(w->hwnd, &r) == 0)
|
|
|
|
logLastError("error getting window client rect for resize in uiWindowWndProc()");
|
2015-04-12 22:14:56 -05:00
|
|
|
contenthwnd = uiParentHWND(w->content);
|
2015-04-12 21:39:36 -05:00
|
|
|
if (MoveWindow(contenthwnd, r.left, r.top, r.right - r.left, r.bottom - r.top, TRUE) == 0)
|
|
|
|
logLastError("error resizing window content parent in uiWindowWndProc()");
|
2015-04-07 02:39:47 -05:00
|
|
|
return 0;
|
2015-04-06 16:41:33 -05:00
|
|
|
case WM_CLOSE:
|
|
|
|
if (!(*(w->onClosing))(w, w->onClosingData))
|
|
|
|
return 0;
|
|
|
|
break; // fall through to DefWindowProcW()
|
2015-04-07 21:46:15 -05:00
|
|
|
case WM_DESTROY:
|
2015-04-12 21:57:05 -05:00
|
|
|
// no need to free the child ourselves; it'll destroy itself after we leave this handler
|
2015-04-07 21:46:15 -05:00
|
|
|
uiFree(w);
|
|
|
|
break; // fall through to DefWindowProcW()
|
2015-04-06 16:41:33 -05:00
|
|
|
}
|
|
|
|
return DefWindowProcW(hwnd, uMsg, wParam, lParam);
|
|
|
|
}
|
|
|
|
|
|
|
|
ATOM registerWindowClass(HICON hDefaultIcon, HCURSOR hDefaultCursor)
|
|
|
|
{
|
|
|
|
WNDCLASSW wc;
|
|
|
|
|
|
|
|
ZeroMemory(&wc, sizeof (WNDCLASSW));
|
|
|
|
wc.lpszClassName = uiWindowClass;
|
|
|
|
wc.lpfnWndProc = uiWindowWndProc;
|
|
|
|
wc.hInstance = hInstance;
|
|
|
|
wc.hIcon = hDefaultIcon;
|
|
|
|
wc.hCursor = hDefaultCursor;
|
|
|
|
wc.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1);
|
|
|
|
return RegisterClassW(&wc);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define exstyle 0
|
|
|
|
#define style WS_OVERLAPPEDWINDOW
|
|
|
|
|
|
|
|
static int defaultOnClosing(uiWindow *w, void *data)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-04-16 08:44:06 -05:00
|
|
|
static void windowDestroy(uiWindow *ww)
|
2015-04-06 16:41:33 -05:00
|
|
|
{
|
2015-04-16 08:44:06 -05:00
|
|
|
struct window *w = (struct window *) ww;
|
2015-04-06 16:41:33 -05:00
|
|
|
|
|
|
|
DestroyWindow(w->hwnd);
|
|
|
|
}
|
|
|
|
|
2015-04-16 08:44:06 -05:00
|
|
|
static uintptr_t windowHandle(uiWindow *ww)
|
2015-04-06 16:41:33 -05:00
|
|
|
{
|
2015-04-16 08:44:06 -05:00
|
|
|
struct window *w = (struct window *) ww;
|
|
|
|
|
2015-04-06 16:41:33 -05:00
|
|
|
return (uintptr_t) (w->hwnd);
|
|
|
|
}
|
|
|
|
|
2015-04-16 08:44:06 -05:00
|
|
|
static char *windowTitle(uiWindow *ww)
|
2015-04-09 10:12:01 -05:00
|
|
|
{
|
2015-04-16 08:44:06 -05:00
|
|
|
struct window *w = (struct window *) ww;
|
2015-04-09 10:12:01 -05:00
|
|
|
WCHAR *wtext;
|
|
|
|
char *text;
|
|
|
|
|
|
|
|
wtext = windowText(w->hwnd);
|
|
|
|
text = toUTF8(wtext);
|
|
|
|
uiFree(wtext);
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2015-04-16 08:44:06 -05:00
|
|
|
static void windowSetTitle(uiWindow *ww, const char *text)
|
2015-04-09 10:12:01 -05:00
|
|
|
{
|
2015-04-16 08:44:06 -05:00
|
|
|
struct window *w = (struct window *) ww;
|
2015-04-09 10:12:01 -05:00
|
|
|
WCHAR *wtext;
|
|
|
|
|
|
|
|
wtext = toUTF16(text);
|
|
|
|
if (SetWindowTextW(w->hwnd, wtext) == 0)
|
|
|
|
logLastError("error setting window title in uiWindowSetTitle()");
|
|
|
|
uiFree(wtext);
|
|
|
|
}
|
2015-04-06 16:41:33 -05:00
|
|
|
|
2015-04-16 08:59:05 -05:00
|
|
|
static void windowShow(uiWindow *ww)
|
2015-04-06 16:41:33 -05:00
|
|
|
{
|
2015-04-16 08:44:06 -05:00
|
|
|
struct window *w = (struct window *) ww;
|
|
|
|
|
2015-04-06 16:41:33 -05:00
|
|
|
if (w->shownOnce) {
|
|
|
|
ShowWindow(w->hwnd, SW_SHOW);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
w->shownOnce = TRUE;
|
|
|
|
ShowWindow(w->hwnd, nCmdShow);
|
|
|
|
if (UpdateWindow(w->hwnd) == 0)
|
|
|
|
logLastError("error calling UpdateWindow() after showing uiWindow for the first time");
|
|
|
|
}
|
|
|
|
|
2015-04-16 08:59:05 -05:00
|
|
|
static void windowHide(uiWindow *ww)
|
2015-04-06 16:41:33 -05:00
|
|
|
{
|
2015-04-16 08:44:06 -05:00
|
|
|
struct window *w = (struct window *) ww;
|
|
|
|
|
2015-04-06 16:41:33 -05:00
|
|
|
ShowWindow(w->hwnd, SW_HIDE);
|
|
|
|
}
|
|
|
|
|
2015-04-16 08:44:06 -05:00
|
|
|
static void windowOnClosing(uiWindow *ww, int (*f)(uiWindow *, void *), void *data)
|
2015-04-06 16:41:33 -05:00
|
|
|
{
|
2015-04-16 08:44:06 -05:00
|
|
|
struct window *w = (struct window *) ww;
|
|
|
|
|
2015-04-06 16:41:33 -05:00
|
|
|
w->onClosing = f;
|
|
|
|
w->onClosingData = data;
|
|
|
|
}
|
2015-04-07 03:02:21 -05:00
|
|
|
|
2015-04-16 08:44:06 -05:00
|
|
|
static void windowSetChild(uiWindow *ww, uiControl *c)
|
2015-04-07 03:02:21 -05:00
|
|
|
{
|
2015-04-16 08:59:05 -05:00
|
|
|
struct window *w = (struct window *) ww;
|
2015-04-16 08:44:06 -05:00
|
|
|
|
2015-04-12 21:39:36 -05:00
|
|
|
uiParentSetChild(w->content, c);
|
2015-04-12 23:44:27 -05:00
|
|
|
// don't call uiParentUpdate(); instead, synthesize a resize
|
|
|
|
// otherwise, we'll have a 0x0 content area at first
|
|
|
|
SendMessageW(w->hwnd, msgUpdateChild, 0, 0);
|
2015-04-07 03:02:21 -05:00
|
|
|
}
|
2015-04-09 12:51:01 -05:00
|
|
|
|
2015-04-16 08:59:05 -05:00
|
|
|
static int windowMargined(uiWindow *ww)
|
2015-04-09 20:11:56 -05:00
|
|
|
{
|
2015-04-16 08:44:06 -05:00
|
|
|
struct window *w = (struct window *) ww;
|
|
|
|
|
2015-04-09 20:11:56 -05:00
|
|
|
return w->margined;
|
|
|
|
}
|
2015-04-09 12:51:01 -05:00
|
|
|
|
2015-04-12 21:39:36 -05:00
|
|
|
// from https://msdn.microsoft.com/en-us/library/windows/desktop/dn742486.aspx#sizingandspacing
|
|
|
|
#define windowMargin 7
|
|
|
|
|
2015-04-16 08:59:05 -05:00
|
|
|
static void windowSetMargined(uiWindow *ww, int margined)
|
2015-04-09 12:51:01 -05:00
|
|
|
{
|
2015-04-16 08:44:06 -05:00
|
|
|
struct window *w = (struct window *) ww;
|
|
|
|
|
2015-04-09 12:51:01 -05:00
|
|
|
w->margined = margined;
|
2015-04-12 21:39:36 -05:00
|
|
|
if (w->margined)
|
|
|
|
uiParentSetMargins(w->content, windowMargin, windowMargin, windowMargin, windowMargin);
|
|
|
|
else
|
|
|
|
uiParentSetMargins(w->content, 0, 0, 0, 0);
|
|
|
|
uiParentUpdate(w->content);
|
2015-04-09 12:51:01 -05:00
|
|
|
}
|
2015-04-16 08:44:06 -05:00
|
|
|
|
2015-04-16 08:59:05 -05:00
|
|
|
uiWindow *uiNewWindow(const char *title, int width, int height)
|
2015-04-16 08:44:06 -05:00
|
|
|
{
|
|
|
|
struct window *w;
|
|
|
|
RECT adjust;
|
|
|
|
WCHAR *wtitle;
|
|
|
|
|
|
|
|
w = uiNew(struct window);
|
|
|
|
w->onClosing = defaultOnClosing;
|
|
|
|
|
|
|
|
adjust.left = 0;
|
|
|
|
adjust.top = 0;
|
|
|
|
adjust.right = width;
|
|
|
|
adjust.bottom = height;
|
|
|
|
if (AdjustWindowRectEx(&adjust, style, FALSE, exstyle) == 0)
|
|
|
|
logLastError("error getting real window coordinates in uiWindow()");
|
|
|
|
|
|
|
|
wtitle = toUTF16(title);
|
|
|
|
w->hwnd = CreateWindowExW(exstyle,
|
|
|
|
uiWindowClass, wtitle,
|
|
|
|
style,
|
|
|
|
CW_USEDEFAULT, CW_USEDEFAULT,
|
|
|
|
adjust.right - adjust.left, adjust.bottom - adjust.top,
|
|
|
|
NULL, NULL, hInstance, w);
|
|
|
|
if (w->hwnd == NULL)
|
|
|
|
logLastError("error creating window in uiWindow()");
|
|
|
|
uiFree(wtitle);
|
|
|
|
|
|
|
|
w->content = uiNewParent((uintptr_t) (w->hwnd));
|
|
|
|
|
|
|
|
uiWindow(w)->Destroy = windowDestroy;
|
|
|
|
uiWindow(w)->Handle = windowHandle;
|
|
|
|
uiWindow(w)->Title = windowTitle;
|
|
|
|
uiWindow(w)->SetTitle = windowSetTitle;
|
|
|
|
uiWindow(w)->Show = windowShow;
|
|
|
|
uiWindow(w)->Hide = windowHide;
|
|
|
|
uiWindow(w)->OnClosing = windowOnClosing;
|
|
|
|
uiWindow(w)->SetChild = windowSetChild;
|
|
|
|
uiWindow(w)->Margined = windowMargined;
|
|
|
|
uiWindow(w)->SetMargined = windowSetMargined;
|
|
|
|
|
|
|
|
return uiWindow(w);
|
|
|
|
}
|