2015-04-24 12:59:49 -05:00
|
|
|
// 6 april 2015
|
|
|
|
#include "uipriv_windows.h"
|
|
|
|
|
|
|
|
HINSTANCE hInstance;
|
|
|
|
int nCmdShow;
|
|
|
|
|
|
|
|
HFONT hMessageFont;
|
|
|
|
|
|
|
|
HBRUSH hollowBrush;
|
|
|
|
|
2015-11-25 13:38:40 -06:00
|
|
|
// TODO this won't work if initAlloc() failed
|
2015-04-24 12:59:49 -05:00
|
|
|
|
2015-06-02 11:31:47 -05:00
|
|
|
#define initErrorFormat L"error %s: %s%s%s %I32u (0x%I32X)%s"
|
|
|
|
#define initErrorArgs wmessage, sysmsg, beforele, label, value, value, afterle
|
2015-04-24 12:59:49 -05:00
|
|
|
|
2015-06-02 11:31:47 -05:00
|
|
|
static const char *initerr(const char *message, const WCHAR *label, DWORD value)
|
2015-04-24 12:59:49 -05:00
|
|
|
{
|
|
|
|
WCHAR *sysmsg;
|
|
|
|
BOOL hassysmsg;
|
|
|
|
WCHAR *beforele;
|
|
|
|
WCHAR *afterle;
|
|
|
|
int n;
|
|
|
|
WCHAR *wmessage;
|
|
|
|
WCHAR *wstr;
|
|
|
|
const char *str;
|
|
|
|
|
2015-06-02 11:31:47 -05:00
|
|
|
if (FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, value, 0, (LPWSTR) (&sysmsg), 0, NULL) != 0) {
|
2015-04-24 12:59:49 -05:00
|
|
|
hassysmsg = TRUE;
|
|
|
|
beforele = L" (";
|
|
|
|
afterle = L")";
|
|
|
|
} else {
|
|
|
|
hassysmsg = FALSE;
|
|
|
|
sysmsg = L"";
|
|
|
|
beforele = L"";
|
|
|
|
afterle = L"";
|
|
|
|
}
|
|
|
|
wmessage = toUTF16(message);
|
|
|
|
n = _scwprintf(initErrorFormat, initErrorArgs);
|
2015-05-08 09:34:32 -05:00
|
|
|
wstr = (WCHAR *) uiAlloc((n + 1) * sizeof (WCHAR), "WCHAR[]");
|
2015-11-27 18:59:46 -06:00
|
|
|
_snwprintf(wstr, n + 1, initErrorFormat, initErrorArgs);
|
2015-04-24 12:59:49 -05:00
|
|
|
str = toUTF8(wstr);
|
|
|
|
uiFree(wstr);
|
|
|
|
if (hassysmsg)
|
|
|
|
if (LocalFree(sysmsg) != NULL)
|
|
|
|
logLastError("error freeing system message in loadLastError()");
|
|
|
|
uiFree(wmessage);
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2015-06-02 11:31:47 -05:00
|
|
|
static const char *loadLastError(const char *message)
|
|
|
|
{
|
|
|
|
return initerr(message, L"GetLastError() ==", GetLastError());
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *loadHRESULT(const char *message, HRESULT hr)
|
|
|
|
{
|
|
|
|
return initerr(message, L"HRESULT", (DWORD) hr);
|
|
|
|
}
|
|
|
|
|
2015-05-09 13:22:51 -05:00
|
|
|
static BOOL WINAPI consoleCtrlHandler(DWORD dwCtrlType)
|
|
|
|
{
|
|
|
|
switch (dwCtrlType) {
|
|
|
|
case CTRL_LOGOFF_EVENT:
|
|
|
|
case CTRL_SHUTDOWN_EVENT:
|
|
|
|
// the handler is run in a separate thread
|
2015-05-16 00:55:34 -05:00
|
|
|
SendMessageW(utilWindow, msgConsoleEndSession, 0, 0);
|
2015-05-09 13:22:51 -05:00
|
|
|
// we handled it here
|
2015-05-09 15:04:54 -05:00
|
|
|
// this WON'T terminate the program because this is a DLL
|
|
|
|
// at least, that's the best I can gather from the MSDN page on the handler function
|
|
|
|
// it says that functions registered by a DLL replace the default handler function (which ends the process)
|
|
|
|
// it works, anyway
|
2015-05-09 13:22:51 -05:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2015-04-24 12:59:49 -05:00
|
|
|
uiInitOptions options;
|
|
|
|
|
2015-05-19 11:01:45 -05:00
|
|
|
#define wantedICCClasses ( \
|
|
|
|
ICC_STANDARD_CLASSES | /* user32.dll controls */ \
|
|
|
|
ICC_PROGRESS_CLASS | /* progress bars */ \
|
|
|
|
ICC_TAB_CLASSES | /* tabs */ \
|
|
|
|
ICC_LISTVIEW_CLASSES | /* table headers */ \
|
|
|
|
ICC_UPDOWN_CLASS | /* spinboxes */ \
|
2015-05-19 22:16:46 -05:00
|
|
|
ICC_BAR_CLASSES | /* trackbar */ \
|
2015-05-22 09:36:24 -05:00
|
|
|
ICC_DATE_CLASSES | /* date/time picker */ \
|
2015-05-19 11:01:45 -05:00
|
|
|
0)
|
|
|
|
|
2015-04-24 12:59:49 -05:00
|
|
|
const char *uiInit(uiInitOptions *o)
|
|
|
|
{
|
|
|
|
STARTUPINFOW si;
|
|
|
|
const char *ce;
|
|
|
|
HICON hDefaultIcon;
|
|
|
|
HCURSOR hDefaultCursor;
|
|
|
|
NONCLIENTMETRICSW ncm;
|
2015-05-19 11:01:45 -05:00
|
|
|
INITCOMMONCONTROLSEX icc;
|
2015-05-31 18:26:21 -05:00
|
|
|
HRESULT hr;
|
2015-04-24 12:59:49 -05:00
|
|
|
|
|
|
|
options = *o;
|
|
|
|
|
2015-05-02 11:27:53 -05:00
|
|
|
if (initAlloc() == 0)
|
|
|
|
return loadLastError("error initializing memory allocations");
|
|
|
|
|
2015-05-16 10:55:03 -05:00
|
|
|
initResizes();
|
|
|
|
|
2015-04-24 12:59:49 -05:00
|
|
|
nCmdShow = SW_SHOWDEFAULT;
|
|
|
|
GetStartupInfoW(&si);
|
|
|
|
if ((si.dwFlags & STARTF_USESHOWWINDOW) != 0)
|
|
|
|
nCmdShow = si.wShowWindow;
|
|
|
|
|
|
|
|
hDefaultIcon = LoadIconW(NULL, IDI_APPLICATION);
|
|
|
|
if (hDefaultIcon == NULL)
|
|
|
|
return loadLastError("loading default icon for window classes");
|
|
|
|
hDefaultCursor = LoadCursorW(NULL, IDC_ARROW);
|
|
|
|
if (hDefaultCursor == NULL)
|
|
|
|
return loadLastError("loading default cursor for window classes");
|
|
|
|
|
2015-05-15 20:11:28 -05:00
|
|
|
ce = initUtilWindow(hDefaultIcon, hDefaultCursor);
|
|
|
|
if (ce != NULL)
|
|
|
|
return loadLastError(ce);
|
|
|
|
|
2015-04-24 12:59:49 -05:00
|
|
|
if (registerWindowClass(hDefaultIcon, hDefaultCursor) == 0)
|
|
|
|
return loadLastError("registering uiWindow window class");
|
|
|
|
|
|
|
|
ZeroMemory(&ncm, sizeof (NONCLIENTMETRICSW));
|
|
|
|
ncm.cbSize = sizeof (NONCLIENTMETRICSW);
|
|
|
|
if (SystemParametersInfoW(SPI_GETNONCLIENTMETRICS, sizeof (NONCLIENTMETRICSW), &ncm, sizeof (NONCLIENTMETRICSW)) == 0)
|
|
|
|
return loadLastError("getting default fonts");
|
|
|
|
hMessageFont = CreateFontIndirectW(&(ncm.lfMessageFont));
|
|
|
|
if (hMessageFont == NULL)
|
|
|
|
return loadLastError("loading default messagebox font; this is the default UI font");
|
|
|
|
|
2015-05-15 20:11:28 -05:00
|
|
|
if (initContainer(hDefaultIcon, hDefaultCursor) == 0)
|
|
|
|
return loadLastError("initializing uiMakeContainer() window class");
|
2015-04-24 12:59:49 -05:00
|
|
|
|
2015-05-09 13:22:51 -05:00
|
|
|
if (SetConsoleCtrlHandler(consoleCtrlHandler, TRUE) == 0)
|
|
|
|
return loadLastError("setting up console end session handler");
|
|
|
|
|
2015-04-24 12:59:49 -05:00
|
|
|
hollowBrush = (HBRUSH) GetStockObject(HOLLOW_BRUSH);
|
|
|
|
if (hollowBrush == NULL)
|
|
|
|
return loadLastError("getting hollow brush");
|
|
|
|
|
2015-05-19 11:01:45 -05:00
|
|
|
ZeroMemory(&icc, sizeof (INITCOMMONCONTROLSEX));
|
|
|
|
icc.dwSize = sizeof (INITCOMMONCONTROLSEX);
|
|
|
|
icc.dwICC = wantedICCClasses;
|
|
|
|
if (InitCommonControlsEx(&icc) == 0)
|
|
|
|
return loadLastError("initializing Common Controls");
|
|
|
|
|
2015-05-31 18:26:21 -05:00
|
|
|
hr = CoInitialize(NULL);
|
|
|
|
if (hr != S_OK && hr != S_FALSE)
|
2015-06-02 11:31:47 -05:00
|
|
|
return loadHRESULT("initializing COM", hr);
|
2015-05-27 12:26:49 -05:00
|
|
|
// TODO initialize COM security
|
|
|
|
// TODO (windows vista) turn off COM exception handling
|
|
|
|
|
2015-10-09 10:17:58 -05:00
|
|
|
hr = initDraw();
|
|
|
|
if (hr != S_OK)
|
|
|
|
return loadHRESULT("initializing Direct2D", hr);
|
|
|
|
|
|
|
|
if (registerAreaClass(hDefaultIcon, hDefaultCursor) == 0)
|
|
|
|
return loadLastError("registering uiArea window class");
|
2015-11-27 20:45:30 -06:00
|
|
|
if (registerAreaFilter() == 0)
|
|
|
|
return loadLastError("registering uiArea message filter");
|
2015-10-09 10:17:58 -05:00
|
|
|
|
2015-04-24 12:59:49 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-05-07 21:32:17 -05:00
|
|
|
void uiUninit(void)
|
|
|
|
{
|
2015-05-08 13:23:22 -05:00
|
|
|
uninitMenus();
|
2015-11-27 20:45:30 -06:00
|
|
|
unregisterArea();
|
2015-10-09 10:17:58 -05:00
|
|
|
uninitDraw();
|
2015-05-31 18:26:21 -05:00
|
|
|
CoUninitialize();
|
2015-06-02 13:12:12 -05:00
|
|
|
if (DeleteObject(hollowBrush) == 0)
|
|
|
|
logLastError("error freeing hollow brush in uiUninit()");
|
2015-05-09 13:22:51 -05:00
|
|
|
if (SetConsoleCtrlHandler(consoleCtrlHandler, FALSE) == 0)
|
2015-06-02 13:12:12 -05:00
|
|
|
logLastError("error unregistering console end session handler in uiUninit()");
|
2015-05-08 14:01:23 -05:00
|
|
|
uninitContainer();
|
2015-05-08 16:17:03 -05:00
|
|
|
if (DeleteObject(hMessageFont) == 0)
|
|
|
|
logLastError("error deleting control font in uiUninit()");
|
2015-05-08 14:01:23 -05:00
|
|
|
unregisterWindowClass();
|
2015-06-02 13:12:12 -05:00
|
|
|
// no need to delete the default icon or cursor; see http://stackoverflow.com/questions/30603077/
|
2015-06-04 14:17:23 -05:00
|
|
|
uninitUtilWindow();
|
2015-05-16 10:55:03 -05:00
|
|
|
uninitResizes();
|
2015-06-01 17:11:23 -05:00
|
|
|
uninitTypes();
|
2015-05-08 13:05:16 -05:00
|
|
|
uninitAlloc();
|
2015-05-07 21:32:17 -05:00
|
|
|
}
|
|
|
|
|
2015-04-24 12:59:49 -05:00
|
|
|
void uiFreeInitError(const char *err)
|
|
|
|
{
|
|
|
|
uiFree((void *) err);
|
|
|
|
}
|
2015-05-01 09:19:44 -05:00
|
|
|
|
|
|
|
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
|
|
|
|
{
|
|
|
|
if (fdwReason == DLL_PROCESS_ATTACH)
|
|
|
|
hInstance = hinstDLL;
|
|
|
|
return TRUE;
|
|
|
|
}
|