added wx{Colour|File|Dir|Font}PickerCtrl (patch 1472329 by Francesco)
[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 #include "wx/fontpicker.h"
28 #include "wx/fontenum.h"
29 #include "wx/tokenzr.h"
30
31
32 // ============================================================================
33 // implementation
34 // ============================================================================
35
36 #if wxUSE_FONTPICKERCTRL
37
38 DEFINE_EVENT_TYPE(wxEVT_COMMAND_FONTPICKER_CHANGED)
39 IMPLEMENT_DYNAMIC_CLASS(wxFontPickerCtrl, wxPickerBase)
40 IMPLEMENT_DYNAMIC_CLASS(wxFontPickerEvent, wxCommandEvent)
41
42 // ----------------------------------------------------------------------------
43 // wxFontPickerCtrl
44 // ----------------------------------------------------------------------------
45
46 #define M_PICKER ((wxFontPickerWidget*)m_picker)
47
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 )
53 {
54 // by default, the textctrl is, if present, as big as the picker, for wxFontPickerCtrl
55 SetTextCtrlProportion(1);
56
57 if (!wxPickerBase::CreateBase(parent, id, Font2String(initial),
58 pos, size, style, validator, name))
59 return false;
60
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),
67 NULL, this);
68
69 return true;
70 }
71
72 wxString wxFontPickerCtrl::Font2String(const wxFont &f)
73 {
74 wxString ret = f.GetNativeFontInfoUserDesc();
75 #ifdef __WXMSW__
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(' '));
81 #endif
82 return ret;
83 }
84
85 wxFont wxFontPickerCtrl::String2Font(const wxString &s)
86 {
87 wxString str(s);
88 wxFont ret;
89 double n;
90
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))
95 {
96 if (n < 1)
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);
101 }
102
103 if (!ret.SetNativeFontInfoUserDesc(str))
104 return wxNullFont;
105
106 return ret;
107 }
108
109 void wxFontPickerCtrl::SetSelectedFont(const wxFont &f)
110 {
111 M_PICKER->SetSelectedFont(f);
112 UpdateTextCtrlFromPicker();
113 }
114
115 void wxFontPickerCtrl::UpdatePickerFromTextCtrl()
116 {
117 wxASSERT(m_text);
118
119 if (m_bIgnoreNextTextCtrlUpdate)
120 {
121 // ignore this update
122 m_bIgnoreNextTextCtrlUpdate = false;
123 return;
124 }
125
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());
131 if (!f.Ok())
132 return; // invalid user input
133
134 if (M_PICKER->GetSelectedFont() != f)
135 {
136 M_PICKER->SetSelectedFont(f);
137
138 // fire an event
139 wxFontPickerEvent event(this, GetId(), f);
140 GetEventHandler()->ProcessEvent(event);
141 }
142 }
143
144 void wxFontPickerCtrl::UpdateTextCtrlFromPicker()
145 {
146 if (!m_text)
147 return; // no textctrl to update
148
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()));
154 }
155
156
157
158 // ----------------------------------------------------------------------------
159 // wxFontPickerCtrl - event handlers
160 // ----------------------------------------------------------------------------
161
162 void wxFontPickerCtrl::OnFontChange(wxFontPickerEvent &ev)
163 {
164 UpdateTextCtrlFromPicker();
165
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);
170 }
171
172 #endif // wxUSE_FONTPICKERCTRL