suppress wxLog::DoLog() and DoLogString() hiding warnings
[wxWidgets.git] / samples / widgets / widgets.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
3 // Name: samples/widgets/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 #include "wx/image.h"
34
35 #include "wx/button.h"
36 #include "wx/checkbox.h"
37 #include "wx/listbox.h"
38 #include "wx/statbox.h"
39 #include "wx/stattext.h"
40 #include "wx/textctrl.h"
41 #include "wx/msgdlg.h"
42 #endif
43
44 #include "wx/sysopt.h"
45 #include "wx/bookctrl.h"
46 #include "wx/treebook.h"
47 #include "wx/sizer.h"
48 #include "wx/colordlg.h"
49 #include "wx/fontdlg.h"
50 #include "wx/textdlg.h"
51 #include "wx/imaglist.h"
52 #include "wx/wupdlock.h"
53
54 #include "wx/persist/toplevel.h"
55 #include "wx/persist/treebook.h"
56
57 #include "widgets.h"
58
59 #include "../sample.xpm"
60
61 // ----------------------------------------------------------------------------
62 // constants
63 // ----------------------------------------------------------------------------
64
65 // control ids
66 enum
67 {
68 Widgets_ClearLog = 100,
69 Widgets_Quit,
70
71 Widgets_BookCtrl,
72
73 #if wxUSE_TOOLTIPS
74 Widgets_SetTooltip,
75 #endif // wxUSE_TOOLTIPS
76 Widgets_SetFgColour,
77 Widgets_SetBgColour,
78 Widgets_SetFont,
79 Widgets_Enable,
80
81 Widgets_BorderNone,
82 Widgets_BorderStatic,
83 Widgets_BorderSimple,
84 Widgets_BorderRaised,
85 Widgets_BorderSunken,
86 Widgets_BorderDouble,
87 Widgets_BorderDefault,
88
89 Widgets_GlobalBusyCursor,
90 Widgets_BusyCursor,
91
92 Widgets_GoToPage,
93 Widgets_GoToPageLast = Widgets_GoToPage + 100,
94
95
96 TextEntry_Begin,
97 TextEntry_DisableAutoComplete = TextEntry_Begin,
98 TextEntry_AutoCompleteFixed,
99 TextEntry_AutoCompleteFilenames,
100
101 TextEntry_SetHint,
102 TextEntry_End
103 };
104
105 const wxChar *WidgetsCategories[MAX_PAGES] = {
106 #if defined(__WXUNIVERSAL__)
107 wxT("Universal"),
108 #else
109 wxT("Native"),
110 #endif
111 wxT("Generic"),
112 wxT("Pickers"),
113 wxT("Comboboxes"),
114 wxT("With items"),
115 wxT("Editable"),
116 wxT("Books"),
117 wxT("All controls")
118 };
119
120 // ----------------------------------------------------------------------------
121 // our classes
122 // ----------------------------------------------------------------------------
123
124 // Define a new application type, each program should derive a class from wxApp
125 class WidgetsApp : public wxApp
126 {
127 public:
128 // override base class virtuals
129 // ----------------------------
130
131 // this one is called on application startup and is a good place for the app
132 // initialization (doing it here and not in the ctor allows to have an error
133 // return: if OnInit() returns false, the application terminates)
134 virtual bool OnInit();
135 };
136
137 // Define a new frame type: this is going to be our main frame
138 class WidgetsFrame : public wxFrame
139 {
140 public:
141 // ctor(s) and dtor
142 WidgetsFrame(const wxString& title);
143 virtual ~WidgetsFrame();
144
145 protected:
146 // event handlers
147 #if USE_LOG
148 void OnButtonClearLog(wxCommandEvent& event);
149 #endif // USE_LOG
150 void OnExit(wxCommandEvent& event);
151
152 #if wxUSE_MENUS
153 void OnPageChanging(WidgetsBookCtrlEvent& event);
154 void OnPageChanged(WidgetsBookCtrlEvent& event);
155 void OnGoToPage(wxCommandEvent& event);
156
157 #if wxUSE_TOOLTIPS
158 void OnSetTooltip(wxCommandEvent& event);
159 #endif // wxUSE_TOOLTIPS
160 void OnSetFgCol(wxCommandEvent& event);
161 void OnSetBgCol(wxCommandEvent& event);
162 void OnSetFont(wxCommandEvent& event);
163 void OnEnable(wxCommandEvent& event);
164 void OnSetBorder(wxCommandEvent& event);
165
166 void OnToggleGlobalBusyCursor(wxCommandEvent& event);
167 void OnToggleBusyCursor(wxCommandEvent& event);
168
169 // wxTextEntry-specific tests
170 void OnDisableAutoComplete(wxCommandEvent& event);
171 void OnAutoCompleteFixed(wxCommandEvent& event);
172 void OnAutoCompleteFilenames(wxCommandEvent& event);
173
174 void OnSetHint(wxCommandEvent& event);
175
176 void OnUpdateTextUI(wxUpdateUIEvent& event)
177 {
178 event.Enable( CurrentPage()->GetTextEntry() != NULL );
179 }
180 #endif // wxUSE_MENUS
181
182 // initialize the book: add all pages to it
183 void InitBook();
184
185 // return the currently selected page (never NULL)
186 WidgetsPage *CurrentPage();
187
188 private:
189 // the panel containing everything
190 wxPanel *m_panel;
191
192 #if USE_LOG
193 // the listbox for logging messages
194 wxListBox *m_lboxLog;
195
196 // the log target we use to redirect messages to the listbox
197 wxLog *m_logTarget;
198 #endif // USE_LOG
199
200 // the book containing the test pages
201 WidgetsBookCtrl *m_book;
202
203 #if wxUSE_MENUS
204 // last chosen fg/bg colours and font
205 wxColour m_colFg,
206 m_colBg;
207 wxFont m_font;
208 #endif // wxUSE_MENUS
209
210 // any class wishing to process wxWidgets events must use this macro
211 DECLARE_EVENT_TABLE()
212 };
213
214 #if USE_LOG
215 // A log target which just redirects the messages to a listbox
216 class LboxLogger : public wxLog
217 {
218 public:
219 LboxLogger(wxListBox *lbox, wxLog *logOld)
220 {
221 m_lbox = lbox;
222 //m_lbox->Disable(); -- looks ugly under MSW
223 m_logOld = logOld;
224 }
225
226 virtual ~LboxLogger()
227 {
228 wxLog::SetActiveTarget(m_logOld);
229 }
230
231 private:
232 wxSUPPRESS_DOLOG_HIDE_WARNING()
233 wxSUPPRESS_DOLOGSTRING_HIDE_WARNING()
234
235 // implement sink functions
236 virtual void DoLog(wxLogLevel level, const wxString& str, time_t t)
237 {
238 // don't put trace messages into listbox or we can get into infinite
239 // recursion
240 if ( level == wxLOG_Trace )
241 {
242 if ( m_logOld )
243 {
244 // cast is needed to call protected method
245 ((LboxLogger *)m_logOld)->DoLog(level, str, t);
246 }
247 }
248 else
249 {
250 wxLog::DoLog(level, str, t);
251 }
252 }
253
254 virtual void DoLogString(const wxString& str, time_t WXUNUSED(t))
255 {
256 wxString msg;
257 TimeStamp(&msg);
258 msg += str;
259
260 #ifdef __WXUNIVERSAL__
261 m_lbox->AppendAndEnsureVisible(msg);
262 #else // other ports don't have this method yet
263 m_lbox->Append(msg);
264 m_lbox->SetFirstItem(m_lbox->GetCount() - 1);
265 #endif
266 }
267
268 // the control we use
269 wxListBox *m_lbox;
270
271 // the old log target
272 wxLog *m_logOld;
273 };
274 #endif // USE_LOG
275
276 // array of pages
277 WX_DEFINE_ARRAY_PTR(WidgetsPage *, ArrayWidgetsPage);
278
279 // ----------------------------------------------------------------------------
280 // misc macros
281 // ----------------------------------------------------------------------------
282
283 IMPLEMENT_APP(WidgetsApp)
284
285 // ----------------------------------------------------------------------------
286 // event tables
287 // ----------------------------------------------------------------------------
288
289 BEGIN_EVENT_TABLE(WidgetsFrame, wxFrame)
290 #if USE_LOG
291 EVT_BUTTON(Widgets_ClearLog, WidgetsFrame::OnButtonClearLog)
292 #endif // USE_LOG
293 EVT_BUTTON(Widgets_Quit, WidgetsFrame::OnExit)
294
295 #if wxUSE_TOOLTIPS
296 EVT_MENU(Widgets_SetTooltip, WidgetsFrame::OnSetTooltip)
297 #endif // wxUSE_TOOLTIPS
298
299 #if wxUSE_MENUS
300 EVT_WIDGETS_PAGE_CHANGING(wxID_ANY, WidgetsFrame::OnPageChanging)
301 EVT_MENU_RANGE(Widgets_GoToPage, Widgets_GoToPageLast,
302 WidgetsFrame::OnGoToPage)
303
304 EVT_MENU(Widgets_SetFgColour, WidgetsFrame::OnSetFgCol)
305 EVT_MENU(Widgets_SetBgColour, WidgetsFrame::OnSetBgCol)
306 EVT_MENU(Widgets_SetFont, WidgetsFrame::OnSetFont)
307 EVT_MENU(Widgets_Enable, WidgetsFrame::OnEnable)
308
309 EVT_MENU_RANGE(Widgets_BorderNone, Widgets_BorderDefault,
310 WidgetsFrame::OnSetBorder)
311
312 EVT_MENU(Widgets_GlobalBusyCursor, WidgetsFrame::OnToggleGlobalBusyCursor)
313 EVT_MENU(Widgets_BusyCursor, WidgetsFrame::OnToggleBusyCursor)
314
315 EVT_MENU(TextEntry_DisableAutoComplete, WidgetsFrame::OnDisableAutoComplete)
316 EVT_MENU(TextEntry_AutoCompleteFixed, WidgetsFrame::OnAutoCompleteFixed)
317 EVT_MENU(TextEntry_AutoCompleteFilenames, WidgetsFrame::OnAutoCompleteFilenames)
318
319 EVT_MENU(TextEntry_SetHint, WidgetsFrame::OnSetHint)
320
321 EVT_UPDATE_UI_RANGE(TextEntry_Begin, TextEntry_End - 1,
322 WidgetsFrame::OnUpdateTextUI)
323
324 EVT_MENU(wxID_EXIT, WidgetsFrame::OnExit)
325 #endif // wxUSE_MENUS
326 END_EVENT_TABLE()
327
328 // ============================================================================
329 // implementation
330 // ============================================================================
331
332 // ----------------------------------------------------------------------------
333 // app class
334 // ----------------------------------------------------------------------------
335
336 bool WidgetsApp::OnInit()
337 {
338 if ( !wxApp::OnInit() )
339 return false;
340
341 SetVendorName("wxWidgets_Samples");
342
343 // the reason for having these ifdef's is that I often run two copies of
344 // this sample side by side and it is useful to see which one is which
345 wxString title;
346 #if defined(__WXUNIVERSAL__)
347 title = _T("wxUniv/");
348 #endif
349
350 #if defined(__WXMSW__)
351 title += _T("wxMSW");
352 #elif defined(__WXGTK__)
353 title += _T("wxGTK");
354 #elif defined(__WXMAC__)
355 title += _T("wxMAC");
356 #elif defined(__WXMOTIF__)
357 title += _T("wxMOTIF");
358 #elif __WXPALMOS5__
359 title += _T("wxPALMOS5");
360 #elif __WXPALMOS6__
361 title += _T("wxPALMOS6");
362 #else
363 title += _T("wxWidgets");
364 #endif
365
366 wxFrame *frame = new WidgetsFrame(title + _T(" widgets demo"));
367 frame->Show();
368
369 return true;
370 }
371
372 // ----------------------------------------------------------------------------
373 // WidgetsFrame construction
374 // ----------------------------------------------------------------------------
375
376 WidgetsFrame::WidgetsFrame(const wxString& title)
377 : wxFrame(NULL, wxID_ANY, title)
378 {
379 SetName("Main");
380 const bool sizeSet = wxPersistentRegisterAndRestore(this);
381
382 // set the frame icon
383 SetIcon(wxICON(sample));
384
385 // init everything
386 #if USE_LOG
387 m_lboxLog = NULL;
388 m_logTarget = NULL;
389 #endif // USE_LOG
390 m_book = NULL;
391
392 #if wxUSE_MENUS
393 // create the menubar
394 wxMenuBar *mbar = new wxMenuBar;
395 wxMenu *menuWidget = new wxMenu;
396 #if wxUSE_TOOLTIPS
397 menuWidget->Append(Widgets_SetTooltip, _T("Set &tooltip...\tCtrl-T"));
398 menuWidget->AppendSeparator();
399 #endif // wxUSE_TOOLTIPS
400 menuWidget->Append(Widgets_SetFgColour, _T("Set &foreground...\tCtrl-F"));
401 menuWidget->Append(Widgets_SetBgColour, _T("Set &background...\tCtrl-B"));
402 menuWidget->Append(Widgets_SetFont, _T("Set f&ont...\tCtrl-O"));
403 menuWidget->AppendCheckItem(Widgets_Enable, _T("&Enable/disable\tCtrl-E"));
404
405 wxMenu *menuBorders = new wxMenu;
406 menuBorders->AppendRadioItem(Widgets_BorderDefault, _T("De&fault\tCtrl-Shift-9"));
407 menuBorders->AppendRadioItem(Widgets_BorderNone, _T("&None\tCtrl-Shift-0"));
408 menuBorders->AppendRadioItem(Widgets_BorderSimple, _T("&Simple\tCtrl-Shift-1"));
409 menuBorders->AppendRadioItem(Widgets_BorderDouble, _T("&Double\tCtrl-Shift-2"));
410 menuBorders->AppendRadioItem(Widgets_BorderStatic, _T("Stati&c\tCtrl-Shift-3"));
411 menuBorders->AppendRadioItem(Widgets_BorderRaised, _T("&Raised\tCtrl-Shift-4"));
412 menuBorders->AppendRadioItem(Widgets_BorderSunken, _T("S&unken\tCtrl-Shift-5"));
413 menuWidget->AppendSubMenu(menuBorders, _T("Set &border"));
414
415 menuWidget->AppendSeparator();
416 menuWidget->AppendCheckItem(Widgets_GlobalBusyCursor,
417 _T("Toggle &global busy cursor\tCtrl-Shift-U"));
418 menuWidget->AppendCheckItem(Widgets_BusyCursor,
419 _T("Toggle b&usy cursor\tCtrl-U"));
420
421 menuWidget->AppendSeparator();
422 menuWidget->Append(wxID_EXIT, _T("&Quit\tCtrl-Q"));
423 mbar->Append(menuWidget, _T("&Widget"));
424
425 wxMenu *menuTextEntry = new wxMenu;
426 menuTextEntry->AppendRadioItem(TextEntry_DisableAutoComplete,
427 _T("&Disable auto-completion"));
428 menuTextEntry->AppendRadioItem(TextEntry_AutoCompleteFixed,
429 _T("Fixed-&list auto-completion"));
430 menuTextEntry->AppendRadioItem(TextEntry_AutoCompleteFilenames,
431 _T("&Files names auto-completion"));
432 menuTextEntry->AppendSeparator();
433 menuTextEntry->Append(TextEntry_SetHint, "Set help &hint");
434
435 mbar->Append(menuTextEntry, _T("&Text"));
436
437 SetMenuBar(mbar);
438
439 mbar->Check(Widgets_Enable, true);
440 #endif // wxUSE_MENUS
441
442 // create controls
443 m_panel = new wxPanel(this, wxID_ANY);
444
445 wxSizer *sizerTop = new wxBoxSizer(wxVERTICAL);
446
447 // we have 2 panes: book with pages demonstrating the controls in the
448 // upper one and the log window with some buttons in the lower
449
450 int style = wxBK_DEFAULT;
451 // Uncomment to suppress page theme (draw in solid colour)
452 //style |= wxNB_NOPAGETHEME;
453
454 m_book = new WidgetsBookCtrl(m_panel, Widgets_BookCtrl,
455 wxDefaultPosition, wxDefaultSize,
456 style, "Widgets");
457
458 InitBook();
459
460 #ifndef __WXHANDHELD__
461 // the lower one only has the log listbox and a button to clear it
462 #if USE_LOG
463 wxSizer *sizerDown = new wxStaticBoxSizer(
464 new wxStaticBox( m_panel, wxID_ANY, _T("&Log window") ),
465 wxVERTICAL);
466
467 m_lboxLog = new wxListBox(m_panel, wxID_ANY);
468 sizerDown->Add(m_lboxLog, 1, wxGROW | wxALL, 5);
469 sizerDown->SetMinSize(100, 150);
470 #else
471 wxSizer *sizerDown = new wxBoxSizer(wxVERTICAL);
472 #endif // USE_LOG
473
474 wxBoxSizer *sizerBtns = new wxBoxSizer(wxHORIZONTAL);
475 wxButton *btn;
476 #if USE_LOG
477 btn = new wxButton(m_panel, Widgets_ClearLog, _T("Clear &log"));
478 sizerBtns->Add(btn);
479 sizerBtns->Add(10, 0); // spacer
480 #endif // USE_LOG
481 btn = new wxButton(m_panel, Widgets_Quit, _T("E&xit"));
482 sizerBtns->Add(btn);
483 sizerDown->Add(sizerBtns, 0, wxALL | wxALIGN_RIGHT, 5);
484
485 // put everything together
486 sizerTop->Add(m_book, 1, wxGROW | (wxALL & ~(wxTOP | wxBOTTOM)), 10);
487 sizerTop->Add(0, 5, 0, wxGROW); // spacer in between
488 sizerTop->Add(sizerDown, 0, wxGROW | (wxALL & ~wxTOP), 10);
489
490 #else // !__WXHANDHELD__/__WXHANDHELD__
491
492 sizerTop->Add(m_book, 1, wxGROW | wxALL );
493
494 #endif // __WXHANDHELD__
495
496 m_panel->SetSizer(sizerTop);
497
498 const wxSize sizeMin = m_panel->GetBestSize();
499 if ( !sizeSet )
500 SetClientSize(sizeMin);
501 SetMinClientSize(sizeMin);
502
503 #if USE_LOG && !defined(__WXCOCOA__)
504 // wxCocoa's listbox is too flakey to use for logging right now
505 // now that everything is created we can redirect the log messages to the
506 // listbox
507 m_logTarget = new LboxLogger(m_lboxLog, wxLog::GetActiveTarget());
508 wxLog::SetActiveTarget(m_logTarget);
509 #endif
510 }
511
512 void WidgetsFrame::InitBook()
513 {
514 #if USE_ICONS_IN_BOOK
515 wxImageList *imageList = new wxImageList(ICON_SIZE, ICON_SIZE);
516
517 wxImage img(sample_xpm);
518 imageList->Add(wxBitmap(img.Scale(ICON_SIZE, ICON_SIZE)));
519 #else
520 wxImageList *imageList = NULL;
521 #endif
522
523 #if !USE_TREEBOOK
524 WidgetsBookCtrl *books[MAX_PAGES];
525 #endif
526
527 ArrayWidgetsPage pages[MAX_PAGES];
528 wxArrayString labels[MAX_PAGES];
529
530 wxMenu *menuPages = new wxMenu;
531 unsigned int nPage = 0, nFKey = 0;
532 int cat, imageId = 1;
533
534 // we need to first create all pages and only then add them to the book
535 // as we need the image list first
536 //
537 // we also construct the pages menu during this first iteration
538 for ( cat = 0; cat < MAX_PAGES; cat++ )
539 {
540 #if USE_TREEBOOK
541 nPage++; // increase for parent page
542 #else
543 books[cat] = new WidgetsBookCtrl(m_book,
544 wxID_ANY,
545 wxDefaultPosition,
546 wxDefaultSize,
547 wxBK_DEFAULT);
548 #endif
549
550 for ( WidgetsPageInfo *info = WidgetsPage::ms_widgetPages;
551 info;
552 info = info->GetNext() )
553 {
554 if( (info->GetCategories() & ( 1 << cat )) == 0)
555 continue;
556
557 WidgetsPage *page = (*info->GetCtor())(
558 #if USE_TREEBOOK
559 m_book
560 #else
561 books[cat]
562 #endif
563 , imageList);
564 pages[cat].Add(page);
565
566 labels[cat].Add(info->GetLabel());
567 if ( cat == ALL_PAGE )
568 {
569 wxString radioLabel(info->GetLabel());
570 nFKey++;
571 if ( nFKey <= 12 )
572 {
573 radioLabel << wxT("\tF" ) << nFKey;
574 }
575
576 menuPages->AppendRadioItem(
577 Widgets_GoToPage + nPage,
578 radioLabel
579 );
580 #if !USE_TREEBOOK
581 // consider only for book in book architecture
582 nPage++;
583 #endif
584 }
585
586 #if USE_TREEBOOK
587 // consider only for treebook architecture (with subpages)
588 nPage++;
589 #endif
590 }
591 }
592
593 GetMenuBar()->Append(menuPages, _T("&Page"));
594
595 #if USE_ICONS_IN_BOOK
596 m_book->AssignImageList(imageList);
597 #endif
598
599 for ( cat = 0; cat < MAX_PAGES; cat++ )
600 {
601 #if USE_TREEBOOK
602 m_book->AddPage(NULL,WidgetsCategories[cat],false,0);
603 #else
604 m_book->AddPage(books[cat],WidgetsCategories[cat],false,0);
605 #if USE_ICONS_IN_BOOK
606 books[cat]->SetImageList(imageList);
607 #endif
608 #endif
609
610 // now do add them
611 size_t count = pages[cat].GetCount();
612 for ( size_t n = 0; n < count; n++ )
613 {
614 #if USE_TREEBOOK
615 m_book->AddSubPage
616 #else
617 books[cat]->AddPage
618 #endif
619 (
620 pages[cat][n],
621 labels[cat][n],
622 false, // don't select
623 imageId++
624 );
625 }
626 }
627
628 Connect( wxID_ANY,
629 wxEVT_COMMAND_WIDGETS_PAGE_CHANGED,
630 wxWidgetsbookEventHandler(WidgetsFrame::OnPageChanged) );
631
632 const bool pageSet = wxPersistentRegisterAndRestore(m_book);
633
634 #if USE_TREEBOOK
635 // for treebook page #0 is empty parent page only so select the first page
636 // with some contents
637 if ( !pageSet )
638 m_book->SetSelection(1);
639
640 // but ensure that the top of the tree is shown nevertheless
641 wxTreeCtrl * const tree = m_book->GetTreeCtrl();
642
643 wxTreeItemIdValue cookie;
644 tree->EnsureVisible(tree->GetFirstChild(tree->GetRootItem(), cookie));
645 #else
646 if ( !pageSet )
647 {
648 // for other books set selection twice to force connected event handler
649 // to force lazy creation of initial visible content
650 m_book->SetSelection(1);
651 m_book->SetSelection(0);
652 }
653 #endif // USE_TREEBOOK
654 }
655
656 WidgetsPage *WidgetsFrame::CurrentPage()
657 {
658 wxWindow *page = m_book->GetCurrentPage();
659
660 #if !USE_TREEBOOK
661 WidgetsBookCtrl *subBook = wxStaticCast(page, WidgetsBookCtrl);
662 wxCHECK_MSG( subBook, NULL, _T("no WidgetsBookCtrl?") );
663
664 page = subBook->GetCurrentPage();
665 #endif // !USE_TREEBOOK
666
667 return wxStaticCast(page, WidgetsPage);
668 }
669
670 WidgetsFrame::~WidgetsFrame()
671 {
672 #if USE_LOG
673 delete m_logTarget;
674 #endif // USE_LOG
675 }
676
677 // ----------------------------------------------------------------------------
678 // WidgetsFrame event handlers
679 // ----------------------------------------------------------------------------
680
681 void WidgetsFrame::OnExit(wxCommandEvent& WXUNUSED(event))
682 {
683 Close();
684 }
685
686 #if USE_LOG
687 void WidgetsFrame::OnButtonClearLog(wxCommandEvent& WXUNUSED(event))
688 {
689 m_lboxLog->Clear();
690 }
691 #endif // USE_LOG
692
693 #if wxUSE_MENUS
694
695 void WidgetsFrame::OnPageChanging(WidgetsBookCtrlEvent& event)
696 {
697 #if USE_TREEBOOK
698 // don't allow selection of entries without pages (categories)
699 if ( !m_book->GetPage(event.GetSelection()) )
700 event.Veto();
701 #else
702 wxUnusedVar(event);
703 #endif
704 }
705
706 void WidgetsFrame::OnPageChanged(WidgetsBookCtrlEvent& event)
707 {
708 const int sel = event.GetSelection();
709
710 // adjust "Page" menu selection
711 wxMenuItem *item = GetMenuBar()->FindItem(Widgets_GoToPage + sel);
712 if ( item )
713 item->Check();
714
715 GetMenuBar()->Check(Widgets_BusyCursor, false);
716
717 // create the pages on demand, otherwise the sample startup is too slow as
718 // it creates hundreds of controls
719 WidgetsPage *page = CurrentPage();
720 if ( page->GetChildren().empty() )
721 {
722 wxWindowUpdateLocker noUpdates(page);
723 page->CreateContent();
724 //page->Layout();
725 page->GetSizer()->Fit(page);
726
727 WidgetsBookCtrl *book = wxStaticCast(page->GetParent(), WidgetsBookCtrl);
728 wxSize size;
729 for ( size_t i = 0; i < book->GetPageCount(); ++i )
730 {
731 wxWindow *page = book->GetPage(i);
732 if ( page )
733 {
734 size.IncTo(page->GetSize());
735 }
736 }
737 page->SetSize(size);
738 }
739
740 event.Skip();
741 }
742
743 void WidgetsFrame::OnGoToPage(wxCommandEvent& event)
744 {
745 #if USE_TREEBOOK
746 m_book->SetSelection(event.GetId() - Widgets_GoToPage);
747 #else
748 m_book->SetSelection(m_book->GetPageCount()-1);
749 WidgetsBookCtrl *book = wxStaticCast(m_book->GetCurrentPage(), WidgetsBookCtrl);
750 book->SetSelection(event.GetId() - Widgets_GoToPage);
751 #endif
752 }
753
754 #if wxUSE_TOOLTIPS
755
756 void WidgetsFrame::OnSetTooltip(wxCommandEvent& WXUNUSED(event))
757 {
758 static wxString s_tip = _T("This is a tooltip");
759
760 wxTextEntryDialog dialog
761 (
762 this,
763 _T("Tooltip text (may use \\n, leave empty to remove): "),
764 _T("Widgets sample"),
765 s_tip
766 );
767
768 if ( dialog.ShowModal() != wxID_OK )
769 return;
770
771 s_tip = dialog.GetValue();
772 s_tip.Replace(_T("\\n"), _T("\n"));
773
774 WidgetsPage *page = CurrentPage();
775
776 page->GetWidget()->SetToolTip(s_tip);
777
778 wxControl *ctrl2 = page->GetWidget2();
779 if ( ctrl2 )
780 ctrl2->SetToolTip(s_tip);
781 }
782
783 #endif // wxUSE_TOOLTIPS
784
785 void WidgetsFrame::OnSetFgCol(wxCommandEvent& WXUNUSED(event))
786 {
787 #if wxUSE_COLOURDLG
788 // allow for debugging the default colour the first time this is called
789 WidgetsPage *page = CurrentPage();
790
791 if (!m_colFg.Ok())
792 m_colFg = page->GetForegroundColour();
793
794 wxColour col = wxGetColourFromUser(this, m_colFg);
795 if ( !col.Ok() )
796 return;
797
798 m_colFg = col;
799
800 page->GetWidget()->SetForegroundColour(m_colFg);
801 page->GetWidget()->Refresh();
802
803 wxControl *ctrl2 = page->GetWidget2();
804 if ( ctrl2 )
805 {
806 ctrl2->SetForegroundColour(m_colFg);
807 ctrl2->Refresh();
808 }
809 #else
810 wxLogMessage(_T("Colour selection dialog not available in current build."));
811 #endif
812 }
813
814 void WidgetsFrame::OnSetBgCol(wxCommandEvent& WXUNUSED(event))
815 {
816 #if wxUSE_COLOURDLG
817 WidgetsPage *page = CurrentPage();
818
819 if ( !m_colBg.Ok() )
820 m_colBg = page->GetBackgroundColour();
821
822 wxColour col = wxGetColourFromUser(this, m_colBg);
823 if ( !col.Ok() )
824 return;
825
826 m_colBg = col;
827
828 page->GetWidget()->SetBackgroundColour(m_colBg);
829 page->GetWidget()->Refresh();
830
831 wxControl *ctrl2 = page->GetWidget2();
832 if ( ctrl2 )
833 {
834 ctrl2->SetBackgroundColour(m_colFg);
835 ctrl2->Refresh();
836 }
837 #else
838 wxLogMessage(_T("Colour selection dialog not available in current build."));
839 #endif
840 }
841
842 void WidgetsFrame::OnSetFont(wxCommandEvent& WXUNUSED(event))
843 {
844 #if wxUSE_FONTDLG
845 WidgetsPage *page = CurrentPage();
846
847 if (!m_font.Ok())
848 m_font = page->GetFont();
849
850 wxFont font = wxGetFontFromUser(this, m_font);
851 if ( !font.Ok() )
852 return;
853
854 m_font = font;
855
856 page->GetWidget()->SetFont(m_font);
857 page->GetWidget()->Refresh();
858
859 wxControl *ctrl2 = page->GetWidget2();
860 if ( ctrl2 )
861 {
862 ctrl2->SetFont(m_font);
863 ctrl2->Refresh();
864 }
865 #else
866 wxLogMessage(_T("Font selection dialog not available in current build."));
867 #endif
868 }
869
870 void WidgetsFrame::OnEnable(wxCommandEvent& event)
871 {
872 CurrentPage()->GetWidget()->Enable(event.IsChecked());
873 if (CurrentPage()->GetWidget2())
874 CurrentPage()->GetWidget2()->Enable(event.IsChecked());
875 }
876
877 void WidgetsFrame::OnSetBorder(wxCommandEvent& event)
878 {
879 int border;
880 switch ( event.GetId() )
881 {
882 case Widgets_BorderNone: border = wxBORDER_NONE; break;
883 case Widgets_BorderStatic: border = wxBORDER_STATIC; break;
884 case Widgets_BorderSimple: border = wxBORDER_SIMPLE; break;
885 case Widgets_BorderRaised: border = wxBORDER_RAISED; break;
886 case Widgets_BorderSunken: border = wxBORDER_SUNKEN; break;
887 case Widgets_BorderDouble: border = wxBORDER_DOUBLE; break;
888
889 default:
890 wxFAIL_MSG( _T("unknown border style") );
891 // fall through
892
893 case Widgets_BorderDefault: border = wxBORDER_DEFAULT; break;
894 }
895
896 WidgetsPage::ms_defaultFlags &= ~wxBORDER_MASK;
897 WidgetsPage::ms_defaultFlags |= border;
898
899 WidgetsPage *page = CurrentPage();
900
901 page->RecreateWidget();
902 }
903
904 void WidgetsFrame::OnToggleGlobalBusyCursor(wxCommandEvent& event)
905 {
906 if ( event.IsChecked() )
907 wxBeginBusyCursor();
908 else
909 wxEndBusyCursor();
910 }
911
912 void WidgetsFrame::OnToggleBusyCursor(wxCommandEvent& event)
913 {
914 CurrentPage()->GetWidget()->SetCursor(*(event.IsChecked()
915 ? wxHOURGLASS_CURSOR
916 : wxSTANDARD_CURSOR));
917 }
918
919 void WidgetsFrame::OnDisableAutoComplete(wxCommandEvent& WXUNUSED(event))
920 {
921 wxTextEntryBase *entry = CurrentPage()->GetTextEntry();
922 wxCHECK_RET( entry, "menu item should be disabled" );
923
924 if ( entry->AutoComplete(wxArrayString()) )
925 wxLogMessage("Disabled auto completion.");
926 else
927 wxLogMessage("AutoComplete() failed.");
928 }
929
930 void WidgetsFrame::OnAutoCompleteFixed(wxCommandEvent& WXUNUSED(event))
931 {
932 wxTextEntryBase *entry = CurrentPage()->GetTextEntry();
933 wxCHECK_RET( entry, "menu item should be disabled" );
934
935 wxArrayString completion_choices;
936
937 // add a few strings so a completion occurs on any letter typed
938 for ( char idxc = 'a'; idxc < 'z'; ++idxc )
939 completion_choices.push_back(wxString::Format("%c%c", idxc, idxc));
940
941 completion_choices.push_back("is this string for test?");
942 completion_choices.push_back("this is a test string");
943 completion_choices.push_back("this is another test string");
944 completion_choices.push_back("this string is for test");
945
946 if ( entry->AutoComplete(completion_choices) )
947 wxLogMessage("Enabled auto completion of a set of fixed strings.");
948 else
949 wxLogMessage("AutoComplete() failed.");
950 }
951
952 void WidgetsFrame::OnAutoCompleteFilenames(wxCommandEvent& WXUNUSED(event))
953 {
954 wxTextEntryBase *entry = CurrentPage()->GetTextEntry();
955 wxCHECK_RET( entry, "menu item should be disabled" );
956
957 if ( entry->AutoCompleteFileNames() )
958 wxLogMessage("Enable auto completion of file names.");
959 else
960 wxLogMessage("AutoCompleteFileNames() failed.");
961 }
962
963 void WidgetsFrame::OnSetHint(wxCommandEvent& WXUNUSED(event))
964 {
965 wxTextEntryBase *entry = CurrentPage()->GetTextEntry();
966 wxCHECK_RET( entry, "menu item should be disabled" );
967
968 static wxString s_hint("Type here");
969 wxString
970 hint = wxGetTextFromUser("Text hint:", "Widgets sample", s_hint, this);
971 if ( hint.empty() )
972 return;
973
974 s_hint = hint;
975
976 if ( entry->SetHint(hint) )
977 wxLogMessage("Set hint to \"%s\".", hint);
978 else
979 wxLogMessage("Text hints not supported.");
980 }
981
982 #endif // wxUSE_MENUS
983
984 // ----------------------------------------------------------------------------
985 // WidgetsPageInfo
986 // ----------------------------------------------------------------------------
987
988 WidgetsPageInfo::WidgetsPageInfo(Constructor ctor, const wxChar *label, int categories)
989 : m_label(label)
990 , m_categories(categories)
991 {
992 m_ctor = ctor;
993
994 m_next = NULL;
995
996 // dummy sorting: add and immediately sort in the list according to label
997 if ( WidgetsPage::ms_widgetPages )
998 {
999 WidgetsPageInfo *node_prev = WidgetsPage::ms_widgetPages;
1000 if ( wxStrcmp(label, node_prev->GetLabel().c_str()) < 0 )
1001 {
1002 // add as first
1003 m_next = node_prev;
1004 WidgetsPage::ms_widgetPages = this;
1005 }
1006 else
1007 {
1008 WidgetsPageInfo *node_next;
1009 do
1010 {
1011 node_next = node_prev->GetNext();
1012 if ( node_next )
1013 {
1014 // add if between two
1015 if ( wxStrcmp(label, node_next->GetLabel().c_str()) < 0 )
1016 {
1017 node_prev->SetNext(this);
1018 m_next = node_next;
1019 // force to break loop
1020 node_next = NULL;
1021 }
1022 }
1023 else
1024 {
1025 // add as last
1026 node_prev->SetNext(this);
1027 m_next = node_next;
1028 }
1029 node_prev = node_next;
1030 }
1031 while ( node_next );
1032 }
1033 }
1034 else
1035 {
1036 // add when first
1037 WidgetsPage::ms_widgetPages = this;
1038 }
1039 }
1040
1041 // ----------------------------------------------------------------------------
1042 // WidgetsPage
1043 // ----------------------------------------------------------------------------
1044
1045 int WidgetsPage::ms_defaultFlags = wxBORDER_DEFAULT;
1046 WidgetsPageInfo *WidgetsPage::ms_widgetPages = NULL;
1047
1048 WidgetsPage::WidgetsPage(WidgetsBookCtrl *book,
1049 wxImageList *imaglist,
1050 const char *const icon[])
1051 : wxPanel(book, wxID_ANY,
1052 wxDefaultPosition, wxDefaultSize,
1053 wxNO_FULL_REPAINT_ON_RESIZE |
1054 wxCLIP_CHILDREN |
1055 wxTAB_TRAVERSAL)
1056 {
1057 #if USE_ICONS_IN_BOOK
1058 imaglist->Add(wxBitmap(wxImage(icon).Scale(ICON_SIZE, ICON_SIZE)));
1059 #else
1060 wxUnusedVar(imaglist);
1061 wxUnusedVar(icon);
1062 #endif
1063 }
1064
1065 wxSizer *WidgetsPage::CreateSizerWithText(wxControl *control,
1066 wxWindowID id,
1067 wxTextCtrl **ppText)
1068 {
1069 wxSizer *sizerRow = new wxBoxSizer(wxHORIZONTAL);
1070 wxTextCtrl *text = new wxTextCtrl(this, id, wxEmptyString,
1071 wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER);
1072
1073 sizerRow->Add(control, 0, wxRIGHT | wxALIGN_CENTRE_VERTICAL, 5);
1074 sizerRow->Add(text, 1, wxLEFT | wxALIGN_CENTRE_VERTICAL, 5);
1075
1076 if ( ppText )
1077 *ppText = text;
1078
1079 return sizerRow;
1080 }
1081
1082 // create a sizer containing a label and a text ctrl
1083 wxSizer *WidgetsPage::CreateSizerWithTextAndLabel(const wxString& label,
1084 wxWindowID id,
1085 wxTextCtrl **ppText)
1086 {
1087 return CreateSizerWithText(new wxStaticText(this, wxID_ANY, label),
1088 id, ppText);
1089 }
1090
1091 // create a sizer containing a button and a text ctrl
1092 wxSizer *WidgetsPage::CreateSizerWithTextAndButton(wxWindowID idBtn,
1093 const wxString& label,
1094 wxWindowID id,
1095 wxTextCtrl **ppText)
1096 {
1097 return CreateSizerWithText(new wxButton(this, idBtn, label), id, ppText);
1098 }
1099
1100 wxCheckBox *WidgetsPage::CreateCheckBoxAndAddToSizer(wxSizer *sizer,
1101 const wxString& label,
1102 wxWindowID id)
1103 {
1104 wxCheckBox *checkbox = new wxCheckBox(this, id, label);
1105 sizer->Add(checkbox, 0, wxLEFT | wxRIGHT, 5);
1106 sizer->Add(0, 2, 0, wxGROW); // spacer
1107
1108 return checkbox;
1109 }