142 lines
3.8 KiB
C
142 lines
3.8 KiB
C
|
// 6 april 2015
|
||
|
#include "uipriv_windows.h"
|
||
|
|
||
|
struct window {
|
||
|
uiWindow w;
|
||
|
HWND hwnd;
|
||
|
uiOSContainer *content;
|
||
|
BOOL shownOnce;
|
||
|
int (*onClosing)(uiWindow *, void *);
|
||
|
void *onClosingData;
|
||
|
int margined;
|
||
|
BOOL canDestroy;
|
||
|
};
|
||
|
|
||
|
#define uiWindowClass L"uiWindowClass"
|
||
|
|
||
|
static LRESULT CALLBACK uiWindowWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
|
||
|
{
|
||
|
struct window *w;
|
||
|
CREATESTRUCTW *cs = (CREATESTRUCTW *) lParam;
|
||
|
WINDOWPOS *wp = (WINDOWPOS *) lParam;
|
||
|
RECT r;
|
||
|
HWND contenthwnd;
|
||
|
|
||
|
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)
|
||
|
logLastError("error getting window client rect for resize in uiWindowWndProc()");
|
||
|
contenthwnd = uiOSContainerHWND(w->content);
|
||
|
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()");
|
||
|
return 0;
|
||
|
case WM_CLOSE:
|
||
|
if (!(*(w->onClosing))(uiWindow(w), w->onClosingData))
|
||
|
uiWindowDestroy(uiWindow(w));
|
||
|
return 0; // we destroyed it already
|
||
|
case WM_DESTROY:
|
||
|
if (!w->canDestroy)
|
||
|
complain("attempt to destroy uiWindow at %p before uiWindowDestroy()", w);
|
||
|
uiFree(w);
|
||
|
break; // fall through to DefWindowProcW()
|
||
|
}
|
||
|
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(const char *title, int width, int height, int hasMenubar)
|
||
|
{
|
||
|
struct window *w;
|
||
|
RECT adjust;
|
||
|
WCHAR *wtitle;
|
||
|
BOOL hasMenubarBOOL;
|
||
|
HMENU hmenu;
|
||
|
|
||
|
w = uiNew(struct window);
|
||
|
w->onClosing = defaultOnClosing;
|
||
|
|
||
|
hasMenubarBOOL = FALSE;
|
||
|
if (hasMenubar)
|
||
|
hasMenubarBOOL = TRUE;
|
||
|
|
||
|
adjust.left = 0;
|
||
|
adjust.top = 0;
|
||
|
adjust.right = width;
|
||
|
adjust.bottom = height;
|
||
|
// TODO does not handle menu wrapping; see http://blogs.msdn.com/b/oldnewthing/archive/2003/09/11/54885.aspx
|
||
|
if (AdjustWindowRectEx(&adjust, style, hasMenubarBOOL, 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 = uiNewOSContainer((uintptr_t) (w->hwnd));
|
||
|
|
||
|
if (hasMenubar) {
|
||
|
hmenu = makeMenubar();
|
||
|
if (SetMenu(w->hwnd, hmenu) == 0)
|
||
|
logLastError("error giving menu to window in uiNewWindow()");
|
||
|
}
|
||
|
|
||
|
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);
|
||
|
}
|