correct access for virtuals, unneeded includes
[wxWidgets.git] / src / common / fontpickercmn.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/fontpickercmn.cpp
3 // Purpose: wxFontPickerCtrl class implementation
4 // Author: Francesco Montorsi
5 // Modified by:
6 // Created: 15/04/2006
7 // RCS-ID: $Id$
8 // Copyright: (c) Francesco Montorsi
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_FONTPICKERCTRL
28
29 #include "wx/fontpicker.h"
30
31 #include "wx/textctrl.h"
32 #include "wx/fontenum.h"
33 #include "wx/tokenzr.h"
34
35 // ============================================================================
36 // implementation
37 // ============================================================================
38
39 DEFINE_EVENT_TYPE(wxEVT_COMMAND_FONTPICKER_CHANGED)
40 IMPLEMENT_DYNAMIC_CLASS(wxFontPickerCtrl, wxPickerBase)
41 IMPLEMENT_DYNAMIC_CLASS(wxFontPickerEvent, wxCommandEvent)
42
43 // ----------------------------------------------------------------------------
44 // wxFontPickerCtrl
45 // ----------------------------------------------------------------------------
46
47 #define M_PICKER ((wxFontPickerWidget*)m_picker)
48
49 bool wxFontPickerCtrl::Create( wxWindow *parent, wxWindowID id,
50 const wxFont &initial,
51 const wxPoint &pos, const wxSize &size,
52 long style, const wxValidator& validator,
53 const wxString &name )
54 {
55 // by default, the textctrl is, if present, as big as the picker, for wxFontPickerCtrl
56 SetTextCtrlProportion(1);
57
58 if (!wxPickerBase::CreateBase(parent, id, Font2String(initial),
59 pos, size, style, validator, name))
60 return false;
61
62 // the picker of a wxFontPickerCtrl is a wxFontPickerWidget
63 m_picker = new wxFontPickerWidget(this, wxID_ANY, initial,
64 wxDefaultPosition, wxDefaultSize,
65 GetPickerStyle(style));
66 m_picker->Connect(wxEVT_COMMAND_FONTPICKER_CHANGED,
67 wxFontPickerEventHandler(wxFontPickerCtrl::OnFontChange),
68 NULL, this);
69
70 return true;
71 }
72
73 wxString wxFontPickerCtrl::Font2String(const wxFont &f)
74 {
75 wxString ret = f.GetNativeFontInfoUserDesc();
76 #ifdef __WXMSW__
77 // on wxMSW the encoding of the font is appended at the end of the string;
78 // since encoding is not very user-friendly we remove it.
79 wxFontEncoding enc = f.GetEncoding();
80 if ( enc != wxFONTENCODING_DEFAULT && enc != wxFONTENCODING_SYSTEM )
81 ret = ret.BeforeLast(wxT(' '));
82 #endif
83 return ret;
84 }
85
86 wxFont wxFontPickerCtrl::String2Font(const wxString &s)
87 {
88 wxString str(s);
89 wxFont ret;
90 double n;
91
92 // put a limit on the maximum point size which the user can enter
93 // NOTE: we suppose the last word of given string is the pointsize
94 wxString size = str.AfterLast(wxT(' '));
95 if (size.ToDouble(&n))
96 {
97 if (n < 1)
98 str = str.Left(str.length() - size.length()) + wxT("1");
99 else if (n >= m_nMaxPointSize)
100 str = str.Left(str.length() - size.length()) +
101 wxString::Format(wxT("%d"), m_nMaxPointSize);
102 }
103
104 if (!ret.SetNativeFontInfoUserDesc(str))
105 return wxNullFont;
106
107 return ret;
108 }
109
110 void wxFontPickerCtrl::SetSelectedFont(const wxFont &f)
111 {
112 M_PICKER->SetSelectedFont(f);
113 UpdateTextCtrlFromPicker();
114 }
115
116 void wxFontPickerCtrl::UpdatePickerFromTextCtrl()
117 {
118 wxASSERT(m_text);
119
120 if (m_bIgnoreNextTextCtrlUpdate)
121 {
122 // ignore this update
123 m_bIgnoreNextTextCtrlUpdate = false;
124 return;
125 }
126
127 // NB: we don't use the wxFont::wxFont(const wxString &) constructor
128 // since that constructor expects the native font description
129 // string returned by wxFont::GetNativeFontInfoDesc() and not
130 // the user-friendly one returned by wxFont::GetNativeFontInfoUserDesc()
131 wxFont f = String2Font(m_text->GetValue());
132 if (!f.Ok())
133 return; // invalid user input
134
135 if (M_PICKER->GetSelectedFont() != f)
136 {
137 M_PICKER->SetSelectedFont(f);
138
139 // fire an event
140 wxFontPickerEvent event(this, GetId(), f);
141 GetEventHandler()->ProcessEvent(event);
142 }
143 }
144
145 void wxFontPickerCtrl::UpdateTextCtrlFromPicker()
146 {
147 if (!m_text)
148 return; // no textctrl to update
149
150 // NOTE: this SetValue() will generate an unwanted wxEVT_COMMAND_TEXT_UPDATED
151 // which will trigger a unneeded UpdateFromTextCtrl(); thus before using
152 // SetValue() we set the m_bIgnoreNextTextCtrlUpdate flag...
153 m_bIgnoreNextTextCtrlUpdate = true;
154 m_text->SetValue(Font2String(M_PICKER->GetSelectedFont()));
155 }
156
157
158
159 // ----------------------------------------------------------------------------
160 // wxFontPickerCtrl - event handlers
161 // ----------------------------------------------------------------------------
162
163 void wxFontPickerCtrl::OnFontChange(wxFontPickerEvent &ev)
164 {
165 UpdateTextCtrlFromPicker();
166
167 // the wxFontPickerWidget sent us a colour-change notification.
168 // forward this event to our parent
169 wxFontPickerEvent event(this, GetId(), ev.GetFont());
170 GetEventHandler()->ProcessEvent(event);
171 }
172
173 #endif // wxUSE_FONTPICKERCTRL