1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxFontDialog class for carbon 10.2+.
8 // Copyright: (c) Ryan Norton
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ===========================================================================
14 // ===========================================================================
16 // ---------------------------------------------------------------------------
18 // ---------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
28 #include "wx/cmndata.h"
31 #include "wx/fontdlg.h"
34 #if !USE_NATIVE_FONT_DIALOG_FOR_MACOSX
38 #include "wx/mac/fontdlg.h"
40 #include "wx/fontenum.h"
41 #include "wx/colordlg.h"
42 #include "wx/spinctrl.h"
44 // ---------------------------------------------------------------------------
45 // wxFontDialog stub for mac OS's without a native font dialog
46 // ---------------------------------------------------------------------------
48 static const wxChar
*wxFontFamilyIntToString(int family
);
49 static int wxFontFamilyStringToInt(const wxChar
*family
);
52 //-----------------------------------------------------------------------------
53 // helper class - wxFontPreviewCtrl
54 //-----------------------------------------------------------------------------
56 class WXDLLEXPORT wxFontPreviewCtrl
: public wxWindow
59 wxFontPreviewCtrl(wxWindow
*parent
, wxWindowID id
= wxID_ANY
, const wxPoint
& pos
= wxDefaultPosition
, const wxSize
& sz
= wxDefaultSize
, long style
= 0) :
60 wxWindow(parent
, id
, pos
, sz
, style
)
62 SetBackgroundColour(*wxWHITE
);
66 void OnPaint(wxPaintEvent
& event
);
70 BEGIN_EVENT_TABLE(wxFontPreviewCtrl
, wxWindow
)
71 EVT_PAINT(wxFontPreviewCtrl::OnPaint
)
74 void wxFontPreviewCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
78 wxSize size
= GetSize();
79 wxFont font
= GetFont();
84 // Calculate vertical centre
86 dc
.GetTextExtent( wxT("X"), &w
, &h
);
87 dc
.SetTextForeground(GetForegroundColour());
88 dc
.SetClippingRegion(2, 2, size
.x
-4, size
.y
-4);
89 dc
.DrawText(_("ABCDEFGabcdefg12345"),
91 dc
.DestroyClippingRegion();
96 * A control for displaying a small preview of a colour or bitmap
99 class wxFontColourSwatchCtrl
: public wxControl
101 DECLARE_CLASS(wxFontColourSwatchCtrl
)
103 wxFontColourSwatchCtrl(wxWindow
* parent
, wxWindowID id
, const wxPoint
& pos
= wxDefaultPosition
, const wxSize
& size
= wxDefaultSize
, long style
= 0);
104 ~wxFontColourSwatchCtrl();
106 void OnPaint(wxPaintEvent
& event
);
107 void OnMouseEvent(wxMouseEvent
& event
);
109 void SetColour(const wxColour
& colour
) { m_colour
= colour
; SetBackgroundColour(m_colour
); }
111 wxColour
& GetColour() { return m_colour
; }
113 virtual wxSize
DoGetBestSize() const { return GetSize(); }
118 DECLARE_EVENT_TABLE()
122 * A control for displaying a small preview of a colour or bitmap
125 BEGIN_EVENT_TABLE(wxFontColourSwatchCtrl
, wxControl
)
126 EVT_MOUSE_EVENTS(wxFontColourSwatchCtrl::OnMouseEvent
)
129 IMPLEMENT_CLASS(wxFontColourSwatchCtrl
, wxControl
)
131 wxFontColourSwatchCtrl::wxFontColourSwatchCtrl(wxWindow
* parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
, long style
):
132 wxControl(parent
, id
, pos
, size
, style
)
134 SetColour(* wxWHITE
);
135 SetBackgroundStyle(wxBG_STYLE_COLOUR
);
138 wxFontColourSwatchCtrl::~wxFontColourSwatchCtrl()
142 void wxFontColourSwatchCtrl::OnMouseEvent(wxMouseEvent
& event
)
144 if (event
.LeftDown())
146 wxWindow
* parent
= GetParent();
147 while (parent
!= NULL
&& !parent
->IsKindOf(CLASSINFO(wxDialog
)) && !parent
->IsKindOf(CLASSINFO(wxFrame
)))
148 parent
= parent
->GetParent();
151 data
.SetChooseFull(TRUE
);
152 data
.SetColour(m_colour
);
153 wxColourDialog
*dialog
= new wxColourDialog(parent
, &data
);
154 // Crashes on wxMac (no m_peer)
156 dialog
->SetTitle(_("Background colour"));
158 if (dialog
->ShowModal() == wxID_OK
)
160 wxColourData retData
= dialog
->GetColourData();
161 m_colour
= retData
.GetColour();
162 SetBackgroundColour(m_colour
);
167 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, GetId());
168 GetEventHandler()->ProcessEvent(event
);
173 * wxFontDialog type definition
176 IMPLEMENT_DYNAMIC_CLASS( wxFontDialog
, wxDialog
)
179 * wxFontDialog event table definition
182 BEGIN_EVENT_TABLE( wxFontDialog
, wxDialog
)
183 EVT_LISTBOX( wxID_FONTDIALOG_FACENAME
, wxFontDialog::OnFontdialogFacenameSelected
)
184 EVT_SPINCTRL( wxID_FONTDIALOG_FONTSIZE
, wxFontDialog::OnFontdialogFontsizeUpdated
)
185 EVT_TEXT( wxID_FONTDIALOG_FONTSIZE
, wxFontDialog::OnFontdialogFontsizeTextUpdated
)
186 EVT_CHECKBOX( wxID_FONTDIALOG_BOLD
, wxFontDialog::OnFontdialogBoldClick
)
187 EVT_CHECKBOX( wxID_FONTDIALOG_ITALIC
, wxFontDialog::OnFontdialogItalicClick
)
188 EVT_CHECKBOX( wxID_FONTDIALOG_UNDERLINED
, wxFontDialog::OnFontdialogUnderlinedClick
)
189 EVT_BUTTON( wxID_OK
, wxFontDialog::OnOkClick
)
190 EVT_BUTTON(wxID_FONTDIALOG_COLOUR
, wxFontDialog::OnColourChanged
)
194 * wxFontDialog constructors
197 wxFontDialog::wxFontDialog( )
199 m_dialogParent
= NULL
;
202 wxFontDialog::wxFontDialog(wxWindow
* parent
, const wxFontData
& fontData
)
204 m_dialogParent
= NULL
;
206 Create(parent
, fontData
);
209 wxFontDialog::~wxFontDialog()
215 * wxFontDialog creator
218 bool wxFontDialog::Create(wxWindow
* parent
, const wxFontData
& fontData
)
220 m_fontData
= fontData
;
221 m_suppressUpdates
= false;
223 wxString caption
= _("Font");
225 m_facenameCtrl
= NULL
;
229 m_underlinedCtrl
= NULL
;
231 m_previewCtrl
= NULL
;
235 SetExtraStyle(GetExtraStyle()|wxWS_EX_BLOCK_EVENTS
);
236 wxDialog::Create( parent
, wxID_ANY
, caption
, wxDefaultPosition
, wxDefaultSize
, wxDEFAULT_DIALOG_STYLE
|wxRESIZE_BORDER
);
239 GetSizer()->Fit(this);
240 GetSizer()->SetSizeHints(this);
247 * Control creation for wxFontDialog
250 void wxFontDialog::CreateControls()
252 wxFontDialog
* itemDialog1
= this;
254 wxBoxSizer
* itemBoxSizer2
= new wxBoxSizer(wxVERTICAL
);
255 itemDialog1
->SetSizer(itemBoxSizer2
);
257 wxBoxSizer
* itemBoxSizer3
= new wxBoxSizer(wxVERTICAL
);
258 itemBoxSizer2
->Add(itemBoxSizer3
, 1, wxGROW
|wxALL
, 5);
260 wxFlexGridSizer
* itemFlexGridSizer4
= new wxFlexGridSizer(6, 2, 10, 0);
261 itemFlexGridSizer4
->AddGrowableRow(4);
262 itemFlexGridSizer4
->AddGrowableCol(1);
263 itemBoxSizer3
->Add(itemFlexGridSizer4
, 1, wxGROW
|wxALL
, 5);
265 wxStaticText
* itemStaticText5
= new wxStaticText( itemDialog1
, wxID_STATIC
, _("Font:"), wxDefaultPosition
, wxDefaultSize
, 0 );
266 itemFlexGridSizer4
->Add(itemStaticText5
, 0, wxALIGN_RIGHT
|wxALIGN_TOP
|wxALL
|wxADJUST_MINSIZE
, 5);
268 wxBoxSizer
* itemBoxSizer6
= new wxBoxSizer(wxVERTICAL
);
269 itemFlexGridSizer4
->Add(itemBoxSizer6
, 0, wxGROW
|wxGROW
, 5);
271 wxString
* m_facenameCtrlStrings
= NULL
;
272 m_facenameCtrl
= new wxListBox( itemDialog1
, wxID_FONTDIALOG_FACENAME
, wxDefaultPosition
, wxSize(320, 100), 0, m_facenameCtrlStrings
, wxLB_SINGLE
);
273 itemBoxSizer6
->Add(m_facenameCtrl
, 0, wxGROW
|wxALL
, 5);
275 wxStaticText
* itemStaticText8
= new wxStaticText( itemDialog1
, wxID_STATIC
, _("Size:"), wxDefaultPosition
, wxDefaultSize
, 0 );
276 itemFlexGridSizer4
->Add(itemStaticText8
, 0, wxALIGN_RIGHT
|wxALIGN_CENTER_VERTICAL
|wxALL
|wxADJUST_MINSIZE
, 5);
278 m_sizeCtrl
= new wxSpinCtrl( itemDialog1
, wxID_FONTDIALOG_FONTSIZE
, _T("12"), wxDefaultPosition
, wxSize(60, -1), wxSP_ARROW_KEYS
, 1, 300, 12 );
279 m_sizeCtrl
->SetHelpText(_("The font size in points."));
281 m_sizeCtrl
->SetToolTip(_("The font size in points."));
282 itemFlexGridSizer4
->Add(m_sizeCtrl
, 0, wxALIGN_LEFT
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
284 wxStaticText
* itemStaticText10
= new wxStaticText( itemDialog1
, wxID_STATIC
, _("Style:"), wxDefaultPosition
, wxDefaultSize
, 0 );
285 itemFlexGridSizer4
->Add(itemStaticText10
, 0, wxALIGN_RIGHT
|wxALIGN_CENTER_VERTICAL
|wxALL
|wxADJUST_MINSIZE
, 5);
287 wxBoxSizer
* itemBoxSizer11
= new wxBoxSizer(wxHORIZONTAL
);
288 itemFlexGridSizer4
->Add(itemBoxSizer11
, 0, wxALIGN_LEFT
|wxALIGN_CENTER_VERTICAL
, 5);
290 m_boldCtrl
= new wxCheckBox( itemDialog1
, wxID_FONTDIALOG_BOLD
, _("Bold"), wxDefaultPosition
, wxDefaultSize
, wxCHK_2STATE
);
291 m_boldCtrl
->SetValue(false);
292 m_boldCtrl
->SetHelpText(_("Check to make the font bold."));
294 m_boldCtrl
->SetToolTip(_("Check to make the font bold."));
295 itemBoxSizer11
->Add(m_boldCtrl
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
297 m_italicCtrl
= new wxCheckBox( itemDialog1
, wxID_FONTDIALOG_ITALIC
, _("Italic"), wxDefaultPosition
, wxDefaultSize
, wxCHK_2STATE
);
298 m_italicCtrl
->SetValue(false);
299 m_italicCtrl
->SetHelpText(_("Check to make the font italic."));
301 m_italicCtrl
->SetToolTip(_("Check to make the font italic."));
302 itemBoxSizer11
->Add(m_italicCtrl
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
304 if (m_fontData
.GetEnableEffects())
306 m_underlinedCtrl
= new wxCheckBox( itemDialog1
, wxID_FONTDIALOG_UNDERLINED
, _("Underlined"), wxDefaultPosition
, wxDefaultSize
, wxCHK_2STATE
);
307 m_underlinedCtrl
->SetValue(false);
308 m_underlinedCtrl
->SetHelpText(_("Check to make the font underlined."));
310 m_underlinedCtrl
->SetToolTip(_("Check to make the font underlined."));
311 itemBoxSizer11
->Add(m_underlinedCtrl
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
314 if (m_fontData
.GetEnableEffects())
316 wxStaticText
* itemStaticText15
= new wxStaticText( itemDialog1
, wxID_STATIC
, _("Colour:"), wxDefaultPosition
, wxDefaultSize
, 0 );
317 itemFlexGridSizer4
->Add(itemStaticText15
, 0, wxALIGN_RIGHT
|wxALIGN_TOP
|wxALL
|wxADJUST_MINSIZE
, 5);
319 m_colourCtrl
= new wxFontColourSwatchCtrl( itemDialog1
, wxID_FONTDIALOG_COLOUR
, wxDefaultPosition
, wxSize(-1, 30), wxSUNKEN_BORDER
|wxTAB_TRAVERSAL
);
320 m_colourCtrl
->SetHelpText(_("Click to change the font colour."));
322 m_colourCtrl
->SetToolTip(_("Click to change the font colour."));
323 itemFlexGridSizer4
->Add(m_colourCtrl
, 0, wxGROW
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
326 wxStaticText
* itemStaticText17
= new wxStaticText( itemDialog1
, wxID_STATIC
, _("Preview:"), wxDefaultPosition
, wxDefaultSize
, 0 );
327 itemFlexGridSizer4
->Add(itemStaticText17
, 0, wxALIGN_RIGHT
|wxALIGN_TOP
|wxALL
|wxADJUST_MINSIZE
, 5);
329 m_previewCtrl
= new wxFontPreviewCtrl( itemDialog1
, wxID_FONTDIALOG_PREVIEW
, wxDefaultPosition
, wxSize(-1, 70), wxSUNKEN_BORDER
|wxTAB_TRAVERSAL
);
330 m_previewCtrl
->SetHelpText(_("Shows a preview of the font."));
332 m_previewCtrl
->SetToolTip(_("Shows a preview of the font."));
333 itemFlexGridSizer4
->Add(m_previewCtrl
, 1, wxGROW
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
335 wxBoxSizer
* itemBoxSizer19
= new wxBoxSizer(wxHORIZONTAL
);
336 itemBoxSizer3
->Add(itemBoxSizer19
, 0, wxALIGN_RIGHT
|wxALL
, 5);
338 wxButton
* itemButton20
= new wxButton( itemDialog1
, wxID_CANCEL
, _("&Cancel"), wxDefaultPosition
, wxDefaultSize
, 0 );
339 itemButton20
->SetHelpText(_("Click to cancel changes to the font."));
341 itemButton20
->SetToolTip(_("Click to cancel changes to the font."));
342 itemBoxSizer19
->Add(itemButton20
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
344 wxButton
* itemButton21
= new wxButton( itemDialog1
, wxID_OK
, _("&OK"), wxDefaultPosition
, wxDefaultSize
, 0 );
345 itemButton21
->SetDefault();
346 itemButton21
->SetHelpText(_("Click to confirm changes to the font."));
348 itemButton21
->SetToolTip(_("Click to confirm changes to the font."));
349 itemBoxSizer19
->Add(itemButton21
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
351 wxFontEnumerator enumerator
;
352 enumerator
.EnumerateFacenames();
353 wxArrayString
* facenames
= enumerator
.GetFacenames();
356 facenames
->Add(_("<Any>"));
357 facenames
->Add(_("<Any Roman>"));
358 facenames
->Add(_("<Any Decorative>"));
359 facenames
->Add(_("<Any Modern>"));
360 facenames
->Add(_("<Any Script>"));
361 facenames
->Add(_("<Any Swiss>"));
362 facenames
->Add(_("<Any Teletype>"));
364 m_facenameCtrl
->Append(*facenames
);
367 InitializeControls();
368 m_previewCtrl
->SetFont(m_dialogFont
);
369 if (m_fontData
.GetColour().Ok())
371 m_previewCtrl
->SetForegroundColour(m_fontData
.GetColour());
373 m_previewCtrl
->Refresh();
377 * wxEVT_COMMAND_SPINCTRL_UPDATED event handler for wxID_FONTDIALOG_FONTSIZE
380 void wxFontDialog::OnFontdialogFontsizeUpdated( wxSpinEvent
& WXUNUSED(event
) )
386 * wxEVT_COMMAND_TEXT_UPDATED event handler for wxID_FONTDIALOG_FONTSIZE
389 void wxFontDialog::OnFontdialogFontsizeTextUpdated( wxCommandEvent
& WXUNUSED(event
) )
395 * wxEVT_COMMAND_CHECKBOX_CLICKED event handler for wxID_FONTDIALOG_BOLD
398 void wxFontDialog::OnFontdialogBoldClick( wxCommandEvent
& WXUNUSED(event
) )
404 * wxEVT_COMMAND_CHECKBOX_CLICKED event handler for wxID_FONTDIALOG_ITALIC
407 void wxFontDialog::OnFontdialogItalicClick( wxCommandEvent
& WXUNUSED(event
) )
413 * wxEVT_COMMAND_CHECKBOX_CLICKED event handler for wxID_FONTDIALOG_UNDERLINED
416 void wxFontDialog::OnFontdialogUnderlinedClick( wxCommandEvent
& WXUNUSED(event
) )
422 * wxEVT_COMMAND_BUTTON_CLICKED event handler for wxID_OK
425 void wxFontDialog::OnOkClick( wxCommandEvent
& event
)
432 * wxEVT_COMMAND_LISTBOX_SELECTED event handler for wxID_FONTDIALOG_FACENAME
435 void wxFontDialog::OnFontdialogFacenameSelected( wxCommandEvent
& WXUNUSED(event
) )
440 void wxFontDialog::OnColourChanged(wxCommandEvent
& WXUNUSED(event
))
442 m_fontData
.SetColour(m_colourCtrl
->GetColour());
443 m_previewCtrl
->SetForegroundColour(m_colourCtrl
->GetColour());
444 m_previewCtrl
->Refresh();
448 * Should we show tooltips?
451 bool wxFontDialog::ShowToolTips()
456 void wxFontDialog::InitializeFont()
458 int fontFamily
= wxSWISS
;
459 int fontWeight
= wxNORMAL
;
460 int fontStyle
= wxNORMAL
;
462 bool fontUnderline
= false;
465 if (m_fontData
.m_initialFont
.Ok())
467 fontFamily
= m_fontData
.m_initialFont
.GetFamily();
468 fontWeight
= m_fontData
.m_initialFont
.GetWeight();
469 fontStyle
= m_fontData
.m_initialFont
.GetStyle();
470 fontSize
= m_fontData
.m_initialFont
.GetPointSize();
471 fontUnderline
= m_fontData
.m_initialFont
.GetUnderlined();
472 fontName
= m_fontData
.m_initialFont
.GetFaceName();
475 m_dialogFont
= wxFont(fontSize
, fontFamily
, fontStyle
,
476 fontWeight
, fontUnderline
, fontName
);
479 m_previewCtrl
->SetFont(m_dialogFont
);
481 m_fontData
.SetChosenFont(m_dialogFont
);
484 /// Set controls according to current font
485 void wxFontDialog::InitializeControls()
487 m_suppressUpdates
= true;
489 if (m_underlinedCtrl
)
490 m_underlinedCtrl
->SetValue(m_dialogFont
.GetUnderlined());
492 m_boldCtrl
->SetValue(m_dialogFont
.GetWeight() == wxBOLD
);
493 m_italicCtrl
->SetValue(m_dialogFont
.GetStyle() == wxITALIC
);
494 m_sizeCtrl
->SetValue(m_dialogFont
.GetPointSize());
496 wxString facename
= m_dialogFont
.GetFaceName();
497 if (facename
.empty() || m_facenameCtrl
->FindString(facename
) == wxNOT_FOUND
)
499 facename
= wxFontFamilyIntToString(m_dialogFont
.GetFamily());
501 m_facenameCtrl
->SetStringSelection(facename
);
503 if (m_colourCtrl
&& m_fontData
.GetColour().Ok())
505 m_colourCtrl
->SetColour(m_fontData
.GetColour());
506 m_colourCtrl
->Refresh();
509 m_suppressUpdates
= false;
512 /// Respond to font change
513 void wxFontDialog::ChangeFont()
515 if (m_suppressUpdates
)
518 bool underlined
= m_underlinedCtrl
? m_underlinedCtrl
->GetValue() : false;
519 bool italic
= m_italicCtrl
->GetValue();
520 bool bold
= m_boldCtrl
->GetValue();
521 int size
= m_sizeCtrl
->GetValue();
522 wxString facename
= m_facenameCtrl
->GetStringSelection();
524 int family
= wxFontFamilyStringToInt(facename
);
528 facename
= wxEmptyString
;
530 m_dialogFont
= wxFont(size
, family
, italic
? wxITALIC
: wxNORMAL
, bold
? wxBOLD
: wxNORMAL
,
531 underlined
, facename
);
533 m_fontData
.SetChosenFont(m_dialogFont
);
535 m_previewCtrl
->SetFont(m_dialogFont
);
536 m_previewCtrl
->Refresh();
539 void wxFontDialog::SetData(const wxFontData
& fontdata
)
541 m_fontData
= fontdata
;
544 bool wxFontDialog::IsShown() const
549 int wxFontDialog::ShowModal()
551 return wxDialog::ShowModal();
554 void wxFontDialog::OnPanelClose()
558 const wxChar
*wxFontFamilyIntToString(int family
)
563 return _("<Any Roman>");
565 return _("<Any Decorative>");
567 return _("<Any Modern>");
569 return _("<Any Script>");
571 return _("<Any Teletype>");
574 return _("<Any Swiss>");
578 int wxFontFamilyStringToInt(const wxChar
*family
)
583 if (wxStrcmp(family
, _("<Any Roman")) == 0)
585 else if (wxStrcmp(family
, _("<Any Decorative>")) == 0)
587 else if (wxStrcmp(family
, _("<Any Modern>")) == 0)
589 else if (wxStrcmp(family
, _("<Any Script>")) == 0)
591 else if (wxStrcmp(family
, _("<Any Teletype>")) == 0)
593 else if (wxStrcmp(family
, _("<Any Swiss>")) == 0)
598 #endif // !USE_NATIVE_FONT_DIALOG_FOR_MACOSX