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