mcicda: Exclude unused headers.
[wine] / dlls / 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       len = MultiByteToWideChar( CP_ACP, 0, MapSL(chf16->lpTemplateName), -1, NULL, 0);
80       chf32w->lpTemplateName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
81       MultiByteToWideChar( CP_ACP, 0, MapSL(chf16->lpTemplateName),
82                            -1, (LPWSTR)chf32w->lpTemplateName, len);
83   }
84   if (chf16->Flags & CF_USESTYLE)
85   {
86       len = MultiByteToWideChar( CP_ACP, 0, MapSL(chf16->lpszStyle), -1, NULL, 0);
87       chf32w->lpszStyle = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
88       MultiByteToWideChar( CP_ACP, 0, MapSL(chf16->lpszStyle), -1, chf32w->lpszStyle, len);
89   }
90   chf32w->lStructSize=sizeof(CHOOSEFONTW);
91   chf32w->hwndOwner=HWND_32(chf16->hwndOwner);
92   chf32w->hDC=HDC_32(chf16->hDC);
93   chf32w->iPointSize=chf16->iPointSize;
94   chf32w->Flags=chf16->Flags;
95   chf32w->rgbColors=chf16->rgbColors;
96   chf32w->lCustData=chf16->lCustData;
97   chf32w->lpfnHook=NULL;
98   chf32w->hInstance=HINSTANCE_32(chf16->hInstance);
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(const CHOOSEFONT16 *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;
126   LOGFONT16 *lplf = MapSL( logfont );
127   TEXTMETRIC16 *lpmtrx16 = MapSL(metrics);
128   ENUMLOGFONTEXW elf32w;
129   NEWTEXTMETRICEXW nmtrx32w;
130
131   lpcf = (LPCHOOSEFONT16)GetPropW(hDlg, strWineFontData16);
132   FONT_LogFont16To32W(lplf, &(elf32w.elfLogFont));
133   FONT_Metrics16To32W(lpmtrx16, &nmtrx32w);
134   return AddFontFamily(&elf32w, &nmtrx32w, nFontType,
135           (LPCHOOSEFONTW)lpcf->lpTemplateName, hwnd,NULL);
136 }
137
138 /***********************************************************************
139  *                 FontStyleEnumProc                     (COMMDLG.18)
140  */
141 INT16 WINAPI FontStyleEnumProc16( SEGPTR logfont, SEGPTR metrics,
142                                   UINT16 nFontType, LPARAM lParam )
143 {
144   HWND hcmb2=HWND_32(LOWORD(lParam));
145   HWND hcmb3=HWND_32(HIWORD(lParam));
146   HWND hDlg=GetParent(hcmb3);
147   LPCHOOSEFONT16 lpcf;
148   LOGFONT16 *lplf = MapSL(logfont);
149   TEXTMETRIC16 *lpmtrx16 = MapSL(metrics);
150   ENUMLOGFONTEXW elf32w;
151   NEWTEXTMETRICEXW nmtrx32w;
152
153   lpcf = (LPCHOOSEFONT16)GetPropW(hDlg, strWineFontData16);
154   FONT_LogFont16To32W(lplf, &(elf32w.elfLogFont));
155   FONT_Metrics16To32W(lpmtrx16, &nmtrx32w);
156   return AddFontStyle(&elf32w, &nmtrx32w, nFontType,
157           (LPCHOOSEFONTW)lpcf->lpTemplateName, hcmb2, hcmb3, hDlg, TRUE);
158 }
159
160 /***********************************************************************
161  *                        ChooseFont   (COMMDLG.15)
162  */
163 BOOL16 WINAPI ChooseFont16(LPCHOOSEFONT16 lpChFont)
164 {
165     HINSTANCE16 hInst;
166     HANDLE16 hDlgTmpl16 = 0;
167     HGLOBAL16 hGlobal16 = 0;
168     BOOL16 bRet = FALSE;
169     LPVOID template;
170     FARPROC16 ptr;
171     CHOOSEFONTW cf32w;
172     LOGFONTW lf32w;
173     LOGFONT16 *font16;
174     SEGPTR lpTemplateName;
175
176     TRACE("ChooseFont\n");
177
178     if (!lpChFont) return FALSE;
179
180     cf32w.lpLogFont=&lf32w;
181     CFn_CHOOSEFONT16to32W(lpChFont, &cf32w);
182
183     if (TRACE_ON(commdlg))
184         _dump_cf_flags(lpChFont->Flags);
185
186     if (lpChFont->Flags & CF_ENABLETEMPLATEHANDLE)
187     {
188         if (!(template = LockResource16( lpChFont->hInstance )))
189         {
190             COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
191             return FALSE;
192         }
193     }
194     else if (lpChFont->Flags & CF_ENABLETEMPLATE)
195     {
196         HANDLE16 hResInfo;
197         if (!(hResInfo = FindResource16( lpChFont->hInstance,
198                                          MapSL(lpChFont->lpTemplateName),
199                                          (LPSTR)RT_DIALOG)))
200         {
201             COMDLG32_SetCommDlgExtendedError(CDERR_FINDRESFAILURE);
202             return FALSE;
203         }
204         if (!(hDlgTmpl16 = LoadResource16( lpChFont->hInstance, hResInfo )) ||
205             !(template = LockResource16( hDlgTmpl16 )))
206         {
207             COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
208             return FALSE;
209         }
210     }
211     else
212     {
213         HRSRC hResInfo;
214         HGLOBAL hDlgTmpl32;
215         LPCVOID template32;
216         DWORD size;
217         if (!(hResInfo = FindResourceA(COMDLG32_hInstance, "CHOOSE_FONT", (LPSTR)RT_DIALOG)))
218         {
219             COMDLG32_SetCommDlgExtendedError(CDERR_FINDRESFAILURE);
220             return FALSE;
221         }
222         if (!(hDlgTmpl32 = LoadResource(COMDLG32_hInstance, hResInfo)) ||
223             !(template32 = LockResource(hDlgTmpl32)))
224         {
225             COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
226             return FALSE;
227         }
228         size = SizeofResource(COMDLG32_hInstance, hResInfo);
229         hGlobal16 = GlobalAlloc16(0, size);
230         if (!hGlobal16)
231         {
232             COMDLG32_SetCommDlgExtendedError(CDERR_MEMALLOCFAILURE);
233             ERR("alloc failure for %d bytes\n", size);
234             return FALSE;
235         }
236         template = GlobalLock16(hGlobal16);
237         if (!template)
238         {
239             COMDLG32_SetCommDlgExtendedError(CDERR_MEMLOCKFAILURE);
240             ERR("global lock failure for %x handle\n", hGlobal16);
241             GlobalFree16(hGlobal16);
242             return FALSE;
243         }
244         ConvertDialog32To16(template32, size, template);
245         hDlgTmpl16 = hGlobal16;
246
247     }
248
249     /* lpTemplateName is not used in the dialog */
250     lpTemplateName=lpChFont->lpTemplateName;
251     lpChFont->lpTemplateName=(SEGPTR)&cf32w;
252
253     ptr = GetProcAddress16(GetModuleHandle16("COMMDLG"), (LPCSTR) 16);
254     hInst = GetWindowLongPtrA(HWND_32(lpChFont->hwndOwner), GWLP_HINSTANCE);
255     bRet = DialogBoxIndirectParam16(hInst, hDlgTmpl16, lpChFont->hwndOwner,
256                      (DLGPROC16) ptr, (DWORD)lpChFont);
257     if (hGlobal16)
258     {
259         GlobalUnlock16(hGlobal16);
260         GlobalFree16(hGlobal16);
261     }
262     lpChFont->lpTemplateName=lpTemplateName;
263
264     lpChFont->iPointSize = cf32w.iPointSize;
265     lpChFont->Flags = cf32w.Flags;
266     lpChFont->rgbColors = cf32w.rgbColors;
267     lpChFont->lCustData = cf32w.lCustData;
268     lpChFont->nFontType = cf32w.nFontType;
269
270     font16 = MapSL(lpChFont->lpLogFont);
271     font16->lfHeight = cf32w.lpLogFont->lfHeight;
272     font16->lfWidth = cf32w.lpLogFont->lfWidth;
273     font16->lfEscapement = cf32w.lpLogFont->lfEscapement;
274     font16->lfOrientation = cf32w.lpLogFont->lfOrientation;
275     font16->lfWeight = cf32w.lpLogFont->lfWeight;
276     font16->lfItalic = cf32w.lpLogFont->lfItalic;
277     font16->lfUnderline = cf32w.lpLogFont->lfUnderline;
278     font16->lfStrikeOut = cf32w.lpLogFont->lfStrikeOut;
279     font16->lfCharSet = cf32w.lpLogFont->lfCharSet;
280     font16->lfOutPrecision = cf32w.lpLogFont->lfOutPrecision;
281     font16->lfClipPrecision = cf32w.lpLogFont->lfClipPrecision;
282     font16->lfQuality = cf32w.lpLogFont->lfQuality;
283     font16->lfPitchAndFamily = cf32w.lpLogFont->lfPitchAndFamily;
284     WideCharToMultiByte(CP_ACP, 0, cf32w.lpLogFont->lfFaceName,
285                           LF_FACESIZE, font16->lfFaceName, LF_FACESIZE, 0, 0);
286
287     HeapFree(GetProcessHeap(), 0, (LPBYTE)cf32w.lpTemplateName);
288     HeapFree(GetProcessHeap(), 0, cf32w.lpszStyle);
289
290     return bRet;
291 }
292
293 /***********************************************************************
294  *           FormatCharDlgProc   (COMMDLG.16)
295              FIXME: 1. some strings are "hardcoded", but it's better load from sysres
296                     2. some CF_.. flags are not supported
297                     3. some TType extensions
298  */
299 BOOL16 CALLBACK FormatCharDlgProc16(HWND16 hDlg16, UINT16 message,
300                                    WPARAM16 wParam, LPARAM lParam)
301 {
302   HWND hDlg = HWND_32(hDlg16);
303   LPCHOOSEFONT16 lpcf;
304   BOOL16 res=0;
305   if (message!=WM_INITDIALOG)
306   {
307       lpcf = (LPCHOOSEFONT16)GetPropW(hDlg, strWineFontData16);
308       if (!lpcf)
309           return FALSE;
310       if (CFn_HookCallChk(lpcf))
311           res=CallWindowProc16((WNDPROC16)lpcf->lpfnHook,hDlg16,message,wParam,lParam);
312       if (res)
313           return res;
314   }
315   else
316   {
317     lpcf=(LPCHOOSEFONT16)lParam;
318     if (!CFn_WMInitDialog(hDlg, wParam, lParam, (LPCHOOSEFONTW)lpcf->lpTemplateName))
319     {
320       TRACE("CFn_WMInitDialog returned FALSE\n");
321       return FALSE;
322     }
323     SetPropW(hDlg, strWineFontData16, (HANDLE)lParam);
324     if (CFn_HookCallChk(lpcf))
325       return CallWindowProc16((WNDPROC16)lpcf->lpfnHook,hDlg16,WM_INITDIALOG,wParam,lParam);
326   }
327   switch (message)
328     {
329     case WM_MEASUREITEM:
330         {
331             MEASUREITEMSTRUCT16* mis16 = MapSL(lParam);
332             MEASUREITEMSTRUCT mis;
333             mis.CtlType    = mis16->CtlType;
334             mis.CtlID      = mis16->CtlID;
335             mis.itemID     = mis16->itemID;
336             mis.itemWidth  = mis16->itemWidth;
337             mis.itemHeight = mis16->itemHeight;
338             mis.itemData   = mis16->itemData;
339             res = CFn_WMMeasureItem(hDlg, wParam, (LPARAM)&mis);
340             mis16->itemWidth  = (UINT16)mis.itemWidth;
341             mis16->itemHeight = (UINT16)mis.itemHeight;
342         }
343         break;
344     case WM_DRAWITEM:
345         {
346             DRAWITEMSTRUCT16* dis16 = MapSL(lParam);
347             DRAWITEMSTRUCT dis;
348             dis.CtlType       = dis16->CtlType;
349             dis.CtlID         = dis16->CtlID;
350             dis.itemID        = dis16->itemID;
351             dis.itemAction    = dis16->itemAction;
352             dis.itemState     = dis16->itemState;
353             dis.hwndItem      = HWND_32(dis16->hwndItem);
354             dis.hDC           = HDC_32(dis16->hDC);
355             dis.itemData      = dis16->itemData;
356             dis.rcItem.left   = dis16->rcItem.left;
357             dis.rcItem.top    = dis16->rcItem.top;
358             dis.rcItem.right  = dis16->rcItem.right;
359             dis.rcItem.bottom = dis16->rcItem.bottom;
360             res = CFn_WMDrawItem(hDlg, wParam, (LPARAM)&dis);
361         }
362         break;
363     case WM_COMMAND:
364         res=CFn_WMCommand(hDlg, MAKEWPARAM( wParam, HIWORD(lParam) ), LOWORD(lParam),
365                           (LPCHOOSEFONTW)lpcf->lpTemplateName);
366         break;
367     case WM_DESTROY:
368         return TRUE;
369     case WM_CHOOSEFONT_GETLOGFONT:
370         TRACE("WM_CHOOSEFONT_GETLOGFONT lParam=%08lX\n", lParam);
371         FIXME("current logfont back to caller\n");
372         break;
373     case WM_PAINT:
374         res= CFn_WMPaint(hDlg, wParam, lParam, (LPCHOOSEFONTW)lpcf->lpTemplateName);
375         break;
376     }
377   return res;
378 }