]> git.saurik.com Git - wxWidgets.git/blob - samples/widgets/widgets.cpp
1. added --enable-gtk2 option, check for GTK+ 2.0 disabled by default
[wxWidgets.git] / samples / widgets / widgets.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWindows Widgets Sample
3 // Name: widgets.cpp
4 // Purpose: Sample showing most of the simple wxWindows widgets
5 // Author: Vadim Zeitlin
6 // Created: 27.03.01
7 // Id: $Id$
8 // Copyright: (c) 2001 Vadim Zeitlin
9 // License: wxWindows license
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 // for all others, include the necessary headers
28 #ifndef WX_PRECOMP
29 #include "wx/app.h"
30 #include "wx/log.h"
31 #include "wx/panel.h"
32 #include "wx/frame.h"
33 #include "wx/button.h"
34 #include "wx/checkbox.h"
35 #include "wx/listbox.h"
36 #include "wx/statbox.h"
37 #include "wx/stattext.h"
38 #include "wx/textctrl.h"
39 #endif
40
41 #include "wx/notebook.h"
42 #include "wx/sizer.h"
43
44 #include "widgets.h"
45
46 // ----------------------------------------------------------------------------
47 // constants
48 // ----------------------------------------------------------------------------
49
50 // control ids
51 enum
52 {
53 Widgets_ClearLog = 100,
54 Widgets_Quit
55 };
56
57 // ----------------------------------------------------------------------------
58 // our classes
59 // ----------------------------------------------------------------------------
60
61 // Define a new application type, each program should derive a class from wxApp
62 class WidgetsApp : public wxApp
63 {
64 public:
65 // override base class virtuals
66 // ----------------------------
67
68 // this one is called on application startup and is a good place for the app
69 // initialization (doing it here and not in the ctor allows to have an error
70 // return: if OnInit() returns false, the application terminates)
71 virtual bool OnInit();
72 };
73
74 // Define a new frame type: this is going to be our main frame
75 class WidgetsFrame : public wxFrame
76 {
77 public:
78 // ctor(s) and dtor
79 WidgetsFrame(const wxString& title);
80 virtual ~WidgetsFrame();
81
82 protected:
83 // event handlers
84 void OnButtonClearLog(wxCommandEvent& event);
85 void OnButtonQuit(wxCommandEvent& event);
86
87 // initialize the notebook: add all pages to it
88 void InitNotebook();
89
90 private:
91 // the panel containing everything
92 wxPanel *m_panel;
93
94 // the listbox for logging messages
95 wxListBox *m_lboxLog;
96
97 // the log target we use to redirect messages to the listbox
98 wxLog *m_logTarget;
99
100 // the notebook containing the test pages
101 wxNotebook *m_notebook;
102
103 // and the image list for it
104 wxImageList *m_imaglist;
105
106 // any class wishing to process wxWindows events must use this macro
107 DECLARE_EVENT_TABLE()
108 };
109
110 // A log target which just redirects the messages to a listbox
111 class LboxLogger : public wxLog
112 {
113 public:
114 LboxLogger(wxListBox *lbox, wxLog *logOld)
115 {
116 m_lbox = lbox;
117 //m_lbox->Disable(); -- looks ugly under MSW
118 m_logOld = logOld;
119 }
120
121 virtual ~LboxLogger()
122 {
123 wxLog::SetActiveTarget(m_logOld);
124 }
125
126 private:
127 // implement sink functions
128 virtual void DoLog(wxLogLevel level, const wxChar *szString, time_t t)
129 {
130 // don't put trace messages into listbox or we can get into infinite
131 // recursion
132 if ( level == wxLOG_Trace )
133 {
134 if ( m_logOld )
135 {
136 // cast is needed to call protected method
137 ((LboxLogger *)m_logOld)->DoLog(level, szString, t);
138 }
139 }
140 else
141 {
142 wxLog::DoLog(level, szString, t);
143 }
144 }
145
146 virtual void DoLogString(const wxChar *szString, time_t t)
147 {
148 wxString msg;
149 TimeStamp(&msg);
150 msg += szString;
151
152 #ifdef __WXUNIVERSAL__
153 m_lbox->AppendAndEnsureVisible(msg);
154 #else // other ports don't have this method yet
155 m_lbox->Append(msg);
156 m_lbox->SetFirstItem(m_lbox->GetCount() - 1);
157 #endif
158 }
159
160 // the control we use
161 wxListBox *m_lbox;
162
163 // the old log target
164 wxLog *m_logOld;
165 };
166
167 // array of pages
168 WX_DEFINE_ARRAY(WidgetsPage *, ArrayWidgetsPage);
169
170 // ----------------------------------------------------------------------------
171 // misc macros
172 // ----------------------------------------------------------------------------
173
174 IMPLEMENT_APP(WidgetsApp)
175
176 #ifdef __WXUNIVERSAL__
177 #include "wx/univ/theme.h"
178
179 WX_USE_THEME(win32);
180 WX_USE_THEME(gtk);
181 #endif // __WXUNIVERSAL__
182
183 // ----------------------------------------------------------------------------
184 // event tables
185 // ----------------------------------------------------------------------------
186
187 BEGIN_EVENT_TABLE(WidgetsFrame, wxFrame)
188 EVT_BUTTON(Widgets_ClearLog, WidgetsFrame::OnButtonClearLog)
189 EVT_BUTTON(Widgets_Quit, WidgetsFrame::OnButtonQuit)
190 END_EVENT_TABLE()
191
192 // ============================================================================
193 // implementation
194 // ============================================================================
195
196 // ----------------------------------------------------------------------------
197 // app class
198 // ----------------------------------------------------------------------------
199
200 bool WidgetsApp::OnInit()
201 {
202 // the reason for having these ifdef's is that I often run two copies of
203 // this sample side by side and it is useful to see which one is which
204 wxString title =
205 #if defined(__WXUNIVERSAL__)
206 _T("wxUniv")
207 #elif defined(__WXMSW__)
208 _T("wxMSW")
209 #elif defined(__WXGTK__)
210 _T("wxGTK")
211 #else
212 _T("wxWindows")
213 #endif
214 ;
215
216 wxFrame *frame = new WidgetsFrame(title + _T(" widgets demo"));
217 frame->Show();
218
219 //wxLog::AddTraceMask(_T("listbox"));
220 //wxLog::AddTraceMask(_T("scrollbar"));
221
222 return TRUE;
223 }
224
225 // ----------------------------------------------------------------------------
226 // WidgetsFrame construction
227 // ----------------------------------------------------------------------------
228
229 WidgetsFrame::WidgetsFrame(const wxString& title)
230 : wxFrame(NULL, -1, title, wxPoint(0, 50))
231 {
232 // init everything
233 m_lboxLog = (wxListBox *)NULL;
234 m_logTarget = (wxLog *)NULL;
235 m_notebook = (wxNotebook *)NULL;
236 m_imaglist = (wxImageList *)NULL;
237
238 // create controls
239 m_panel = new wxPanel(this, -1);
240
241 wxSizer *sizerTop = new wxBoxSizer(wxVERTICAL);
242
243 // we have 2 panes: notebook which pages demonstrating the controls in the
244 // upper one and the log window with some buttons in the lower
245
246 m_notebook = new wxNotebook(m_panel, -1);
247 InitNotebook();
248 wxSizer *sizerUp = new wxNotebookSizer(m_notebook);
249
250 // the lower one only has the log listbox and a button to clear it
251 wxSizer *sizerDown = new wxStaticBoxSizer
252 (
253 new wxStaticBox(m_panel, -1, _T("&Log window")),
254 wxVERTICAL
255 );
256 m_lboxLog = new wxListBox(m_panel, -1);
257 sizerDown->Add(m_lboxLog, 1, wxGROW | wxALL, 5);
258 wxBoxSizer *sizerBtns = new wxBoxSizer(wxHORIZONTAL);
259 wxButton *btn = new wxButton(m_panel, Widgets_ClearLog, _T("Clear &log"));
260 sizerBtns->Add(btn);
261 sizerBtns->Add(10, 0); // spacer
262 btn = new wxButton(m_panel, Widgets_Quit, _T("E&xit"));
263 sizerBtns->Add(btn);
264 sizerDown->Add(sizerBtns, 0, wxALL | wxALIGN_RIGHT, 5);
265
266 // put everything together
267 sizerTop->Add(sizerUp, 1, wxGROW | (wxALL & ~(wxTOP | wxBOTTOM)), 10);
268 sizerTop->Add(0, 5, 0, wxGROW); // spacer in between
269 sizerTop->Add(sizerDown, 0, wxGROW | (wxALL & ~wxTOP), 10);
270
271 m_panel->SetAutoLayout(TRUE);
272 m_panel->SetSizer(sizerTop);
273
274 sizerTop->Fit(this);
275 sizerTop->SetSizeHints(this);
276
277 // now that everything is created we can redirect the log messages to the
278 // listbox
279 m_logTarget = new LboxLogger(m_lboxLog, wxLog::GetActiveTarget());
280 wxLog::SetActiveTarget(m_logTarget);
281 }
282
283 void WidgetsFrame::InitNotebook()
284 {
285 m_imaglist = new wxImageList(32, 32);
286
287 ArrayWidgetsPage pages;
288 wxArrayString labels;
289
290 // we need to first create all pages and only then add them to the notebook
291 // as we need the image list first
292 WidgetsPageInfo *info = WidgetsPage::ms_widgetPages;
293 while ( info )
294 {
295 WidgetsPage *page = (*info->GetCtor())(m_notebook, m_imaglist);
296 pages.Add(page);
297
298 labels.Add(info->GetLabel());
299
300 info = info->GetNext();
301 }
302
303 m_notebook->SetImageList(m_imaglist);
304
305 // now do add them
306 size_t count = pages.GetCount();
307 for ( size_t n = 0; n < count; n++ )
308 {
309 m_notebook->AddPage(
310 pages[n],
311 labels[n],
312 FALSE, // don't select
313 n // image id
314 );
315 }
316 }
317
318 WidgetsFrame::~WidgetsFrame()
319 {
320 delete m_logTarget;
321 delete m_imaglist;
322 }
323
324 // ----------------------------------------------------------------------------
325 // WidgetsFrame event handlers
326 // ----------------------------------------------------------------------------
327
328 void WidgetsFrame::OnButtonQuit(wxCommandEvent& WXUNUSED(event))
329 {
330 Close();
331 }
332
333 void WidgetsFrame::OnButtonClearLog(wxCommandEvent& event)
334 {
335 m_lboxLog->Clear();
336 }
337
338 // ----------------------------------------------------------------------------
339 // WidgetsPageInfo
340 // ----------------------------------------------------------------------------
341
342 WidgetsPageInfo *WidgetsPage::ms_widgetPages = NULL;
343
344 WidgetsPageInfo::WidgetsPageInfo(Constructor ctor, const wxChar *label)
345 : m_label(label)
346 {
347 m_ctor = ctor;
348
349 m_next = WidgetsPage::ms_widgetPages;
350 WidgetsPage::ms_widgetPages = this;
351 }
352
353 // ----------------------------------------------------------------------------
354 // WidgetsPage
355 // ----------------------------------------------------------------------------
356
357 WidgetsPage::WidgetsPage(wxNotebook *notebook)
358 : wxPanel(notebook, -1)
359 {
360 }
361
362 wxSizer *WidgetsPage::CreateSizerWithText(wxControl *control,
363 wxWindowID id,
364 wxTextCtrl **ppText)
365 {
366 wxSizer *sizerRow = new wxBoxSizer(wxHORIZONTAL);
367 wxTextCtrl *text = new wxTextCtrl(this, id, _T(""));
368 sizerRow->Add(control, 0, wxRIGHT | wxALIGN_CENTRE_VERTICAL, 5);
369 sizerRow->Add(text, 1, wxLEFT | wxALIGN_CENTRE_VERTICAL, 5);
370
371 if ( ppText )
372 *ppText = text;
373
374 return sizerRow;
375 }
376
377 // create a sizer containing a label and a text ctrl
378 wxSizer *WidgetsPage::CreateSizerWithTextAndLabel(const wxString& label,
379 wxWindowID id,
380 wxTextCtrl **ppText)
381 {
382 return CreateSizerWithText(new wxStaticText(this, -1, label), id, ppText);
383 }
384
385 // create a sizer containing a button and a text ctrl
386 wxSizer *WidgetsPage::CreateSizerWithTextAndButton(wxWindowID idBtn,
387 const wxString& label,
388 wxWindowID id,
389 wxTextCtrl **ppText)
390 {
391 return CreateSizerWithText(new wxButton(this, idBtn, label), id, ppText);
392 }
393
394 wxCheckBox *WidgetsPage::CreateCheckBoxAndAddToSizer(wxSizer *sizer,
395 const wxString& label,
396 wxWindowID id)
397 {
398 wxCheckBox *checkbox = new wxCheckBox(this, id, label);
399 sizer->Add(checkbox, 0, wxLEFT | wxRIGHT, 5);
400 sizer->Add(0, 2, 0, wxGROW); // spacer
401
402 return checkbox;
403 }
404