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"
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
)
364 const bool sizeSet
= wxPersistentRegisterAndRestore(this, "Main");
366 // set the frame icon
367 SetIcon(wxICON(sample
));
377 // create the menubar
378 wxMenuBar
*mbar
= new wxMenuBar
;
379 wxMenu
*menuWidget
= new wxMenu
;
381 menuWidget
->Append(Widgets_SetTooltip
, wxT("Set &tooltip...\tCtrl-T"));
382 menuWidget
->AppendSeparator();
383 #endif // wxUSE_TOOLTIPS
384 menuWidget
->Append(Widgets_SetFgColour
, wxT("Set &foreground...\tCtrl-F"));
385 menuWidget
->Append(Widgets_SetBgColour
, wxT("Set &background...\tCtrl-B"));
386 menuWidget
->Append(Widgets_SetPageBg
, wxT("Set &page background...\tShift-Ctrl-B"));
387 menuWidget
->Append(Widgets_SetFont
, wxT("Set f&ont...\tCtrl-O"));
388 menuWidget
->AppendCheckItem(Widgets_Enable
, wxT("&Enable/disable\tCtrl-E"));
390 wxMenu
*menuBorders
= new wxMenu
;
391 menuBorders
->AppendRadioItem(Widgets_BorderDefault
, wxT("De&fault\tCtrl-Shift-9"));
392 menuBorders
->AppendRadioItem(Widgets_BorderNone
, wxT("&None\tCtrl-Shift-0"));
393 menuBorders
->AppendRadioItem(Widgets_BorderSimple
, wxT("&Simple\tCtrl-Shift-1"));
394 menuBorders
->AppendRadioItem(Widgets_BorderDouble
, wxT("&Double\tCtrl-Shift-2"));
395 menuBorders
->AppendRadioItem(Widgets_BorderStatic
, wxT("Stati&c\tCtrl-Shift-3"));
396 menuBorders
->AppendRadioItem(Widgets_BorderRaised
, wxT("&Raised\tCtrl-Shift-4"));
397 menuBorders
->AppendRadioItem(Widgets_BorderSunken
, wxT("S&unken\tCtrl-Shift-5"));
398 menuWidget
->AppendSubMenu(menuBorders
, wxT("Set &border"));
400 menuWidget
->AppendSeparator();
401 menuWidget
->AppendCheckItem(Widgets_GlobalBusyCursor
,
402 wxT("Toggle &global busy cursor\tCtrl-Shift-U"));
403 menuWidget
->AppendCheckItem(Widgets_BusyCursor
,
404 wxT("Toggle b&usy cursor\tCtrl-U"));
406 menuWidget
->AppendSeparator();
407 menuWidget
->Append(wxID_EXIT
, wxT("&Quit\tCtrl-Q"));
408 mbar
->Append(menuWidget
, wxT("&Widget"));
410 wxMenu
*menuTextEntry
= new wxMenu
;
411 menuTextEntry
->AppendRadioItem(TextEntry_DisableAutoComplete
,
412 wxT("&Disable auto-completion"));
413 menuTextEntry
->AppendRadioItem(TextEntry_AutoCompleteFixed
,
414 wxT("Fixed-&list auto-completion"));
415 menuTextEntry
->AppendRadioItem(TextEntry_AutoCompleteFilenames
,
416 wxT("&Files names auto-completion"));
417 menuTextEntry
->AppendSeparator();
418 menuTextEntry
->Append(TextEntry_SetHint
, "Set help &hint");
420 mbar
->Append(menuTextEntry
, wxT("&Text"));
424 mbar
->Check(Widgets_Enable
, true);
425 #endif // wxUSE_MENUS
428 m_panel
= new wxPanel(this, wxID_ANY
);
430 wxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
432 // we have 2 panes: book with pages demonstrating the controls in the
433 // upper one and the log window with some buttons in the lower
435 int style
= wxBK_DEFAULT
;
436 // Uncomment to suppress page theme (draw in solid colour)
437 //style |= wxNB_NOPAGETHEME;
439 m_book
= new WidgetsBookCtrl(m_panel
, Widgets_BookCtrl
,
440 wxDefaultPosition
, wxDefaultSize
,
445 #ifndef __WXHANDHELD__
446 // the lower one only has the log listbox and a button to clear it
448 wxSizer
*sizerDown
= new wxStaticBoxSizer(
449 new wxStaticBox( m_panel
, wxID_ANY
, wxT("&Log window") ),
452 m_lboxLog
= new wxListBox(m_panel
, wxID_ANY
);
453 sizerDown
->Add(m_lboxLog
, 1, wxGROW
| wxALL
, 5);
454 sizerDown
->SetMinSize(100, 150);
456 wxSizer
*sizerDown
= new wxBoxSizer(wxVERTICAL
);
459 wxBoxSizer
*sizerBtns
= new wxBoxSizer(wxHORIZONTAL
);
462 btn
= new wxButton(m_panel
, Widgets_ClearLog
, wxT("Clear &log"));
464 sizerBtns
->Add(10, 0); // spacer
466 btn
= new wxButton(m_panel
, Widgets_Quit
, wxT("E&xit"));
468 sizerDown
->Add(sizerBtns
, 0, wxALL
| wxALIGN_RIGHT
, 5);
470 // put everything together
471 sizerTop
->Add(m_book
, 1, wxGROW
| (wxALL
& ~(wxTOP
| wxBOTTOM
)), 10);
472 sizerTop
->Add(0, 5, 0, wxGROW
); // spacer in between
473 sizerTop
->Add(sizerDown
, 0, wxGROW
| (wxALL
& ~wxTOP
), 10);
475 #else // !__WXHANDHELD__/__WXHANDHELD__
477 sizerTop
->Add(m_book
, 1, wxGROW
| wxALL
);
479 #endif // __WXHANDHELD__
481 m_panel
->SetSizer(sizerTop
);
483 const wxSize sizeMin
= m_panel
->GetBestSize();
485 SetClientSize(sizeMin
);
486 SetMinClientSize(sizeMin
);
488 #if USE_LOG && !defined(__WXCOCOA__)
489 // wxCocoa's listbox is too flakey to use for logging right now
490 // now that everything is created we can redirect the log messages to the
492 m_logTarget
= new LboxLogger(m_lboxLog
, wxLog::GetActiveTarget());
493 wxLog::SetActiveTarget(m_logTarget
);
497 void WidgetsFrame::InitBook()
499 #if USE_ICONS_IN_BOOK
500 wxImageList
*imageList
= new wxImageList(ICON_SIZE
, ICON_SIZE
);
502 wxImage
img(sample_xpm
);
503 imageList
->Add(wxBitmap(img
.Scale(ICON_SIZE
, ICON_SIZE
)));
505 wxImageList
*imageList
= NULL
;
509 WidgetsBookCtrl
*books
[MAX_PAGES
];
512 ArrayWidgetsPage pages
[MAX_PAGES
];
513 wxArrayString labels
[MAX_PAGES
];
515 wxMenu
*menuPages
= new wxMenu
;
516 unsigned int nPage
= 0, nFKey
= 0;
517 int cat
, imageId
= 1;
519 // we need to first create all pages and only then add them to the book
520 // as we need the image list first
522 // we also construct the pages menu during this first iteration
523 for ( cat
= 0; cat
< MAX_PAGES
; cat
++ )
526 nPage
++; // increase for parent page
528 books
[cat
] = new WidgetsBookCtrl(m_book
,
535 for ( WidgetsPageInfo
*info
= WidgetsPage::ms_widgetPages
;
537 info
= info
->GetNext() )
539 if( (info
->GetCategories() & ( 1 << cat
)) == 0)
542 WidgetsPage
*page
= (*info
->GetCtor())(
549 pages
[cat
].Add(page
);
551 labels
[cat
].Add(info
->GetLabel());
552 if ( cat
== ALL_PAGE
)
554 wxString
radioLabel(info
->GetLabel());
558 radioLabel
<< wxT("\tF" ) << nFKey
;
561 menuPages
->AppendRadioItem(
562 Widgets_GoToPage
+ nPage
,
566 // consider only for book in book architecture
572 // consider only for treebook architecture (with subpages)
578 GetMenuBar()->Append(menuPages
, wxT("&Page"));
580 #if USE_ICONS_IN_BOOK
581 m_book
->AssignImageList(imageList
);
584 for ( cat
= 0; cat
< MAX_PAGES
; cat
++ )
587 m_book
->AddPage(NULL
,WidgetsCategories
[cat
],false,0);
589 m_book
->AddPage(books
[cat
],WidgetsCategories
[cat
],false,0);
590 #if USE_ICONS_IN_BOOK
591 books
[cat
]->SetImageList(imageList
);
596 size_t count
= pages
[cat
].GetCount();
597 for ( size_t n
= 0; n
< count
; n
++ )
607 false, // don't select
614 wxEVT_COMMAND_WIDGETS_PAGE_CHANGED
,
615 wxWidgetsbookEventHandler(WidgetsFrame::OnPageChanged
) );
617 const bool pageSet
= wxPersistentRegisterAndRestore(m_book
);
620 // for treebook page #0 is empty parent page only so select the first page
621 // with some contents
623 m_book
->SetSelection(1);
625 // but ensure that the top of the tree is shown nevertheless
626 wxTreeCtrl
* const tree
= m_book
->GetTreeCtrl();
628 wxTreeItemIdValue cookie
;
629 tree
->EnsureVisible(tree
->GetFirstChild(tree
->GetRootItem(), cookie
));
633 // for other books set selection twice to force connected event handler
634 // to force lazy creation of initial visible content
635 m_book
->SetSelection(1);
636 m_book
->SetSelection(0);
638 #endif // USE_TREEBOOK
641 WidgetsPage
*WidgetsFrame::CurrentPage()
643 wxWindow
*page
= m_book
->GetCurrentPage();
646 WidgetsBookCtrl
*subBook
= wxStaticCast(page
, WidgetsBookCtrl
);
647 wxCHECK_MSG( subBook
, NULL
, wxT("no WidgetsBookCtrl?") );
649 page
= subBook
->GetCurrentPage();
650 #endif // !USE_TREEBOOK
652 return wxStaticCast(page
, WidgetsPage
);
655 WidgetsFrame::~WidgetsFrame()
662 // ----------------------------------------------------------------------------
663 // WidgetsFrame event handlers
664 // ----------------------------------------------------------------------------
666 void WidgetsFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
672 void WidgetsFrame::OnButtonClearLog(wxCommandEvent
& WXUNUSED(event
))
680 void WidgetsFrame::OnPageChanging(WidgetsBookCtrlEvent
& event
)
683 // don't allow selection of entries without pages (categories)
684 if ( !m_book
->GetPage(event
.GetSelection()) )
691 void WidgetsFrame::OnPageChanged(WidgetsBookCtrlEvent
& event
)
693 const int sel
= event
.GetSelection();
695 // adjust "Page" menu selection
696 wxMenuItem
*item
= GetMenuBar()->FindItem(Widgets_GoToPage
+ sel
);
700 GetMenuBar()->Check(Widgets_BusyCursor
, false);
702 // create the pages on demand, otherwise the sample startup is too slow as
703 // it creates hundreds of controls
704 WidgetsPage
*page
= CurrentPage();
705 if ( page
->GetChildren().empty() )
707 wxWindowUpdateLocker
noUpdates(page
);
708 page
->CreateContent();
710 page
->GetSizer()->Fit(page
);
712 WidgetsBookCtrl
*book
= wxStaticCast(page
->GetParent(), WidgetsBookCtrl
);
714 for ( size_t i
= 0; i
< book
->GetPageCount(); ++i
)
716 wxWindow
*page
= book
->GetPage(i
);
719 size
.IncTo(page
->GetSize());
728 void WidgetsFrame::OnGoToPage(wxCommandEvent
& event
)
731 m_book
->SetSelection(event
.GetId() - Widgets_GoToPage
);
733 m_book
->SetSelection(m_book
->GetPageCount()-1);
734 WidgetsBookCtrl
*book
= wxStaticCast(m_book
->GetCurrentPage(), WidgetsBookCtrl
);
735 book
->SetSelection(event
.GetId() - Widgets_GoToPage
);
741 void WidgetsFrame::OnSetTooltip(wxCommandEvent
& WXUNUSED(event
))
743 static wxString s_tip
= wxT("This is a tooltip");
745 wxTextEntryDialog dialog
748 wxT("Tooltip text (may use \\n, leave empty to remove): "),
749 wxT("Widgets sample"),
753 if ( dialog
.ShowModal() != wxID_OK
)
756 s_tip
= dialog
.GetValue();
757 s_tip
.Replace(wxT("\\n"), wxT("\n"));
759 WidgetsPage
*page
= CurrentPage();
761 const Widgets widgets
= page
->GetWidgets();
762 for ( Widgets::const_iterator it
= widgets
.begin();
766 (*it
)->SetToolTip(s_tip
);
770 #endif // wxUSE_TOOLTIPS
775 // Trivial wrapper for wxGetColourFromUser() which also does something even if
776 // the colour dialog is not available in the current build (which may happen
777 // for the ports in development and it is still useful to see how colours work)
778 wxColour
GetColourFromUser(wxWindow
*parent
, const wxColour
& colDefault
)
781 return wxGetColourFromUser(parent
, colDefault
);
782 #else // !wxUSE_COLOURDLG
783 if ( colDefault
== *wxBLACK
)
787 #endif // wxUSE_COLOURDLG/!wxUSE_COLOURDLG
790 } // anonymous namespace
792 void WidgetsFrame::OnSetFgCol(wxCommandEvent
& WXUNUSED(event
))
794 // allow for debugging the default colour the first time this is called
795 WidgetsPage
*page
= CurrentPage();
798 m_colFg
= page
->GetForegroundColour();
800 wxColour col
= GetColourFromUser(this, m_colFg
);
806 const Widgets widgets
= page
->GetWidgets();
807 for ( Widgets::const_iterator it
= widgets
.begin();
811 (*it
)->SetForegroundColour(m_colFg
);
816 void WidgetsFrame::OnSetBgCol(wxCommandEvent
& WXUNUSED(event
))
818 WidgetsPage
*page
= CurrentPage();
821 m_colBg
= page
->GetBackgroundColour();
823 wxColour col
= GetColourFromUser(this, m_colBg
);
829 const Widgets widgets
= page
->GetWidgets();
830 for ( Widgets::const_iterator it
= widgets
.begin();
834 (*it
)->SetBackgroundColour(m_colBg
);
839 void WidgetsFrame::OnSetPageBg(wxCommandEvent
& WXUNUSED(event
))
841 wxColour col
= GetColourFromUser(this, GetBackgroundColour());
845 CurrentPage()->SetBackgroundColour(col
);
846 CurrentPage()->Refresh();
849 void WidgetsFrame::OnSetFont(wxCommandEvent
& WXUNUSED(event
))
852 WidgetsPage
*page
= CurrentPage();
855 m_font
= page
->GetFont();
857 wxFont font
= wxGetFontFromUser(this, m_font
);
863 const Widgets widgets
= page
->GetWidgets();
864 for ( Widgets::const_iterator it
= widgets
.begin();
868 (*it
)->SetFont(m_font
);
872 wxLogMessage(wxT("Font selection dialog not available in current build."));
876 void WidgetsFrame::OnEnable(wxCommandEvent
& event
)
878 const Widgets widgets
= CurrentPage()->GetWidgets();
879 for ( Widgets::const_iterator it
= widgets
.begin();
883 (*it
)->Enable(event
.IsChecked());
887 void WidgetsFrame::OnSetBorder(wxCommandEvent
& event
)
890 switch ( event
.GetId() )
892 case Widgets_BorderNone
: border
= wxBORDER_NONE
; break;
893 case Widgets_BorderStatic
: border
= wxBORDER_STATIC
; break;
894 case Widgets_BorderSimple
: border
= wxBORDER_SIMPLE
; break;
895 case Widgets_BorderRaised
: border
= wxBORDER_RAISED
; break;
896 case Widgets_BorderSunken
: border
= wxBORDER_SUNKEN
; break;
897 case Widgets_BorderDouble
: border
= wxBORDER_DOUBLE
; break;
900 wxFAIL_MSG( wxT("unknown border style") );
903 case Widgets_BorderDefault
: border
= wxBORDER_DEFAULT
; break;
906 WidgetsPage::ms_defaultFlags
&= ~wxBORDER_MASK
;
907 WidgetsPage::ms_defaultFlags
|= border
;
909 WidgetsPage
*page
= CurrentPage();
911 page
->RecreateWidget();
914 void WidgetsFrame::OnToggleGlobalBusyCursor(wxCommandEvent
& event
)
916 if ( event
.IsChecked() )
922 void WidgetsFrame::OnToggleBusyCursor(wxCommandEvent
& event
)
924 wxCursor
cursor(*(event
.IsChecked() ? wxHOURGLASS_CURSOR
925 : wxSTANDARD_CURSOR
));
927 const Widgets widgets
= CurrentPage()->GetWidgets();
928 for ( Widgets::const_iterator it
= widgets
.begin();
932 (*it
)->SetCursor(cursor
);
936 void WidgetsFrame::OnDisableAutoComplete(wxCommandEvent
& WXUNUSED(event
))
938 wxTextEntryBase
*entry
= CurrentPage()->GetTextEntry();
939 wxCHECK_RET( entry
, "menu item should be disabled" );
941 if ( entry
->AutoComplete(wxArrayString()) )
943 wxLogMessage("Disabled auto completion.");
947 wxLogMessage("AutoComplete() failed.");
951 void WidgetsFrame::OnAutoCompleteFixed(wxCommandEvent
& WXUNUSED(event
))
953 wxTextEntryBase
*entry
= CurrentPage()->GetTextEntry();
954 wxCHECK_RET( entry
, "menu item should be disabled" );
956 wxArrayString completion_choices
;
958 // add a few strings so a completion occurs on any letter typed
959 for ( char idxc
= 'a'; idxc
< 'z'; ++idxc
)
960 completion_choices
.push_back(wxString::Format("%c%c", idxc
, idxc
));
962 completion_choices
.push_back("is this string for test?");
963 completion_choices
.push_back("this is a test string");
964 completion_choices
.push_back("this is another test string");
965 completion_choices
.push_back("this string is for test");
967 if ( entry
->AutoComplete(completion_choices
) )
969 wxLogMessage("Enabled auto completion of a set of fixed strings.");
973 wxLogMessage("AutoComplete() failed.");
977 void WidgetsFrame::OnAutoCompleteFilenames(wxCommandEvent
& WXUNUSED(event
))
979 wxTextEntryBase
*entry
= CurrentPage()->GetTextEntry();
980 wxCHECK_RET( entry
, "menu item should be disabled" );
982 if ( entry
->AutoCompleteFileNames() )
984 wxLogMessage("Enable auto completion of file names.");
988 wxLogMessage("AutoCompleteFileNames() failed.");
992 void WidgetsFrame::OnSetHint(wxCommandEvent
& WXUNUSED(event
))
994 wxTextEntryBase
*entry
= CurrentPage()->GetTextEntry();
995 wxCHECK_RET( entry
, "menu item should be disabled" );
997 static wxString
s_hint("Type here");
999 hint
= wxGetTextFromUser("Text hint:", "Widgets sample", s_hint
, this);
1005 if ( entry
->SetHint(hint
) )
1007 wxLogMessage("Set hint to \"%s\".", hint
);
1011 wxLogMessage("Text hints not supported.");
1015 #endif // wxUSE_MENUS
1017 // ----------------------------------------------------------------------------
1019 // ----------------------------------------------------------------------------
1021 WidgetsPageInfo::WidgetsPageInfo(Constructor ctor
, const wxChar
*label
, int categories
)
1023 , m_categories(categories
)
1029 // dummy sorting: add and immediately sort in the list according to label
1030 if ( WidgetsPage::ms_widgetPages
)
1032 WidgetsPageInfo
*node_prev
= WidgetsPage::ms_widgetPages
;
1033 if ( wxStrcmp(label
, node_prev
->GetLabel().c_str()) < 0 )
1037 WidgetsPage::ms_widgetPages
= this;
1041 WidgetsPageInfo
*node_next
;
1044 node_next
= node_prev
->GetNext();
1047 // add if between two
1048 if ( wxStrcmp(label
, node_next
->GetLabel().c_str()) < 0 )
1050 node_prev
->SetNext(this);
1052 // force to break loop
1059 node_prev
->SetNext(this);
1062 node_prev
= node_next
;
1064 while ( node_next
);
1070 WidgetsPage::ms_widgetPages
= this;
1074 // ----------------------------------------------------------------------------
1076 // ----------------------------------------------------------------------------
1078 int WidgetsPage::ms_defaultFlags
= wxBORDER_DEFAULT
;
1079 WidgetsPageInfo
*WidgetsPage::ms_widgetPages
= NULL
;
1081 WidgetsPage::WidgetsPage(WidgetsBookCtrl
*book
,
1082 wxImageList
*imaglist
,
1083 const char *const icon
[])
1084 : wxPanel(book
, wxID_ANY
,
1085 wxDefaultPosition
, wxDefaultSize
,
1086 wxNO_FULL_REPAINT_ON_RESIZE
|
1090 #if USE_ICONS_IN_BOOK
1091 imaglist
->Add(wxBitmap(wxImage(icon
).Scale(ICON_SIZE
, ICON_SIZE
)));
1093 wxUnusedVar(imaglist
);
1098 wxSizer
*WidgetsPage::CreateSizerWithText(wxControl
*control
,
1100 wxTextCtrl
**ppText
)
1102 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
1103 wxTextCtrl
*text
= new wxTextCtrl(this, id
, wxEmptyString
,
1104 wxDefaultPosition
, wxDefaultSize
, wxTE_PROCESS_ENTER
);
1106 sizerRow
->Add(control
, 0, wxRIGHT
| wxALIGN_CENTRE_VERTICAL
, 5);
1107 sizerRow
->Add(text
, 1, wxLEFT
| wxALIGN_CENTRE_VERTICAL
, 5);
1115 // create a sizer containing a label and a text ctrl
1116 wxSizer
*WidgetsPage::CreateSizerWithTextAndLabel(const wxString
& label
,
1118 wxTextCtrl
**ppText
)
1120 return CreateSizerWithText(new wxStaticText(this, wxID_ANY
, label
),
1124 // create a sizer containing a button and a text ctrl
1125 wxSizer
*WidgetsPage::CreateSizerWithTextAndButton(wxWindowID idBtn
,
1126 const wxString
& label
,
1128 wxTextCtrl
**ppText
)
1130 return CreateSizerWithText(new wxButton(this, idBtn
, label
), id
, ppText
);
1133 wxCheckBox
*WidgetsPage::CreateCheckBoxAndAddToSizer(wxSizer
*sizer
,
1134 const wxString
& label
,
1137 wxCheckBox
*checkbox
= new wxCheckBox(this, id
, label
);
1138 sizer
->Add(checkbox
, 0, wxLEFT
| wxRIGHT
, 5);
1139 sizer
->Add(0, 2, 0, wxGROW
); // spacer