Minor source cleaning.
[wxWidgets.git] / src / common / fontpickercmn.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/fontpickercmn.cpp
3 // Purpose: wxFontPickerCtrl class implementation
4 // Author: Francesco Montorsi
5 // Modified by:
6 // Created: 15/04/2006
7 // RCS-ID: $Id$
8 // Copyright: (c) Francesco Montorsi
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_FONTPICKERCTRL
28
29 #include "wx/fontpicker.h"
30 #include "wx/fontenum.h"
31 #include "wx/tokenzr.h"
32
33 // ============================================================================
34 // implementation
35 // ============================================================================
36
37 DEFINE_EVENT_TYPE(wxEVT_COMMAND_FONTPICKER_CHANGED)
38 IMPLEMENT_DYNAMIC_CLASS(wxFontPickerCtrl, wxPickerBase)
39 IMPLEMENT_DYNAMIC_CLASS(wxFontPickerEvent, wxCommandEvent)
40
41 // ----------------------------------------------------------------------------
42 // wxFontPickerCtrl
43 // ----------------------------------------------------------------------------
44
45 #define M_PICKER ((wxFontPickerWidget*)m_picker)
46
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 )
52 {
53 // by default, the textctrl is, if present, as big as the picker, for wxFontPickerCtrl
54 SetTextCtrlProportion(1);
55
56 if (!wxPickerBase::CreateBase(parent, id, Font2String(initial),
57 pos, size, style, validator, name))
58 return false;
59
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),
66 NULL, this);
67
68 return true;
69 }
70
71 wxString wxFontPickerCtrl::Font2String(const wxFont &f)
72 {
73 wxString ret = f.GetNativeFontInfoUserDesc();
74 #ifdef __WXMSW__
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(' '));
80 #endif
81 return ret;
82 }
83
84 wxFont wxFontPickerCtrl::String2Font(const wxString &s)
85 {
86 wxString str(s);
87 wxFont ret;
88 double n;
89
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))
94 {
95 if (n < 1)
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);
100 }
101
102 if (!ret.SetNativeFontInfoUserDesc(str))
103 return wxNullFont;
104
105 return ret;
106 }
107
108 void wxFontPickerCtrl::SetSelectedFont(const wxFont &f)
109 {
110 M_PICKER->SetSelectedFont(f);
111 UpdateTextCtrlFromPicker();
112 }
113
114 void wxFontPickerCtrl::UpdatePickerFromTextCtrl()
115 {
116 wxASSERT(m_text);
117
118 if (m_bIgnoreNextTextCtrlUpdate)
119 {
120 // ignore this update
121 m_bIgnoreNextTextCtrlUpdate = false;
122 return;
123 }
124
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());
130 if (!f.Ok())
131 return; // invalid user input
132
133 if (M_PICKER->GetSelectedFont() != f)
134 {
135 M_PICKER->SetSelectedFont(f);
136
137 // fire an event
138 wxFontPickerEvent event(this, GetId(), f);
139 GetEventHandler()->ProcessEvent(event);
140 }
141 }
142
143 void wxFontPickerCtrl::UpdateTextCtrlFromPicker()
144 {
145 if (!m_text)
146 return; // no textctrl to update
147
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()));
153 }
154
155
156
157 // ----------------------------------------------------------------------------
158 // wxFontPickerCtrl - event handlers
159 // ----------------------------------------------------------------------------
160
161 void wxFontPickerCtrl::OnFontChange(wxFontPickerEvent &ev)
162 {
163 UpdateTextCtrlFromPicker();
164
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);
169 }
170
171 #endif // wxUSE_FONTPICKERCTRL