]> git.saurik.com Git - wxWidgets.git/blob - samples/widgets/widgets.cpp
Use GetWidgets() in the widgets samples instead of GetWidget2().
[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 defined(__WXPALMOS5__)
341 title += wxT("wxPALMOS5");
342 #elif defined(__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 const Widgets widgets = page->GetWidgets();
759 for ( Widgets::const_iterator it = widgets.begin();
760 it != widgets.end();
761 ++it )
762 {
763 (*it)->SetToolTip(s_tip);
764 }
765 }
766
767 #endif // wxUSE_TOOLTIPS
768
769 void WidgetsFrame::OnSetFgCol(wxCommandEvent& WXUNUSED(event))
770 {
771 #if wxUSE_COLOURDLG
772 // allow for debugging the default colour the first time this is called
773 WidgetsPage *page = CurrentPage();
774
775 if (!m_colFg.Ok())
776 m_colFg = page->GetForegroundColour();
777
778 wxColour col = wxGetColourFromUser(this, m_colFg);
779 if ( !col.Ok() )
780 return;
781
782 m_colFg = col;
783
784 const Widgets widgets = page->GetWidgets();
785 for ( Widgets::const_iterator it = widgets.begin();
786 it != widgets.end();
787 ++it )
788 {
789 (*it)->SetForegroundColour(m_colFg);
790 (*it)->Refresh();
791 }
792 #else
793 wxLogMessage(wxT("Colour selection dialog not available in current build."));
794 #endif
795 }
796
797 void WidgetsFrame::OnSetBgCol(wxCommandEvent& WXUNUSED(event))
798 {
799 #if wxUSE_COLOURDLG
800 WidgetsPage *page = CurrentPage();
801
802 if ( !m_colBg.Ok() )
803 m_colBg = page->GetBackgroundColour();
804
805 wxColour col = wxGetColourFromUser(this, m_colBg);
806 if ( !col.Ok() )
807 return;
808
809 m_colBg = col;
810
811 const Widgets widgets = page->GetWidgets();
812 for ( Widgets::const_iterator it = widgets.begin();
813 it != widgets.end();
814 ++it )
815 {
816 (*it)->SetBackgroundColour(m_colBg);
817 (*it)->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 const Widgets widgets = page->GetWidgets();
839 for ( Widgets::const_iterator it = widgets.begin();
840 it != widgets.end();
841 ++it )
842 {
843 (*it)->SetFont(m_font);
844 (*it)->Refresh();
845 }
846 #else
847 wxLogMessage(wxT("Font selection dialog not available in current build."));
848 #endif
849 }
850
851 void WidgetsFrame::OnEnable(wxCommandEvent& event)
852 {
853 const Widgets widgets = CurrentPage()->GetWidgets();
854 for ( Widgets::const_iterator it = widgets.begin();
855 it != widgets.end();
856 ++it )
857 {
858 (*it)->Enable(event.IsChecked());
859 }
860 }
861
862 void WidgetsFrame::OnSetBorder(wxCommandEvent& event)
863 {
864 int border;
865 switch ( event.GetId() )
866 {
867 case Widgets_BorderNone: border = wxBORDER_NONE; break;
868 case Widgets_BorderStatic: border = wxBORDER_STATIC; break;
869 case Widgets_BorderSimple: border = wxBORDER_SIMPLE; break;
870 case Widgets_BorderRaised: border = wxBORDER_RAISED; break;
871 case Widgets_BorderSunken: border = wxBORDER_SUNKEN; break;
872 case Widgets_BorderDouble: border = wxBORDER_DOUBLE; break;
873
874 default:
875 wxFAIL_MSG( wxT("unknown border style") );
876 // fall through
877
878 case Widgets_BorderDefault: border = wxBORDER_DEFAULT; break;
879 }
880
881 WidgetsPage::ms_defaultFlags &= ~wxBORDER_MASK;
882 WidgetsPage::ms_defaultFlags |= border;
883
884 WidgetsPage *page = CurrentPage();
885
886 page->RecreateWidget();
887 }
888
889 void WidgetsFrame::OnToggleGlobalBusyCursor(wxCommandEvent& event)
890 {
891 if ( event.IsChecked() )
892 wxBeginBusyCursor();
893 else
894 wxEndBusyCursor();
895 }
896
897 void WidgetsFrame::OnToggleBusyCursor(wxCommandEvent& event)
898 {
899 wxCursor cursor(*(event.IsChecked() ? wxHOURGLASS_CURSOR
900 : wxSTANDARD_CURSOR));
901
902 const Widgets widgets = CurrentPage()->GetWidgets();
903 for ( Widgets::const_iterator it = widgets.begin();
904 it != widgets.end();
905 ++it )
906 {
907 (*it)->SetCursor(cursor);
908 }
909 }
910
911 void WidgetsFrame::OnDisableAutoComplete(wxCommandEvent& WXUNUSED(event))
912 {
913 wxTextEntryBase *entry = CurrentPage()->GetTextEntry();
914 wxCHECK_RET( entry, "menu item should be disabled" );
915
916 if ( entry->AutoComplete(wxArrayString()) )
917 {
918 wxLogMessage("Disabled auto completion.");
919 }
920 else
921 {
922 wxLogMessage("AutoComplete() failed.");
923 }
924 }
925
926 void WidgetsFrame::OnAutoCompleteFixed(wxCommandEvent& WXUNUSED(event))
927 {
928 wxTextEntryBase *entry = CurrentPage()->GetTextEntry();
929 wxCHECK_RET( entry, "menu item should be disabled" );
930
931 wxArrayString completion_choices;
932
933 // add a few strings so a completion occurs on any letter typed
934 for ( char idxc = 'a'; idxc < 'z'; ++idxc )
935 completion_choices.push_back(wxString::Format("%c%c", idxc, idxc));
936
937 completion_choices.push_back("is this string for test?");
938 completion_choices.push_back("this is a test string");
939 completion_choices.push_back("this is another test string");
940 completion_choices.push_back("this string is for test");
941
942 if ( entry->AutoComplete(completion_choices) )
943 {
944 wxLogMessage("Enabled auto completion of a set of fixed strings.");
945 }
946 else
947 {
948 wxLogMessage("AutoComplete() failed.");
949 }
950 }
951
952 void WidgetsFrame::OnAutoCompleteFilenames(wxCommandEvent& WXUNUSED(event))
953 {
954 wxTextEntryBase *entry = CurrentPage()->GetTextEntry();
955 wxCHECK_RET( entry, "menu item should be disabled" );
956
957 if ( entry->AutoCompleteFileNames() )
958 {
959 wxLogMessage("Enable auto completion of file names.");
960 }
961 else
962 {
963 wxLogMessage("AutoCompleteFileNames() failed.");
964 }
965 }
966
967 void WidgetsFrame::OnSetHint(wxCommandEvent& WXUNUSED(event))
968 {
969 wxTextEntryBase *entry = CurrentPage()->GetTextEntry();
970 wxCHECK_RET( entry, "menu item should be disabled" );
971
972 static wxString s_hint("Type here");
973 wxString
974 hint = wxGetTextFromUser("Text hint:", "Widgets sample", s_hint, this);
975 if ( hint.empty() )
976 return;
977
978 s_hint = hint;
979
980 if ( entry->SetHint(hint) )
981 {
982 wxLogMessage("Set hint to \"%s\".", hint);
983 }
984 else
985 {
986 wxLogMessage("Text hints not supported.");
987 }
988 }
989
990 #endif // wxUSE_MENUS
991
992 // ----------------------------------------------------------------------------
993 // WidgetsPageInfo
994 // ----------------------------------------------------------------------------
995
996 WidgetsPageInfo::WidgetsPageInfo(Constructor ctor, const wxChar *label, int categories)
997 : m_label(label)
998 , m_categories(categories)
999 {
1000 m_ctor = ctor;
1001
1002 m_next = NULL;
1003
1004 // dummy sorting: add and immediately sort in the list according to label
1005 if ( WidgetsPage::ms_widgetPages )
1006 {
1007 WidgetsPageInfo *node_prev = WidgetsPage::ms_widgetPages;
1008 if ( wxStrcmp(label, node_prev->GetLabel().c_str()) < 0 )
1009 {
1010 // add as first
1011 m_next = node_prev;
1012 WidgetsPage::ms_widgetPages = this;
1013 }
1014 else
1015 {
1016 WidgetsPageInfo *node_next;
1017 do
1018 {
1019 node_next = node_prev->GetNext();
1020 if ( node_next )
1021 {
1022 // add if between two
1023 if ( wxStrcmp(label, node_next->GetLabel().c_str()) < 0 )
1024 {
1025 node_prev->SetNext(this);
1026 m_next = node_next;
1027 // force to break loop
1028 node_next = NULL;
1029 }
1030 }
1031 else
1032 {
1033 // add as last
1034 node_prev->SetNext(this);
1035 m_next = node_next;
1036 }
1037 node_prev = node_next;
1038 }
1039 while ( node_next );
1040 }
1041 }
1042 else
1043 {
1044 // add when first
1045 WidgetsPage::ms_widgetPages = this;
1046 }
1047 }
1048
1049 // ----------------------------------------------------------------------------
1050 // WidgetsPage
1051 // ----------------------------------------------------------------------------
1052
1053 int WidgetsPage::ms_defaultFlags = wxBORDER_DEFAULT;
1054 WidgetsPageInfo *WidgetsPage::ms_widgetPages = NULL;
1055
1056 WidgetsPage::WidgetsPage(WidgetsBookCtrl *book,
1057 wxImageList *imaglist,
1058 const char *const icon[])
1059 : wxPanel(book, wxID_ANY,
1060 wxDefaultPosition, wxDefaultSize,
1061 wxNO_FULL_REPAINT_ON_RESIZE |
1062 wxCLIP_CHILDREN |
1063 wxTAB_TRAVERSAL)
1064 {
1065 #if USE_ICONS_IN_BOOK
1066 imaglist->Add(wxBitmap(wxImage(icon).Scale(ICON_SIZE, ICON_SIZE)));
1067 #else
1068 wxUnusedVar(imaglist);
1069 wxUnusedVar(icon);
1070 #endif
1071 }
1072
1073 wxSizer *WidgetsPage::CreateSizerWithText(wxControl *control,
1074 wxWindowID id,
1075 wxTextCtrl **ppText)
1076 {
1077 wxSizer *sizerRow = new wxBoxSizer(wxHORIZONTAL);
1078 wxTextCtrl *text = new wxTextCtrl(this, id, wxEmptyString,
1079 wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER);
1080
1081 sizerRow->Add(control, 0, wxRIGHT | wxALIGN_CENTRE_VERTICAL, 5);
1082 sizerRow->Add(text, 1, wxLEFT | wxALIGN_CENTRE_VERTICAL, 5);
1083
1084 if ( ppText )
1085 *ppText = text;
1086
1087 return sizerRow;
1088 }
1089
1090 // create a sizer containing a label and a text ctrl
1091 wxSizer *WidgetsPage::CreateSizerWithTextAndLabel(const wxString& label,
1092 wxWindowID id,
1093 wxTextCtrl **ppText)
1094 {
1095 return CreateSizerWithText(new wxStaticText(this, wxID_ANY, label),
1096 id, ppText);
1097 }
1098
1099 // create a sizer containing a button and a text ctrl
1100 wxSizer *WidgetsPage::CreateSizerWithTextAndButton(wxWindowID idBtn,
1101 const wxString& label,
1102 wxWindowID id,
1103 wxTextCtrl **ppText)
1104 {
1105 return CreateSizerWithText(new wxButton(this, idBtn, label), id, ppText);
1106 }
1107
1108 wxCheckBox *WidgetsPage::CreateCheckBoxAndAddToSizer(wxSizer *sizer,
1109 const wxString& label,
1110 wxWindowID id)
1111 {
1112 wxCheckBox *checkbox = new wxCheckBox(this, id, label);
1113 sizer->Add(checkbox, 0, wxLEFT | wxRIGHT, 5);
1114 sizer->Add(0, 2, 0, wxGROW); // spacer
1115
1116 return checkbox;
1117 }