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__))
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"
34 #include "wx/cmndata.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 // Calculate vertical centre
84 dc
.GetTextExtent( wxT("X"), &w
, &h
);
85 dc
.SetTextForeground(GetForegroundColour());
86 dc
.SetClippingRegion(2, 2, size
.x
-4, size
.y
-4);
87 dc
.DrawText(_("ABCDEFGabcdefg12345"),
89 dc
.DestroyClippingRegion();
93 //-----------------------------------------------------------------------------
94 // wxGenericFontDialog
95 //-----------------------------------------------------------------------------
97 IMPLEMENT_DYNAMIC_CLASS(wxGenericFontDialog
, wxDialog
)
99 BEGIN_EVENT_TABLE(wxGenericFontDialog
, wxDialog
)
100 EVT_CHECKBOX(wxID_FONT_UNDERLINE
, wxGenericFontDialog::OnChangeFont
)
101 EVT_CHOICE(wxID_FONT_STYLE
, wxGenericFontDialog::OnChangeFont
)
102 EVT_CHOICE(wxID_FONT_WEIGHT
, wxGenericFontDialog::OnChangeFont
)
103 EVT_CHOICE(wxID_FONT_FAMILY
, wxGenericFontDialog::OnChangeFont
)
104 EVT_CHOICE(wxID_FONT_COLOUR
, wxGenericFontDialog::OnChangeFont
)
105 #if USE_SPINCTRL_FOR_POINT_SIZE
106 EVT_SPINCTRL(wxID_FONT_SIZE
, wxGenericFontDialog::OnChangeSize
)
107 EVT_TEXT(wxID_FONT_SIZE
, wxGenericFontDialog::OnChangeFont
)
109 EVT_CHOICE(wxID_FONT_SIZE
, wxGenericFontDialog::OnChangeFont
)
111 EVT_CLOSE(wxGenericFontDialog::OnCloseWindow
)
116 static wxString wxColourDialogNames
[NUM_COLS
]={wxT("ORANGE"),
122 wxT("MEDIUM VIOLET RED"),
127 wxT("MEDIUM SPRING GREEN"),
130 wxT("LIGHT STEEL BLUE"),
132 wxT("LIGHT MAGENTA"),
150 wxT("MEDIUM VIOLET RED"),
154 wxT("MEDIUM SEA GREEN"),
156 wxT("MIDNIGHT BLUE"),
162 wxT("MEDIUM FOREST GREEN"),
167 wxT("MEDIUM SLATE BLUE"),
172 * Generic wxFontDialog
175 void wxGenericFontDialog::Init()
182 wxGenericFontDialog::~wxGenericFontDialog()
186 void wxGenericFontDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
188 EndModal(wxID_CANCEL
);
191 bool wxGenericFontDialog::DoCreate(wxWindow
*parent
)
193 parent
= GetParentForModalDialog(parent
);
195 if ( !wxDialog::Create( parent
, wxID_ANY
, _T("Choose Font") ,
196 wxDefaultPosition
, wxDefaultSize
, wxDEFAULT_DIALOG_STYLE
,
199 wxFAIL_MSG( wxT("wxFontDialog creation failed") );
206 // sets initial font in preview area
212 int wxGenericFontDialog::ShowModal()
214 int ret
= wxDialog::ShowModal();
216 if (ret
!= wxID_CANCEL
)
218 m_fontData
.m_chosenFont
= m_dialogFont
;
224 // This should be application-settable
225 static bool ShowToolTips() { return false; }
227 void wxGenericFontDialog::CreateWidgets()
229 wxString
*families
= new wxString
[6],
230 *styles
= new wxString
[3],
231 *weights
= new wxString
[3];
232 families
[0] = _("Roman");
233 families
[1] = _("Decorative");
234 families
[2] = _("Modern");
235 families
[3] = _("Script");
236 families
[4] = _("Swiss" );
237 families
[5] = _("Teletype" );
238 styles
[0] = _("Normal");
239 styles
[1] = _("Italic");
240 styles
[2] = _("Slant");
241 weights
[0] = _("Normal");
242 weights
[1] = _("Light");
243 weights
[2] = _("Bold");
245 #if !USE_SPINCTRL_FOR_POINT_SIZE
246 wxString
*pointSizes
= new wxString
[40];
248 for ( i
= 0; i
< 40; i
++)
251 wxSprintf(buf
, wxT("%d"), i
+ 1);
258 bool is_pda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
262 noCols
= 2; noRows
= 3;
266 noCols
= 3; noRows
= 2;
269 wxBoxSizer
* itemBoxSizer2
= new wxBoxSizer(wxVERTICAL
);
270 this->SetSizer(itemBoxSizer2
);
271 this->SetAutoLayout(true);
273 wxBoxSizer
* itemBoxSizer3
= new wxBoxSizer(wxVERTICAL
);
274 itemBoxSizer2
->Add(itemBoxSizer3
, 1, wxGROW
|wxALL
, 5);
276 wxFlexGridSizer
* itemGridSizer4
= new wxFlexGridSizer(noRows
, noCols
, 0, 0);
277 itemBoxSizer3
->Add(itemGridSizer4
, 0, wxGROW
, 5);
279 wxBoxSizer
* itemBoxSizer5
= new wxBoxSizer(wxVERTICAL
);
280 itemGridSizer4
->Add(itemBoxSizer5
, 0, wxALIGN_CENTER_HORIZONTAL
|wxGROW
, 5);
281 wxStaticText
* itemStaticText6
= new wxStaticText( this, wxID_STATIC
, _("&Font family:"), wxDefaultPosition
, wxDefaultSize
, 0 );
282 itemBoxSizer5
->Add(itemStaticText6
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
, 5);
284 wxChoice
* itemChoice7
= new wxChoice( this, wxID_FONT_FAMILY
, wxDefaultPosition
, wxDefaultSize
, 5, families
, 0 );
285 itemChoice7
->SetHelpText(_("The font family."));
287 itemChoice7
->SetToolTip(_("The font family."));
288 itemBoxSizer5
->Add(itemChoice7
, 0, wxALIGN_LEFT
|wxALL
, 5);
290 wxBoxSizer
* itemBoxSizer8
= new wxBoxSizer(wxVERTICAL
);
291 itemGridSizer4
->Add(itemBoxSizer8
, 0, wxALIGN_CENTER_HORIZONTAL
|wxGROW
, 5);
292 wxStaticText
* itemStaticText9
= new wxStaticText( this, wxID_STATIC
, _("&Style:"), wxDefaultPosition
, wxDefaultSize
, 0 );
293 itemBoxSizer8
->Add(itemStaticText9
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
, 5);
295 wxChoice
* itemChoice10
= new wxChoice( this, wxID_FONT_STYLE
, wxDefaultPosition
, wxDefaultSize
, 3, styles
, 0 );
296 itemChoice10
->SetHelpText(_("The font style."));
298 itemChoice10
->SetToolTip(_("The font style."));
299 itemBoxSizer8
->Add(itemChoice10
, 0, wxALIGN_LEFT
|wxALL
, 5);
301 wxBoxSizer
* itemBoxSizer11
= new wxBoxSizer(wxVERTICAL
);
302 itemGridSizer4
->Add(itemBoxSizer11
, 0, wxALIGN_CENTER_HORIZONTAL
|wxGROW
, 5);
303 wxStaticText
* itemStaticText12
= new wxStaticText( this, wxID_STATIC
, _("&Weight:"), wxDefaultPosition
, wxDefaultSize
, 0 );
304 itemBoxSizer11
->Add(itemStaticText12
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
, 5);
306 wxChoice
* itemChoice13
= new wxChoice( this, wxID_FONT_WEIGHT
, wxDefaultPosition
, wxDefaultSize
, 3, weights
, 0 );
307 itemChoice13
->SetHelpText(_("The font weight."));
309 itemChoice13
->SetToolTip(_("The font weight."));
310 itemBoxSizer11
->Add(itemChoice13
, 0, wxALIGN_LEFT
|wxALL
, 5);
312 wxBoxSizer
* itemBoxSizer14
= new wxBoxSizer(wxVERTICAL
);
313 itemGridSizer4
->Add(itemBoxSizer14
, 0, wxALIGN_CENTER_HORIZONTAL
|wxGROW
, 5);
314 if (m_fontData
.GetEnableEffects())
316 wxStaticText
* itemStaticText15
= new wxStaticText( this, wxID_STATIC
, _("C&olour:"), wxDefaultPosition
, wxDefaultSize
, 0 );
317 itemBoxSizer14
->Add(itemStaticText15
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
, 5);
319 wxSize colourSize
= wxDefaultSize
;
323 wxChoice
* itemChoice16
= new wxChoice( this, wxID_FONT_COLOUR
, wxDefaultPosition
, colourSize
, NUM_COLS
, wxColourDialogNames
, 0 );
324 itemChoice16
->SetHelpText(_("The font colour."));
326 itemChoice16
->SetToolTip(_("The font colour."));
327 itemBoxSizer14
->Add(itemChoice16
, 0, wxALIGN_LEFT
|wxALL
, 5);
330 wxBoxSizer
* itemBoxSizer17
= new wxBoxSizer(wxVERTICAL
);
331 itemGridSizer4
->Add(itemBoxSizer17
, 0, wxALIGN_CENTER_HORIZONTAL
|wxGROW
, 5);
332 wxStaticText
* itemStaticText18
= new wxStaticText( this, wxID_STATIC
, _("&Point size:"), wxDefaultPosition
, wxDefaultSize
, 0 );
333 itemBoxSizer17
->Add(itemStaticText18
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
, 5);
335 #if USE_SPINCTRL_FOR_POINT_SIZE
336 wxSpinCtrl
* spinCtrl
= new wxSpinCtrl(this, wxID_FONT_SIZE
, wxT("12"), wxDefaultPosition
, wxSize(80, wxDefaultCoord
), wxSP_ARROW_KEYS
, 1, 500, 12);
337 spinCtrl
->SetHelpText(_("The font point size."));
339 spinCtrl
->SetToolTip(_("The font point size."));
341 itemBoxSizer17
->Add(spinCtrl
, 0, wxALIGN_LEFT
|wxALL
, 5);
343 wxChoice
* itemChoice19
= new wxChoice( this, wxID_FONT_SIZE
, wxDefaultPosition
, wxDefaultSize
, 40, pointSizes
, 0 );
344 itemChoice19
->SetHelpText(_("The font point size."));
346 itemChoice19
->SetToolTip(_("The font point size."));
347 itemBoxSizer17
->Add(itemChoice19
, 0, wxALIGN_LEFT
|wxALL
, 5);
350 if (m_fontData
.GetEnableEffects())
352 wxBoxSizer
* itemBoxSizer20
= new wxBoxSizer(wxVERTICAL
);
353 itemGridSizer4
->Add(itemBoxSizer20
, 0, wxALIGN_LEFT
|wxALIGN_CENTER_VERTICAL
, 5);
354 wxCheckBox
* itemCheckBox21
= new wxCheckBox( this, wxID_FONT_UNDERLINE
, _("&Underline"), wxDefaultPosition
, wxDefaultSize
, 0 );
355 itemCheckBox21
->SetValue(false);
356 itemCheckBox21
->SetHelpText(_("Whether the font is underlined."));
358 itemCheckBox21
->SetToolTip(_("Whether the font is underlined."));
359 itemBoxSizer20
->Add(itemCheckBox21
, 0, wxALIGN_LEFT
|wxALL
, 5);
363 itemBoxSizer3
->Add(5, 5, 0, wxALIGN_CENTER_HORIZONTAL
|wxALL
, 5);
365 wxStaticText
* itemStaticText23
= new wxStaticText( this, wxID_STATIC
, _("Preview:"), wxDefaultPosition
, wxDefaultSize
, 0 );
366 itemBoxSizer3
->Add(itemStaticText23
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
, 5);
368 wxFontPreviewer
* itemWindow24
= new wxFontPreviewer( this );
369 m_previewer
= itemWindow24
;
370 itemWindow24
->SetHelpText(_("Shows the font preview."));
372 itemWindow24
->SetToolTip(_("Shows the font preview."));
373 itemBoxSizer3
->Add(itemWindow24
, 1, wxGROW
|wxALL
, 5);
375 wxBoxSizer
* itemBoxSizer25
= new wxBoxSizer(wxHORIZONTAL
);
376 itemBoxSizer3
->Add(itemBoxSizer25
, 0, wxGROW
, 5);
377 itemBoxSizer25
->Add(5, 5, 1, wxGROW
|wxALL
, 5);
380 wxButton
* itemButton28
= new wxButton( this, wxID_CANCEL
, _("&Cancel"), wxDefaultPosition
, wxDefaultSize
, 0 );
382 itemButton28
->SetToolTip(_("Click to cancel the font selection."));
383 itemBoxSizer25
->Add(itemButton28
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
385 wxButton
* itemButton27
= new wxButton( this, wxID_OK
, _("&OK"), wxDefaultPosition
, wxDefaultSize
, 0 );
386 itemButton27
->SetDefault();
387 itemButton27
->SetHelpText(_("Click to confirm the font selection."));
389 itemButton27
->SetToolTip(_("Click to confirm the font selection."));
390 itemBoxSizer25
->Add(itemButton27
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
392 wxButton
* itemButton27
= new wxButton( this, wxID_OK
, _("&OK"), wxDefaultPosition
, wxDefaultSize
, 0 );
393 itemButton27
->SetDefault();
394 itemButton27
->SetHelpText(_("Click to confirm the font selection."));
396 itemButton27
->SetToolTip(_("Click to confirm the font selection."));
397 itemBoxSizer25
->Add(itemButton27
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
399 wxButton
* itemButton28
= new wxButton( this, wxID_CANCEL
, _("&Cancel"), wxDefaultPosition
, wxDefaultSize
, 0 );
401 itemButton28
->SetToolTip(_("Click to cancel the font selection."));
402 itemBoxSizer25
->Add(itemButton28
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
405 m_familyChoice
= (wxChoice
*) FindWindow(wxID_FONT_FAMILY
);
406 m_styleChoice
= (wxChoice
*) FindWindow(wxID_FONT_STYLE
);
407 m_weightChoice
= (wxChoice
*) FindWindow(wxID_FONT_WEIGHT
);
408 m_colourChoice
= (wxChoice
*) FindWindow(wxID_FONT_COLOUR
);
409 m_underLineCheckBox
= (wxCheckBox
*) FindWindow(wxID_FONT_UNDERLINE
);
411 m_familyChoice
->SetStringSelection( wxFontFamilyIntToString(m_dialogFont
.GetFamily()) );
412 m_styleChoice
->SetStringSelection(wxFontStyleIntToString(m_dialogFont
.GetStyle()));
413 m_weightChoice
->SetStringSelection(wxFontWeightIntToString(m_dialogFont
.GetWeight()));
417 wxString
name(wxTheColourDatabase
->FindName(m_fontData
.GetColour()));
419 m_colourChoice
->SetStringSelection(name
);
421 m_colourChoice
->SetStringSelection(wxT("BLACK"));
424 if (m_underLineCheckBox
)
426 m_underLineCheckBox
->SetValue(m_dialogFont
.GetUnderlined());
429 #if USE_SPINCTRL_FOR_POINT_SIZE
430 spinCtrl
->SetValue(m_dialogFont
.GetPointSize());
432 m_pointSizeChoice
= (wxChoice
*) FindWindow(wxID_FONT_SIZE
);
433 m_pointSizeChoice
->SetSelection(m_dialogFont
.GetPointSize()-1);
436 GetSizer()->SetItemMinSize(m_previewer
, is_pda
? 100 : 430, is_pda
? 40 : 100);
437 GetSizer()->SetSizeHints(this);
438 GetSizer()->Fit(this);
445 #if !USE_SPINCTRL_FOR_POINT_SIZE
449 // Don't block events any more
454 void wxGenericFontDialog::InitializeFont()
456 int fontFamily
= wxSWISS
;
457 int fontWeight
= wxNORMAL
;
458 int fontStyle
= wxNORMAL
;
460 bool fontUnderline
= false;
462 if (m_fontData
.m_initialFont
.Ok())
464 fontFamily
= m_fontData
.m_initialFont
.GetFamily();
465 fontWeight
= m_fontData
.m_initialFont
.GetWeight();
466 fontStyle
= m_fontData
.m_initialFont
.GetStyle();
467 fontSize
= m_fontData
.m_initialFont
.GetPointSize();
468 fontUnderline
= m_fontData
.m_initialFont
.GetUnderlined();
471 m_dialogFont
= wxFont(fontSize
, fontFamily
, fontStyle
,
472 fontWeight
, fontUnderline
);
475 m_previewer
->SetFont(m_dialogFont
);
478 void wxGenericFontDialog::OnChangeFont(wxCommandEvent
& WXUNUSED(event
))
483 void wxGenericFontDialog::DoChangeFont()
485 if (!m_useEvents
) return;
487 int fontFamily
= wxFontFamilyStringToInt(WXSTRINGCAST m_familyChoice
->GetStringSelection());
488 int fontWeight
= wxFontWeightStringToInt(WXSTRINGCAST m_weightChoice
->GetStringSelection());
489 int fontStyle
= wxFontStyleStringToInt(WXSTRINGCAST m_styleChoice
->GetStringSelection());
490 #if USE_SPINCTRL_FOR_POINT_SIZE
491 wxSpinCtrl
* fontSizeCtrl
= wxDynamicCast(FindWindow(wxID_FONT_SIZE
), wxSpinCtrl
);
492 int fontSize
= fontSizeCtrl
->GetValue();
494 int fontSize
= wxAtoi(m_pointSizeChoice
->GetStringSelection());
497 // Start with previous underline setting, we want to retain it even if we can't edit it
498 // m_dialogFont is always initialized because of the call to InitializeFont
499 int fontUnderline
= m_dialogFont
.GetUnderlined();
501 if (m_underLineCheckBox
)
503 fontUnderline
= m_underLineCheckBox
->GetValue();
506 m_dialogFont
= wxFont(fontSize
, fontFamily
, fontStyle
, fontWeight
, (fontUnderline
!= 0));
507 m_previewer
->SetFont(m_dialogFont
);
509 if ( m_colourChoice
)
511 if ( !m_colourChoice
->GetStringSelection().empty() )
513 wxColour col
= wxTheColourDatabase
->Find(m_colourChoice
->GetStringSelection());
516 m_fontData
.m_fontColour
= col
;
520 // Update color here so that we can also use the color originally passed in
521 // (EnableEffects may be false)
522 if (m_fontData
.m_fontColour
.Ok())
523 m_previewer
->SetForegroundColour(m_fontData
.m_fontColour
);
525 m_previewer
->Refresh();
528 #if USE_SPINCTRL_FOR_POINT_SIZE
529 void wxGenericFontDialog::OnChangeSize(wxSpinEvent
& WXUNUSED(event
))
535 const wxChar
*wxFontWeightIntToString(int weight
)
545 return wxT("Normal");
549 const wxChar
*wxFontStyleIntToString(int style
)
554 return wxT("Italic");
559 return wxT("Normal");
563 const wxChar
*wxFontFamilyIntToString(int family
)
570 return wxT("Decorative");
572 return wxT("Modern");
574 return wxT("Script");
576 return wxT("Teletype");
583 int wxFontFamilyStringToInt(wxChar
*family
)
588 if (wxStrcmp(family
, wxT("Roman")) == 0)
590 else if (wxStrcmp(family
, wxT("Decorative")) == 0)
592 else if (wxStrcmp(family
, wxT("Modern")) == 0)
594 else if (wxStrcmp(family
, wxT("Script")) == 0)
596 else if (wxStrcmp(family
, wxT("Teletype")) == 0)
601 int wxFontStyleStringToInt(wxChar
*style
)
605 if (wxStrcmp(style
, wxT("Italic")) == 0)
607 else if (wxStrcmp(style
, wxT("Slant")) == 0)
613 int wxFontWeightStringToInt(wxChar
*weight
)
617 if (wxStrcmp(weight
, wxT("Bold")) == 0)
619 else if (wxStrcmp(weight
, wxT("Light")) == 0)