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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "fontdlgg.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
23 #if wxUSE_FONTDLG && (!defined(__WXGTK__) || defined(__WXGPE__) || defined(__WXUNIVERSAL__))
28 #include "wx/dialog.h"
29 #include "wx/listbox.h"
30 #include "wx/button.h"
31 #include "wx/stattext.h"
32 #include "wx/layout.h"
33 #include "wx/dcclient.h"
34 #include "wx/choice.h"
35 #include "wx/checkbox.h"
42 #include "wx/cmndata.h"
44 #include "wx/fontdlg.h"
45 #include "wx/generic/fontdlgg.h"
46 #include "wx/settings.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 EVT_CHOICE(wxID_FONT_SIZE
, wxGenericFontDialog::OnChangeFont
)
106 EVT_CLOSE(wxGenericFontDialog::OnCloseWindow
)
111 static wxString wxColourDialogNames
[NUM_COLS
]={wxT("ORANGE"),
117 wxT("MEDIUM VIOLET RED"),
122 wxT("MEDIUM SPRING GREEN"),
125 wxT("LIGHT STEEL BLUE"),
127 wxT("LIGHT MAGENTA"),
145 wxT("MEDIUM VIOLET RED"),
149 wxT("MEDIUM SEA GREEN"),
151 wxT("MIDNIGHT BLUE"),
157 wxT("MEDIUM FOREST GREEN"),
162 wxT("MEDIUM SLATE BLUE"),
167 * Generic wxFontDialog
170 void wxGenericFontDialog::Init()
177 wxGenericFontDialog::~wxGenericFontDialog()
181 void wxGenericFontDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
183 EndModal(wxID_CANCEL
);
186 bool wxGenericFontDialog::DoCreate(wxWindow
*parent
)
188 if ( !wxDialog::Create( parent
, wxID_ANY
, _T("Choose Font") , wxDefaultPosition
, wxDefaultSize
, wxDEFAULT_DIALOG_STYLE
,
191 wxFAIL_MSG( wxT("wxFontDialog creation failed") );
198 // sets initial font in preview area
199 wxCommandEvent dummy
;
205 int wxGenericFontDialog::ShowModal()
207 int ret
= wxDialog::ShowModal();
209 if (ret
!= wxID_CANCEL
)
211 m_fontData
.m_chosenFont
= dialogFont
;
217 // This should be application-settable
218 static bool ShowToolTips() { return false; }
220 void wxGenericFontDialog::CreateWidgets()
223 *families
= new wxString
[6],
224 *styles
= new wxString
[3],
225 *weights
= new wxString
[3];
226 families
[0] = _("Roman");
227 families
[1] = _("Decorative");
228 families
[2] = _("Modern");
229 families
[3] = _("Script");
230 families
[4] = _("Swiss" );
231 families
[5] = _("Teletype" );
232 styles
[0] = _("Normal");
233 styles
[1] = _("Italic");
234 styles
[2] = _("Slant");
235 weights
[0] = _("Normal");
236 weights
[1] = _("Light");
237 weights
[2] = _("Bold");
239 wxString
*pointSizes
= new wxString
[40];
241 for ( i
= 0; i
< 40; i
++)
244 wxSprintf(buf
, wxT("%d"), i
+ 1);
250 bool is_pda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
254 noCols
= 2; noRows
= 3;
258 noCols
= 3; noRows
= 2;
261 wxBoxSizer
* itemBoxSizer2
= new wxBoxSizer(wxVERTICAL
);
262 this->SetSizer(itemBoxSizer2
);
263 this->SetAutoLayout(TRUE
);
265 wxBoxSizer
* itemBoxSizer3
= new wxBoxSizer(wxVERTICAL
);
266 itemBoxSizer2
->Add(itemBoxSizer3
, 1, wxGROW
|wxALL
, 5);
268 wxFlexGridSizer
* itemGridSizer4
= new wxFlexGridSizer(noRows
, noCols
, 0, 0);
269 itemBoxSizer3
->Add(itemGridSizer4
, 0, wxGROW
, 5);
271 wxBoxSizer
* itemBoxSizer5
= new wxBoxSizer(wxVERTICAL
);
272 itemGridSizer4
->Add(itemBoxSizer5
, 0, wxALIGN_CENTER_HORIZONTAL
|wxGROW
, 5);
273 wxStaticText
* itemStaticText6
= new wxStaticText( this, wxID_STATIC
, _("&Font family:"), wxDefaultPosition
, wxDefaultSize
, 0 );
274 itemBoxSizer5
->Add(itemStaticText6
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
|wxADJUST_MINSIZE
, 5);
276 wxChoice
* itemChoice7
= new wxChoice( this, wxID_FONT_FAMILY
, wxDefaultPosition
, wxDefaultSize
, 5, families
, 0 );
277 itemChoice7
->SetHelpText(_("The font family."));
279 itemChoice7
->SetToolTip(_("The font family."));
280 itemBoxSizer5
->Add(itemChoice7
, 0, wxALIGN_LEFT
|wxALL
, 5);
282 wxBoxSizer
* itemBoxSizer8
= new wxBoxSizer(wxVERTICAL
);
283 itemGridSizer4
->Add(itemBoxSizer8
, 0, wxALIGN_CENTER_HORIZONTAL
|wxGROW
, 5);
284 wxStaticText
* itemStaticText9
= new wxStaticText( this, wxID_STATIC
, _("&Style:"), wxDefaultPosition
, wxDefaultSize
, 0 );
285 itemBoxSizer8
->Add(itemStaticText9
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
|wxADJUST_MINSIZE
, 5);
287 wxChoice
* itemChoice10
= new wxChoice( this, wxID_FONT_STYLE
, wxDefaultPosition
, wxDefaultSize
, 3, styles
, 0 );
288 itemChoice10
->SetHelpText(_("The font style."));
290 itemChoice10
->SetToolTip(_("The font style."));
291 itemBoxSizer8
->Add(itemChoice10
, 0, wxALIGN_LEFT
|wxALL
, 5);
293 wxBoxSizer
* itemBoxSizer11
= new wxBoxSizer(wxVERTICAL
);
294 itemGridSizer4
->Add(itemBoxSizer11
, 0, wxALIGN_CENTER_HORIZONTAL
|wxGROW
, 5);
295 wxStaticText
* itemStaticText12
= new wxStaticText( this, wxID_STATIC
, _("&Weight:"), wxDefaultPosition
, wxDefaultSize
, 0 );
296 itemBoxSizer11
->Add(itemStaticText12
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
|wxADJUST_MINSIZE
, 5);
298 wxChoice
* itemChoice13
= new wxChoice( this, wxID_FONT_WEIGHT
, wxDefaultPosition
, wxDefaultSize
, 3, weights
, 0 );
299 itemChoice13
->SetHelpText(_("The font weight."));
301 itemChoice13
->SetToolTip(_("The font weight."));
302 itemBoxSizer11
->Add(itemChoice13
, 0, wxALIGN_LEFT
|wxALL
, 5);
304 wxBoxSizer
* itemBoxSizer14
= new wxBoxSizer(wxVERTICAL
);
305 itemGridSizer4
->Add(itemBoxSizer14
, 0, wxALIGN_CENTER_HORIZONTAL
|wxGROW
, 5);
306 if (m_fontData
.GetEnableEffects())
308 wxStaticText
* itemStaticText15
= new wxStaticText( this, wxID_STATIC
, _("C&olour:"), wxDefaultPosition
, wxDefaultSize
, 0 );
309 itemBoxSizer14
->Add(itemStaticText15
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
|wxADJUST_MINSIZE
, 5);
311 wxSize colourSize
= wxDefaultSize
;
315 wxChoice
* itemChoice16
= new wxChoice( this, wxID_FONT_COLOUR
, wxDefaultPosition
, wxDefaultSize
, NUM_COLS
, wxColourDialogNames
, 0 );
316 itemChoice16
->SetHelpText(_("The font colour."));
318 itemChoice16
->SetToolTip(_("The font colour."));
319 itemBoxSizer14
->Add(itemChoice16
, 0, wxALIGN_LEFT
|wxALL
, 5);
322 wxBoxSizer
* itemBoxSizer17
= new wxBoxSizer(wxVERTICAL
);
323 itemGridSizer4
->Add(itemBoxSizer17
, 0, wxALIGN_CENTER_HORIZONTAL
|wxGROW
, 5);
324 wxStaticText
* itemStaticText18
= new wxStaticText( this, wxID_STATIC
, _("&Point size:"), wxDefaultPosition
, wxDefaultSize
, 0 );
325 itemBoxSizer17
->Add(itemStaticText18
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
|wxADJUST_MINSIZE
, 5);
327 wxChoice
* itemChoice19
= new wxChoice( this, wxID_FONT_SIZE
, wxDefaultPosition
, wxDefaultSize
, 40, pointSizes
, 0 );
328 itemChoice19
->SetHelpText(_("The font point size."));
330 itemChoice19
->SetToolTip(_("The font point size."));
331 itemBoxSizer17
->Add(itemChoice19
, 0, wxALIGN_LEFT
|wxALL
, 5);
333 if (m_fontData
.GetEnableEffects())
335 wxBoxSizer
* itemBoxSizer20
= new wxBoxSizer(wxVERTICAL
);
336 itemGridSizer4
->Add(itemBoxSizer20
, 0, wxALIGN_LEFT
|wxALIGN_CENTER_VERTICAL
, 5);
337 wxCheckBox
* itemCheckBox21
= new wxCheckBox( this, wxID_FONT_UNDERLINE
, _("&Underline"), wxDefaultPosition
, wxDefaultSize
, 0 );
338 itemCheckBox21
->SetValue(FALSE
);
339 itemCheckBox21
->SetHelpText(_("Whether the font is underlined."));
341 itemCheckBox21
->SetToolTip(_("Whether the font is underlined."));
342 itemBoxSizer20
->Add(itemCheckBox21
, 0, wxALIGN_LEFT
|wxALL
, 5);
346 itemBoxSizer3
->Add(5, 5, 0, wxALIGN_CENTER_HORIZONTAL
|wxALL
, 5);
348 wxStaticText
* itemStaticText23
= new wxStaticText( this, wxID_STATIC
, _("Preview:"), wxDefaultPosition
, wxDefaultSize
, 0 );
349 itemBoxSizer3
->Add(itemStaticText23
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
|wxADJUST_MINSIZE
, 5);
351 wxFontPreviewer
* itemWindow24
= new wxFontPreviewer( this );
352 m_previewer
= itemWindow24
;
353 itemWindow24
->SetHelpText(_("Shows the font preview."));
355 itemWindow24
->SetToolTip(_("Shows the font preview."));
356 itemBoxSizer3
->Add(itemWindow24
, 0, wxGROW
|wxALL
, 5);
358 wxBoxSizer
* itemBoxSizer25
= new wxBoxSizer(wxHORIZONTAL
);
359 itemBoxSizer3
->Add(itemBoxSizer25
, 0, wxGROW
, 5);
360 itemBoxSizer25
->Add(5, 5, 1, wxGROW
|wxALL
, 5);
363 wxButton
* itemButton28
= new wxButton( this, wxID_CANCEL
, _("&Cancel"), wxDefaultPosition
, wxDefaultSize
, 0 );
365 itemButton28
->SetToolTip(_("Click to cancel the font selection."));
366 itemBoxSizer25
->Add(itemButton28
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
368 wxButton
* itemButton27
= new wxButton( this, wxID_OK
, _("&OK"), wxDefaultPosition
, wxDefaultSize
, 0 );
369 itemButton27
->SetDefault();
370 itemButton27
->SetHelpText(_("Click to confirm the font selection."));
372 itemButton27
->SetToolTip(_("Click to confirm the font selection."));
373 itemBoxSizer25
->Add(itemButton27
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
375 wxButton
* itemButton27
= new wxButton( this, wxID_OK
, _("&OK"), wxDefaultPosition
, wxDefaultSize
, 0 );
376 itemButton27
->SetDefault();
377 itemButton27
->SetHelpText(_("Click to confirm the font selection."));
379 itemButton27
->SetToolTip(_("Click to confirm the font selection."));
380 itemBoxSizer25
->Add(itemButton27
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
382 wxButton
* itemButton28
= new wxButton( this, wxID_CANCEL
, _("&Cancel"), wxDefaultPosition
, wxDefaultSize
, 0 );
384 itemButton28
->SetToolTip(_("Click to cancel the font selection."));
385 itemBoxSizer25
->Add(itemButton28
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
388 familyChoice
= (wxChoice
*) FindWindow(wxID_FONT_FAMILY
);
389 styleChoice
= (wxChoice
*) FindWindow(wxID_FONT_STYLE
);
390 weightChoice
= (wxChoice
*) FindWindow(wxID_FONT_WEIGHT
);
391 colourChoice
= (wxChoice
*) FindWindow(wxID_FONT_COLOUR
);
392 pointSizeChoice
= (wxChoice
*) FindWindow(wxID_FONT_SIZE
);
393 underLineCheckBox
= (wxCheckBox
*) FindWindow(wxID_FONT_UNDERLINE
);
395 familyChoice
->SetStringSelection( wxFontFamilyIntToString(dialogFont
.GetFamily()) );
396 styleChoice
->SetStringSelection(wxFontStyleIntToString(dialogFont
.GetStyle()));
397 weightChoice
->SetStringSelection(wxFontWeightIntToString(dialogFont
.GetWeight()));
401 wxString
name(wxTheColourDatabase
->FindName(m_fontData
.GetColour()));
403 colourChoice
->SetStringSelection(name
);
405 colourChoice
->SetStringSelection(wxT("BLACK"));
408 if (underLineCheckBox
)
410 underLineCheckBox
->SetValue(dialogFont
.GetUnderlined());
413 pointSizeChoice
->SetSelection(dialogFont
.GetPointSize()-1);
415 #if !defined(__SMARTPHONE__) && !defined(__POCKETPC__)
416 GetSizer()->SetItemMinSize(m_previewer
, is_pda
? 100 : 430, is_pda
? 40 : 100);
417 GetSizer()->SetSizeHints(this);
418 GetSizer()->Fit(this);
428 // Don't block events any more
433 void wxGenericFontDialog::InitializeFont()
435 int fontFamily
= wxSWISS
;
436 int fontWeight
= wxNORMAL
;
437 int fontStyle
= wxNORMAL
;
439 bool fontUnderline
= false;
441 if (m_fontData
.m_initialFont
.Ok())
443 fontFamily
= m_fontData
.m_initialFont
.GetFamily();
444 fontWeight
= m_fontData
.m_initialFont
.GetWeight();
445 fontStyle
= m_fontData
.m_initialFont
.GetStyle();
446 fontSize
= m_fontData
.m_initialFont
.GetPointSize();
447 fontUnderline
= m_fontData
.m_initialFont
.GetUnderlined();
450 dialogFont
= wxFont(fontSize
, fontFamily
, fontStyle
,
451 fontWeight
, fontUnderline
);
454 m_previewer
->SetFont(dialogFont
);
457 void wxGenericFontDialog::OnChangeFont(wxCommandEvent
& WXUNUSED(event
))
459 if (!m_useEvents
) return;
461 int fontFamily
= wxFontFamilyStringToInt(WXSTRINGCAST familyChoice
->GetStringSelection());
462 int fontWeight
= wxFontWeightStringToInt(WXSTRINGCAST weightChoice
->GetStringSelection());
463 int fontStyle
= wxFontStyleStringToInt(WXSTRINGCAST styleChoice
->GetStringSelection());
464 int fontSize
= wxAtoi(pointSizeChoice
->GetStringSelection());
465 // Start with previous underline setting, we want to retain it even if we can't edit it
466 // dialogFont is always initialized because of the call to InitializeFont
467 int fontUnderline
= dialogFont
.GetUnderlined();
469 if (underLineCheckBox
)
471 fontUnderline
= underLineCheckBox
->GetValue();
474 dialogFont
= wxFont(fontSize
, fontFamily
, fontStyle
, fontWeight
, (fontUnderline
!= 0));
475 m_previewer
->SetFont(dialogFont
);
479 if ( !colourChoice
->GetStringSelection().empty() )
481 wxColour col
= wxTheColourDatabase
->Find(colourChoice
->GetStringSelection());
484 m_fontData
.m_fontColour
= col
;
488 // Update color here so that we can also use the color originally passed in
489 // (EnableEffects may be false)
490 if (m_fontData
.m_fontColour
.Ok())
491 m_previewer
->SetForegroundColour(m_fontData
.m_fontColour
);
493 m_previewer
->Refresh();
496 const wxChar
*wxFontWeightIntToString(int weight
)
506 return wxT("Normal");
510 const wxChar
*wxFontStyleIntToString(int style
)
515 return wxT("Italic");
520 return wxT("Normal");
524 const wxChar
*wxFontFamilyIntToString(int family
)
531 return wxT("Decorative");
533 return wxT("Modern");
535 return wxT("Script");
537 return wxT("Teletype");
544 int wxFontFamilyStringToInt(wxChar
*family
)
549 if (wxStrcmp(family
, wxT("Roman")) == 0)
551 else if (wxStrcmp(family
, wxT("Decorative")) == 0)
553 else if (wxStrcmp(family
, wxT("Modern")) == 0)
555 else if (wxStrcmp(family
, wxT("Script")) == 0)
557 else if (wxStrcmp(family
, wxT("Teletype")) == 0)
562 int wxFontStyleStringToInt(wxChar
*style
)
566 if (wxStrcmp(style
, wxT("Italic")) == 0)
568 else if (wxStrcmp(style
, wxT("Slant")) == 0)
574 int wxFontWeightStringToInt(wxChar
*weight
)
578 if (wxStrcmp(weight
, wxT("Bold")) == 0)
580 else if (wxStrcmp(weight
, wxT("Light")) == 0)