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"
41 #include "wx/fontdlg.h"
42 #include "wx/generic/fontdlgg.h"
44 #if USE_SPINCTRL_FOR_POINT_SIZE
45 #include "wx/spinctrl.h"
48 //-----------------------------------------------------------------------------
49 // helper class - wxFontPreviewer
50 //-----------------------------------------------------------------------------
52 class WXDLLEXPORT wxFontPreviewer
: public wxWindow
55 wxFontPreviewer(wxWindow
*parent
, const wxSize
& sz
= wxDefaultSize
) : wxWindow(parent
, wxID_ANY
, wxDefaultPosition
, sz
)
60 void OnPaint(wxPaintEvent
& event
);
64 BEGIN_EVENT_TABLE(wxFontPreviewer
, wxWindow
)
65 EVT_PAINT(wxFontPreviewer::OnPaint
)
68 void wxFontPreviewer::OnPaint(wxPaintEvent
& WXUNUSED(event
))
72 wxSize size
= GetSize();
73 wxFont font
= GetFont();
75 dc
.SetPen(*wxBLACK_PEN
);
76 dc
.SetBrush(*wxWHITE_BRUSH
);
77 dc
.DrawRectangle(0, 0, size
.x
, size
.y
);
82 dc
.SetTextForeground(GetForegroundColour());
83 dc
.SetClippingRegion(2, 2, size
.x
-4, size
.y
-4);
84 dc
.DrawText(_("ABCDEFGabcdefg12345"),
85 10, (size
.y
- dc
.GetTextExtent(wxT("X")).y
)/2);
86 dc
.DestroyClippingRegion();
90 //-----------------------------------------------------------------------------
92 //-----------------------------------------------------------------------------
94 static const wxChar
*wxFontWeightIntToString(int weight
)
104 return wxT("Normal");
108 static const wxChar
*wxFontStyleIntToString(int style
)
113 return wxT("Italic");
118 return wxT("Normal");
122 static const wxChar
*wxFontFamilyIntToString(int family
)
129 return wxT("Decorative");
131 return wxT("Modern");
133 return wxT("Script");
135 return wxT("Teletype");
142 static int wxFontFamilyStringToInt(const wxString
& family
)
147 if (wxStrcmp(family
, wxT("Roman")) == 0)
149 else if (wxStrcmp(family
, wxT("Decorative")) == 0)
151 else if (wxStrcmp(family
, wxT("Modern")) == 0)
153 else if (wxStrcmp(family
, wxT("Script")) == 0)
155 else if (wxStrcmp(family
, wxT("Teletype")) == 0)
160 static int wxFontStyleStringToInt(const wxString
& style
)
164 if (wxStrcmp(style
, wxT("Italic")) == 0)
166 else if (wxStrcmp(style
, wxT("Slant")) == 0)
172 static int wxFontWeightStringToInt(const wxString
& weight
)
176 if (wxStrcmp(weight
, wxT("Bold")) == 0)
178 else if (wxStrcmp(weight
, wxT("Light")) == 0)
184 //-----------------------------------------------------------------------------
185 // wxGenericFontDialog
186 //-----------------------------------------------------------------------------
188 IMPLEMENT_DYNAMIC_CLASS(wxGenericFontDialog
, wxDialog
)
190 BEGIN_EVENT_TABLE(wxGenericFontDialog
, wxDialog
)
191 EVT_CHECKBOX(wxID_FONT_UNDERLINE
, wxGenericFontDialog::OnChangeFont
)
192 EVT_CHOICE(wxID_FONT_STYLE
, wxGenericFontDialog::OnChangeFont
)
193 EVT_CHOICE(wxID_FONT_WEIGHT
, wxGenericFontDialog::OnChangeFont
)
194 EVT_CHOICE(wxID_FONT_FAMILY
, wxGenericFontDialog::OnChangeFont
)
195 EVT_CHOICE(wxID_FONT_COLOUR
, wxGenericFontDialog::OnChangeFont
)
196 #if USE_SPINCTRL_FOR_POINT_SIZE
197 EVT_SPINCTRL(wxID_FONT_SIZE
, wxGenericFontDialog::OnChangeSize
)
198 EVT_TEXT(wxID_FONT_SIZE
, wxGenericFontDialog::OnChangeFont
)
200 EVT_CHOICE(wxID_FONT_SIZE
, wxGenericFontDialog::OnChangeFont
)
202 EVT_CLOSE(wxGenericFontDialog::OnCloseWindow
)
207 static wxString wxColourDialogNames
[NUM_COLS
]={wxT("ORANGE"),
213 wxT("MEDIUM VIOLET RED"),
218 wxT("MEDIUM SPRING GREEN"),
221 wxT("LIGHT STEEL BLUE"),
223 wxT("LIGHT MAGENTA"),
241 wxT("MEDIUM VIOLET RED"),
245 wxT("MEDIUM SEA GREEN"),
247 wxT("MIDNIGHT BLUE"),
253 wxT("MEDIUM FOREST GREEN"),
258 wxT("MEDIUM SLATE BLUE"),
263 * Generic wxFontDialog
266 void wxGenericFontDialog::Init()
273 wxGenericFontDialog::~wxGenericFontDialog()
277 void wxGenericFontDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
279 EndModal(wxID_CANCEL
);
282 bool wxGenericFontDialog::DoCreate(wxWindow
*parent
)
284 parent
= GetParentForModalDialog(parent
, 0);
286 if ( !wxDialog::Create( parent
, wxID_ANY
, wxT("Choose Font") ,
287 wxDefaultPosition
, wxDefaultSize
, wxDEFAULT_DIALOG_STYLE
,
288 wxT("fontdialog") ) )
290 wxFAIL_MSG( wxT("wxFontDialog creation failed") );
297 // sets initial font in preview area
303 int wxGenericFontDialog::ShowModal()
305 int ret
= wxDialog::ShowModal();
307 if (ret
!= wxID_CANCEL
)
309 m_fontData
.m_chosenFont
= m_dialogFont
;
315 // This should be application-settable
316 static bool ShowToolTips() { return false; }
318 void wxGenericFontDialog::CreateWidgets()
320 wxString
*families
= new wxString
[6],
321 *styles
= new wxString
[3],
322 *weights
= new wxString
[3];
323 families
[0] = _("Roman");
324 families
[1] = _("Decorative");
325 families
[2] = _("Modern");
326 families
[3] = _("Script");
327 families
[4] = _("Swiss" );
328 families
[5] = _("Teletype" );
329 styles
[0] = _("Normal");
330 styles
[1] = _("Italic");
331 styles
[2] = _("Slant");
332 weights
[0] = _("Normal");
333 weights
[1] = _("Light");
334 weights
[2] = _("Bold");
336 #if !USE_SPINCTRL_FOR_POINT_SIZE
337 wxString
*pointSizes
= new wxString
[40];
339 for ( i
= 0; i
< 40; i
++)
342 wxSprintf(buf
, wxT("%d"), i
+ 1);
349 bool is_pda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
353 noCols
= 2; noRows
= 3;
357 noCols
= 3; noRows
= 2;
360 wxBoxSizer
* itemBoxSizer2
= new wxBoxSizer(wxVERTICAL
);
361 this->SetSizer(itemBoxSizer2
);
362 this->SetAutoLayout(true);
364 wxBoxSizer
* itemBoxSizer3
= new wxBoxSizer(wxVERTICAL
);
365 itemBoxSizer2
->Add(itemBoxSizer3
, 1, wxGROW
|wxALL
, 5);
367 wxFlexGridSizer
* itemGridSizer4
= new wxFlexGridSizer(noRows
, noCols
, 0, 0);
368 itemBoxSizer3
->Add(itemGridSizer4
, 0, wxGROW
, 5);
370 wxBoxSizer
* itemBoxSizer5
= new wxBoxSizer(wxVERTICAL
);
371 itemGridSizer4
->Add(itemBoxSizer5
, 0, wxALIGN_CENTER_HORIZONTAL
|wxGROW
, 5);
372 wxStaticText
* itemStaticText6
= new wxStaticText( this, wxID_STATIC
, _("&Font family:"), wxDefaultPosition
, wxDefaultSize
, 0 );
373 itemBoxSizer5
->Add(itemStaticText6
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
, 5);
375 wxChoice
* itemChoice7
= new wxChoice( this, wxID_FONT_FAMILY
, wxDefaultPosition
, wxDefaultSize
, 5, families
, 0 );
376 itemChoice7
->SetHelpText(_("The font family."));
378 itemChoice7
->SetToolTip(_("The font family."));
379 itemBoxSizer5
->Add(itemChoice7
, 0, wxALIGN_LEFT
|wxALL
, 5);
381 wxBoxSizer
* itemBoxSizer8
= new wxBoxSizer(wxVERTICAL
);
382 itemGridSizer4
->Add(itemBoxSizer8
, 0, wxALIGN_CENTER_HORIZONTAL
|wxGROW
, 5);
383 wxStaticText
* itemStaticText9
= new wxStaticText( this, wxID_STATIC
, _("&Style:"), wxDefaultPosition
, wxDefaultSize
, 0 );
384 itemBoxSizer8
->Add(itemStaticText9
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
, 5);
386 wxChoice
* itemChoice10
= new wxChoice( this, wxID_FONT_STYLE
, wxDefaultPosition
, wxDefaultSize
, 3, styles
, 0 );
387 itemChoice10
->SetHelpText(_("The font style."));
389 itemChoice10
->SetToolTip(_("The font style."));
390 itemBoxSizer8
->Add(itemChoice10
, 0, wxALIGN_LEFT
|wxALL
, 5);
392 wxBoxSizer
* itemBoxSizer11
= new wxBoxSizer(wxVERTICAL
);
393 itemGridSizer4
->Add(itemBoxSizer11
, 0, wxALIGN_CENTER_HORIZONTAL
|wxGROW
, 5);
394 wxStaticText
* itemStaticText12
= new wxStaticText( this, wxID_STATIC
, _("&Weight:"), wxDefaultPosition
, wxDefaultSize
, 0 );
395 itemBoxSizer11
->Add(itemStaticText12
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
, 5);
397 wxChoice
* itemChoice13
= new wxChoice( this, wxID_FONT_WEIGHT
, wxDefaultPosition
, wxDefaultSize
, 3, weights
, 0 );
398 itemChoice13
->SetHelpText(_("The font weight."));
400 itemChoice13
->SetToolTip(_("The font weight."));
401 itemBoxSizer11
->Add(itemChoice13
, 0, wxALIGN_LEFT
|wxALL
, 5);
403 wxBoxSizer
* itemBoxSizer14
= new wxBoxSizer(wxVERTICAL
);
404 itemGridSizer4
->Add(itemBoxSizer14
, 0, wxALIGN_CENTER_HORIZONTAL
|wxGROW
, 5);
405 if (m_fontData
.GetEnableEffects())
407 wxStaticText
* itemStaticText15
= new wxStaticText( this, wxID_STATIC
, _("C&olour:"), wxDefaultPosition
, wxDefaultSize
, 0 );
408 itemBoxSizer14
->Add(itemStaticText15
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
, 5);
410 wxSize colourSize
= wxDefaultSize
;
414 wxChoice
* itemChoice16
= new wxChoice( this, wxID_FONT_COLOUR
, wxDefaultPosition
, colourSize
, NUM_COLS
, wxColourDialogNames
, 0 );
415 itemChoice16
->SetHelpText(_("The font colour."));
417 itemChoice16
->SetToolTip(_("The font colour."));
418 itemBoxSizer14
->Add(itemChoice16
, 0, wxALIGN_LEFT
|wxALL
, 5);
421 wxBoxSizer
* itemBoxSizer17
= new wxBoxSizer(wxVERTICAL
);
422 itemGridSizer4
->Add(itemBoxSizer17
, 0, wxALIGN_CENTER_HORIZONTAL
|wxGROW
, 5);
423 wxStaticText
* itemStaticText18
= new wxStaticText( this, wxID_STATIC
, _("&Point size:"), wxDefaultPosition
, wxDefaultSize
, 0 );
424 itemBoxSizer17
->Add(itemStaticText18
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
, 5);
426 #if USE_SPINCTRL_FOR_POINT_SIZE
427 wxSpinCtrl
* spinCtrl
= new wxSpinCtrl(this, wxID_FONT_SIZE
, wxT("12"), wxDefaultPosition
, wxSize(80, wxDefaultCoord
), wxSP_ARROW_KEYS
, 1, 500, 12);
428 spinCtrl
->SetHelpText(_("The font point size."));
430 spinCtrl
->SetToolTip(_("The font point size."));
432 itemBoxSizer17
->Add(spinCtrl
, 0, wxALIGN_LEFT
|wxALL
, 5);
434 wxChoice
* itemChoice19
= new wxChoice( this, wxID_FONT_SIZE
, wxDefaultPosition
, wxDefaultSize
, 40, pointSizes
, 0 );
435 itemChoice19
->SetHelpText(_("The font point size."));
437 itemChoice19
->SetToolTip(_("The font point size."));
438 itemBoxSizer17
->Add(itemChoice19
, 0, wxALIGN_LEFT
|wxALL
, 5);
441 if (m_fontData
.GetEnableEffects())
443 wxBoxSizer
* itemBoxSizer20
= new wxBoxSizer(wxVERTICAL
);
444 itemGridSizer4
->Add(itemBoxSizer20
, 0, wxALIGN_LEFT
|wxALIGN_CENTER_VERTICAL
, 5);
445 wxCheckBox
* itemCheckBox21
= new wxCheckBox( this, wxID_FONT_UNDERLINE
, _("&Underline"), wxDefaultPosition
, wxDefaultSize
, 0 );
446 itemCheckBox21
->SetValue(false);
447 itemCheckBox21
->SetHelpText(_("Whether the font is underlined."));
449 itemCheckBox21
->SetToolTip(_("Whether the font is underlined."));
450 itemBoxSizer20
->Add(itemCheckBox21
, 0, wxALIGN_LEFT
|wxALL
, 5);
454 itemBoxSizer3
->Add(5, 5, 0, wxALIGN_CENTER_HORIZONTAL
|wxALL
, 5);
456 wxStaticText
* itemStaticText23
= new wxStaticText( this, wxID_STATIC
, _("Preview:"), wxDefaultPosition
, wxDefaultSize
, 0 );
457 itemBoxSizer3
->Add(itemStaticText23
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
, 5);
459 wxFontPreviewer
* itemWindow24
= new wxFontPreviewer( this );
460 m_previewer
= itemWindow24
;
461 itemWindow24
->SetHelpText(_("Shows the font preview."));
463 itemWindow24
->SetToolTip(_("Shows the font preview."));
464 itemBoxSizer3
->Add(itemWindow24
, 1, wxGROW
|wxALL
, 5);
466 wxBoxSizer
* itemBoxSizer25
= new wxBoxSizer(wxHORIZONTAL
);
467 itemBoxSizer3
->Add(itemBoxSizer25
, 0, wxGROW
, 5);
468 itemBoxSizer25
->Add(5, 5, 1, wxGROW
|wxALL
, 5);
471 wxButton
* itemButton28
= new wxButton( this, wxID_CANCEL
, _("&Cancel"), wxDefaultPosition
, wxDefaultSize
, 0 );
473 itemButton28
->SetToolTip(_("Click to cancel the font selection."));
474 itemBoxSizer25
->Add(itemButton28
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
476 wxButton
* itemButton27
= new wxButton( this, wxID_OK
, _("&OK"), wxDefaultPosition
, wxDefaultSize
, 0 );
477 itemButton27
->SetDefault();
478 itemButton27
->SetHelpText(_("Click to confirm the font selection."));
480 itemButton27
->SetToolTip(_("Click to confirm the font selection."));
481 itemBoxSizer25
->Add(itemButton27
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
483 wxButton
* itemButton27
= new wxButton( this, wxID_OK
, _("&OK"), wxDefaultPosition
, wxDefaultSize
, 0 );
484 itemButton27
->SetDefault();
485 itemButton27
->SetHelpText(_("Click to confirm the font selection."));
487 itemButton27
->SetToolTip(_("Click to confirm the font selection."));
488 itemBoxSizer25
->Add(itemButton27
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
490 wxButton
* itemButton28
= new wxButton( this, wxID_CANCEL
, _("&Cancel"), wxDefaultPosition
, wxDefaultSize
, 0 );
492 itemButton28
->SetToolTip(_("Click to cancel the font selection."));
493 itemBoxSizer25
->Add(itemButton28
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
496 m_familyChoice
= (wxChoice
*) FindWindow(wxID_FONT_FAMILY
);
497 m_styleChoice
= (wxChoice
*) FindWindow(wxID_FONT_STYLE
);
498 m_weightChoice
= (wxChoice
*) FindWindow(wxID_FONT_WEIGHT
);
499 m_colourChoice
= (wxChoice
*) FindWindow(wxID_FONT_COLOUR
);
500 m_underLineCheckBox
= (wxCheckBox
*) FindWindow(wxID_FONT_UNDERLINE
);
502 m_familyChoice
->SetStringSelection( wxFontFamilyIntToString(m_dialogFont
.GetFamily()) );
503 m_styleChoice
->SetStringSelection(wxFontStyleIntToString(m_dialogFont
.GetStyle()));
504 m_weightChoice
->SetStringSelection(wxFontWeightIntToString(m_dialogFont
.GetWeight()));
508 wxString
name(wxTheColourDatabase
->FindName(m_fontData
.GetColour()));
510 m_colourChoice
->SetStringSelection(wxT("BLACK"));
512 m_colourChoice
->SetStringSelection(name
);
515 if (m_underLineCheckBox
)
517 m_underLineCheckBox
->SetValue(m_dialogFont
.GetUnderlined());
520 #if USE_SPINCTRL_FOR_POINT_SIZE
521 spinCtrl
->SetValue(m_dialogFont
.GetPointSize());
523 m_pointSizeChoice
= (wxChoice
*) FindWindow(wxID_FONT_SIZE
);
524 m_pointSizeChoice
->SetSelection(m_dialogFont
.GetPointSize()-1);
527 GetSizer()->SetItemMinSize(m_previewer
, is_pda
? 100 : 430, is_pda
? 40 : 100);
528 GetSizer()->SetSizeHints(this);
529 GetSizer()->Fit(this);
536 #if !USE_SPINCTRL_FOR_POINT_SIZE
540 // Don't block events any more
545 void wxGenericFontDialog::InitializeFont()
547 int fontFamily
= wxSWISS
;
548 int fontWeight
= wxNORMAL
;
549 int fontStyle
= wxNORMAL
;
551 bool fontUnderline
= false;
553 if (m_fontData
.m_initialFont
.Ok())
555 fontFamily
= m_fontData
.m_initialFont
.GetFamily();
556 fontWeight
= m_fontData
.m_initialFont
.GetWeight();
557 fontStyle
= m_fontData
.m_initialFont
.GetStyle();
558 fontSize
= m_fontData
.m_initialFont
.GetPointSize();
559 fontUnderline
= m_fontData
.m_initialFont
.GetUnderlined();
562 m_dialogFont
= wxFont(fontSize
, fontFamily
, fontStyle
,
563 fontWeight
, fontUnderline
);
566 m_previewer
->SetFont(m_dialogFont
);
569 void wxGenericFontDialog::OnChangeFont(wxCommandEvent
& WXUNUSED(event
))
574 void wxGenericFontDialog::DoChangeFont()
576 if (!m_useEvents
) return;
578 int fontFamily
= wxFontFamilyStringToInt(m_familyChoice
->GetStringSelection());
579 int fontWeight
= wxFontWeightStringToInt(m_weightChoice
->GetStringSelection());
580 int fontStyle
= wxFontStyleStringToInt(m_styleChoice
->GetStringSelection());
581 #if USE_SPINCTRL_FOR_POINT_SIZE
582 wxSpinCtrl
* fontSizeCtrl
= wxDynamicCast(FindWindow(wxID_FONT_SIZE
), wxSpinCtrl
);
583 int fontSize
= fontSizeCtrl
->GetValue();
585 int fontSize
= wxAtoi(m_pointSizeChoice
->GetStringSelection());
588 // Start with previous underline setting, we want to retain it even if we can't edit it
589 // m_dialogFont is always initialized because of the call to InitializeFont
590 int fontUnderline
= m_dialogFont
.GetUnderlined();
592 if (m_underLineCheckBox
)
594 fontUnderline
= m_underLineCheckBox
->GetValue();
597 m_dialogFont
= wxFont(fontSize
, fontFamily
, fontStyle
, fontWeight
, (fontUnderline
!= 0));
598 m_previewer
->SetFont(m_dialogFont
);
600 if ( m_colourChoice
)
602 if ( !m_colourChoice
->GetStringSelection().empty() )
604 wxColour col
= wxTheColourDatabase
->Find(m_colourChoice
->GetStringSelection());
607 m_fontData
.m_fontColour
= col
;
611 // Update color here so that we can also use the color originally passed in
612 // (EnableEffects may be false)
613 if (m_fontData
.m_fontColour
.Ok())
614 m_previewer
->SetForegroundColour(m_fontData
.m_fontColour
);
616 m_previewer
->Refresh();
619 #if USE_SPINCTRL_FOR_POINT_SIZE
620 void wxGenericFontDialog::OnChangeSize(wxSpinEvent
& WXUNUSED(event
))