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