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
8 // Copyright: (c) 2001 Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // for compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
27 // for all others, include the necessary headers
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"
44 #include "wx/sysopt.h"
45 #include "wx/bookctrl.h"
46 #include "wx/treebook.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"
55 #include "wx/persist/toplevel.h"
56 #include "wx/persist/treebook.h"
60 #include "../sample.xpm"
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
69 Widgets_ClearLog
= 100,
76 #endif // wxUSE_TOOLTIPS
89 Widgets_BorderDefault
,
91 Widgets_GlobalBusyCursor
,
95 Widgets_GoToPageLast
= Widgets_GoToPage
+ 100,
99 TextEntry_DisableAutoComplete
= TextEntry_Begin
,
100 TextEntry_AutoCompleteFixed
,
101 TextEntry_AutoCompleteFilenames
,
102 TextEntry_AutoCompleteCustom
,
108 const wxChar
*WidgetsCategories
[MAX_PAGES
] = {
109 #if defined(__WXUNIVERSAL__)
123 // ----------------------------------------------------------------------------
125 // ----------------------------------------------------------------------------
127 // Define a new application type, each program should derive a class from wxApp
128 class WidgetsApp
: public wxApp
131 // override base class virtuals
132 // ----------------------------
134 // this one is called on application startup and is a good place for the app
135 // initialization (doing it here and not in the ctor allows to have an error
136 // return: if OnInit() returns false, the application terminates)
137 virtual bool OnInit();
140 // Define a new frame type: this is going to be our main frame
141 class WidgetsFrame
: public wxFrame
145 WidgetsFrame(const wxString
& title
);
146 virtual ~WidgetsFrame();
151 void OnButtonClearLog(wxCommandEvent
& event
);
153 void OnExit(wxCommandEvent
& event
);
156 void OnPageChanging(WidgetsBookCtrlEvent
& event
);
157 void OnPageChanged(WidgetsBookCtrlEvent
& event
);
158 void OnGoToPage(wxCommandEvent
& event
);
161 void OnSetTooltip(wxCommandEvent
& event
);
162 #endif // wxUSE_TOOLTIPS
163 void OnSetFgCol(wxCommandEvent
& event
);
164 void OnSetBgCol(wxCommandEvent
& event
);
165 void OnSetPageBg(wxCommandEvent
& event
);
166 void OnSetFont(wxCommandEvent
& event
);
167 void OnEnable(wxCommandEvent
& event
);
168 void OnSetBorder(wxCommandEvent
& event
);
170 void OnToggleGlobalBusyCursor(wxCommandEvent
& event
);
171 void OnToggleBusyCursor(wxCommandEvent
& event
);
173 // wxTextEntry-specific tests
174 void OnDisableAutoComplete(wxCommandEvent
& event
);
175 void OnAutoCompleteFixed(wxCommandEvent
& event
);
176 void OnAutoCompleteFilenames(wxCommandEvent
& event
);
177 void OnAutoCompleteCustom(wxCommandEvent
& event
);
179 void OnSetHint(wxCommandEvent
& event
);
181 void OnUpdateTextUI(wxUpdateUIEvent
& event
)
183 event
.Enable( CurrentPage()->GetTextEntry() != NULL
);
185 #endif // wxUSE_MENUS
187 // initialize the book: add all pages to it
190 // return the currently selected page (never NULL)
191 WidgetsPage
*CurrentPage();
194 // the panel containing everything
198 // the listbox for logging messages
199 wxListBox
*m_lboxLog
;
201 // the log target we use to redirect messages to the listbox
205 // the book containing the test pages
206 WidgetsBookCtrl
*m_book
;
209 // last chosen fg/bg colours and font
213 #endif // wxUSE_MENUS
215 // any class wishing to process wxWidgets events must use this macro
216 DECLARE_EVENT_TABLE()
220 // A log target which just redirects the messages to a listbox
221 class LboxLogger
: public wxLog
224 LboxLogger(wxListBox
*lbox
, wxLog
*logOld
)
227 //m_lbox->Disable(); -- looks ugly under MSW
231 virtual ~LboxLogger()
233 wxLog::SetActiveTarget(m_logOld
);
237 // implement sink functions
238 virtual void DoLogTextAtLevel(wxLogLevel level
, const wxString
& msg
)
240 if ( level
== wxLOG_Trace
)
243 m_logOld
->LogTextAtLevel(level
, msg
);
247 #ifdef __WXUNIVERSAL__
248 m_lbox
->AppendAndEnsureVisible(msg
);
249 #else // other ports don't have this method yet
251 m_lbox
->SetFirstItem(m_lbox
->GetCount() - 1);
255 // the control we use
258 // the old log target
264 WX_DEFINE_ARRAY_PTR(WidgetsPage
*, ArrayWidgetsPage
);
266 // ----------------------------------------------------------------------------
268 // ----------------------------------------------------------------------------
270 IMPLEMENT_APP(WidgetsApp
)
272 // ----------------------------------------------------------------------------
274 // ----------------------------------------------------------------------------
276 BEGIN_EVENT_TABLE(WidgetsFrame
, wxFrame
)
278 EVT_BUTTON(Widgets_ClearLog
, WidgetsFrame::OnButtonClearLog
)
280 EVT_BUTTON(Widgets_Quit
, WidgetsFrame::OnExit
)
283 EVT_MENU(Widgets_SetTooltip
, WidgetsFrame::OnSetTooltip
)
284 #endif // wxUSE_TOOLTIPS
287 EVT_WIDGETS_PAGE_CHANGING(wxID_ANY
, WidgetsFrame::OnPageChanging
)
288 EVT_MENU_RANGE(Widgets_GoToPage
, Widgets_GoToPageLast
,
289 WidgetsFrame::OnGoToPage
)
291 EVT_MENU(Widgets_SetFgColour
, WidgetsFrame::OnSetFgCol
)
292 EVT_MENU(Widgets_SetBgColour
, WidgetsFrame::OnSetBgCol
)
293 EVT_MENU(Widgets_SetPageBg
, WidgetsFrame::OnSetPageBg
)
294 EVT_MENU(Widgets_SetFont
, WidgetsFrame::OnSetFont
)
295 EVT_MENU(Widgets_Enable
, WidgetsFrame::OnEnable
)
297 EVT_MENU_RANGE(Widgets_BorderNone
, Widgets_BorderDefault
,
298 WidgetsFrame::OnSetBorder
)
300 EVT_MENU(Widgets_GlobalBusyCursor
, WidgetsFrame::OnToggleGlobalBusyCursor
)
301 EVT_MENU(Widgets_BusyCursor
, WidgetsFrame::OnToggleBusyCursor
)
303 EVT_MENU(TextEntry_DisableAutoComplete
, WidgetsFrame::OnDisableAutoComplete
)
304 EVT_MENU(TextEntry_AutoCompleteFixed
, WidgetsFrame::OnAutoCompleteFixed
)
305 EVT_MENU(TextEntry_AutoCompleteFilenames
, WidgetsFrame::OnAutoCompleteFilenames
)
306 EVT_MENU(TextEntry_AutoCompleteCustom
, WidgetsFrame::OnAutoCompleteCustom
)
308 EVT_MENU(TextEntry_SetHint
, WidgetsFrame::OnSetHint
)
310 EVT_UPDATE_UI_RANGE(TextEntry_Begin
, TextEntry_End
- 1,
311 WidgetsFrame::OnUpdateTextUI
)
313 EVT_MENU(wxID_EXIT
, WidgetsFrame::OnExit
)
314 #endif // wxUSE_MENUS
317 // ============================================================================
319 // ============================================================================
321 // ----------------------------------------------------------------------------
323 // ----------------------------------------------------------------------------
325 bool WidgetsApp::OnInit()
327 if ( !wxApp::OnInit() )
330 SetVendorName("wxWidgets_Samples");
332 // the reason for having these ifdef's is that I often run two copies of
333 // this sample side by side and it is useful to see which one is which
335 #if defined(__WXUNIVERSAL__)
336 title
= wxT("wxUniv/");
339 #if defined(__WXMSW__)
340 title
+= wxT("wxMSW");
341 #elif defined(__WXGTK__)
342 title
+= wxT("wxGTK");
343 #elif defined(__WXMAC__)
344 title
+= wxT("wxMAC");
345 #elif defined(__WXMOTIF__)
346 title
+= wxT("wxMOTIF");
347 #elif defined(__WXPALMOS5__)
348 title
+= wxT("wxPALMOS5");
349 #elif defined(__WXPALMOS6__)
350 title
+= wxT("wxPALMOS6");
352 title
+= wxT("wxWidgets");
355 wxFrame
*frame
= new WidgetsFrame(title
+ wxT(" widgets demo"));
361 // ----------------------------------------------------------------------------
362 // WidgetsFrame construction
363 // ----------------------------------------------------------------------------
365 WidgetsFrame::WidgetsFrame(const wxString
& title
)
366 : wxFrame(NULL
, wxID_ANY
, title
)
368 const bool sizeSet
= wxPersistentRegisterAndRestore(this, "Main");
370 // set the frame icon
371 SetIcon(wxICON(sample
));
381 // create the menubar
382 wxMenuBar
*mbar
= new wxMenuBar
;
383 wxMenu
*menuWidget
= new wxMenu
;
385 menuWidget
->Append(Widgets_SetTooltip
, wxT("Set &tooltip...\tCtrl-T"));
386 menuWidget
->AppendSeparator();
387 #endif // wxUSE_TOOLTIPS
388 menuWidget
->Append(Widgets_SetFgColour
, wxT("Set &foreground...\tCtrl-F"));
389 menuWidget
->Append(Widgets_SetBgColour
, wxT("Set &background...\tCtrl-B"));
390 menuWidget
->Append(Widgets_SetPageBg
, wxT("Set &page background...\tShift-Ctrl-B"));
391 menuWidget
->Append(Widgets_SetFont
, wxT("Set f&ont...\tCtrl-O"));
392 menuWidget
->AppendCheckItem(Widgets_Enable
, wxT("&Enable/disable\tCtrl-E"));
394 wxMenu
*menuBorders
= new wxMenu
;
395 menuBorders
->AppendRadioItem(Widgets_BorderDefault
, wxT("De&fault\tCtrl-Shift-9"));
396 menuBorders
->AppendRadioItem(Widgets_BorderNone
, wxT("&None\tCtrl-Shift-0"));
397 menuBorders
->AppendRadioItem(Widgets_BorderSimple
, wxT("&Simple\tCtrl-Shift-1"));
398 menuBorders
->AppendRadioItem(Widgets_BorderDouble
, wxT("&Double\tCtrl-Shift-2"));
399 menuBorders
->AppendRadioItem(Widgets_BorderStatic
, wxT("Stati&c\tCtrl-Shift-3"));
400 menuBorders
->AppendRadioItem(Widgets_BorderRaised
, wxT("&Raised\tCtrl-Shift-4"));
401 menuBorders
->AppendRadioItem(Widgets_BorderSunken
, wxT("S&unken\tCtrl-Shift-5"));
402 menuWidget
->AppendSubMenu(menuBorders
, wxT("Set &border"));
404 menuWidget
->AppendSeparator();
405 menuWidget
->AppendCheckItem(Widgets_GlobalBusyCursor
,
406 wxT("Toggle &global busy cursor\tCtrl-Shift-U"));
407 menuWidget
->AppendCheckItem(Widgets_BusyCursor
,
408 wxT("Toggle b&usy cursor\tCtrl-U"));
410 menuWidget
->AppendSeparator();
411 menuWidget
->Append(wxID_EXIT
, wxT("&Quit\tCtrl-Q"));
412 mbar
->Append(menuWidget
, wxT("&Widget"));
414 wxMenu
*menuTextEntry
= new wxMenu
;
415 menuTextEntry
->AppendRadioItem(TextEntry_DisableAutoComplete
,
416 wxT("&Disable auto-completion"));
417 menuTextEntry
->AppendRadioItem(TextEntry_AutoCompleteFixed
,
418 wxT("Fixed-&list auto-completion"));
419 menuTextEntry
->AppendRadioItem(TextEntry_AutoCompleteFilenames
,
420 wxT("&Files names auto-completion"));
421 menuTextEntry
->AppendRadioItem(TextEntry_AutoCompleteCustom
,
422 wxT("&Custom auto-completion"));
423 menuTextEntry
->AppendSeparator();
424 menuTextEntry
->Append(TextEntry_SetHint
, "Set help &hint");
426 mbar
->Append(menuTextEntry
, wxT("&Text"));
430 mbar
->Check(Widgets_Enable
, true);
431 #endif // wxUSE_MENUS
434 m_panel
= new wxPanel(this, wxID_ANY
);
436 wxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
438 // we have 2 panes: book with pages demonstrating the controls in the
439 // upper one and the log window with some buttons in the lower
441 int style
= wxBK_DEFAULT
;
442 // Uncomment to suppress page theme (draw in solid colour)
443 //style |= wxNB_NOPAGETHEME;
445 m_book
= new WidgetsBookCtrl(m_panel
, Widgets_BookCtrl
,
446 wxDefaultPosition
, wxDefaultSize
,
451 #ifndef __WXHANDHELD__
452 // the lower one only has the log listbox and a button to clear it
454 wxSizer
*sizerDown
= new wxStaticBoxSizer(
455 new wxStaticBox( m_panel
, wxID_ANY
, wxT("&Log window") ),
458 m_lboxLog
= new wxListBox(m_panel
, wxID_ANY
);
459 sizerDown
->Add(m_lboxLog
, 1, wxGROW
| wxALL
, 5);
460 sizerDown
->SetMinSize(100, 150);
462 wxSizer
*sizerDown
= new wxBoxSizer(wxVERTICAL
);
465 wxBoxSizer
*sizerBtns
= new wxBoxSizer(wxHORIZONTAL
);
468 btn
= new wxButton(m_panel
, Widgets_ClearLog
, wxT("Clear &log"));
470 sizerBtns
->Add(10, 0); // spacer
472 btn
= new wxButton(m_panel
, Widgets_Quit
, wxT("E&xit"));
474 sizerDown
->Add(sizerBtns
, 0, wxALL
| wxALIGN_RIGHT
, 5);
476 // put everything together
477 sizerTop
->Add(m_book
, 1, wxGROW
| (wxALL
& ~(wxTOP
| wxBOTTOM
)), 10);
478 sizerTop
->Add(0, 5, 0, wxGROW
); // spacer in between
479 sizerTop
->Add(sizerDown
, 0, wxGROW
| (wxALL
& ~wxTOP
), 10);
481 #else // !__WXHANDHELD__/__WXHANDHELD__
483 sizerTop
->Add(m_book
, 1, wxGROW
| wxALL
);
485 #endif // __WXHANDHELD__
487 m_panel
->SetSizer(sizerTop
);
489 const wxSize sizeMin
= m_panel
->GetBestSize();
491 SetClientSize(sizeMin
);
492 SetMinClientSize(sizeMin
);
494 #if USE_LOG && !defined(__WXCOCOA__)
495 // wxCocoa's listbox is too flakey to use for logging right now
496 // now that everything is created we can redirect the log messages to the
498 m_logTarget
= new LboxLogger(m_lboxLog
, wxLog::GetActiveTarget());
499 wxLog::SetActiveTarget(m_logTarget
);
503 void WidgetsFrame::InitBook()
505 #if USE_ICONS_IN_BOOK
506 wxImageList
*imageList
= new wxImageList(ICON_SIZE
, ICON_SIZE
);
508 wxImage
img(sample_xpm
);
509 imageList
->Add(wxBitmap(img
.Scale(ICON_SIZE
, ICON_SIZE
)));
511 wxImageList
*imageList
= NULL
;
515 WidgetsBookCtrl
*books
[MAX_PAGES
];
518 ArrayWidgetsPage pages
[MAX_PAGES
];
519 wxArrayString labels
[MAX_PAGES
];
521 wxMenu
*menuPages
= new wxMenu
;
522 unsigned int nPage
= 0, nFKey
= 0;
523 int cat
, imageId
= 1;
525 // we need to first create all pages and only then add them to the book
526 // as we need the image list first
528 // we also construct the pages menu during this first iteration
529 for ( cat
= 0; cat
< MAX_PAGES
; cat
++ )
532 nPage
++; // increase for parent page
534 books
[cat
] = new WidgetsBookCtrl(m_book
,
541 for ( WidgetsPageInfo
*info
= WidgetsPage::ms_widgetPages
;
543 info
= info
->GetNext() )
545 if( (info
->GetCategories() & ( 1 << cat
)) == 0)
548 WidgetsPage
*page
= (*info
->GetCtor())(
555 pages
[cat
].Add(page
);
557 labels
[cat
].Add(info
->GetLabel());
558 if ( cat
== ALL_PAGE
)
560 wxString
radioLabel(info
->GetLabel());
564 radioLabel
<< wxT("\tF" ) << nFKey
;
567 menuPages
->AppendRadioItem(
568 Widgets_GoToPage
+ nPage
,
572 // consider only for book in book architecture
578 // consider only for treebook architecture (with subpages)
584 GetMenuBar()->Append(menuPages
, wxT("&Page"));
586 #if USE_ICONS_IN_BOOK
587 m_book
->AssignImageList(imageList
);
590 for ( cat
= 0; cat
< MAX_PAGES
; cat
++ )
593 m_book
->AddPage(NULL
,WidgetsCategories
[cat
],false,0);
595 m_book
->AddPage(books
[cat
],WidgetsCategories
[cat
],false,0);
596 #if USE_ICONS_IN_BOOK
597 books
[cat
]->SetImageList(imageList
);
602 size_t count
= pages
[cat
].GetCount();
603 for ( size_t n
= 0; n
< count
; n
++ )
613 false, // don't select
620 wxEVT_COMMAND_WIDGETS_PAGE_CHANGED
,
621 wxWidgetsbookEventHandler(WidgetsFrame::OnPageChanged
) );
623 const bool pageSet
= wxPersistentRegisterAndRestore(m_book
);
626 // for treebook page #0 is empty parent page only so select the first page
627 // with some contents
629 m_book
->SetSelection(1);
631 // but ensure that the top of the tree is shown nevertheless
632 wxTreeCtrl
* const tree
= m_book
->GetTreeCtrl();
634 wxTreeItemIdValue cookie
;
635 tree
->EnsureVisible(tree
->GetFirstChild(tree
->GetRootItem(), cookie
));
639 // for other books set selection twice to force connected event handler
640 // to force lazy creation of initial visible content
641 m_book
->SetSelection(1);
642 m_book
->SetSelection(0);
644 #endif // USE_TREEBOOK
647 WidgetsPage
*WidgetsFrame::CurrentPage()
649 wxWindow
*page
= m_book
->GetCurrentPage();
652 WidgetsBookCtrl
*subBook
= wxStaticCast(page
, WidgetsBookCtrl
);
653 wxCHECK_MSG( subBook
, NULL
, wxT("no WidgetsBookCtrl?") );
655 page
= subBook
->GetCurrentPage();
656 #endif // !USE_TREEBOOK
658 return wxStaticCast(page
, WidgetsPage
);
661 WidgetsFrame::~WidgetsFrame()
668 // ----------------------------------------------------------------------------
669 // WidgetsFrame event handlers
670 // ----------------------------------------------------------------------------
672 void WidgetsFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
678 void WidgetsFrame::OnButtonClearLog(wxCommandEvent
& WXUNUSED(event
))
686 void WidgetsFrame::OnPageChanging(WidgetsBookCtrlEvent
& event
)
689 // don't allow selection of entries without pages (categories)
690 if ( !m_book
->GetPage(event
.GetSelection()) )
697 void WidgetsFrame::OnPageChanged(WidgetsBookCtrlEvent
& event
)
699 const int sel
= event
.GetSelection();
701 // adjust "Page" menu selection
702 wxMenuItem
*item
= GetMenuBar()->FindItem(Widgets_GoToPage
+ sel
);
706 GetMenuBar()->Check(Widgets_BusyCursor
, false);
708 // create the pages on demand, otherwise the sample startup is too slow as
709 // it creates hundreds of controls
710 WidgetsPage
*page
= CurrentPage();
711 if ( page
->GetChildren().empty() )
713 wxWindowUpdateLocker
noUpdates(page
);
714 page
->CreateContent();
716 page
->GetSizer()->Fit(page
);
718 WidgetsBookCtrl
*book
= wxStaticCast(page
->GetParent(), WidgetsBookCtrl
);
720 for ( size_t i
= 0; i
< book
->GetPageCount(); ++i
)
722 wxWindow
*page
= book
->GetPage(i
);
725 size
.IncTo(page
->GetSize());
734 void WidgetsFrame::OnGoToPage(wxCommandEvent
& event
)
737 m_book
->SetSelection(event
.GetId() - Widgets_GoToPage
);
739 m_book
->SetSelection(m_book
->GetPageCount()-1);
740 WidgetsBookCtrl
*book
= wxStaticCast(m_book
->GetCurrentPage(), WidgetsBookCtrl
);
741 book
->SetSelection(event
.GetId() - Widgets_GoToPage
);
747 void WidgetsFrame::OnSetTooltip(wxCommandEvent
& WXUNUSED(event
))
749 static wxString s_tip
= wxT("This is a tooltip");
751 wxTextEntryDialog dialog
754 wxT("Tooltip text (may use \\n, leave empty to remove): "),
755 wxT("Widgets sample"),
759 if ( dialog
.ShowModal() != wxID_OK
)
762 s_tip
= dialog
.GetValue();
763 s_tip
.Replace(wxT("\\n"), wxT("\n"));
765 WidgetsPage
*page
= CurrentPage();
767 const Widgets widgets
= page
->GetWidgets();
768 for ( Widgets::const_iterator it
= widgets
.begin();
772 (*it
)->SetToolTip(s_tip
);
776 #endif // wxUSE_TOOLTIPS
781 // Trivial wrapper for wxGetColourFromUser() which also does something even if
782 // the colour dialog is not available in the current build (which may happen
783 // for the ports in development and it is still useful to see how colours work)
784 wxColour
GetColourFromUser(wxWindow
*parent
, const wxColour
& colDefault
)
787 return wxGetColourFromUser(parent
, colDefault
);
788 #else // !wxUSE_COLOURDLG
789 if ( colDefault
== *wxBLACK
)
793 #endif // wxUSE_COLOURDLG/!wxUSE_COLOURDLG
796 } // anonymous namespace
798 void WidgetsFrame::OnSetFgCol(wxCommandEvent
& WXUNUSED(event
))
800 // allow for debugging the default colour the first time this is called
801 WidgetsPage
*page
= CurrentPage();
804 m_colFg
= page
->GetForegroundColour();
806 wxColour col
= GetColourFromUser(this, m_colFg
);
812 const Widgets widgets
= page
->GetWidgets();
813 for ( Widgets::const_iterator it
= widgets
.begin();
817 (*it
)->SetForegroundColour(m_colFg
);
822 void WidgetsFrame::OnSetBgCol(wxCommandEvent
& WXUNUSED(event
))
824 WidgetsPage
*page
= CurrentPage();
826 if ( !m_colBg
.IsOk() )
827 m_colBg
= page
->GetBackgroundColour();
829 wxColour col
= GetColourFromUser(this, m_colBg
);
835 const Widgets widgets
= page
->GetWidgets();
836 for ( Widgets::const_iterator it
= widgets
.begin();
840 (*it
)->SetBackgroundColour(m_colBg
);
845 void WidgetsFrame::OnSetPageBg(wxCommandEvent
& WXUNUSED(event
))
847 wxColour col
= GetColourFromUser(this, GetBackgroundColour());
851 CurrentPage()->SetBackgroundColour(col
);
852 CurrentPage()->Refresh();
855 void WidgetsFrame::OnSetFont(wxCommandEvent
& WXUNUSED(event
))
858 WidgetsPage
*page
= CurrentPage();
861 m_font
= page
->GetFont();
863 wxFont font
= wxGetFontFromUser(this, m_font
);
869 const Widgets widgets
= page
->GetWidgets();
870 for ( Widgets::const_iterator it
= widgets
.begin();
874 (*it
)->SetFont(m_font
);
878 wxLogMessage(wxT("Font selection dialog not available in current build."));
882 void WidgetsFrame::OnEnable(wxCommandEvent
& event
)
884 const Widgets widgets
= CurrentPage()->GetWidgets();
885 for ( Widgets::const_iterator it
= widgets
.begin();
889 (*it
)->Enable(event
.IsChecked());
893 void WidgetsFrame::OnSetBorder(wxCommandEvent
& event
)
896 switch ( event
.GetId() )
898 case Widgets_BorderNone
: border
= wxBORDER_NONE
; break;
899 case Widgets_BorderStatic
: border
= wxBORDER_STATIC
; break;
900 case Widgets_BorderSimple
: border
= wxBORDER_SIMPLE
; break;
901 case Widgets_BorderRaised
: border
= wxBORDER_RAISED
; break;
902 case Widgets_BorderSunken
: border
= wxBORDER_SUNKEN
; break;
903 case Widgets_BorderDouble
: border
= wxBORDER_DOUBLE
; break;
906 wxFAIL_MSG( wxT("unknown border style") );
909 case Widgets_BorderDefault
: border
= wxBORDER_DEFAULT
; break;
912 WidgetsPage::ms_defaultFlags
&= ~wxBORDER_MASK
;
913 WidgetsPage::ms_defaultFlags
|= border
;
915 WidgetsPage
*page
= CurrentPage();
917 page
->RecreateWidget();
920 void WidgetsFrame::OnToggleGlobalBusyCursor(wxCommandEvent
& event
)
922 if ( event
.IsChecked() )
928 void WidgetsFrame::OnToggleBusyCursor(wxCommandEvent
& event
)
930 wxCursor
cursor(*(event
.IsChecked() ? wxHOURGLASS_CURSOR
931 : wxSTANDARD_CURSOR
));
933 const Widgets widgets
= CurrentPage()->GetWidgets();
934 for ( Widgets::const_iterator it
= widgets
.begin();
938 (*it
)->SetCursor(cursor
);
942 void WidgetsFrame::OnDisableAutoComplete(wxCommandEvent
& WXUNUSED(event
))
944 wxTextEntryBase
*entry
= CurrentPage()->GetTextEntry();
945 wxCHECK_RET( entry
, "menu item should be disabled" );
947 if ( entry
->AutoComplete(wxArrayString()) )
949 wxLogMessage("Disabled auto completion.");
953 wxLogMessage("AutoComplete() failed.");
957 void WidgetsFrame::OnAutoCompleteFixed(wxCommandEvent
& WXUNUSED(event
))
959 wxTextEntryBase
*entry
= CurrentPage()->GetTextEntry();
960 wxCHECK_RET( entry
, "menu item should be disabled" );
962 wxArrayString completion_choices
;
964 // add a few strings so a completion occurs on any letter typed
965 for ( char idxc
= 'a'; idxc
< 'z'; ++idxc
)
966 completion_choices
.push_back(wxString::Format("%c%c", idxc
, idxc
));
968 completion_choices
.push_back("is this string for test?");
969 completion_choices
.push_back("this is a test string");
970 completion_choices
.push_back("this is another test string");
971 completion_choices
.push_back("this string is for test");
973 if ( entry
->AutoComplete(completion_choices
) )
975 wxLogMessage("Enabled auto completion of a set of fixed strings.");
979 wxLogMessage("AutoComplete() failed.");
983 void WidgetsFrame::OnAutoCompleteFilenames(wxCommandEvent
& WXUNUSED(event
))
985 wxTextEntryBase
*entry
= CurrentPage()->GetTextEntry();
986 wxCHECK_RET( entry
, "menu item should be disabled" );
988 if ( entry
->AutoCompleteFileNames() )
990 wxLogMessage("Enabled auto completion of file names.");
994 wxLogMessage("AutoCompleteFileNames() failed.");
998 void WidgetsFrame::OnAutoCompleteCustom(wxCommandEvent
& WXUNUSED(event
))
1000 wxTextEntryBase
*entry
= CurrentPage()->GetTextEntry();
1001 wxCHECK_RET( entry
, "menu item should be disabled" );
1003 // This is a simple (and hence rather useless) example of a custom
1004 // completer class that completes the first word (only) initially and only
1005 // build the list of the possible second words once the first word is
1006 // known. This allows to avoid building the full 676000 item list of
1007 // possible strings all at once as the we have 1000 possibilities for the
1008 // first word (000..999) and 676 (aa..zz) for the second one.
1009 class CustomTextCompleter
: public wxTextCompleterSimple
1012 virtual void GetCompletions(const wxString
& prefix
, wxArrayString
& res
)
1014 // This is used for illustrative purposes only and shows how many
1015 // completions we return every time when we're called.
1016 class LogCompletions
1019 LogCompletions(const wxString
& prefix
, const wxArrayString
& res
)
1027 wxLogMessage("Returning %lu possible completions for "
1029 m_res
.size(), m_prefix
);
1033 const wxString
& m_prefix
;
1034 const wxArrayString
& m_res
;
1035 } logCompletions(prefix
, res
);
1038 // Normally it doesn't make sense to complete empty control, there
1039 // are too many choices and listing them all wouldn't be helpful.
1040 if ( prefix
.empty() )
1043 // The only valid strings start with 3 digits so check for their
1044 // presence proposing to complete the remaining ones.
1045 if ( !wxIsdigit(prefix
[0]) )
1048 if ( prefix
.length() == 1 )
1050 for ( int i
= 0; i
< 10; i
++ )
1051 for ( int j
= 0; j
< 10; j
++ )
1052 res
.push_back(wxString::Format("%s%02d",
1056 else if ( !wxIsdigit(prefix
[1]) )
1059 if ( prefix
.length() == 2 )
1061 for ( int i
= 0; i
< 10; i
++ )
1062 res
.push_back(wxString::Format("%s%d", prefix
, i
));
1065 else if ( !wxIsdigit(prefix
[2]) )
1068 // Next we must have a space and two letters.
1069 wxString
prefix2(prefix
);
1070 if ( prefix
.length() == 3 )
1072 else if ( prefix
[3] != ' ' )
1075 if ( prefix2
.length() == 4 )
1077 for ( char c
= 'a'; c
<= 'z'; c
++ )
1078 for ( char d
= 'a'; d
<= 'z'; d
++ )
1079 res
.push_back(wxString::Format("%s%c%c", prefix2
, c
, d
));
1082 else if ( !wxIslower(prefix
[4]) )
1085 if ( prefix
.length() == 5 )
1087 for ( char c
= 'a'; c
<= 'z'; c
++ )
1088 res
.push_back(prefix
+ c
);
1093 if ( entry
->AutoComplete(new CustomTextCompleter
) )
1095 wxLogMessage("Enabled custom auto completer for \"NNN XX\" items "
1096 "(where N is a digit and X is a letter).");
1100 wxLogMessage("AutoComplete() failed.");
1104 void WidgetsFrame::OnSetHint(wxCommandEvent
& WXUNUSED(event
))
1106 wxTextEntryBase
*entry
= CurrentPage()->GetTextEntry();
1107 wxCHECK_RET( entry
, "menu item should be disabled" );
1109 static wxString
s_hint("Type here");
1111 hint
= wxGetTextFromUser("Text hint:", "Widgets sample", s_hint
, this);
1117 if ( entry
->SetHint(hint
) )
1119 wxLogMessage("Set hint to \"%s\".", hint
);
1123 wxLogMessage("Text hints not supported.");
1127 #endif // wxUSE_MENUS
1129 // ----------------------------------------------------------------------------
1131 // ----------------------------------------------------------------------------
1133 WidgetsPageInfo::WidgetsPageInfo(Constructor ctor
, const wxChar
*label
, int categories
)
1135 , m_categories(categories
)
1141 // dummy sorting: add and immediately sort in the list according to label
1142 if ( WidgetsPage::ms_widgetPages
)
1144 WidgetsPageInfo
*node_prev
= WidgetsPage::ms_widgetPages
;
1145 if ( wxStrcmp(label
, node_prev
->GetLabel().c_str()) < 0 )
1149 WidgetsPage::ms_widgetPages
= this;
1153 WidgetsPageInfo
*node_next
;
1156 node_next
= node_prev
->GetNext();
1159 // add if between two
1160 if ( wxStrcmp(label
, node_next
->GetLabel().c_str()) < 0 )
1162 node_prev
->SetNext(this);
1164 // force to break loop
1171 node_prev
->SetNext(this);
1174 node_prev
= node_next
;
1176 while ( node_next
);
1182 WidgetsPage::ms_widgetPages
= this;
1186 // ----------------------------------------------------------------------------
1188 // ----------------------------------------------------------------------------
1190 int WidgetsPage::ms_defaultFlags
= wxBORDER_DEFAULT
;
1191 WidgetsPageInfo
*WidgetsPage::ms_widgetPages
= NULL
;
1193 WidgetsPage::WidgetsPage(WidgetsBookCtrl
*book
,
1194 wxImageList
*imaglist
,
1195 const char *const icon
[])
1196 : wxPanel(book
, wxID_ANY
,
1197 wxDefaultPosition
, wxDefaultSize
,
1198 wxNO_FULL_REPAINT_ON_RESIZE
|
1202 #if USE_ICONS_IN_BOOK
1203 imaglist
->Add(wxBitmap(wxImage(icon
).Scale(ICON_SIZE
, ICON_SIZE
)));
1205 wxUnusedVar(imaglist
);
1210 wxSizer
*WidgetsPage::CreateSizerWithText(wxControl
*control
,
1212 wxTextCtrl
**ppText
)
1214 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
1215 wxTextCtrl
*text
= new wxTextCtrl(this, id
, wxEmptyString
,
1216 wxDefaultPosition
, wxDefaultSize
, wxTE_PROCESS_ENTER
);
1218 sizerRow
->Add(control
, 0, wxRIGHT
| wxALIGN_CENTRE_VERTICAL
, 5);
1219 sizerRow
->Add(text
, 1, wxLEFT
| wxALIGN_CENTRE_VERTICAL
, 5);
1227 // create a sizer containing a label and a text ctrl
1228 wxSizer
*WidgetsPage::CreateSizerWithTextAndLabel(const wxString
& label
,
1230 wxTextCtrl
**ppText
)
1232 return CreateSizerWithText(new wxStaticText(this, wxID_ANY
, label
),
1236 // create a sizer containing a button and a text ctrl
1237 wxSizer
*WidgetsPage::CreateSizerWithTextAndButton(wxWindowID idBtn
,
1238 const wxString
& label
,
1240 wxTextCtrl
**ppText
)
1242 return CreateSizerWithText(new wxButton(this, idBtn
, label
), id
, ppText
);
1245 wxCheckBox
*WidgetsPage::CreateCheckBoxAndAddToSizer(wxSizer
*sizer
,
1246 const wxString
& label
,
1249 wxCheckBox
*checkbox
= new wxCheckBox(this, id
, label
);
1250 sizer
->Add(checkbox
, 0, wxLEFT
| wxRIGHT
, 5);
1251 sizer
->Add(0, 2, 0, wxGROW
); // spacer