fontdlg16.c 13.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * COMMDLG - Font Dialog
 *
 * Copyright 1994 Martin Ayotte
 * Copyright 1996 Albrecht Kleine
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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 23
 */

#include <ctype.h>
#include <stdlib.h>
24
#include <stdarg.h>
25 26 27 28 29
#include <stdio.h>
#include <string.h>
#include "windef.h"
#include "winbase.h"
#include "wingdi.h"
30
#include "winuser.h"
31
#include "winnls.h"
32 33 34 35 36 37 38 39 40
#include "wine/winbase16.h"
#include "wine/winuser16.h"
#include "commdlg.h"
#include "wine/debug.h"
#include "cderr.h"

WINE_DEFAULT_DEBUG_CHANNEL(commdlg);

#include "cdlg.h"
41
#include "cdlg16.h"
42

43 44 45
static const WCHAR strWineFontData16[] =
                               {'_','_','W','I','N','E','_','F','O','N','T','D','L','G','D','A','T','A','1','6',0};

46
static void FONT_LogFont16To32W( const LOGFONT16 *font16, LPLOGFONTW font32 )
47 48 49 50 51 52 53 54 55 56 57 58 59 60
{
    font32->lfHeight = font16->lfHeight;
    font32->lfWidth = font16->lfWidth;
    font32->lfEscapement = font16->lfEscapement;
    font32->lfOrientation = font16->lfOrientation;
    font32->lfWeight = font16->lfWeight;
    font32->lfItalic = font16->lfItalic;
    font32->lfUnderline = font16->lfUnderline;
    font32->lfStrikeOut = font16->lfStrikeOut;
    font32->lfCharSet = font16->lfCharSet;
    font32->lfOutPrecision = font16->lfOutPrecision;
    font32->lfClipPrecision = font16->lfClipPrecision;
    font32->lfQuality = font16->lfQuality;
    font32->lfPitchAndFamily = font16->lfPitchAndFamily;
Jacek Caban's avatar
Jacek Caban committed
61 62
    MultiByteToWideChar(CP_ACP, 0, font16->lfFaceName,
                        LF_FACESIZE, font32->lfFaceName, LF_FACESIZE);
63
}
64

Jacek Caban's avatar
Jacek Caban committed
65 66
static void FONT_Metrics16To32W( const TEXTMETRIC16 *pm16,
                                 NEWTEXTMETRICEXW *pnm32w)
67
{
Jacek Caban's avatar
Jacek Caban committed
68
    ZeroMemory( pnm32w, sizeof(NEWTEXTMETRICEXW));
69
    /* NOTE: only the fields used by AddFontStyle() are filled in */
Jacek Caban's avatar
Jacek Caban committed
70 71
    pnm32w->ntmTm.tmHeight = pm16->tmHeight;
    pnm32w->ntmTm.tmExternalLeading = pm16->tmExternalLeading;
72
}
73

74
static void CFn_CHOOSEFONT16to32W(const CHOOSEFONT16 *chf16, LPCHOOSEFONTW chf32w)
75
{
Jacek Caban's avatar
Jacek Caban committed
76
  int len;
77
  if (chf16->Flags & CF_ENABLETEMPLATE)
Jacek Caban's avatar
Jacek Caban committed
78
  {
79 80
      LPWSTR name32w;

81
      len = MultiByteToWideChar( CP_ACP, 0, MapSL(chf16->lpTemplateName), -1, NULL, 0);
82 83 84
      name32w = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
      MultiByteToWideChar( CP_ACP, 0, MapSL(chf16->lpTemplateName), -1, name32w, len);
      chf32w->lpTemplateName = name32w;
Jacek Caban's avatar
Jacek Caban committed
85
  }
86
  if (chf16->Flags & CF_USESTYLE)
Jacek Caban's avatar
Jacek Caban committed
87
  {
88 89 90
      len = MultiByteToWideChar( CP_ACP, 0, MapSL(chf16->lpszStyle), -1, NULL, 0);
      chf32w->lpszStyle = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
      MultiByteToWideChar( CP_ACP, 0, MapSL(chf16->lpszStyle), -1, chf32w->lpszStyle, len);
Jacek Caban's avatar
Jacek Caban committed
91 92 93 94 95 96 97 98 99 100 101 102 103 104
  }
  chf32w->lStructSize=sizeof(CHOOSEFONTW);
  chf32w->hwndOwner=HWND_32(chf16->hwndOwner);
  chf32w->hDC=HDC_32(chf16->hDC);
  chf32w->iPointSize=chf16->iPointSize;
  chf32w->Flags=chf16->Flags;
  chf32w->rgbColors=chf16->rgbColors;
  chf32w->lCustData=chf16->lCustData;
  chf32w->lpfnHook=NULL;
  chf32w->hInstance=HINSTANCE_32(chf16->hInstance);
  chf32w->nFontType=chf16->nFontType;
  chf32w->nSizeMax=chf16->nSizeMax;
  chf32w->nSizeMin=chf16->nSizeMin;
  FONT_LogFont16To32W(MapSL(chf16->lpLogFont), chf32w->lpLogFont);
105
}
106 107 108 109

