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