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