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
|
|
|
|
|
|
|
struct uiWindow {
|
|
|
|
HWND hwnd;
|
2015-04-07 02:39:47 -05:00
|
|
|
uiControl *child;
|
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"
|
|
|
|
|
2015-04-09 22:24:18 -05:00
|
|
|
// from https://msdn.microsoft.com/en-us/library/windows/desktop/dn742486.aspx#sizingandspacing
|
2015-04-09 12:51:01 -05:00
|
|
|
#define windowMargin 7
|
|
|
|
|
2015-04-06 16:41:33 -05:00
|
|
|
static LRESULT CALLBACK uiWindowWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
|
|
|
|
{
|
|
|
|
uiWindow *w;
|
|
|
|
CREATESTRUCTW *cs = (CREATESTRUCTW *) lParam;
|
2015-04-07 02:12:34 -05:00
|
|
|
LRESULT lResult;
|
2015-04-07 02:39:47 -05:00
|
|
|
WINDOWPOS *wp = (WINDOWPOS *) lParam;
|
2015-04-09 12:51:01 -05:00
|
|
|
RECT r, margin;
|
2015-04-06 16:41:33 -05:00
|
|
|
|
|
|
|
w = (uiWindow *) GetWindowLongPtrW(hwnd, GWLP_USERDATA);
|
|
|
|
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-04-07 02:12:34 -05:00
|
|
|
if (sharedWndProc(hwnd, uMsg, wParam, lParam, &lResult) != FALSE)
|
2015-04-07 01:55:33 -05:00
|
|
|
return lResult;
|
2015-04-06 16:41:33 -05:00
|
|
|
switch (uMsg) {
|
2015-04-08 19:37:32 -05:00
|
|
|
case WM_WINDOWPOSCHANGED:
|
2015-04-07 02:39:47 -05:00
|
|
|
if ((wp->flags & SWP_NOSIZE) != 0)
|
|
|
|
break;
|
2015-04-08 19:53:34 -05:00
|
|
|
// fall through
|
|
|
|
case msgUpdateChild:
|
|
|
|
if (w->child == NULL)
|
|
|
|
break;
|
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-09 12:51:01 -05:00
|
|
|
margin.left = 0;
|
|
|
|
margin.top = 0;
|
|
|
|
margin.right = 0;
|
|
|
|
margin.bottom = 0;
|
|
|
|
if (w->margined) {
|
|
|
|
margin.left = windowMargin;
|
|
|
|
margin.top = windowMargin;
|
|
|
|
margin.right = windowMargin;
|
|
|
|
margin.bottom = windowMargin;
|
|
|
|
}
|
|
|
|
resize(w->child, w->hwnd, r, margin);
|
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-07 23:43:35 -05:00
|
|
|
if (w->child != NULL)
|
|
|
|
uiControlDestroy(w->child);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
uiWindow *uiNewWindow(char *title, int width, int height)
|
|
|
|
{
|
|
|
|
uiWindow *w;
|
|
|
|
RECT adjust;
|
|
|
|
WCHAR *wtitle;
|
|
|
|
|
2015-04-07 21:48:02 -05:00
|
|
|
w = uiNew(uiWindow);
|
2015-04-07 21:58:06 -05:00
|
|
|
// TODO move other cases of default events here relative
|
2015-04-06 16:41:33 -05:00
|
|
|
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);
|
|
|
|
return w;
|
|
|
|
}
|
|
|
|
|
|
|
|
void uiWindowDestroy(uiWindow *w)
|
|
|
|
{
|
|
|
|
DestroyWindow(w->hwnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
uintptr_t uiWindowHandle(uiWindow *w)
|
|
|
|
{
|
|
|
|
return (uintptr_t) (w->hwnd);
|
|
|
|
}
|
|
|
|
|
2015-04-09 10:12:01 -05:00
|
|
|
char *uiWindowTitle(uiWindow *w)
|
|
|
|
{
|
|
|
|
WCHAR *wtext;
|
|
|
|
char *text;
|
|
|
|
|
|
|
|
wtext = windowText(w->hwnd);
|
|
|
|
text = toUTF8(wtext);
|
|
|
|
uiFree(wtext);
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
|
|
|
void uiWindowSetTitle(uiWindow *w, const char *text)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
|
|
|
void uiWindowShow(uiWindow *w)
|
|
|
|
{
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
|
|
|
void uiWindowHide(uiWindow *w)
|
|
|
|
{
|
|
|
|
ShowWindow(w->hwnd, SW_HIDE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void uiWindowOnClosing(uiWindow *w, int (*f)(uiWindow *, void *), void *data)
|
|
|
|
{
|
|
|
|
w->onClosing = f;
|
|
|
|
w->onClosingData = data;
|
|
|
|
}
|
2015-04-07 03:02:21 -05:00
|
|
|
|
|
|
|
void uiWindowSetChild(uiWindow *w, uiControl *c)
|
|
|
|
{
|
|
|
|
w->child = c;
|
2015-04-09 18:17:23 -05:00
|
|
|
uiControlSetParent(w->child, (uintptr_t) (w->hwnd));
|
2015-04-07 03:02:21 -05:00
|
|
|
}
|
2015-04-09 12:51:01 -05:00
|
|
|
|
2015-04-09 20:11:56 -05:00
|
|
|
int uiWindowMargined(uiWindow *w)
|
|
|
|
{
|
|
|
|
return w->margined;
|
|
|
|
}
|
2015-04-09 12:51:01 -05:00
|
|
|
|
|
|
|
void uiWindowSetMargined(uiWindow *w, int margined)
|
|
|
|
{
|
|
|
|
w->margined = margined;
|
|
|
|
updateParent((uintptr_t) (w->hwnd));
|
|
|
|
}
|