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