]> git.saurik.com Git - wxWidgets.git/blob - src/common/fontpickercmn.cpp
Don't generate any events from wxSpinCtrl and wxSpinCtrlDouble methods.
[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 // 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 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #if wxUSE_FONTPICKERCTRL
27
28 #include "wx/fontpicker.h"
29
30 #ifndef WX_PRECOMP
31 #include "wx/textctrl.h"
32 #endif
33
34 #include "wx/fontenum.h"
35 #include "wx/tokenzr.h"
36
37 // ============================================================================
38 // implementation
39 // ============================================================================
40
41 const char wxFontPickerCtrlNameStr[] = "fontpicker";
42 const char wxFontPickerWidgetNameStr[] = "fontpickerwidget";
43
44 wxDEFINE_EVENT(wxEVT_FONTPICKER_CHANGED, wxFontPickerEvent);
45 IMPLEMENT_DYNAMIC_CLASS(wxFontPickerCtrl, wxPickerBase)
46 IMPLEMENT_DYNAMIC_CLASS(wxFontPickerEvent, wxCommandEvent)
47
48 // ----------------------------------------------------------------------------
49 // wxFontPickerCtrl
50 // ----------------------------------------------------------------------------
51
52 #define M_PICKER ((wxFontPickerWidget*)m_picker)
53
54 bool wxFontPickerCtrl::Create( wxWindow *parent, wxWindowID id,
55 const wxFont &initial,
56 const wxPoint &pos, const wxSize &size,
57 long style, const wxValidator& validator,
58 const wxString &name )
59 {
60 if (!wxPickerBase::CreateBase(parent, id,
61 Font2String(initial.IsOk() ? initial
62 : *wxNORMAL_FONT),
63 pos, size, style, validator, name))
64 return false;
65
66 // the picker of a wxFontPickerCtrl is a wxFontPickerWidget
67 m_picker = new wxFontPickerWidget(this, wxID_ANY, initial,
68 wxDefaultPosition, wxDefaultSize,
69 GetPickerStyle(style));
70 // complete sizer creation
71 wxPickerBase::PostCreation();
72
73 m_picker->Connect(wxEVT_FONTPICKER_CHANGED,
74 wxFontPickerEventHandler(wxFontPickerCtrl::OnFontChange),
75 NULL, this);
76
77 return true;
78 }
79
80 wxString wxFontPickerCtrl::Font2String(const wxFont &f)
81 {
82 wxString ret = f.GetNativeFontInfoUserDesc();
83 #ifdef __WXMSW__
84 // on wxMSW the encoding of the font is appended at the end of the string;
85 // since encoding is not very user-friendly we remove it.
86 wxFontEncoding enc = f.GetEncoding();
87 if ( enc != wxFONTENCODING_DEFAULT && enc != wxFONTENCODING_SYSTEM )
88 ret = ret.BeforeLast(wxT(' '));
89 #endif
90 return ret;
91 }
92
93 wxFont wxFontPickerCtrl::String2Font(const wxString &s)
94 {
95 wxString str(s);
96 wxFont ret;
97 double n;
98
99 // put a limit on the maximum point size which the user can enter
100 // NOTE: we suppose the last word of given string is the pointsize
101 wxString size = str.AfterLast(wxT(' '));
102 if (size.ToDouble(&n))
103 {
104 if (n < 1)
105 str = str.Left(str.length() - size.length()) + wxT("1");
106 else if (n >= m_nMaxPointSize)
107 str = str.Left(str.length() - size.length()) +
108 wxString::Format(wxT("%d"), m_nMaxPointSize);
109 }
110
111 if (!ret.SetNativeFontInfoUserDesc(str))
112 return wxNullFont;
113
114 return ret;
115 }
116
117 void wxFontPickerCtrl::SetSelectedFont(const wxFont &f)
118 {
119 M_PICKER->SetSelectedFont(f);
120 UpdateTextCtrlFromPicker();
121 }
122
123 void wxFontPickerCtrl::UpdatePickerFromTextCtrl()
124 {
125 wxASSERT(m_text);
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.IsOk())
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 // Take care to use ChangeValue() here and not SetValue() to avoid
151 // infinite recursion.
152 m_text->ChangeValue(Font2String(M_PICKER->GetSelectedFont()));
153 }
154
155
156
157 // ----------------------------------------------------------------------------
158 // wxFontPickerCtrl - event handlers
159 // ----------------------------------------------------------------------------
160
161 void wxFontPickerCtrl::OnFontChange(wxFontPickerEvent &ev)
162 {
163 UpdateTextCtrlFromPicker();
164
165 // the wxFontPickerWidget sent us a colour-change notification.
166 // forward this event to our parent
167 wxFontPickerEvent event(this, GetId(), ev.GetFont());
168 GetEventHandler()->ProcessEvent(event);
169 }
170
171 #endif // wxUSE_FONTPICKERCTRL