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_AutoCompleteDirectories
,
103 TextEntry_AutoCompleteCustom
,
109 const wxChar
*WidgetsCategories
[MAX_PAGES
] = {
110 #if defined(__WXUNIVERSAL__)
124 // ----------------------------------------------------------------------------
126 // ----------------------------------------------------------------------------
128 // Define a new application type, each program should derive a class from wxApp
129 class WidgetsApp
: public wxApp
132 // override base class virtuals
133 // ----------------------------
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();
141 // Define a new frame type: this is going to be our main frame
142 class WidgetsFrame
: public wxFrame
146 WidgetsFrame(const wxString
& title
);
147 virtual ~WidgetsFrame();
152 void OnButtonClearLog(wxCommandEvent
& event
);
154 void OnExit(wxCommandEvent
& event
);
157 void OnPageChanging(WidgetsBookCtrlEvent
& event
);
158 void OnPageChanged(WidgetsBookCtrlEvent
& event
);
159 void OnGoToPage(wxCommandEvent
& event
);
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
);
171 void OnToggleGlobalBusyCursor(wxCommandEvent
& event
);
172 void OnToggleBusyCursor(wxCommandEvent
& event
);
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
);
181 void OnSetHint(wxCommandEvent
& event
);
183 void OnUpdateTextUI(wxUpdateUIEvent
& event
)
185 event
.Enable( CurrentPage()->GetTextEntry() != NULL
);
187 #endif // wxUSE_MENUS
189 // initialize the book: add all pages to it
192 // return the currently selected page (never NULL)
193 WidgetsPage
*CurrentPage();
196 // the panel containing everything
200 // the listbox for logging messages
201 wxListBox
*m_lboxLog
;
203 // the log target we use to redirect messages to the listbox
207 // the book containing the test pages
208 WidgetsBookCtrl
*m_book
;
211 // last chosen fg/bg colours and font
215 #endif // wxUSE_MENUS
217 // any class wishing to process wxWidgets events must use this macro
218 DECLARE_EVENT_TABLE()
222 // A log target which just redirects the messages to a listbox
223 class LboxLogger
: public wxLog
226 LboxLogger(wxListBox
*lbox
, wxLog
*logOld
)
229 //m_lbox->Disable(); -- looks ugly under MSW
233 virtual ~LboxLogger()
235 wxLog::SetActiveTarget(m_logOld
);
239 // implement sink functions
240 virtual void DoLogTextAtLevel(wxLogLevel level
, const wxString
& msg
)
242 if ( level
== wxLOG_Trace
)
245 m_logOld
->LogTextAtLevel(level
, msg
);
249 #ifdef __WXUNIVERSAL__
250 m_lbox
->AppendAndEnsureVisible(msg
);
251 #else // other ports don't have this method yet
253 m_lbox
->SetFirstItem(m_lbox
->GetCount() - 1);
257 // the control we use
260 // the old log target
266 WX_DEFINE_ARRAY_PTR(WidgetsPage
*, ArrayWidgetsPage
);
268 // ----------------------------------------------------------------------------
270 // ----------------------------------------------------------------------------
272 IMPLEMENT_APP(WidgetsApp
)
274 // ----------------------------------------------------------------------------
276 // ----------------------------------------------------------------------------
278 BEGIN_EVENT_TABLE(WidgetsFrame
, wxFrame
)
280 EVT_BUTTON(Widgets_ClearLog
, WidgetsFrame::OnButtonClearLog
)
282 EVT_BUTTON(Widgets_Quit
, WidgetsFrame::OnExit
)
285 EVT_MENU(Widgets_SetTooltip
, WidgetsFrame::OnSetTooltip
)
286 #endif // wxUSE_TOOLTIPS
289 EVT_WIDGETS_PAGE_CHANGING(wxID_ANY
, WidgetsFrame::OnPageChanging
)
290 EVT_MENU_RANGE(Widgets_GoToPage
, Widgets_GoToPageLast
,
291 WidgetsFrame::OnGoToPage
)
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
)
299 EVT_MENU_RANGE(Widgets_BorderNone
, Widgets_BorderDefault
,
300 WidgetsFrame::OnSetBorder
)
302 EVT_MENU(Widgets_GlobalBusyCursor
, WidgetsFrame::OnToggleGlobalBusyCursor
)
303 EVT_MENU(Widgets_BusyCursor
, WidgetsFrame::OnToggleBusyCursor
)
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
)
311 EVT_MENU(TextEntry_SetHint
, WidgetsFrame::OnSetHint
)
313 EVT_UPDATE_UI_RANGE(TextEntry_Begin
, TextEntry_End
- 1,
314 WidgetsFrame::OnUpdateTextUI
)
316 EVT_MENU(wxID_EXIT
, WidgetsFrame::OnExit
)
317 #endif // wxUSE_MENUS
320 // ============================================================================
322 // ============================================================================
324 // ----------------------------------------------------------------------------
326 // ----------------------------------------------------------------------------
328 bool WidgetsApp::OnInit()
330 if ( !wxApp::OnInit() )
333 SetVendorName("wxWidgets_Samples");
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
338 #if defined(__WXUNIVERSAL__)
339 title
= wxT("wxUniv/");
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 #elif defined(__WXPALMOS5__)
351 title
+= wxT("wxPALMOS5");
352 #elif defined(__WXPALMOS6__)
353 title
+= wxT("wxPALMOS6");
355 title
+= wxT("wxWidgets");
358 wxFrame
*frame
= new WidgetsFrame(title
+ wxT(" widgets demo"));
364 // ----------------------------------------------------------------------------
365 // WidgetsFrame construction
366 // ----------------------------------------------------------------------------
368 WidgetsFrame::WidgetsFrame(const wxString
& title
)
369 : wxFrame(NULL
, wxID_ANY
, title
)
371 const bool sizeSet
= wxPersistentRegisterAndRestore(this, "Main");
373 // set the frame icon
374 SetIcon(wxICON(sample
));
384 // create the menubar
385 wxMenuBar
*mbar
= new wxMenuBar
;
386 wxMenu
*menuWidget
= new wxMenu
;
388 menuWidget
->Append(Widgets_SetTooltip
, wxT("Set &tooltip...\tCtrl-T"));
389 menuWidget
->AppendSeparator();
390 #endif // wxUSE_TOOLTIPS
391 menuWidget
->Append(Widgets_SetFgColour
, wxT("Set &foreground...\tCtrl-F"));
392 menuWidget
->Append(Widgets_SetBgColour
, wxT("Set &background...\tCtrl-B"));
393 menuWidget
->Append(Widgets_SetPageBg
, wxT("Set &page background...\tShift-Ctrl-B"));
394 menuWidget
->Append(Widgets_SetFont
, wxT("Set f&ont...\tCtrl-O"));
395 menuWidget
->AppendCheckItem(Widgets_Enable
, wxT("&Enable/disable\tCtrl-E"));
397 wxMenu
*menuBorders
= new wxMenu
;
398 menuBorders
->AppendRadioItem(Widgets_BorderDefault
, wxT("De&fault\tCtrl-Shift-9"));
399 menuBorders
->AppendRadioItem(Widgets_BorderNone
, wxT("&None\tCtrl-Shift-0"));
400 menuBorders
->AppendRadioItem(Widgets_BorderSimple
, wxT("&Simple\tCtrl-Shift-1"));
401 menuBorders
->AppendRadioItem(Widgets_BorderDouble
, wxT("&Double\tCtrl-Shift-2"));
402 menuBorders
->AppendRadioItem(Widgets_BorderStatic
, wxT("Stati&c\tCtrl-Shift-3"));
403 menuBorders
->AppendRadioItem(Widgets_BorderRaised
, wxT("&Raised\tCtrl-Shift-4"));
404 menuBorders
->AppendRadioItem(Widgets_BorderSunken
, wxT("S&unken\tCtrl-Shift-5"));
405 menuWidget
->AppendSubMenu(menuBorders
, wxT("Set &border"));
407 menuWidget
->AppendSeparator();
408 menuWidget
->AppendCheckItem(Widgets_GlobalBusyCursor
,
409 wxT("Toggle &global busy cursor\tCtrl-Shift-U"));
410 menuWidget
->AppendCheckItem(Widgets_BusyCursor
,
411 wxT("Toggle b&usy cursor\tCtrl-U"));
413 menuWidget
->AppendSeparator();
414 menuWidget
->Append(wxID_EXIT
, wxT("&Quit\tCtrl-Q"));
415 mbar
->Append(menuWidget
, wxT("&Widget"));
417 wxMenu
*menuTextEntry
= new wxMenu
;
418 menuTextEntry
->AppendRadioItem(TextEntry_DisableAutoComplete
,
419 wxT("&Disable auto-completion"));
420 menuTextEntry
->AppendRadioItem(TextEntry_AutoCompleteFixed
,
421 wxT("Fixed-&list auto-completion"));
422 menuTextEntry
->AppendRadioItem(TextEntry_AutoCompleteFilenames
,
423 wxT("&Files names auto-completion"));
424 menuTextEntry
->AppendRadioItem(TextEntry_AutoCompleteDirectories
,
425 wxT("&Directories names auto-completion"));
426 menuTextEntry
->AppendRadioItem(TextEntry_AutoCompleteCustom
,
427 wxT("&Custom auto-completion"));
428 menuTextEntry
->AppendSeparator();
429 menuTextEntry
->Append(TextEntry_SetHint
, "Set help &hint");
431 mbar
->Append(menuTextEntry
, wxT("&Text"));
435 mbar
->Check(Widgets_Enable
, true);
436 #endif // wxUSE_MENUS
439 m_panel
= new wxPanel(this, wxID_ANY
);
441 wxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
443 // we have 2 panes: book with pages demonstrating the controls in the
444 // upper one and the log window with some buttons in the lower
446 int style
= wxBK_DEFAULT
;
447 // Uncomment to suppress page theme (draw in solid colour)
448 //style |= wxNB_NOPAGETHEME;
450 m_book
= new WidgetsBookCtrl(m_panel
, Widgets_BookCtrl
,
451 wxDefaultPosition
, wxDefaultSize
,
456 #ifndef __WXHANDHELD__
457 // the lower one only has the log listbox and a button to clear it
459 wxSizer
*sizerDown
= new wxStaticBoxSizer(
460 new wxStaticBox( m_panel
, wxID_ANY
, wxT("&Log window") ),
463 m_lboxLog
= new wxListBox(m_panel
, wxID_ANY
);
464 sizerDown
->Add(m_lboxLog
, 1, wxGROW
| wxALL
, 5);
465 sizerDown
->SetMinSize(100, 150);
467 wxSizer
*sizerDown
= new wxBoxSizer(wxVERTICAL
);
470 wxBoxSizer
*sizerBtns
= new wxBoxSizer(wxHORIZONTAL
);
473 btn
= new wxButton(m_panel
, Widgets_ClearLog
, wxT("Clear &log"));
475 sizerBtns
->Add(10, 0); // spacer
477 btn
= new wxButton(m_panel
, Widgets_Quit
, wxT("E&xit"));
479 sizerDown
->Add(sizerBtns
, 0, wxALL
| wxALIGN_RIGHT
, 5);
481 // put everything together
482 sizerTop
->Add(m_book
, 1, wxGROW
| (wxALL
& ~(wxTOP
| wxBOTTOM
)), 10);
483 sizerTop
->Add(0, 5, 0, wxGROW
); // spacer in between
484 sizerTop
->Add(sizerDown
, 0, wxGROW
| (wxALL
& ~wxTOP
), 10);
486 #else // !__WXHANDHELD__/__WXHANDHELD__
488 sizerTop
->Add(m_book
, 1, wxGROW
| wxALL
);
490 #endif // __WXHANDHELD__
492 m_panel
->SetSizer(sizerTop
);
494 const wxSize sizeMin
= m_panel
->GetBestSize();
496 SetClientSize(sizeMin
);
497 SetMinClientSize(sizeMin
);
499 #if USE_LOG && !defined(__WXCOCOA__)
500 // wxCocoa's listbox is too flakey to use for logging right now
501 // now that everything is created we can redirect the log messages to the
503 m_logTarget
= new LboxLogger(m_lboxLog
, wxLog::GetActiveTarget());
504 wxLog::SetActiveTarget(m_logTarget
);
508 void WidgetsFrame::InitBook()
510 #if USE_ICONS_IN_BOOK
511 wxImageList
*imageList
= new wxImageList(ICON_SIZE
, ICON_SIZE
);
513 wxImage
img(sample_xpm
);
514 imageList
->Add(wxBitmap(img
.Scale(ICON_SIZE
, ICON_SIZE
)));
516 wxImageList
*imageList
= NULL
;
520 WidgetsBookCtrl
*books
[MAX_PAGES
];
523 ArrayWidgetsPage pages
[MAX_PAGES
];
524 wxArrayString labels
[MAX_PAGES
];
526 wxMenu
*menuPages
= new wxMenu
;
527 unsigned int nPage
= 0, nFKey
= 0;
528 int cat
, imageId
= 1;
530 // we need to first create all pages and only then add them to the book
531 // as we need the image list first
533 // we also construct the pages menu during this first iteration
534 for ( cat
= 0; cat
< MAX_PAGES
; cat
++ )
537 nPage
++; // increase for parent page
539 books
[cat
] = new WidgetsBookCtrl(m_book
,
546 for ( WidgetsPageInfo
*info
= WidgetsPage::ms_widgetPages
;
548 info
= info
->GetNext() )
550 if( (info
->GetCategories() & ( 1 << cat
)) == 0)
553 WidgetsPage
*page
= (*info
->GetCtor())(
560 pages
[cat
].Add(page
);
562 labels
[cat
].Add(info
->GetLabel());
563 if ( cat
== ALL_PAGE
)
565 wxString
radioLabel(info
->GetLabel());
569 radioLabel
<< wxT("\tF" ) << nFKey
;
572 menuPages
->AppendRadioItem(
573 Widgets_GoToPage
+ nPage
,
577 // consider only for book in book architecture
583 // consider only for treebook architecture (with subpages)
589 GetMenuBar()->Append(menuPages
, wxT("&Page"));
591 #if USE_ICONS_IN_BOOK
592 m_book
->AssignImageList(imageList
);
595 for ( cat
= 0; cat
< MAX_PAGES
; cat
++ )
598 m_book
->AddPage(NULL
,WidgetsCategories
[cat
],false,0);
600 m_book
->AddPage(books
[cat
],WidgetsCategories
[cat
],false,0);
601 #if USE_ICONS_IN_BOOK
602 books
[cat
]->SetImageList(imageList
);
607 size_t count
= pages
[cat
].GetCount();
608 for ( size_t n
= 0; n
< count
; n
++ )
618 false, // don't select
625 wxEVT_COMMAND_WIDGETS_PAGE_CHANGED
,
626 wxWidgetsbookEventHandler(WidgetsFrame::OnPageChanged
) );
628 const bool pageSet
= wxPersistentRegisterAndRestore(m_book
);
631 // for treebook page #0 is empty parent page only so select the first page
632 // with some contents
634 m_book
->SetSelection(1);
636 // but ensure that the top of the tree is shown nevertheless
637 wxTreeCtrl
* const tree
= m_book
->GetTreeCtrl();
639 wxTreeItemIdValue cookie
;
640 tree
->EnsureVisible(tree
->GetFirstChild(tree
->GetRootItem(), cookie
));
644 // for other books set selection twice to force connected event handler
645 // to force lazy creation of initial visible content
646 m_book
->SetSelection(1);
647 m_book
->SetSelection(0);
649 #endif // USE_TREEBOOK
652 WidgetsPage
*WidgetsFrame::CurrentPage()
654 wxWindow
*page
= m_book
->GetCurrentPage();
657 WidgetsBookCtrl
*subBook
= wxStaticCast(page
, WidgetsBookCtrl
);
658 wxCHECK_MSG( subBook
, NULL
, wxT("no WidgetsBookCtrl?") );
660 page
= subBook
->GetCurrentPage();
661 #endif // !USE_TREEBOOK
663 return wxStaticCast(page
, WidgetsPage
);
666 WidgetsFrame::~WidgetsFrame()
673 // ----------------------------------------------------------------------------
674 // WidgetsFrame event handlers
675 // ----------------------------------------------------------------------------
677 void WidgetsFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
683 void WidgetsFrame::OnButtonClearLog(wxCommandEvent
& WXUNUSED(event
))
691 void WidgetsFrame::OnPageChanging(WidgetsBookCtrlEvent
& event
)
694 // don't allow selection of entries without pages (categories)
695 if ( !m_book
->GetPage(event
.GetSelection()) )
702 void WidgetsFrame::OnPageChanged(WidgetsBookCtrlEvent
& event
)
704 const int sel
= event
.GetSelection();
706 // adjust "Page" menu selection
707 wxMenuItem
*item
= GetMenuBar()->FindItem(Widgets_GoToPage
+ sel
);
711 GetMenuBar()->Check(Widgets_BusyCursor
, false);
713 // create the pages on demand, otherwise the sample startup is too slow as
714 // it creates hundreds of controls
715 WidgetsPage
*page
= CurrentPage();
716 if ( page
->GetChildren().empty() )
718 wxWindowUpdateLocker
noUpdates(page
);
719 page
->CreateContent();
721 page
->GetSizer()->Fit(page
);
723 WidgetsBookCtrl
*book
= wxStaticCast(page
->GetParent(), WidgetsBookCtrl
);
725 for ( size_t i
= 0; i
< book
->GetPageCount(); ++i
)
727 wxWindow
*page
= book
->GetPage(i
);
730 size
.IncTo(page
->GetSize());
739 void WidgetsFrame::OnGoToPage(wxCommandEvent
& event
)
742 m_book
->SetSelection(event
.GetId() - Widgets_GoToPage
);
744 m_book
->SetSelection(m_book
->GetPageCount()-1);
745 WidgetsBookCtrl
*book
= wxStaticCast(m_book
->GetCurrentPage(), WidgetsBookCtrl
);
746 book
->SetSelection(event
.GetId() - Widgets_GoToPage
);
752 void WidgetsFrame::OnSetTooltip(wxCommandEvent
& WXUNUSED(event
))
754 static wxString s_tip
= wxT("This is a tooltip");
756 wxTextEntryDialog dialog
759 wxT("Tooltip text (may use \\n, leave empty to remove): "),
760 wxT("Widgets sample"),
764 if ( dialog
.ShowModal() != wxID_OK
)
767 s_tip
= dialog
.GetValue();
768 s_tip
.Replace(wxT("\\n"), wxT("\n"));
770 WidgetsPage
*page
= CurrentPage();
772 const Widgets widgets
= page
->GetWidgets();
773 for ( Widgets::const_iterator it
= widgets
.begin();
777 (*it
)->SetToolTip(s_tip
);
781 #endif // wxUSE_TOOLTIPS
786 // Trivial wrapper for wxGetColourFromUser() which also does something even if
787 // the colour dialog is not available in the current build (which may happen
788 // for the ports in development and it is still useful to see how colours work)
789 wxColour
GetColourFromUser(wxWindow
*parent
, const wxColour
& colDefault
)
792 return wxGetColourFromUser(parent
, colDefault
);
793 #else // !wxUSE_COLOURDLG
794 if ( colDefault
== *wxBLACK
)
798 #endif // wxUSE_COLOURDLG/!wxUSE_COLOURDLG
801 } // anonymous namespace
803 void WidgetsFrame::OnSetFgCol(wxCommandEvent
& WXUNUSED(event
))
805 // allow for debugging the default colour the first time this is called
806 WidgetsPage
*page
= CurrentPage();
809 m_colFg
= page
->GetForegroundColour();
811 wxColour col
= GetColourFromUser(this, m_colFg
);
817 const Widgets widgets
= page
->GetWidgets();
818 for ( Widgets::const_iterator it
= widgets
.begin();
822 (*it
)->SetForegroundColour(m_colFg
);
827 void WidgetsFrame::OnSetBgCol(wxCommandEvent
& WXUNUSED(event
))
829 WidgetsPage
*page
= CurrentPage();
831 if ( !m_colBg
.IsOk() )
832 m_colBg
= page
->GetBackgroundColour();
834 wxColour col
= GetColourFromUser(this, m_colBg
);
840 const Widgets widgets
= page
->GetWidgets();
841 for ( Widgets::const_iterator it
= widgets
.begin();
845 (*it
)->SetBackgroundColour(m_colBg
);
850 void WidgetsFrame::OnSetPageBg(wxCommandEvent
& WXUNUSED(event
))
852 wxColour col
= GetColourFromUser(this, GetBackgroundColour());
856 CurrentPage()->SetBackgroundColour(col
);
857 CurrentPage()->Refresh();
860 void WidgetsFrame::OnSetFont(wxCommandEvent
& WXUNUSED(event
))
863 WidgetsPage
*page
= CurrentPage();
866 m_font
= page
->GetFont();
868 wxFont font
= wxGetFontFromUser(this, m_font
);
874 const Widgets widgets
= page
->GetWidgets();
875 for ( Widgets::const_iterator it
= widgets
.begin();
879 (*it
)->SetFont(m_font
);
883 wxLogMessage(wxT("Font selection dialog not available in current build."));
887 void WidgetsFrame::OnEnable(wxCommandEvent
& event
)
889 const Widgets widgets
= CurrentPage()->GetWidgets();
890 for ( Widgets::const_iterator it
= widgets
.begin();
894 (*it
)->Enable(event
.IsChecked());
898 void WidgetsFrame::OnSetBorder(wxCommandEvent
& event
)
901 switch ( event
.GetId() )
903 case Widgets_BorderNone
: border
= wxBORDER_NONE
; break;
904 case Widgets_BorderStatic
: border
= wxBORDER_STATIC
; break;
905 case Widgets_BorderSimple
: border
= wxBORDER_SIMPLE
; break;
906 case Widgets_BorderRaised
: border
= wxBORDER_RAISED
; break;
907 case Widgets_BorderSunken
: border
= wxBORDER_SUNKEN
; break;
908 case Widgets_BorderDouble
: border
= wxBORDER_DOUBLE
; break;
911 wxFAIL_MSG( wxT("unknown border style") );
914 case Widgets_BorderDefault
: border
= wxBORDER_DEFAULT
; break;
917 WidgetsPage::ms_defaultFlags
&= ~wxBORDER_MASK
;
918 WidgetsPage::ms_defaultFlags
|= border
;
920 WidgetsPage
*page
= CurrentPage();
922 page
->RecreateWidget();
925 void WidgetsFrame::OnToggleGlobalBusyCursor(wxCommandEvent
& event
)
927 if ( event
.IsChecked() )
933 void WidgetsFrame::OnToggleBusyCursor(wxCommandEvent
& event
)
935 wxCursor
cursor(*(event
.IsChecked() ? wxHOURGLASS_CURSOR
936 : wxSTANDARD_CURSOR
));
938 const Widgets widgets
= CurrentPage()->GetWidgets();
939 for ( Widgets::const_iterator it
= widgets
.begin();
943 (*it
)->SetCursor(cursor
);
947 void WidgetsFrame::OnDisableAutoComplete(wxCommandEvent
& WXUNUSED(event
))
949 wxTextEntryBase
*entry
= CurrentPage()->GetTextEntry();
950 wxCHECK_RET( entry
, "menu item should be disabled" );
952 if ( entry
->AutoComplete(wxArrayString()) )
954 wxLogMessage("Disabled auto completion.");
958 wxLogMessage("AutoComplete() failed.");
962 void WidgetsFrame::OnAutoCompleteFixed(wxCommandEvent
& WXUNUSED(event
))
964 wxTextEntryBase
*entry
= CurrentPage()->GetTextEntry();
965 wxCHECK_RET( entry
, "menu item should be disabled" );
967 wxArrayString completion_choices
;
969 // add a few strings so a completion occurs on any letter typed
970 for ( char idxc
= 'a'; idxc
< 'z'; ++idxc
)
971 completion_choices
.push_back(wxString::Format("%c%c", idxc
, idxc
));
973 completion_choices
.push_back("is this string for test?");
974 completion_choices
.push_back("this is a test string");
975 completion_choices
.push_back("this is another test string");
976 completion_choices
.push_back("this string is for test");
978 if ( entry
->AutoComplete(completion_choices
) )
980 wxLogMessage("Enabled auto completion of a set of fixed strings.");
984 wxLogMessage("AutoComplete() failed.");
988 void WidgetsFrame::OnAutoCompleteFilenames(wxCommandEvent
& WXUNUSED(event
))
990 wxTextEntryBase
*entry
= CurrentPage()->GetTextEntry();
991 wxCHECK_RET( entry
, "menu item should be disabled" );
993 if ( entry
->AutoCompleteFileNames() )
995 wxLogMessage("Enabled auto completion of file names.");
999 wxLogMessage("AutoCompleteFileNames() failed.");
1003 void WidgetsFrame::OnAutoCompleteDirectories(wxCommandEvent
& WXUNUSED(event
))
1005 wxTextEntryBase
*entry
= CurrentPage()->GetTextEntry();
1006 wxCHECK_RET( entry
, "menu item should be disabled" );
1008 if ( entry
->AutoCompleteDirectories() )
1010 wxLogMessage("Enabled auto completion of directories.");
1014 wxLogMessage("AutoCompleteDirectories() failed.");
1018 void WidgetsFrame::OnAutoCompleteCustom(wxCommandEvent
& WXUNUSED(event
))
1020 wxTextEntryBase
*entry
= CurrentPage()->GetTextEntry();
1021 wxCHECK_RET( entry
, "menu item should be disabled" );
1023 // This is a simple (and hence rather useless) example of a custom
1024 // completer class that completes the first word (only) initially and only
1025 // build the list of the possible second words once the first word is
1026 // known. This allows to avoid building the full 676000 item list of
1027 // possible strings all at once as the we have 1000 possibilities for the
1028 // first word (000..999) and 676 (aa..zz) for the second one.
1029 class CustomTextCompleter
: public wxTextCompleterSimple
1032 virtual void GetCompletions(const wxString
& prefix
, wxArrayString
& res
)
1034 // This is used for illustrative purposes only and shows how many
1035 // completions we return every time when we're called.
1036 class LogCompletions
1039 LogCompletions(const wxString
& prefix
, const wxArrayString
& res
)
1047 wxLogMessage("Returning %lu possible completions for "
1049 m_res
.size(), m_prefix
);
1053 const wxString
& m_prefix
;
1054 const wxArrayString
& m_res
;
1055 } logCompletions(prefix
, res
);
1058 // Normally it doesn't make sense to complete empty control, there
1059 // are too many choices and listing them all wouldn't be helpful.
1060 if ( prefix
.empty() )
1063 // The only valid strings start with 3 digits so check for their
1064 // presence proposing to complete the remaining ones.
1065 if ( !wxIsdigit(prefix
[0]) )
1068 if ( prefix
.length() == 1 )
1070 for ( int i
= 0; i
< 10; i
++ )
1071 for ( int j
= 0; j
< 10; j
++ )
1072 res
.push_back(wxString::Format("%s%02d",
1076 else if ( !wxIsdigit(prefix
[1]) )
1079 if ( prefix
.length() == 2 )
1081 for ( int i
= 0; i
< 10; i
++ )
1082 res
.push_back(wxString::Format("%s%d", prefix
, i
));
1085 else if ( !wxIsdigit(prefix
[2]) )
1088 // Next we must have a space and two letters.
1089 wxString
prefix2(prefix
);
1090 if ( prefix
.length() == 3 )
1092 else if ( prefix
[3] != ' ' )
1095 if ( prefix2
.length() == 4 )
1097 for ( char c
= 'a'; c
<= 'z'; c
++ )
1098 for ( char d
= 'a'; d
<= 'z'; d
++ )
1099 res
.push_back(wxString::Format("%s%c%c", prefix2
, c
, d
));
1102 else if ( !wxIslower(prefix
[4]) )
1105 if ( prefix
.length() == 5 )
1107 for ( char c
= 'a'; c
<= 'z'; c
++ )
1108 res
.push_back(prefix
+ c
);
1113 if ( entry
->AutoComplete(new CustomTextCompleter
) )
1115 wxLogMessage("Enabled custom auto completer for \"NNN XX\" items "
1116 "(where N is a digit and X is a letter).");
1120 wxLogMessage("AutoComplete() failed.");
1124 void WidgetsFrame::OnSetHint(wxCommandEvent
& WXUNUSED(event
))
1126 wxTextEntryBase
*entry
= CurrentPage()->GetTextEntry();
1127 wxCHECK_RET( entry
, "menu item should be disabled" );
1129 static wxString
s_hint("Type here");
1131 hint
= wxGetTextFromUser("Text hint:", "Widgets sample", s_hint
, this);
1137 if ( entry
->SetHint(hint
) )
1139 wxLogMessage("Set hint to \"%s\".", hint
);
1143 wxLogMessage("Text hints not supported.");
1147 #endif // wxUSE_MENUS
1149 // ----------------------------------------------------------------------------
1151 // ----------------------------------------------------------------------------
1153 WidgetsPageInfo::WidgetsPageInfo(Constructor ctor
, const wxChar
*label
, int categories
)
1155 , m_categories(categories
)
1161 // dummy sorting: add and immediately sort in the list according to label
1162 if ( WidgetsPage::ms_widgetPages
)
1164 WidgetsPageInfo
*node_prev
= WidgetsPage::ms_widgetPages
;
1165 if ( wxStrcmp(label
, node_prev
->GetLabel().c_str()) < 0 )
1169 WidgetsPage::ms_widgetPages
= this;
1173 WidgetsPageInfo
*node_next
;
1176 node_next
= node_prev
->GetNext();
1179 // add if between two
1180 if ( wxStrcmp(label
, node_next
->GetLabel().c_str()) < 0 )
1182 node_prev
->SetNext(this);
1184 // force to break loop
1191 node_prev
->SetNext(this);
1194 node_prev
= node_next
;
1196 while ( node_next
);
1202 WidgetsPage::ms_widgetPages
= this;
1206 // ----------------------------------------------------------------------------
1208 // ----------------------------------------------------------------------------
1210 int WidgetsPage::ms_defaultFlags
= wxBORDER_DEFAULT
;
1211 WidgetsPageInfo
*WidgetsPage::ms_widgetPages
= NULL
;
1213 WidgetsPage::WidgetsPage(WidgetsBookCtrl
*book
,
1214 wxImageList
*imaglist
,
1215 const char *const icon
[])
1216 : wxPanel(book
, wxID_ANY
,
1217 wxDefaultPosition
, wxDefaultSize
,
1218 wxNO_FULL_REPAINT_ON_RESIZE
|
1222 #if USE_ICONS_IN_BOOK
1223 imaglist
->Add(wxBitmap(wxImage(icon
).Scale(ICON_SIZE
, ICON_SIZE
)));
1225 wxUnusedVar(imaglist
);
1230 wxSizer
*WidgetsPage::CreateSizerWithText(wxControl
*control
,
1232 wxTextCtrl
**ppText
)
1234 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
1235 wxTextCtrl
*text
= new wxTextCtrl(this, id
, wxEmptyString
,
1236 wxDefaultPosition
, wxDefaultSize
, wxTE_PROCESS_ENTER
);
1238 sizerRow
->Add(control
, 0, wxRIGHT
| wxALIGN_CENTRE_VERTICAL
, 5);
1239 sizerRow
->Add(text
, 1, wxLEFT
| wxALIGN_CENTRE_VERTICAL
, 5);
1247 // create a sizer containing a label and a text ctrl
1248 wxSizer
*WidgetsPage::CreateSizerWithTextAndLabel(const wxString
& label
,
1250 wxTextCtrl
**ppText
)
1252 return CreateSizerWithText(new wxStaticText(this, wxID_ANY
, label
),
1256 // create a sizer containing a button and a text ctrl
1257 wxSizer
*WidgetsPage::CreateSizerWithTextAndButton(wxWindowID idBtn
,
1258 const wxString
& label
,
1260 wxTextCtrl
**ppText
)
1262 return CreateSizerWithText(new wxButton(this, idBtn
, label
), id
, ppText
);
1265 wxCheckBox
*WidgetsPage::CreateCheckBoxAndAddToSizer(wxSizer
*sizer
,
1266 const wxString
& label
,
1269 wxCheckBox
*checkbox
= new wxCheckBox(this, id
, label
);
1270 sizer
->Add(checkbox
, 0, wxLEFT
| wxRIGHT
, 5);
1271 sizer
->Add(0, 2, 0, wxGROW
); // spacer