]> git.saurik.com Git - wxWidgets.git/blob - src/common/fontpickercmn.cpp
remove unneeded WXDLLEXPORTs for the classes only used inside the implementation...
[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
31 #ifndef WX_PRECOMP
32 #include "wx/textctrl.h"
33 #endif
34
35 #include "wx/fontenum.h"
36 #include "wx/tokenzr.h"
37
38 // ============================================================================
39 // implementation
40 // ============================================================================
41
42 const wxChar wxFontPickerCtrlNameStr[] = wxT("fontpicker");
43 const wxChar wxFontPickerWidgetNameStr[] = wxT("fontpickerwidget");
44
45 DEFINE_EVENT_TYPE(wxEVT_COMMAND_FONTPICKER_CHANGED)
46 IMPLEMENT_DYNAMIC_CLASS(wxFontPickerCtrl, wxPickerBase)
47 IMPLEMENT_DYNAMIC_CLASS(wxFontPickerEvent, wxCommandEvent)
48
49 // ----------------------------------------------------------------------------
50 // wxFontPickerCtrl
51 // ----------------------------------------------------------------------------
52
53 #define M_PICKER ((wxFontPickerWidget*)m_picker)
54
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 )
60 {
61 if (!wxPickerBase::CreateBase(parent, id, Font2String(initial),
62 pos, size, style, validator, name))
63 return false;
64
65 // the picker of a wxFontPickerCtrl is a wxFontPickerWidget
66 m_picker = new wxFontPickerWidget(this, wxID_ANY, initial,
67 wxDefaultPosition, wxDefaultSize,
68 GetPickerStyle(style));
69 // complete sizer creation
70 wxPickerBase::PostCreation();
71
72 m_picker->Connect(wxEVT_COMMAND_FONTPICKER_CHANGED,
73 wxFontPickerEventHandler(wxFontPickerCtrl::OnFontChange),
74 NULL, this);
75
76 return true;
77 }
78
79 wxString wxFontPickerCtrl::Font2String(const wxFont &f)
80 {
81 wxString ret = f.GetNativeFontInfoUserDesc();
82 #ifdef __WXMSW__
83 // on wxMSW the encoding of the font is appended at the end of the string;
84 // since encoding is not very user-friendly we remove it.
85 wxFontEncoding enc = f.GetEncoding();
86 if ( enc != wxFONTENCODING_DEFAULT && enc != wxFONTENCODING_SYSTEM )
87 ret = ret.BeforeLast(wxT(' '));
88 #endif
89 return ret;
90 }
91
92 wxFont wxFontPickerCtrl::String2Font(const wxString &s)
93 {
94 wxString str(s);
95 wxFont ret;
96 double n;
97
98 // put a limit on the maximum point size which the user can enter
99 // NOTE: we suppose the last word of given string is the pointsize
100 wxString size = str.AfterLast(wxT(' '));
101 if (size.ToDouble(&n))
102 {
103 if (n < 1)
104 str = str.Left(str.length() - size.length()) + wxT("1");
105 else if (n >= m_nMaxPointSize)
106 str = str.Left(str.length() - size.length()) +
107 wxString::Format(wxT("%d"), m_nMaxPointSize);
108 }
109
110 if (!ret.SetNativeFontInfoUserDesc(str))
111 return wxNullFont;
112
113 return ret;
114 }
115
116 void wxFontPickerCtrl::SetSelectedFont(const wxFont &f)
117 {
118 M_PICKER->SetSelectedFont(f);
119 UpdateTextCtrlFromPicker();
120 }
121
122 void wxFontPickerCtrl::UpdatePickerFromTextCtrl()
123 {
124 wxASSERT(m_text);
125
126 if (m_bIgnoreNextTextCtrlUpdate)
127 {
128 // ignore this update
129 m_bIgnoreNextTextCtrlUpdate = false;
130 return;
131 }
132
133 // NB: we don't use the wxFont::wxFont(const wxString &) constructor
134 // since that constructor expects the native font description
135 // string returned by wxFont::GetNativeFontInfoDesc() and not
136 // the user-friendly one returned by wxFont::GetNativeFontInfoUserDesc()
137 wxFont f = String2Font(m_text->GetValue());
138 if (!f.Ok())
139 return; // invalid user input
140
141 if (M_PICKER->GetSelectedFont() != f)
142 {
143 M_PICKER->SetSelectedFont(f);
144
145 // fire an event
146 wxFontPickerEvent event(this, GetId(), f);
147 GetEventHandler()->ProcessEvent(event);
148 }
149 }
150
151 void wxFontPickerCtrl::UpdateTextCtrlFromPicker()
152 {
153 if (!m_text)
154 return; // no textctrl to update
155
156 // NOTE: this SetValue() will generate an unwanted wxEVT_COMMAND_TEXT_UPDATED
157 // which will trigger a unneeded UpdateFromTextCtrl(); thus before using
158 // SetValue() we set the m_bIgnoreNextTextCtrlUpdate flag...
159 m_bIgnoreNextTextCtrlUpdate = true;
160 m_text->SetValue(Font2String(M_PICKER->GetSelectedFont()));
161 }
162
163
164
165 // ----------------------------------------------------------------------------
166 // wxFontPickerCtrl - event handlers
167 // ----------------------------------------------------------------------------
168
169 void wxFontPickerCtrl::OnFontChange(wxFontPickerEvent &ev)
170 {
171 UpdateTextCtrlFromPicker();
172
173 // the wxFontPickerWidget sent us a colour-change notification.
174 // forward this event to our parent
175 wxFontPickerEvent event(this, GetId(), ev.GetFont());
176 GetEventHandler()->ProcessEvent(event);
177 }
178
179 #endif // wxUSE_FONTPICKERCTRL