]> git.saurik.com Git - wxWidgets.git/blob - samples/widgets/widgets.cpp
eVC3 warning fix.
[wxWidgets.git] / samples / widgets / widgets.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
3 // Name: widgets.cpp
4 // Purpose: Sample showing most of the simple wxWidgets 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/frame.h"
32 #include "wx/menu.h"
33
34 #include "wx/button.h"
35 #include "wx/checkbox.h"
36 #include "wx/listbox.h"
37 #include "wx/statbox.h"
38 #include "wx/stattext.h"
39 #include "wx/textctrl.h"
40 #endif
41
42 #include "wx/bookctrl.h"
43 #include "wx/sizer.h"
44 #include "wx/colordlg.h"
45
46 #include "widgets.h"
47
48 // ----------------------------------------------------------------------------
49 // constants
50 // ----------------------------------------------------------------------------
51
52 // control ids
53 enum
54 {
55 Widgets_ClearLog = 100,
56 Widgets_Quit,
57 Widgets_SetFgColour,
58 Widgets_SetBgColour
59 };
60
61 // ----------------------------------------------------------------------------
62 // our classes
63 // ----------------------------------------------------------------------------
64
65 // Define a new application type, each program should derive a class from wxApp
66 class WidgetsApp : public wxApp
67 {
68 public:
69 // override base class virtuals
70 // ----------------------------
71
72 // this one is called on application startup and is a good place for the app
73 // initialization (doing it here and not in the ctor allows to have an error
74 // return: if OnInit() returns false, the application terminates)
75 virtual bool OnInit();
76 };
77
78 // Define a new frame type: this is going to be our main frame
79 class WidgetsFrame : public wxFrame
80 {
81 public:
82 // ctor(s) and dtor
83 WidgetsFrame(const wxString& title);
84 virtual ~WidgetsFrame();
85
86 protected:
87 // event handlers
88 #if wxUSE_LOG
89 void OnButtonClearLog(wxCommandEvent& event);
90 #endif // wxUSE_LOG
91 void OnExit(wxCommandEvent& event);
92 #if wxUSE_MENUS
93 void OnSetFgCol(wxCommandEvent& event);
94 void OnSetBgCol(wxCommandEvent& event);
95 #endif // wxUSE_MENUS
96
97 // initialize the book: add all pages to it
98 void InitBook();
99
100 private:
101 // the panel containing everything
102 wxPanel *m_panel;
103
104 #if wxUSE_LOG
105 // the listbox for logging messages
106 wxListBox *m_lboxLog;
107
108 // the log target we use to redirect messages to the listbox
109 wxLog *m_logTarget;
110 #endif // wxUSE_LOG
111
112 // the book containing the test pages
113 wxBookCtrl *m_book;
114
115 // and the image list for it
116 wxImageList *m_imaglist;
117
118 #if wxUSE_MENUS
119 // last chosen fg/bg colours
120 wxColour m_colFg,
121 m_colBg;
122 #endif // wxUSE_MENUS
123
124 // any class wishing to process wxWidgets events must use this macro
125 DECLARE_EVENT_TABLE()
126 };
127
128 #if wxUSE_LOG
129 // A log target which just redirects the messages to a listbox
130 class LboxLogger : public wxLog
131 {
132 public:
133 LboxLogger(wxListBox *lbox, wxLog *logOld)
134 {
135 m_lbox = lbox;
136 //m_lbox->Disable(); -- looks ugly under MSW
137 m_logOld = logOld;
138 }
139
140 virtual ~LboxLogger()
141 {
142 wxLog::SetActiveTarget(m_logOld);
143 }
144
145 private:
146 // implement sink functions
147 virtual void DoLog(wxLogLevel level, const wxChar *szString, time_t t)
148 {
149 // don't put trace messages into listbox or we can get into infinite
150 // recursion
151 if ( level == wxLOG_Trace )
152 {
153 if ( m_logOld )
154 {
155 // cast is needed to call protected method
156 ((LboxLogger *)m_logOld)->DoLog(level, szString, t);
157 }
158 }
159 else
160 {
161 wxLog::DoLog(level, szString, t);
162 }
163 }
164
165 virtual void DoLogString(const wxChar *szString, time_t WXUNUSED(t))
166 {
167 wxString msg;
168 TimeStamp(&msg);
169 msg += szString;
170
171 #ifdef __WXUNIVERSAL__
172 m_lbox->AppendAndEnsureVisible(msg);
173 #else // other ports don't have this method yet
174 m_lbox->Append(msg);
175 m_lbox->SetFirstItem(m_lbox->GetCount() - 1);
176 #endif
177 }
178
179 // the control we use
180 wxListBox *m_lbox;
181
182 // the old log target
183 wxLog *m_logOld;
184 };
185 #endif // wxUSE_LOG
186
187 // array of pages
188 WX_DEFINE_ARRAY_PTR(WidgetsPage *, ArrayWidgetsPage);
189
190 // ----------------------------------------------------------------------------
191 // misc macros
192 // ----------------------------------------------------------------------------
193
194 IMPLEMENT_APP(WidgetsApp)
195
196 // ----------------------------------------------------------------------------
197 // event tables
198 // ----------------------------------------------------------------------------
199
200 BEGIN_EVENT_TABLE(WidgetsFrame, wxFrame)
201 #if wxUSE_LOG
202 EVT_BUTTON(Widgets_ClearLog, WidgetsFrame::OnButtonClearLog)
203 #endif // wxUSE_LOG
204 EVT_BUTTON(Widgets_Quit, WidgetsFrame::OnExit)
205
206 EVT_MENU(wxID_EXIT, WidgetsFrame::OnExit)
207 EVT_MENU(Widgets_SetFgColour, WidgetsFrame::OnSetFgCol)
208 EVT_MENU(Widgets_SetBgColour, WidgetsFrame::OnSetBgCol)
209 END_EVENT_TABLE()
210
211 // ============================================================================
212 // implementation
213 // ============================================================================
214
215 // ----------------------------------------------------------------------------
216 // app class
217 // ----------------------------------------------------------------------------
218
219 bool WidgetsApp::OnInit()
220 {
221 if ( !wxApp::OnInit() )
222 return false;
223
224 // the reason for having these ifdef's is that I often run two copies of
225 // this sample side by side and it is useful to see which one is which
226 wxString title;
227 #if defined(__WXUNIVERSAL__)
228 title = _T("wxUniv/");
229 #endif
230
231 #if defined(__WXMSW__)
232 title += _T("wxMSW");
233 #elif defined(__WXGTK__)
234 title += _T("wxGTK");
235 #elif defined(__WXMAC__)
236 title += _T("wxMAC");
237 #elif defined(__WXMOTIF__)
238 title += _T("wxMOTIF");
239 #else
240 title += _T("wxWidgets");
241 #endif
242
243 wxFrame *frame = new WidgetsFrame(title + _T(" widgets demo"));
244 frame->Show();
245
246 //wxLog::AddTraceMask(_T("listbox"));
247 //wxLog::AddTraceMask(_T("scrollbar"));
248 //wxLog::AddTraceMask(_T("focus"));
249
250 return true;
251 }
252
253 // ----------------------------------------------------------------------------
254 // WidgetsFrame construction
255 // ----------------------------------------------------------------------------
256
257 WidgetsFrame::WidgetsFrame(const wxString& title)
258 : wxFrame(NULL, wxID_ANY, title,
259 wxPoint(0, 50), wxDefaultSize,
260 wxDEFAULT_FRAME_STYLE |
261 wxNO_FULL_REPAINT_ON_RESIZE |
262 wxCLIP_CHILDREN |
263 wxTAB_TRAVERSAL)
264 {
265 // init everything
266 #if wxUSE_LOG
267 m_lboxLog = (wxListBox *)NULL;
268 m_logTarget = (wxLog *)NULL;
269 #endif // wxUSE_LOG
270 m_book = (wxBookCtrl *)NULL;
271 m_imaglist = (wxImageList *)NULL;
272
273 #if wxUSE_MENUS
274 // create the menubar
275 wxMenuBar *mbar = new wxMenuBar;
276 wxMenu *menuWidget = new wxMenu;
277 menuWidget->Append(Widgets_SetFgColour, _T("Set &foreground...\tCtrl-F"));
278 menuWidget->Append(Widgets_SetBgColour, _T("Set &background...\tCtrl-B"));
279 menuWidget->AppendSeparator();
280 menuWidget->Append(wxID_EXIT, _T("&Quit\tCtrl-Q"));
281 mbar->Append(menuWidget, _T("&Widget"));
282 SetMenuBar(mbar);
283 #endif // wxUSE_MENUS
284
285 // create controls
286 m_panel = new wxPanel(this, wxID_ANY,
287 wxDefaultPosition, wxDefaultSize, wxCLIP_CHILDREN);
288
289 wxSizer *sizerTop = new wxBoxSizer(wxVERTICAL);
290
291 // we have 2 panes: book which pages demonstrating the controls in the
292 // upper one and the log window with some buttons in the lower
293
294 m_book = new wxBookCtrl(m_panel, wxID_ANY, wxDefaultPosition,
295 wxDefaultSize, wxNO_FULL_REPAINT_ON_RESIZE|wxCLIP_CHILDREN|wxBC_DEFAULT);
296 InitBook();
297
298 // the lower one only has the log listbox and a button to clear it
299 #if wxUSE_LOG
300 wxSizer *sizerDown = new wxStaticBoxSizer(
301 new wxStaticBox( m_panel, wxID_ANY, _T("&Log window") ),
302 wxVERTICAL);
303
304 m_lboxLog = new wxListBox(m_panel, wxID_ANY);
305 sizerDown->Add(m_lboxLog, 1, wxGROW | wxALL, 5);
306 sizerDown->SetMinSize(100, 150);
307 #else
308 wxSizer *sizerDown = new wxBoxSizer(wxVERTICAL);
309 #endif // wxUSE_LOG
310
311 wxBoxSizer *sizerBtns = new wxBoxSizer(wxHORIZONTAL);
312 wxButton *btn;
313 #if wxUSE_LOG
314 btn = new wxButton(m_panel, Widgets_ClearLog, _T("Clear &log"));
315 sizerBtns->Add(btn);
316 sizerBtns->Add(10, 0); // spacer
317 #endif // wxUSE_LOG
318 btn = new wxButton(m_panel, Widgets_Quit, _T("E&xit"));
319 sizerBtns->Add(btn);
320 sizerDown->Add(sizerBtns, 0, wxALL | wxALIGN_RIGHT, 5);
321
322 // put everything together
323 sizerTop->Add(m_book, 1, wxGROW | (wxALL & ~(wxTOP | wxBOTTOM)), 10);
324 sizerTop->Add(0, 5, 0, wxGROW); // spacer in between
325 sizerTop->Add(sizerDown, 0, wxGROW | (wxALL & ~wxTOP), 10);
326
327 m_panel->SetSizer(sizerTop);
328
329 sizerTop->Fit(this);
330 sizerTop->SetSizeHints(this);
331
332 #if wxUSE_LOG && !defined(__WXCOCOA__)
333 // wxCocoa's listbox is too flakey to use for logging right now
334 // now that everything is created we can redirect the log messages to the
335 // listbox
336 m_logTarget = new LboxLogger(m_lboxLog, wxLog::GetActiveTarget());
337 wxLog::SetActiveTarget(m_logTarget);
338 #endif
339 }
340
341 void WidgetsFrame::InitBook()
342 {
343 m_imaglist = new wxImageList(32, 32);
344
345 ArrayWidgetsPage pages;
346 wxArrayString labels;
347
348 // we need to first create all pages and only then add them to the book
349 // as we need the image list first
350 WidgetsPageInfo *info = WidgetsPage::ms_widgetPages;
351 while ( info )
352 {
353 WidgetsPage *page = (*info->GetCtor())(m_book, m_imaglist);
354 pages.Add(page);
355
356 labels.Add(info->GetLabel());
357
358 info = info->GetNext();
359 }
360
361 m_book->SetImageList(m_imaglist);
362
363 // now do add them
364 size_t count = pages.GetCount();
365 for ( size_t n = 0; n < count; n++ )
366 {
367 m_book->AddPage(
368 pages[n],
369 labels[n],
370 false, // don't select
371 n // image id
372 );
373 }
374 }
375
376 WidgetsFrame::~WidgetsFrame()
377 {
378 #if wxUSE_LOG
379 delete m_logTarget;
380 #endif // wxUSE_LOG
381 delete m_imaglist;
382 }
383
384 // ----------------------------------------------------------------------------
385 // WidgetsFrame event handlers
386 // ----------------------------------------------------------------------------
387
388 void WidgetsFrame::OnExit(wxCommandEvent& WXUNUSED(event))
389 {
390 Close();
391 }
392
393 #if wxUSE_LOG
394 void WidgetsFrame::OnButtonClearLog(wxCommandEvent& WXUNUSED(event))
395 {
396 m_lboxLog->Clear();
397 }
398 #endif // wxUSE_LOG
399
400 #if wxUSE_MENUS
401
402 void WidgetsFrame::OnSetFgCol(wxCommandEvent& WXUNUSED(event))
403 {
404 wxColour col = wxGetColourFromUser(this, m_colFg);
405 if ( !col.Ok() )
406 return;
407
408 m_colFg = col;
409
410 WidgetsPage *page = wxStaticCast(m_book->GetCurrentPage(), WidgetsPage);
411 page->GetWidget()->SetForegroundColour(m_colFg);
412 page->GetWidget()->Refresh();
413 }
414
415 void WidgetsFrame::OnSetBgCol(wxCommandEvent& WXUNUSED(event))
416 {
417 wxColour col = wxGetColourFromUser(this, m_colBg);
418 if ( !col.Ok() )
419 return;
420
421 m_colBg = col;
422
423 WidgetsPage *page = wxStaticCast(m_book->GetCurrentPage(), WidgetsPage);
424 page->GetWidget()->SetBackgroundColour(m_colBg);
425 page->GetWidget()->Refresh();
426 }
427
428 #endif // wxUSE_MENUS
429
430 // ----------------------------------------------------------------------------
431 // WidgetsPageInfo
432 // ----------------------------------------------------------------------------
433
434 WidgetsPageInfo *WidgetsPage::ms_widgetPages = NULL;
435
436 WidgetsPageInfo::WidgetsPageInfo(Constructor ctor, const wxChar *label)
437 : m_label(label)
438 {
439 m_ctor = ctor;
440
441 m_next = NULL;
442
443 // dummy sorting: add and immediately sort on list according to label
444
445 if(WidgetsPage::ms_widgetPages)
446 {
447 WidgetsPageInfo *node_prev = WidgetsPage::ms_widgetPages;
448 if(wxStrcmp(label,node_prev->GetLabel().c_str())<0)
449 {
450 // add as first
451 m_next = node_prev;
452 WidgetsPage::ms_widgetPages = this;
453 }
454 else
455 {
456 WidgetsPageInfo *node_next;
457 do
458 {
459 node_next = node_prev->GetNext();
460 if(node_next)
461 {
462 // add if between two
463 if(wxStrcmp(label,node_next->GetLabel().c_str())<0)
464 {
465 node_prev->SetNext(this);
466 m_next = node_next;
467 // force to break loop
468 node_next = NULL;
469 }
470 }
471 else
472 {
473 // add as last
474 node_prev->SetNext(this);
475 m_next = node_next;
476 }
477 node_prev = node_next;
478 }while(node_next);
479 }
480 }
481 else
482 {
483 // add when first
484
485 WidgetsPage::ms_widgetPages = this;
486
487 }
488
489 }
490
491 // ----------------------------------------------------------------------------
492 // WidgetsPage
493 // ----------------------------------------------------------------------------
494
495 WidgetsPage::WidgetsPage(wxBookCtrl *book)
496 : wxPanel(book, wxID_ANY,
497 wxDefaultPosition, wxDefaultSize,
498 wxNO_FULL_REPAINT_ON_RESIZE |
499 wxCLIP_CHILDREN |
500 wxTAB_TRAVERSAL)
501 {
502 }
503
504 wxSizer *WidgetsPage::CreateSizerWithText(wxControl *control,
505 wxWindowID id,
506 wxTextCtrl **ppText)
507 {
508 wxSizer *sizerRow = new wxBoxSizer(wxHORIZONTAL);
509 wxTextCtrl *text = new wxTextCtrl(this, id, wxEmptyString,
510 wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER);
511
512 sizerRow->Add(control, 0, wxRIGHT | wxALIGN_CENTRE_VERTICAL, 5);
513 sizerRow->Add(text, 1, wxLEFT | wxALIGN_CENTRE_VERTICAL, 5);
514
515 if ( ppText )
516 *ppText = text;
517
518 return sizerRow;
519 }
520
521 // create a sizer containing a label and a text ctrl
522 wxSizer *WidgetsPage::CreateSizerWithTextAndLabel(const wxString& label,
523 wxWindowID id,
524 wxTextCtrl **ppText)
525 {
526 return CreateSizerWithText(new wxStaticText(this, wxID_ANY, label),
527 id, ppText);
528 }
529
530 // create a sizer containing a button and a text ctrl
531 wxSizer *WidgetsPage::CreateSizerWithTextAndButton(wxWindowID idBtn,
532 const wxString& label,
533 wxWindowID id,
534 wxTextCtrl **ppText)
535 {
536 return CreateSizerWithText(new wxButton(this, idBtn, label), id, ppText);
537 }
538
539 wxCheckBox *WidgetsPage::CreateCheckBoxAndAddToSizer(wxSizer *sizer,
540 const wxString& label,
541 wxWindowID id)
542 {
543 wxCheckBox *checkbox = new wxCheckBox(this, id, label);
544 sizer->Add(checkbox, 0, wxLEFT | wxRIGHT, 5);
545 sizer->Add(0, 2, 0, wxGROW); // spacer
546
547 return checkbox;
548 }
549