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 //-----------------------------------------------------------------------------
93 //-----------------------------------------------------------------------------
95 static const wxChar
*wxFontWeightIntToString(int weight
)
105 return wxT("Normal");
109 static const wxChar
*wxFontStyleIntToString(int style
)
114 return wxT("Italic");
119 return wxT("Normal");
123 static const wxChar
*wxFontFamilyIntToString(int family
)
130 return wxT("Decorative");
132 return wxT("Modern");
134 return wxT("Script");
136 return wxT("Teletype");
143 static int wxFontFamilyStringToInt(const wxString
& family
)
148 if (wxStrcmp(family
, wxT("Roman")) == 0)
150 else if (wxStrcmp(family
, wxT("Decorative")) == 0)
152 else if (wxStrcmp(family
, wxT("Modern")) == 0)
154 else if (wxStrcmp(family
, wxT("Script")) == 0)
156 else if (wxStrcmp(family
, wxT("Teletype")) == 0)
161 static int wxFontStyleStringToInt(const wxString
& style
)
165 if (wxStrcmp(style
, wxT("Italic")) == 0)
167 else if (wxStrcmp(style
, wxT("Slant")) == 0)
173 static int wxFontWeightStringToInt(const wxString
& weight
)
177 if (wxStrcmp(weight
, wxT("Bold")) == 0)
179 else if (wxStrcmp(weight
, wxT("Light")) == 0)
185 //-----------------------------------------------------------------------------
186 // wxGenericFontDialog
187 //-----------------------------------------------------------------------------
189 IMPLEMENT_DYNAMIC_CLASS(wxGenericFontDialog
, wxDialog
)
191 BEGIN_EVENT_TABLE(wxGenericFontDialog
, wxDialog
)
192 EVT_CHECKBOX(wxID_FONT_UNDERLINE
, wxGenericFontDialog::OnChangeFont
)
193 EVT_CHOICE(wxID_FONT_STYLE
, wxGenericFontDialog::OnChangeFont
)
194 EVT_CHOICE(wxID_FONT_WEIGHT
, wxGenericFontDialog::OnChangeFont
)
195 EVT_CHOICE(wxID_FONT_FAMILY
, wxGenericFontDialog::OnChangeFont
)
196 EVT_CHOICE(wxID_FONT_COLOUR
, wxGenericFontDialog::OnChangeFont
)
197 #if USE_SPINCTRL_FOR_POINT_SIZE
198 EVT_SPINCTRL(wxID_FONT_SIZE
, wxGenericFontDialog::OnChangeSize
)
199 EVT_TEXT(wxID_FONT_SIZE
, wxGenericFontDialog::OnChangeFont
)
201 EVT_CHOICE(wxID_FONT_SIZE
, wxGenericFontDialog::OnChangeFont
)
203 EVT_CLOSE(wxGenericFontDialog::OnCloseWindow
)
208 static wxString wxColourDialogNames
[NUM_COLS
]={wxT("ORANGE"),
214 wxT("MEDIUM VIOLET RED"),
219 wxT("MEDIUM SPRING GREEN"),
222 wxT("LIGHT STEEL BLUE"),
224 wxT("LIGHT MAGENTA"),
242 wxT("MEDIUM VIOLET RED"),
246 wxT("MEDIUM SEA GREEN"),
248 wxT("MIDNIGHT BLUE"),
254 wxT("MEDIUM FOREST GREEN"),
259 wxT("MEDIUM SLATE BLUE"),
264 * Generic wxFontDialog
267 void wxGenericFontDialog::Init()
274 wxGenericFontDialog::~wxGenericFontDialog()
278 void wxGenericFontDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
280 EndModal(wxID_CANCEL
);
283 bool wxGenericFontDialog::DoCreate(wxWindow
*parent
)
285 parent
= GetParentForModalDialog(parent
);
287 if ( !wxDialog::Create( parent
, wxID_ANY
, wxT("Choose Font") ,
288 wxDefaultPosition
, wxDefaultSize
, wxDEFAULT_DIALOG_STYLE
,
289 wxT("fontdialog") ) )
291 wxFAIL_MSG( wxT("wxFontDialog creation failed") );
298 // sets initial font in preview area
304 int wxGenericFontDialog::ShowModal()
306 int ret
= wxDialog::ShowModal();
308 if (ret
!= wxID_CANCEL
)
310 m_fontData
.m_chosenFont
= m_dialogFont
;
316 // This should be application-settable
317 static bool ShowToolTips() { return false; }
319 void wxGenericFontDialog::CreateWidgets()
321 wxString
*families
= new wxString
[6],
322 *styles
= new wxString
[3],
323 *weights
= new wxString
[3];
324 families
[0] = _("Roman");
325 families
[1] = _("Decorative");
326 families
[2] = _("Modern");
327 families
[3] = _("Script");
328 families
[4] = _("Swiss" );
329 families
[5] = _("Teletype" );
330 styles
[0] = _("Normal");
331 styles
[1] = _("Italic");
332 styles
[2] = _("Slant");
333 weights
[0] = _("Normal");
334 weights
[1] = _("Light");
335 weights
[2] = _("Bold");
337 #if !USE_SPINCTRL_FOR_POINT_SIZE
338 wxString
*pointSizes
= new wxString
[40];
340 for ( i
= 0; i
< 40; i
++)
343 wxSprintf(buf
, wxT("%d"), i
+ 1);
350 bool is_pda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
354 noCols
= 2; noRows
= 3;
358 noCols
= 3; noRows
= 2;
361 wxBoxSizer
* itemBoxSizer2
= new wxBoxSizer(wxVERTICAL
);
362 this->SetSizer(itemBoxSizer2
);
363 this->SetAutoLayout(true);
365 wxBoxSizer
* itemBoxSizer3
= new wxBoxSizer(wxVERTICAL
);
366 itemBoxSizer2
->Add(itemBoxSizer3
, 1, wxGROW
|wxALL
, 5);
368 wxFlexGridSizer
* itemGridSizer4
= new wxFlexGridSizer(noRows
, noCols
, 0, 0);
369 itemBoxSizer3
->Add(itemGridSizer4
, 0, wxGROW
, 5);
371 wxBoxSizer
* itemBoxSizer5
= new wxBoxSizer(wxVERTICAL
);
372 itemGridSizer4
->Add(itemBoxSizer5
, 0, wxALIGN_CENTER_HORIZONTAL
|wxGROW
, 5);
373 wxStaticText
* itemStaticText6
= new wxStaticText( this, wxID_STATIC
, _("&Font family:"), wxDefaultPosition
, wxDefaultSize
, 0 );
374 itemBoxSizer5
->Add(itemStaticText6
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
, 5);
376 wxChoice
* itemChoice7
= new wxChoice( this, wxID_FONT_FAMILY
, wxDefaultPosition
, wxDefaultSize
, 5, families
, 0 );
377 itemChoice7
->SetHelpText(_("The font family."));
379 itemChoice7
->SetToolTip(_("The font family."));
380 itemBoxSizer5
->Add(itemChoice7
, 0, wxALIGN_LEFT
|wxALL
, 5);
382 wxBoxSizer
* itemBoxSizer8
= new wxBoxSizer(wxVERTICAL
);
383 itemGridSizer4
->Add(itemBoxSizer8
, 0, wxALIGN_CENTER_HORIZONTAL
|wxGROW
, 5);
384 wxStaticText
* itemStaticText9
= new wxStaticText( this, wxID_STATIC
, _("&Style:"), wxDefaultPosition
, wxDefaultSize
, 0 );
385 itemBoxSizer8
->Add(itemStaticText9
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
, 5);
387 wxChoice
* itemChoice10
= new wxChoice( this, wxID_FONT_STYLE
, wxDefaultPosition
, wxDefaultSize
, 3, styles
, 0 );
388 itemChoice10
->SetHelpText(_("The font style."));
390 itemChoice10
->SetToolTip(_("The font style."));
391 itemBoxSizer8
->Add(itemChoice10
, 0, wxALIGN_LEFT
|wxALL
, 5);
393 wxBoxSizer
* itemBoxSizer11
= new wxBoxSizer(wxVERTICAL
);
394 itemGridSizer4
->Add(itemBoxSizer11
, 0, wxALIGN_CENTER_HORIZONTAL
|wxGROW
, 5);
395 wxStaticText
* itemStaticText12
= new wxStaticText( this, wxID_STATIC
, _("&Weight:"), wxDefaultPosition
, wxDefaultSize
, 0 );
396 itemBoxSizer11
->Add(itemStaticText12
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
, 5);
398 wxChoice
* itemChoice13
= new wxChoice( this, wxID_FONT_WEIGHT
, wxDefaultPosition
, wxDefaultSize
, 3, weights
, 0 );
399 itemChoice13
->SetHelpText(_("The font weight."));
401 itemChoice13
->SetToolTip(_("The font weight."));
402 itemBoxSizer11
->Add(itemChoice13
, 0, wxALIGN_LEFT
|wxALL
, 5);
404 wxBoxSizer
* itemBoxSizer14
= new wxBoxSizer(wxVERTICAL
);
405 itemGridSizer4
->Add(itemBoxSizer14
, 0, wxALIGN_CENTER_HORIZONTAL
|wxGROW
, 5);
406 if (m_fontData
.GetEnableEffects())
408 wxStaticText
* itemStaticText15
= new wxStaticText( this, wxID_STATIC
, _("C&olour:"), wxDefaultPosition
, wxDefaultSize
, 0 );
409 itemBoxSizer14
->Add(itemStaticText15
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
, 5);
411 wxSize colourSize
= wxDefaultSize
;
415 wxChoice
* itemChoice16
= new wxChoice( this, wxID_FONT_COLOUR
, wxDefaultPosition
, colourSize
, NUM_COLS
, wxColourDialogNames
, 0 );
416 itemChoice16
->SetHelpText(_("The font colour."));
418 itemChoice16
->SetToolTip(_("The font colour."));
419 itemBoxSizer14
->Add(itemChoice16
, 0, wxALIGN_LEFT
|wxALL
, 5);
422 wxBoxSizer
* itemBoxSizer17
= new wxBoxSizer(wxVERTICAL
);
423 itemGridSizer4
->Add(itemBoxSizer17
, 0, wxALIGN_CENTER_HORIZONTAL
|wxGROW
, 5);
424 wxStaticText
* itemStaticText18
= new wxStaticText( this, wxID_STATIC
, _("&Point size:"), wxDefaultPosition
, wxDefaultSize
, 0 );
425 itemBoxSizer17
->Add(itemStaticText18
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
, 5);
427 #if USE_SPINCTRL_FOR_POINT_SIZE
428 wxSpinCtrl
* spinCtrl
= new wxSpinCtrl(this, wxID_FONT_SIZE
, wxT("12"), wxDefaultPosition
, wxSize(80, wxDefaultCoord
), wxSP_ARROW_KEYS
, 1, 500, 12);
429 spinCtrl
->SetHelpText(_("The font point size."));
431 spinCtrl
->SetToolTip(_("The font point size."));
433 itemBoxSizer17
->Add(spinCtrl
, 0, wxALIGN_LEFT
|wxALL
, 5);
435 wxChoice
* itemChoice19
= new wxChoice( this, wxID_FONT_SIZE
, wxDefaultPosition
, wxDefaultSize
, 40, pointSizes
, 0 );
436 itemChoice19
->SetHelpText(_("The font point size."));
438 itemChoice19
->SetToolTip(_("The font point size."));
439 itemBoxSizer17
->Add(itemChoice19
, 0, wxALIGN_LEFT
|wxALL
, 5);
442 if (m_fontData
.GetEnableEffects())
444 wxBoxSizer
* itemBoxSizer20
= new wxBoxSizer(wxVERTICAL
);
445 itemGridSizer4
->Add(itemBoxSizer20
, 0, wxALIGN_LEFT
|wxALIGN_CENTER_VERTICAL
, 5);
446 wxCheckBox
* itemCheckBox21
= new wxCheckBox( this, wxID_FONT_UNDERLINE
, _("&Underline"), wxDefaultPosition
, wxDefaultSize
, 0 );
447 itemCheckBox21
->SetValue(false);
448 itemCheckBox21
->SetHelpText(_("Whether the font is underlined."));
450 itemCheckBox21
->SetToolTip(_("Whether the font is underlined."));
451 itemBoxSizer20
->Add(itemCheckBox21
, 0, wxALIGN_LEFT
|wxALL
, 5);
455 itemBoxSizer3
->Add(5, 5, 0, wxALIGN_CENTER_HORIZONTAL
|wxALL
, 5);
457 wxStaticText
* itemStaticText23
= new wxStaticText( this, wxID_STATIC
, _("Preview:"), wxDefaultPosition
, wxDefaultSize
, 0 );
458 itemBoxSizer3
->Add(itemStaticText23
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
, 5);
460 wxFontPreviewer
* itemWindow24
= new wxFontPreviewer( this );
461 m_previewer
= itemWindow24
;
462 itemWindow24
->SetHelpText(_("Shows the font preview."));
464 itemWindow24
->SetToolTip(_("Shows the font preview."));
465 itemBoxSizer3
->Add(itemWindow24
, 1, wxGROW
|wxALL
, 5);
467 wxBoxSizer
* itemBoxSizer25
= new wxBoxSizer(wxHORIZONTAL
);
468 itemBoxSizer3
->Add(itemBoxSizer25
, 0, wxGROW
, 5);
469 itemBoxSizer25
->Add(5, 5, 1, wxGROW
|wxALL
, 5);
472 wxButton
* itemButton28
= new wxButton( this, wxID_CANCEL
, _("&Cancel"), wxDefaultPosition
, wxDefaultSize
, 0 );
474 itemButton28
->SetToolTip(_("Click to cancel the font selection."));
475 itemBoxSizer25
->Add(itemButton28
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
477 wxButton
* itemButton27
= new wxButton( this, wxID_OK
, _("&OK"), wxDefaultPosition
, wxDefaultSize
, 0 );
478 itemButton27
->SetDefault();
479 itemButton27
->SetHelpText(_("Click to confirm the font selection."));
481 itemButton27
->SetToolTip(_("Click to confirm the font selection."));
482 itemBoxSizer25
->Add(itemButton27
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
484 wxButton
* itemButton27
= new wxButton( this, wxID_OK
, _("&OK"), wxDefaultPosition
, wxDefaultSize
, 0 );
485 itemButton27
->SetDefault();
486 itemButton27
->SetHelpText(_("Click to confirm the font selection."));
488 itemButton27
->SetToolTip(_("Click to confirm the font selection."));
489 itemBoxSizer25
->Add(itemButton27
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
491 wxButton
* itemButton28
= new wxButton( this, wxID_CANCEL
, _("&Cancel"), wxDefaultPosition
, wxDefaultSize
, 0 );
493 itemButton28
->SetToolTip(_("Click to cancel the font selection."));
494 itemBoxSizer25
->Add(itemButton28
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
497 m_familyChoice
= (wxChoice
*) FindWindow(wxID_FONT_FAMILY
);
498 m_styleChoice
= (wxChoice
*) FindWindow(wxID_FONT_STYLE
);
499 m_weightChoice
= (wxChoice
*) FindWindow(wxID_FONT_WEIGHT
);
500 m_colourChoice
= (wxChoice
*) FindWindow(wxID_FONT_COLOUR
);
501 m_underLineCheckBox
= (wxCheckBox
*) FindWindow(wxID_FONT_UNDERLINE
);
503 m_familyChoice
->SetStringSelection( wxFontFamilyIntToString(m_dialogFont
.GetFamily()) );
504 m_styleChoice
->SetStringSelection(wxFontStyleIntToString(m_dialogFont
.GetStyle()));
505 m_weightChoice
->SetStringSelection(wxFontWeightIntToString(m_dialogFont
.GetWeight()));
509 wxString
name(wxTheColourDatabase
->FindName(m_fontData
.GetColour()));
511 m_colourChoice
->SetStringSelection(name
);
513 m_colourChoice
->SetStringSelection(wxT("BLACK"));
516 if (m_underLineCheckBox
)
518 m_underLineCheckBox
->SetValue(m_dialogFont
.GetUnderlined());
521 #if USE_SPINCTRL_FOR_POINT_SIZE
522 spinCtrl
->SetValue(m_dialogFont
.GetPointSize());
524 m_pointSizeChoice
= (wxChoice
*) FindWindow(wxID_FONT_SIZE
);
525 m_pointSizeChoice
->SetSelection(m_dialogFont
.GetPointSize()-1);
528 GetSizer()->SetItemMinSize(m_previewer
, is_pda
? 100 : 430, is_pda
? 40 : 100);
529 GetSizer()->SetSizeHints(this);
530 GetSizer()->Fit(this);
537 #if !USE_SPINCTRL_FOR_POINT_SIZE
541 // Don't block events any more
546 void wxGenericFontDialog::InitializeFont()
548 int fontFamily
= wxSWISS
;
549 int fontWeight
= wxNORMAL
;
550 int fontStyle
= wxNORMAL
;
552 bool fontUnderline
= false;
554 if (m_fontData
.m_initialFont
.Ok())
556 fontFamily
= m_fontData
.m_initialFont
.GetFamily();
557 fontWeight
= m_fontData
.m_initialFont
.GetWeight();
558 fontStyle
= m_fontData
.m_initialFont
.GetStyle();
559 fontSize
= m_fontData
.m_initialFont
.GetPointSize();
560 fontUnderline
= m_fontData
.m_initialFont
.GetUnderlined();
563 m_dialogFont
= wxFont(fontSize
, fontFamily
, fontStyle
,
564 fontWeight
, fontUnderline
);
567 m_previewer
->SetFont(m_dialogFont
);
570 void wxGenericFontDialog::OnChangeFont(wxCommandEvent
& WXUNUSED(event
))
575 void wxGenericFontDialog::DoChangeFont()
577 if (!m_useEvents
) return;
579 int fontFamily
= wxFontFamilyStringToInt(m_familyChoice
->GetStringSelection());
580 int fontWeight
= wxFontWeightStringToInt(m_weightChoice
->GetStringSelection());
581 int fontStyle
= wxFontStyleStringToInt(m_styleChoice
->GetStringSelection());
582 #if USE_SPINCTRL_FOR_POINT_SIZE
583 wxSpinCtrl
* fontSizeCtrl
= wxDynamicCast(FindWindow(wxID_FONT_SIZE
), wxSpinCtrl
);
584 int fontSize
= fontSizeCtrl
->GetValue();
586 int fontSize
= wxAtoi(m_pointSizeChoice
->GetStringSelection());
589 // Start with previous underline setting, we want to retain it even if we can't edit it
590 // m_dialogFont is always initialized because of the call to InitializeFont
591 int fontUnderline
= m_dialogFont
.GetUnderlined();
593 if (m_underLineCheckBox
)
595 fontUnderline
= m_underLineCheckBox
->GetValue();
598 m_dialogFont
= wxFont(fontSize
, fontFamily
, fontStyle
, fontWeight
, (fontUnderline
!= 0));
599 m_previewer
->SetFont(m_dialogFont
);
601 if ( m_colourChoice
)
603 if ( !m_colourChoice
->GetStringSelection().empty() )
605 wxColour col
= wxTheColourDatabase
->Find(m_colourChoice
->GetStringSelection());
608 m_fontData
.m_fontColour
= col
;
612 // Update color here so that we can also use the color originally passed in
613 // (EnableEffects may be false)
614 if (m_fontData
.m_fontColour
.Ok())
615 m_previewer
->SetForegroundColour(m_fontData
.m_fontColour
);
617 m_previewer
->Refresh();
620 #if USE_SPINCTRL_FOR_POINT_SIZE
621 void wxGenericFontDialog::OnChangeSize(wxSpinEvent
& WXUNUSED(event
))