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