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