/***********************************************************************
 *                          CFn_HookCallChk                 [internal]
 */
110
static BOOL CFn_HookCallChk(const CHOOSEFONT16 *lpcf)
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
{
 if (lpcf)
  if(lpcf->Flags & CF_ENABLEHOOK)
   if (lpcf->lpfnHook)
    return TRUE;
 return FALSE;
}

/***********************************************************************
 *                FontFamilyEnumProc                     (COMMDLG.19)
 */
INT16 WINAPI FontFamilyEnumProc16( SEGPTR logfont, SEGPTR metrics,
                                   UINT16 nFontType, LPARAM lParam )
{
  HWND hwnd=HWND_32(LOWORD(lParam));
  HWND hDlg=GetParent(hwnd);
127
  LPCHOOSEFONT16 lpcf;
128
  LOGFONT16 *lplf = MapSL( logfont );
129
  TEXTMETRIC16 *lpmtrx16 = MapSL(metrics);
Jacek Caban's avatar
Jacek Caban committed
130 131
  ENUMLOGFONTEXW elf32w;
  NEWTEXTMETRICEXW nmtrx32w;
132 133

  lpcf = (LPCHOOSEFONT16)GetPropW(hDlg, strWineFontData16);
Jacek Caban's avatar
Jacek Caban committed
134 135 136 137
  FONT_LogFont16To32W(lplf, &(elf32w.elfLogFont));
  FONT_Metrics16To32W(lpmtrx16, &nmtrx32w);
  return AddFontFamily(&elf32w, &nmtrx32w, nFontType,
          (LPCHOOSEFONTW)lpcf->lpTemplateName, hwnd,NULL);
138 139 140 141 142 143 144 145 146 147 148
}

/***********************************************************************
 *                 FontStyleEnumProc                     (COMMDLG.18)
 */
INT16 WINAPI FontStyleEnumProc16( SEGPTR logfont, SEGPTR metrics,
                                  UINT16 nFontType, LPARAM lParam )
{
  HWND hcmb2=HWND_32(LOWORD(lParam));
  HWND hcmb3=HWND_32(HIWORD(lParam));
  HWND hDlg=GetParent(hcmb3);
149
  LPCHOOSEFONT16 lpcf;
150
  LOGFONT16 *lplf = MapSL(logfont);
151
  TEXTMETRIC16 *lpmtrx16 = MapSL(metrics);
Jacek Caban's avatar
Jacek Caban committed
152 153
  ENUMLOGFONTEXW elf32w;
  NEWTEXTMETRICEXW nmtrx32w;
154 155

  lpcf = (LPCHOOSEFONT16)GetPropW(hDlg, strWineFontData16);
Jacek Caban's avatar
Jacek Caban committed
156 157 158 159
  FONT_LogFont16To32W(lplf, &(elf32w.elfLogFont));
  FONT_Metrics16To32W(lpmtrx16, &nmtrx32w);
  return AddFontStyle(&elf32w, &nmtrx32w, nFontType,
          (LPCHOOSEFONTW)lpcf->lpTemplateName, hcmb2, hcmb3, hDlg, TRUE);
160 161 162 163 164 165 166 167
}

/***********************************************************************
 *                        ChooseFont   (COMMDLG.15)
 */
