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