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
) : wxWindow(parent
, wxID_ANY
) {}
58 void OnPaint(wxPaintEvent
& event
);
62 BEGIN_EVENT_TABLE(wxFontPreviewer
, wxWindow
)
63 EVT_PAINT(wxFontPreviewer::OnPaint
)
66 void wxFontPreviewer::OnPaint(wxPaintEvent
& WXUNUSED(event
))
70 wxSize size
= GetSize();
71 wxFont font
= GetFont();
73 dc
.SetPen(*wxBLACK_PEN
);
74 dc
.SetBrush(*wxWHITE_BRUSH
);
75 dc
.DrawRectangle(0, 0, size
.x
, size
.y
);
80 // Calculate vertical centre
82 dc
.GetTextExtent( wxT("X"), &w
, &h
);
83 dc
.SetTextForeground(GetForegroundColour());
84 dc
.SetClippingRegion(2, 2, size
.x
-4, size
.y
-4);
85 dc
.DrawText(_("ABCDEFGabcdefg12345"),
87 dc
.DestroyClippingRegion();
91 //-----------------------------------------------------------------------------
92 // wxGenericFontDialog
93 //-----------------------------------------------------------------------------
95 IMPLEMENT_DYNAMIC_CLASS(wxGenericFontDialog
, wxDialog
)
97 BEGIN_EVENT_TABLE(wxGenericFontDialog
, wxDialog
)
98 EVT_CHECKBOX(wxID_FONT_UNDERLINE
, wxGenericFontDialog::OnChangeFont
)
99 EVT_CHOICE(wxID_FONT_STYLE
, wxGenericFontDialog::OnChangeFont
)
100 EVT_CHOICE(wxID_FONT_WEIGHT
, wxGenericFontDialog::OnChangeFont
)
101 EVT_CHOICE(wxID_FONT_FAMILY
, wxGenericFontDialog::OnChangeFont
)
102 EVT_CHOICE(wxID_FONT_COLOUR
, wxGenericFontDialog::OnChangeFont
)
103 EVT_CHOICE(wxID_FONT_SIZE
, wxGenericFontDialog::OnChangeFont
)
104 EVT_CLOSE(wxGenericFontDialog::OnCloseWindow
)
109 static wxString wxColourDialogNames
[NUM_COLS
]={wxT("ORANGE"),
115 wxT("MEDIUM VIOLET RED"),
120 wxT("MEDIUM SPRING GREEN"),
123 wxT("LIGHT STEEL BLUE"),
125 wxT("LIGHT MAGENTA"),
143 wxT("MEDIUM VIOLET RED"),
147 wxT("MEDIUM SEA GREEN"),
149 wxT("MIDNIGHT BLUE"),
155 wxT("MEDIUM FOREST GREEN"),
160 wxT("MEDIUM SLATE BLUE"),
165 * Generic wxFontDialog
168 void wxGenericFontDialog::Init()
175 wxGenericFontDialog::~wxGenericFontDialog()
179 void wxGenericFontDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
181 EndModal(wxID_CANCEL
);
184 bool wxGenericFontDialog::DoCreate(wxWindow
*parent
)
186 if ( !wxDialog::Create( parent
, wxID_ANY
, _T("Choose Font") , wxDefaultPosition
, wxDefaultSize
, wxDEFAULT_DIALOG_STYLE
,
189 wxFAIL_MSG( wxT("wxFontDialog creation failed") );
196 // sets initial font in preview area
197 wxCommandEvent dummy
;
203 int wxGenericFontDialog::ShowModal()
205 int ret
= wxDialog::ShowModal();
207 if (ret
!= wxID_CANCEL
)
209 m_fontData
.m_chosenFont
= dialogFont
;
215 // This should be application-settable
216 static bool ShowToolTips() { return false; }
218 void wxGenericFontDialog::CreateWidgets()
221 *families
= new wxString
[6],
222 *styles
= new wxString
[3],
223 *weights
= new wxString
[3];
224 families
[0] = _("Roman");
225 families
[1] = _("Decorative");
226 families
[2] = _("Modern");
227 families
[3] = _("Script");
228 families
[4] = _("Swiss" );
229 families
[5] = _("Teletype" );
230 styles
[0] = _("Normal");
231 styles
[1] = _("Italic");
232 styles
[2] = _("Slant");
233 weights
[0] = _("Normal");
234 weights
[1] = _("Light");
235 weights
[2] = _("Bold");
237 wxString
*pointSizes
= new wxString
[40];
239 for ( i
= 0; i
< 40; i
++)
242 wxSprintf(buf
, wxT("%d"), i
+ 1);
248 bool is_pda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
252 noCols
= 2; noRows
= 3;
256 noCols
= 3; noRows
= 2;
259 wxBoxSizer
* itemBoxSizer2
= new wxBoxSizer(wxVERTICAL
);
260 this->SetSizer(itemBoxSizer2
);
261 this->SetAutoLayout(TRUE
);
263 wxBoxSizer
* itemBoxSizer3
= new wxBoxSizer(wxVERTICAL
);
264 itemBoxSizer2
->Add(itemBoxSizer3
, 1, wxGROW
|wxALL
, 5);
266 wxFlexGridSizer
* itemGridSizer4
= new wxFlexGridSizer(noRows
, noCols
, 0, 0);
267 itemBoxSizer3
->Add(itemGridSizer4
, 0, wxGROW
, 5);
269 wxBoxSizer
* itemBoxSizer5
= new wxBoxSizer(wxVERTICAL
);
270 itemGridSizer4
->Add(itemBoxSizer5
, 0, wxALIGN_CENTER_HORIZONTAL
|wxGROW
, 5);
271 wxStaticText
* itemStaticText6
= new wxStaticText( this, wxID_STATIC
, _("&Font family:"), wxDefaultPosition
, wxDefaultSize
, 0 );
272 itemBoxSizer5
->Add(itemStaticText6
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
|wxADJUST_MINSIZE
, 5);
274 wxChoice
* itemChoice7
= new wxChoice( this, wxID_FONT_FAMILY
, wxDefaultPosition
, wxDefaultSize
, 5, families
, 0 );
275 itemChoice7
->SetHelpText(_("The font family."));
277 itemChoice7
->SetToolTip(_("The font family."));
278 itemBoxSizer5
->Add(itemChoice7
, 0, wxALIGN_LEFT
|wxALL
, 5);
280 wxBoxSizer
* itemBoxSizer8
= new wxBoxSizer(wxVERTICAL
);
281 itemGridSizer4
->Add(itemBoxSizer8
, 0, wxALIGN_CENTER_HORIZONTAL
|wxGROW
, 5);
282 wxStaticText
* itemStaticText9
= new wxStaticText( this, wxID_STATIC
, _("&Style:"), wxDefaultPosition
, wxDefaultSize
, 0 );
283 itemBoxSizer8
->Add(itemStaticText9
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
|wxADJUST_MINSIZE
, 5);
285 wxChoice
* itemChoice10
= new wxChoice( this, wxID_FONT_STYLE
, wxDefaultPosition
, wxDefaultSize
, 3, styles
, 0 );
286 itemChoice10
->SetHelpText(_("The font style."));
288 itemChoice10
->SetToolTip(_("The font style."));
289 itemBoxSizer8
->Add(itemChoice10
, 0, wxALIGN_LEFT
|wxALL
, 5);
291 wxBoxSizer
* itemBoxSizer11
= new wxBoxSizer(wxVERTICAL
);
292 itemGridSizer4
->Add(itemBoxSizer11
, 0, wxALIGN_CENTER_HORIZONTAL
|wxGROW
, 5);
293 wxStaticText
* itemStaticText12
= new wxStaticText( this, wxID_STATIC
, _("&Weight:"), wxDefaultPosition
, wxDefaultSize
, 0 );
294 itemBoxSizer11
->Add(itemStaticText12
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
|wxADJUST_MINSIZE
, 5);
296 wxChoice
* itemChoice13
= new wxChoice( this, wxID_FONT_WEIGHT
, wxDefaultPosition
, wxDefaultSize
, 3, weights
, 0 );
297 itemChoice13
->SetHelpText(_("The font weight."));
299 itemChoice13
->SetToolTip(_("The font weight."));
300 itemBoxSizer11
->Add(itemChoice13
, 0, wxALIGN_LEFT
|wxALL
, 5);
302 wxBoxSizer
* itemBoxSizer14
= new wxBoxSizer(wxVERTICAL
);
303 itemGridSizer4
->Add(itemBoxSizer14
, 0, wxALIGN_CENTER_HORIZONTAL
|wxGROW
, 5);
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 wxChoice
* itemChoice16
= new wxChoice( this, wxID_FONT_COLOUR
, wxDefaultPosition
, wxDefaultSize
, NUM_COLS
, wxColourDialogNames
, 0 );
308 itemChoice16
->SetHelpText(_("The font colour."));
310 itemChoice16
->SetToolTip(_("The font colour."));
311 itemBoxSizer14
->Add(itemChoice16
, 0, wxALIGN_LEFT
|wxALL
, 5);
313 wxBoxSizer
* itemBoxSizer17
= new wxBoxSizer(wxVERTICAL
);
314 itemGridSizer4
->Add(itemBoxSizer17
, 0, wxALIGN_CENTER_HORIZONTAL
|wxGROW
, 5);
315 wxStaticText
* itemStaticText18
= new wxStaticText( this, wxID_STATIC
, _("&Point size:"), wxDefaultPosition
, wxDefaultSize
, 0 );
316 itemBoxSizer17
->Add(itemStaticText18
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
|wxADJUST_MINSIZE
, 5);
318 wxChoice
* itemChoice19
= new wxChoice( this, wxID_FONT_SIZE
, wxDefaultPosition
, wxDefaultSize
, 40, pointSizes
, 0 );
319 itemChoice19
->SetHelpText(_("The font point size."));
321 itemChoice19
->SetToolTip(_("The font point size."));
322 itemBoxSizer17
->Add(itemChoice19
, 0, wxALIGN_LEFT
|wxALL
, 5);
324 wxBoxSizer
* itemBoxSizer20
= new wxBoxSizer(wxVERTICAL
);
325 itemGridSizer4
->Add(itemBoxSizer20
, 0, wxALIGN_LEFT
|wxALIGN_CENTER_VERTICAL
, 5);
326 wxCheckBox
* itemCheckBox21
= new wxCheckBox( this, wxID_FONT_UNDERLINE
, _("&Underline"), wxDefaultPosition
, wxDefaultSize
, 0 );
327 itemCheckBox21
->SetValue(FALSE
);
328 itemCheckBox21
->SetHelpText(_("Whether the font is underlined."));
330 itemCheckBox21
->SetToolTip(_("Whether the font is underlined."));
331 itemBoxSizer20
->Add(itemCheckBox21
, 0, wxALIGN_LEFT
|wxALL
, 5);
333 itemBoxSizer3
->Add(5, 5, 0, wxALIGN_CENTER_HORIZONTAL
|wxALL
, 5);
335 wxStaticText
* itemStaticText23
= new wxStaticText( this, wxID_STATIC
, _("Preview:"), wxDefaultPosition
, wxDefaultSize
, 0 );
336 itemBoxSizer3
->Add(itemStaticText23
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
|wxADJUST_MINSIZE
, 5);
338 wxFontPreviewer
* itemWindow24
= new wxFontPreviewer( this );
339 m_previewer
= itemWindow24
;
340 itemWindow24
->SetHelpText(_("Shows the font preview."));
342 itemWindow24
->SetToolTip(_("Shows the font preview."));
343 itemBoxSizer3
->Add(itemWindow24
, 0, wxGROW
|wxALL
, 5);
345 wxBoxSizer
* itemBoxSizer25
= new wxBoxSizer(wxHORIZONTAL
);
346 itemBoxSizer3
->Add(itemBoxSizer25
, 0, wxGROW
, 5);
347 itemBoxSizer25
->Add(5, 5, 1, wxGROW
|wxALL
, 5);
350 wxButton
* itemButton28
= new wxButton( this, wxID_CANCEL
, _("&Cancel"), wxDefaultPosition
, wxDefaultSize
, 0 );
352 itemButton28
->SetToolTip(_("Click to cancel the font selection."));
353 itemBoxSizer25
->Add(itemButton28
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
355 wxButton
* itemButton27
= new wxButton( this, wxID_OK
, _("&OK"), wxDefaultPosition
, wxDefaultSize
, 0 );
356 itemButton27
->SetDefault();
357 itemButton27
->SetHelpText(_("Click to confirm the font selection."));
359 itemButton27
->SetToolTip(_("Click to confirm the font selection."));
360 itemBoxSizer25
->Add(itemButton27
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
362 wxButton
* itemButton27
= new wxButton( this, wxID_OK
, _("&OK"), wxDefaultPosition
, wxDefaultSize
, 0 );
363 itemButton27
->SetDefault();
364 itemButton27
->SetHelpText(_("Click to confirm the font selection."));
366 itemButton27
->SetToolTip(_("Click to confirm the font selection."));
367 itemBoxSizer25
->Add(itemButton27
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
369 wxButton
* itemButton28
= new wxButton( this, wxID_CANCEL
, _("&Cancel"), wxDefaultPosition
, wxDefaultSize
, 0 );
371 itemButton28
->SetToolTip(_("Click to cancel the font selection."));
372 itemBoxSizer25
->Add(itemButton28
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
375 familyChoice
= (wxChoice
*) FindWindow(wxID_FONT_FAMILY
);
376 styleChoice
= (wxChoice
*) FindWindow(wxID_FONT_STYLE
);
377 weightChoice
= (wxChoice
*) FindWindow(wxID_FONT_WEIGHT
);
378 colourChoice
= (wxChoice
*) FindWindow(wxID_FONT_COLOUR
);
379 pointSizeChoice
= (wxChoice
*) FindWindow(wxID_FONT_SIZE
);
380 underLineCheckBox
= (wxCheckBox
*) FindWindow(wxID_FONT_UNDERLINE
);
382 familyChoice
->SetStringSelection( wxFontFamilyIntToString(dialogFont
.GetFamily()) );
383 styleChoice
->SetStringSelection(wxFontStyleIntToString(dialogFont
.GetStyle()));
384 weightChoice
->SetStringSelection(wxFontWeightIntToString(dialogFont
.GetWeight()));
385 wxString
name(wxTheColourDatabase
->FindName(m_fontData
.GetColour()));
387 colourChoice
->SetStringSelection(name
);
389 colourChoice
->SetStringSelection(wxT("BLACK"));
391 underLineCheckBox
->SetValue(dialogFont
.GetUnderlined());
392 pointSizeChoice
->SetSelection(dialogFont
.GetPointSize()-1);
394 GetSizer()->SetItemMinSize(m_previewer
, 430, 100);
395 GetSizer()->SetSizeHints(this);
396 GetSizer()->Fit(this);
405 // Don't block events any more
410 void wxGenericFontDialog::InitializeFont()
412 int fontFamily
= wxSWISS
;
413 int fontWeight
= wxNORMAL
;
414 int fontStyle
= wxNORMAL
;
416 bool fontUnderline
= false;
418 if (m_fontData
.m_initialFont
.Ok())
420 fontFamily
= m_fontData
.m_initialFont
.GetFamily();
421 fontWeight
= m_fontData
.m_initialFont
.GetWeight();
422 fontStyle
= m_fontData
.m_initialFont
.GetStyle();
423 fontSize
= m_fontData
.m_initialFont
.GetPointSize();
424 fontUnderline
= m_fontData
.m_initialFont
.GetUnderlined();
427 dialogFont
= wxFont(fontSize
, fontFamily
, fontStyle
,
428 fontWeight
, fontUnderline
);
431 m_previewer
->SetFont(dialogFont
);
434 void wxGenericFontDialog::OnChangeFont(wxCommandEvent
& WXUNUSED(event
))
436 if (!m_useEvents
) return;
438 int fontFamily
= wxFontFamilyStringToInt(WXSTRINGCAST familyChoice
->GetStringSelection());
439 int fontWeight
= wxFontWeightStringToInt(WXSTRINGCAST weightChoice
->GetStringSelection());
440 int fontStyle
= wxFontStyleStringToInt(WXSTRINGCAST styleChoice
->GetStringSelection());
441 int fontSize
= wxAtoi(pointSizeChoice
->GetStringSelection());
442 int fontUnderline
= underLineCheckBox
->GetValue();
444 dialogFont
= wxFont(fontSize
, fontFamily
, fontStyle
, fontWeight
, (fontUnderline
!= 0));
445 m_previewer
->SetFont(dialogFont
);
446 if ( !colourChoice
->GetStringSelection().empty() )
448 wxColour col
= wxTheColourDatabase
->Find(colourChoice
->GetStringSelection());
451 m_fontData
.m_fontColour
= col
;
452 m_previewer
->SetForegroundColour(col
);
455 m_previewer
->Refresh();
458 const wxChar
*wxFontWeightIntToString(int weight
)
468 return wxT("Normal");
472 const wxChar
*wxFontStyleIntToString(int style
)
477 return wxT("Italic");
482 return wxT("Normal");
486 const wxChar
*wxFontFamilyIntToString(int family
)
493 return wxT("Decorative");
495 return wxT("Modern");
497 return wxT("Script");
499 return wxT("Teletype");
506 int wxFontFamilyStringToInt(wxChar
*family
)
511 if (wxStrcmp(family
, wxT("Roman")) == 0)
513 else if (wxStrcmp(family
, wxT("Decorative")) == 0)
515 else if (wxStrcmp(family
, wxT("Modern")) == 0)
517 else if (wxStrcmp(family
, wxT("Script")) == 0)
519 else if (wxStrcmp(family
, wxT("Teletype")) == 0)
524 int wxFontStyleStringToInt(wxChar
*style
)
528 if (wxStrcmp(style
, wxT("Italic")) == 0)
530 else if (wxStrcmp(style
, wxT("Slant")) == 0)
536 int wxFontWeightStringToInt(wxChar
*weight
)
540 if (wxStrcmp(weight
, wxT("Bold")) == 0)
542 else if (wxStrcmp(weight
, wxT("Light")) == 0)