BOOL16 WINAPI ChooseFont16(LPCHOOSEFONT16 lpChFont)
{
    HINSTANCE16 hInst;
168
    HANDLE16 hDlgTmpl16 = 0;
169 170
    HGLOBAL16 hGlobal16 = 0;
    BOOL16 bRet = FALSE;
171
    LPVOID template;
172
    FARPROC16 ptr;
Jacek Caban's avatar
Jacek Caban committed
173 174
    CHOOSEFONTW cf32w;
    LOGFONTW lf32w;
175 176 177 178
    LOGFONT16 *font16;
    SEGPTR lpTemplateName;

    TRACE("ChooseFont\n");
179

180 181
    if (!lpChFont) return FALSE;

182 183 184
    cf32w.lpLogFont=&lf32w;
    CFn_CHOOSEFONT16to32W(lpChFont, &cf32w);

185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
    if (TRACE_ON(commdlg))
	_dump_cf_flags(lpChFont->Flags);

    if (lpChFont->Flags & CF_ENABLETEMPLATEHANDLE)
    {
        if (!(template = LockResource16( lpChFont->hInstance )))
        {
            COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
            return FALSE;
        }
    }
    else if (lpChFont->Flags & CF_ENABLETEMPLATE)
    {
        HANDLE16 hResInfo;
        if (!(hResInfo = FindResource16( lpChFont->hInstance,
                                         MapSL(lpChFont->lpTemplateName),
201
                                         (LPSTR)RT_DIALOG)))
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
        {
            COMDLG32_SetCommDlgExtendedError(CDERR_FINDRESFAILURE);
            return FALSE;
        }
        if (!(hDlgTmpl16 = LoadResource16( lpChFont->hInstance, hResInfo )) ||
            !(template = LockResource16( hDlgTmpl16 )))
        {
            COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
            return FALSE;
        }
    }
    else
    {
	HRSRC hResInfo;
	HGLOBAL hDlgTmpl32;
        LPCVOID template32;
        DWORD size;
219
        if (!(hResInfo = FindResourceA(COMDLG32_hInstance, "CHOOSE_FONT", (LPSTR)RT_DIALOG)))
220 221 222 223 224 225 226 227 228 229
        {
            COMDLG32_SetCommDlgExtendedError(CDERR_FINDRESFAILURE);
            return FALSE;
        }
        if (!(hDlgTmpl32 = LoadResource(COMDLG32_hInstance, hResInfo)) ||
            !(template32 = LockResource(hDlgTmpl32)))
        {
            COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
            return FALSE;
        }
230
        size = SizeofResource(COMDLG32_hInstance, hResInfo);
231 232 233 234
        hGlobal16 = GlobalAlloc16(0, size);
        if (!hGlobal16)
        {
            COMDLG32_SetCommDlgExtendedError(CDERR_MEMALLOCFAILURE);
235
            ERR("alloc failure for %d bytes\n", size);
236 237 238 239 240 241 242 243 244 245
            return FALSE;
        }
        template = GlobalLock16(hGlobal16);
        if (!template)
        {
            COMDLG32_SetCommDlgExtendedError(CDERR_MEMLOCKFAILURE);
            ERR("global lock failure for %x handle\n", hGlobal16);
            GlobalFree16(hGlobal16);
            return FALSE;
        }
246
        ConvertDialog32To16(template32, size, template);
247 248 249 250 251 252
        hDlgTmpl16 = hGlobal16;

    }

    /* lpTemplateName is not used in the dialog */
    lpTemplateName=lpChFont->lpTemplateName;
Jacek Caban's avatar
Jacek Caban committed
253
    lpChFont->lpTemplateName=(SEGPTR)&cf32w;
254 255

    ptr = GetProcAddress16(GetModuleHandle16("COMMDLG"), (LPCSTR) 16);
256
    hInst = GetWindowLongPtrA(HWND_32(lpChFont->hwndOwner), GWLP_HINSTANCE);
257 258 259 260 261 262 263 264 265
    bRet = DialogBoxIndirectParam16(hInst, hDlgTmpl16, lpChFont->hwndOwner,
                     (DLGPROC16) ptr, (DWORD)lpChFont);
    if (hGlobal16)
    {
        GlobalUnlock16(hGlobal16);
        GlobalFree16(hGlobal16);
    }
    lpChFont->lpTemplateName=lpTemplateName;

Jacek Caban's avatar
Jacek Caban committed
266 267 268 269 270
    lpChFont->iPointSize = cf32w.iPointSize;
    lpChFont->Flags = cf32w.Flags;
    lpChFont->rgbColors = cf32w.rgbColors;
    lpChFont->lCustData = cf32w.lCustData;
    lpChFont->nFontType = cf32w.nFontType;
271 272

    font16 = MapSL(lpChFont->lpLogFont);
Jacek Caban's avatar
Jacek Caban committed
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
    font16->lfHeight = cf32w.lpLogFont->lfHeight;
    font16->lfWidth = cf32w.lpLogFont->lfWidth;
    font16->lfEscapement = cf32w.lpLogFont->lfEscapement;
    font16->lfOrientation = cf32w.lpLogFont->lfOrientation;
    font16->lfWeight = cf32w.lpLogFont->lfWeight;
    font16->lfItalic = cf32w.lpLogFont->lfItalic;
    font16->lfUnderline = cf32w.lpLogFont->lfUnderline;
    font16->lfStrikeOut = cf32w.lpLogFont->lfStrikeOut;
    font16->lfCharSet = cf32w.lpLogFont->lfCharSet;
    font16->lfOutPrecision = cf32w.lpLogFont->lfOutPrecision;
    font16->lfClipPrecision = cf32w.lpLogFont->lfClipPrecision;
    font16->lfQuality = cf32w.lpLogFont->lfQuality;
    font16->lfPitchAndFamily = cf32w.lpLogFont->lfPitchAndFamily;
    WideCharToMultiByte(CP_ACP, 0, cf32w.lpLogFont->lfFaceName,
                          LF_FACESIZE, font16->lfFaceName, LF_FACESIZE, 0, 0);

289 290
    HeapFree(GetProcessHeap(), 0, (LPBYTE)cf32w.lpTemplateName);
    HeapFree(GetProcessHeap(), 0, cf32w.lpszStyle);
Jacek Caban's avatar
Jacek Caban committed
291

292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
    return bRet;
}

