d80e75ca6bbc3383aa173048839b865c9900ed5d
[reactos.git] / reactos / lib / 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 "dlgs.h"
36 #include "wine/debug.h"
37 #include "cderr.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(commdlg);
40
41 #include "cdlg.h"
42 #include "cdlg16.h"
43
44 static void FONT_LogFont16To32W( const LPLOGFONT16 font16, LPLOGFONTW font32 )
45 {
46 font32->lfHeight = font16->lfHeight;
47 font32->lfWidth = font16->lfWidth;
48 font32->lfEscapement = font16->lfEscapement;
49 font32->lfOrientation = font16->lfOrientation;
50 font32->lfWeight = font16->lfWeight;
51 font32->lfItalic = font16->lfItalic;
52 font32->lfUnderline = font16->lfUnderline;
53 font32->lfStrikeOut = font16->lfStrikeOut;
54 font32->lfCharSet = font16->lfCharSet;
55 font32->lfOutPrecision = font16->lfOutPrecision;
56 font32->lfClipPrecision = font16->lfClipPrecision;
57 font32->lfQuality = font16->lfQuality;
58 font32->lfPitchAndFamily = font16->lfPitchAndFamily;
59 MultiByteToWideChar(CP_ACP, 0, font16->lfFaceName,
60 LF_FACESIZE, font32->lfFaceName, LF_FACESIZE);
61 };
62
63 static void FONT_Metrics16To32W( const TEXTMETRIC16 *pm16,
64 NEWTEXTMETRICEXW *pnm32w)
65 {
66 ZeroMemory( pnm32w, sizeof(NEWTEXTMETRICEXW));
67 /* NOTE: only the fields used by AddFontStyle() are filled in */
68 pnm32w->ntmTm.tmHeight = pm16->tmHeight;
69 pnm32w->ntmTm.tmExternalLeading = pm16->tmExternalLeading;
70 };
71
72 static void CFn_CHOOSEFONT16to32W(LPCHOOSEFONT16 chf16, LPCHOOSEFONTW chf32w)
73 {
74 int len;
75 if(chf16->lpTemplateName)
76 {
77 len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)chf16->lpTemplateName, -1, NULL, 0);
78 chf32w->lpTemplateName = HeapAlloc(GetProcessHeap(), 0,len*sizeof(WCHAR));
79 MultiByteToWideChar(CP_ACP, 0, (LPSTR)MapSL(chf16->lpTemplateName),
80 -1, (LPWSTR)chf32w->lpTemplateName, len);
81 }
82 if(chf16->lpszStyle)
83 {
84 len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)chf16->lpszStyle, -1, NULL, 0);
85 chf32w->lpszStyle = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
86 MultiByteToWideChar(CP_ACP, 0, (LPSTR)MapSL(chf16->lpTemplateName),
87 -1, chf32w->lpszStyle, len);
88 }
89 chf32w->lStructSize=sizeof(CHOOSEFONTW);
90 chf32w->hwndOwner=HWND_32(chf16->hwndOwner);
91 chf32w->hDC=HDC_32(chf16->hDC);
92 chf32w->iPointSize=chf16->iPointSize;
93 chf32w->Flags=chf16->Flags;
94 chf32w->rgbColors=chf16->rgbColors;
95 chf32w->lCustData=chf16->lCustData;
96 chf32w->lpfnHook=NULL;
97 chf32w->hInstance=HINSTANCE_32(chf16->hInstance);
98 chf32w->lpszStyle=MapSL(chf16->lpszStyle);
99 chf32w->nFontType=chf16->nFontType;
100 chf32w->nSizeMax=chf16->nSizeMax;
101 chf32w->nSizeMin=chf16->nSizeMin;
102 FONT_LogFont16To32W(MapSL(chf16->lpLogFont), chf32w->lpLogFont);
103 };
104
105 /***********************************************************************
106 * CFn_HookCallChk [internal]
107 */
108 static BOOL CFn_HookCallChk(LPCHOOSEFONT16 lpcf)
109 {
110 if (lpcf)
111 if(lpcf->Flags & CF_ENABLEHOOK)
112 if (lpcf->lpfnHook)
113 return TRUE;
114 return FALSE;
115 }
116
117 /***********************************************************************
118 * FontFamilyEnumProc (COMMDLG.19)
119 */
120 INT16 WINAPI FontFamilyEnumProc16( SEGPTR logfont, SEGPTR metrics,
121 UINT16 nFontType, LPARAM lParam )
122 {
123 HWND hwnd=HWND_32(LOWORD(lParam));
124 HWND hDlg=GetParent(hwnd);
125 LPCHOOSEFONT16 lpcf=(LPCHOOSEFONT16)GetWindowLongPtrW(hDlg, DWLP_USER);
126 LOGFONT16 *lplf = MapSL( logfont );
127 TEXTMETRIC16 *lpmtrx16 = MapSL(metrics);
128 ENUMLOGFONTEXW elf32w;
129 NEWTEXTMETRICEXW nmtrx32w;
130 FONT_LogFont16To32W(lplf, &(elf32w.elfLogFont));
131 FONT_Metrics16To32W(lpmtrx16, &nmtrx32w);
132 return AddFontFamily(&elf32w, &nmtrx32w, nFontType,
133 (LPCHOOSEFONTW)lpcf->lpTemplateName, hwnd,NULL);
134 }
135
136 /***********************************************************************
137 * FontStyleEnumProc (COMMDLG.18)
138 */
139 INT16 WINAPI FontStyleEnumProc16( SEGPTR logfont, SEGPTR metrics,
140 UINT16 nFontType, LPARAM lParam )
141 {
142 HWND hcmb2=HWND_32(LOWORD(lParam));
143 HWND hcmb3=HWND_32(HIWORD(lParam));
144 HWND hDlg=GetParent(hcmb3);
145 LPCHOOSEFONT16 lpcf=(LPCHOOSEFONT16)GetWindowLongPtrW(hDlg, DWLP_USER);
146 LOGFONT16 *lplf = MapSL(logfont);
147 TEXTMETRIC16 *lpmtrx16 = MapSL(metrics);
148 ENUMLOGFONTEXW elf32w;
149 NEWTEXTMETRICEXW nmtrx32w;
150 FONT_LogFont16To32W(lplf, &(elf32w.elfLogFont));
151 FONT_Metrics16To32W(lpmtrx16, &nmtrx32w);
152 return AddFontStyle(&elf32w, &nmtrx32w, nFontType,
153 (LPCHOOSEFONTW)lpcf->lpTemplateName, hcmb2, hcmb3, hDlg, TRUE);
154 }
155
156 /***********************************************************************
157 * ChooseFont (COMMDLG.15)
158 */
159 BOOL16 WINAPI ChooseFont16(LPCHOOSEFONT16 lpChFont)
160 {
161 HINSTANCE16 hInst;
162 HANDLE16 hDlgTmpl16 = 0, hResource16 = 0;
163 HGLOBAL16 hGlobal16 = 0;
164 BOOL16 bRet = FALSE;
165 LPCVOID template;
166 FARPROC16 ptr;
167 CHOOSEFONTW cf32w;
168 LOGFONTW lf32w;
169 LOGFONT16 *font16;
170 SEGPTR lpTemplateName;
171
172 cf32w.lpLogFont=&lf32w;
173 CFn_CHOOSEFONT16to32W(lpChFont, &cf32w);
174
175 TRACE("ChooseFont\n");
176 if (!lpChFont) return FALSE;
177
178 if (TRACE_ON(commdlg))
179 _dump_cf_flags(lpChFont->Flags);
180
181 if (lpChFont->Flags & CF_ENABLETEMPLATEHANDLE)
182 {
183 if (!(template = LockResource16( lpChFont->hInstance )))
184 {
185 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
186 return FALSE;
187 }
188 }
189 else if (lpChFont->Flags & CF_ENABLETEMPLATE)
190 {
191 HANDLE16 hResInfo;
192 if (!(hResInfo = FindResource16( lpChFont->hInstance,
193 MapSL(lpChFont->lpTemplateName),
194 (LPSTR)RT_DIALOG)))
195 {
196 COMDLG32_SetCommDlgExtendedError(CDERR_FINDRESFAILURE);
197 return FALSE;
198 }
199 if (!(hDlgTmpl16 = LoadResource16( lpChFont->hInstance, hResInfo )) ||
200 !(template = LockResource16( hDlgTmpl16 )))
201 {
202 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
203 return FALSE;
204 }
205 }
206 else
207 {
208 HRSRC hResInfo;
209 HGLOBAL hDlgTmpl32;
210 LPCVOID template32;
211 DWORD size;
212 if (!(hResInfo = FindResourceA(COMDLG32_hInstance, "CHOOSE_FONT", (LPSTR)RT_DIALOG)))
213 {
214 COMDLG32_SetCommDlgExtendedError(CDERR_FINDRESFAILURE);
215 return FALSE;
216 }
217 if (!(hDlgTmpl32 = LoadResource(COMDLG32_hInstance, hResInfo)) ||
218 !(template32 = LockResource(hDlgTmpl32)))
219 {
220 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
221 return FALSE;
222 }
223 size = SizeofResource(COMDLG32_hInstance, hResInfo);
224 hGlobal16 = GlobalAlloc16(0, size);
225 if (!hGlobal16)
226 {
227 COMDLG32_SetCommDlgExtendedError(CDERR_MEMALLOCFAILURE);
228 ERR("alloc failure for %ld bytes\n", size);
229 return FALSE;
230 }
231 template = GlobalLock16(hGlobal16);
232 if (!template)
233 {
234 COMDLG32_SetCommDlgExtendedError(CDERR_MEMLOCKFAILURE);
235 ERR("global lock failure for %x handle\n", hGlobal16);
236 GlobalFree16(hGlobal16);
237 return FALSE;
238 }
239 ConvertDialog32To16((LPVOID)template32, size, (LPVOID)template);
240 hDlgTmpl16 = hGlobal16;
241
242 }
243
244 /* lpTemplateName is not used in the dialog */
245 lpTemplateName=lpChFont->lpTemplateName;
246 lpChFont->lpTemplateName=(SEGPTR)&cf32w;
247
248 ptr = GetProcAddress16(GetModuleHandle16("COMMDLG"), (LPCSTR) 16);
249 hInst = GetWindowLongPtrA(HWND_32(lpChFont->hwndOwner), GWLP_HINSTANCE);
250 bRet = DialogBoxIndirectParam16(hInst, hDlgTmpl16, lpChFont->hwndOwner,
251 (DLGPROC16) ptr, (DWORD)lpChFont);
252 if (hResource16) FreeResource16(hDlgTmpl16);
253 if (hGlobal16)
254 {
255 GlobalUnlock16(hGlobal16);
256 GlobalFree16(hGlobal16);
257 }
258 lpChFont->lpTemplateName=lpTemplateName;
259
260 lpChFont->iPointSize = cf32w.iPointSize;
261 lpChFont->Flags = cf32w.Flags;
262 lpChFont->rgbColors = cf32w.rgbColors;
263 lpChFont->lCustData = cf32w.lCustData;
264 lpChFont->nFontType = cf32w.nFontType;
265
266 font16 = MapSL(lpChFont->lpLogFont);
267 font16->lfHeight = cf32w.lpLogFont->lfHeight;
268 font16->lfWidth = cf32w.lpLogFont->lfWidth;
269 font16->lfEscapement = cf32w.lpLogFont->lfEscapement;
270 font16->lfOrientation = cf32w.lpLogFont->lfOrientation;
271 font16->lfWeight = cf32w.lpLogFont->lfWeight;
272 font16->lfItalic = cf32w.lpLogFont->lfItalic;
273 font16->lfUnderline = cf32w.lpLogFont->lfUnderline;
274 font16->lfStrikeOut = cf32w.lpLogFont->lfStrikeOut;
275 font16->lfCharSet = cf32w.lpLogFont->lfCharSet;
276 font16->lfOutPrecision = cf32w.lpLogFont->lfOutPrecision;
277 font16->lfClipPrecision = cf32w.lpLogFont->lfClipPrecision;
278 font16->lfQuality = cf32w.lpLogFont->lfQuality;
279 font16->lfPitchAndFamily = cf32w.lpLogFont->lfPitchAndFamily;
280 WideCharToMultiByte(CP_ACP, 0, cf32w.lpLogFont->lfFaceName,
281 LF_FACESIZE, font16->lfFaceName, LF_FACESIZE, 0, 0);
282
283 HeapFree(GetProcessHeap(), 0, (LPBYTE)cf32w.lpTemplateName);
284 HeapFree(GetProcessHeap(), 0, cf32w.lpszStyle);
285
286 return bRet;
287 }
288
289 /***********************************************************************
290 * FormatCharDlgProc (COMMDLG.16)
291 FIXME: 1. some strings are "hardcoded", but it's better load from sysres
292 2. some CF_.. flags are not supported
293 3. some TType extensions
294 */
295 BOOL16 CALLBACK FormatCharDlgProc16(HWND16 hDlg16, UINT16 message,
296 WPARAM16 wParam, LPARAM lParam)
297 {
298 HWND hDlg = HWND_32(hDlg16);
299 LPCHOOSEFONT16 lpcf;
300 BOOL16 res=0;
301 if (message!=WM_INITDIALOG)
302 {
303 lpcf=(LPCHOOSEFONT16)GetWindowLongPtrW(hDlg, DWLP_USER);
304 if (!lpcf && message != WM_MEASUREITEM)
305 return FALSE;
306 if (CFn_HookCallChk(lpcf))
307 res=CallWindowProc16((WNDPROC16)lpcf->lpfnHook,hDlg16,message,wParam,lParam);
308 if (res)
309 return res;
310 }
311 else
312 {
313 lpcf=(LPCHOOSEFONT16)lParam;
314 if (!CFn_WMInitDialog(hDlg, wParam, lParam, (LPCHOOSEFONTW)lpcf->lpTemplateName))
315 {
316 TRACE("CFn_WMInitDialog returned FALSE\n");
317 return FALSE;
318 }
319 if (CFn_HookCallChk(lpcf))
320 return CallWindowProc16((WNDPROC16)lpcf->lpfnHook,hDlg16,WM_INITDIALOG,wParam,lParam);
321 }
322 switch (message)
323 {
324 case WM_MEASUREITEM:
325 {
326 MEASUREITEMSTRUCT16* mis16 = MapSL(lParam);
327 MEASUREITEMSTRUCT mis;
328 mis.CtlType = mis16->CtlType;
329 mis.CtlID = mis16->CtlID;
330 mis.itemID = mis16->itemID;
331 mis.itemWidth = mis16->itemWidth;
332 mis.itemHeight = mis16->itemHeight;
333 mis.itemData = mis16->itemData;
334 res = CFn_WMMeasureItem(hDlg, wParam, (LPARAM)&mis);
335 mis16->itemWidth = (UINT16)mis.itemWidth;
336 mis16->itemHeight = (UINT16)mis.itemHeight;
337 }
338 break;
339 case WM_DRAWITEM:
340 {
341 DRAWITEMSTRUCT16* dis16 = MapSL(lParam);
342 DRAWITEMSTRUCT dis;
343 dis.CtlType = dis16->CtlType;
344 dis.CtlID = dis16->CtlID;
345 dis.itemID = dis16->itemID;
346 dis.itemAction = dis16->itemAction;
347 dis.itemState = dis16->itemState;
348 dis.hwndItem = HWND_32(dis16->hwndItem);
349 dis.hDC = HDC_32(dis16->hDC);
350 dis.itemData = dis16->itemData;
351 dis.rcItem.left = dis16->rcItem.left;
352 dis.rcItem.top = dis16->rcItem.top;
353 dis.rcItem.right = dis16->rcItem.right;
354 dis.rcItem.bottom = dis16->rcItem.bottom;
355 res = CFn_WMDrawItem(hDlg, wParam, (LPARAM)&dis);
356 }
357 break;
358 case WM_COMMAND:
359 res=CFn_WMCommand(hDlg, MAKEWPARAM( wParam, HIWORD(lParam) ), LOWORD(lParam),
360 (LPCHOOSEFONTW)lpcf->lpTemplateName);
361 break;
362 case WM_DESTROY:
363 return TRUE;
364 case WM_CHOOSEFONT_GETLOGFONT:
365 TRACE("WM_CHOOSEFONT_GETLOGFONT lParam=%08lX\n", lParam);
366 FIXME("current logfont back to caller\n");
367 break;
368 case WM_PAINT:
369 res= CFn_WMPaint(hDlg, wParam, lParam, (LPCHOOSEFONTW)lpcf->lpTemplateName);
370 break;
371 }
372 return res;
373 }