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
209 int wxGenericFontDialog::ShowModal()
211 int ret
= wxDialog::ShowModal();
213 if (ret
!= wxID_CANCEL
)
215 m_fontData
.m_chosenFont
= m_dialogFont
;
221 // This should be application-settable
222 static bool ShowToolTips() { return false; }
224 void wxGenericFontDialog::CreateWidgets()
226 wxString
*families
= new wxString
[6],
227 *styles
= new wxString
[3],
228 *weights
= new wxString
[3];
229 families
[0] = _("Roman");
230 families
[1] = _("Decorative");
231 families
[2] = _("Modern");
232 families
[3] = _("Script");
233 families
[4] = _("Swiss" );
234 families
[5] = _("Teletype" );
235 styles
[0] = _("Normal");
236 styles
[1] = _("Italic");
237 styles
[2] = _("Slant");
238 weights
[0] = _("Normal");
239 weights
[1] = _("Light");
240 weights
[2] = _("Bold");
242 #if !USE_SPINCTRL_FOR_POINT_SIZE
243 wxString
*pointSizes
= new wxString
[40];
245 for ( i
= 0; i
< 40; i
++)
248 wxSprintf(buf
, wxT("%d"), i
+ 1);
255 bool is_pda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
259 noCols
= 2; noRows
= 3;
263 noCols
= 3; noRows
= 2;
266 wxBoxSizer
* itemBoxSizer2
= new wxBoxSizer(wxVERTICAL
);
267 this->SetSizer(itemBoxSizer2
);
268 this->SetAutoLayout(true);
270 wxBoxSizer
* itemBoxSizer3
= new wxBoxSizer(wxVERTICAL
);
271 itemBoxSizer2
->Add(itemBoxSizer3
, 1, wxGROW
|wxALL
, 5);
273 wxFlexGridSizer
* itemGridSizer4
= new wxFlexGridSizer(noRows
, noCols
, 0, 0);
274 itemBoxSizer3
->Add(itemGridSizer4
, 0, wxGROW
, 5);
276 wxBoxSizer
* itemBoxSizer5
= new wxBoxSizer(wxVERTICAL
);
277 itemGridSizer4
->Add(itemBoxSizer5
, 0, wxALIGN_CENTER_HORIZONTAL
|wxGROW
, 5);
278 wxStaticText
* itemStaticText6
= new wxStaticText( this, wxID_STATIC
, _("&Font family:"), wxDefaultPosition
, wxDefaultSize
, 0 );
279 itemBoxSizer5
->Add(itemStaticText6
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
|wxADJUST_MINSIZE
, 5);
281 wxChoice
* itemChoice7
= new wxChoice( this, wxID_FONT_FAMILY
, wxDefaultPosition
, wxDefaultSize
, 5, families
, 0 );
282 itemChoice7
->SetHelpText(_("The font family."));
284 itemChoice7
->SetToolTip(_("The font family."));
285 itemBoxSizer5
->Add(itemChoice7
, 0, wxALIGN_LEFT
|wxALL
, 5);
287 wxBoxSizer
* itemBoxSizer8
= new wxBoxSizer(wxVERTICAL
);
288 itemGridSizer4
->Add(itemBoxSizer8
, 0, wxALIGN_CENTER_HORIZONTAL
|wxGROW
, 5);
289 wxStaticText
* itemStaticText9
= new wxStaticText( this, wxID_STATIC
, _("&Style:"), wxDefaultPosition
, wxDefaultSize
, 0 );
290 itemBoxSizer8
->Add(itemStaticText9
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
|wxADJUST_MINSIZE
, 5);
292 wxChoice
* itemChoice10
= new wxChoice( this, wxID_FONT_STYLE
, wxDefaultPosition
, wxDefaultSize
, 3, styles
, 0 );
293 itemChoice10
->SetHelpText(_("The font style."));
295 itemChoice10
->SetToolTip(_("The font style."));
296 itemBoxSizer8
->Add(itemChoice10
, 0, wxALIGN_LEFT
|wxALL
, 5);
298 wxBoxSizer
* itemBoxSizer11
= new wxBoxSizer(wxVERTICAL
);
299 itemGridSizer4
->Add(itemBoxSizer11
, 0, wxALIGN_CENTER_HORIZONTAL
|wxGROW
, 5);
300 wxStaticText
* itemStaticText12
= new wxStaticText( this, wxID_STATIC
, _("&Weight:"), wxDefaultPosition
, wxDefaultSize
, 0 );
301 itemBoxSizer11
->Add(itemStaticText12
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
|wxADJUST_MINSIZE
, 5);
303 wxChoice
* itemChoice13
= new wxChoice( this, wxID_FONT_WEIGHT
, wxDefaultPosition
, wxDefaultSize
, 3, weights
, 0 );
304 itemChoice13
->SetHelpText(_("The font weight."));
306 itemChoice13
->SetToolTip(_("The font weight."));
307 itemBoxSizer11
->Add(itemChoice13
, 0, wxALIGN_LEFT
|wxALL
, 5);
309 wxBoxSizer
* itemBoxSizer14
= new wxBoxSizer(wxVERTICAL
);
310 itemGridSizer4
->Add(itemBoxSizer14
, 0, wxALIGN_CENTER_HORIZONTAL
|wxGROW
, 5);
311 if (m_fontData
.GetEnableEffects())
313 wxStaticText
* itemStaticText15
= new wxStaticText( this, wxID_STATIC
, _("C&olour:"), wxDefaultPosition
, wxDefaultSize
, 0 );
314 itemBoxSizer14
->Add(itemStaticText15
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
|wxADJUST_MINSIZE
, 5);
316 wxSize colourSize
= wxDefaultSize
;
320 wxChoice
* itemChoice16
= new wxChoice( this, wxID_FONT_COLOUR
, wxDefaultPosition
, colourSize
, NUM_COLS
, wxColourDialogNames
, 0 );
321 itemChoice16
->SetHelpText(_("The font colour."));
323 itemChoice16
->SetToolTip(_("The font colour."));
324 itemBoxSizer14
->Add(itemChoice16
, 0, wxALIGN_LEFT
|wxALL
, 5);
327 wxBoxSizer
* itemBoxSizer17
= new wxBoxSizer(wxVERTICAL
);
328 itemGridSizer4
->Add(itemBoxSizer17
, 0, wxALIGN_CENTER_HORIZONTAL
|wxGROW
, 5);
329 wxStaticText
* itemStaticText18
= new wxStaticText( this, wxID_STATIC
, _("&Point size:"), wxDefaultPosition
, wxDefaultSize
, 0 );
330 itemBoxSizer17
->Add(itemStaticText18
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
|wxADJUST_MINSIZE
, 5);
332 #if USE_SPINCTRL_FOR_POINT_SIZE
333 wxSpinCtrl
* spinCtrl
= new wxSpinCtrl(this, wxID_FONT_SIZE
, wxT("12"), wxDefaultPosition
, wxSize(80, wxDefaultCoord
), wxSP_ARROW_KEYS
, 1, 500, 12);
334 spinCtrl
->SetHelpText(_("The font point size."));
336 spinCtrl
->SetToolTip(_("The font point size."));
338 itemBoxSizer17
->Add(spinCtrl
, 0, wxALIGN_LEFT
|wxALL
, 5);
340 wxChoice
* itemChoice19
= new wxChoice( this, wxID_FONT_SIZE
, wxDefaultPosition
, wxDefaultSize
, 40, pointSizes
, 0 );
341 itemChoice19
->SetHelpText(_("The font point size."));
343 itemChoice19
->SetToolTip(_("The font point size."));
344 itemBoxSizer17
->Add(itemChoice19
, 0, wxALIGN_LEFT
|wxALL
, 5);
347 if (m_fontData
.GetEnableEffects())
349 wxBoxSizer
* itemBoxSizer20
= new wxBoxSizer(wxVERTICAL
);
350 itemGridSizer4
->Add(itemBoxSizer20
, 0, wxALIGN_LEFT
|wxALIGN_CENTER_VERTICAL
, 5);
351 wxCheckBox
* itemCheckBox21
= new wxCheckBox( this, wxID_FONT_UNDERLINE
, _("&Underline"), wxDefaultPosition
, wxDefaultSize
, 0 );
352 itemCheckBox21
->SetValue(false);
353 itemCheckBox21
->SetHelpText(_("Whether the font is underlined."));
355 itemCheckBox21
->SetToolTip(_("Whether the font is underlined."));
356 itemBoxSizer20
->Add(itemCheckBox21
, 0, wxALIGN_LEFT
|wxALL
, 5);
360 itemBoxSizer3
->Add(5, 5, 0, wxALIGN_CENTER_HORIZONTAL
|wxALL
, 5);
362 wxStaticText
* itemStaticText23
= new wxStaticText( this, wxID_STATIC
, _("Preview:"), wxDefaultPosition
, wxDefaultSize
, 0 );
363 itemBoxSizer3
->Add(itemStaticText23
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
|wxADJUST_MINSIZE
, 5);
365 wxFontPreviewer
* itemWindow24
= new wxFontPreviewer( this );
366 m_previewer
= itemWindow24
;
367 itemWindow24
->SetHelpText(_("Shows the font preview."));
369 itemWindow24
->SetToolTip(_("Shows the font preview."));
370 itemBoxSizer3
->Add(itemWindow24
, 1, wxGROW
|wxALL
, 5);
372 wxBoxSizer
* itemBoxSizer25
= new wxBoxSizer(wxHORIZONTAL
);
373 itemBoxSizer3
->Add(itemBoxSizer25
, 0, wxGROW
, 5);
374 itemBoxSizer25
->Add(5, 5, 1, wxGROW
|wxALL
, 5);
377 wxButton
* itemButton28
= new wxButton( this, wxID_CANCEL
, _("&Cancel"), wxDefaultPosition
, wxDefaultSize
, 0 );
379 itemButton28
->SetToolTip(_("Click to cancel the font selection."));
380 itemBoxSizer25
->Add(itemButton28
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
382 wxButton
* itemButton27
= new wxButton( this, wxID_OK
, _("&OK"), wxDefaultPosition
, wxDefaultSize
, 0 );
383 itemButton27
->SetDefault();
384 itemButton27
->SetHelpText(_("Click to confirm the font selection."));
386 itemButton27
->SetToolTip(_("Click to confirm the font selection."));
387 itemBoxSizer25
->Add(itemButton27
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
389 wxButton
* itemButton27
= new wxButton( this, wxID_OK
, _("&OK"), wxDefaultPosition
, wxDefaultSize
, 0 );
390 itemButton27
->SetDefault();
391 itemButton27
->SetHelpText(_("Click to confirm the font selection."));
393 itemButton27
->SetToolTip(_("Click to confirm the font selection."));
394 itemBoxSizer25
->Add(itemButton27
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
396 wxButton
* itemButton28
= new wxButton( this, wxID_CANCEL
, _("&Cancel"), wxDefaultPosition
, wxDefaultSize
, 0 );
398 itemButton28
->SetToolTip(_("Click to cancel the font selection."));
399 itemBoxSizer25
->Add(itemButton28
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
402 m_familyChoice
= (wxChoice
*) FindWindow(wxID_FONT_FAMILY
);
403 m_styleChoice
= (wxChoice
*) FindWindow(wxID_FONT_STYLE
);
404 m_weightChoice
= (wxChoice
*) FindWindow(wxID_FONT_WEIGHT
);
405 m_colourChoice
= (wxChoice
*) FindWindow(wxID_FONT_COLOUR
);
406 m_underLineCheckBox
= (wxCheckBox
*) FindWindow(wxID_FONT_UNDERLINE
);
408 m_familyChoice
->SetStringSelection( wxFontFamilyIntToString(m_dialogFont
.GetFamily()) );
409 m_styleChoice
->SetStringSelection(wxFontStyleIntToString(m_dialogFont
.GetStyle()));
410 m_weightChoice
->SetStringSelection(wxFontWeightIntToString(m_dialogFont
.GetWeight()));
414 wxString
name(wxTheColourDatabase
->FindName(m_fontData
.GetColour()));
416 m_colourChoice
->SetStringSelection(name
);
418 m_colourChoice
->SetStringSelection(wxT("BLACK"));
421 if (m_underLineCheckBox
)
423 m_underLineCheckBox
->SetValue(m_dialogFont
.GetUnderlined());
426 #if USE_SPINCTRL_FOR_POINT_SIZE
427 spinCtrl
->SetValue(m_dialogFont
.GetPointSize());
429 m_pointSizeChoice
= (wxChoice
*) FindWindow(wxID_FONT_SIZE
);
430 m_pointSizeChoice
->SetSelection(m_dialogFont
.GetPointSize()-1);
433 #if !defined(__SMARTPHONE__) && !defined(__POCKETPC__)
434 GetSizer()->SetItemMinSize(m_previewer
, is_pda
? 100 : 430, is_pda
? 40 : 100);
435 GetSizer()->SetSizeHints(this);
436 GetSizer()->Fit(this);
444 #if !USE_SPINCTRL_FOR_POINT_SIZE
448 // Don't block events any more
453 void wxGenericFontDialog::InitializeFont()
455 int fontFamily
= wxSWISS
;
456 int fontWeight
= wxNORMAL
;
457 int fontStyle
= wxNORMAL
;
459 bool fontUnderline
= false;
461 if (m_fontData
.m_initialFont
.Ok())
463 fontFamily
= m_fontData
.m_initialFont
.GetFamily();
464 fontWeight
= m_fontData
.m_initialFont
.GetWeight();
465 fontStyle
= m_fontData
.m_initialFont
.GetStyle();
466 fontSize
= m_fontData
.m_initialFont
.GetPointSize();
467 fontUnderline
= m_fontData
.m_initialFont
.GetUnderlined();
470 m_dialogFont
= wxFont(fontSize
, fontFamily
, fontStyle
,
471 fontWeight
, fontUnderline
);
474 m_previewer
->SetFont(m_dialogFont
);
477 void wxGenericFontDialog::OnChangeFont(wxCommandEvent
& WXUNUSED(event
))
482 void wxGenericFontDialog::DoChangeFont()
484 if (!m_useEvents
) return;
486 int fontFamily
= wxFontFamilyStringToInt(WXSTRINGCAST m_familyChoice
->GetStringSelection());
487 int fontWeight
= wxFontWeightStringToInt(WXSTRINGCAST m_weightChoice
->GetStringSelection());
488 int fontStyle
= wxFontStyleStringToInt(WXSTRINGCAST m_styleChoice
->GetStringSelection());
489 #if USE_SPINCTRL_FOR_POINT_SIZE
490 wxSpinCtrl
* fontSizeCtrl
= wxDynamicCast(FindWindow(wxID_FONT_SIZE
), wxSpinCtrl
);
491 int fontSize
= fontSizeCtrl
->GetValue();
493 int fontSize
= wxAtoi(m_pointSizeChoice
->GetStringSelection());
496 // Start with previous underline setting, we want to retain it even if we can't edit it
497 // m_dialogFont is always initialized because of the call to InitializeFont
498 int fontUnderline
= m_dialogFont
.GetUnderlined();
500 if (m_underLineCheckBox
)
502 fontUnderline
= m_underLineCheckBox
->GetValue();
505 m_dialogFont
= wxFont(fontSize
, fontFamily
, fontStyle
, fontWeight
, (fontUnderline
!= 0));
506 m_previewer
->SetFont(m_dialogFont
);
508 if ( m_colourChoice
)
510 if ( !m_colourChoice
->GetStringSelection().empty() )
512 wxColour col
= wxTheColourDatabase
->Find(m_colourChoice
->GetStringSelection());
515 m_fontData
.m_fontColour
= col
;
519 // Update color here so that we can also use the color originally passed in
520 // (EnableEffects may be false)
521 if (m_fontData
.m_fontColour
.Ok())
522 m_previewer
->SetForegroundColour(m_fontData
.m_fontColour
);
524 m_previewer
->Refresh();
527 #if USE_SPINCTRL_FOR_POINT_SIZE
528 void wxGenericFontDialog::OnChangeSize(wxSpinEvent
& WXUNUSED(event
))
534 const wxChar
*wxFontWeightIntToString(int weight
)
544 return wxT("Normal");
548 const wxChar
*wxFontStyleIntToString(int style
)
553 return wxT("Italic");
558 return wxT("Normal");
562 const wxChar
*wxFontFamilyIntToString(int family
)
569 return wxT("Decorative");
571 return wxT("Modern");
573 return wxT("Script");
575 return wxT("Teletype");
582 int wxFontFamilyStringToInt(wxChar
*family
)
587 if (wxStrcmp(family
, wxT("Roman")) == 0)
589 else if (wxStrcmp(family
, wxT("Decorative")) == 0)
591 else if (wxStrcmp(family
, wxT("Modern")) == 0)
593 else if (wxStrcmp(family
, wxT("Script")) == 0)
595 else if (wxStrcmp(family
, wxT("Teletype")) == 0)
600 int wxFontStyleStringToInt(wxChar
*style
)
604 if (wxStrcmp(style
, wxT("Italic")) == 0)
606 else if (wxStrcmp(style
, wxT("Slant")) == 0)
612 int wxFontWeightStringToInt(wxChar
*weight
)
616 if (wxStrcmp(weight
, wxT("Bold")) == 0)
618 else if (wxStrcmp(weight
, wxT("Light")) == 0)