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 // License: wxWindows license
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"
54 #include "wx/persist/toplevel.h"
55 #include "wx/persist/treebook.h"
59 #include "../sample.xpm"
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
68 Widgets_ClearLog
= 100,
75 #endif // wxUSE_TOOLTIPS
88 Widgets_BorderDefault
,
90 Widgets_GlobalBusyCursor
,
94 Widgets_GoToPageLast
= Widgets_GoToPage
+ 100,
98 TextEntry_DisableAutoComplete
= TextEntry_Begin
,
99 TextEntry_AutoCompleteFixed
,
100 TextEntry_AutoCompleteFilenames
,
106 const wxChar
*WidgetsCategories
[MAX_PAGES
] = {
107 #if defined(__WXUNIVERSAL__)
121 // ----------------------------------------------------------------------------
123 // ----------------------------------------------------------------------------
125 // Define a new application type, each program should derive a class from wxApp
126 class WidgetsApp
: public wxApp
129 // override base class virtuals
130 // ----------------------------
132 // this one is called on application startup and is a good place for the app
133 // initialization (doing it here and not in the ctor allows to have an error
134 // return: if OnInit() returns false, the application terminates)
135 virtual bool OnInit();
138 // Define a new frame type: this is going to be our main frame
139 class WidgetsFrame
: public wxFrame
143 WidgetsFrame(const wxString
& title
);
144 virtual ~WidgetsFrame();
149 void OnButtonClearLog(wxCommandEvent
& event
);
151 void OnExit(wxCommandEvent
& event
);
154 void OnPageChanging(WidgetsBookCtrlEvent
& event
);
155 void OnPageChanged(WidgetsBookCtrlEvent
& event
);
156 void OnGoToPage(wxCommandEvent
& event
);
159 void OnSetTooltip(wxCommandEvent
& event
);
160 #endif // wxUSE_TOOLTIPS
161 void OnSetFgCol(wxCommandEvent
& event
);
162 void OnSetBgCol(wxCommandEvent
& event
);
163 void OnSetPageBg(wxCommandEvent
& event
);
164 void OnSetFont(wxCommandEvent
& event
);
165 void OnEnable(wxCommandEvent
& event
);
166 void OnSetBorder(wxCommandEvent
& event
);
168 void OnToggleGlobalBusyCursor(wxCommandEvent
& event
);
169 void OnToggleBusyCursor(wxCommandEvent
& event
);
171 // wxTextEntry-specific tests
172 void OnDisableAutoComplete(wxCommandEvent
& event
);
173 void OnAutoCompleteFixed(wxCommandEvent
& event
);
174 void OnAutoCompleteFilenames(wxCommandEvent
& event
);
176 void OnSetHint(wxCommandEvent
& event
);
178 void OnUpdateTextUI(wxUpdateUIEvent
& event
)
180 event
.Enable( CurrentPage()->GetTextEntry() != NULL
);
182 #endif // wxUSE_MENUS
184 // initialize the book: add all pages to it
187 // return the currently selected page (never NULL)
188 WidgetsPage
*CurrentPage();
191 // the panel containing everything
195 // the listbox for logging messages
196 wxListBox
*m_lboxLog
;
198 // the log target we use to redirect messages to the listbox
202 // the book containing the test pages
203 WidgetsBookCtrl
*m_book
;
206 // last chosen fg/bg colours and font
210 #endif // wxUSE_MENUS
212 // any class wishing to process wxWidgets events must use this macro
213 DECLARE_EVENT_TABLE()
217 // A log target which just redirects the messages to a listbox
218 class LboxLogger
: public wxLog
221 LboxLogger(wxListBox
*lbox
, wxLog
*logOld
)
224 //m_lbox->Disable(); -- looks ugly under MSW
228 virtual ~LboxLogger()
230 wxLog::SetActiveTarget(m_logOld
);
234 // implement sink functions
235 virtual void DoLogTextAtLevel(wxLogLevel level
, const wxString
& msg
)
237 if ( level
== wxLOG_Trace
)
240 m_logOld
->LogTextAtLevel(level
, msg
);
244 #ifdef __WXUNIVERSAL__
245 m_lbox
->AppendAndEnsureVisible(msg
);
246 #else // other ports don't have this method yet
248 m_lbox
->SetFirstItem(m_lbox
->GetCount() - 1);
252 // the control we use
255 // the old log target
261 WX_DEFINE_ARRAY_PTR(WidgetsPage
*, ArrayWidgetsPage
);
263 // ----------------------------------------------------------------------------
265 // ----------------------------------------------------------------------------
267 IMPLEMENT_APP(WidgetsApp
)
269 // ----------------------------------------------------------------------------
271 // ----------------------------------------------------------------------------
273 BEGIN_EVENT_TABLE(WidgetsFrame
, wxFrame
)
275 EVT_BUTTON(Widgets_ClearLog
, WidgetsFrame::OnButtonClearLog
)
277 EVT_BUTTON(Widgets_Quit
, WidgetsFrame::OnExit
)
280 EVT_MENU(Widgets_SetTooltip
, WidgetsFrame::OnSetTooltip
)
281 #endif // wxUSE_TOOLTIPS
284 EVT_WIDGETS_PAGE_CHANGING(wxID_ANY
, WidgetsFrame::OnPageChanging
)
285 EVT_MENU_RANGE(Widgets_GoToPage
, Widgets_GoToPageLast
,
286 WidgetsFrame::OnGoToPage
)
288 EVT_MENU(Widgets_SetFgColour
, WidgetsFrame::OnSetFgCol
)
289 EVT_MENU(Widgets_SetBgColour
, WidgetsFrame::OnSetBgCol
)
290 EVT_MENU(Widgets_SetPageBg
, WidgetsFrame::OnSetPageBg
)
291 EVT_MENU(Widgets_SetFont
, WidgetsFrame::OnSetFont
)
292 EVT_MENU(Widgets_Enable
, WidgetsFrame::OnEnable
)
294 EVT_MENU_RANGE(Widgets_BorderNone
, Widgets_BorderDefault
,
295 WidgetsFrame::OnSetBorder
)
297 EVT_MENU(Widgets_GlobalBusyCursor
, WidgetsFrame::OnToggleGlobalBusyCursor
)
298 EVT_MENU(Widgets_BusyCursor
, WidgetsFrame::OnToggleBusyCursor
)
300 EVT_MENU(TextEntry_DisableAutoComplete
, WidgetsFrame::OnDisableAutoComplete
)
301 EVT_MENU(TextEntry_AutoCompleteFixed
, WidgetsFrame::OnAutoCompleteFixed
)
302 EVT_MENU(TextEntry_AutoCompleteFilenames
, WidgetsFrame::OnAutoCompleteFilenames
)
304 EVT_MENU(TextEntry_SetHint
, WidgetsFrame::OnSetHint
)
306 EVT_UPDATE_UI_RANGE(TextEntry_Begin
, TextEntry_End
- 1,
307 WidgetsFrame::OnUpdateTextUI
)
309 EVT_MENU(wxID_EXIT
, WidgetsFrame::OnExit
)
310 #endif // wxUSE_MENUS
313 // ============================================================================
315 // ============================================================================
317 // ----------------------------------------------------------------------------
319 // ----------------------------------------------------------------------------
321 bool WidgetsApp::OnInit()
323 if ( !wxApp::OnInit() )
326 SetVendorName("wxWidgets_Samples");
328 // the reason for having these ifdef's is that I often run two copies of
329 // this sample side by side and it is useful to see which one is which
331 #if defined(__WXUNIVERSAL__)
332 title
= wxT("wxUniv/");
335 #if defined(__WXMSW__)
336 title
+= wxT("wxMSW");
337 #elif defined(__WXGTK__)
338 title
+= wxT("wxGTK");
339 #elif defined(__WXMAC__)
340 title
+= wxT("wxMAC");
341 #elif defined(__WXMOTIF__)
342 title
+= wxT("wxMOTIF");
343 #elif defined(__WXPALMOS5__)
344 title
+= wxT("wxPALMOS5");
345 #elif defined(__WXPALMOS6__)
346 title
+= wxT("wxPALMOS6");
348 title
+= wxT("wxWidgets");
351 wxFrame
*frame
= new WidgetsFrame(title
+ wxT(" widgets demo"));
357 // ----------------------------------------------------------------------------
358 // WidgetsFrame construction
359 // ----------------------------------------------------------------------------
361 WidgetsFrame::WidgetsFrame(const wxString
& title
)
362 : wxFrame(NULL
, wxID_ANY
, title
)
365 const bool sizeSet
= wxPersistentRegisterAndRestore(this);
367 // set the frame icon
368 SetIcon(wxICON(sample
));
378 // create the menubar
379 wxMenuBar
*mbar
= new wxMenuBar
;
380 wxMenu
*menuWidget
= new wxMenu
;
382 menuWidget
->Append(Widgets_SetTooltip
, wxT("Set &tooltip...\tCtrl-T"));
383 menuWidget
->AppendSeparator();
384 #endif // wxUSE_TOOLTIPS
385 menuWidget
->Append(Widgets_SetFgColour
, wxT("Set &foreground...\tCtrl-F"));
386 menuWidget
->Append(Widgets_SetBgColour
, wxT("Set &background...\tCtrl-B"));
387 menuWidget
->Append(Widgets_SetPageBg
, wxT("Set &page background...\tShift-Ctrl-B"));
388 menuWidget
->Append(Widgets_SetFont
, wxT("Set f&ont...\tCtrl-O"));
389 menuWidget
->AppendCheckItem(Widgets_Enable
, wxT("&Enable/disable\tCtrl-E"));
391 wxMenu
*menuBorders
= new wxMenu
;
392 menuBorders
->AppendRadioItem(Widgets_BorderDefault
, wxT("De&fault\tCtrl-Shift-9"));
393 menuBorders
->AppendRadioItem(Widgets_BorderNone
, wxT("&None\tCtrl-Shift-0"));
394 menuBorders
->AppendRadioItem(Widgets_BorderSimple
, wxT("&Simple\tCtrl-Shift-1"));
395 menuBorders
->AppendRadioItem(Widgets_BorderDouble
, wxT("&Double\tCtrl-Shift-2"));
396 menuBorders
->AppendRadioItem(Widgets_BorderStatic
, wxT("Stati&c\tCtrl-Shift-3"));
397 menuBorders
->AppendRadioItem(Widgets_BorderRaised
, wxT("&Raised\tCtrl-Shift-4"));
398 menuBorders
->AppendRadioItem(Widgets_BorderSunken
, wxT("S&unken\tCtrl-Shift-5"));
399 menuWidget
->AppendSubMenu(menuBorders
, wxT("Set &border"));
401 menuWidget
->AppendSeparator();
402 menuWidget
->AppendCheckItem(Widgets_GlobalBusyCursor
,
403 wxT("Toggle &global busy cursor\tCtrl-Shift-U"));
404 menuWidget
->AppendCheckItem(Widgets_BusyCursor
,
405 wxT("Toggle b&usy cursor\tCtrl-U"));
407 menuWidget
->AppendSeparator();
408 menuWidget
->Append(wxID_EXIT
, wxT("&Quit\tCtrl-Q"));
409 mbar
->Append(menuWidget
, wxT("&Widget"));
411 wxMenu
*menuTextEntry
= new wxMenu
;
412 menuTextEntry
->AppendRadioItem(TextEntry_DisableAutoComplete
,
413 wxT("&Disable auto-completion"));
414 menuTextEntry
->AppendRadioItem(TextEntry_AutoCompleteFixed
,
415 wxT("Fixed-&list auto-completion"));
416 menuTextEntry
->AppendRadioItem(TextEntry_AutoCompleteFilenames
,
417 wxT("&Files names auto-completion"));
418 menuTextEntry
->AppendSeparator();
419 menuTextEntry
->Append(TextEntry_SetHint
, "Set help &hint");
421 mbar
->Append(menuTextEntry
, wxT("&Text"));
425 mbar
->Check(Widgets_Enable
, true);
426 #endif // wxUSE_MENUS
429 m_panel
= new wxPanel(this, wxID_ANY
);
431 wxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
433 // we have 2 panes: book with pages demonstrating the controls in the
434 // upper one and the log window with some buttons in the lower
436 int style
= wxBK_DEFAULT
;
437 // Uncomment to suppress page theme (draw in solid colour)
438 //style |= wxNB_NOPAGETHEME;
440 m_book
= new WidgetsBookCtrl(m_panel
, Widgets_BookCtrl
,
441 wxDefaultPosition
, wxDefaultSize
,
446 #ifndef __WXHANDHELD__
447 // the lower one only has the log listbox and a button to clear it
449 wxSizer
*sizerDown
= new wxStaticBoxSizer(
450 new wxStaticBox( m_panel
, wxID_ANY
, wxT("&Log window") ),
453 m_lboxLog
= new wxListBox(m_panel
, wxID_ANY
);
454 sizerDown
->Add(m_lboxLog
, 1, wxGROW
| wxALL
, 5);
455 sizerDown
->SetMinSize(100, 150);
457 wxSizer
*sizerDown
= new wxBoxSizer(wxVERTICAL
);
460 wxBoxSizer
*sizerBtns
= new wxBoxSizer(wxHORIZONTAL
);
463 btn
= new wxButton(m_panel
, Widgets_ClearLog
, wxT("Clear &log"));
465 sizerBtns
->Add(10, 0); // spacer
467 btn
= new wxButton(m_panel
, Widgets_Quit
, wxT("E&xit"));
469 sizerDown
->Add(sizerBtns
, 0, wxALL
| wxALIGN_RIGHT
, 5);
471 // put everything together
472 sizerTop
->Add(m_book
, 1, wxGROW
| (wxALL
& ~(wxTOP
| wxBOTTOM
)), 10);
473 sizerTop
->Add(0, 5, 0, wxGROW
); // spacer in between
474 sizerTop
->Add(sizerDown
, 0, wxGROW
| (wxALL
& ~wxTOP
), 10);
476 #else // !__WXHANDHELD__/__WXHANDHELD__
478 sizerTop
->Add(m_book
, 1, wxGROW
| wxALL
);
480 #endif // __WXHANDHELD__
482 m_panel
->SetSizer(sizerTop
);
484 const wxSize sizeMin
= m_panel
->GetBestSize();
486 SetClientSize(sizeMin
);
487 SetMinClientSize(sizeMin
);
489 #if USE_LOG && !defined(__WXCOCOA__)
490 // wxCocoa's listbox is too flakey to use for logging right now
491 // now that everything is created we can redirect the log messages to the
493 m_logTarget
= new LboxLogger(m_lboxLog
, wxLog::GetActiveTarget());
494 wxLog::SetActiveTarget(m_logTarget
);
498 void WidgetsFrame::InitBook()
500 #if USE_ICONS_IN_BOOK
501 wxImageList
*imageList
= new wxImageList(ICON_SIZE
, ICON_SIZE
);
503 wxImage
img(sample_xpm
);
504 imageList
->Add(wxBitmap(img
.Scale(ICON_SIZE
, ICON_SIZE
)));
506 wxImageList
*imageList
= NULL
;
510 WidgetsBookCtrl
*books
[MAX_PAGES
];
513 ArrayWidgetsPage pages
[MAX_PAGES
];
514 wxArrayString labels
[MAX_PAGES
];
516 wxMenu
*menuPages
= new wxMenu
;
517 unsigned int nPage
= 0, nFKey
= 0;
518 int cat
, imageId
= 1;
520 // we need to first create all pages and only then add them to the book
521 // as we need the image list first
523 // we also construct the pages menu during this first iteration
524 for ( cat
= 0; cat
< MAX_PAGES
; cat
++ )
527 nPage
++; // increase for parent page
529 books
[cat
] = new WidgetsBookCtrl(m_book
,
536 for ( WidgetsPageInfo
*info
= WidgetsPage::ms_widgetPages
;
538 info
= info
->GetNext() )
540 if( (info
->GetCategories() & ( 1 << cat
)) == 0)
543 WidgetsPage
*page
= (*info
->GetCtor())(
550 pages
[cat
].Add(page
);
552 labels
[cat
].Add(info
->GetLabel());
553 if ( cat
== ALL_PAGE
)
555 wxString
radioLabel(info
->GetLabel());
559 radioLabel
<< wxT("\tF" ) << nFKey
;
562 menuPages
->AppendRadioItem(
563 Widgets_GoToPage
+ nPage
,
567 // consider only for book in book architecture
573 // consider only for treebook architecture (with subpages)
579 GetMenuBar()->Append(menuPages
, wxT("&Page"));
581 #if USE_ICONS_IN_BOOK
582 m_book
->AssignImageList(imageList
);
585 for ( cat
= 0; cat
< MAX_PAGES
; cat
++ )
588 m_book
->AddPage(NULL
,WidgetsCategories
[cat
],false,0);
590 m_book
->AddPage(books
[cat
],WidgetsCategories
[cat
],false,0);
591 #if USE_ICONS_IN_BOOK
592 books
[cat
]->SetImageList(imageList
);
597 size_t count
= pages
[cat
].GetCount();
598 for ( size_t n
= 0; n
< count
; n
++ )
608 false, // don't select
615 wxEVT_COMMAND_WIDGETS_PAGE_CHANGED
,
616 wxWidgetsbookEventHandler(WidgetsFrame::OnPageChanged
) );
618 const bool pageSet
= wxPersistentRegisterAndRestore(m_book
);
621 // for treebook page #0 is empty parent page only so select the first page
622 // with some contents
624 m_book
->SetSelection(1);
626 // but ensure that the top of the tree is shown nevertheless
627 wxTreeCtrl
* const tree
= m_book
->GetTreeCtrl();
629 wxTreeItemIdValue cookie
;
630 tree
->EnsureVisible(tree
->GetFirstChild(tree
->GetRootItem(), cookie
));
634 // for other books set selection twice to force connected event handler
635 // to force lazy creation of initial visible content
636 m_book
->SetSelection(1);
637 m_book
->SetSelection(0);
639 #endif // USE_TREEBOOK
642 WidgetsPage
*WidgetsFrame::CurrentPage()
644 wxWindow
*page
= m_book
->GetCurrentPage();
647 WidgetsBookCtrl
*subBook
= wxStaticCast(page
, WidgetsBookCtrl
);
648 wxCHECK_MSG( subBook
, NULL
, wxT("no WidgetsBookCtrl?") );
650 page
= subBook
->GetCurrentPage();
651 #endif // !USE_TREEBOOK
653 return wxStaticCast(page
, WidgetsPage
);
656 WidgetsFrame::~WidgetsFrame()
663 // ----------------------------------------------------------------------------
664 // WidgetsFrame event handlers
665 // ----------------------------------------------------------------------------
667 void WidgetsFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
673 void WidgetsFrame::OnButtonClearLog(wxCommandEvent
& WXUNUSED(event
))
681 void WidgetsFrame::OnPageChanging(WidgetsBookCtrlEvent
& event
)
684 // don't allow selection of entries without pages (categories)
685 if ( !m_book
->GetPage(event
.GetSelection()) )
692 void WidgetsFrame::OnPageChanged(WidgetsBookCtrlEvent
& event
)
694 const int sel
= event
.GetSelection();
696 // adjust "Page" menu selection
697 wxMenuItem
*item
= GetMenuBar()->FindItem(Widgets_GoToPage
+ sel
);
701 GetMenuBar()->Check(Widgets_BusyCursor
, false);
703 // create the pages on demand, otherwise the sample startup is too slow as
704 // it creates hundreds of controls
705 WidgetsPage
*page
= CurrentPage();
706 if ( page
->GetChildren().empty() )
708 wxWindowUpdateLocker
noUpdates(page
);
709 page
->CreateContent();
711 page
->GetSizer()->Fit(page
);
713 WidgetsBookCtrl
*book
= wxStaticCast(page
->GetParent(), WidgetsBookCtrl
);
715 for ( size_t i
= 0; i
< book
->GetPageCount(); ++i
)
717 wxWindow
*page
= book
->GetPage(i
);
720 size
.IncTo(page
->GetSize());
729 void WidgetsFrame::OnGoToPage(wxCommandEvent
& event
)
732 m_book
->SetSelection(event
.GetId() - Widgets_GoToPage
);
734 m_book
->SetSelection(m_book
->GetPageCount()-1);
735 WidgetsBookCtrl
*book
= wxStaticCast(m_book
->GetCurrentPage(), WidgetsBookCtrl
);
736 book
->SetSelection(event
.GetId() - Widgets_GoToPage
);
742 void WidgetsFrame::OnSetTooltip(wxCommandEvent
& WXUNUSED(event
))
744 static wxString s_tip
= wxT("This is a tooltip");
746 wxTextEntryDialog dialog
749 wxT("Tooltip text (may use \\n, leave empty to remove): "),
750 wxT("Widgets sample"),
754 if ( dialog
.ShowModal() != wxID_OK
)
757 s_tip
= dialog
.GetValue();
758 s_tip
.Replace(wxT("\\n"), wxT("\n"));
760 WidgetsPage
*page
= CurrentPage();
762 const Widgets widgets
= page
->GetWidgets();
763 for ( Widgets::const_iterator it
= widgets
.begin();
767 (*it
)->SetToolTip(s_tip
);
771 #endif // wxUSE_TOOLTIPS
776 // Trivial wrapper for wxGetColourFromUser() which also does something even if
777 // the colour dialog is not available in the current build (which may happen
778 // for the ports in development and it is still useful to see how colours work)
779 wxColour
GetColourFromUser(wxWindow
*parent
, const wxColour
& colDefault
)
782 return wxGetColourFromUser(parent
, colDefault
);
783 #else // !wxUSE_COLOURDLG
784 if ( colDefault
== *wxBLACK
)
788 #endif // wxUSE_COLOURDLG/!wxUSE_COLOURDLG
791 } // anonymous namespace
793 void WidgetsFrame::OnSetFgCol(wxCommandEvent
& WXUNUSED(event
))
795 // allow for debugging the default colour the first time this is called
796 WidgetsPage
*page
= CurrentPage();
799 m_colFg
= page
->GetForegroundColour();
801 wxColour col
= GetColourFromUser(this, m_colFg
);
807 const Widgets widgets
= page
->GetWidgets();
808 for ( Widgets::const_iterator it
= widgets
.begin();
812 (*it
)->SetForegroundColour(m_colFg
);
817 void WidgetsFrame::OnSetBgCol(wxCommandEvent
& WXUNUSED(event
))
819 WidgetsPage
*page
= CurrentPage();
822 m_colBg
= page
->GetBackgroundColour();
824 wxColour col
= GetColourFromUser(this, m_colBg
);
830 const Widgets widgets
= page
->GetWidgets();
831 for ( Widgets::const_iterator it
= widgets
.begin();
835 (*it
)->SetBackgroundColour(m_colBg
);
840 void WidgetsFrame::OnSetPageBg(wxCommandEvent
& WXUNUSED(event
))
842 wxColour col
= GetColourFromUser(this, GetBackgroundColour());
846 CurrentPage()->SetBackgroundColour(col
);
847 CurrentPage()->Refresh();
850 void WidgetsFrame::OnSetFont(wxCommandEvent
& WXUNUSED(event
))
853 WidgetsPage
*page
= CurrentPage();
856 m_font
= page
->GetFont();
858 wxFont font
= wxGetFontFromUser(this, m_font
);
864 const Widgets widgets
= page
->GetWidgets();
865 for ( Widgets::const_iterator it
= widgets
.begin();
869 (*it
)->SetFont(m_font
);
873 wxLogMessage(wxT("Font selection dialog not available in current build."));
877 void WidgetsFrame::OnEnable(wxCommandEvent
& event
)
879 const Widgets widgets
= CurrentPage()->GetWidgets();
880 for ( Widgets::const_iterator it
= widgets
.begin();
884 (*it
)->Enable(event
.IsChecked());
888 void WidgetsFrame::OnSetBorder(wxCommandEvent
& event
)
891 switch ( event
.GetId() )
893 case Widgets_BorderNone
: border
= wxBORDER_NONE
; break;
894 case Widgets_BorderStatic
: border
= wxBORDER_STATIC
; break;
895 case Widgets_BorderSimple
: border
= wxBORDER_SIMPLE
; break;
896 case Widgets_BorderRaised
: border
= wxBORDER_RAISED
; break;
897 case Widgets_BorderSunken
: border
= wxBORDER_SUNKEN
; break;
898 case Widgets_BorderDouble
: border
= wxBORDER_DOUBLE
; break;
901 wxFAIL_MSG( wxT("unknown border style") );
904 case Widgets_BorderDefault
: border
= wxBORDER_DEFAULT
; break;
907 WidgetsPage::ms_defaultFlags
&= ~wxBORDER_MASK
;
908 WidgetsPage::ms_defaultFlags
|= border
;
910 WidgetsPage
*page
= CurrentPage();
912 page
->RecreateWidget();
915 void WidgetsFrame::OnToggleGlobalBusyCursor(wxCommandEvent
& event
)
917 if ( event
.IsChecked() )
923 void WidgetsFrame::OnToggleBusyCursor(wxCommandEvent
& event
)
925 wxCursor
cursor(*(event
.IsChecked() ? wxHOURGLASS_CURSOR
926 : wxSTANDARD_CURSOR
));
928 const Widgets widgets
= CurrentPage()->GetWidgets();
929 for ( Widgets::const_iterator it
= widgets
.begin();
933 (*it
)->SetCursor(cursor
);
937 void WidgetsFrame::OnDisableAutoComplete(wxCommandEvent
& WXUNUSED(event
))
939 wxTextEntryBase
*entry
= CurrentPage()->GetTextEntry();
940 wxCHECK_RET( entry
, "menu item should be disabled" );
942 if ( entry
->AutoComplete(wxArrayString()) )
944 wxLogMessage("Disabled auto completion.");
948 wxLogMessage("AutoComplete() failed.");
952 void WidgetsFrame::OnAutoCompleteFixed(wxCommandEvent
& WXUNUSED(event
))
954 wxTextEntryBase
*entry
= CurrentPage()->GetTextEntry();
955 wxCHECK_RET( entry
, "menu item should be disabled" );
957 wxArrayString completion_choices
;
959 // add a few strings so a completion occurs on any letter typed
960 for ( char idxc
= 'a'; idxc
< 'z'; ++idxc
)
961 completion_choices
.push_back(wxString::Format("%c%c", idxc
, idxc
));
963 completion_choices
.push_back("is this string for test?");
964 completion_choices
.push_back("this is a test string");
965 completion_choices
.push_back("this is another test string");
966 completion_choices
.push_back("this string is for test");
968 if ( entry
->AutoComplete(completion_choices
) )
970 wxLogMessage("Enabled auto completion of a set of fixed strings.");
974 wxLogMessage("AutoComplete() failed.");
978 void WidgetsFrame::OnAutoCompleteFilenames(wxCommandEvent
& WXUNUSED(event
))
980 wxTextEntryBase
*entry
= CurrentPage()->GetTextEntry();
981 wxCHECK_RET( entry
, "menu item should be disabled" );
983 if ( entry
->AutoCompleteFileNames() )
985 wxLogMessage("Enable auto completion of file names.");
989 wxLogMessage("AutoCompleteFileNames() failed.");
993 void WidgetsFrame::OnSetHint(wxCommandEvent
& WXUNUSED(event
))
995 wxTextEntryBase
*entry
= CurrentPage()->GetTextEntry();
996 wxCHECK_RET( entry
, "menu item should be disabled" );
998 static wxString
s_hint("Type here");
1000 hint
= wxGetTextFromUser("Text hint:", "Widgets sample", s_hint
, this);
1006 if ( entry
->SetHint(hint
) )
1008 wxLogMessage("Set hint to \"%s\".", hint
);
1012 wxLogMessage("Text hints not supported.");
1016 #endif // wxUSE_MENUS
1018 // ----------------------------------------------------------------------------
1020 // ----------------------------------------------------------------------------
1022 WidgetsPageInfo::WidgetsPageInfo(Constructor ctor
, const wxChar
*label
, int categories
)
1024 , m_categories(categories
)
1030 // dummy sorting: add and immediately sort in the list according to label
1031 if ( WidgetsPage::ms_widgetPages
)
1033 WidgetsPageInfo
*node_prev
= WidgetsPage::ms_widgetPages
;
1034 if ( wxStrcmp(label
, node_prev
->GetLabel().c_str()) < 0 )
1038 WidgetsPage::ms_widgetPages
= this;
1042 WidgetsPageInfo
*node_next
;
1045 node_next
= node_prev
->GetNext();
1048 // add if between two
1049 if ( wxStrcmp(label
, node_next
->GetLabel().c_str()) < 0 )
1051 node_prev
->SetNext(this);
1053 // force to break loop
1060 node_prev
->SetNext(this);
1063 node_prev
= node_next
;
1065 while ( node_next
);
1071 WidgetsPage::ms_widgetPages
= this;
1075 // ----------------------------------------------------------------------------
1077 // ----------------------------------------------------------------------------
1079 int WidgetsPage::ms_defaultFlags
= wxBORDER_DEFAULT
;
1080 WidgetsPageInfo
*WidgetsPage::ms_widgetPages
= NULL
;
1082 WidgetsPage::WidgetsPage(WidgetsBookCtrl
*book
,
1083 wxImageList
*imaglist
,
1084 const char *const icon
[])
1085 : wxPanel(book
, wxID_ANY
,
1086 wxDefaultPosition
, wxDefaultSize
,
1087 wxNO_FULL_REPAINT_ON_RESIZE
|
1091 #if USE_ICONS_IN_BOOK
1092 imaglist
->Add(wxBitmap(wxImage(icon
).Scale(ICON_SIZE
, ICON_SIZE
)));
1094 wxUnusedVar(imaglist
);
1099 wxSizer
*WidgetsPage::CreateSizerWithText(wxControl
*control
,
1101 wxTextCtrl
**ppText
)
1103 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
1104 wxTextCtrl
*text
= new wxTextCtrl(this, id
, wxEmptyString
,
1105 wxDefaultPosition
, wxDefaultSize
, wxTE_PROCESS_ENTER
);
1107 sizerRow
->Add(control
, 0, wxRIGHT
| wxALIGN_CENTRE_VERTICAL
, 5);
1108 sizerRow
->Add(text
, 1, wxLEFT
| wxALIGN_CENTRE_VERTICAL
, 5);
1116 // create a sizer containing a label and a text ctrl
1117 wxSizer
*WidgetsPage::CreateSizerWithTextAndLabel(const wxString
& label
,
1119 wxTextCtrl
**ppText
)
1121 return CreateSizerWithText(new wxStaticText(this, wxID_ANY
, label
),
1125 // create a sizer containing a button and a text ctrl
1126 wxSizer
*WidgetsPage::CreateSizerWithTextAndButton(wxWindowID idBtn
,
1127 const wxString
& label
,
1129 wxTextCtrl
**ppText
)
1131 return CreateSizerWithText(new wxButton(this, idBtn
, label
), id
, ppText
);
1134 wxCheckBox
*WidgetsPage::CreateCheckBoxAndAddToSizer(wxSizer
*sizer
,
1135 const wxString
& label
,
1138 wxCheckBox
*checkbox
= new wxCheckBox(this, id
, label
);
1139 sizer
->Add(checkbox
, 0, wxLEFT
| wxRIGHT
, 5);
1140 sizer
->Add(0, 2, 0, wxGROW
); // spacer