Always link with expat in monolithic build.
[wxWidgets.git] / samples / widgets / fontpicker.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
3 // Name: fontpicker.cpp
4 // Purpose: Shows wxFontPickerCtrl
5 // Author: Francesco Montorsi
6 // Created: 20/6/2006
7 // Id: $Id$
8 // Copyright: (c) 2006 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/wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_FONTPICKERCTRL
28
29 // for all others, include the necessary headers
30 #ifndef WX_PRECOMP
31 #include "wx/app.h"
32 #include "wx/log.h"
33 #include "wx/radiobox.h"
34 #endif
35
36 #include "wx/artprov.h"
37 #include "wx/sizer.h"
38 #include "wx/stattext.h"
39 #include "wx/checkbox.h"
40 #include "wx/imaglist.h"
41
42 #include "wx/fontpicker.h"
43 #include "widgets.h"
44
45 #include "icons/fontpicker.xpm"
46
47 // ----------------------------------------------------------------------------
48 // constants
49 // ----------------------------------------------------------------------------
50
51 // control ids
52 enum
53 {
54 PickerPage_Reset = wxID_HIGHEST,
55 PickerPage_Font
56 };
57
58
59 // ----------------------------------------------------------------------------
60 // FontPickerWidgetsPage
61 // ----------------------------------------------------------------------------
62
63 class FontPickerWidgetsPage : public WidgetsPage
64 {
65 public:
66 FontPickerWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist);
67 virtual ~FontPickerWidgetsPage(){};
68
69 virtual wxControl *GetWidget() const { return m_fontPicker; }
70 virtual void RecreateWidget() { RecreatePicker(); }
71
72 // lazy creation of the content
73 virtual void CreateContent();
74
75 protected:
76
77 // called only once at first construction
78 void CreatePicker();
79
80 // called to recreate an existing control
81 void RecreatePicker();
82
83 // restore the checkboxes state to the initial values
84 void Reset();
85
86 // get the initial style for the picker of the given kind
87 long GetPickerStyle();
88
89
90 void OnFontChange(wxFontPickerEvent &ev);
91 void OnCheckBox(wxCommandEvent &ev);
92 void OnButtonReset(wxCommandEvent &ev);
93
94 // the picker
95 wxFontPickerCtrl *m_fontPicker;
96
97
98 // other controls
99 // --------------
100
101 wxCheckBox *m_chkFontTextCtrl,
102 *m_chkFontDescAsLabel,
103 *m_chkFontUseFontForLabel;
104 wxBoxSizer *m_sizer;
105
106 private:
107 DECLARE_EVENT_TABLE()
108 DECLARE_WIDGETS_PAGE(FontPickerWidgetsPage)
109 };
110
111 // ----------------------------------------------------------------------------
112 // event tables
113 // ----------------------------------------------------------------------------
114
115 BEGIN_EVENT_TABLE(FontPickerWidgetsPage, WidgetsPage)
116 EVT_BUTTON(PickerPage_Reset, FontPickerWidgetsPage::OnButtonReset)
117
118 EVT_FONTPICKER_CHANGED(PickerPage_Font, FontPickerWidgetsPage::OnFontChange)
119
120 EVT_CHECKBOX(wxID_ANY, FontPickerWidgetsPage::OnCheckBox)
121 END_EVENT_TABLE()
122
123 // ============================================================================
124 // implementation
125 // ============================================================================
126
127 #if defined(__WXGTK24__)
128 #define FAMILY_CTRLS NATIVE_CTRLS
129 #else
130 #define FAMILY_CTRLS GENERIC_CTRLS
131 #endif
132
133 IMPLEMENT_WIDGETS_PAGE(FontPickerWidgetsPage, wxT("FontPicker"),
134 PICKER_CTRLS | FAMILY_CTRLS);
135
136 FontPickerWidgetsPage::FontPickerWidgetsPage(WidgetsBookCtrl *book,
137 wxImageList *imaglist)
138 : WidgetsPage(book, imaglist, fontpicker_xpm)
139 {
140 }
141
142 void FontPickerWidgetsPage::CreateContent()
143 {
144 // left pane
145 wxSizer *boxleft = new wxBoxSizer(wxVERTICAL);
146
147 wxStaticBoxSizer *fontbox = new wxStaticBoxSizer(wxVERTICAL, this, wxT("&FontPicker style"));
148 m_chkFontTextCtrl = CreateCheckBoxAndAddToSizer(fontbox, wxT("With textctrl"));
149 m_chkFontDescAsLabel = CreateCheckBoxAndAddToSizer(fontbox, wxT("Font desc as btn label"));
150 m_chkFontUseFontForLabel = CreateCheckBoxAndAddToSizer(fontbox, wxT("Use font for label"), false);
151 boxleft->Add(fontbox, 0, wxALL|wxGROW, 5);
152
153 boxleft->Add(new wxButton(this, PickerPage_Reset, wxT("&Reset")),
154 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
155
156 Reset(); // set checkboxes state
157
158 // create pickers
159 m_fontPicker = NULL;
160 CreatePicker();
161
162 // right pane
163 m_sizer = new wxBoxSizer(wxVERTICAL);
164 m_sizer->Add(1, 1, 1, wxGROW | wxALL, 5); // spacer
165 m_sizer->Add(m_fontPicker, 0, wxALIGN_CENTER|wxALL, 5);
166 m_sizer->Add(1, 1, 1, wxGROW | wxALL, 5); // spacer
167
168 // global pane
169 wxSizer *sz = new wxBoxSizer(wxHORIZONTAL);
170 sz->Add(boxleft, 0, wxGROW|wxALL, 5);
171 sz->Add(m_sizer, 1, wxGROW|wxALL, 5);
172
173 SetSizer(sz);
174 }
175
176 void FontPickerWidgetsPage::CreatePicker()
177 {
178 delete m_fontPicker;
179
180 m_fontPicker = new wxFontPickerCtrl(this, PickerPage_Font,
181 *wxSWISS_FONT,
182 wxDefaultPosition, wxDefaultSize,
183 GetPickerStyle());
184 }
185
186 long FontPickerWidgetsPage::GetPickerStyle()
187 {
188 long style = 0;
189
190 if ( m_chkFontTextCtrl->GetValue() )
191 style |= wxFNTP_USE_TEXTCTRL;
192
193 if ( m_chkFontUseFontForLabel->GetValue() )
194 style |= wxFNTP_USEFONT_FOR_LABEL;
195
196 if ( m_chkFontDescAsLabel->GetValue() )
197 style |= wxFNTP_FONTDESC_AS_LABEL;
198
199 return style;
200 }
201
202 void FontPickerWidgetsPage::RecreatePicker()
203 {
204 m_sizer->Remove(1);
205 CreatePicker();
206 m_sizer->Insert(1, m_fontPicker, 0, wxALIGN_CENTER|wxALL, 5);
207
208 m_sizer->Layout();
209 }
210
211 void FontPickerWidgetsPage::Reset()
212 {
213 m_chkFontTextCtrl->SetValue((wxFNTP_DEFAULT_STYLE & wxFNTP_USE_TEXTCTRL) != 0);
214 m_chkFontUseFontForLabel->SetValue((wxFNTP_DEFAULT_STYLE & wxFNTP_USEFONT_FOR_LABEL) != 0);
215 m_chkFontDescAsLabel->SetValue((wxFNTP_DEFAULT_STYLE & wxFNTP_FONTDESC_AS_LABEL) != 0);
216 }
217
218
219 // ----------------------------------------------------------------------------
220 // event handlers
221 // ----------------------------------------------------------------------------
222
223 void FontPickerWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
224 {
225 Reset();
226 RecreatePicker();
227 }
228
229 void FontPickerWidgetsPage::OnFontChange(wxFontPickerEvent& event)
230 {
231 wxLogMessage(wxT("The font changed to '%s' with size %d !"),
232 event.GetFont().GetFaceName().c_str(), event.GetFont().GetPointSize());
233 }
234
235 void FontPickerWidgetsPage::OnCheckBox(wxCommandEvent &event)
236 {
237 if (event.GetEventObject() == m_chkFontTextCtrl ||
238 event.GetEventObject() == m_chkFontDescAsLabel ||
239 event.GetEventObject() == m_chkFontUseFontForLabel)
240 RecreatePicker();
241 }
242
243 #endif // wxUSE_FONTPICKERCTRL