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