1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/richtext/richeditfontpage.cpp
3 // Purpose: Font page for wxRichTextFormattingDialog
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "../../include/wx/richtext/richtextfontpage.h"
15 * wxRichTextFontPage type definition
18 IMPLEMENT_DYNAMIC_CLASS( wxRichTextFontPage
, wxPanel
)
21 * wxRichTextFontPage event table definition
24 BEGIN_EVENT_TABLE( wxRichTextFontPage
, wxPanel
)
25 EVT_LISTBOX( ID_RICHTEXTFONTPAGE_FACELISTBOX
, wxRichTextFontPage::OnFaceListBoxSelected
)
26 EVT_BUTTON( ID_RICHTEXTFONTPAGE_COLOURCTRL
, wxRichTextFontPage::OnColourClicked
)
28 ////@begin wxRichTextFontPage event table entries
29 EVT_TEXT( ID_RICHTEXTFONTPAGE_FACETEXTCTRL
, wxRichTextFontPage::OnFaceTextCtrlUpdated
)
31 EVT_TEXT( ID_RICHTEXTFONTPAGE_SIZETEXTCTRL
, wxRichTextFontPage::OnSizeTextCtrlUpdated
)
33 EVT_LISTBOX( ID_RICHTEXTFONTPAGE_SIZELISTBOX
, wxRichTextFontPage::OnSizeListBoxSelected
)
35 EVT_COMBOBOX( ID_RICHTEXTFONTPAGE_STYLECTRL
, wxRichTextFontPage::OnStyleCtrlSelected
)
37 EVT_COMBOBOX( ID_RICHTEXTFONTPAGE_WEIGHTCTRL
, wxRichTextFontPage::OnWeightCtrlSelected
)
39 EVT_COMBOBOX( ID_RICHTEXTFONTPAGE_UNDERLINING_CTRL
, wxRichTextFontPage::OnUnderliningCtrlSelected
)
41 ////@end wxRichTextFontPage event table entries
46 * wxRichTextFontPage constructors
49 wxRichTextFontPage::wxRichTextFontPage( )
54 wxRichTextFontPage::wxRichTextFontPage( wxWindow
* parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
, long style
)
57 Create(parent
, id
, pos
, size
, style
);
64 void wxRichTextFontPage::Init()
67 m_colourPresent
= false;
69 ////@begin wxRichTextFontPage member initialisation
70 m_faceTextCtrl
= NULL
;
72 m_sizeTextCtrl
= NULL
;
76 m_underliningCtrl
= NULL
;
79 ////@end wxRichTextFontPage member initialisation
83 * wxRichTextFontPage creator
86 bool wxRichTextFontPage::Create( wxWindow
* parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
, long style
)
88 ////@begin wxRichTextFontPage creation
89 wxPanel::Create( parent
, id
, pos
, size
, style
);
94 GetSizer()->SetSizeHints(this);
97 ////@end wxRichTextFontPage creation
102 * Control creation for wxRichTextFontPage
105 void wxRichTextFontPage::CreateControls()
107 ////@begin wxRichTextFontPage content construction
108 wxRichTextFontPage
* itemPanel1
= this;
110 wxBoxSizer
* itemBoxSizer2
= new wxBoxSizer(wxVERTICAL
);
111 itemPanel1
->SetSizer(itemBoxSizer2
);
113 wxBoxSizer
* itemBoxSizer3
= new wxBoxSizer(wxVERTICAL
);
114 itemBoxSizer2
->Add(itemBoxSizer3
, 1, wxGROW
|wxALL
, 5);
116 wxBoxSizer
* itemBoxSizer4
= new wxBoxSizer(wxHORIZONTAL
);
117 itemBoxSizer3
->Add(itemBoxSizer4
, 1, wxGROW
, 5);
119 wxBoxSizer
* itemBoxSizer5
= new wxBoxSizer(wxVERTICAL
);
120 itemBoxSizer4
->Add(itemBoxSizer5
, 1, wxGROW
, 5);
122 wxStaticText
* itemStaticText6
= new wxStaticText( itemPanel1
, wxID_STATIC
, _("&Font:"), wxDefaultPosition
, wxDefaultSize
, 0 );
123 itemBoxSizer5
->Add(itemStaticText6
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
|wxADJUST_MINSIZE
, 5);
125 m_faceTextCtrl
= new wxTextCtrl( itemPanel1
, ID_RICHTEXTFONTPAGE_FACETEXTCTRL
, _T(""), wxDefaultPosition
, wxDefaultSize
, 0 );
126 m_faceTextCtrl
->SetHelpText(_("Type a font name."));
128 m_faceTextCtrl
->SetToolTip(_("Type a font name."));
129 itemBoxSizer5
->Add(m_faceTextCtrl
, 0, wxGROW
|wxLEFT
|wxRIGHT
|wxTOP
, 5);
131 m_faceListBox
= new wxRichTextFontListBox( itemPanel1
, ID_RICHTEXTFONTPAGE_FACELISTBOX
, wxDefaultPosition
, wxSize(200, 140), wxSIMPLE_BORDER
);
132 m_faceListBox
->SetHelpText(_("Lists the available fonts."));
134 m_faceListBox
->SetToolTip(_("Lists the available fonts."));
135 itemBoxSizer5
->Add(m_faceListBox
, 1, wxGROW
|wxALL
|wxFIXED_MINSIZE
, 5);
137 wxBoxSizer
* itemBoxSizer9
= new wxBoxSizer(wxVERTICAL
);
138 itemBoxSizer4
->Add(itemBoxSizer9
, 0, wxGROW
, 5);
140 wxStaticText
* itemStaticText10
= new wxStaticText( itemPanel1
, wxID_STATIC
, _("&Size:"), wxDefaultPosition
, wxDefaultSize
, 0 );
141 itemBoxSizer9
->Add(itemStaticText10
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
|wxADJUST_MINSIZE
, 5);
143 m_sizeTextCtrl
= new wxTextCtrl( itemPanel1
, ID_RICHTEXTFONTPAGE_SIZETEXTCTRL
, _T(""), wxDefaultPosition
, wxSize(50, -1), 0 );
144 m_sizeTextCtrl
->SetHelpText(_("Type a size in points."));
146 m_sizeTextCtrl
->SetToolTip(_("Type a size in points."));
147 itemBoxSizer9
->Add(m_sizeTextCtrl
, 0, wxGROW
|wxLEFT
|wxRIGHT
|wxTOP
, 5);
149 wxString
* m_sizeListBoxStrings
= NULL
;
150 m_sizeListBox
= new wxListBox( itemPanel1
, ID_RICHTEXTFONTPAGE_SIZELISTBOX
, wxDefaultPosition
, wxSize(50, -1), 0, m_sizeListBoxStrings
, wxLB_SINGLE
);
151 m_sizeListBox
->SetHelpText(_("Lists font sizes in points."));
153 m_sizeListBox
->SetToolTip(_("Lists font sizes in points."));
154 itemBoxSizer9
->Add(m_sizeListBox
, 1, wxALIGN_CENTER_HORIZONTAL
|wxALL
|wxFIXED_MINSIZE
, 5);
156 wxBoxSizer
* itemBoxSizer13
= new wxBoxSizer(wxHORIZONTAL
);
157 itemBoxSizer3
->Add(itemBoxSizer13
, 0, wxGROW
, 5);
159 wxBoxSizer
* itemBoxSizer14
= new wxBoxSizer(wxVERTICAL
);
160 itemBoxSizer13
->Add(itemBoxSizer14
, 1, wxGROW
, 5);
162 wxStaticText
* itemStaticText15
= new wxStaticText( itemPanel1
, wxID_STATIC
, _("Font st&yle:"), wxDefaultPosition
, wxDefaultSize
, 0 );
163 itemBoxSizer14
->Add(itemStaticText15
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
|wxADJUST_MINSIZE
, 5);
165 wxString
* m_styleCtrlStrings
= NULL
;
166 m_styleCtrl
= new wxComboBox( itemPanel1
, ID_RICHTEXTFONTPAGE_STYLECTRL
, _T(""), wxDefaultPosition
, wxDefaultSize
, 0, m_styleCtrlStrings
, wxCB_READONLY
);
167 m_styleCtrl
->SetHelpText(_("Select regular or italic style."));
169 m_styleCtrl
->SetToolTip(_("Select regular or italic style."));
170 itemBoxSizer14
->Add(m_styleCtrl
, 0, wxGROW
|wxALL
, 5);
172 wxBoxSizer
* itemBoxSizer17
= new wxBoxSizer(wxVERTICAL
);
173 itemBoxSizer13
->Add(itemBoxSizer17
, 1, wxGROW
, 5);
175 wxStaticText
* itemStaticText18
= new wxStaticText( itemPanel1
, wxID_STATIC
, _("Font &weight:"), wxDefaultPosition
, wxDefaultSize
, 0 );
176 itemBoxSizer17
->Add(itemStaticText18
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
|wxADJUST_MINSIZE
, 5);
178 wxString
* m_weightCtrlStrings
= NULL
;
179 m_weightCtrl
= new wxComboBox( itemPanel1
, ID_RICHTEXTFONTPAGE_WEIGHTCTRL
, _T(""), wxDefaultPosition
, wxDefaultSize
, 0, m_weightCtrlStrings
, wxCB_READONLY
);
180 m_weightCtrl
->SetHelpText(_("Select regular or bold."));
182 m_weightCtrl
->SetToolTip(_("Select regular or bold."));
183 itemBoxSizer17
->Add(m_weightCtrl
, 0, wxGROW
|wxALL
, 5);
185 wxBoxSizer
* itemBoxSizer20
= new wxBoxSizer(wxVERTICAL
);
186 itemBoxSizer13
->Add(itemBoxSizer20
, 1, wxGROW
, 5);
188 wxStaticText
* itemStaticText21
= new wxStaticText( itemPanel1
, wxID_STATIC
, _("&Underlining:"), wxDefaultPosition
, wxDefaultSize
, 0 );
189 itemBoxSizer20
->Add(itemStaticText21
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
|wxADJUST_MINSIZE
, 5);
191 wxString
* m_underliningCtrlStrings
= NULL
;
192 m_underliningCtrl
= new wxComboBox( itemPanel1
, ID_RICHTEXTFONTPAGE_UNDERLINING_CTRL
, _T(""), wxDefaultPosition
, wxDefaultSize
, 0, m_underliningCtrlStrings
, wxCB_READONLY
);
193 m_underliningCtrl
->SetHelpText(_("Select underlining or no underlining."));
195 m_underliningCtrl
->SetToolTip(_("Select underlining or no underlining."));
196 itemBoxSizer20
->Add(m_underliningCtrl
, 0, wxGROW
|wxALL
, 5);
198 wxBoxSizer
* itemBoxSizer23
= new wxBoxSizer(wxVERTICAL
);
199 itemBoxSizer13
->Add(itemBoxSizer23
, 0, wxGROW
, 5);
201 wxStaticText
* itemStaticText24
= new wxStaticText( itemPanel1
, wxID_STATIC
, _("&Colour:"), wxDefaultPosition
, wxDefaultSize
, 0 );
202 itemBoxSizer23
->Add(itemStaticText24
, 0, wxALIGN_LEFT
|wxLEFT
|wxRIGHT
|wxTOP
|wxADJUST_MINSIZE
, 5);
204 m_colourCtrl
= new wxRichTextColourSwatchCtrl( itemPanel1
, ID_RICHTEXTFONTPAGE_COLOURCTRL
, wxDefaultPosition
, wxSize(40, 20), wxSIMPLE_BORDER
);
205 m_colourCtrl
->SetHelpText(_("Click to change the text colour."));
207 m_colourCtrl
->SetToolTip(_("Click to change the text colour."));
208 itemBoxSizer23
->Add(m_colourCtrl
, 0, wxALIGN_LEFT
|wxALL
, 5);
210 itemBoxSizer3
->Add(5, 5, 0, wxALIGN_CENTER_HORIZONTAL
|wxALL
, 5);
212 m_previewCtrl
= new wxRichTextFontPreviewCtrl( itemPanel1
, ID_RICHTEXTFONTPAGE_PREVIEWCTRL
, wxDefaultPosition
, wxSize(100, 80), wxSIMPLE_BORDER
);
213 m_previewCtrl
->SetHelpText(_("Shows a preview of the font settings."));
215 m_previewCtrl
->SetToolTip(_("Shows a preview of the font settings."));
216 itemBoxSizer3
->Add(m_previewCtrl
, 0, wxGROW
|wxALL
, 5);
218 ////@end wxRichTextFontPage content construction
220 m_faceListBox
->UpdateFonts();
222 m_styleCtrl
->Append(_("Regular"));
223 m_styleCtrl
->Append(_("Italic"));
225 m_weightCtrl
->Append(_("Regular"));
226 m_weightCtrl
->Append(_("Bold"));
228 m_underliningCtrl
->Append(_("Not underlined"));
229 m_underliningCtrl
->Append(_("Underlined"));
233 for (i
= 8; i
< 40; i
++)
235 nStr
.Printf(wxT("%d"), i
);
236 m_sizeListBox
->Append(nStr
);
238 m_sizeListBox
->Append(wxT("48"));
239 m_sizeListBox
->Append(wxT("72"));
242 /// Transfer data from/to window
243 bool wxRichTextFontPage::TransferDataFromWindow()
245 wxPanel::TransferDataFromWindow();
247 wxTextAttrEx
* attr
= GetAttributes();
249 if (m_faceListBox
->GetSelection() != wxNOT_FOUND
)
251 wxString faceName
= m_faceListBox
->GetFaceName(m_faceListBox
->GetSelection());
252 if (!faceName
.IsEmpty())
254 wxFont
font(attr
->GetFont().Ok() ? attr
->GetFont() : *wxNORMAL_FONT
);
255 font
.SetFaceName(faceName
);
256 wxSetFontPreservingStyles(*attr
, font
);
257 attr
->SetFlags(attr
->GetFlags() | wxTEXT_ATTR_FONT_FACE
);
261 attr
->SetFlags(attr
->GetFlags() & (~ wxTEXT_ATTR_FONT_FACE
));
263 wxString strSize
= m_sizeTextCtrl
->GetValue();
264 if (!strSize
.IsEmpty())
266 int sz
= wxAtoi(strSize
);
269 wxFont
font(attr
->GetFont().Ok() ? attr
->GetFont() : *wxNORMAL_FONT
);
270 font
.SetPointSize(sz
);
271 wxSetFontPreservingStyles(*attr
, font
);
272 attr
->SetFlags(attr
->GetFlags() | wxTEXT_ATTR_FONT_SIZE
);
276 attr
->SetFlags(attr
->GetFlags() & (~ wxTEXT_ATTR_FONT_SIZE
));
278 if (m_styleCtrl
->GetSelection() != wxNOT_FOUND
)
281 if (m_styleCtrl
->GetStringSelection() == _("Italic"))
286 wxFont
font(attr
->GetFont().Ok() ? attr
->GetFont() : *wxNORMAL_FONT
);
287 font
.SetStyle(style
);
288 wxSetFontPreservingStyles(*attr
, font
);
289 attr
->SetFlags(attr
->GetFlags() | wxTEXT_ATTR_FONT_ITALIC
);
292 attr
->SetFlags(attr
->GetFlags() & (~ wxTEXT_ATTR_FONT_ITALIC
));
294 if (m_weightCtrl
->GetSelection() != wxNOT_FOUND
)
297 if (m_weightCtrl
->GetStringSelection() == _("Bold"))
302 wxFont
font(attr
->GetFont().Ok() ? attr
->GetFont() : *wxNORMAL_FONT
);
303 font
.SetWeight(weight
);
304 wxSetFontPreservingStyles(*attr
, font
);
305 attr
->SetFlags(attr
->GetFlags() | wxTEXT_ATTR_FONT_WEIGHT
);
308 attr
->SetFlags(attr
->GetFlags() & (~ wxTEXT_ATTR_FONT_WEIGHT
));
310 if (m_underliningCtrl
->GetSelection() != wxNOT_FOUND
)
313 if (m_underliningCtrl
->GetStringSelection() == _("Underlined"))
318 wxFont
font(attr
->GetFont().Ok() ? attr
->GetFont() : *wxNORMAL_FONT
);
319 font
.SetUnderlined(underlined
);
320 wxSetFontPreservingStyles(*attr
, font
);
321 attr
->SetFlags(attr
->GetFlags() | wxTEXT_ATTR_FONT_UNDERLINE
);
324 attr
->SetFlags(attr
->GetFlags() & (~ wxTEXT_ATTR_FONT_UNDERLINE
));
328 attr
->SetTextColour(m_colourCtrl
->GetBackgroundColour());
331 attr
->SetFlags(attr
->GetFlags() & (~ wxTEXT_ATTR_TEXT_COLOUR
));
336 bool wxRichTextFontPage::TransferDataToWindow()
338 wxPanel::TransferDataToWindow();
341 wxTextAttrEx
* attr
= GetAttributes();
343 if (attr
->HasFont() && attr
->HasFaceName())
345 m_faceTextCtrl
->SetValue(attr
->GetFont().GetFaceName());
346 m_faceListBox
->SetFaceNameSelection(attr
->GetFont().GetFaceName());
350 m_faceTextCtrl
->SetValue(wxEmptyString
);
351 m_faceListBox
->SetFaceNameSelection(wxEmptyString
);
354 if (attr
->HasFont() && attr
->HasSize())
356 wxString strSize
= wxString::Format(wxT("%d"), attr
->GetFont().GetPointSize());
357 m_sizeTextCtrl
->SetValue(strSize
);
358 if (m_sizeListBox
->FindString(strSize
) != wxNOT_FOUND
)
359 m_sizeListBox
->SetStringSelection(strSize
);
363 m_sizeTextCtrl
->SetValue(wxEmptyString
);
364 m_sizeListBox
->SetSelection(wxNOT_FOUND
);
367 if (attr
->HasFont() && attr
->HasWeight())
369 if (attr
->GetFont().GetWeight() == wxBOLD
)
370 m_weightCtrl
->SetSelection(1);
372 m_weightCtrl
->SetSelection(0);
376 m_weightCtrl
->SetSelection(wxNOT_FOUND
);
379 if (attr
->HasFont() && attr
->HasItalic())
381 if (attr
->GetFont().GetStyle() == wxITALIC
)
382 m_styleCtrl
->SetSelection(1);
384 m_styleCtrl
->SetSelection(0);
388 m_styleCtrl
->SetSelection(wxNOT_FOUND
);
391 if (attr
->HasFont() && attr
->HasUnderlined())
393 if (attr
->GetFont().GetUnderlined())
394 m_underliningCtrl
->SetSelection(1);
396 m_underliningCtrl
->SetSelection(0);
400 m_underliningCtrl
->SetSelection(wxNOT_FOUND
);
403 if (attr
->HasTextColour())
405 m_colourCtrl
->SetBackgroundColour(attr
->GetTextColour());
406 m_colourPresent
= true;
411 m_dontUpdate
= false;
416 wxTextAttrEx
* wxRichTextFontPage::GetAttributes()
418 return wxRichTextFormattingDialog::GetDialogAttributes(this);
421 /// Updates the font preview
422 void wxRichTextFontPage::UpdatePreview()
424 wxFont
font(*wxNORMAL_FONT
);
427 m_previewCtrl
->SetForegroundColour(m_colourCtrl
->GetBackgroundColour());
429 if (m_faceListBox
->GetSelection() != wxNOT_FOUND
)
431 wxString faceName
= m_faceListBox
->GetFaceName(m_faceListBox
->GetSelection());
432 font
.SetFaceName(faceName
);
435 wxString strSize
= m_sizeTextCtrl
->GetValue();
436 if (!strSize
.IsEmpty())
438 int sz
= wxAtoi(strSize
);
440 font
.SetPointSize(sz
);
443 if (m_styleCtrl
->GetSelection() != wxNOT_FOUND
)
446 if (m_styleCtrl
->GetStringSelection() == _("Italic"))
451 font
.SetStyle(style
);
454 if (m_weightCtrl
->GetSelection() != wxNOT_FOUND
)
457 if (m_weightCtrl
->GetStringSelection() == _("Bold"))
462 font
.SetWeight(weight
);
465 if (m_underliningCtrl
->GetSelection() != wxNOT_FOUND
)
468 if (m_underliningCtrl
->GetStringSelection() == _("Underlined"))
473 font
.SetUnderlined(underlined
);
476 m_previewCtrl
->SetFont(font
);
477 m_previewCtrl
->Refresh();
481 * Should we show tooltips?
484 bool wxRichTextFontPage::ShowToolTips()
490 * Get bitmap resources
493 wxBitmap
wxRichTextFontPage::GetBitmapResource( const wxString
& name
)
496 ////@begin wxRichTextFontPage bitmap retrieval
499 ////@end wxRichTextFontPage bitmap retrieval
506 wxIcon
wxRichTextFontPage::GetIconResource( const wxString
& name
)
509 ////@begin wxRichTextFontPage icon retrieval
512 ////@end wxRichTextFontPage icon retrieval
516 * wxEVT_COMMAND_TEXT_UPDATED event handler for ID_RICHTEXTFONTPAGE_FACETEXTCTRL
519 void wxRichTextFontPage::OnFaceTextCtrlUpdated( wxCommandEvent
& WXUNUSED(event
) )
524 wxString facename
= m_faceTextCtrl
->GetValue();
525 if (!facename
.IsEmpty())
527 if (m_faceListBox
->HasFaceName(facename
))
529 m_faceListBox
->SetFaceNameSelection(facename
);
534 // Try to find a partial match
535 const wxArrayString
& arr
= m_faceListBox
->GetFaceNames();
537 for (i
= 0; i
< arr
.GetCount(); i
++)
539 if (arr
[i
].Mid(0, facename
.Length()).Lower() == facename
.Lower())
541 m_faceListBox
->ScrollToLine(i
);
551 * wxEVT_COMMAND_TEXT_UPDATED event handler for ID_RICHTEXTFONTPAGE_SIZETEXTCTRL
554 void wxRichTextFontPage::OnSizeTextCtrlUpdated( wxCommandEvent
& WXUNUSED(event
) )
559 wxString strSize
= m_sizeTextCtrl
->GetValue();
560 if (!strSize
.IsEmpty() && m_sizeListBox
->FindString(strSize
) != wxNOT_FOUND
)
561 m_sizeListBox
->SetStringSelection(strSize
);
567 * wxEVT_COMMAND_LISTBOX_SELECTED event handler for ID_RICHTEXTFONTPAGE_SIZELISTBOX
570 void wxRichTextFontPage::OnSizeListBoxSelected( wxCommandEvent
& event
)
574 m_sizeTextCtrl
->SetValue(event
.GetString());
576 m_dontUpdate
= false;
582 * wxEVT_COMMAND_LISTBOX_SELECTED event handler for ID_RICHTEXTFONTPAGE_FACELISTBOX
585 void wxRichTextFontPage::OnFaceListBoxSelected( wxCommandEvent
& WXUNUSED(event
) )
589 m_faceTextCtrl
->SetValue(m_faceListBox
->GetFaceName(m_faceListBox
->GetSelection()));
591 m_dontUpdate
= false;
597 * wxEVT_COMMAND_COMBOBOX_SELECTED event handler for ID_RICHTEXTFONTPAGE_STYLECTRL
600 void wxRichTextFontPage::OnStyleCtrlSelected( wxCommandEvent
& WXUNUSED(event
) )
607 * wxEVT_COMMAND_COMBOBOX_SELECTED event handler for ID_RICHTEXTFONTPAGE_UNDERLINING_CTRL
610 void wxRichTextFontPage::OnUnderliningCtrlSelected( wxCommandEvent
& WXUNUSED(event
) )
617 * wxEVT_COMMAND_COMBOBOX_SELECTED event handler for ID_RICHTEXTFONTPAGE_WEIGHTCTRL
620 void wxRichTextFontPage::OnWeightCtrlSelected( wxCommandEvent
& WXUNUSED(event
) )
625 void wxRichTextFontPage::OnColourClicked( wxCommandEvent
& WXUNUSED(event
) )
627 m_colourPresent
= true;