1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/fontpickercmn.cpp
3 // Purpose: wxFontPickerCtrl class implementation
4 // Author: Francesco Montorsi
7 // Copyright: (c) Francesco Montorsi
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
26 #if wxUSE_FONTPICKERCTRL
28 #include "wx/fontpicker.h"
31 #include "wx/textctrl.h"
34 #include "wx/fontenum.h"
35 #include "wx/tokenzr.h"
37 // ============================================================================
39 // ============================================================================
41 const char wxFontPickerCtrlNameStr
[] = "fontpicker";
42 const char wxFontPickerWidgetNameStr
[] = "fontpickerwidget";
44 wxDEFINE_EVENT(wxEVT_FONTPICKER_CHANGED
, wxFontPickerEvent
);
45 IMPLEMENT_DYNAMIC_CLASS(wxFontPickerCtrl
, wxPickerBase
)
46 IMPLEMENT_DYNAMIC_CLASS(wxFontPickerEvent
, wxCommandEvent
)
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 #define M_PICKER ((wxFontPickerWidget*)m_picker)
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
)
60 if (!wxPickerBase::CreateBase(parent
, id
,
61 Font2String(initial
.IsOk() ? initial
63 pos
, size
, style
, validator
, name
))
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();
73 m_picker
->Connect(wxEVT_FONTPICKER_CHANGED
,
74 wxFontPickerEventHandler(wxFontPickerCtrl::OnFontChange
),
80 wxString
wxFontPickerCtrl::Font2String(const wxFont
&f
)
82 wxString ret
= f
.GetNativeFontInfoUserDesc();
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(' '));
93 wxFont
wxFontPickerCtrl::String2Font(const wxString
&s
)
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
))
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
);
111 if (!ret
.SetNativeFontInfoUserDesc(str
))
117 void wxFontPickerCtrl::SetSelectedFont(const wxFont
&f
)
119 M_PICKER
->SetSelectedFont(f
);
120 UpdateTextCtrlFromPicker();
123 void wxFontPickerCtrl::UpdatePickerFromTextCtrl()
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());
133 return; // invalid user input
135 if (M_PICKER
->GetSelectedFont() != f
)
137 M_PICKER
->SetSelectedFont(f
);
140 wxFontPickerEvent
event(this, GetId(), f
);
141 GetEventHandler()->ProcessEvent(event
);
145 void wxFontPickerCtrl::UpdateTextCtrlFromPicker()
148 return; // no textctrl to update
150 // Take care to use ChangeValue() here and not SetValue() to avoid
151 // infinite recursion.
152 m_text
->ChangeValue(Font2String(M_PICKER
->GetSelectedFont()));
157 // ----------------------------------------------------------------------------
158 // wxFontPickerCtrl - event handlers
159 // ----------------------------------------------------------------------------
161 void wxFontPickerCtrl::OnFontChange(wxFontPickerEvent
&ev
)
163 UpdateTextCtrlFromPicker();
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
);
171 #endif // wxUSE_FONTPICKERCTRL