2015-04-27 17:04:41 -05:00
|
|
|
// 27 april 2015
|
2015-04-24 13:09:48 -05:00
|
|
|
#include "uipriv_windows.h"
|
|
|
|
|
2015-04-27 19:23:52 -05:00
|
|
|
#define windowClass L"libui_uiWindowClass"
|
|
|
|
|
2015-04-24 13:09:48 -05:00
|
|
|
struct window {
|
|
|
|
uiWindow w;
|
|
|
|
HWND hwnd;
|
2015-04-30 15:44:06 -05:00
|
|
|
HMENU menubar;
|
2015-04-27 17:04:41 -05:00
|
|
|
uiContainer *bin;
|
|
|
|
int hidden;
|
2015-04-27 19:02:33 -05:00
|
|
|
BOOL shownOnce;
|
|
|
|
int (*onClosing)(uiWindow *, void *);
|
|
|
|
void *onClosingData;
|
2015-04-24 13:09:48 -05:00
|
|
|
int margined;
|
|
|
|
};
|
|
|
|
|
2015-04-27 21:06:13 -05:00
|
|
|
static LRESULT CALLBACK windowWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
|
|
|
|
{
|
|
|
|
struct window *w;
|
|
|
|
CREATESTRUCTW *cs = (CREATESTRUCTW *) lParam;
|
|
|
|
WINDOWPOS *wp = (WINDOWPOS *) lParam;
|
|
|
|
RECT r;
|
2015-04-27 21:25:27 -05:00
|
|
|
HWND binhwnd;
|
2015-04-27 21:06:13 -05:00
|
|
|
|
|
|
|
w = (struct window *) 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);
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
// fall through
|
|
|
|
case msgUpdateChild:
|
|
|
|
if (GetClientRect(w->hwnd, &r) == 0)
|
2015-05-07 11:58:33 -05:00
|
|
|
logLastError("error getting window client rect for resize in windowWndProc()");
|
2015-04-27 21:25:27 -05:00
|
|
|
binhwnd = (HWND) uiControlHandle(uiControl(w->bin));
|
|
|
|
if (MoveWindow(binhwnd, r.left, r.top, r.right - r.left, r.bottom - r.top, TRUE) == 0)
|
2015-04-27 21:06:13 -05:00
|
|
|
logLastError("error resizing uiWindow box in windowWndProc()");
|
|
|
|
return 0;
|
|
|
|
case WM_CLOSE:
|
2015-04-30 15:41:41 -05:00
|
|
|
if ((*(w->onClosing))(uiWindow(w), w->onClosingData))
|
2015-04-27 21:25:27 -05:00
|
|
|
uiControlDestroy(uiControl(w));
|
2015-04-27 21:06:13 -05:00
|
|
|
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);
|
|
|
|
}
|
2015-04-24 13:09:48 -05:00
|
|
|
|
2015-04-27 19:23:52 -05:00
|
|
|
static int defaultOnClosing(uiWindow *w, void *data)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-04-27 17:04:41 -05:00
|
|
|
static void windowDestroy(uiControl *c)
|
2015-04-24 13:09:48 -05:00
|
|
|
{
|
2015-04-27 17:04:41 -05:00
|
|
|
struct window *w = (struct window *) c;
|
2015-04-24 13:09:48 -05:00
|
|
|
|
2015-04-27 17:04:41 -05:00
|
|
|
// first hide ourselves
|
|
|
|
ShowWindow(w->hwnd, SW_HIDE);
|
|
|
|
// now destroy the bin
|
2015-05-05 23:52:24 -05:00
|
|
|
// we need to remove the OS parent first
|
|
|
|
binSetParent(w->bin, 0);
|
2015-04-27 17:04:41 -05:00
|
|
|
uiControlDestroy(uiControl(w->bin));
|
2015-04-30 16:05:41 -05:00
|
|
|
// now free the menubar, if any
|
|
|
|
if (w->menubar != NULL)
|
|
|
|
freeMenubar(w->menubar);
|
2015-04-27 17:04:41 -05:00
|
|
|
// now destroy ourselves
|
|
|
|
if (DestroyWindow(w->hwnd) == 0)
|
|
|
|
logLastError("error destroying uiWindow in windowDestroy()");
|
|
|
|
uiFree(w);
|
2015-04-24 13:09:48 -05:00
|
|
|
}
|
|
|
|
|
2015-04-27 17:04:41 -05:00
|
|
|
static uintptr_t windowHandle(uiControl *c)
|
2015-04-24 13:09:48 -05:00
|
|
|
{
|
2015-04-27 17:04:41 -05:00
|
|
|
struct window *w = (struct window *) c;
|
2015-04-24 13:09:48 -05:00
|
|
|
|
|
|
|
return (uintptr_t) (w->hwnd);
|
|
|
|
}
|
|
|
|
|
2015-04-27 21:25:27 -05:00
|
|
|
static void windowSetParent(uiControl *c, uiContainer *parent)
|
2015-04-24 13:09:48 -05:00
|
|
|
{
|
2015-04-27 17:04:41 -05:00
|
|
|
complain("attempt to give the uiWindow at %p a parent", c);
|
2015-04-24 13:09:48 -05:00
|
|
|
}
|
|
|
|
|
2015-04-27 17:04:41 -05:00
|
|
|
static void windowPreferredSize(uiControl *c, uiSizing *d, intmax_t *width, intmax_t *height)
|
2015-04-24 13:09:48 -05:00
|
|
|
{
|
2015-04-27 17:04:41 -05:00
|
|
|
complain("attempt to get the preferred size of the uiWindow at %p", c);
|
2015-04-24 13:09:48 -05:00
|
|
|
}
|
|
|
|
|
2015-04-27 17:04:41 -05:00
|
|
|
static void windowResize(uiControl *c, intmax_t x, intmax_t y, intmax_t width, intmax_t height, uiSizing *d)
|
2015-04-24 13:09:48 -05:00
|
|
|
{
|
2015-04-27 17:04:41 -05:00
|
|
|
complain("attempt to resize the uiWindow at %p", c);
|
2015-04-24 13:09:48 -05:00
|
|
|
}
|
|
|
|
|
2015-04-27 17:04:41 -05:00
|
|
|
static int windowVisible(uiControl *c)
|
2015-04-24 13:09:48 -05:00
|
|
|
{
|
2015-04-27 17:04:41 -05:00
|
|
|
struct window *w = (struct window *) c;
|
2015-04-24 13:09:48 -05:00
|
|
|
|
2015-04-27 17:04:41 -05:00
|
|
|
return !w->hidden;
|
2015-04-24 13:09:48 -05:00
|
|
|
}
|
|
|
|
|
2015-04-27 21:25:27 -05:00
|
|
|
static void windowShow(uiControl *c)
|
2015-04-24 13:09:48 -05:00
|
|
|
{
|
2015-04-27 17:04:41 -05:00
|
|
|
struct window *w = (struct window *) c;
|
2015-04-24 13:09:48 -05:00
|
|
|
|
2015-04-27 19:02:33 -05:00
|
|
|
if (w->shownOnce) {
|
|
|
|
ShowWindow(w->hwnd, SW_SHOW);
|
|
|
|
w->hidden = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
w->shownOnce = TRUE;
|
2015-04-29 01:31:24 -05:00
|
|
|
// make sure the bin is the correct size
|
|
|
|
SendMessage(w->hwnd, msgUpdateChild, 0, 0);
|
2015-04-27 19:02:33 -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-04-27 17:04:41 -05:00
|
|
|
w->hidden = 0;
|
2015-04-24 13:09:48 -05:00
|
|
|
}
|
|
|
|
|
2015-04-27 21:25:27 -05:00
|
|
|
static void windowHide(uiControl *c)
|
2015-04-24 13:09:48 -05:00
|
|
|
{
|
2015-04-27 17:04:41 -05:00
|
|
|
struct window *w = (struct window *) c;
|
2015-04-24 13:09:48 -05:00
|
|
|
|
2015-04-27 17:04:41 -05:00
|
|
|
ShowWindow(w->hwnd, SW_HIDE);
|
|
|
|
w->hidden = 1;
|
2015-04-24 13:09:48 -05:00
|
|
|
}
|
|
|
|
|
2015-04-27 17:04:41 -05:00
|
|
|
static void windowEnable(uiControl *c)
|
2015-04-24 13:09:48 -05:00
|
|
|
{
|
2015-04-27 17:04:41 -05:00
|
|
|
struct window *w = (struct window *) c;
|
2015-04-24 13:09:48 -05:00
|
|
|
|
2015-04-27 17:04:41 -05:00
|
|
|
EnableWindow(w->hwnd, TRUE);
|
2015-05-04 13:32:55 -05:00
|
|
|
uiControlEnable(uiControl(w->bin));
|
2015-04-24 13:09:48 -05:00
|
|
|
}
|
|
|
|
|
2015-04-27 17:04:41 -05:00
|
|
|
static void windowDisable(uiControl *c)
|
2015-04-24 13:09:48 -05:00
|
|
|
{
|
2015-04-27 17:04:41 -05:00
|
|
|
struct window *w = (struct window *) c;
|
2015-04-24 13:09:48 -05:00
|
|
|
|
2015-04-27 17:04:41 -05:00
|
|
|
EnableWindow(w->hwnd, FALSE);
|
2015-05-04 13:32:55 -05:00
|
|
|
uiControlDisable(uiControl(w->bin));
|
2015-04-24 13:09:48 -05:00
|
|
|
}
|
2015-04-27 19:02:33 -05:00
|
|
|
|
|
|
|
static char *windowTitle(uiWindow *ww)
|
|
|
|
{
|
|
|
|
struct window *w = (struct window *) ww;
|
|
|
|
WCHAR *wtext;
|
|
|
|
char *text;
|
|
|
|
|
|
|
|
wtext = windowText(w->hwnd);
|
|
|
|
text = toUTF8(wtext);
|
|
|
|
uiFree(wtext);
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void windowSetTitle(uiWindow *ww, const char *title)
|
|
|
|
{
|
|
|
|
struct window *w = (struct window *) ww;
|
2015-04-27 21:25:27 -05:00
|
|
|
WCHAR *wtitle;
|
2015-04-27 19:02:33 -05:00
|
|
|
|
2015-04-27 21:25:27 -05:00
|
|
|
wtitle = toUTF16(title);
|
|
|
|
if (SetWindowTextW(w->hwnd, wtitle) == 0)
|
2015-04-27 19:02:33 -05:00
|
|
|
logLastError("error setting window title in uiWindowSetTitle()");
|
2015-04-27 21:25:27 -05:00
|
|
|
uiFree(wtitle);
|
2015-04-27 19:02:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void windowOnClosing(uiWindow *ww, int (*f)(uiWindow *, void *), void *data)
|
|
|
|
{
|
|
|
|
struct window *w = (struct window *) ww;
|
|
|
|
|
|
|
|
w->onClosing = f;
|
|
|
|
w->onClosingData = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void windowSetChild(uiWindow *ww, uiControl *child)
|
|
|
|
{
|
|
|
|
struct window *w = (struct window *) ww;
|
|
|
|
|
|
|
|
binSetMainControl(w->bin, child);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int windowMargined(uiWindow *ww)
|
|
|
|
{
|
|
|
|
struct window *w = (struct window *) ww;
|
|
|
|
|
|
|
|
return w->margined;
|
|
|
|
}
|
|
|
|
|
|
|
|
// from https://msdn.microsoft.com/en-us/library/windows/desktop/dn742486.aspx#sizingandspacing
|
|
|
|
#define windowMargin 7
|
|
|
|
|
|
|
|
static void windowSetMargined(uiWindow *ww, int margined)
|
|
|
|
{
|
|
|
|
struct window *w = (struct window *) ww;
|
|
|
|
|
|
|
|
w->margined = margined;
|
|
|
|
if (w->margined)
|
|
|
|
binSetMargins(w->bin, windowMargin, windowMargin, windowMargin, windowMargin);
|
|
|
|
else
|
|
|
|
binSetMargins(w->bin, 0, 0, 0, 0);
|
|
|
|
}
|
2015-04-27 19:23:52 -05:00
|
|
|
|
2015-05-03 11:15:24 -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
|
|
|
|
static void setClientSize(struct window *w, int width, int height, BOOL hasMenubar, DWORD style, DWORD exstyle)
|
|
|
|
{
|
|
|
|
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()");
|
|
|
|
}
|
|
|
|
|
2015-04-27 19:23:52 -05:00
|
|
|
uiWindow *uiNewWindow(const char *title, int width, int height, int hasMenubar)
|
|
|
|
{
|
|
|
|
struct window *w;
|
|
|
|
WCHAR *wtitle;
|
|
|
|
BOOL hasMenubarBOOL;
|
|
|
|
|
|
|
|
w = uiNew(struct window);
|
|
|
|
|
|
|
|
hasMenubarBOOL = FALSE;
|
|
|
|
if (hasMenubar)
|
|
|
|
hasMenubarBOOL = TRUE;
|
|
|
|
|
2015-04-27 21:25:27 -05:00
|
|
|
#define style WS_OVERLAPPEDWINDOW
|
|
|
|
#define exstyle 0
|
|
|
|
|
2015-04-27 19:23:52 -05:00
|
|
|
wtitle = toUTF16(title);
|
2015-04-27 21:25:27 -05:00
|
|
|
w->hwnd = CreateWindowExW(exstyle,
|
2015-04-27 19:23:52 -05:00
|
|
|
windowClass, wtitle,
|
2015-04-27 21:25:27 -05:00
|
|
|
style,
|
2015-04-27 19:23:52 -05:00
|
|
|
CW_USEDEFAULT, CW_USEDEFAULT,
|
2015-05-03 11:15:24 -05:00
|
|
|
// 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,
|
2015-04-27 19:23:52 -05:00
|
|
|
NULL, NULL, hInstance, w);
|
|
|
|
if (w->hwnd == NULL)
|
|
|
|
logLastError("error creating window in uiWindow()");
|
|
|
|
uiFree(wtitle);
|
|
|
|
|
|
|
|
w->bin = newBin();
|
|
|
|
binSetParent(w->bin, (uintptr_t) (w->hwnd));
|
|
|
|
|
|
|
|
if (hasMenubar) {
|
2015-04-30 15:44:06 -05:00
|
|
|
w->menubar = makeMenubar();
|
|
|
|
if (SetMenu(w->hwnd, w->menubar) == 0)
|
2015-04-27 19:23:52 -05:00
|
|
|
logLastError("error giving menu to window in uiNewWindow()");
|
|
|
|
}
|
|
|
|
|
2015-05-03 11:15:24 -05:00
|
|
|
// and use the proper size
|
|
|
|
setClientSize(w, width, height, hasMenubarBOOL, style, exstyle);
|
|
|
|
|
2015-04-27 21:25:27 -05:00
|
|
|
w->onClosing = defaultOnClosing;
|
|
|
|
|
2015-04-27 19:23:52 -05:00
|
|
|
uiControl(w)->Destroy = windowDestroy;
|
|
|
|
uiControl(w)->Handle = windowHandle;
|
|
|
|
uiControl(w)->SetParent = windowSetParent;
|
|
|
|
uiControl(w)->PreferredSize = windowPreferredSize;
|
|
|
|
uiControl(w)->Resize = windowResize;
|
|
|
|
uiControl(w)->Visible = windowVisible;
|
|
|
|
uiControl(w)->Show = windowShow;
|
|
|
|
uiControl(w)->Hide = windowHide;
|
|
|
|
uiControl(w)->Enable = windowEnable;
|
|
|
|
uiControl(w)->Disable = windowDisable;
|
|
|
|
|
|
|
|
uiWindow(w)->Title = windowTitle;
|
|
|
|
uiWindow(w)->SetTitle = windowSetTitle;
|
|
|
|
uiWindow(w)->OnClosing = windowOnClosing;
|
|
|
|
uiWindow(w)->SetChild = windowSetChild;
|
|
|
|
uiWindow(w)->Margined = windowMargined;
|
|
|
|
uiWindow(w)->SetMargined = windowSetMargined;
|
|
|
|
|
|
|
|
return uiWindow(w);
|
|
|
|
}
|