1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/fontdlgg.cpp
3 // Purpose: Generic font dialog
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
19 #if wxUSE_FONTDLG && (!defined(__WXGTK__) || defined(__WXGPE__) || defined(__WXUNIVERSAL__))
25 #include "wx/dialog.h"
26 #include "wx/listbox.h"
27 #include "wx/button.h"
28 #include "wx/stattext.h"
29 #include "wx/layout.h"
30 #include "wx/dcclient.h"
31 #include "wx/choice.h"
32 #include "wx/checkbox.h"
34 #include "wx/settings.h"
35 #include "wx/cmndata.h"
42 #include "wx/fontdlg.h"
43 #include "wx/generic/fontdlgg.h"
45 #if USE_SPINCTRL_FOR_POINT_SIZE
46 #include "wx/spinctrl.h"
49 //-----------------------------------------------------------------------------
50 // helper class - wxFontPreviewer
51 //-----------------------------------------------------------------------------
53 class WXDLLEXPORT wxFontPreviewer
: public wxWindow
56 wxFontPreviewer(wxWindow
*parent
, const wxSize
& sz
= wxDefaultSize
) : wxWindow(parent
, wxID_ANY
, wxDefaultPosition
, sz
)
61 void OnPaint(wxPaintEvent
& event
);
65 BEGIN_EVENT_TABLE(wxFontPreviewer
, wxWindow
)
66 EVT_PAINT(wxFontPreviewer::OnPaint
)
69 void wxFontPreviewer::OnPaint(wxPaintEvent
& WXUNUSED(event
))
73 wxSize size
= GetSize();
74 wxFont font
= GetFont();
76 dc
.SetPen(*wxBLACK_PEN
);
77 dc
.SetBrush(*wxWHITE_BRUSH
);
78 dc
.DrawRectangle(0, 0, size
.x
, size
.y
);
83 dc
.SetTextForeground(GetForegroundColour());
84 dc
.SetClippingRegion(2, 2, size
.x
-4, size
.y
-4);
85 dc
.DrawText(_("ABCDEFGabcdefg12345"),
86 10, (size
.y
- dc
.GetTextExtent(wxT("X")).y
)/2);
87 dc
.DestroyClippingRegion();
91 //-----------------------------------------------------------------------------
92 // wxGenericFontDialog
93 //-----------------------------------------------------------------------------
95 IMPLEMENT_DYNAMIC_CLASS(wxGenericFontDialog
, wxDialog
)
97 BEGIN_EVENT_TABLE(wxGenericFontDialog
, wxDialog
)
98 EVT_CHECKBOX(wxID_FONT_UNDERLINE
, wxGenericFontDialog::OnChangeFont
)
99 EVT_CHOICE(wxID_FONT_STYLE
, wxGenericFontDialog::OnChangeFont
)
100 EVT_CHOICE(wxID_FONT_WEIGHT
, wxGenericFontDialog::OnChangeFont
)
101 EVT_CHOICE(wxID_FONT_FAMILY
, wxGenericFontDialog::OnChangeFont
)
102 EVT_CHOICE(wxID_FONT_COLOUR
, wxGenericFontDialog::OnChangeFont
)
103 #if USE_SPINCTRL_FOR_POINT_SIZE
104 EVT_SPINCTRL(wxID_FONT_SIZE
, wxGenericFontDialog::OnChangeSize
)
105 EVT_TEXT(wxID_FONT_SIZE
, wxGenericFontDialog::OnChangeFont
)
107 EVT_CHOICE(wxID_FONT_SIZE
, wxGenericFontDialog::OnChangeFont
)
109 EVT_CLOSE(wxGenericFontDialog::OnCloseWindow
)
114 static wxString wxColourDialogNames
[NUM_COLS
]={wxT("ORANGE"),
120 wxT("MEDIUM VIOLET RED"),
125 wxT("MEDIUM SPRING GREEN"),
128 wxT("LIGHT STEEL BLUE"),
130 wxT("LIGHT MAGENTA"),
148 wxT("MEDIUM VIOLET RED"),
152 wxT("MEDIUM SEA GREEN"),
154 wxT("MIDNIGHT BLUE"),
160 wxT("MEDIUM FOREST GREEN"),
165 wxT("MEDIUM SLATE BLUE"),
170 * Generic wxFontDialog
173 void wxGenericFontDialog::Init()
180 wxGenericFontDialog::~wxGenericFontDialog()
184 void wxGenericFontDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
186 EndModal(wxID_CANCEL
);
189 bool wxGenericFontDialog::DoCreate(wxWindow
*parent
)
191 parent
= GetParentForModalDialog(parent
);
193 if ( !wxDialog::Create( parent
, wxID_ANY
, _T("Choose Font") ,
194 wxDefaultPosition
, wxDefaultSize
, wxDEFAULT_DIALOG_STYLE
,
197 wxFAIL_MSG( wxT("wxFontDialog creation failed") );
204 // sets initial font in preview area
210 int wxGenericFontDialog::ShowModal()
212 int ret
= wxDialog::ShowModal();
214 if (ret
!= wxID_CANCEL
)
216 m_fontData
.m_chosenFont
= m_dialogFont
;
222 // This should be application-settable
223 static bool ShowToolTips() { return false; }
225 void wxGenericFontDialog::CreateWidgets()
227 wxString
*families
= new wxString
[6],
228 *styles
= new wxString
[3],
229 *weights
= new wxString
[3];
230 families
[0] = _("Roman");
231 families
[1] = _("Decorative");
232 families
[2] = _("Modern");
233 families
[3] = _("Script");
234 families
[4] = _("Swiss" );
235 families
[5] = _("Teletype" );
236 styles
[0] = _("Normal");
237 styles
[1] = _("Italic");
238 styles
[2] = _("Slant");
239 weights
[0] = _("Normal");
240 weights
[1] = _("Light");
241 weights
[2] = _("Bold");
243 #if !USE_SPINCTRL_FOR_POINT_SIZE
244 wxString
*pointSizes
= new wxString
[40];
246 for ( i
= 0; i
< 40; i
++)
249 wxSprintf(buf
, wxT("%d"), i
+ 1);
256 bool is_pda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
260 noCols
= 2; noRows
= 3;
264 noCols
= 3; noRows
= 2;
267 wxBoxSizer
* itemBoxSizer2
= new wxBoxSizer(wxVERTICAL
);
268 this->SetSizer(itemBoxSizer2
);
269 this->SetAutoLayout(true);
271 wxBoxSizer
* itemBoxSizer3
= new wxBoxSizer(wxVERTICAL
);
272 itemBoxSizer2
->Add(itemBoxSizer3
, 1, wxGROW
|wxALL
, 5);
274 wxFlexGridSizer
* itemGridSizer4
= new wxFlexGridSizer(noRows
, noCols
, 0, 0);
275 itemBoxSizer3
->Add(itemGridSizer4
, 0, wxGROW
, 5);
277 wxBoxSizer
* itemBoxSizer5
= new wxBoxSizer(wxVERTICAL
);
278 itemGridSizer4
->Add(itemBoxSizer5
, 0, wxALIGN_CENTER_HORIZONTAL
|wxGROW
, 5);
279 wxStaticText
* itemStaticText6
= new wxStaticText( this, wxID_STATIC
, _("&Font family:"), wxDefaultPosition
, wxDefaultSize
, 0 );
280 itemBoxSizer5
->Add(itemStaticText6
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
, 5);
282 wxChoice
* itemChoice7
= new wxChoice( this, wxID_FONT_FAMILY
, wxDefaultPosition
, wxDefaultSize
, 5, families
, 0 );
283 itemChoice7
->SetHelpText(_("The font family."));
285 itemChoice7
->SetToolTip(_("The font family."));
286 itemBoxSizer5
->Add(itemChoice7
, 0, wxALIGN_LEFT
|wxALL
, 5);
288 wxBoxSizer
* itemBoxSizer8
= new wxBoxSizer(wxVERTICAL
);
289 itemGridSizer4
->Add(itemBoxSizer8
, 0, wxALIGN_CENTER_HORIZONTAL
|wxGROW
, 5);
290 wxStaticText
* itemStaticText9
= new wxStaticText( this, wxID_STATIC
, _("&Style:"), wxDefaultPosition
, wxDefaultSize
, 0 );
291 itemBoxSizer8
->Add(itemStaticText9
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
, 5);
293 wxChoice
* itemChoice10
= new wxChoice( this, wxID_FONT_STYLE
, wxDefaultPosition
, wxDefaultSize
, 3, styles
, 0 );
294 itemChoice10
->SetHelpText(_("The font style."));
296 itemChoice10
->SetToolTip(_("The font style."));
297 itemBoxSizer8
->Add(itemChoice10
, 0, wxALIGN_LEFT
|wxALL
, 5);
299 wxBoxSizer
* itemBoxSizer11
= new wxBoxSizer(wxVERTICAL
);
300 itemGridSizer4
->Add(itemBoxSizer11
, 0, wxALIGN_CENTER_HORIZONTAL
|wxGROW
, 5);
301 wxStaticText
* itemStaticText12
= new wxStaticText( this, wxID_STATIC
, _("&Weight:"), wxDefaultPosition
, wxDefaultSize
, 0 );
302 itemBoxSizer11
->Add(itemStaticText12
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
, 5);
304 wxChoice
* itemChoice13
= new wxChoice( this, wxID_FONT_WEIGHT
, wxDefaultPosition
, wxDefaultSize
, 3, weights
, 0 );
305 itemChoice13
->SetHelpText(_("The font weight."));
307 itemChoice13
->SetToolTip(_("The font weight."));
308 itemBoxSizer11
->Add(itemChoice13
, 0, wxALIGN_LEFT
|wxALL
, 5);
310 wxBoxSizer
* itemBoxSizer14
= new wxBoxSizer(wxVERTICAL
);
311 itemGridSizer4
->Add(itemBoxSizer14
, 0, wxALIGN_CENTER_HORIZONTAL
|wxGROW
, 5);
312 if (m_fontData
.GetEnableEffects())
314 wxStaticText
* itemStaticText15
= new wxStaticText( this, wxID_STATIC
, _("C&olour:"), wxDefaultPosition
, wxDefaultSize
, 0 );
315 itemBoxSizer14
->Add(itemStaticText15
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
, 5);
317 wxSize colourSize
= wxDefaultSize
;
321 wxChoice
* itemChoice16
= new wxChoice( this, wxID_FONT_COLOUR
, wxDefaultPosition
, colourSize
, NUM_COLS
, wxColourDialogNames
, 0 );
322 itemChoice16
->SetHelpText(_("The font colour."));
324 itemChoice16
->SetToolTip(_("The font colour."));
325 itemBoxSizer14
->Add(itemChoice16
, 0, wxALIGN_LEFT
|wxALL
, 5);
328 wxBoxSizer
* itemBoxSizer17
= new wxBoxSizer(wxVERTICAL
);
329 itemGridSizer4
->Add(itemBoxSizer17
, 0, wxALIGN_CENTER_HORIZONTAL
|wxGROW
, 5);
330 wxStaticText
* itemStaticText18
= new wxStaticText( this, wxID_STATIC
, _("&Point size:"), wxDefaultPosition
, wxDefaultSize
, 0 );
331 itemBoxSizer17
->Add(itemStaticText18
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
, 5);
333 #if USE_SPINCTRL_FOR_POINT_SIZE
334 wxSpinCtrl
* spinCtrl
= new wxSpinCtrl(this, wxID_FONT_SIZE
, wxT("12"), wxDefaultPosition
, wxSize(80, wxDefaultCoord
), wxSP_ARROW_KEYS
, 1, 500, 12);
335 spinCtrl
->SetHelpText(_("The font point size."));
337 spinCtrl
->SetToolTip(_("The font point size."));
339 itemBoxSizer17
->Add(spinCtrl
, 0, wxALIGN_LEFT
|wxALL
, 5);
341 wxChoice
* itemChoice19
= new wxChoice( this, wxID_FONT_SIZE
, wxDefaultPosition
, wxDefaultSize
, 40, pointSizes
, 0 );
342 itemChoice19
->SetHelpText(_("The font point size."));
344 itemChoice19
->SetToolTip(_("The font point size."));
345 itemBoxSizer17
->Add(itemChoice19
, 0, wxALIGN_LEFT
|wxALL
, 5);
348 if (m_fontData
.GetEnableEffects())
350 wxBoxSizer
* itemBoxSizer20
= new wxBoxSizer(wxVERTICAL
);
351 itemGridSizer4
->Add(itemBoxSizer20
, 0, wxALIGN_LEFT
|wxALIGN_CENTER_VERTICAL
, 5);
352 wxCheckBox
* itemCheckBox21
= new wxCheckBox( this, wxID_FONT_UNDERLINE
, _("&Underline"), wxDefaultPosition
, wxDefaultSize
, 0 );
353 itemCheckBox21
->SetValue(false);
354 itemCheckBox21
->SetHelpText(_("Whether the font is underlined."));
356 itemCheckBox21
->SetToolTip(_("Whether the font is underlined."));
357 itemBoxSizer20
->Add(itemCheckBox21
, 0, wxALIGN_LEFT
|wxALL
, 5);
361 itemBoxSizer3
->Add(5, 5, 0, wxALIGN_CENTER_HORIZONTAL
|wxALL
, 5);
363 wxStaticText
* itemStaticText23
= new wxStaticText( this, wxID_STATIC
, _("Preview:"), wxDefaultPosition
, wxDefaultSize
, 0 );
364 itemBoxSizer3
->Add(itemStaticText23
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
, 5);
366 wxFontPreviewer
* itemWindow24
= new wxFontPreviewer( this );
367 m_previewer
= itemWindow24
;
368 itemWindow24
->SetHelpText(_("Shows the font preview."));
370 itemWindow24
->SetToolTip(_("Shows the font preview."));
371 itemBoxSizer3
->Add(itemWindow24
, 1, wxGROW
|wxALL
, 5);
373 wxBoxSizer
* itemBoxSizer25
= new wxBoxSizer(wxHORIZONTAL
);
374 itemBoxSizer3
->Add(itemBoxSizer25
, 0, wxGROW
, 5);
375 itemBoxSizer25
->Add(5, 5, 1, wxGROW
|wxALL
, 5);
378 wxButton
* itemButton28
= new wxButton( this, wxID_CANCEL
, _("&Cancel"), wxDefaultPosition
, wxDefaultSize
, 0 );
380 itemButton28
->SetToolTip(_("Click to cancel the font selection."));
381 itemBoxSizer25
->Add(itemButton28
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
383 wxButton
* itemButton27
= new wxButton( this, wxID_OK
, _("&OK"), wxDefaultPosition
, wxDefaultSize
, 0 );
384 itemButton27
->SetDefault();
385 itemButton27
->SetHelpText(_("Click to confirm the font selection."));
387 itemButton27
->SetToolTip(_("Click to confirm the font selection."));
388 itemBoxSizer25
->Add(itemButton27
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
390 wxButton
* itemButton27
= new wxButton( this, wxID_OK
, _("&OK"), wxDefaultPosition
, wxDefaultSize
, 0 );
391 itemButton27
->SetDefault();
392 itemButton27
->SetHelpText(_("Click to confirm the font selection."));
394 itemButton27
->SetToolTip(_("Click to confirm the font selection."));
395 itemBoxSizer25
->Add(itemButton27
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
397 wxButton
* itemButton28
= new wxButton( this, wxID_CANCEL
, _("&Cancel"), wxDefaultPosition
, wxDefaultSize
, 0 );
399 itemButton28
->SetToolTip(_("Click to cancel the font selection."));
400 itemBoxSizer25
->Add(itemButton28
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
403 m_familyChoice
= (wxChoice
*) FindWindow(wxID_FONT_FAMILY
);
404 m_styleChoice
= (wxChoice
*) FindWindow(wxID_FONT_STYLE
);
405 m_weightChoice
= (wxChoice
*) FindWindow(wxID_FONT_WEIGHT
);
406 m_colourChoice
= (wxChoice
*) FindWindow(wxID_FONT_COLOUR
);
407 m_underLineCheckBox
= (wxCheckBox
*) FindWindow(wxID_FONT_UNDERLINE
);
409 m_familyChoice
->SetStringSelection( wxFontFamilyIntToString(m_dialogFont
.GetFamily()) );
410 m_styleChoice
->SetStringSelection(wxFontStyleIntToString(m_dialogFont
.GetStyle()));
411 m_weightChoice
->SetStringSelection(wxFontWeightIntToString(m_dialogFont
.GetWeight()));
415 wxString
name(wxTheColourDatabase
->FindName(m_fontData
.GetColour()));
417 m_colourChoice
->SetStringSelection(name
);
419 m_colourChoice
->SetStringSelection(wxT("BLACK"));
422 if (m_underLineCheckBox
)
424 m_underLineCheckBox
->SetValue(m_dialogFont
.GetUnderlined());
427 #if USE_SPINCTRL_FOR_POINT_SIZE
428 spinCtrl
->SetValue(m_dialogFont
.GetPointSize());
430 m_pointSizeChoice
= (wxChoice
*) FindWindow(wxID_FONT_SIZE
);
431 m_pointSizeChoice
->SetSelection(m_dialogFont
.GetPointSize()-1);
434 GetSizer()->SetItemMinSize(m_previewer
, is_pda
? 100 : 430, is_pda
? 40 : 100);
435 GetSizer()->SetSizeHints(this);
436 GetSizer()->Fit(this);
443 #if !USE_SPINCTRL_FOR_POINT_SIZE
447 // Don't block events any more
452 void wxGenericFontDialog::InitializeFont()
454 int fontFamily
= wxSWISS
;
455 int fontWeight
= wxNORMAL
;
456 int fontStyle
= wxNORMAL
;
458 bool fontUnderline
= false;
460 if (m_fontData
.m_initialFont
.Ok())
462 fontFamily
= m_fontData
.m_initialFont
.GetFamily();
463 fontWeight
= m_fontData
.m_initialFont
.GetWeight();
464 fontStyle
= m_fontData
.m_initialFont
.GetStyle();
465 fontSize
= m_fontData
.m_initialFont
.GetPointSize();
466 fontUnderline
= m_fontData
.m_initialFont
.GetUnderlined();
469 m_dialogFont
= wxFont(fontSize
, fontFamily
, fontStyle
,
470 fontWeight
, fontUnderline
);
473 m_previewer
->SetFont(m_dialogFont
);
476 void wxGenericFontDialog::OnChangeFont(wxCommandEvent
& WXUNUSED(event
))
481 void wxGenericFontDialog::DoChangeFont()
483 if (!m_useEvents
) return;
485 int fontFamily
= wxFontFamilyStringToInt(WXSTRINGCAST m_familyChoice
->GetStringSelection());
486 int fontWeight
= wxFontWeightStringToInt(WXSTRINGCAST m_weightChoice
->GetStringSelection());
487 int fontStyle
= wxFontStyleStringToInt(WXSTRINGCAST m_styleChoice
->GetStringSelection());
488 #if USE_SPINCTRL_FOR_POINT_SIZE
489 wxSpinCtrl
* fontSizeCtrl
= wxDynamicCast(FindWindow(wxID_FONT_SIZE
), wxSpinCtrl
);
490 int fontSize
= fontSizeCtrl
->GetValue();
492 int fontSize
= wxAtoi(m_pointSizeChoice
->GetStringSelection());
495 // Start with previous underline setting, we want to retain it even if we can't edit it
496 // m_dialogFont is always initialized because of the call to InitializeFont
497 int fontUnderline
= m_dialogFont
.GetUnderlined();
499 if (m_underLineCheckBox
)
501 fontUnderline
= m_underLineCheckBox
->GetValue();
504 m_dialogFont
= wxFont(fontSize
, fontFamily
, fontStyle
, fontWeight
, (fontUnderline
!= 0));
505 m_previewer
->SetFont(m_dialogFont
);
507 if ( m_colourChoice
)
509 if ( !m_colourChoice
->GetStringSelection().empty() )
511 wxColour col
= wxTheColourDatabase
->Find(m_colourChoice
->GetStringSelection());
514 m_fontData
.m_fontColour
= col
;
518 // Update color here so that we can also use the color originally passed in
519 // (EnableEffects may be false)
520 if (m_fontData
.m_fontColour
.Ok())
521 m_previewer
->SetForegroundColour(m_fontData
.m_fontColour
);
523 m_previewer
->Refresh();
526 #if USE_SPINCTRL_FOR_POINT_SIZE
527 void wxGenericFontDialog::OnChangeSize(wxSpinEvent
& WXUNUSED(event
))
533 const wxChar
*wxFontWeightIntToString(int weight
)
543 return wxT("Normal");
547 const wxChar
*wxFontStyleIntToString(int style
)
552 return wxT("Italic");
557 return wxT("Normal");
561 const wxChar
*wxFontFamilyIntToString(int family
)
568 return wxT("Decorative");
570 return wxT("Modern");
572 return wxT("Script");
574 return wxT("Teletype");
581 int wxFontFamilyStringToInt(wxChar
*family
)
586 if (wxStrcmp(family
, wxT("Roman")) == 0)
588 else if (wxStrcmp(family
, wxT("Decorative")) == 0)
590 else if (wxStrcmp(family
, wxT("Modern")) == 0)
592 else if (wxStrcmp(family
, wxT("Script")) == 0)
594 else if (wxStrcmp(family
, wxT("Teletype")) == 0)
599 int wxFontStyleStringToInt(wxChar
*style
)
603 if (wxStrcmp(style
, wxT("Italic")) == 0)
605 else if (wxStrcmp(style
, wxT("Slant")) == 0)
611 int wxFontWeightStringToInt(wxChar
*weight
)
615 if (wxStrcmp(weight
, wxT("Bold")) == 0)
617 else if (wxStrcmp(weight
, wxT("Light")) == 0)