]> git.saurik.com Git - wxWidgets.git/blob - src/msw/fontdlg.cpp
added operator[](unsigned int) const -- testing it now on Linux/axp,
[wxWidgets.git] / src / msw / fontdlg.cpp
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
9 // Licence: wxWindows licence
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
34 #if !defined(__WIN32__) || defined(__SALFORDC__) || defined(__WXWINE__)
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
49 IMPLEMENT_DYNAMIC_CLASS(wxFontDialog, wxDialog)
50 #endif
51
52 /*
53 * wxFontDialog
54 */
55
56
57 wxFontDialog::wxFontDialog(void)
58 {
59 m_dialogParent = NULL;
60 }
61
62 wxFontDialog::wxFontDialog(wxWindow *parent, wxFontData *data)
63 {
64 Create(parent, data);
65 }
66
67 bool wxFontDialog::Create(wxWindow *parent, wxFontData *data)
68 {
69 m_dialogParent = parent;
70
71 if (data)
72 m_fontData = *data;
73 return TRUE;
74 }
75
76 int 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);
86 chooseFontStruct.hwndOwner = (HWND) (m_dialogParent ? (HWND) m_dialogParent->GetHWND() : NULL);
87 chooseFontStruct.lpLogFont = &logFont;
88
89 if (m_fontData.initialFont.Ok())
90 {
91 flags |= CF_INITTOLOGFONTSTRUCT;
92 wxFillLogFont(&logFont, & m_fontData.initialFont);
93 }
94
95 chooseFontStruct.iPointSize = 0;
96 chooseFontStruct.rgbColors = RGB((BYTE)m_fontData.fontColour.Red(), (BYTE)m_fontData.fontColour.Green(), (BYTE)m_fontData.fontColour.Blue());
97
98 if (!m_fontData.GetAllowSymbols())
99 flags |= CF_ANSIONLY;
100 if (m_fontData.GetEnableEffects())
101 flags |= CF_EFFECTS;
102 if (m_fontData.GetShowHelp())
103 flags |= CF_SHOWHELP;
104 if (!(m_fontData.minSize == 0 && m_fontData.maxSize == 0))
105 {
106 chooseFontStruct.nSizeMin = m_fontData.minSize;
107 chooseFontStruct.nSizeMax = m_fontData.maxSize;
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 {
118 m_fontData.fontColour.Set(GetRValue(chooseFontStruct.rgbColors), GetGValue(chooseFontStruct.rgbColors),
119 GetBValue(chooseFontStruct.rgbColors));
120 m_fontData.chosenFont = wxCreateFontFromLogFont(&logFont);
121 }
122
123 return success ? wxID_OK : wxID_CANCEL;
124 }
125
126 void 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;
152 ff_face = "MS Sans Serif" ;
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
177 #if wxFONT_SIZE_COMPATIBILITY
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;
197 logFont->lfCharSet = wxCharsetFromEncoding(font->GetEncoding());
198 logFont->lfOutPrecision = OUT_DEFAULT_PRECIS;
199 logFont->lfClipPrecision = CLIP_DEFAULT_PRECIS;
200 logFont->lfQuality = PROOF_QUALITY;
201 logFont->lfPitchAndFamily = DEFAULT_PITCH | ff_family;
202 wxStrcpy(logFont->lfFaceName, ff_face);
203 }
204
205 wxFont wxCreateFontFromLogFont(LOGFONT *logFont)
206 {
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);
326 }
327
328