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"
38 #include "wx/cmndata.h"
40 #include "wx/fontdlg.h"
41 #include "wx/generic/fontdlgg.h"
42 #include "wx/settings.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 if ( !wxDialog::Create( parent
, wxID_ANY
, _T("Choose Font") , wxDefaultPosition
, wxDefaultSize
, wxDEFAULT_DIALOG_STYLE
,
196 wxFAIL_MSG( wxT("wxFontDialog creation failed") );
203 // sets initial font in preview area
204 wxCommandEvent dummy
;
210 int wxGenericFontDialog::ShowModal()
212 int ret
= wxDialog::ShowModal();
214 if (ret
!= wxID_CANCEL
)
216 m_fontData
.m_chosenFont
= dialogFont
;
222 // This should be application-settable
223 static bool ShowToolTips() { return false; }
225 void wxGenericFontDialog::CreateWidgets()
228 *families
= new wxString
[6],
229 *styles
= new wxString
[3],
230 *weights
= new wxString
[3];
231 families
[0] = _("Roman");
232 families
[1] = _("Decorative");
233 families
[2] = _("Modern");
234 families
[3] = _("Script");
235 families
[4] = _("Swiss" );
236 families
[5] = _("Teletype" );
237 styles
[0] = _("Normal");
238 styles
[1] = _("Italic");
239 styles
[2] = _("Slant");
240 weights
[0] = _("Normal");
241 weights
[1] = _("Light");
242 weights
[2] = _("Bold");
244 #if !USE_SPINCTRL_FOR_POINT_SIZE
245 wxString
*pointSizes
= new wxString
[40];
247 for ( i
= 0; i
< 40; i
++)
250 wxSprintf(buf
, wxT("%d"), i
+ 1);
257 bool is_pda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
261 noCols
= 2; noRows
= 3;
265 noCols
= 3; noRows
= 2;
268 wxBoxSizer
* itemBoxSizer2
= new wxBoxSizer(wxVERTICAL
);
269 this->SetSizer(itemBoxSizer2
);
270 this->SetAutoLayout(TRUE
);
272 wxBoxSizer
* itemBoxSizer3
= new wxBoxSizer(wxVERTICAL
);
273 itemBoxSizer2
->Add(itemBoxSizer3
, 1, wxGROW
|wxALL
, 5);
275 wxFlexGridSizer
* itemGridSizer4
= new wxFlexGridSizer(noRows
, noCols
, 0, 0);
276 itemBoxSizer3
->Add(itemGridSizer4
, 0, wxGROW
, 5);
278 wxBoxSizer
* itemBoxSizer5
= new wxBoxSizer(wxVERTICAL
);
279 itemGridSizer4
->Add(itemBoxSizer5
, 0, wxALIGN_CENTER_HORIZONTAL
|wxGROW
, 5);
280 wxStaticText
* itemStaticText6
= new wxStaticText( this, wxID_STATIC
, _("&Font family:"), wxDefaultPosition
, wxDefaultSize
, 0 );
281 itemBoxSizer5
->Add(itemStaticText6
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
|wxADJUST_MINSIZE
, 5);
283 wxChoice
* itemChoice7
= new wxChoice( this, wxID_FONT_FAMILY
, wxDefaultPosition
, wxDefaultSize
, 5, families
, 0 );
284 itemChoice7
->SetHelpText(_("The font family."));
286 itemChoice7
->SetToolTip(_("The font family."));
287 itemBoxSizer5
->Add(itemChoice7
, 0, wxALIGN_LEFT
|wxALL
, 5);
289 wxBoxSizer
* itemBoxSizer8
= new wxBoxSizer(wxVERTICAL
);
290 itemGridSizer4
->Add(itemBoxSizer8
, 0, wxALIGN_CENTER_HORIZONTAL
|wxGROW
, 5);
291 wxStaticText
* itemStaticText9
= new wxStaticText( this, wxID_STATIC
, _("&Style:"), wxDefaultPosition
, wxDefaultSize
, 0 );
292 itemBoxSizer8
->Add(itemStaticText9
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
|wxADJUST_MINSIZE
, 5);
294 wxChoice
* itemChoice10
= new wxChoice( this, wxID_FONT_STYLE
, wxDefaultPosition
, wxDefaultSize
, 3, styles
, 0 );
295 itemChoice10
->SetHelpText(_("The font style."));
297 itemChoice10
->SetToolTip(_("The font style."));
298 itemBoxSizer8
->Add(itemChoice10
, 0, wxALIGN_LEFT
|wxALL
, 5);
300 wxBoxSizer
* itemBoxSizer11
= new wxBoxSizer(wxVERTICAL
);
301 itemGridSizer4
->Add(itemBoxSizer11
, 0, wxALIGN_CENTER_HORIZONTAL
|wxGROW
, 5);
302 wxStaticText
* itemStaticText12
= new wxStaticText( this, wxID_STATIC
, _("&Weight:"), wxDefaultPosition
, wxDefaultSize
, 0 );
303 itemBoxSizer11
->Add(itemStaticText12
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
|wxADJUST_MINSIZE
, 5);
305 wxChoice
* itemChoice13
= new wxChoice( this, wxID_FONT_WEIGHT
, wxDefaultPosition
, wxDefaultSize
, 3, weights
, 0 );
306 itemChoice13
->SetHelpText(_("The font weight."));
308 itemChoice13
->SetToolTip(_("The font weight."));
309 itemBoxSizer11
->Add(itemChoice13
, 0, wxALIGN_LEFT
|wxALL
, 5);
311 wxBoxSizer
* itemBoxSizer14
= new wxBoxSizer(wxVERTICAL
);
312 itemGridSizer4
->Add(itemBoxSizer14
, 0, wxALIGN_CENTER_HORIZONTAL
|wxGROW
, 5);
313 if (m_fontData
.GetEnableEffects())
315 wxStaticText
* itemStaticText15
= new wxStaticText( this, wxID_STATIC
, _("C&olour:"), wxDefaultPosition
, wxDefaultSize
, 0 );
316 itemBoxSizer14
->Add(itemStaticText15
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
|wxADJUST_MINSIZE
, 5);
318 wxSize colourSize
= wxDefaultSize
;
322 wxChoice
* itemChoice16
= new wxChoice( this, wxID_FONT_COLOUR
, wxDefaultPosition
, colourSize
, NUM_COLS
, wxColourDialogNames
, 0 );
323 itemChoice16
->SetHelpText(_("The font colour."));
325 itemChoice16
->SetToolTip(_("The font colour."));
326 itemBoxSizer14
->Add(itemChoice16
, 0, wxALIGN_LEFT
|wxALL
, 5);
329 wxBoxSizer
* itemBoxSizer17
= new wxBoxSizer(wxVERTICAL
);
330 itemGridSizer4
->Add(itemBoxSizer17
, 0, wxALIGN_CENTER_HORIZONTAL
|wxGROW
, 5);
331 wxStaticText
* itemStaticText18
= new wxStaticText( this, wxID_STATIC
, _("&Point size:"), wxDefaultPosition
, wxDefaultSize
, 0 );
332 itemBoxSizer17
->Add(itemStaticText18
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
|wxADJUST_MINSIZE
, 5);
334 #if USE_SPINCTRL_FOR_POINT_SIZE
335 wxSpinCtrl
* spinCtrl
= new wxSpinCtrl(this, wxID_FONT_SIZE
, wxT("12"), wxDefaultPosition
, wxSize(80, -1), wxSP_ARROW_KEYS
, 1, 500, 12);
336 spinCtrl
->SetHelpText(_("The font point size."));
338 spinCtrl
->SetToolTip(_("The font point size."));
340 itemBoxSizer17
->Add(spinCtrl
, 0, wxALIGN_LEFT
|wxALL
, 5);
342 wxChoice
* itemChoice19
= new wxChoice( this, wxID_FONT_SIZE
, wxDefaultPosition
, wxDefaultSize
, 40, pointSizes
, 0 );
343 itemChoice19
->SetHelpText(_("The font point size."));
345 itemChoice19
->SetToolTip(_("The font point size."));
346 itemBoxSizer17
->Add(itemChoice19
, 0, wxALIGN_LEFT
|wxALL
, 5);
349 if (m_fontData
.GetEnableEffects())
351 wxBoxSizer
* itemBoxSizer20
= new wxBoxSizer(wxVERTICAL
);
352 itemGridSizer4
->Add(itemBoxSizer20
, 0, wxALIGN_LEFT
|wxALIGN_CENTER_VERTICAL
, 5);
353 wxCheckBox
* itemCheckBox21
= new wxCheckBox( this, wxID_FONT_UNDERLINE
, _("&Underline"), wxDefaultPosition
, wxDefaultSize
, 0 );
354 itemCheckBox21
->SetValue(FALSE
);
355 itemCheckBox21
->SetHelpText(_("Whether the font is underlined."));
357 itemCheckBox21
->SetToolTip(_("Whether the font is underlined."));
358 itemBoxSizer20
->Add(itemCheckBox21
, 0, wxALIGN_LEFT
|wxALL
, 5);
362 itemBoxSizer3
->Add(5, 5, 0, wxALIGN_CENTER_HORIZONTAL
|wxALL
, 5);
364 wxStaticText
* itemStaticText23
= new wxStaticText( this, wxID_STATIC
, _("Preview:"), wxDefaultPosition
, wxDefaultSize
, 0 );
365 itemBoxSizer3
->Add(itemStaticText23
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
|wxADJUST_MINSIZE
, 5);
367 wxFontPreviewer
* itemWindow24
= new wxFontPreviewer( this );
368 m_previewer
= itemWindow24
;
369 itemWindow24
->SetHelpText(_("Shows the font preview."));
371 itemWindow24
->SetToolTip(_("Shows the font preview."));
372 itemBoxSizer3
->Add(itemWindow24
, 1, wxGROW
|wxALL
, 5);
374 wxBoxSizer
* itemBoxSizer25
= new wxBoxSizer(wxHORIZONTAL
);
375 itemBoxSizer3
->Add(itemBoxSizer25
, 0, wxGROW
, 5);
376 itemBoxSizer25
->Add(5, 5, 1, wxGROW
|wxALL
, 5);
379 wxButton
* itemButton28
= new wxButton( this, wxID_CANCEL
, _("&Cancel"), wxDefaultPosition
, wxDefaultSize
, 0 );
381 itemButton28
->SetToolTip(_("Click to cancel the font selection."));
382 itemBoxSizer25
->Add(itemButton28
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
384 wxButton
* itemButton27
= new wxButton( this, wxID_OK
, _("&OK"), wxDefaultPosition
, wxDefaultSize
, 0 );
385 itemButton27
->SetDefault();
386 itemButton27
->SetHelpText(_("Click to confirm the font selection."));
388 itemButton27
->SetToolTip(_("Click to confirm the font selection."));
389 itemBoxSizer25
->Add(itemButton27
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
391 wxButton
* itemButton27
= new wxButton( this, wxID_OK
, _("&OK"), wxDefaultPosition
, wxDefaultSize
, 0 );
392 itemButton27
->SetDefault();
393 itemButton27
->SetHelpText(_("Click to confirm the font selection."));
395 itemButton27
->SetToolTip(_("Click to confirm the font selection."));
396 itemBoxSizer25
->Add(itemButton27
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
398 wxButton
* itemButton28
= new wxButton( this, wxID_CANCEL
, _("&Cancel"), wxDefaultPosition
, wxDefaultSize
, 0 );
400 itemButton28
->SetToolTip(_("Click to cancel the font selection."));
401 itemBoxSizer25
->Add(itemButton28
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
404 familyChoice
= (wxChoice
*) FindWindow(wxID_FONT_FAMILY
);
405 styleChoice
= (wxChoice
*) FindWindow(wxID_FONT_STYLE
);
406 weightChoice
= (wxChoice
*) FindWindow(wxID_FONT_WEIGHT
);
407 colourChoice
= (wxChoice
*) FindWindow(wxID_FONT_COLOUR
);
408 underLineCheckBox
= (wxCheckBox
*) FindWindow(wxID_FONT_UNDERLINE
);
410 familyChoice
->SetStringSelection( wxFontFamilyIntToString(dialogFont
.GetFamily()) );
411 styleChoice
->SetStringSelection(wxFontStyleIntToString(dialogFont
.GetStyle()));
412 weightChoice
->SetStringSelection(wxFontWeightIntToString(dialogFont
.GetWeight()));
416 wxString
name(wxTheColourDatabase
->FindName(m_fontData
.GetColour()));
418 colourChoice
->SetStringSelection(name
);
420 colourChoice
->SetStringSelection(wxT("BLACK"));
423 if (underLineCheckBox
)
425 underLineCheckBox
->SetValue(dialogFont
.GetUnderlined());
428 #if USE_SPINCTRL_FOR_POINT_SIZE
429 spinCtrl
->SetValue(dialogFont
.GetPointSize());
431 pointSizeChoice
= (wxChoice
*) FindWindow(wxID_FONT_SIZE
);
432 pointSizeChoice
->SetSelection(dialogFont
.GetPointSize()-1);
435 #if !defined(__SMARTPHONE__) && !defined(__POCKETPC__)
436 GetSizer()->SetItemMinSize(m_previewer
, is_pda
? 100 : 430, is_pda
? 40 : 100);
437 GetSizer()->SetSizeHints(this);
438 GetSizer()->Fit(this);
446 #if !USE_SPINCTRL_FOR_POINT_SIZE
450 // Don't block events any more
455 void wxGenericFontDialog::InitializeFont()
457 int fontFamily
= wxSWISS
;
458 int fontWeight
= wxNORMAL
;
459 int fontStyle
= wxNORMAL
;
461 bool fontUnderline
= false;
463 if (m_fontData
.m_initialFont
.Ok())
465 fontFamily
= m_fontData
.m_initialFont
.GetFamily();
466 fontWeight
= m_fontData
.m_initialFont
.GetWeight();
467 fontStyle
= m_fontData
.m_initialFont
.GetStyle();
468 fontSize
= m_fontData
.m_initialFont
.GetPointSize();
469 fontUnderline
= m_fontData
.m_initialFont
.GetUnderlined();
472 dialogFont
= wxFont(fontSize
, fontFamily
, fontStyle
,
473 fontWeight
, fontUnderline
);
476 m_previewer
->SetFont(dialogFont
);
479 void wxGenericFontDialog::OnChangeFont(wxCommandEvent
& WXUNUSED(event
))
481 if (!m_useEvents
) return;
483 int fontFamily
= wxFontFamilyStringToInt(WXSTRINGCAST familyChoice
->GetStringSelection());
484 int fontWeight
= wxFontWeightStringToInt(WXSTRINGCAST weightChoice
->GetStringSelection());
485 int fontStyle
= wxFontStyleStringToInt(WXSTRINGCAST styleChoice
->GetStringSelection());
486 #if USE_SPINCTRL_FOR_POINT_SIZE
487 wxSpinCtrl
* fontSizeCtrl
= wxDynamicCast(FindWindow(wxID_FONT_SIZE
), wxSpinCtrl
);
488 int fontSize
= fontSizeCtrl
->GetValue();
490 int fontSize
= wxAtoi(pointSizeChoice
->GetStringSelection());
493 // Start with previous underline setting, we want to retain it even if we can't edit it
494 // dialogFont is always initialized because of the call to InitializeFont
495 int fontUnderline
= dialogFont
.GetUnderlined();
497 if (underLineCheckBox
)
499 fontUnderline
= underLineCheckBox
->GetValue();
502 dialogFont
= wxFont(fontSize
, fontFamily
, fontStyle
, fontWeight
, (fontUnderline
!= 0));
503 m_previewer
->SetFont(dialogFont
);
507 if ( !colourChoice
->GetStringSelection().empty() )
509 wxColour col
= wxTheColourDatabase
->Find(colourChoice
->GetStringSelection());
512 m_fontData
.m_fontColour
= col
;
516 // Update color here so that we can also use the color originally passed in
517 // (EnableEffects may be false)
518 if (m_fontData
.m_fontColour
.Ok())
519 m_previewer
->SetForegroundColour(m_fontData
.m_fontColour
);
521 m_previewer
->Refresh();
524 #if USE_SPINCTRL_FOR_POINT_SIZE
525 void wxGenericFontDialog::OnChangeSize(wxSpinEvent
& WXUNUSED(event
))
527 wxCommandEvent cmdEvent
;
528 OnChangeFont(cmdEvent
);
532 const wxChar
*wxFontWeightIntToString(int weight
)
542 return wxT("Normal");
546 const wxChar
*wxFontStyleIntToString(int style
)
551 return wxT("Italic");
556 return wxT("Normal");
560 const wxChar
*wxFontFamilyIntToString(int family
)
567 return wxT("Decorative");
569 return wxT("Modern");
571 return wxT("Script");
573 return wxT("Teletype");
580 int wxFontFamilyStringToInt(wxChar
*family
)
585 if (wxStrcmp(family
, wxT("Roman")) == 0)
587 else if (wxStrcmp(family
, wxT("Decorative")) == 0)
589 else if (wxStrcmp(family
, wxT("Modern")) == 0)
591 else if (wxStrcmp(family
, wxT("Script")) == 0)
593 else if (wxStrcmp(family
, wxT("Teletype")) == 0)
598 int wxFontStyleStringToInt(wxChar
*style
)
602 if (wxStrcmp(style
, wxT("Italic")) == 0)
604 else if (wxStrcmp(style
, wxT("Slant")) == 0)
610 int wxFontWeightStringToInt(wxChar
*weight
)
614 if (wxStrcmp(weight
, wxT("Bold")) == 0)
616 else if (wxStrcmp(weight
, wxT("Light")) == 0)