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"
30 #include "wx/fontenum.h"
31 #include "wx/tokenzr.h"
33 // ============================================================================
35 // ============================================================================
37 DEFINE_EVENT_TYPE(wxEVT_COMMAND_FONTPICKER_CHANGED
)
38 IMPLEMENT_DYNAMIC_CLASS(wxFontPickerCtrl
, wxPickerBase
)
39 IMPLEMENT_DYNAMIC_CLASS(wxFontPickerEvent
, wxCommandEvent
)
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
45 #define M_PICKER ((wxFontPickerWidget*)m_picker)
47 bool wxFontPickerCtrl::Create( wxWindow
*parent
, wxWindowID id
,
48 const wxFont
&initial
,
49 const wxPoint
&pos
, const wxSize
&size
,
50 long style
, const wxValidator
& validator
,
51 const wxString
&name
)
53 // by default, the textctrl is, if present, as big as the picker, for wxFontPickerCtrl
54 SetTextCtrlProportion(1);
56 if (!wxPickerBase::CreateBase(parent
, id
, Font2String(initial
),
57 pos
, size
, style
, validator
, name
))
60 // the picker of a wxFontPickerCtrl is a wxFontPickerWidget
61 m_picker
= new wxFontPickerWidget(this, wxID_ANY
, initial
,
62 wxDefaultPosition
, wxDefaultSize
,
63 GetPickerStyle(style
));
64 m_picker
->Connect(wxEVT_COMMAND_FONTPICKER_CHANGED
,
65 wxFontPickerEventHandler(wxFontPickerCtrl::OnFontChange
),
71 wxString
wxFontPickerCtrl::Font2String(const wxFont
&f
)
73 wxString ret
= f
.GetNativeFontInfoUserDesc();
75 // on wxMSW the encoding of the font is appended at the end of the string;
76 // since encoding is not very user-friendly we remove it.
77 wxFontEncoding enc
= f
.GetEncoding();
78 if ( enc
!= wxFONTENCODING_DEFAULT
&& enc
!= wxFONTENCODING_SYSTEM
)
79 ret
= ret
.BeforeLast(wxT(' '));
84 wxFont
wxFontPickerCtrl::String2Font(const wxString
&s
)
90 // put a limit on the maximum point size which the user can enter
91 // NOTE: we suppose the last word of given string is the pointsize
92 wxString size
= str
.AfterLast(wxT(' '));
93 if (size
.ToDouble(&n
))
96 str
= str
.Left(str
.length() - size
.length()) + wxT("1");
97 else if (n
>= m_nMaxPointSize
)
98 str
= str
.Left(str
.length() - size
.length()) +
99 wxString::Format(wxT("%d"), m_nMaxPointSize
);
102 if (!ret
.SetNativeFontInfoUserDesc(str
))
108 void wxFontPickerCtrl::SetSelectedFont(const wxFont
&f
)
110 M_PICKER
->SetSelectedFont(f
);
111 UpdateTextCtrlFromPicker();
114 void wxFontPickerCtrl::UpdatePickerFromTextCtrl()
118 if (m_bIgnoreNextTextCtrlUpdate
)
120 // ignore this update
121 m_bIgnoreNextTextCtrlUpdate
= false;
125 // NB: we don't use the wxFont::wxFont(const wxString &) constructor
126 // since that constructor expects the native font description
127 // string returned by wxFont::GetNativeFontInfoDesc() and not
128 // the user-friendly one returned by wxFont::GetNativeFontInfoUserDesc()
129 wxFont f
= String2Font(m_text
->GetValue());
131 return; // invalid user input
133 if (M_PICKER
->GetSelectedFont() != f
)
135 M_PICKER
->SetSelectedFont(f
);
138 wxFontPickerEvent
event(this, GetId(), f
);
139 GetEventHandler()->ProcessEvent(event
);
143 void wxFontPickerCtrl::UpdateTextCtrlFromPicker()
146 return; // no textctrl to update
148 // NOTE: this SetValue() will generate an unwanted wxEVT_COMMAND_TEXT_UPDATED
149 // which will trigger a unneeded UpdateFromTextCtrl(); thus before using
150 // SetValue() we set the m_bIgnoreNextTextCtrlUpdate flag...
151 m_bIgnoreNextTextCtrlUpdate
= true;
152 m_text
->SetValue(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