180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/*
380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Copyright 2011 Google Inc.
480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru *
580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Use of this source code is governed by a BSD-style license that can be
680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * found in the LICENSE file.
780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */
8910f694aefb0b671dd8522a9afe9b6be645701c1Derek Sollenberger#include <windows.h>
980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include <tchar.h>
10363e546ed626b6dbbc42f5db87b3594bc0b5944bDerek Sollenberger
11363e546ed626b6dbbc42f5db87b3594bc0b5944bDerek Sollenberger#include "SkApplication.h"
12363e546ed626b6dbbc42f5db87b3594bc0b5944bDerek Sollenberger
1380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#define MAX_LOADSTRING 100
1480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru// Global Variables:
1680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste QueruHINSTANCE hInst;                            // current instance
1780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste QueruTCHAR szTitle[] = _T("SampleApp");          // The title bar text
1880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste QueruTCHAR szWindowClass[] = _T("SAMPLEAPP");    // the main window class name
1980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
2080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru// Forward declarations of functions included in this code module:
2180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste QueruATOM                MyRegisterClass(HINSTANCE hInstance);
2280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste QueruBOOL                InitInstance(HINSTANCE, int, LPTSTR);
2380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste QueruLRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
2480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste QueruINT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);
2580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
2680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruint APIENTRY _tWinMain(HINSTANCE hInstance,
2780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                     HINSTANCE hPrevInstance,
2880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                     LPTSTR    lpCmdLine,
2980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                     int       nCmdShow)
3080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru{
3180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    UNREFERENCED_PARAMETER(hPrevInstance);
3280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
3380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    MSG msg;
3480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
3580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    // Initialize global strings
3680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    MyRegisterClass(hInstance);
3780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
3880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    // Perform application initialization:
3980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    if (!InitInstance (hInstance, nCmdShow, lpCmdLine))
4080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    {
4180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return FALSE;
4280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
4380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
4480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    // Main message loop:
4580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    while (GetMessage(&msg, NULL, 0, 0))
4680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    {
4780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        if (true)
4880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        {
4980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            TranslateMessage(&msg);
5080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            DispatchMessage(&msg);
5180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        }
5280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
5380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
54363e546ed626b6dbbc42f5db87b3594bc0b5944bDerek Sollenberger    application_term();
55363e546ed626b6dbbc42f5db87b3594bc0b5944bDerek Sollenberger
5680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    return (int) msg.wParam;
5780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
5880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
5980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
6080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
6180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//
6280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//  FUNCTION: MyRegisterClass()
6380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//
6480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//  PURPOSE: Registers the window class.
6580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//
6680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//  COMMENTS:
6780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//
6880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//    This function and its usage are only necessary if you want this code
6980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
7080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//    function that was added to Windows 95. It is important to call this function
7180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//    so that the application will get 'well formed' small icons associated
7280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//    with it.
7380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//
7480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste QueruATOM MyRegisterClass(HINSTANCE hInstance)
7580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru{
7680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    WNDCLASSEX wcex;
7780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
7880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    wcex.cbSize = sizeof(WNDCLASSEX);
7980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
8080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    wcex.style            = CS_HREDRAW | CS_VREDRAW;
8180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    wcex.lpfnWndProc    = WndProc;
8280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    wcex.cbClsExtra        = 0;
8380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    wcex.cbWndExtra        = 0;
8480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    wcex.hInstance        = hInstance;
8580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    wcex.hIcon            = NULL;
8680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    wcex.hCursor        = NULL;
8780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    wcex.hbrBackground    = (HBRUSH)(COLOR_WINDOW+1);
8880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    wcex.lpszMenuName    = NULL;
8980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    wcex.lpszClassName    = szWindowClass;
9080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    wcex.hIconSm        = NULL;
9180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
9280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    return RegisterClassEx(&wcex);
9380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
9480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
9580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkOSWindow_Win.h"
9680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruextern SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv);
9780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
9880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querustatic SkOSWindow* gSkWind;
9980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
10080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruchar* tchar_to_utf8(const TCHAR* str) {
10180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#ifdef _UNICODE
10280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    int size = WideCharToMultiByte(CP_UTF8, 0, str, wcslen(str), NULL, 0, NULL, NULL);
10380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    char* str8 = (char*) malloc(size+1);
10480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    WideCharToMultiByte(CP_UTF8, 0, str, wcslen(str), str8, size, NULL, NULL);
10580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    str8[size] = '\0';
10680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    return str8;
10780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#else
10880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    return _strdup(str);
10980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#endif
11080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
11180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
11280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//
11380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//   FUNCTION: InitInstance(HINSTANCE, int, LPTSTR)
11480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//
11580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//   PURPOSE: Saves instance handle and creates main window
11680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//
11780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//   COMMENTS:
11880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//
11980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//        In this function, we save the instance handle in a global variable and
12080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//        create and display the main program window.
12180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//
12280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
12380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
12480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste QueruBOOL InitInstance(HINSTANCE hInstance, int nCmdShow, LPTSTR lpCmdLine)
12580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru{
126363e546ed626b6dbbc42f5db87b3594bc0b5944bDerek Sollenberger   application_init();
12780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
12880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru   hInst = hInstance; // Store instance handle in our global variable
12980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
130363e546ed626b6dbbc42f5db87b3594bc0b5944bDerek Sollenberger   HWND hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
131363e546ed626b6dbbc42f5db87b3594bc0b5944bDerek Sollenberger                            CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
13280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
13380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru   if (!hWnd)
13480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru   {
13580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru      return FALSE;
13680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru   }
13780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
13880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru   char* argv[4096];
13980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru   int argc = 0;
14080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru   TCHAR exename[1024], *next;
141e2022cc36e47b9f0d219eb5cd24be61772c28d3bDerek Sollenberger   int exenameLen = GetModuleFileName(NULL, exename, SK_ARRAY_COUNT(exename));
142e2022cc36e47b9f0d219eb5cd24be61772c28d3bDerek Sollenberger   // we're ignoring the possibility that the exe name exceeds the exename buffer
143e2022cc36e47b9f0d219eb5cd24be61772c28d3bDerek Sollenberger   (void) exenameLen;
14480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru   argv[argc++] = tchar_to_utf8(exename);
14580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru   TCHAR* arg = _tcstok_s(lpCmdLine, _T(" "), &next);
14680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru   while (arg != NULL) {
14780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru      argv[argc++] = tchar_to_utf8(arg);
14880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru      arg = _tcstok_s(NULL, _T(" "), &next);
14980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru   }
15080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
15180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru   gSkWind = create_sk_window(hWnd, argc, argv);
15280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru   for (int i = 0; i < argc; ++i) {
15380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru      free(argv[i]);
15480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru   }
15580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru   ShowWindow(hWnd, nCmdShow);
15680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru   UpdateWindow(hWnd);
15780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
15880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru   return TRUE;
15980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
16080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
16180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//
16280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
16380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//
16480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//  PURPOSE:  Processes messages for the main window.
16580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//
16680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//  WM_COMMAND    - process the application menu
16780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//  WM_PAINT    - Paint the main window
16880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//  WM_DESTROY    - post a quit message and return
16980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//
17080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//
17180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste QueruLRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
17280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru{
17380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    switch (message) {
17480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    case WM_COMMAND:
17580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return DefWindowProc(hWnd, message, wParam, lParam);
17680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    case WM_DESTROY:
17780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        PostQuitMessage(0);
17880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        break;
17980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    default:
18080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        if (gSkWind->wndProc(hWnd, message, wParam, lParam)) {
18180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            return 0;
18280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        } else {
18380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            return DefWindowProc(hWnd, message, wParam, lParam);
18480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        }
18580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
18680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    return 0;
18780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
18880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
18980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru// Message handler for about box.
19080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste QueruINT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
19180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru{
19280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    UNREFERENCED_PARAMETER(lParam);
19380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    switch (message)
19480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    {
19580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    case WM_INITDIALOG:
19680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return (INT_PTR)TRUE;
19780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
19880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    case WM_COMMAND:
19980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
20080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        {
20180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            EndDialog(hDlg, LOWORD(wParam));
20280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            return (INT_PTR)TRUE;
20380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        }
20480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        break;
20580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
20680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    return (INT_PTR)FALSE;
20780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
208