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