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 //-----------------------------------------------------------------------------
45 // helper class - wxFontPreviewer
46 //-----------------------------------------------------------------------------
48 class WXDLLEXPORT wxFontPreviewer
: public wxWindow
51 wxFontPreviewer(wxWindow
*parent
, const wxSize
& sz
= wxDefaultSize
) : wxWindow(parent
, wxID_ANY
, wxDefaultPosition
, sz
)
56 void OnPaint(wxPaintEvent
& event
);
60 BEGIN_EVENT_TABLE(wxFontPreviewer
, wxWindow
)
61 EVT_PAINT(wxFontPreviewer::OnPaint
)
64 void wxFontPreviewer::OnPaint(wxPaintEvent
& WXUNUSED(event
))
68 wxSize size
= GetSize();
69 wxFont font
= GetFont();
71 dc
.SetPen(*wxBLACK_PEN
);
72 dc
.SetBrush(*wxWHITE_BRUSH
);
73 dc
.DrawRectangle(0, 0, size
.x
, size
.y
);
78 // Calculate vertical centre
80 dc
.GetTextExtent( wxT("X"), &w
, &h
);
81 dc
.SetTextForeground(GetForegroundColour());
82 dc
.SetClippingRegion(2, 2, size
.x
-4, size
.y
-4);
83 dc
.DrawText(_("ABCDEFGabcdefg12345"),
85 dc
.DestroyClippingRegion();
89 //-----------------------------------------------------------------------------
90 // wxGenericFontDialog
91 //-----------------------------------------------------------------------------
93 IMPLEMENT_DYNAMIC_CLASS(wxGenericFontDialog
, wxDialog
)
95 BEGIN_EVENT_TABLE(wxGenericFontDialog
, wxDialog
)
96 EVT_CHECKBOX(wxID_FONT_UNDERLINE
, wxGenericFontDialog::OnChangeFont
)
97 EVT_CHOICE(wxID_FONT_STYLE
, wxGenericFontDialog::OnChangeFont
)
98 EVT_CHOICE(wxID_FONT_WEIGHT
, wxGenericFontDialog::OnChangeFont
)
99 EVT_CHOICE(wxID_FONT_FAMILY
, wxGenericFontDialog::OnChangeFont
)
100 EVT_CHOICE(wxID_FONT_COLOUR
, wxGenericFontDialog::OnChangeFont
)
101 EVT_CHOICE(wxID_FONT_SIZE
, wxGenericFontDialog::OnChangeFont
)
102 EVT_CLOSE(wxGenericFontDialog::OnCloseWindow
)
107 static wxString wxColourDialogNames
[NUM_COLS
]={wxT("ORANGE"),
113 wxT("MEDIUM VIOLET RED"),
118 wxT("MEDIUM SPRING GREEN"),
121 wxT("LIGHT STEEL BLUE"),
123 wxT("LIGHT MAGENTA"),
141 wxT("MEDIUM VIOLET RED"),
145 wxT("MEDIUM SEA GREEN"),
147 wxT("MIDNIGHT BLUE"),
153 wxT("MEDIUM FOREST GREEN"),
158 wxT("MEDIUM SLATE BLUE"),
163 * Generic wxFontDialog
166 void wxGenericFontDialog::Init()
173 wxGenericFontDialog::~wxGenericFontDialog()
177 void wxGenericFontDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
179 EndModal(wxID_CANCEL
);
182 bool wxGenericFontDialog::DoCreate(wxWindow
*parent
)
184 if ( !wxDialog::Create( parent
, wxID_ANY
, _T("Choose Font") , wxDefaultPosition
, wxDefaultSize
, wxDEFAULT_DIALOG_STYLE
,
187 wxFAIL_MSG( wxT("wxFontDialog creation failed") );
194 // sets initial font in preview area
195 wxCommandEvent dummy
;
201 int wxGenericFontDialog::ShowModal()
203 int ret
= wxDialog::ShowModal();
205 if (ret
!= wxID_CANCEL
)
207 m_fontData
.m_chosenFont
= dialogFont
;
213 // This should be application-settable
214 static bool ShowToolTips() { return false; }
216 void wxGenericFontDialog::CreateWidgets()
219 *families
= new wxString
[6],
220 *styles
= new wxString
[3],
221 *weights
= new wxString
[3];
222 families
[0] = _("Roman");
223 families
[1] = _("Decorative");
224 families
[2] = _("Modern");
225 families
[3] = _("Script");
226 families
[4] = _("Swiss" );
227 families
[5] = _("Teletype" );
228 styles
[0] = _("Normal");
229 styles
[1] = _("Italic");
230 styles
[2] = _("Slant");
231 weights
[0] = _("Normal");
232 weights
[1] = _("Light");
233 weights
[2] = _("Bold");
235 wxString
*pointSizes
= new wxString
[40];
237 for ( i
= 0; i
< 40; i
++)
240 wxSprintf(buf
, wxT("%d"), i
+ 1);
246 bool is_pda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
250 noCols
= 2; noRows
= 3;
254 noCols
= 3; noRows
= 2;
257 wxBoxSizer
* itemBoxSizer2
= new wxBoxSizer(wxVERTICAL
);
258 this->SetSizer(itemBoxSizer2
);
259 this->SetAutoLayout(TRUE
);
261 wxBoxSizer
* itemBoxSizer3
= new wxBoxSizer(wxVERTICAL
);
262 itemBoxSizer2
->Add(itemBoxSizer3
, 1, wxGROW
|wxALL
, 5);
264 wxFlexGridSizer
* itemGridSizer4
= new wxFlexGridSizer(noRows
, noCols
, 0, 0);
265 itemBoxSizer3
->Add(itemGridSizer4
, 0, wxGROW
, 5);
267 wxBoxSizer
* itemBoxSizer5
= new wxBoxSizer(wxVERTICAL
);
268 itemGridSizer4
->Add(itemBoxSizer5
, 0, wxALIGN_CENTER_HORIZONTAL
|wxGROW
, 5);
269 wxStaticText
* itemStaticText6
= new wxStaticText( this, wxID_STATIC
, _("&Font family:"), wxDefaultPosition
, wxDefaultSize
, 0 );
270 itemBoxSizer5
->Add(itemStaticText6
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
|wxADJUST_MINSIZE
, 5);
272 wxChoice
* itemChoice7
= new wxChoice( this, wxID_FONT_FAMILY
, wxDefaultPosition
, wxDefaultSize
, 5, families
, 0 );
273 itemChoice7
->SetHelpText(_("The font family."));
275 itemChoice7
->SetToolTip(_("The font family."));
276 itemBoxSizer5
->Add(itemChoice7
, 0, wxALIGN_LEFT
|wxALL
, 5);
278 wxBoxSizer
* itemBoxSizer8
= new wxBoxSizer(wxVERTICAL
);
279 itemGridSizer4
->Add(itemBoxSizer8
, 0, wxALIGN_CENTER_HORIZONTAL
|wxGROW
, 5);
280 wxStaticText
* itemStaticText9
= new wxStaticText( this, wxID_STATIC
, _("&Style:"), wxDefaultPosition
, wxDefaultSize
, 0 );
281 itemBoxSizer8
->Add(itemStaticText9
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
|wxADJUST_MINSIZE
, 5);
283 wxChoice
* itemChoice10
= new wxChoice( this, wxID_FONT_STYLE
, wxDefaultPosition
, wxDefaultSize
, 3, styles
, 0 );
284 itemChoice10
->SetHelpText(_("The font style."));
286 itemChoice10
->SetToolTip(_("The font style."));
287 itemBoxSizer8
->Add(itemChoice10
, 0, wxALIGN_LEFT
|wxALL
, 5);
289 wxBoxSizer
* itemBoxSizer11
= new wxBoxSizer(wxVERTICAL
);
290 itemGridSizer4
->Add(itemBoxSizer11
, 0, wxALIGN_CENTER_HORIZONTAL
|wxGROW
, 5);
291 wxStaticText
* itemStaticText12
= new wxStaticText( this, wxID_STATIC
, _("&Weight:"), wxDefaultPosition
, wxDefaultSize
, 0 );
292 itemBoxSizer11
->Add(itemStaticText12
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
|wxADJUST_MINSIZE
, 5);
294 wxChoice
* itemChoice13
= new wxChoice( this, wxID_FONT_WEIGHT
, wxDefaultPosition
, wxDefaultSize
, 3, weights
, 0 );
295 itemChoice13
->SetHelpText(_("The font weight."));
297 itemChoice13
->SetToolTip(_("The font weight."));
298 itemBoxSizer11
->Add(itemChoice13
, 0, wxALIGN_LEFT
|wxALL
, 5);
300 wxBoxSizer
* itemBoxSizer14
= new wxBoxSizer(wxVERTICAL
);
301 itemGridSizer4
->Add(itemBoxSizer14
, 0, wxALIGN_CENTER_HORIZONTAL
|wxGROW
, 5);
302 if (m_fontData
.GetEnableEffects())
304 wxStaticText
* itemStaticText15
= new wxStaticText( this, wxID_STATIC
, _("C&olour:"), wxDefaultPosition
, wxDefaultSize
, 0 );
305 itemBoxSizer14
->Add(itemStaticText15
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
|wxADJUST_MINSIZE
, 5);
307 wxSize colourSize
= wxDefaultSize
;
311 wxChoice
* itemChoice16
= new wxChoice( this, wxID_FONT_COLOUR
, wxDefaultPosition
, colourSize
, NUM_COLS
, wxColourDialogNames
, 0 );
312 itemChoice16
->SetHelpText(_("The font colour."));
314 itemChoice16
->SetToolTip(_("The font colour."));
315 itemBoxSizer14
->Add(itemChoice16
, 0, wxALIGN_LEFT
|wxALL
, 5);
318 wxBoxSizer
* itemBoxSizer17
= new wxBoxSizer(wxVERTICAL
);
319 itemGridSizer4
->Add(itemBoxSizer17
, 0, wxALIGN_CENTER_HORIZONTAL
|wxGROW
, 5);
320 wxStaticText
* itemStaticText18
= new wxStaticText( this, wxID_STATIC
, _("&Point size:"), wxDefaultPosition
, wxDefaultSize
, 0 );
321 itemBoxSizer17
->Add(itemStaticText18
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
|wxADJUST_MINSIZE
, 5);
323 wxChoice
* itemChoice19
= new wxChoice( this, wxID_FONT_SIZE
, wxDefaultPosition
, wxDefaultSize
, 40, pointSizes
, 0 );
324 itemChoice19
->SetHelpText(_("The font point size."));
326 itemChoice19
->SetToolTip(_("The font point size."));
327 itemBoxSizer17
->Add(itemChoice19
, 0, wxALIGN_LEFT
|wxALL
, 5);
329 if (m_fontData
.GetEnableEffects())
331 wxBoxSizer
* itemBoxSizer20
= new wxBoxSizer(wxVERTICAL
);
332 itemGridSizer4
->Add(itemBoxSizer20
, 0, wxALIGN_LEFT
|wxALIGN_CENTER_VERTICAL
, 5);
333 wxCheckBox
* itemCheckBox21
= new wxCheckBox( this, wxID_FONT_UNDERLINE
, _("&Underline"), wxDefaultPosition
, wxDefaultSize
, 0 );
334 itemCheckBox21
->SetValue(FALSE
);
335 itemCheckBox21
->SetHelpText(_("Whether the font is underlined."));
337 itemCheckBox21
->SetToolTip(_("Whether the font is underlined."));
338 itemBoxSizer20
->Add(itemCheckBox21
, 0, wxALIGN_LEFT
|wxALL
, 5);
342 itemBoxSizer3
->Add(5, 5, 0, wxALIGN_CENTER_HORIZONTAL
|wxALL
, 5);
344 wxStaticText
* itemStaticText23
= new wxStaticText( this, wxID_STATIC
, _("Preview:"), wxDefaultPosition
, wxDefaultSize
, 0 );
345 itemBoxSizer3
->Add(itemStaticText23
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
|wxADJUST_MINSIZE
, 5);
347 wxFontPreviewer
* itemWindow24
= new wxFontPreviewer( this );
348 m_previewer
= itemWindow24
;
349 itemWindow24
->SetHelpText(_("Shows the font preview."));
351 itemWindow24
->SetToolTip(_("Shows the font preview."));
352 itemBoxSizer3
->Add(itemWindow24
, 0, wxGROW
|wxALL
, 5);
354 wxBoxSizer
* itemBoxSizer25
= new wxBoxSizer(wxHORIZONTAL
);
355 itemBoxSizer3
->Add(itemBoxSizer25
, 0, wxGROW
, 5);
356 itemBoxSizer25
->Add(5, 5, 1, wxGROW
|wxALL
, 5);
359 wxButton
* itemButton28
= new wxButton( this, wxID_CANCEL
, _("&Cancel"), wxDefaultPosition
, wxDefaultSize
, 0 );
361 itemButton28
->SetToolTip(_("Click to cancel the font selection."));
362 itemBoxSizer25
->Add(itemButton28
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
364 wxButton
* itemButton27
= new wxButton( this, wxID_OK
, _("&OK"), wxDefaultPosition
, wxDefaultSize
, 0 );
365 itemButton27
->SetDefault();
366 itemButton27
->SetHelpText(_("Click to confirm the font selection."));
368 itemButton27
->SetToolTip(_("Click to confirm the font selection."));
369 itemBoxSizer25
->Add(itemButton27
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
371 wxButton
* itemButton27
= new wxButton( this, wxID_OK
, _("&OK"), wxDefaultPosition
, wxDefaultSize
, 0 );
372 itemButton27
->SetDefault();
373 itemButton27
->SetHelpText(_("Click to confirm the font selection."));
375 itemButton27
->SetToolTip(_("Click to confirm the font selection."));
376 itemBoxSizer25
->Add(itemButton27
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
378 wxButton
* itemButton28
= new wxButton( this, wxID_CANCEL
, _("&Cancel"), wxDefaultPosition
, wxDefaultSize
, 0 );
380 itemButton28
->SetToolTip(_("Click to cancel the font selection."));
381 itemBoxSizer25
->Add(itemButton28
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
384 familyChoice
= (wxChoice
*) FindWindow(wxID_FONT_FAMILY
);
385 styleChoice
= (wxChoice
*) FindWindow(wxID_FONT_STYLE
);
386 weightChoice
= (wxChoice
*) FindWindow(wxID_FONT_WEIGHT
);
387 colourChoice
= (wxChoice
*) FindWindow(wxID_FONT_COLOUR
);
388 pointSizeChoice
= (wxChoice
*) FindWindow(wxID_FONT_SIZE
);
389 underLineCheckBox
= (wxCheckBox
*) FindWindow(wxID_FONT_UNDERLINE
);
391 familyChoice
->SetStringSelection( wxFontFamilyIntToString(dialogFont
.GetFamily()) );
392 styleChoice
->SetStringSelection(wxFontStyleIntToString(dialogFont
.GetStyle()));
393 weightChoice
->SetStringSelection(wxFontWeightIntToString(dialogFont
.GetWeight()));
397 wxString
name(wxTheColourDatabase
->FindName(m_fontData
.GetColour()));
399 colourChoice
->SetStringSelection(name
);
401 colourChoice
->SetStringSelection(wxT("BLACK"));
404 if (underLineCheckBox
)
406 underLineCheckBox
->SetValue(dialogFont
.GetUnderlined());
409 pointSizeChoice
->SetSelection(dialogFont
.GetPointSize()-1);
411 #if !defined(__SMARTPHONE__) && !defined(__POCKETPC__)
412 GetSizer()->SetItemMinSize(m_previewer
, is_pda
? 100 : 430, is_pda
? 40 : 100);
413 GetSizer()->SetSizeHints(this);
414 GetSizer()->Fit(this);
424 // Don't block events any more
429 void wxGenericFontDialog::InitializeFont()
431 int fontFamily
= wxSWISS
;
432 int fontWeight
= wxNORMAL
;
433 int fontStyle
= wxNORMAL
;
435 bool fontUnderline
= false;
437 if (m_fontData
.m_initialFont
.Ok())
439 fontFamily
= m_fontData
.m_initialFont
.GetFamily();
440 fontWeight
= m_fontData
.m_initialFont
.GetWeight();
441 fontStyle
= m_fontData
.m_initialFont
.GetStyle();
442 fontSize
= m_fontData
.m_initialFont
.GetPointSize();
443 fontUnderline
= m_fontData
.m_initialFont
.GetUnderlined();
446 dialogFont
= wxFont(fontSize
, fontFamily
, fontStyle
,
447 fontWeight
, fontUnderline
);
450 m_previewer
->SetFont(dialogFont
);
453 void wxGenericFontDialog::OnChangeFont(wxCommandEvent
& WXUNUSED(event
))
455 if (!m_useEvents
) return;
457 int fontFamily
= wxFontFamilyStringToInt(WXSTRINGCAST familyChoice
->GetStringSelection());
458 int fontWeight
= wxFontWeightStringToInt(WXSTRINGCAST weightChoice
->GetStringSelection());
459 int fontStyle
= wxFontStyleStringToInt(WXSTRINGCAST styleChoice
->GetStringSelection());
460 int fontSize
= wxAtoi(pointSizeChoice
->GetStringSelection());
461 // Start with previous underline setting, we want to retain it even if we can't edit it
462 // dialogFont is always initialized because of the call to InitializeFont
463 int fontUnderline
= dialogFont
.GetUnderlined();
465 if (underLineCheckBox
)
467 fontUnderline
= underLineCheckBox
->GetValue();
470 dialogFont
= wxFont(fontSize
, fontFamily
, fontStyle
, fontWeight
, (fontUnderline
!= 0));
471 m_previewer
->SetFont(dialogFont
);
475 if ( !colourChoice
->GetStringSelection().empty() )
477 wxColour col
= wxTheColourDatabase
->Find(colourChoice
->GetStringSelection());
480 m_fontData
.m_fontColour
= col
;
484 // Update color here so that we can also use the color originally passed in
485 // (EnableEffects may be false)
486 if (m_fontData
.m_fontColour
.Ok())
487 m_previewer
->SetForegroundColour(m_fontData
.m_fontColour
);
489 m_previewer
->Refresh();
492 const wxChar
*wxFontWeightIntToString(int weight
)
502 return wxT("Normal");
506 const wxChar
*wxFontStyleIntToString(int style
)
511 return wxT("Italic");
516 return wxT("Normal");
520 const wxChar
*wxFontFamilyIntToString(int family
)
527 return wxT("Decorative");
529 return wxT("Modern");
531 return wxT("Script");
533 return wxT("Teletype");
540 int wxFontFamilyStringToInt(wxChar
*family
)
545 if (wxStrcmp(family
, wxT("Roman")) == 0)
547 else if (wxStrcmp(family
, wxT("Decorative")) == 0)
549 else if (wxStrcmp(family
, wxT("Modern")) == 0)
551 else if (wxStrcmp(family
, wxT("Script")) == 0)
553 else if (wxStrcmp(family
, wxT("Teletype")) == 0)
558 int wxFontStyleStringToInt(wxChar
*style
)
562 if (wxStrcmp(style
, wxT("Italic")) == 0)
564 else if (wxStrcmp(style
, wxT("Slant")) == 0)
570 int wxFontWeightStringToInt(wxChar
*weight
)
574 if (wxStrcmp(weight
, wxT("Bold")) == 0)
576 else if (wxStrcmp(weight
, wxT("Light")) == 0)