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 // Copyright: (c) Francesco Montorsi
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22
23 #if wxUSE_FONTPICKERCTRL
24
25 #include "wx/fontpicker.h"
26
27 #ifndef WX_PRECOMP
28 #include "wx/textctrl.h"
29 #endif
30
31 #include "wx/fontenum.h"
32 #include "wx/tokenzr.h"
33
34 // ============================================================================
35 // implementation
36 // ============================================================================
37
38 #if defined(__WXGTK20__) && !defined(__WXUNIVERSAL__)
39 #define SetMinMaxPointSize(min, max)
40 #else
41 #define SetMinMaxPointSize(min, max) GetPickerWidget()->GetFontData()->SetRange((min), (max))
42 #endif
43
44 const char wxFontPickerCtrlNameStr[] = "fontpicker";
45 const char wxFontPickerWidgetNameStr[] = "fontpickerwidget";
46
47 wxDEFINE_EVENT(wxEVT_FONTPICKER_CHANGED, wxFontPickerEvent);
48 wxIMPLEMENT_DYNAMIC_CLASS(wxFontPickerCtrl, wxPickerBase);
49 wxIMPLEMENT_DYNAMIC_CLASS(wxFontPickerEvent, wxCommandEvent);
50
51 // ----------------------------------------------------------------------------
52 // wxFontPickerCtrl
53 // ----------------------------------------------------------------------------
54
Create(wxWindow * parent,wxWindowID id,const wxFont & initial,const wxPoint & pos,const wxSize & size,long style,const wxValidator & validator,const wxString & name)55 bool wxFontPickerCtrl::Create( wxWindow *parent, wxWindowID id,
56 const wxFont &initial,
57 const wxPoint &pos, const wxSize &size,
58 long style, const wxValidator& validator,
59 const wxString &name )
60 {
61 if (!wxPickerBase::CreateBase(parent, id,
62 Font2String(initial.IsOk() ? initial
63 : *wxNORMAL_FONT),
64 pos, size, style, validator, name))
65 return false;
66
67 // the picker of a wxFontPickerCtrl is a wxFontPickerWidget
68 m_picker = new wxFontPickerWidget(this, wxID_ANY, initial,
69 wxDefaultPosition, wxDefaultSize,
70 GetPickerStyle(style));
71 // complete sizer creation
72 wxPickerBase::PostCreation();
73
74 m_picker->Bind(wxEVT_FONTPICKER_CHANGED, &wxFontPickerCtrl::OnFontChange, this);
75
76 return true;
77 }
78
Font2String(const wxFont & f)79 wxString wxFontPickerCtrl::Font2String(const wxFont &f)
80 {
81 wxString ret = f.GetNativeFontInfoUserDesc();
82 #ifdef __WXMSW__
83 // on wxMSW the encoding of the font is appended at the end of the string;
84 // since encoding is not very user-friendly we remove it.
85 wxFontEncoding enc = f.GetEncoding();
86 if ( enc != wxFONTENCODING_DEFAULT && enc != wxFONTENCODING_SYSTEM )
87 ret = ret.BeforeLast(wxT(' '));
88 #endif
89 return ret;
90 }
91
String2Font(const wxString & s)92 wxFont wxFontPickerCtrl::String2Font(const wxString &s)
93 {
94 wxString str(s);
95 wxFont ret;
96 double n;
97
98 // put a limit on the maximum point size which the user can enter
99 // NOTE: we suppose the last word of given string is the pointsize
100 wxString size = str.AfterLast(wxT(' '));
101 if (size.ToDouble(&n))
102 {
103 if (n < 1)
104 str = str.Left(str.length() - size.length()) + wxT("1");
105 else if (n >= m_nMaxPointSize)
106 str = str.Left(str.length() - size.length()) +
107 wxString::Format(wxT("%d"), m_nMaxPointSize);
108 }
109
110 if (!ret.SetNativeFontInfoUserDesc(str))
111 return wxNullFont;
112
113 return ret;
114 }
115
SetSelectedFont(const wxFont & f)116 void wxFontPickerCtrl::SetSelectedFont(const wxFont &f)
117 {
118 GetPickerWidget()->SetSelectedFont(f);
119 UpdateTextCtrlFromPicker();
120 }
121
UpdatePickerFromTextCtrl()122 void wxFontPickerCtrl::UpdatePickerFromTextCtrl()
123 {
124 wxASSERT(m_text);
125
126 // NB: we don't use the wxFont::wxFont(const wxString &) constructor
127 // since that constructor expects the native font description
128 // string returned by wxFont::GetNativeFontInfoDesc() and not
129 // the user-friendly one returned by wxFont::GetNativeFontInfoUserDesc()
130 wxFont f = String2Font(m_text->GetValue());
131 if (!f.IsOk())
132 return; // invalid user input
133
134 if (GetPickerWidget()->GetSelectedFont() != f)
135 {
136 GetPickerWidget()->SetSelectedFont(f);
137
138 // fire an event
139 wxFontPickerEvent event(this, GetId(), f);
140 GetEventHandler()->ProcessEvent(event);
141 }
142 }
143
UpdateTextCtrlFromPicker()144 void wxFontPickerCtrl::UpdateTextCtrlFromPicker()
145 {
146 if (!m_text)
147 return; // no textctrl to update
148
149 // Take care to use ChangeValue() here and not SetValue() to avoid
150 // infinite recursion.
151 m_text->ChangeValue(Font2String(GetPickerWidget()->GetSelectedFont()));
152 }
153
SetMinPointSize(unsigned int min)154 void wxFontPickerCtrl::SetMinPointSize(unsigned int min)
155 {
156 m_nMinPointSize = min;
157 SetMinMaxPointSize(m_nMinPointSize, m_nMaxPointSize);
158 }
159
SetMaxPointSize(unsigned int max)160 void wxFontPickerCtrl::SetMaxPointSize(unsigned int max)
161 {
162 m_nMaxPointSize = max;
163 SetMinMaxPointSize(m_nMinPointSize, m_nMaxPointSize);
164 }
165
166 // ----------------------------------------------------------------------------
167 // wxFontPickerCtrl - event handlers
168 // ----------------------------------------------------------------------------
169
OnFontChange(wxFontPickerEvent & ev)170 void wxFontPickerCtrl::OnFontChange(wxFontPickerEvent &ev)
171 {
172 UpdateTextCtrlFromPicker();
173
174 // the wxFontPickerWidget sent us a colour-change notification.
175 // forward this event to our parent
176 wxFontPickerEvent event(this, GetId(), ev.GetFont());
177 GetEventHandler()->ProcessEvent(event);
178 }
179
180 #endif // wxUSE_FONTPICKERCTRL
181