/***********************************************************************
 *           FormatCharDlgProc   (COMMDLG.16)
             FIXME: 1. some strings are "hardcoded", but it's better load from sysres
                    2. some CF_.. flags are not supported
                    3. some TType extensions
 */
BOOL16 CALLBACK FormatCharDlgProc16(HWND16 hDlg16, UINT16 message,
				   WPARAM16 wParam, LPARAM lParam)
{
  HWND hDlg = HWND_32(hDlg16);
  LPCHOOSEFONT16 lpcf;
  BOOL16 res=0;
  if (message!=WM_INITDIALOG)
  {
309 310 311 312 313 314 315
      lpcf = (LPCHOOSEFONT16)GetPropW(hDlg, strWineFontData16);
      if (!lpcf)
          return FALSE;
      if (CFn_HookCallChk(lpcf))
          res=CallWindowProc16((WNDPROC16)lpcf->lpfnHook,hDlg16,message,wParam,lParam);
      if (res)
          return res;
316 317 318 319
  }
  else
  {
    lpcf=(LPCHOOSEFONT16)lParam;
Jacek Caban's avatar
Jacek Caban committed
320
    if (!CFn_WMInitDialog(hDlg, wParam, lParam, (LPCHOOSEFONTW)lpcf->lpTemplateName))
321 322 323 324
    {
      TRACE("CFn_WMInitDialog returned FALSE\n");
      return FALSE;
    }
325
    SetPropW(hDlg, strWineFontData16, (HANDLE)lParam);
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
    if (CFn_HookCallChk(lpcf))
      return CallWindowProc16((WNDPROC16)lpcf->lpfnHook,hDlg16,WM_INITDIALOG,wParam,lParam);
  }
  switch (message)
    {
    case WM_MEASUREITEM:
        {
            MEASUREITEMSTRUCT16* mis16 = MapSL(lParam);
            MEASUREITEMSTRUCT mis;
            mis.CtlType    = mis16->CtlType;
            mis.CtlID      = mis16->CtlID;
            mis.itemID     = mis16->itemID;
            mis.itemWidth  = mis16->itemWidth;
            mis.itemHeight = mis16->itemHeight;
            mis.itemData   = mis16->itemData;
            res = CFn_WMMeasureItem(hDlg, wParam, (LPARAM)&mis);
            mis16->itemWidth  = (UINT16)mis.itemWidth;
            mis16->itemHeight = (UINT16)mis.itemHeight;
        }
        break;
    case WM_DRAWITEM:
        {
            DRAWITEMSTRUCT16* dis16 = MapSL(lParam);
            DRAWITEMSTRUCT dis;
350 351 352 353 354 355 356 357 358 359 360 361
            dis.CtlType       = dis16->CtlType;
            dis.CtlID         = dis16->CtlID;
            dis.itemID        = dis16->itemID;
            dis.itemAction    = dis16->itemAction;
            dis.itemState     = dis16->itemState;
            dis.hwndItem      = HWND_32(dis16->hwndItem);
            dis.hDC           = HDC_32(dis16->hDC);
            dis.itemData      = dis16->itemData;
            dis.rcItem.left   = dis16->rcItem.left;
            dis.rcItem.top    = dis16->rcItem.top;
            dis.rcItem.right  = dis16->rcItem.right;
            dis.rcItem.bottom = dis16->rcItem.bottom;
362 363 364 365
            res = CFn_WMDrawItem(hDlg, wParam, (LPARAM)&dis);
        }
        break;
    case WM_COMMAND:
366
        res=CFn_WMCommand(hDlg, MAKEWPARAM( wParam, HIWORD(lParam) ), LOWORD(lParam),
Jacek Caban's avatar
Jacek Caban committed
367
                          (LPCHOOSEFONTW)lpcf->lpTemplateName);
368 369
        break;
    case WM_DESTROY:
Jacek Caban's avatar
Jacek Caban committed
370
        return TRUE;
371 372 373 374
    case WM_CHOOSEFONT_GETLOGFONT:
        TRACE("WM_CHOOSEFONT_GETLOGFONT lParam=%08lX\n", lParam);
        FIXME("current logfont back to caller\n");
        break;
375
    case WM_PAINT:
Jacek Caban's avatar
Jacek Caban committed
376
        res= CFn_WMPaint(hDlg, wParam, lParam, (LPCHOOSEFONTW)lpcf->lpTemplateName);
377
        break;
378 379 380
    }
  return res;
}