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