Home | History | Annotate | Download | only in win
      1 
      2 /*
      3  * Copyright 2011 Google Inc.
      4  *
      5  * Use of this source code is governed by a BSD-style license that can be
      6  * found in the LICENSE file.
      7  */
      8 #include <Windows.h>
      9 #include <tchar.h>
     10 
     11 #include "SkApplication.h"
     12 
     13 #define MAX_LOADSTRING 100
     14 
     15 // Global Variables:
     16 HINSTANCE hInst;                            // current instance
     17 TCHAR szTitle[] = _T("SampleApp");          // The title bar text
     18 TCHAR szWindowClass[] = _T("SAMPLEAPP");    // the main window class name
     19 
     20 // Forward declarations of functions included in this code module:
     21 ATOM                MyRegisterClass(HINSTANCE hInstance);
     22 BOOL                InitInstance(HINSTANCE, int, LPTSTR);
     23 LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
     24 INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);
     25 
     26 int APIENTRY _tWinMain(HINSTANCE hInstance,
     27                      HINSTANCE hPrevInstance,
     28                      LPTSTR    lpCmdLine,
     29                      int       nCmdShow)
     30 {
     31     UNREFERENCED_PARAMETER(hPrevInstance);
     32 
     33     MSG msg;
     34 
     35     // Initialize global strings
     36     MyRegisterClass(hInstance);
     37 
     38     // Perform application initialization:
     39     if (!InitInstance (hInstance, nCmdShow, lpCmdLine))
     40     {
     41         return FALSE;
     42     }
     43 
     44     // Main message loop:
     45     while (GetMessage(&msg, NULL, 0, 0))
     46     {
     47         if (true)
     48         {
     49             TranslateMessage(&msg);
     50             DispatchMessage(&msg);
     51         }
     52     }
     53 
     54     application_term();
     55 
     56     return (int) msg.wParam;
     57 }
     58 
     59 
     60 
     61 //
     62 //  FUNCTION: MyRegisterClass()
     63 //
     64 //  PURPOSE: Registers the window class.
     65 //
     66 //  COMMENTS:
     67 //
     68 //    This function and its usage are only necessary if you want this code
     69 //    to be compatible with Win32 systems prior to the 'RegisterClassEx'
     70 //    function that was added to Windows 95. It is important to call this function
     71 //    so that the application will get 'well formed' small icons associated
     72 //    with it.
     73 //
     74 ATOM MyRegisterClass(HINSTANCE hInstance)
     75 {
     76     WNDCLASSEX wcex;
     77 
     78     wcex.cbSize = sizeof(WNDCLASSEX);
     79 
     80     wcex.style            = CS_HREDRAW | CS_VREDRAW;
     81     wcex.lpfnWndProc    = WndProc;
     82     wcex.cbClsExtra        = 0;
     83     wcex.cbWndExtra        = 0;
     84     wcex.hInstance        = hInstance;
     85     wcex.hIcon            = NULL;
     86     wcex.hCursor        = NULL;
     87     wcex.hbrBackground    = (HBRUSH)(COLOR_WINDOW+1);
     88     wcex.lpszMenuName    = NULL;
     89     wcex.lpszClassName    = szWindowClass;
     90     wcex.hIconSm        = NULL;
     91 
     92     return RegisterClassEx(&wcex);
     93 }
     94 
     95 #include "SkOSWindow_Win.h"
     96 extern SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv);
     97 
     98 static SkOSWindow* gSkWind;
     99 
    100 char* tchar_to_utf8(const TCHAR* str) {
    101 #ifdef _UNICODE
    102     int size = WideCharToMultiByte(CP_UTF8, 0, str, wcslen(str), NULL, 0, NULL, NULL);
    103     char* str8 = (char*) malloc(size+1);
    104     WideCharToMultiByte(CP_UTF8, 0, str, wcslen(str), str8, size, NULL, NULL);
    105     str8[size] = '\0';
    106     return str8;
    107 #else
    108     return _strdup(str);
    109 #endif
    110 }
    111 
    112 //
    113 //   FUNCTION: InitInstance(HINSTANCE, int, LPTSTR)
    114 //
    115 //   PURPOSE: Saves instance handle and creates main window
    116 //
    117 //   COMMENTS:
    118 //
    119 //        In this function, we save the instance handle in a global variable and
    120 //        create and display the main program window.
    121 //
    122 
    123 
    124 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow, LPTSTR lpCmdLine)
    125 {
    126    application_init();
    127 
    128    hInst = hInstance; // Store instance handle in our global variable
    129 
    130    HWND hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
    131                             CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
    132 
    133    if (!hWnd)
    134    {
    135       return FALSE;
    136    }
    137 
    138    char* argv[4096];
    139    int argc = 0;
    140    TCHAR exename[1024], *next;
    141    int exenameLen = GetModuleFileName(NULL, exename, SK_ARRAY_COUNT(exename));
    142    // we're ignoring the possibility that the exe name exceeds the exename buffer
    143    (void) exenameLen;
    144    argv[argc++] = tchar_to_utf8(exename);
    145    TCHAR* arg = _tcstok_s(lpCmdLine, _T(" "), &next);
    146    while (arg != NULL) {
    147       argv[argc++] = tchar_to_utf8(arg);
    148       arg = _tcstok_s(NULL, _T(" "), &next);
    149    }
    150 
    151    gSkWind = create_sk_window(hWnd, argc, argv);
    152    for (int i = 0; i < argc; ++i) {
    153       free(argv[i]);
    154    }
    155    ShowWindow(hWnd, nCmdShow);
    156    UpdateWindow(hWnd);
    157 
    158    return TRUE;
    159 }
    160 
    161 //
    162 //  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
    163 //
    164 //  PURPOSE:  Processes messages for the main window.
    165 //
    166 //  WM_COMMAND    - process the application menu
    167 //  WM_PAINT    - Paint the main window
    168 //  WM_DESTROY    - post a quit message and return
    169 //
    170 //
    171 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    172 {
    173     switch (message) {
    174     case WM_COMMAND:
    175         return DefWindowProc(hWnd, message, wParam, lParam);
    176     case WM_DESTROY:
    177         PostQuitMessage(0);
    178         break;
    179     default:
    180         if (gSkWind->wndProc(hWnd, message, wParam, lParam)) {
    181             return 0;
    182         } else {
    183             return DefWindowProc(hWnd, message, wParam, lParam);
    184         }
    185     }
    186     return 0;
    187 }
    188 
    189 // Message handler for about box.
    190 INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
    191 {
    192     UNREFERENCED_PARAMETER(lParam);
    193     switch (message)
    194     {
    195     case WM_INITDIALOG:
    196         return (INT_PTR)TRUE;
    197 
    198     case WM_COMMAND:
    199         if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
    200         {
    201             EndDialog(hDlg, LOWORD(wParam));
    202             return (INT_PTR)TRUE;
    203         }
    204         break;
    205     }
    206     return (INT_PTR)FALSE;
    207 }
    208