1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/fontdlgg.cpp
3 // Purpose: Generic font dialog
4 // Author: Julian Smart
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
18 #if wxUSE_FONTDLG && (!defined(__WXGTK__) || defined(__WXGPE__) || defined(__WXUNIVERSAL__))
24 #include "wx/dialog.h"
25 #include "wx/listbox.h"
26 #include "wx/button.h"
27 #include "wx/stattext.h"
28 #include "wx/layout.h"
29 #include "wx/dcclient.h"
30 #include "wx/choice.h"
31 #include "wx/checkbox.h"
33 #include "wx/settings.h"
40 #include "wx/fontdlg.h"
41 #include "wx/generic/fontdlgg.h"
43 #if USE_SPINCTRL_FOR_POINT_SIZE
44 #include "wx/spinctrl.h"
47 //-----------------------------------------------------------------------------
48 // helper class - wxFontPreviewer
49 //-----------------------------------------------------------------------------
51 class WXDLLEXPORT wxFontPreviewer
: public wxWindow
54 wxFontPreviewer(wxWindow
*parent
, const wxSize
& sz
= wxDefaultSize
) : wxWindow(parent
, wxID_ANY
, wxDefaultPosition
, sz
)
59 void OnPaint(wxPaintEvent
& event
);
63 BEGIN_EVENT_TABLE(wxFontPreviewer
, wxWindow
)
64 EVT_PAINT(wxFontPreviewer::OnPaint
)
67 void wxFontPreviewer::OnPaint(wxPaintEvent
& WXUNUSED(event
))
71 wxSize size
= GetSize();
72 wxFont font
= GetFont();
74 dc
.SetPen(*wxBLACK_PEN
);
75 dc
.SetBrush(*wxWHITE_BRUSH
);
76 dc
.DrawRectangle(0, 0, size
.x
, size
.y
);
81 dc
.SetTextForeground(GetForegroundColour());
82 dc
.SetClippingRegion(2, 2, size
.x
-4, size
.y
-4);
83 dc
.DrawText(_("ABCDEFGabcdefg12345"),
84 10, (size
.y
- dc
.GetTextExtent(wxT("X")).y
)/2);
85 dc
.DestroyClippingRegion();
89 //-----------------------------------------------------------------------------
91 //-----------------------------------------------------------------------------
93 static const wxChar
*wxFontWeightIntToString(int weight
)
103 return wxT("Normal");
107 static const wxChar
*wxFontStyleIntToString(int style
)
112 return wxT("Italic");
117 return wxT("Normal");
121 static const wxChar
*wxFontFamilyIntToString(int family
)
128 return wxT("Decorative");
130 return wxT("Modern");
132 return wxT("Script");
134 return wxT("Teletype");
141 static int wxFontFamilyStringToInt(const wxString
& family
)
146 if (wxStrcmp(family
, wxT("Roman")) == 0)
148 else if (wxStrcmp(family
, wxT("Decorative")) == 0)
150 else if (wxStrcmp(family
, wxT("Modern")) == 0)
152 else if (wxStrcmp(family
, wxT("Script")) == 0)
154 else if (wxStrcmp(family
, wxT("Teletype")) == 0)
159 static int wxFontStyleStringToInt(const wxString
& style
)
163 if (wxStrcmp(style
, wxT("Italic")) == 0)
165 else if (wxStrcmp(style
, wxT("Slant")) == 0)
171 static int wxFontWeightStringToInt(const wxString
& weight
)
175 if (wxStrcmp(weight
, wxT("Bold")) == 0)
177 else if (wxStrcmp(weight
, wxT("Light")) == 0)
183 //-----------------------------------------------------------------------------
184 // wxGenericFontDialog
185 //-----------------------------------------------------------------------------
187 IMPLEMENT_DYNAMIC_CLASS(wxGenericFontDialog
, wxDialog
)
189 BEGIN_EVENT_TABLE(wxGenericFontDialog
, wxDialog
)
190 EVT_CHECKBOX(wxID_FONT_UNDERLINE
, wxGenericFontDialog::OnChangeFont
)
191 EVT_CHOICE(wxID_FONT_STYLE
, wxGenericFontDialog::OnChangeFont
)
192 EVT_CHOICE(wxID_FONT_WEIGHT
, wxGenericFontDialog::OnChangeFont
)
193 EVT_CHOICE(wxID_FONT_FAMILY
, wxGenericFontDialog::OnChangeFont
)
194 EVT_CHOICE(wxID_FONT_COLOUR
, wxGenericFontDialog::OnChangeFont
)
195 #if USE_SPINCTRL_FOR_POINT_SIZE
196 EVT_SPINCTRL(wxID_FONT_SIZE
, wxGenericFontDialog::OnChangeSize
)
197 EVT_TEXT(wxID_FONT_SIZE
, wxGenericFontDialog::OnChangeFont
)
199 EVT_CHOICE(wxID_FONT_SIZE
, wxGenericFontDialog::OnChangeFont
)
201 EVT_CLOSE(wxGenericFontDialog::OnCloseWindow
)
206 static wxString wxColourDialogNames
[NUM_COLS
]={wxT("ORANGE"),
212 wxT("MEDIUM VIOLET RED"),
217 wxT("MEDIUM SPRING GREEN"),
220 wxT("LIGHT STEEL BLUE"),
222 wxT("LIGHT MAGENTA"),
240 wxT("MEDIUM VIOLET RED"),
244 wxT("MEDIUM SEA GREEN"),
246 wxT("MIDNIGHT BLUE"),
252 wxT("MEDIUM FOREST GREEN"),
257 wxT("MEDIUM SLATE BLUE"),
262 * Generic wxFontDialog
265 void wxGenericFontDialog::Init()
272 wxGenericFontDialog::~wxGenericFontDialog()
276 void wxGenericFontDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
278 EndModal(wxID_CANCEL
);
281 bool wxGenericFontDialog::DoCreate(wxWindow
*parent
)
283 parent
= GetParentForModalDialog(parent
, 0);
285 if ( !wxDialog::Create( parent
, wxID_ANY
, wxT("Choose Font") ,
286 wxDefaultPosition
, wxDefaultSize
, wxDEFAULT_DIALOG_STYLE
,
287 wxT("fontdialog") ) )
289 wxFAIL_MSG( wxT("wxFontDialog creation failed") );
296 // sets initial font in preview area
302 int wxGenericFontDialog::ShowModal()
304 int ret
= wxDialog::ShowModal();
306 if (ret
!= wxID_CANCEL
)
308 m_fontData
.m_chosenFont
= m_dialogFont
;
314 // This should be application-settable
315 static bool ShowToolTips() { return false; }
317 void wxGenericFontDialog::CreateWidgets()
319 wxString
*families
= new wxString
[6],
320 *styles
= new wxString
[3],
321 *weights
= new wxString
[3];
322 families
[0] = _("Roman");
323 families
[1] = _("Decorative");
324 families
[2] = _("Modern");
325 families
[3] = _("Script");
326 families
[4] = _("Swiss" );
327 families
[5] = _("Teletype" );
328 styles
[0] = _("Normal");
329 styles
[1] = _("Italic");
330 styles
[2] = _("Slant");
331 weights
[0] = _("Normal");
332 weights
[1] = _("Light");
333 weights
[2] = _("Bold");
335 #if !USE_SPINCTRL_FOR_POINT_SIZE
336 wxString
*pointSizes
= new wxString
[40];
338 for ( i
= 0; i
< 40; i
++)
341 wxSprintf(buf
, wxT("%d"), i
+ 1);
348 bool is_pda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
352 noCols
= 2; noRows
= 3;
356 noCols
= 3; noRows
= 2;
359 wxBoxSizer
* itemBoxSizer2
= new wxBoxSizer(wxVERTICAL
);
360 this->SetSizer(itemBoxSizer2
);
361 this->SetAutoLayout(true);
363 wxBoxSizer
* itemBoxSizer3
= new wxBoxSizer(wxVERTICAL
);
364 itemBoxSizer2
->Add(itemBoxSizer3
, 1, wxGROW
|wxALL
, 5);
366 wxFlexGridSizer
* itemGridSizer4
= new wxFlexGridSizer(noRows
, noCols
, 0, 0);
367 itemBoxSizer3
->Add(itemGridSizer4
, 0, wxGROW
, 5);
369 wxBoxSizer
* itemBoxSizer5
= new wxBoxSizer(wxVERTICAL
);
370 itemGridSizer4
->Add(itemBoxSizer5
, 0, wxALIGN_CENTER_HORIZONTAL
|wxGROW
, 5);
371 wxStaticText
* itemStaticText6
= new wxStaticText( this, wxID_STATIC
, _("&Font family:"), wxDefaultPosition
, wxDefaultSize
, 0 );
372 itemBoxSizer5
->Add(itemStaticText6
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
, 5);
374 wxChoice
* itemChoice7
= new wxChoice( this, wxID_FONT_FAMILY
, wxDefaultPosition
, wxDefaultSize
, 5, families
, 0 );
375 itemChoice7
->SetHelpText(_("The font family."));
377 itemChoice7
->SetToolTip(_("The font family."));
378 itemBoxSizer5
->Add(itemChoice7
, 0, wxALIGN_LEFT
|wxALL
, 5);
380 wxBoxSizer
* itemBoxSizer8
= new wxBoxSizer(wxVERTICAL
);
381 itemGridSizer4
->Add(itemBoxSizer8
, 0, wxALIGN_CENTER_HORIZONTAL
|wxGROW
, 5);
382 wxStaticText
* itemStaticText9
= new wxStaticText( this, wxID_STATIC
, _("&Style:"), wxDefaultPosition
, wxDefaultSize
, 0 );
383 itemBoxSizer8
->Add(itemStaticText9
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
, 5);
385 wxChoice
* itemChoice10
= new wxChoice( this, wxID_FONT_STYLE
, wxDefaultPosition
, wxDefaultSize
, 3, styles
, 0 );
386 itemChoice10
->SetHelpText(_("The font style."));
388 itemChoice10
->SetToolTip(_("The font style."));
389 itemBoxSizer8
->Add(itemChoice10
, 0, wxALIGN_LEFT
|wxALL
, 5);
391 wxBoxSizer
* itemBoxSizer11
= new wxBoxSizer(wxVERTICAL
);
392 itemGridSizer4
->Add(itemBoxSizer11
, 0, wxALIGN_CENTER_HORIZONTAL
|wxGROW
, 5);
393 wxStaticText
* itemStaticText12
= new wxStaticText( this, wxID_STATIC
, _("&Weight:"), wxDefaultPosition
, wxDefaultSize
, 0 );
394 itemBoxSizer11
->Add(itemStaticText12
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
, 5);
396 wxChoice
* itemChoice13
= new wxChoice( this, wxID_FONT_WEIGHT
, wxDefaultPosition
, wxDefaultSize
, 3, weights
, 0 );
397 itemChoice13
->SetHelpText(_("The font weight."));
399 itemChoice13
->SetToolTip(_("The font weight."));
400 itemBoxSizer11
->Add(itemChoice13
, 0, wxALIGN_LEFT
|wxALL
, 5);
402 wxBoxSizer
* itemBoxSizer14
= new wxBoxSizer(wxVERTICAL
);
403 itemGridSizer4
->Add(itemBoxSizer14
, 0, wxALIGN_CENTER_HORIZONTAL
|wxGROW
, 5);
404 if (m_fontData
.GetEnableEffects())
406 wxStaticText
* itemStaticText15
= new wxStaticText( this, wxID_STATIC
, _("C&olour:"), wxDefaultPosition
, wxDefaultSize
, 0 );
407 itemBoxSizer14
->Add(itemStaticText15
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
, 5);
409 wxSize colourSize
= wxDefaultSize
;
413 wxChoice
* itemChoice16
= new wxChoice( this, wxID_FONT_COLOUR
, wxDefaultPosition
, colourSize
, NUM_COLS
, wxColourDialogNames
, 0 );
414 itemChoice16
->SetHelpText(_("The font colour."));
416 itemChoice16
->SetToolTip(_("The font colour."));
417 itemBoxSizer14
->Add(itemChoice16
, 0, wxALIGN_LEFT
|wxALL
, 5);
420 wxBoxSizer
* itemBoxSizer17
= new wxBoxSizer(wxVERTICAL
);
421 itemGridSizer4
->Add(itemBoxSizer17
, 0, wxALIGN_CENTER_HORIZONTAL
|wxGROW
, 5);
422 wxStaticText
* itemStaticText18
= new wxStaticText( this, wxID_STATIC
, _("&Point size:"), wxDefaultPosition
, wxDefaultSize
, 0 );
423 itemBoxSizer17
->Add(itemStaticText18
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
, 5);
425 #if USE_SPINCTRL_FOR_POINT_SIZE
426 wxSpinCtrl
* spinCtrl
= new wxSpinCtrl(this, wxID_FONT_SIZE
, wxT("12"), wxDefaultPosition
, wxSize(80, wxDefaultCoord
), wxSP_ARROW_KEYS
, 1, 500, 12);
427 spinCtrl
->SetHelpText(_("The font point size."));
429 spinCtrl
->SetToolTip(_("The font point size."));
431 itemBoxSizer17
->Add(spinCtrl
, 0, wxALIGN_LEFT
|wxALL
, 5);
433 wxChoice
* itemChoice19
= new wxChoice( this, wxID_FONT_SIZE
, wxDefaultPosition
, wxDefaultSize
, 40, pointSizes
, 0 );
434 itemChoice19
->SetHelpText(_("The font point size."));
436 itemChoice19
->SetToolTip(_("The font point size."));
437 itemBoxSizer17
->Add(itemChoice19
, 0, wxALIGN_LEFT
|wxALL
, 5);
440 if (m_fontData
.GetEnableEffects())
442 wxBoxSizer
* itemBoxSizer20
= new wxBoxSizer(wxVERTICAL
);
443 itemGridSizer4
->Add(itemBoxSizer20
, 0, wxALIGN_LEFT
|wxALIGN_CENTER_VERTICAL
, 5);
444 wxCheckBox
* itemCheckBox21
= new wxCheckBox( this, wxID_FONT_UNDERLINE
, _("&Underline"), wxDefaultPosition
, wxDefaultSize
, 0 );
445 itemCheckBox21
->SetValue(false);
446 itemCheckBox21
->SetHelpText(_("Whether the font is underlined."));
448 itemCheckBox21
->SetToolTip(_("Whether the font is underlined."));
449 itemBoxSizer20
->Add(itemCheckBox21
, 0, wxALIGN_LEFT
|wxALL
, 5);
453 itemBoxSizer3
->Add(5, 5, 0, wxALIGN_CENTER_HORIZONTAL
|wxALL
, 5);
455 wxStaticText
* itemStaticText23
= new wxStaticText( this, wxID_STATIC
, _("Preview:"), wxDefaultPosition
, wxDefaultSize
, 0 );
456 itemBoxSizer3
->Add(itemStaticText23
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
, 5);
458 wxFontPreviewer
* itemWindow24
= new wxFontPreviewer( this );
459 m_previewer
= itemWindow24
;
460 itemWindow24
->SetHelpText(_("Shows the font preview."));
462 itemWindow24
->SetToolTip(_("Shows the font preview."));
463 itemBoxSizer3
->Add(itemWindow24
, 1, wxGROW
|wxALL
, 5);
465 wxBoxSizer
* itemBoxSizer25
= new wxBoxSizer(wxHORIZONTAL
);
466 itemBoxSizer3
->Add(itemBoxSizer25
, 0, wxGROW
, 5);
467 itemBoxSizer25
->Add(5, 5, 1, wxGROW
|wxALL
, 5);
470 wxButton
* itemButton28
= new wxButton( this, wxID_CANCEL
, _("&Cancel"), wxDefaultPosition
, wxDefaultSize
, 0 );
472 itemButton28
->SetToolTip(_("Click to cancel the font selection."));
473 itemBoxSizer25
->Add(itemButton28
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
475 wxButton
* itemButton27
= new wxButton( this, wxID_OK
, _("&OK"), wxDefaultPosition
, wxDefaultSize
, 0 );
476 itemButton27
->SetDefault();
477 itemButton27
->SetHelpText(_("Click to confirm the font selection."));
479 itemButton27
->SetToolTip(_("Click to confirm the font selection."));
480 itemBoxSizer25
->Add(itemButton27
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
482 wxButton
* itemButton27
= new wxButton( this, wxID_OK
, _("&OK"), wxDefaultPosition
, wxDefaultSize
, 0 );
483 itemButton27
->SetDefault();
484 itemButton27
->SetHelpText(_("Click to confirm the font selection."));
486 itemButton27
->SetToolTip(_("Click to confirm the font selection."));
487 itemBoxSizer25
->Add(itemButton27
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
489 wxButton
* itemButton28
= new wxButton( this, wxID_CANCEL
, _("&Cancel"), wxDefaultPosition
, wxDefaultSize
, 0 );
491 itemButton28
->SetToolTip(_("Click to cancel the font selection."));
492 itemBoxSizer25
->Add(itemButton28
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
495 m_familyChoice
= (wxChoice
*) FindWindow(wxID_FONT_FAMILY
);
496 m_styleChoice
= (wxChoice
*) FindWindow(wxID_FONT_STYLE
);
497 m_weightChoice
= (wxChoice
*) FindWindow(wxID_FONT_WEIGHT
);
498 m_colourChoice
= (wxChoice
*) FindWindow(wxID_FONT_COLOUR
);
499 m_underLineCheckBox
= (wxCheckBox
*) FindWindow(wxID_FONT_UNDERLINE
);
501 m_familyChoice
->SetStringSelection( wxFontFamilyIntToString(m_dialogFont
.GetFamily()) );
502 m_styleChoice
->SetStringSelection(wxFontStyleIntToString(m_dialogFont
.GetStyle()));
503 m_weightChoice
->SetStringSelection(wxFontWeightIntToString(m_dialogFont
.GetWeight()));
507 wxString
name(wxTheColourDatabase
->FindName(m_fontData
.GetColour()));
509 m_colourChoice
->SetStringSelection(wxT("BLACK"));
511 m_colourChoice
->SetStringSelection(name
);
514 if (m_underLineCheckBox
)
516 m_underLineCheckBox
->SetValue(m_dialogFont
.GetUnderlined());
519 #if USE_SPINCTRL_FOR_POINT_SIZE
520 spinCtrl
->SetValue(m_dialogFont
.GetPointSize());
522 m_pointSizeChoice
= (wxChoice
*) FindWindow(wxID_FONT_SIZE
);
523 m_pointSizeChoice
->SetSelection(m_dialogFont
.GetPointSize()-1);
526 GetSizer()->SetItemMinSize(m_previewer
, is_pda
? 100 : 430, is_pda
? 40 : 100);
527 GetSizer()->SetSizeHints(this);
528 GetSizer()->Fit(this);
535 #if !USE_SPINCTRL_FOR_POINT_SIZE
539 // Don't block events any more
544 void wxGenericFontDialog::InitializeFont()
546 int fontFamily
= wxSWISS
;
547 int fontWeight
= wxNORMAL
;
548 int fontStyle
= wxNORMAL
;
550 bool fontUnderline
= false;
552 if (m_fontData
.m_initialFont
.IsOk())
554 fontFamily
= m_fontData
.m_initialFont
.GetFamily();
555 fontWeight
= m_fontData
.m_initialFont
.GetWeight();
556 fontStyle
= m_fontData
.m_initialFont
.GetStyle();
557 fontSize
= m_fontData
.m_initialFont
.GetPointSize();
558 fontUnderline
= m_fontData
.m_initialFont
.GetUnderlined();
561 m_dialogFont
= wxFont(fontSize
, fontFamily
, fontStyle
,
562 fontWeight
, fontUnderline
);
565 m_previewer
->SetFont(m_dialogFont
);
568 void wxGenericFontDialog::OnChangeFont(wxCommandEvent
& WXUNUSED(event
))
573 void wxGenericFontDialog::DoChangeFont()
575 if (!m_useEvents
) return;
577 int fontFamily
= wxFontFamilyStringToInt(m_familyChoice
->GetStringSelection());
578 int fontWeight
= wxFontWeightStringToInt(m_weightChoice
->GetStringSelection());
579 int fontStyle
= wxFontStyleStringToInt(m_styleChoice
->GetStringSelection());
580 #if USE_SPINCTRL_FOR_POINT_SIZE
581 wxSpinCtrl
* fontSizeCtrl
= wxDynamicCast(FindWindow(wxID_FONT_SIZE
), wxSpinCtrl
);
582 int fontSize
= fontSizeCtrl
->GetValue();
584 int fontSize
= wxAtoi(m_pointSizeChoice
->GetStringSelection());
587 // Start with previous underline setting, we want to retain it even if we can't edit it
588 // m_dialogFont is always initialized because of the call to InitializeFont
589 int fontUnderline
= m_dialogFont
.GetUnderlined();
591 if (m_underLineCheckBox
)
593 fontUnderline
= m_underLineCheckBox
->GetValue();
596 m_dialogFont
= wxFont(fontSize
, fontFamily
, fontStyle
, fontWeight
, (fontUnderline
!= 0));
597 m_previewer
->SetFont(m_dialogFont
);
599 if ( m_colourChoice
)
601 if ( !m_colourChoice
->GetStringSelection().empty() )
603 wxColour col
= wxTheColourDatabase
->Find(m_colourChoice
->GetStringSelection());
606 m_fontData
.m_fontColour
= col
;
610 // Update color here so that we can also use the color originally passed in
611 // (EnableEffects may be false)
612 if (m_fontData
.m_fontColour
.IsOk())
613 m_previewer
->SetForegroundColour(m_fontData
.m_fontColour
);
615 m_previewer
->Refresh();
618 #if USE_SPINCTRL_FOR_POINT_SIZE
619 void wxGenericFontDialog::OnChangeSize(wxSpinEvent
& WXUNUSED(event
))