c7be1d393da20cf8650205bd768a75d3484b714a
[reactos.git] / reactos / dll / win32 / comdlg32 / fontdlg16.c
1 /*
2 * COMMDLG - Font Dialog
3 *
4 * Copyright 1994 Martin Ayotte
5 * Copyright 1996 Albrecht Kleine
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 */
21
22 #include <ctype.h>
23 #include <stdlib.h>
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wingdi.h"
30 #include "winuser.h"
31 #include "winnls.h"
32 #include "wine/winbase16.h"
33 #include "wine/winuser16.h"
34 #include "commdlg.h"
35 #include "wine/debug.h"
36 #include "cderr.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(commdlg);
39
40 #include "cdlg.h"
41 #include "cdlg16.h"
42
43 static const WCHAR strWineFontData16[] =
44 {'_','_','W','I','N','E','_','F','O','N','T','D','L','G','D','A','T','A','1','6',0};
45
46 static void FONT_LogFont16To32W( const LOGFONT16 *font16, LPLOGFONTW font32 )
47 {
48 font32->lfHeight = font16->lfHeight;
49 font32->lfWidth = font16->lfWidth;
50 font32->lfEscapement = font16->lfEscapement;
51 font32->lfOrientation = font16->lfOrientation;
52 font32->lfWeight = font16->lfWeight;
53 font32->lfItalic = font16->lfItalic;
54 font32->lfUnderline = font16->lfUnderline;
55 font32->lfStrikeOut = font16->lfStrikeOut;
56 font32->lfCharSet = font16->lfCharSet;
57 font32->lfOutPrecision = font16->lfOutPrecision;
58 font32->lfClipPrecision = font16->lfClipPrecision;
59 font32->lfQuality = font16->lfQuality;
60 font32->lfPitchAndFamily = font16->lfPitchAndFamily;
61 MultiByteToWideChar(CP_ACP, 0, font16->lfFaceName,
62 LF_FACESIZE, font32->lfFaceName, LF_FACESIZE);
63 }
64
65 static void FONT_Metrics16To32W( const TEXTMETRIC16 *pm16,
66 NEWTEXTMETRICEXW *pnm32w)
67 {
68 ZeroMemory( pnm32w, sizeof(NEWTEXTMETRICEXW));
69 /* NOTE: only the fields used by AddFontStyle() are filled in */
70 pnm32w->ntmTm.tmHeight = pm16->tmHeight;
71 pnm32w->ntmTm.tmExternalLeading = pm16->tmExternalLeading;
72 }
73
74 static void CFn_CHOOSEFONT16to32W(const CHOOSEFONT16 *chf16, LPCHOOSEFONTW chf32w)
75 {
76 int len;
77 if (chf16->Flags & CF_ENABLETEMPLATE)
78 {
79 LPWSTR name32w;
80
81 len = MultiByteToWideChar( CP_ACP, 0, MapSL(chf16->lpTemplateName), -1, NULL, 0);
82 name32w = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
83 MultiByteToWideChar( CP_ACP, 0, MapSL(chf16->lpTemplateName), -1, name32w, len);
84 chf32w->lpTemplateName = name32w;
85 }
86 if (chf16->Flags & CF_USESTYLE)
87 {
88 len = MultiByteToWideChar( CP_ACP, 0, MapSL(chf16->lpszStyle), -1, NULL, 0);
89 chf32w->lpszStyle = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
90 MultiByteToWideChar( CP_ACP, 0, MapSL(chf16->lpszStyle), -1, chf32w->lpszStyle, len);
91 }
92 chf32w->lStructSize=sizeof(CHOOSEFONTW);
93 chf32w->hwndOwner=HWND_32(chf16->hwndOwner);
94 chf32w->hDC=HDC_32(chf16->hDC);
95 chf32w->iPointSize=chf16->iPointSize;
96 chf32w->Flags=chf16->Flags;
97 chf32w->rgbColors=chf16->rgbColors;
98 chf32w->lCustData=chf16->lCustData;
99 chf32w->lpfnHook=NULL;
100 chf32w->hInstance=HINSTANCE_32(chf16->hInstance);
101 chf32w->nFontType=chf16->nFontType;
102 chf32w->nSizeMax=chf16->nSizeMax;
103 chf32w->nSizeMin=chf16->nSizeMin;
104 FONT_LogFont16To32W(MapSL(chf16->lpLogFont), chf32w->lpLogFont);
105 }
106
107 /***********************************************************************
108 * CFn_HookCallChk [internal]
109 */
110 static BOOL CFn_HookCallChk(const CHOOSEFONT16 *lpcf)
111 {
112 if (lpcf)
113 if(lpcf->Flags & CF_ENABLEHOOK)
114 if (lpcf->lpfnHook)
115 return TRUE;
116 return FALSE;
117 }
118
119 /***********************************************************************
120 * FontFamilyEnumProc (COMMDLG.19)
121 */
122 INT16 WINAPI FontFamilyEnumProc16( SEGPTR logfont, SEGPTR metrics,
123 UINT16 nFontType, LPARAM lParam )
124 {
125 HWND hwnd=HWND_32(LOWORD(lParam));
126 HWND hDlg=GetParent(hwnd);
127 LPCHOOSEFONT16 lpcf;
128 LOGFONT16 *lplf = MapSL( logfont );
129 TEXTMETRIC16 *lpmtrx16 = MapSL(metrics);
130 ENUMLOGFONTEXW elf32w;
131 NEWTEXTMETRICEXW nmtrx32w;
132
133 lpcf = (LPCHOOSEFONT16)GetPropW(hDlg, strWineFontData16);
134 FONT_LogFont16To32W(lplf, &(elf32w.elfLogFont));
135 FONT_Metrics16To32W(lpmtrx16, &nmtrx32w);
136 return AddFontFamily(&elf32w, &nmtrx32w, nFontType,
137 (LPCHOOSEFONTW)lpcf->lpTemplateName, hwnd,NULL);
138 }
139
140 /***********************************************************************
141 * FontStyleEnumProc (COMMDLG.18)
142 */
143 INT16 WINAPI FontStyleEnumProc16( SEGPTR logfont, SEGPTR metrics,
144 UINT16 nFontType, LPARAM lParam )
145 {
146 HWND hcmb2=HWND_32(LOWORD(lParam));
147 HWND hcmb3=HWND_32(HIWORD(lParam));
148 HWND hDlg=GetParent(hcmb3);
149 LPCHOOSEFONT16 lpcf;
150 LOGFONT16 *lplf = MapSL(logfont);
151 TEXTMETRIC16 *lpmtrx16 = MapSL(metrics);
152 ENUMLOGFONTEXW elf32w;
153 NEWTEXTMETRICEXW nmtrx32w;
154
155 lpcf = (LPCHOOSEFONT16)GetPropW(hDlg, strWineFontData16);
156 FONT_LogFont16To32W(lplf, &(elf32w.elfLogFont));
157 FONT_Metrics16To32W(lpmtrx16, &nmtrx32w);
158 return AddFontStyle(&elf32w, &nmtrx32w, nFontType,
159 (LPCHOOSEFONTW)lpcf->lpTemplateName, hcmb2, hcmb3, hDlg, TRUE);
160 }
161
162 /***********************************************************************
163 * ChooseFont (COMMDLG.15)
164 */
165 BOOL16 WINAPI ChooseFont16(LPCHOOSEFONT16 lpChFont)
166 {
167 HINSTANCE16 hInst;
168 HANDLE16 hDlgTmpl16 = 0;
169 HGLOBAL16 hGlobal16 = 0;
170 BOOL16 bRet = FALSE;
171 LPVOID template;
172 FARPROC16 ptr;
173 CHOOSEFONTW cf32w;
174 LOGFONTW lf32w;
175 LOGFONT16 *font16;
176 SEGPTR lpTemplateName;
177
178 TRACE("ChooseFont\n");
179
180 if (!lpChFont) return FALSE;
181
182 cf32w.lpLogFont=&lf32w;
183 CFn_CHOOSEFONT16to32W(lpChFont, &cf32w);
184
185 if (TRACE_ON(commdlg))
186 _dump_cf_flags(lpChFont->Flags);
187
188 if (lpChFont->Flags & CF_ENABLETEMPLATEHANDLE)
189 {
190 if (!LockResource16( lpChFont->hInstance ))
191 {
192 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
193 return FALSE;
194 }
195 }
196 else if (lpChFont->Flags & CF_ENABLETEMPLATE)
197 {
198 HANDLE16 hResInfo;
199 if (!(hResInfo = FindResource16( lpChFont->hInstance,
200 MapSL(lpChFont->lpTemplateName),
201 (LPSTR)RT_DIALOG)))
202 {
203 COMDLG32_SetCommDlgExtendedError(CDERR_FINDRESFAILURE);
204 return FALSE;
205 }
206 if (!(hDlgTmpl16 = LoadResource16( lpChFont->hInstance, hResInfo )) ||
207 !LockResource16( hDlgTmpl16 ))
208 {
209 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
210 return FALSE;
211 }
212 }
213 else
214 {
215 HRSRC hResInfo;
216 HGLOBAL hDlgTmpl32;
217 LPCVOID template32;
218 DWORD size;
219 if (!(hResInfo = FindResourceA(COMDLG32_hInstance, "CHOOSE_FONT", (LPSTR)RT_DIALOG)))
220 {
221 COMDLG32_SetCommDlgExtendedError(CDERR_FINDRESFAILURE);
222 return FALSE;
223 }
224 if (!(hDlgTmpl32 = LoadResource(COMDLG32_hInstance, hResInfo)) ||
225 !(template32 = LockResource(hDlgTmpl32)))
226 {
227 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
228 return FALSE;
229 }
230 size = SizeofResource(COMDLG32_hInstance, hResInfo);
231 hGlobal16 = GlobalAlloc16(0, size);
232 if (!hGlobal16)
233 {
234 COMDLG32_SetCommDlgExtendedError(CDERR_MEMALLOCFAILURE);
235 ERR("alloc failure for %d bytes\n", size);
236 return FALSE;
237 }
238 template = GlobalLock16(hGlobal16);
239 if (!template)
240 {
241 COMDLG32_SetCommDlgExtendedError(CDERR_MEMLOCKFAILURE);
242 ERR("global lock failure for %x handle\n", hGlobal16);
243 GlobalFree16(hGlobal16);
244 return FALSE;
245 }
246 ConvertDialog32To16(template32, size, template);
247 hDlgTmpl16 = hGlobal16;
248
249 }
250
251 /* lpTemplateName is not used in the dialog */
252 lpTemplateName=lpChFont->lpTemplateName;
253 lpChFont->lpTemplateName=(SEGPTR)&cf32w;
254
255 ptr = GetProcAddress16(GetModuleHandle16("COMMDLG"), (LPCSTR) 16);
256 hInst = GetWindowLongPtrA(HWND_32(lpChFont->hwndOwner), GWLP_HINSTANCE);
257 bRet = DialogBoxIndirectParam16(hInst, hDlgTmpl16, lpChFont->hwndOwner,
258 (DLGPROC16) ptr, (DWORD)lpChFont);
259 if (hGlobal16)
260 {
261 GlobalUnlock16(hGlobal16);
262 GlobalFree16(hGlobal16);
263 }
264 lpChFont->lpTemplateName=lpTemplateName;
265
266 lpChFont->iPointSize = cf32w.iPointSize;
267 lpChFont->Flags = cf32w.Flags;
268 lpChFont->rgbColors = cf32w.rgbColors;
269 lpChFont->lCustData = cf32w.lCustData;
270 lpChFont->nFontType = cf32w.nFontType;
271
272 font16 = MapSL(lpChFont->lpLogFont);
273 font16->lfHeight = cf32w.lpLogFont->lfHeight;
274 font16->lfWidth = cf32w.lpLogFont->lfWidth;
275 font16->lfEscapement = cf32w.lpLogFont->lfEscapement;
276 font16->lfOrientation = cf32w.lpLogFont->lfOrientation;
277 font16->lfWeight = cf32w.lpLogFont->lfWeight;
278 font16->lfItalic = cf32w.lpLogFont->lfItalic;
279 font16->lfUnderline = cf32w.lpLogFont->lfUnderline;
280 font16->lfStrikeOut = cf32w.lpLogFont->lfStrikeOut;
281 font16->lfCharSet = cf32w.lpLogFont->lfCharSet;
282 font16->lfOutPrecision = cf32w.lpLogFont->lfOutPrecision;
283 font16->lfClipPrecision = cf32w.lpLogFont->lfClipPrecision;
284 font16->lfQuality = cf32w.lpLogFont->lfQuality;
285 font16->lfPitchAndFamily = cf32w.lpLogFont->lfPitchAndFamily;
286 WideCharToMultiByte(CP_ACP, 0, cf32w.lpLogFont->lfFaceName,
287 LF_FACESIZE, font16->lfFaceName, LF_FACESIZE, 0, 0);
288
289 HeapFree(GetProcessHeap(), 0, (LPBYTE)cf32w.lpTemplateName);
290 HeapFree(GetProcessHeap(), 0, cf32w.lpszStyle);
291
292 return bRet;
293 }
294
295 /***********************************************************************
296 * FormatCharDlgProc (COMMDLG.16)
297 FIXME: 1. some strings are "hardcoded", but it's better load from sysres
298 2. some CF_.. flags are not supported
299 3. some TType extensions
300 */
301 BOOL16 CALLBACK FormatCharDlgProc16(HWND16 hDlg16, UINT16 message,
302 WPARAM16 wParam, LPARAM lParam)
303 {
304 HWND hDlg = HWND_32(hDlg16);
305 LPCHOOSEFONT16 lpcf;
306 BOOL16 res=0;
307 if (message!=WM_INITDIALOG)
308 {
309 lpcf = (LPCHOOSEFONT16)GetPropW(hDlg, strWineFontData16);
310 if (!lpcf)
311 return FALSE;
312 if (CFn_HookCallChk(lpcf))
313 res=CallWindowProc16((WNDPROC16)lpcf->lpfnHook,hDlg16,message,wParam,lParam);
314 if (res)
315 return res;
316 }
317 else
318 {
319 lpcf=(LPCHOOSEFONT16)lParam;
320 if (!CFn_WMInitDialog(hDlg, wParam, lParam, (LPCHOOSEFONTW)lpcf->lpTemplateName))
321 {
322 TRACE("CFn_WMInitDialog returned FALSE\n");
323 return FALSE;
324 }
325 SetPropW(hDlg, strWineFontData16, (HANDLE)lParam);
326 if (CFn_HookCallChk(lpcf))
327 return CallWindowProc16((WNDPROC16)lpcf->lpfnHook,hDlg16,WM_INITDIALOG,wParam,lParam);
328 }
329 switch (message)
330 {
331 case WM_MEASUREITEM:
332 {
333 MEASUREITEMSTRUCT16* mis16 = MapSL(lParam);
334 MEASUREITEMSTRUCT mis;
335 mis.CtlType = mis16->CtlType;
336 mis.CtlID = mis16->CtlID;
337 mis.itemID = mis16->itemID;
338 mis.itemWidth = mis16->itemWidth;
339 mis.itemHeight = mis16->itemHeight;
340 mis.itemData = mis16->itemData;
341 res = CFn_WMMeasureItem(hDlg, wParam, (LPARAM)&mis);
342 mis16->itemWidth = (UINT16)mis.itemWidth;
343 mis16->itemHeight = (UINT16)mis.itemHeight;
344 }
345 break;
346 case WM_DRAWITEM:
347 {
348 DRAWITEMSTRUCT16* dis16 = MapSL(lParam);
349 DRAWITEMSTRUCT dis;
350 dis.CtlType = dis16->CtlType;
351 dis.CtlID = dis16->CtlID;
352 dis.itemID = dis16->itemID;
353 dis.itemAction = dis16->itemAction;
354 dis.itemState = dis16->itemState;
355 dis.hwndItem = HWND_32(dis16->hwndItem);
356 dis.hDC = HDC_32(dis16->hDC);
357 dis.itemData = dis16->itemData;
358 dis.rcItem.left = dis16->rcItem.left;
359 dis.rcItem.top = dis16->rcItem.top;
360 dis.rcItem.right = dis16->rcItem.right;
361 dis.rcItem.bottom = dis16->rcItem.bottom;
362 res = CFn_WMDrawItem(hDlg, wParam, (LPARAM)&dis);
363 }
364 break;
365 case WM_COMMAND:
366 res=CFn_WMCommand(hDlg, MAKEWPARAM( wParam, HIWORD(lParam) ), LOWORD(lParam),
367 (LPCHOOSEFONTW)lpcf->lpTemplateName);
368 break;
369 case WM_DESTROY:
370 return TRUE;
371 case WM_CHOOSEFONT_GETLOGFONT:
372 TRACE("WM_CHOOSEFONT_GETLOGFONT lParam=%08lX\n", lParam);
373 FIXME("current logfont back to caller\n");
374 break;
375 case WM_PAINT:
376 res= CFn_WMPaint(hDlg, wParam, lParam, (LPCHOOSEFONTW)lpcf->lpTemplateName);
377 break;
378 }
379 return res;
380 }