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