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