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 #include "wx/fontpicker.h"
28 #include "wx/fontenum.h"
29 #include "wx/tokenzr.h"
32 // ============================================================================
34 // ============================================================================
36 #if wxUSE_FONTPICKERCTRL
38 DEFINE_EVENT_TYPE(wxEVT_COMMAND_FONTPICKER_CHANGED
)
39 IMPLEMENT_DYNAMIC_CLASS(wxFontPickerCtrl
, wxPickerBase
)
40 IMPLEMENT_DYNAMIC_CLASS(wxFontPickerEvent
, wxCommandEvent
)
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 #define M_PICKER ((wxFontPickerWidget*)m_picker)
48 bool wxFontPickerCtrl::Create( wxWindow
*parent
, wxWindowID id
,
49 const wxFont
&initial
,
50 const wxPoint
&pos
, const wxSize
&size
,
51 long style
, const wxValidator
& validator
,
52 const wxString
&name
)
54 // by default, the textctrl is, if present, as big as the picker, for wxFontPickerCtrl
55 SetTextCtrlProportion(1);
57 if (!wxPickerBase::CreateBase(parent
, id
, Font2String(initial
),
58 pos
, size
, style
, validator
, name
))
61 // the picker of a wxFontPickerCtrl is a wxFontPickerWidget
62 m_picker
= new wxFontPickerWidget(this, wxID_ANY
, initial
,
63 wxDefaultPosition
, wxDefaultSize
,
64 GetPickerStyle(style
));
65 m_picker
->Connect(wxEVT_COMMAND_FONTPICKER_CHANGED
,
66 wxFontPickerEventHandler(wxFontPickerCtrl::OnFontChange
),
72 wxString
wxFontPickerCtrl::Font2String(const wxFont
&f
)
74 wxString ret
= f
.GetNativeFontInfoUserDesc();
76 // on wxMSW the encoding of the font is appended at the end of the string;
77 // since encoding is not very user-friendly we remove it.
78 wxFontEncoding enc
= f
.GetEncoding();
79 if ( enc
!= wxFONTENCODING_DEFAULT
&& enc
!= wxFONTENCODING_SYSTEM
)
80 ret
= ret
.BeforeLast(wxT(' '));
85 wxFont
wxFontPickerCtrl::String2Font(const wxString
&s
)
91 // put a limit on the maximum point size which the user can enter
92 // NOTE: we suppose the last word of given string is the pointsize
93 wxString size
= str
.AfterLast(wxT(' '));
94 if (size
.ToDouble(&n
))
97 str
= str
.Left(str
.Len() - size
.Len()) + wxT("1");
98 else if (n
>= m_nMaxPointSize
)
99 str
= str
.Left(str
.Len() - size
.Len()) +
100 wxString::Format(wxT("%d"), m_nMaxPointSize
);
103 if (!ret
.SetNativeFontInfoUserDesc(str
))
109 void wxFontPickerCtrl::SetSelectedFont(const wxFont
&f
)
111 M_PICKER
->SetSelectedFont(f
);
112 UpdateTextCtrlFromPicker();
115 void wxFontPickerCtrl::UpdatePickerFromTextCtrl()
119 if (m_bIgnoreNextTextCtrlUpdate
)
121 // ignore this update
122 m_bIgnoreNextTextCtrlUpdate
= false;
126 // NB: we don't use the wxFont::wxFont(const wxString &) constructor
127 // since that constructor expects the native font description
128 // string returned by wxFont::GetNativeFontInfoDesc() and not
129 // the user-friendly one returned by wxFont::GetNativeFontInfoUserDesc()
130 wxFont f
= String2Font(m_text
->GetValue());
132 return; // invalid user input
134 if (M_PICKER
->GetSelectedFont() != f
)
136 M_PICKER
->SetSelectedFont(f
);
139 wxFontPickerEvent
event(this, GetId(), f
);
140 GetEventHandler()->ProcessEvent(event
);
144 void wxFontPickerCtrl::UpdateTextCtrlFromPicker()
147 return; // no textctrl to update
149 // NOTE: this SetValue() will generate an unwanted wxEVT_COMMAND_TEXT_UPDATED
150 // which will trigger a unneeded UpdateFromTextCtrl(); thus before using
151 // SetValue() we set the m_bIgnoreNextTextCtrlUpdate flag...
152 m_bIgnoreNextTextCtrlUpdate
= true;
153 m_text
->SetValue(Font2String(M_PICKER
->GetSelectedFont()));
158 // ----------------------------------------------------------------------------
159 // wxFontPickerCtrl - event handlers
160 // ----------------------------------------------------------------------------
162 void wxFontPickerCtrl::OnFontChange(wxFontPickerEvent
&ev
)
164 UpdateTextCtrlFromPicker();
166 // the wxFontPickerWidget sent us a colour-change notification.
167 // forward this event to our parent
168 wxFontPickerEvent
event(this, GetId(), ev
.GetFont());
169 GetEventHandler()->ProcessEvent(event
);
172 #endif // wxUSE_FONTPICKERCTRL