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