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 const char wxFontPickerCtrlNameStr
[] = "fontpicker";
43 const char wxFontPickerWidgetNameStr
[] = "fontpickerwidget";
45 DEFINE_EVENT_TYPE(wxEVT_COMMAND_FONTPICKER_CHANGED
)
46 IMPLEMENT_DYNAMIC_CLASS(wxFontPickerCtrl
, wxPickerBase
)
47 IMPLEMENT_DYNAMIC_CLASS(wxFontPickerEvent
, wxCommandEvent
)
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 #define M_PICKER ((wxFontPickerWidget*)m_picker)
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
)
61 if (!wxPickerBase::CreateBase(parent
, id
,
62 Font2String(initial
.IsOk() ? initial
64 pos
, size
, style
, validator
, name
))
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();
74 m_picker
->Connect(wxEVT_COMMAND_FONTPICKER_CHANGED
,
75 wxFontPickerEventHandler(wxFontPickerCtrl::OnFontChange
),
81 wxString
wxFontPickerCtrl::Font2String(const wxFont
&f
)
83 wxString ret
= f
.GetNativeFontInfoUserDesc();
85 // on wxMSW the encoding of the font is appended at the end of the string;
86 // since encoding is not very user-friendly we remove it.
87 wxFontEncoding enc
= f
.GetEncoding();
88 if ( enc
!= wxFONTENCODING_DEFAULT
&& enc
!= wxFONTENCODING_SYSTEM
)
89 ret
= ret
.BeforeLast(wxT(' '));
94 wxFont
wxFontPickerCtrl::String2Font(const wxString
&s
)
100 // put a limit on the maximum point size which the user can enter
101 // NOTE: we suppose the last word of given string is the pointsize
102 wxString size
= str
.AfterLast(wxT(' '));
103 if (size
.ToDouble(&n
))
106 str
= str
.Left(str
.length() - size
.length()) + wxT("1");
107 else if (n
>= m_nMaxPointSize
)
108 str
= str
.Left(str
.length() - size
.length()) +
109 wxString::Format(wxT("%d"), m_nMaxPointSize
);
112 if (!ret
.SetNativeFontInfoUserDesc(str
))
118 void wxFontPickerCtrl::SetSelectedFont(const wxFont
&f
)
120 M_PICKER
->SetSelectedFont(f
);
121 UpdateTextCtrlFromPicker();
124 void wxFontPickerCtrl::UpdatePickerFromTextCtrl()
128 if (m_bIgnoreNextTextCtrlUpdate
)
130 // ignore this update
131 m_bIgnoreNextTextCtrlUpdate
= false;
135 // NB: we don't use the wxFont::wxFont(const wxString &) constructor
136 // since that constructor expects the native font description
137 // string returned by wxFont::GetNativeFontInfoDesc() and not
138 // the user-friendly one returned by wxFont::GetNativeFontInfoUserDesc()
139 wxFont f
= String2Font(m_text
->GetValue());
141 return; // invalid user input
143 if (M_PICKER
->GetSelectedFont() != f
)
145 M_PICKER
->SetSelectedFont(f
);
148 wxFontPickerEvent
event(this, GetId(), f
);
149 GetEventHandler()->ProcessEvent(event
);
153 void wxFontPickerCtrl::UpdateTextCtrlFromPicker()
156 return; // no textctrl to update
158 // NOTE: this SetValue() will generate an unwanted wxEVT_COMMAND_TEXT_UPDATED
159 // which will trigger a unneeded UpdateFromTextCtrl(); thus before using
160 // SetValue() we set the m_bIgnoreNextTextCtrlUpdate flag...
161 m_bIgnoreNextTextCtrlUpdate
= true;
162 m_text
->SetValue(Font2String(M_PICKER
->GetSelectedFont()));
167 // ----------------------------------------------------------------------------
168 // wxFontPickerCtrl - event handlers
169 // ----------------------------------------------------------------------------
171 void wxFontPickerCtrl::OnFontChange(wxFontPickerEvent
&ev
)
173 UpdateTextCtrlFromPicker();
175 // the wxFontPickerWidget sent us a colour-change notification.
176 // forward this event to our parent
177 wxFontPickerEvent
event(this, GetId(), ev
.GetFont());
178 GetEventHandler()->ProcessEvent(event
);
181 #endif // wxUSE_FONTPICKERCTRL