sync with trunk r47346
[reactos.git] / base / applications / charmap / charmap.c
1 /*
2 * PROJECT: ReactOS Character Map
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/applications/charmap/charmap.c
5 * PURPOSE: main dialog implementation
6 * COPYRIGHT: Copyright 2007 Ged Murphy <gedmurphy@reactos.org>
7 *
8 */
9
10 #include <precomp.h>
11
12 #define ID_ABOUT 0x1
13
14 HINSTANCE hInstance;
15
16 /* Font-enumeration callback */
17 static
18 int
19 CALLBACK
20 EnumFontNames(ENUMLOGFONTEXW *lpelfe,
21 NEWTEXTMETRICEXW *lpntme,
22 DWORD FontType,
23 LPARAM lParam)
24 {
25 HWND hwndCombo = (HWND)lParam;
26 LPWSTR pszName = lpelfe->elfLogFont.lfFaceName;
27
28 /* make sure font doesn't already exist in our list */
29 if(SendMessageW(hwndCombo,
30 CB_FINDSTRING,
31 0,
32 (LPARAM)pszName) == CB_ERR)
33 {
34 INT idx;
35 BOOL fFixed;
36 BOOL fTrueType;
37
38 /* add the font */
39 idx = (INT)SendMessageW(hwndCombo,
40 CB_ADDSTRING,
41 0,
42 (LPARAM)pszName);
43
44 /* record the font's attributes (Fixedwidth and Truetype) */
45 fFixed = (lpelfe->elfLogFont.lfPitchAndFamily & FIXED_PITCH) ? TRUE : FALSE;
46 fTrueType = (lpelfe->elfLogFont.lfOutPrecision == OUT_STROKE_PRECIS) ? TRUE : FALSE;
47
48 /* store this information in the list-item's userdata area */
49 SendMessageW(hwndCombo,
50 CB_SETITEMDATA,
51 idx,
52 MAKEWPARAM(fFixed, fTrueType));
53 }
54
55 return 1;
56 }
57
58
59 /* Initialize the font-list by enumeration all system fonts */
60 static
61 VOID
62 FillFontStyleComboList(HWND hwndCombo)
63 {
64 HDC hdc;
65 LOGFONTW lf;
66
67 /* FIXME: for fun, draw each font in its own style */
68 HFONT hFont = GetStockObject(DEFAULT_GUI_FONT);
69 SendMessageW(hwndCombo,
70 WM_SETFONT,
71 (WPARAM)hFont,
72 0);
73
74 ZeroMemory(&lf, sizeof(lf));
75 lf.lfCharSet = DEFAULT_CHARSET;
76
77 hdc = GetDC(hwndCombo);
78
79 /* store the list of fonts in the combo */
80 EnumFontFamiliesExW(hdc,
81 &lf,
82 (FONTENUMPROCW)EnumFontNames,
83 (LPARAM)hwndCombo,
84 0);
85
86 ReleaseDC(hwndCombo,
87 hdc);
88
89 SendMessageW(hwndCombo,
90 CB_SETCURSEL,
91 0,
92 0);
93 }
94
95
96 static
97 VOID
98 ChangeMapFont(HWND hDlg)
99 {
100 HWND hCombo;
101 HWND hMap;
102 LPWSTR lpFontName;
103 INT Len;
104
105 hCombo = GetDlgItem(hDlg, IDC_FONTCOMBO);
106
107 Len = GetWindowTextLengthW(hCombo);
108
109 if (Len != 0)
110 {
111 lpFontName = HeapAlloc(GetProcessHeap(),
112 0,
113 (Len + 1) * sizeof(WCHAR));
114
115 if (lpFontName)
116 {
117 SendMessageW(hCombo,
118 WM_GETTEXT,
119 Len + 1,
120 (LPARAM)lpFontName);
121
122 hMap = GetDlgItem(hDlg, IDC_FONTMAP);
123
124 SendMessageW(hMap,
125 FM_SETFONT,
126 0,
127 (LPARAM)lpFontName);
128 }
129
130 HeapFree(GetProcessHeap(),
131 0,
132 lpFontName);
133 }
134 }
135
136
137 static
138 VOID
139 AddCharToSelection(HWND hText,
140 WCHAR ch)
141 {
142 LPWSTR lpText;
143 INT Len = GetWindowTextLength(hText);
144
145 if (Len != 0)
146 {
147 lpText = HeapAlloc(GetProcessHeap(),
148 0,
149 (Len + 2) * sizeof(WCHAR));
150
151 if (lpText)
152 {
153 LPWSTR lpStr = lpText;
154
155 SendMessageW(hText,
156 WM_GETTEXT,
157 Len + 1,
158 (LPARAM)lpStr);
159
160 lpStr += Len;
161 *lpStr = ch;
162 lpStr++;
163 *lpStr = L'\0';
164
165 SendMessageW(hText,
166 WM_SETTEXT,
167 0,
168 (LPARAM)lpText);
169
170 HeapFree(GetProcessHeap(),
171 0,
172 lpText);
173 }
174 }
175 else
176 {
177 WCHAR szText[2];
178
179 szText[0] = ch;
180 szText[1] = L'\0';
181
182 SendMessageW(hText,
183 WM_SETTEXT,
184 0,
185 (LPARAM)szText);
186 }
187 }
188
189
190 static
191 INT_PTR
192 CALLBACK
193 DlgProc(HWND hDlg,
194 UINT Message,
195 WPARAM wParam,
196 LPARAM lParam)
197 {
198 static HICON hSmIcon;
199 static HICON hBgIcon;
200 LPWSTR lpAboutText = NULL;
201
202 switch(Message)
203 {
204 case WM_INITDIALOG:
205 {
206 HMENU hSysMenu;
207
208 hSmIcon = LoadImageW(hInstance,
209 MAKEINTRESOURCEW(IDI_ICON),
210 IMAGE_ICON,
211 16,
212 16,
213 0);
214 if (hSmIcon)
215 {
216 SendMessageW(hDlg,
217 WM_SETICON,
218 ICON_SMALL,
219 (LPARAM)hSmIcon);
220 }
221
222 hBgIcon = LoadImageW(hInstance,
223 MAKEINTRESOURCEW(IDI_ICON),
224 IMAGE_ICON,
225 32,
226 32,
227 0);
228 if (hBgIcon)
229 {
230 SendMessageW(hDlg,
231 WM_SETICON,
232 ICON_BIG,
233 (LPARAM)hBgIcon);
234 }
235
236 FillFontStyleComboList(GetDlgItem(hDlg,
237 IDC_FONTCOMBO));
238
239 ChangeMapFont(hDlg);
240 hSysMenu = GetSystemMenu(hDlg,
241 FALSE);
242 if (hSysMenu != NULL)
243 {
244 if (LoadStringW(hInstance,
245 IDS_ABOUT,
246 lpAboutText,
247 0))
248 {
249 AppendMenuW(hSysMenu,
250 MF_SEPARATOR,
251 0,
252 NULL);
253 AppendMenuW(hSysMenu,
254 MF_STRING,
255 ID_ABOUT,
256 lpAboutText);
257 }
258 }
259 return TRUE;
260 }
261
262 case WM_COMMAND:
263 {
264 switch(LOWORD(wParam))
265 {
266 case IDC_FONTMAP:
267 {
268 switch (HIWORD(wParam))
269 {
270 case FM_SETCHAR:
271 AddCharToSelection(GetDlgItem(hDlg, IDC_TEXTBOX),
272 LOWORD(lParam));
273 break;
274 }
275 }
276 break;
277
278 case IDC_FONTCOMBO:
279 {
280 if (HIWORD(wParam) == CBN_SELCHANGE)
281 {
282 ChangeMapFont(hDlg);
283 }
284 }
285 break;
286
287 case IDC_SELECT:
288 {
289 WCHAR ch;
290 HWND hMap = GetDlgItem(hDlg, IDC_FONTMAP);
291
292 ch = (WCHAR) SendMessageW(hMap, FM_GETCHAR, 0, 0);
293
294 if (ch)
295 {
296 AddCharToSelection(GetDlgItem(hDlg, IDC_TEXTBOX),
297 ch);
298 }
299
300 break;
301 }
302
303 case IDOK:
304 if (hSmIcon)
305 DestroyIcon(hSmIcon);
306 if (hBgIcon)
307 DestroyIcon(hBgIcon);
308 EndDialog(hDlg, 0);
309 break;
310 }
311 }
312 break;
313
314 case WM_SYSCOMMAND:
315 {
316 switch(wParam)
317 {
318 case ID_ABOUT:
319 ShowAboutDlg(hDlg);
320 break;
321 }
322 }
323 break;
324
325 case WM_CLOSE:
326 if (hSmIcon)
327 DestroyIcon(hSmIcon);
328 if (hBgIcon)
329 DestroyIcon(hBgIcon);
330 EndDialog(hDlg, 0);
331 break;
332
333 default:
334 return FALSE;
335 }
336
337 return FALSE;
338 }
339
340
341 INT
342 WINAPI
343 wWinMain(HINSTANCE hInst,
344 HINSTANCE hPrev,
345 LPWSTR Cmd,
346 int iCmd)
347 {
348 INITCOMMONCONTROLSEX iccx;
349 INT Ret = 1;
350
351 hInstance = hInst;
352
353 iccx.dwSize = sizeof(INITCOMMONCONTROLSEX);
354 iccx.dwICC = ICC_TAB_CLASSES;
355 InitCommonControlsEx(&iccx);
356
357 if (RegisterMapClasses(hInstance))
358 {
359 Ret = DialogBoxW(hInstance,
360 MAKEINTRESOURCEW(IDD_CHARMAP),
361 NULL,
362 DlgProc) >= 0;
363
364 UnregisterMapClasses(hInstance);
365 }
366
367 return Ret;
368 }