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