]> git.saurik.com Git - wxWidgets.git/blame - src/msw/fontdlg.cpp
1. wxFontMapper starts to materialise
[wxWidgets.git] / src / msw / fontdlg.cpp
CommitLineData
2bda0e17
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: fontdlg.cpp
3// Purpose: wxFontDialog class
4// Author: Julian Smart
5// Modified by:
6// Created: 01/02/97
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart and Markus Holzem
a1d58ddc 9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation "fontdlg.h"
14#endif
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
20#pragma hdrstop
21#endif
22
23#ifndef WX_PRECOMP
24#include <stdio.h>
25#include "wx/defs.h"
26#include "wx/utils.h"
27#include "wx/dialog.h"
28#endif
29
30#include "wx/fontdlg.h"
31
32#include <windows.h>
33
4286a5b5 34#if !defined(__WIN32__) || defined(__SALFORDC__) || defined(__WXWINE__)
2bda0e17
KB
35#include <commdlg.h>
36#endif
37
38#include "wx/msw/private.h"
39#include "wx/cmndata.h"
40
41#include <math.h>
42#include <stdlib.h>
43#include <string.h>
44
45#define wxDIALOG_DEFAULT_X 300
46#define wxDIALOG_DEFAULT_Y 300
47
48#if !USE_SHARED_LIBRARY
49IMPLEMENT_DYNAMIC_CLASS(wxFontDialog, wxDialog)
50#endif
51
52/*
53 * wxFontDialog
54 */
55
56
57wxFontDialog::wxFontDialog(void)
58{
bbcdf8bc 59 m_dialogParent = NULL;
2bda0e17
KB
60}
61
62wxFontDialog::wxFontDialog(wxWindow *parent, wxFontData *data)
63{
64 Create(parent, data);
65}
66
67bool wxFontDialog::Create(wxWindow *parent, wxFontData *data)
68{
bbcdf8bc
JS
69 m_dialogParent = parent;
70
2bda0e17 71 if (data)
bbcdf8bc 72 m_fontData = *data;
2bda0e17
KB
73 return TRUE;
74}
75
76int wxFontDialog::ShowModal(void)
77{
78 CHOOSEFONT chooseFontStruct;
79 LOGFONT logFont;
80
81 DWORD flags = CF_TTONLY | CF_SCREENFONTS | CF_NOSIMULATIONS;
82
83 memset(&chooseFontStruct, 0, sizeof(CHOOSEFONT));
84
85 chooseFontStruct.lStructSize = sizeof(CHOOSEFONT);
bbcdf8bc 86 chooseFontStruct.hwndOwner = (HWND) (m_dialogParent ? (HWND) m_dialogParent->GetHWND() : NULL);
2bda0e17
KB
87 chooseFontStruct.lpLogFont = &logFont;
88
bbcdf8bc 89 if (m_fontData.initialFont.Ok())
2bda0e17
KB
90 {
91 flags |= CF_INITTOLOGFONTSTRUCT;
bbcdf8bc 92 wxFillLogFont(&logFont, & m_fontData.initialFont);
2bda0e17
KB
93 }
94
95 chooseFontStruct.iPointSize = 0;
bbcdf8bc 96 chooseFontStruct.rgbColors = RGB((BYTE)m_fontData.fontColour.Red(), (BYTE)m_fontData.fontColour.Green(), (BYTE)m_fontData.fontColour.Blue());
2bda0e17 97
bbcdf8bc 98 if (!m_fontData.GetAllowSymbols())
2bda0e17 99 flags |= CF_ANSIONLY;
bbcdf8bc 100 if (m_fontData.GetEnableEffects())
2bda0e17 101 flags |= CF_EFFECTS;
bbcdf8bc 102 if (m_fontData.GetShowHelp())
2bda0e17 103 flags |= CF_SHOWHELP;
bbcdf8bc 104 if (!(m_fontData.minSize == 0 && m_fontData.maxSize == 0))
2bda0e17 105 {
bbcdf8bc
JS
106 chooseFontStruct.nSizeMin = m_fontData.minSize;
107 chooseFontStruct.nSizeMax = m_fontData.maxSize;
2bda0e17
KB
108 flags |= CF_LIMITSIZE;
109 }
110
111 chooseFontStruct.Flags = flags;
112 chooseFontStruct.nFontType = SCREEN_FONTTYPE;
113 bool success = (ChooseFont(&(chooseFontStruct)) != 0);
114
115 // Restore values
116 if (success)
117 {
bbcdf8bc 118 m_fontData.fontColour.Set(GetRValue(chooseFontStruct.rgbColors), GetGValue(chooseFontStruct.rgbColors),
2bda0e17 119 GetBValue(chooseFontStruct.rgbColors));
bbcdf8bc 120 m_fontData.chosenFont = wxCreateFontFromLogFont(&logFont);
2bda0e17
KB
121 }
122
123 return success ? wxID_OK : wxID_CANCEL;
124}
125
126void wxFillLogFont(LOGFONT *logFont, wxFont *font)
127{
128 BYTE ff_italic;
129 int ff_weight = 0;
130 int ff_family = 0;
131 wxString ff_face("");
132
133 switch (font->GetFamily())
134 {
135 case wxSCRIPT: ff_family = FF_SCRIPT ;
136 ff_face = "Script" ;
137 break ;
138 case wxDECORATIVE: ff_family = FF_DECORATIVE;
139 break;
140 case wxROMAN: ff_family = FF_ROMAN;
141 ff_face = "Times New Roman" ;
142 break;
143 case wxTELETYPE:
144 case wxMODERN: ff_family = FF_MODERN;
145 ff_face = "Courier New" ;
146 break;
147 case wxSWISS: ff_family = FF_SWISS;
148 ff_face = "Arial";
149 break;
150 case wxDEFAULT:
151 default: ff_family = FF_SWISS;
a1d58ddc 152 ff_face = "MS Sans Serif" ;
2bda0e17
KB
153 }
154
155 if (font->GetStyle() == wxITALIC || font->GetStyle() == wxSLANT)
156 ff_italic = 1;
157 else
158 ff_italic = 0;
159
160 if (font->GetWeight() == wxNORMAL)
161 ff_weight = FW_NORMAL;
162 else if (font->GetWeight() == wxLIGHT)
163 ff_weight = FW_LIGHT;
164 else if (font->GetWeight() == wxBOLD)
165 ff_weight = FW_BOLD;
166
167 // Have to get screen DC Caps, because a metafile will return 0.
168 HDC dc2 = ::GetDC(NULL);
169 int ppInch = ::GetDeviceCaps(dc2, LOGPIXELSY);
170 ::ReleaseDC(NULL, dc2);
171
172 // New behaviour: apparently ppInch varies according to
173 // Large/Small Fonts setting in Windows. This messes
174 // up fonts. So, set ppInch to a constant 96 dpi.
175 ppInch = 96;
176
1f112209 177#if wxFONT_SIZE_COMPATIBILITY
2bda0e17
KB
178 // Incorrect, but compatible with old wxWindows behaviour
179 int nHeight = (font->GetPointSize()*ppInch/72);
180#else
181 // Correct for Windows compatibility
182 int nHeight = - (font->GetPointSize()*ppInch/72);
183#endif
184
185 bool ff_underline = font->GetUnderlined();
186
187 ff_face = font->GetFaceName();
188
189 logFont->lfHeight = nHeight;
190 logFont->lfWidth = 0;
191 logFont->lfEscapement = 0;
192 logFont->lfOrientation = 0;
193 logFont->lfWeight = ff_weight;
194 logFont->lfItalic = ff_italic;
195 logFont->lfUnderline = (BYTE)ff_underline;
196 logFont->lfStrikeOut = 0;
a1d58ddc 197 logFont->lfCharSet = wxCharsetFromEncoding(font->GetEncoding());
2bda0e17
KB
198 logFont->lfOutPrecision = OUT_DEFAULT_PRECIS;
199 logFont->lfClipPrecision = CLIP_DEFAULT_PRECIS;
200 logFont->lfQuality = PROOF_QUALITY;
201 logFont->lfPitchAndFamily = DEFAULT_PITCH | ff_family;
837e5743 202 wxStrcpy(logFont->lfFaceName, ff_face);
2bda0e17
KB
203}
204
a1d58ddc 205wxFont wxCreateFontFromLogFont(LOGFONT *logFont)
2bda0e17 206{
a1d58ddc
VZ
207 int fontFamily = wxSWISS;
208 int fontStyle = wxNORMAL;
209 int fontWeight = wxNORMAL;
210 int fontPoints = 10;
211 bool fontUnderline = FALSE;
212 wxChar *fontFace = NULL;
213
214 int lfFamily = logFont->lfPitchAndFamily;
215 if (lfFamily & FIXED_PITCH)
216 lfFamily -= FIXED_PITCH;
217 if (lfFamily & VARIABLE_PITCH)
218 lfFamily -= VARIABLE_PITCH;
219
220 switch (lfFamily)
221 {
222 case FF_ROMAN:
223 fontFamily = wxROMAN;
224 break;
225 case FF_SWISS:
226 fontFamily = wxSWISS;
227 break;
228 case FF_SCRIPT:
229 fontFamily = wxSCRIPT;
230 break;
231 case FF_MODERN:
232 fontFamily = wxMODERN;
233 break;
234 case FF_DECORATIVE:
235 fontFamily = wxDECORATIVE;
236 break;
237 default:
238 fontFamily = wxSWISS;
239 break;
240 }
241 switch (logFont->lfWeight)
242 {
243 case FW_LIGHT:
244 fontWeight = wxLIGHT;
245 break;
246 case FW_NORMAL:
247 fontWeight = wxNORMAL;
248 break;
249 case FW_BOLD:
250 fontWeight = wxBOLD;
251 break;
252 default:
253 fontWeight = wxNORMAL;
254 break;
255 }
256 if (logFont->lfItalic)
257 fontStyle = wxITALIC;
258 else
259 fontStyle = wxNORMAL;
260
261 if (logFont->lfUnderline)
262 fontUnderline = TRUE;
263
264 if (logFont->lfFaceName)
265 fontFace = logFont->lfFaceName;
266
267 wxFontEncoding fontEncoding;
268 switch ( logFont->lfCharSet )
269 {
270 default:
271 wxFAIL_MSG(wxT("unsupported charset"));
272 // fall through
273
274 case ANSI_CHARSET:
275 fontEncoding = wxFONTENCODING_ISO8859_1;
276 break;
277
278 case EASTEUROPE_CHARSET:
279 fontEncoding = wxFONTENCODING_ISO8859_2;
280 break;
281
282 case BALTIC_CHARSET:
283 fontEncoding = wxFONTENCODING_ISO8859_4;
284 break;
285
286 case RUSSIAN_CHARSET:
287 fontEncoding = wxFONTENCODING_CP1251;
288 break;
289
290 case ARABIC_CHARSET:
291 fontEncoding = wxFONTENCODING_ISO8859_6;
292 break;
293
294 case GREEK_CHARSET:
295 fontEncoding = wxFONTENCODING_ISO8859_7;
296 break;
297
298 case HEBREW_CHARSET:
299 fontEncoding = wxFONTENCODING_ISO8859_8;
300 break;
301
302 case TURKISH_CHARSET:
303 fontEncoding = wxFONTENCODING_ISO8859_9;
304 break;
305
306 case THAI_CHARSET:
307 fontEncoding = wxFONTENCODING_ISO8859_11;
308 break;
309
310
311 case OEM_CHARSET:
312 fontEncoding = wxFONTENCODING_CP437;
313 break;
314 }
315
316 HDC dc2 = ::GetDC(NULL);
317
318 if ( logFont->lfHeight < 0 )
319 logFont->lfHeight = - logFont->lfHeight;
320 fontPoints = abs(72*logFont->lfHeight/GetDeviceCaps(dc2, LOGPIXELSY));
321 ::ReleaseDC(NULL, dc2);
322
323 return wxFont(fontPoints, fontFamily, fontStyle,
324 fontWeight, fontUnderline, fontFace,
325 fontEncoding);
2bda0e17
KB
326}
327
328