Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(546)

Side by Side Diff: src/views/win/skia_win.cpp

Issue 1185303004: Allow skia_win.cpp to be compiled in CONSOLE or GUI mode. (Closed) Base URL: https://skia.googlesource.com/skia.git@vb
Patch Set: Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « include/views/SkOSWindow_Win.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 1
2 /* 2 /*
3 * Copyright 2011 Google Inc. 3 * Copyright 2011 Google Inc.
4 * 4 *
5 * Use of this source code is governed by a BSD-style license that can be 5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file. 6 * found in the LICENSE file.
7 */ 7 */
8 #include <windows.h> 8 #include <windows.h>
9 #include <tchar.h> 9 #include <tchar.h>
10 10
11 #include "SkTypes.h"
11 #include "SkApplication.h" 12 #include "SkApplication.h"
13 #include "SkOSWindow_Win.h"
12 14
13 #define MAX_LOADSTRING 100 15 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
14 16
15 // Global Variables: 17 // Returns the main window Win32 class name.
16 HINSTANCE gHInst; // current instance 18 static const TCHAR* register_class(HINSTANCE hInstance) {
17 TCHAR gSZWindowClass[] = _T("SkiaApp"); // the main window class name
18
19 // Forward declarations of functions included in this code module:
20 ATOM MyRegisterClass(HINSTANCE hInstance);
21 BOOL InitInstance(HINSTANCE, int, LPTSTR);
22 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
23 INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
24
25 int APIENTRY _tWinMain(HINSTANCE hInstance,
26 HINSTANCE hPrevInstance,
27 LPTSTR lpCmdLine,
28 int nCmdShow)
29 {
30 UNREFERENCED_PARAMETER(hPrevInstance);
31
32 MSG msg;
33
34 // Initialize global strings
35 MyRegisterClass(hInstance);
36
37 // Perform application initialization:
38 if (!InitInstance (hInstance, nCmdShow, lpCmdLine))
39 {
40 return FALSE;
41 }
42
43 // Main message loop:
44 while (GetMessage(&msg, NULL, 0, 0))
45 {
46 if (true)
47 {
48 TranslateMessage(&msg);
49 DispatchMessage(&msg);
50 }
51 }
52
53 application_term();
54
55 return (int) msg.wParam;
56 }
57
58
59
60 //
61 // FUNCTION: MyRegisterClass()
62 //
63 // PURPOSE: Registers the window class.
64 //
65 // COMMENTS:
66 //
67 // This function and its usage are only necessary if you want this code
68 // to be compatible with Win32 systems prior to the 'RegisterClassEx'
69 // function that was added to Windows 95. It is important to call this functi on
70 // so that the application will get 'well formed' small icons associated
71 // with it.
72 //
73 ATOM MyRegisterClass(HINSTANCE hInstance)
74 {
75 WNDCLASSEX wcex; 19 WNDCLASSEX wcex;
20 // The main window class name
21 static const TCHAR gSZWindowClass[] = _T("SkiaApp");
76 22
77 wcex.cbSize = sizeof(WNDCLASSEX); 23 wcex.cbSize = sizeof(WNDCLASSEX);
78 24
79 wcex.style = CS_HREDRAW | CS_VREDRAW; 25 wcex.style = CS_HREDRAW | CS_VREDRAW;
80 wcex.lpfnWndProc = WndProc; 26 wcex.lpfnWndProc = WndProc;
81 wcex.cbClsExtra = 0; 27 wcex.cbClsExtra = 0;
82 wcex.cbWndExtra = 0; 28 wcex.cbWndExtra = 0;
83 wcex.hInstance = hInstance; 29 wcex.hInstance = hInstance;
84 wcex.hIcon = NULL; 30 wcex.hIcon = NULL;
85 wcex.hCursor = NULL; 31 wcex.hCursor = NULL;
86 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); 32 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
87 wcex.lpszMenuName = NULL; 33 wcex.lpszMenuName = NULL;
88 wcex.lpszClassName = gSZWindowClass; 34 wcex.lpszClassName = gSZWindowClass;
89 wcex.hIconSm = NULL; 35 wcex.hIconSm = NULL;
90 36
91 return RegisterClassEx(&wcex); 37 RegisterClassEx(&wcex);
38
39 return gSZWindowClass;
92 } 40 }
93 41
94 #include "SkOSWindow_Win.h" 42 static char* tchar_to_utf8(const TCHAR* str) {
95 extern SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv);
96
97 char* tchar_to_utf8(const TCHAR* str) {
98 #ifdef _UNICODE 43 #ifdef _UNICODE
99 int size = WideCharToMultiByte(CP_UTF8, 0, str, wcslen(str), NULL, 0, NULL, NULL); 44 int size = WideCharToMultiByte(CP_UTF8, 0, str, wcslen(str), NULL, 0, NULL, NULL);
100 char* str8 = (char*) sk_malloc_throw(size+1); 45 char* str8 = (char*) sk_malloc_throw(size+1);
101 WideCharToMultiByte(CP_UTF8, 0, str, wcslen(str), str8, size, NULL, NULL); 46 WideCharToMultiByte(CP_UTF8, 0, str, wcslen(str), str8, size, NULL, NULL);
102 str8[size] = '\0'; 47 str8[size] = '\0';
103 return str8; 48 return str8;
104 #else 49 #else
105 return _strdup(str); 50 return _strdup(str);
106 #endif 51 #endif
107 } 52 }
108 53
109 // 54 // This file can work with GUI or CONSOLE subsystem types since we define _tWinM ain and main().
110 // FUNCTION: InitInstance(HINSTANCE, int, LPTSTR)
111 //
112 // PURPOSE: Saves instance handle and creates main window
113 //
114 // COMMENTS:
115 //
116 // In this function, we save the instance handle in a global variable and
117 // create and display the main program window.
118 //
119 55
56 static int main_common(HINSTANCE hInstance, int show, int argc, char**argv);
120 57
121 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow, LPTSTR lpCmdLine) 58 int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCm dLine,
122 { 59 int nCmdShow) {
123 application_init();
124 60
125 gHInst = hInstance; // Store instance handle in our global variable 61 // convert from lpCmdLine to argc, argv.
126 char* argv[4096]; 62 char* argv[4096];
127 int argc = 0; 63 int argc = 0;
128 TCHAR exename[1024], *next; 64 TCHAR exename[1024], *next;
129 int exenameLen = GetModuleFileName(NULL, exename, SK_ARRAY_COUNT(exename)); 65 int exenameLen = GetModuleFileName(NULL, exename, SK_ARRAY_COUNT(exename));
130 // we're ignoring the possibility that the exe name exceeds the exename buffe r 66 // we're ignoring the possibility that the exe name exceeds the exename buff er
131 (void) exenameLen; 67 (void) exenameLen;
132 argv[argc++] = tchar_to_utf8(exename); 68 argv[argc++] = tchar_to_utf8(exename);
133 TCHAR* arg = _tcstok_s(lpCmdLine, _T(" "), &next); 69 TCHAR* arg = _tcstok_s(lpCmdLine, _T(" "), &next);
134 while (arg != NULL) { 70 while (arg != NULL) {
135 argv[argc++] = tchar_to_utf8(arg); 71 argv[argc++] = tchar_to_utf8(arg);
136 arg = _tcstok_s(NULL, _T(" "), &next); 72 arg = _tcstok_s(NULL, _T(" "), &next);
137 } 73 }
138 74 int result = main_common(hInstance, nCmdShow, argc, argv);
139 SkOSWindow::WindowInit winInit; 75 for (int i = 0; i < argc; ++i) {
140 winInit.fInstance = gHInst; 76 sk_free(argv[i]);
141 winInit.fClass = gSZWindowClass; 77 }
142 78 return result;
143 create_sk_window(&winInit, argc, argv);
144 for (int i = 0; i < argc; ++i) {
145 sk_free(argv[i]);
146 }
147 SkOSWindow::ForAllWindows([nCmdShow](void* hWnd, SkOSWindow**){
148 ShowWindow((HWND)hWnd, nCmdShow);
149 UpdateWindow((HWND)hWnd); }
150 );
151
152 return TRUE;
153 } 79 }
154 80
155 // 81 int main(int argc, char**argv) {
156 // FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM) 82 return main_common(GetModuleHandle(NULL), SW_SHOW, argc, argv);
157 // 83 }
158 // PURPOSE: Processes messages for the main window. 84
159 // 85 static int main_common(HINSTANCE hInstance, int show, int argc, char**argv) {
160 // WM_COMMAND - process the application menu 86 const TCHAR* windowClass = register_class(hInstance);
161 // WM_PAINT - Paint the main window 87
162 // WM_DESTROY - post a quit message and return 88 application_init();
163 // 89
164 // 90 SkOSWindow::WindowInit winInit;
165 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 91 winInit.fInstance = hInstance;
166 { 92 winInit.fClass = windowClass;
93
94 create_sk_window(&winInit, argc, argv);
95 SkOSWindow::ForAllWindows([show](void* hWnd, SkOSWindow**) {
96 ShowWindow((HWND)hWnd, show);
97 UpdateWindow((HWND)hWnd); }
98 );
99
100 MSG msg;
101 // Main message loop
102 while (GetMessage(&msg, NULL, 0, 0)) {
103 if (true) {
104 TranslateMessage(&msg);
105 DispatchMessage(&msg);
106 }
107 }
108
109 application_term();
110
111 return (int) msg.wParam;
112 }
113
114 extern SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv);
115
116 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
167 switch (message) { 117 switch (message) {
168 case WM_COMMAND: 118 case WM_COMMAND:
169 return DefWindowProc(hWnd, message, wParam, lParam); 119 return DefWindowProc(hWnd, message, wParam, lParam);
170 case WM_DESTROY: 120 case WM_DESTROY:
171 PostQuitMessage(0); 121 PostQuitMessage(0);
172 break; 122 break;
173 default: { 123 default: {
174 SkOSWindow* window = SkOSWindow::GetOSWindowForHWND(hWnd); 124 SkOSWindow* window = SkOSWindow::GetOSWindowForHWND(hWnd);
175 if (window && window->wndProc(hWnd, message, wParam, lParam)) { 125 if (window && window->wndProc(hWnd, message, wParam, lParam)) {
176 return 0; 126 return 0;
177 } else { 127 } else {
178 return DefWindowProc(hWnd, message, wParam, lParam); 128 return DefWindowProc(hWnd, message, wParam, lParam);
179 } 129 }
180 } 130 }
181 } 131 }
182 return 0; 132 return 0;
183 } 133 }
184 134
185 // Message handler for about box.
186 INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
187 {
188 UNREFERENCED_PARAMETER(lParam);
189 switch (message)
190 {
191 case WM_INITDIALOG:
192 return (INT_PTR)TRUE;
193
194 case WM_COMMAND:
195 if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
196 {
197 EndDialog(hDlg, LOWORD(wParam));
198 return (INT_PTR)TRUE;
199 }
200 break;
201 }
202 return (INT_PTR)FALSE;
203 }
OLDNEW
« no previous file with comments | « include/views/SkOSWindow_Win.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698