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