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