1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/fontpickercmn.cpp
3 // Purpose: wxFontPickerCtrl class implementation
4 // Author: Francesco Montorsi
8 // Copyright: (c) Francesco Montorsi
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #if wxUSE_FONTPICKERCTRL
29 #include "wx/fontpicker.h"
32 #include "wx/textctrl.h"
35 #include "wx/fontenum.h"
36 #include "wx/tokenzr.h"
38 // ============================================================================
40 // ============================================================================
42 DEFINE_EVENT_TYPE(wxEVT_COMMAND_FONTPICKER_CHANGED
)
43 IMPLEMENT_DYNAMIC_CLASS(wxFontPickerCtrl
, wxPickerBase
)
44 IMPLEMENT_DYNAMIC_CLASS(wxFontPickerEvent
, wxCommandEvent
)
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 #define M_PICKER ((wxFontPickerWidget*)m_picker)
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
)
58 if (!wxPickerBase::CreateBase(parent
, id
, Font2String(initial
),
59 pos
, size
, style
, validator
, name
))
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 // complete sizer creation
67 wxPickerBase::PostCreation();
69 m_picker
->Connect(wxEVT_COMMAND_FONTPICKER_CHANGED
,
70 wxFontPickerEventHandler(wxFontPickerCtrl::OnFontChange
),
76 wxString
wxFontPickerCtrl::Font2String(const wxFont
&f
)
78 wxString ret
= f
.GetNativeFontInfoUserDesc();
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(' '));
89 wxFont
wxFontPickerCtrl::String2Font(const wxString
&s
)
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
))
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
);
107 if (!ret
.SetNativeFontInfoUserDesc(str
))
113 void wxFontPickerCtrl::SetSelectedFont(const wxFont
&f
)
115 M_PICKER
->SetSelectedFont(f
);
116 UpdateTextCtrlFromPicker();
119 void wxFontPickerCtrl::UpdatePickerFromTextCtrl()
123 if (m_bIgnoreNextTextCtrlUpdate
)
125 // ignore this update
126 m_bIgnoreNextTextCtrlUpdate
= false;
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());
136 return; // invalid user input
138 if (M_PICKER
->GetSelectedFont() != f
)
140 M_PICKER
->SetSelectedFont(f
);
143 wxFontPickerEvent
event(this, GetId(), f
);
144 GetEventHandler()->ProcessEvent(event
);
148 void wxFontPickerCtrl::UpdateTextCtrlFromPicker()
151 return; // no textctrl to update
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()));
162 // ----------------------------------------------------------------------------
163 // wxFontPickerCtrl - event handlers
164 // ----------------------------------------------------------------------------
166 void wxFontPickerCtrl::OnFontChange(wxFontPickerEvent
&ev
)
168 UpdateTextCtrlFromPicker();
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
);
176 #endif // wxUSE_FONTPICKERCTRL