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