1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxFontDialog class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "fontdlg.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
27 #include "wx/dialog.h"
30 #include "wx/fontdlg.h"
34 #if !defined(__WIN32__) || defined(__SALFORDC__) || defined(__WXWINE__)
38 #include "wx/msw/private.h"
39 #include "wx/cmndata.h"
45 #define wxDIALOG_DEFAULT_X 300
46 #define wxDIALOG_DEFAULT_Y 300
48 #if !USE_SHARED_LIBRARY
49 IMPLEMENT_DYNAMIC_CLASS(wxFontDialog
, wxDialog
)
57 wxFontDialog::wxFontDialog(void)
59 m_dialogParent
= NULL
;
62 wxFontDialog::wxFontDialog(wxWindow
*parent
, wxFontData
*data
)
67 bool wxFontDialog::Create(wxWindow
*parent
, wxFontData
*data
)
69 m_dialogParent
= parent
;
76 int wxFontDialog::ShowModal(void)
78 CHOOSEFONT chooseFontStruct
;
81 DWORD flags
= CF_TTONLY
| CF_SCREENFONTS
| CF_NOSIMULATIONS
;
83 memset(&chooseFontStruct
, 0, sizeof(CHOOSEFONT
));
85 chooseFontStruct
.lStructSize
= sizeof(CHOOSEFONT
);
86 chooseFontStruct
.hwndOwner
= (HWND
) (m_dialogParent
? (HWND
) m_dialogParent
->GetHWND() : NULL
);
87 chooseFontStruct
.lpLogFont
= &logFont
;
89 if (m_fontData
.initialFont
.Ok())
91 flags
|= CF_INITTOLOGFONTSTRUCT
;
92 wxFillLogFont(&logFont
, & m_fontData
.initialFont
);
95 chooseFontStruct
.iPointSize
= 0;
96 chooseFontStruct
.rgbColors
= RGB((BYTE
)m_fontData
.fontColour
.Red(), (BYTE
)m_fontData
.fontColour
.Green(), (BYTE
)m_fontData
.fontColour
.Blue());
98 if (!m_fontData
.GetAllowSymbols())
100 if (m_fontData
.GetEnableEffects())
102 if (m_fontData
.GetShowHelp())
103 flags
|= CF_SHOWHELP
;
104 if (!(m_fontData
.minSize
== 0 && m_fontData
.maxSize
== 0))
106 chooseFontStruct
.nSizeMin
= m_fontData
.minSize
;
107 chooseFontStruct
.nSizeMax
= m_fontData
.maxSize
;
108 flags
|= CF_LIMITSIZE
;
111 chooseFontStruct
.Flags
= flags
;
112 chooseFontStruct
.nFontType
= SCREEN_FONTTYPE
;
113 bool success
= (ChooseFont(&(chooseFontStruct
)) != 0);
118 m_fontData
.fontColour
.Set(GetRValue(chooseFontStruct
.rgbColors
), GetGValue(chooseFontStruct
.rgbColors
),
119 GetBValue(chooseFontStruct
.rgbColors
));
120 m_fontData
.chosenFont
= wxCreateFontFromLogFont(&logFont
);
123 return success
? wxID_OK
: wxID_CANCEL
;
126 void wxFillLogFont(LOGFONT
*logFont
, wxFont
*font
)
131 wxString
ff_face("");
133 switch (font
->GetFamily())
135 case wxSCRIPT
: ff_family
= FF_SCRIPT
;
138 case wxDECORATIVE
: ff_family
= FF_DECORATIVE
;
140 case wxROMAN
: ff_family
= FF_ROMAN
;
141 ff_face
= "Times New Roman" ;
144 case wxMODERN
: ff_family
= FF_MODERN
;
145 ff_face
= "Courier New" ;
147 case wxSWISS
: ff_family
= FF_SWISS
;
151 default: ff_family
= FF_SWISS
;
152 ff_face
= "MS Sans Serif" ;
155 if (font
->GetStyle() == wxITALIC
|| font
->GetStyle() == wxSLANT
)
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
)
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
);
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.
177 #if wxFONT_SIZE_COMPATIBILITY
178 // Incorrect, but compatible with old wxWindows behaviour
179 int nHeight
= (font
->GetPointSize()*ppInch
/72);
181 // Correct for Windows compatibility
182 int nHeight
= - (font
->GetPointSize()*ppInch
/72);
185 bool ff_underline
= font
->GetUnderlined();
187 ff_face
= font
->GetFaceName();
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
);
205 wxFont
wxCreateFontFromLogFont(LOGFONT
*logFont
)
207 int fontFamily
= wxSWISS
;
208 int fontStyle
= wxNORMAL
;
209 int fontWeight
= wxNORMAL
;
211 bool fontUnderline
= FALSE
;
212 wxChar
*fontFace
= NULL
;
214 int lfFamily
= logFont
->lfPitchAndFamily
;
215 if (lfFamily
& FIXED_PITCH
)
216 lfFamily
-= FIXED_PITCH
;
217 if (lfFamily
& VARIABLE_PITCH
)
218 lfFamily
-= VARIABLE_PITCH
;
223 fontFamily
= wxROMAN
;
226 fontFamily
= wxSWISS
;
229 fontFamily
= wxSCRIPT
;
232 fontFamily
= wxMODERN
;
235 fontFamily
= wxDECORATIVE
;
238 fontFamily
= wxSWISS
;
241 switch (logFont
->lfWeight
)
244 fontWeight
= wxLIGHT
;
247 fontWeight
= wxNORMAL
;
253 fontWeight
= wxNORMAL
;
256 if (logFont
->lfItalic
)
257 fontStyle
= wxITALIC
;
259 fontStyle
= wxNORMAL
;
261 if (logFont
->lfUnderline
)
262 fontUnderline
= TRUE
;
264 if (logFont
->lfFaceName
)
265 fontFace
= logFont
->lfFaceName
;
267 wxFontEncoding fontEncoding
;
268 switch ( logFont
->lfCharSet
)
271 wxFAIL_MSG(wxT("unsupported charset"));
275 fontEncoding
= wxFONTENCODING_ISO8859_1
;
278 case EASTEUROPE_CHARSET
:
279 fontEncoding
= wxFONTENCODING_ISO8859_2
;
283 fontEncoding
= wxFONTENCODING_ISO8859_4
;
286 case RUSSIAN_CHARSET
:
287 fontEncoding
= wxFONTENCODING_CP1251
;
291 fontEncoding
= wxFONTENCODING_ISO8859_6
;
295 fontEncoding
= wxFONTENCODING_ISO8859_7
;
299 fontEncoding
= wxFONTENCODING_ISO8859_8
;
302 case TURKISH_CHARSET
:
303 fontEncoding
= wxFONTENCODING_ISO8859_9
;
307 fontEncoding
= wxFONTENCODING_ISO8859_11
;
312 fontEncoding
= wxFONTENCODING_CP437
;
316 HDC dc2
= ::GetDC(NULL
);
318 if ( logFont
->lfHeight
< 0 )
319 logFont
->lfHeight
= - logFont
->lfHeight
;
320 fontPoints
= abs(72*logFont
->lfHeight
/GetDeviceCaps(dc2
, LOGPIXELSY
));
321 ::ReleaseDC(NULL
, dc2
);
323 return wxFont(fontPoints
, fontFamily
, fontStyle
,
324 fontWeight
, fontUnderline
, fontFace
,