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
|
|
|
|
|
|
|
HINSTANCE hInstance;
|
|
|
|
int nCmdShow;
|
|
|
|
|
|
|
|
HFONT hMessageFont;
|
|
|
|
|
2015-04-07 02:02:07 -05:00
|
|
|
HWND initialParent;
|
|
|
|
|
2015-04-06 16:41:33 -05:00
|
|
|
struct uiInitError {
|
|
|
|
char *msg;
|
|
|
|
char failbuf[256];
|
|
|
|
};
|
|
|
|
|
2015-04-07 21:19:24 -05:00
|
|
|
static uiInitError *loadLastError(uiInitError *err, const char *message)
|
2015-04-06 16:41:33 -05:00
|
|
|
{
|
|
|
|
DWORD le;
|
|
|
|
|
|
|
|
le = GetLastError();
|
|
|
|
// TODO FormatMessageW() it
|
2015-04-07 03:22:30 -05:00
|
|
|
// TODO make sure argument is right; _snprintf_s() isn't supported on Windows XP
|
2015-04-07 18:36:46 -05:00
|
|
|
snprintf(err->failbuf, 256, "error %s (last error %I32u)", message, le);
|
2015-04-06 16:41:33 -05:00
|
|
|
err->msg = err->failbuf;
|
2015-04-07 21:19:24 -05:00
|
|
|
return err;
|
2015-04-06 16:41:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
uiInitError *uiInit(uiInitOptions *o)
|
|
|
|
{
|
|
|
|
uiInitError *err;
|
|
|
|
STARTUPINFOW si;
|
2015-04-07 01:11:31 -05:00
|
|
|
const char *ce;
|
2015-04-06 16:41:33 -05:00
|
|
|
HICON hDefaultIcon;
|
|
|
|
HCURSOR hDefaultCursor;
|
|
|
|
NONCLIENTMETRICSW ncm;
|
|
|
|
|
2015-04-07 21:19:24 -05:00
|
|
|
err = uiNew(uiInitError);
|
2015-04-06 16:41:33 -05:00
|
|
|
|
|
|
|
hInstance = GetModuleHandle(NULL);
|
2015-04-07 21:19:24 -05:00
|
|
|
if (hInstance == NULL)
|
|
|
|
return loadLastError(err, "getting program HINSTANCE");
|
2015-04-06 16:41:33 -05:00
|
|
|
|
|
|
|
nCmdShow = SW_SHOWDEFAULT;
|
|
|
|
GetStartupInfoW(&si);
|
|
|
|
if ((si.dwFlags & STARTF_USESHOWWINDOW) != 0)
|
|
|
|
nCmdShow = si.wShowWindow;
|
|
|
|
|
2015-04-07 01:11:31 -05:00
|
|
|
ce = initCommonControls();
|
2015-04-07 21:19:24 -05:00
|
|
|
if (ce != NULL)
|
|
|
|
return loadLastError(err, ce);
|
2015-04-07 01:11:31 -05:00
|
|
|
|
2015-04-06 16:41:33 -05:00
|
|
|
hDefaultIcon = LoadIconW(NULL, IDI_APPLICATION);
|
2015-04-07 21:19:24 -05:00
|
|
|
if (hDefaultIcon == NULL)
|
|
|
|
return loadLastError(err, "loading default icon for window classes");
|
2015-04-06 16:41:33 -05:00
|
|
|
hDefaultCursor = LoadCursorW(NULL, IDC_ARROW);
|
2015-04-07 21:19:24 -05:00
|
|
|
if (hDefaultCursor == NULL)
|
|
|
|
return loadLastError(err, "loading default cursor for window classes");
|
2015-04-06 16:41:33 -05:00
|
|
|
|
2015-04-07 21:19:24 -05:00
|
|
|
if (registerWindowClass(hDefaultIcon, hDefaultCursor) == 0)
|
|
|
|
return loadLastError(err, "registering uiWindow window class");
|
2015-04-06 16:41:33 -05:00
|
|
|
|
|
|
|
ZeroMemory(&ncm, sizeof (NONCLIENTMETRICSW));
|
|
|
|
ncm.cbSize = sizeof (NONCLIENTMETRICSW);
|
2015-04-07 21:19:24 -05:00
|
|
|
if (SystemParametersInfoW(SPI_GETNONCLIENTMETRICS, sizeof (NONCLIENTMETRICSW), &ncm, sizeof (NONCLIENTMETRICSW)) == 0)
|
|
|
|
return loadLastError(err, "getting default fonts");
|
2015-04-06 16:41:33 -05:00
|
|
|
hMessageFont = CreateFontIndirectW(&(ncm.lfMessageFont));
|
2015-04-07 21:19:24 -05:00
|
|
|
if (hMessageFont == NULL)
|
|
|
|
return loadLastError(err, "loading default messagebox font; this is the default UI font");
|
2015-04-06 16:41:33 -05:00
|
|
|
|
2015-04-07 02:02:07 -05:00
|
|
|
// give each control a reasonable initial parent
|
|
|
|
// don't free the initial parent!
|
|
|
|
// TODO tune this better; it shouldn't be closed, for instance
|
2015-04-07 02:12:34 -05:00
|
|
|
initialParent = (HWND) uiWindowHandle(uiNewWindow("", 0, 0));
|
2015-04-07 02:02:07 -05:00
|
|
|
|
2015-04-06 16:41:33 -05:00
|
|
|
uiFree(err);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *uiInitErrorMessage(uiInitError *err)
|
|
|
|
{
|
|
|
|
return err->msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
void uiInitErrorFree(uiInitError *err)
|
|
|
|
{
|
|
|
|
if (err->msg != err->failbuf)
|
|
|
|
uiFree(err->msg);
|
|
|
|
uiFree(err);
|
|
|
|
}
|