michael@0: michael@0: /* michael@0: * Copyright 2011 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: #include michael@0: #include michael@0: michael@0: #include "SkApplication.h" michael@0: michael@0: #define MAX_LOADSTRING 100 michael@0: michael@0: // Global Variables: michael@0: HINSTANCE hInst; // current instance michael@0: TCHAR szTitle[] = _T("SampleApp"); // The title bar text michael@0: TCHAR szWindowClass[] = _T("SAMPLEAPP"); // the main window class name michael@0: michael@0: // Forward declarations of functions included in this code module: michael@0: ATOM MyRegisterClass(HINSTANCE hInstance); michael@0: BOOL InitInstance(HINSTANCE, int, LPTSTR); michael@0: LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); michael@0: INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM); michael@0: michael@0: int APIENTRY _tWinMain(HINSTANCE hInstance, michael@0: HINSTANCE hPrevInstance, michael@0: LPTSTR lpCmdLine, michael@0: int nCmdShow) michael@0: { michael@0: UNREFERENCED_PARAMETER(hPrevInstance); michael@0: michael@0: MSG msg; michael@0: michael@0: // Initialize global strings michael@0: MyRegisterClass(hInstance); michael@0: michael@0: // Perform application initialization: michael@0: if (!InitInstance (hInstance, nCmdShow, lpCmdLine)) michael@0: { michael@0: return FALSE; michael@0: } michael@0: michael@0: // Main message loop: michael@0: while (GetMessage(&msg, NULL, 0, 0)) michael@0: { michael@0: if (true) michael@0: { michael@0: TranslateMessage(&msg); michael@0: DispatchMessage(&msg); michael@0: } michael@0: } michael@0: michael@0: application_term(); michael@0: michael@0: return (int) msg.wParam; michael@0: } michael@0: michael@0: michael@0: michael@0: // michael@0: // FUNCTION: MyRegisterClass() michael@0: // michael@0: // PURPOSE: Registers the window class. michael@0: // michael@0: // COMMENTS: michael@0: // michael@0: // This function and its usage are only necessary if you want this code michael@0: // to be compatible with Win32 systems prior to the 'RegisterClassEx' michael@0: // function that was added to Windows 95. It is important to call this function michael@0: // so that the application will get 'well formed' small icons associated michael@0: // with it. michael@0: // michael@0: ATOM MyRegisterClass(HINSTANCE hInstance) michael@0: { michael@0: WNDCLASSEX wcex; michael@0: michael@0: wcex.cbSize = sizeof(WNDCLASSEX); michael@0: michael@0: wcex.style = CS_HREDRAW | CS_VREDRAW; michael@0: wcex.lpfnWndProc = WndProc; michael@0: wcex.cbClsExtra = 0; michael@0: wcex.cbWndExtra = 0; michael@0: wcex.hInstance = hInstance; michael@0: wcex.hIcon = NULL; michael@0: wcex.hCursor = NULL; michael@0: wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); michael@0: wcex.lpszMenuName = NULL; michael@0: wcex.lpszClassName = szWindowClass; michael@0: wcex.hIconSm = NULL; michael@0: michael@0: return RegisterClassEx(&wcex); michael@0: } michael@0: michael@0: #include "SkOSWindow_Win.h" michael@0: extern SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv); michael@0: michael@0: static SkOSWindow* gSkWind; michael@0: michael@0: char* tchar_to_utf8(const TCHAR* str) { michael@0: #ifdef _UNICODE michael@0: int size = WideCharToMultiByte(CP_UTF8, 0, str, wcslen(str), NULL, 0, NULL, NULL); michael@0: char* str8 = (char*) malloc(size+1); michael@0: WideCharToMultiByte(CP_UTF8, 0, str, wcslen(str), str8, size, NULL, NULL); michael@0: str8[size] = '\0'; michael@0: return str8; michael@0: #else michael@0: return _strdup(str); michael@0: #endif michael@0: } michael@0: michael@0: // michael@0: // FUNCTION: InitInstance(HINSTANCE, int, LPTSTR) michael@0: // michael@0: // PURPOSE: Saves instance handle and creates main window michael@0: // michael@0: // COMMENTS: michael@0: // michael@0: // In this function, we save the instance handle in a global variable and michael@0: // create and display the main program window. michael@0: // michael@0: michael@0: michael@0: BOOL InitInstance(HINSTANCE hInstance, int nCmdShow, LPTSTR lpCmdLine) michael@0: { michael@0: application_init(); michael@0: michael@0: hInst = hInstance; // Store instance handle in our global variable michael@0: michael@0: HWND hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, michael@0: CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); michael@0: michael@0: if (!hWnd) michael@0: { michael@0: return FALSE; michael@0: } michael@0: michael@0: char* argv[4096]; michael@0: int argc = 0; michael@0: TCHAR exename[1024], *next; michael@0: int exenameLen = GetModuleFileName(NULL, exename, SK_ARRAY_COUNT(exename)); michael@0: // we're ignoring the possibility that the exe name exceeds the exename buffer michael@0: (void) exenameLen; michael@0: argv[argc++] = tchar_to_utf8(exename); michael@0: TCHAR* arg = _tcstok_s(lpCmdLine, _T(" "), &next); michael@0: while (arg != NULL) { michael@0: argv[argc++] = tchar_to_utf8(arg); michael@0: arg = _tcstok_s(NULL, _T(" "), &next); michael@0: } michael@0: michael@0: gSkWind = create_sk_window(hWnd, argc, argv); michael@0: for (int i = 0; i < argc; ++i) { michael@0: free(argv[i]); michael@0: } michael@0: ShowWindow(hWnd, nCmdShow); michael@0: UpdateWindow(hWnd); michael@0: michael@0: return TRUE; michael@0: } michael@0: michael@0: // michael@0: // FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM) michael@0: // michael@0: // PURPOSE: Processes messages for the main window. michael@0: // michael@0: // WM_COMMAND - process the application menu michael@0: // WM_PAINT - Paint the main window michael@0: // WM_DESTROY - post a quit message and return michael@0: // michael@0: // michael@0: LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) michael@0: { michael@0: switch (message) { michael@0: case WM_COMMAND: michael@0: return DefWindowProc(hWnd, message, wParam, lParam); michael@0: case WM_DESTROY: michael@0: PostQuitMessage(0); michael@0: break; michael@0: default: michael@0: if (gSkWind->wndProc(hWnd, message, wParam, lParam)) { michael@0: return 0; michael@0: } else { michael@0: return DefWindowProc(hWnd, message, wParam, lParam); michael@0: } michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: // Message handler for about box. michael@0: INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) michael@0: { michael@0: UNREFERENCED_PARAMETER(lParam); michael@0: switch (message) michael@0: { michael@0: case WM_INITDIALOG: michael@0: return (INT_PTR)TRUE; michael@0: michael@0: case WM_COMMAND: michael@0: if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) michael@0: { michael@0: EndDialog(hDlg, LOWORD(wParam)); michael@0: return (INT_PTR)TRUE; michael@0: } michael@0: break; michael@0: } michael@0: return (INT_PTR)FALSE; michael@0: }