[BASE] Spelling fixes by Josh Soref. CORE-12286
[reactos.git] / reactos / base / applications / fontview / fontview.c
1 /*
2 * fontview
3 *
4 * fontview.c
5 *
6 * Copyright (C) 2007 Timo Kreuzer <timo <dot> kreuzer <at> reactos <dot> org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23 #include "precomp.h"
24
25 #include <winnls.h>
26 #include <shellapi.h>
27 #include <windowsx.h>
28
29 #include "fontview.h"
30 #include "resource.h"
31
32 HINSTANCE g_hInstance;
33 EXTLOGFONTW g_ExtLogFontW;
34 LPCWSTR g_fileName;
35
36 static const WCHAR g_szFontViewClassName[] = L"FontViewWClass";
37
38 /* GetFontResourceInfoW is undocumented */
39 BOOL WINAPI GetFontResourceInfoW(LPCWSTR lpFileName, DWORD *pdwBufSize, void* lpBuffer, DWORD dwType);
40
41 DWORD
42 FormatString(
43 DWORD dwFlags,
44 HINSTANCE hInstance,
45 DWORD dwStringId,
46 DWORD dwLanguageId,
47 LPWSTR lpBuffer,
48 DWORD nSize,
49 va_list* Arguments
50 )
51 {
52 DWORD dwRet;
53 int len;
54 WCHAR Buffer[1000];
55
56 len = LoadStringW(hInstance, dwStringId, (LPWSTR)Buffer, 1000);
57
58 if (len)
59 {
60 dwFlags |= FORMAT_MESSAGE_FROM_STRING;
61 dwFlags &= ~(FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_FROM_SYSTEM);
62 dwRet = FormatMessageW(dwFlags, Buffer, 0, dwLanguageId, lpBuffer, nSize, Arguments);
63 return dwRet;
64 }
65 return 0;
66 }
67
68 static void
69 ErrorMsgBox(HWND hParent, DWORD dwMessageId, ...)
70 {
71 HLOCAL hMemCaption = NULL;
72 HLOCAL hMemText = NULL;
73 va_list args;
74
75 va_start(args, dwMessageId);
76 FormatString(FORMAT_MESSAGE_ALLOCATE_BUFFER,
77 NULL, dwMessageId, 0, (LPWSTR)&hMemText, 0, &args);
78 va_end(args);
79
80 FormatString(FORMAT_MESSAGE_ALLOCATE_BUFFER,
81 NULL, IDS_ERROR, 0, (LPWSTR)&hMemCaption, 0, NULL);
82
83 MessageBoxW(hParent, hMemText, hMemCaption, MB_ICONERROR);
84
85 LocalFree(hMemCaption);
86 LocalFree(hMemText);
87 }
88
89 int WINAPI
90 WinMain (HINSTANCE hThisInstance,
91 HINSTANCE hPrevInstance,
92 LPSTR lpCmdLine,
93 int nCmdShow)
94 {
95 int argc;
96 WCHAR** argv;
97 WCHAR szFileName[MAX_PATH] = L"";
98 DWORD dwSize;
99 HWND hMainWnd;
100 MSG msg;
101 WNDCLASSEXW wincl;
102 LPCWSTR fileName;
103
104 switch (GetUserDefaultUILanguage())
105 {
106 case MAKELANGID(LANG_HEBREW, SUBLANG_DEFAULT):
107 SetProcessDefaultLayout(LAYOUT_RTL);
108 break;
109
110 default:
111 break;
112 }
113
114 g_hInstance = hThisInstance;
115
116 /* Get unicode command line */
117 argv = CommandLineToArgvW(GetCommandLineW(), &argc);
118 if (argc < 2)
119 {
120 OPENFILENAMEW fontOpen;
121 WCHAR filter[MAX_PATH], dialogTitle[MAX_PATH];
122
123 LoadStringW(NULL, IDS_OPEN, dialogTitle, MAX_PATH);
124 LoadStringW(NULL, IDS_FILTER_LIST, filter, MAX_PATH);
125
126 /* Clears out any values of fontOpen before we use it */
127 ZeroMemory(&fontOpen, sizeof(fontOpen));
128
129 /* Sets up the open dialog box */
130 fontOpen.lStructSize = sizeof(fontOpen);
131 fontOpen.hwndOwner = NULL;
132 fontOpen.lpstrFilter = filter;
133 fontOpen.lpstrFile = szFileName;
134 fontOpen.lpstrTitle = dialogTitle;
135 fontOpen.nMaxFile = MAX_PATH;
136 fontOpen.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
137 fontOpen.lpstrDefExt = L"ttf";
138
139 /* Opens up the Open File dialog box in order to chose a font file. */
140 if(GetOpenFileNameW(&fontOpen))
141 {
142 fileName = fontOpen.lpstrFile;
143 g_fileName = fileName;
144 } else {
145 /* If the user decides to close out of the open dialog effectively
146 exiting the program altogether */
147 return 0;
148 }
149 }
150 else
151 {
152 /* Try to add the font resource from command line */
153 fileName = argv[1];
154 g_fileName = fileName;
155 }
156
157 if (!AddFontResourceW(fileName))
158 {
159 ErrorMsgBox(0, IDS_ERROR_NOFONT, fileName);
160 return -1;
161 }
162
163 /* Get the font name */
164 dwSize = sizeof(g_ExtLogFontW.elfFullName);
165 if (!GetFontResourceInfoW(fileName, &dwSize, g_ExtLogFontW.elfFullName, 1))
166 {
167 ErrorMsgBox(0, IDS_ERROR_NOFONT, fileName);
168 return -1;
169 }
170
171 dwSize = sizeof(LOGFONTW);
172 if (!GetFontResourceInfoW(fileName, &dwSize, &g_ExtLogFontW.elfLogFont, 2))
173 {
174 ErrorMsgBox(0, IDS_ERROR_NOFONT, fileName);
175 return -1;
176 }
177
178 if (!Display_InitClass(hThisInstance))
179 {
180 ErrorMsgBox(0, IDS_ERROR_NOCLASS);
181 return -1;
182 }
183
184 /* The main window class */
185 wincl.cbSize = sizeof (WNDCLASSEXW);
186 wincl.style = CS_DBLCLKS;
187 wincl.lpfnWndProc = MainWndProc;
188 wincl.cbClsExtra = 0;
189 wincl.cbWndExtra = 0;
190 wincl.hInstance = hThisInstance;
191 wincl.hIcon = LoadIcon (GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_TT));
192 wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
193 wincl.hbrBackground = (HBRUSH)COLOR_BACKGROUND;
194 wincl.lpszMenuName = NULL;
195 wincl.lpszClassName = g_szFontViewClassName;
196 wincl.hIconSm = LoadIcon (GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_TT));
197
198 /* Register the window class, and if it fails quit the program */
199 if (!RegisterClassExW (&wincl))
200 {
201 ErrorMsgBox(0, IDS_ERROR_NOCLASS);
202 return 0;
203 }
204
205 /* The class is registered, let's create the main window */
206 hMainWnd = CreateWindowExW(
207 0, /* Extended possibilities for variation */
208 g_szFontViewClassName, /* Classname */
209 g_ExtLogFontW.elfFullName,/* Title Text */
210 WS_OVERLAPPEDWINDOW, /* default window */
211 CW_USEDEFAULT, /* Windows decides the position */
212 CW_USEDEFAULT, /* where the window ends up on the screen */
213 544, /* The programs width */
214 375, /* and height in pixels */
215 HWND_DESKTOP, /* The window is a child-window to desktop */
216 NULL, /* No menu */
217 hThisInstance, /* Program Instance handler */
218 NULL /* No Window Creation data */
219 );
220 ShowWindow(hMainWnd, nCmdShow);
221
222 /* Main message loop */
223 while (GetMessage (&msg, NULL, 0, 0))
224 {
225 TranslateMessage(&msg);
226 DispatchMessage(&msg);
227 }
228
229 RemoveFontResourceW(argv[1]);
230
231 return (int)msg.wParam;
232 }
233
234 static LRESULT
235 MainWnd_OnCreate(HWND hwnd)
236 {
237 WCHAR szQuit[MAX_BUTTONNAME];
238 WCHAR szPrint[MAX_BUTTONNAME];
239 WCHAR szString[MAX_STRING];
240 HWND hDisplay, hButtonInstall, hButtonPrint;
241
242 /* create the display window */
243 hDisplay = CreateWindowExW(
244 0, /* Extended style */
245 g_szFontDisplayClassName, /* Classname */
246 L"", /* Title text */
247 WS_CHILD | WS_VSCROLL, /* Window style */
248 0, /* X-pos */
249 HEADER_SIZE, /* Y-Pos */
250 550, /* Width */
251 370-HEADER_SIZE, /* Height */
252 hwnd, /* Parent */
253 (HMENU)IDC_DISPLAY, /* Identifier */
254 g_hInstance, /* Program Instance handler */
255 NULL /* Window Creation data */
256 );
257
258 LoadStringW(g_hInstance, IDS_STRING, szString, MAX_STRING);
259 SendMessage(hDisplay, FVM_SETSTRING, 0, (LPARAM)szString);
260
261 /* Init the display window with the font name */
262 SendMessage(hDisplay, FVM_SETTYPEFACE, 0, (LPARAM)&g_ExtLogFontW);
263 ShowWindow(hDisplay, SW_SHOWNORMAL);
264
265 /* Create the install button */
266 LoadStringW(g_hInstance, IDS_INSTALL, szQuit, MAX_BUTTONNAME);
267 hButtonInstall = CreateWindowExW(
268 0, /* Extended style */
269 L"button", /* Classname */
270 szQuit, /* Title text */
271 WS_CHILD | WS_VISIBLE, /* Window style */
272 BUTTON_POS_X, /* X-pos */
273 BUTTON_POS_Y, /* Y-Pos */
274 BUTTON_WIDTH, /* Width */
275 BUTTON_HEIGHT, /* Height */
276 hwnd, /* Parent */
277 (HMENU)IDC_INSTALL, /* Identifier */
278 g_hInstance, /* Program Instance handler */
279 NULL /* Window Creation data */
280 );
281 SendMessage(hButtonInstall, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), (LPARAM)TRUE);
282
283 /* Create the print button */
284 LoadStringW(g_hInstance, IDS_PRINT, szPrint, MAX_BUTTONNAME);
285 hButtonPrint = CreateWindowExW(
286 0, /* Extended style */
287 L"button", /* Classname */
288 szPrint, /* Title text */
289 WS_CHILD | WS_VISIBLE, /* Window style */
290 450, /* X-pos */
291 BUTTON_POS_Y, /* Y-Pos */
292 BUTTON_WIDTH, /* Width */
293 BUTTON_HEIGHT, /* Height */
294 hwnd, /* Parent */
295 (HMENU)IDC_PRINT, /* Identifier */
296 g_hInstance, /* Program Instance handler */
297 NULL /* Window Creation data */
298 );
299 SendMessage(hButtonPrint, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), (LPARAM)TRUE);
300
301 return 0;
302 }
303
304 static LRESULT
305 MainWnd_OnSize(HWND hwnd)
306 {
307 RECT rc;
308
309 GetClientRect(hwnd, &rc);
310 MoveWindow(GetDlgItem(hwnd, IDC_PRINT), rc.right - BUTTON_WIDTH - BUTTON_POS_X, BUTTON_POS_Y, BUTTON_WIDTH, BUTTON_HEIGHT, TRUE);
311 MoveWindow(GetDlgItem(hwnd, IDC_DISPLAY), 0, HEADER_SIZE, rc.right, rc.bottom - HEADER_SIZE, TRUE);
312
313 return 0;
314 }
315
316 static LRESULT
317 MainWnd_OnPaint(HWND hwnd)
318 {
319 HDC hDC;
320 PAINTSTRUCT ps;
321 RECT rc;
322
323 hDC = BeginPaint(hwnd, &ps);
324 GetClientRect(hwnd, &rc);
325 rc.top = HEADER_SIZE - 2;
326 rc.bottom = HEADER_SIZE;
327 FillRect(hDC, &rc, GetStockObject(GRAY_BRUSH));
328 EndPaint(hwnd, &ps);
329 return 0;
330 }
331
332 static LRESULT
333 MainWnd_OnInstall(HWND hwnd)
334 {
335 DWORD fontExists;
336
337 /* First, we have to find out if the font still exists. */
338 fontExists = GetFileAttributes((LPCSTR)g_fileName);
339 if (fontExists != 0xFFFFFFFF) /* If the file does not exist */
340 {
341 ErrorMsgBox(0, IDS_ERROR_NOFONT, g_fileName);
342 return -1;
343 }
344
345 //CopyFile(g_fileName, NULL, TRUE);
346
347 MessageBox(hwnd, TEXT("This function is unimplemented"), TEXT("Unimplemented"), MB_OK);
348
349 return 0;
350 }
351
352 LRESULT CALLBACK
353 MainWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
354 {
355 switch (message)
356 {
357 case WM_CREATE:
358 return MainWnd_OnCreate(hwnd);
359
360 case WM_PAINT:
361 return MainWnd_OnPaint(hwnd);
362
363 case WM_SIZE:
364 return MainWnd_OnSize(hwnd);
365
366 case WM_COMMAND:
367 switch(LOWORD(wParam))
368 {
369 case IDC_INSTALL:
370 return MainWnd_OnInstall(hwnd);
371 break;
372
373 case IDC_PRINT:
374 return Display_OnPrint(hwnd);
375 break;
376 }
377 break;
378
379 case WM_DESTROY:
380 PostQuitMessage (0); /* send a WM_QUIT to the message queue */
381 break;
382
383 default: /* for messages that we don't deal with */
384 return DefWindowProcW(hwnd, message, wParam, lParam);
385 }
386
387 return 0;
388 }
389
390 /* EOF */