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
34 #include "wx/button.h"
35 #include "wx/checkbox.h"
36 #include "wx/listbox.h"
37 #include "wx/statbox.h"
38 #include "wx/stattext.h"
39 #include "wx/textctrl.h"
40 #include "wx/msgdlg.h"
43 #include "wx/sysopt.h"
44 #include "wx/bookctrl.h"
45 #include "wx/treebook.h"
47 #include "wx/colordlg.h"
48 #include "wx/fontdlg.h"
49 #include "wx/textdlg.h"
50 #include "wx/imaglist.h"
54 #include "../sample.xpm"
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
63 Widgets_ClearLog
= 100,
70 #endif // wxUSE_TOOLTIPS
82 Widgets_BorderDefault
,
85 Widgets_GoToPageLast
= Widgets_GoToPage
+ 100
88 const wxChar
*WidgetsCategories
[MAX_PAGES
] = {
89 #if defined(__WXUNIVERSAL__)
103 // ----------------------------------------------------------------------------
105 // ----------------------------------------------------------------------------
107 // Define a new application type, each program should derive a class from wxApp
108 class WidgetsApp
: public wxApp
111 // override base class virtuals
112 // ----------------------------
114 // this one is called on application startup and is a good place for the app
115 // initialization (doing it here and not in the ctor allows to have an error
116 // return: if OnInit() returns false, the application terminates)
117 virtual bool OnInit();
120 // Define a new frame type: this is going to be our main frame
121 class WidgetsFrame
: public wxFrame
125 WidgetsFrame(const wxString
& title
);
126 virtual ~WidgetsFrame();
131 void OnButtonClearLog(wxCommandEvent
& event
);
133 void OnExit(wxCommandEvent
& event
);
136 void OnPageChanged(WidgetsBookCtrlEvent
& event
);
137 void OnGoToPage(wxCommandEvent
& event
);
140 void OnSetTooltip(wxCommandEvent
& event
);
141 #endif // wxUSE_TOOLTIPS
142 void OnSetFgCol(wxCommandEvent
& event
);
143 void OnSetBgCol(wxCommandEvent
& event
);
144 void OnSetFont(wxCommandEvent
& event
);
145 void OnEnable(wxCommandEvent
& event
);
146 void OnSetBorder(wxCommandEvent
& event
);
147 #endif // wxUSE_MENUS
149 // initialize the book: add all pages to it
152 // finding current page assuming book inside book
153 WidgetsPage
*CurrentPage();
156 // the panel containing everything
160 // the listbox for logging messages
161 wxListBox
*m_lboxLog
;
163 // the log target we use to redirect messages to the listbox
167 // the book containing the test pages
168 WidgetsBookCtrl
*m_book
;
171 // last chosen fg/bg colours and font
175 #endif // wxUSE_MENUS
177 // any class wishing to process wxWidgets events must use this macro
178 DECLARE_EVENT_TABLE()
182 // A log target which just redirects the messages to a listbox
183 class LboxLogger
: public wxLog
186 LboxLogger(wxListBox
*lbox
, wxLog
*logOld
)
189 //m_lbox->Disable(); -- looks ugly under MSW
193 virtual ~LboxLogger()
195 wxLog::SetActiveTarget(m_logOld
);
199 // implement sink functions
200 virtual void DoLog(wxLogLevel level
, const wxChar
*szString
, time_t t
)
202 // don't put trace messages into listbox or we can get into infinite
204 if ( level
== wxLOG_Trace
)
208 // cast is needed to call protected method
209 ((LboxLogger
*)m_logOld
)->DoLog(level
, szString
, t
);
214 wxLog::DoLog(level
, szString
, t
);
218 virtual void DoLogString(const wxChar
*szString
, time_t WXUNUSED(t
))
224 #ifdef __WXUNIVERSAL__
225 m_lbox
->AppendAndEnsureVisible(msg
);
226 #else // other ports don't have this method yet
228 m_lbox
->SetFirstItem(m_lbox
->GetCount() - 1);
232 // the control we use
235 // the old log target
241 WX_DEFINE_ARRAY_PTR(WidgetsPage
*, ArrayWidgetsPage
);
243 // ----------------------------------------------------------------------------
245 // ----------------------------------------------------------------------------
247 IMPLEMENT_APP(WidgetsApp
)
249 // ----------------------------------------------------------------------------
251 // ----------------------------------------------------------------------------
253 BEGIN_EVENT_TABLE(WidgetsFrame
, wxFrame
)
255 EVT_BUTTON(Widgets_ClearLog
, WidgetsFrame::OnButtonClearLog
)
257 EVT_BUTTON(Widgets_Quit
, WidgetsFrame::OnExit
)
260 EVT_MENU(Widgets_SetTooltip
, WidgetsFrame::OnSetTooltip
)
261 #endif // wxUSE_TOOLTIPS
264 EVT_WIDGETS_PAGE_CHANGED(wxID_ANY
, WidgetsFrame::OnPageChanged
)
265 EVT_MENU_RANGE(Widgets_GoToPage
, Widgets_GoToPageLast
,
266 WidgetsFrame::OnGoToPage
)
268 EVT_MENU(Widgets_SetFgColour
, WidgetsFrame::OnSetFgCol
)
269 EVT_MENU(Widgets_SetBgColour
, WidgetsFrame::OnSetBgCol
)
270 EVT_MENU(Widgets_SetFont
, WidgetsFrame::OnSetFont
)
271 EVT_MENU(Widgets_Enable
, WidgetsFrame::OnEnable
)
273 EVT_MENU_RANGE(Widgets_BorderNone
, Widgets_BorderDefault
,
274 WidgetsFrame::OnSetBorder
)
276 EVT_MENU(wxID_EXIT
, WidgetsFrame::OnExit
)
277 #endif // wxUSE_MENUS
280 // ============================================================================
282 // ============================================================================
284 // ----------------------------------------------------------------------------
286 // ----------------------------------------------------------------------------
288 bool WidgetsApp::OnInit()
290 if ( !wxApp::OnInit() )
293 // the reason for having these ifdef's is that I often run two copies of
294 // this sample side by side and it is useful to see which one is which
296 #if defined(__WXUNIVERSAL__)
297 title
= _T("wxUniv/");
300 #if defined(__WXMSW__)
301 title
+= _T("wxMSW");
302 #elif defined(__WXGTK__)
303 title
+= _T("wxGTK");
304 #elif defined(__WXMAC__)
305 title
+= _T("wxMAC");
306 #elif defined(__WXMOTIF__)
307 title
+= _T("wxMOTIF");
309 title
+= _T("wxWidgets");
312 wxFrame
*frame
= new WidgetsFrame(title
+ _T(" widgets demo"));
315 //wxLog::AddTraceMask(_T("listbox"));
316 //wxLog::AddTraceMask(_T("scrollbar"));
317 //wxLog::AddTraceMask(_T("focus"));
322 // ----------------------------------------------------------------------------
323 // WidgetsFrame construction
324 // ----------------------------------------------------------------------------
326 WidgetsFrame::WidgetsFrame(const wxString
& title
)
327 : wxFrame(NULL
, wxID_ANY
, title
,
328 wxDefaultPosition
, wxDefaultSize
,
329 wxDEFAULT_FRAME_STYLE
|
330 wxNO_FULL_REPAINT_ON_RESIZE
|
334 // set the frame icon
335 SetIcon(wxICON(sample
));
339 m_lboxLog
= (wxListBox
*)NULL
;
340 m_logTarget
= (wxLog
*)NULL
;
342 m_book
= (WidgetsBookCtrl
*)NULL
;
345 // create the menubar
346 wxMenuBar
*mbar
= new wxMenuBar
;
347 wxMenu
*menuWidget
= new wxMenu
;
349 menuWidget
->Append(Widgets_SetTooltip
, _T("Set &tooltip...\tCtrl-T"));
350 menuWidget
->AppendSeparator();
351 #endif // wxUSE_TOOLTIPS
352 menuWidget
->Append(Widgets_SetFgColour
, _T("Set &foreground...\tCtrl-F"));
353 menuWidget
->Append(Widgets_SetBgColour
, _T("Set &background...\tCtrl-B"));
354 menuWidget
->Append(Widgets_SetFont
, _T("Set f&ont...\tCtrl-O"));
355 menuWidget
->AppendCheckItem(Widgets_Enable
, _T("&Enable/disable\tCtrl-E"));
357 wxMenu
*menuBorders
= new wxMenu
;
358 menuBorders
->AppendRadioItem(Widgets_BorderDefault
, _T("De&fault\tCtrl-Shift-9"));
359 menuBorders
->AppendRadioItem(Widgets_BorderNone
, _T("&None\tCtrl-Shift-0"));
360 menuBorders
->AppendRadioItem(Widgets_BorderSimple
, _T("&Simple\tCtrl-Shift-1"));
361 menuBorders
->AppendRadioItem(Widgets_BorderDouble
, _T("&Double\tCtrl-Shift-2"));
362 menuBorders
->AppendRadioItem(Widgets_BorderStatic
, _T("Stati&c\tCtrl-Shift-3"));
363 menuBorders
->AppendRadioItem(Widgets_BorderRaised
, _T("&Raised\tCtrl-Shift-4"));
364 menuBorders
->AppendRadioItem(Widgets_BorderSunken
, _T("S&unken\tCtrl-Shift-5"));
365 menuWidget
->AppendSubMenu(menuBorders
, _T("Set &border"));
367 menuWidget
->AppendSeparator();
368 menuWidget
->Append(wxID_EXIT
, _T("&Quit\tCtrl-Q"));
369 mbar
->Append(menuWidget
, _T("&Widget"));
372 mbar
->Check(Widgets_Enable
, true);
373 #endif // wxUSE_MENUS
376 m_panel
= new wxPanel(this, wxID_ANY
,
377 wxDefaultPosition
, wxDefaultSize
, wxCLIP_CHILDREN
);
379 wxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
381 // we have 2 panes: book with pages demonstrating the controls in the
382 // upper one and the log window with some buttons in the lower
384 int style
= wxNO_FULL_REPAINT_ON_RESIZE
|wxCLIP_CHILDREN
|WidgetBookStyle
;
385 // Uncomment to suppress page theme (draw in solid colour)
386 //style |= wxNB_NOPAGETHEME;
388 m_book
= new WidgetsBookCtrl(m_panel
, Widgets_BookCtrl
, wxDefaultPosition
,
390 wxSize(500, wxDefaultCoord
), // under Motif, height is a function of the width...
397 #ifndef __WXHANDHELD__
398 // the lower one only has the log listbox and a button to clear it
400 wxSizer
*sizerDown
= new wxStaticBoxSizer(
401 new wxStaticBox( m_panel
, wxID_ANY
, _T("&Log window") ),
404 m_lboxLog
= new wxListBox(m_panel
, wxID_ANY
);
405 sizerDown
->Add(m_lboxLog
, 1, wxGROW
| wxALL
, 5);
406 sizerDown
->SetMinSize(100, 150);
408 wxSizer
*sizerDown
= new wxBoxSizer(wxVERTICAL
);
411 wxBoxSizer
*sizerBtns
= new wxBoxSizer(wxHORIZONTAL
);
414 btn
= new wxButton(m_panel
, Widgets_ClearLog
, _T("Clear &log"));
416 sizerBtns
->Add(10, 0); // spacer
418 btn
= new wxButton(m_panel
, Widgets_Quit
, _T("E&xit"));
420 sizerDown
->Add(sizerBtns
, 0, wxALL
| wxALIGN_RIGHT
, 5);
422 // put everything together
423 sizerTop
->Add(m_book
, 1, wxGROW
| (wxALL
& ~(wxTOP
| wxBOTTOM
)), 10);
424 sizerTop
->Add(0, 5, 0, wxGROW
); // spacer in between
425 sizerTop
->Add(sizerDown
, 0, wxGROW
| (wxALL
& ~wxTOP
), 10);
427 #else // !__WXHANDHELD__/__WXHANDHELD__
429 sizerTop
->Add(m_book
, 1, wxGROW
| wxALL
);
431 #endif // __WXHANDHELD__
433 m_panel
->SetSizer(sizerTop
);
436 sizerTop
->SetSizeHints(this);
438 #if USE_LOG && !defined(__WXCOCOA__)
439 // wxCocoa's listbox is too flakey to use for logging right now
440 // now that everything is created we can redirect the log messages to the
442 m_logTarget
= new LboxLogger(m_lboxLog
, wxLog::GetActiveTarget());
443 wxLog::SetActiveTarget(m_logTarget
);
447 void WidgetsFrame::InitBook()
449 #if USE_ICONS_IN_BOOK
450 wxImageList
*imageList
= new wxImageList(32, 32);
452 imageList
->Add(wxBitmap(sample_xpm
));
454 wxImageList
*imageList
= NULL
;
458 WidgetsBookCtrl
*books
[MAX_PAGES
];
461 ArrayWidgetsPage pages
[MAX_PAGES
];
462 wxArrayString labels
[MAX_PAGES
];
464 wxMenu
*menuPages
= new wxMenu
;
465 unsigned int nPage
= 0, nFKey
= 0;
466 int cat
, imageId
= 1;
468 // we need to first create all pages and only then add them to the book
469 // as we need the image list first
471 // we also construct the pages menu during this first iteration
472 for ( cat
= 0; cat
< MAX_PAGES
; cat
++ )
475 nPage
++; // increase for parent page
477 books
[cat
] = new WidgetsBookCtrl(m_book
,
484 for ( WidgetsPageInfo
*info
= WidgetsPage::ms_widgetPages
;
486 info
= info
->GetNext() )
488 if( (info
->GetCategories() & ( 1 << cat
)) == 0)
491 WidgetsPage
*page
= (*info
->GetCtor())(
498 pages
[cat
].Add(page
);
500 labels
[cat
].Add(info
->GetLabel());
501 if ( cat
== ALL_PAGE
)
503 wxString
radioLabel(info
->GetLabel());
507 radioLabel
<< wxT("\tF" ) << nFKey
;
510 menuPages
->AppendRadioItem(
511 Widgets_GoToPage
+ nPage
,
515 // consider only for book in book architecture
521 // consider only for treebook architecture (with subpages)
527 GetMenuBar()->Append(menuPages
, _T("&Page"));
529 #if USE_ICONS_IN_BOOK
530 m_book
->AssignImageList(imageList
);
533 for ( cat
= 0; cat
< MAX_PAGES
; cat
++ )
536 m_book
->AddPage(NULL
,WidgetsCategories
[cat
],false,0);
538 m_book
->AddPage(books
[cat
],WidgetsCategories
[cat
],false,0);
539 #if USE_ICONS_IN_BOOK
540 books
[cat
]->SetImageList(imageList
);
545 size_t count
= pages
[cat
].GetCount();
546 for ( size_t n
= 0; n
< count
; n
++ )
555 false, // don't select
562 // for treebook page #0 is empty parent page only
563 m_book
->SetSelection(1);
564 m_book
->SetSelection(0);
568 WidgetsPage
*WidgetsFrame::CurrentPage()
571 return wxStaticCast(m_book
->GetCurrentPage(), WidgetsPage
);
573 WidgetsBookCtrl
*book
= wxStaticCast(m_book
->GetCurrentPage(), WidgetsBookCtrl
);
574 if (!book
) return NULL
;
575 return wxStaticCast(book
->GetCurrentPage(), WidgetsPage
);
579 WidgetsFrame::~WidgetsFrame()
586 // ----------------------------------------------------------------------------
587 // WidgetsFrame event handlers
588 // ----------------------------------------------------------------------------
590 void WidgetsFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
596 void WidgetsFrame::OnButtonClearLog(wxCommandEvent
& WXUNUSED(event
))
604 void WidgetsFrame::OnPageChanged(WidgetsBookCtrlEvent
& event
)
606 wxMenuItem
*item
= GetMenuBar()->FindItem(Widgets_GoToPage
+ event
.GetSelection());
607 if (item
) item
->Check();
611 void WidgetsFrame::OnGoToPage(wxCommandEvent
& event
)
614 m_book
->SetSelection(event
.GetId() - Widgets_GoToPage
);
616 m_book
->SetSelection(m_book
->GetPageCount()-1);
617 WidgetsBookCtrl
*book
= wxStaticCast(m_book
->GetCurrentPage(), WidgetsBookCtrl
);
618 book
->SetSelection(event
.GetId() - Widgets_GoToPage
);
624 void WidgetsFrame::OnSetTooltip(wxCommandEvent
& WXUNUSED(event
))
626 static wxString s_tip
= _T("This is a tooltip");
628 wxTextEntryDialog dialog
631 _T("Tooltip text (may use \\n, leave empty to remove): "),
632 _T("Widgets sample"),
636 if ( dialog
.ShowModal() != wxID_OK
)
639 s_tip
= dialog
.GetValue();
640 s_tip
.Replace(_T("\\n"), _T("\n"));
642 WidgetsPage
*page
= CurrentPage();
643 page
->GetWidget()->SetToolTip(s_tip
);
645 wxControl
*ctrl2
= page
->GetWidget2();
647 ctrl2
->SetToolTip(s_tip
);
650 #endif // wxUSE_TOOLTIPS
652 void WidgetsFrame::OnSetFgCol(wxCommandEvent
& WXUNUSED(event
))
655 // allow for debugging the default colour the first time this is called
656 WidgetsPage
*page
= CurrentPage();
658 m_colFg
= page
->GetForegroundColour();
660 wxColour col
= wxGetColourFromUser(this, m_colFg
);
666 page
->GetWidget()->SetForegroundColour(m_colFg
);
667 page
->GetWidget()->Refresh();
669 wxControl
*ctrl2
= page
->GetWidget2();
672 ctrl2
->SetForegroundColour(m_colFg
);
676 wxLogMessage(_T("Colour selection dialog not available in current build."));
680 void WidgetsFrame::OnSetBgCol(wxCommandEvent
& WXUNUSED(event
))
683 WidgetsPage
*page
= CurrentPage();
685 m_colBg
= page
->GetBackgroundColour();
687 wxColour col
= wxGetColourFromUser(this, m_colBg
);
693 page
->GetWidget()->SetBackgroundColour(m_colBg
);
694 page
->GetWidget()->Refresh();
696 wxControl
*ctrl2
= page
->GetWidget2();
699 ctrl2
->SetBackgroundColour(m_colFg
);
703 wxLogMessage(_T("Colour selection dialog not available in current build."));
707 void WidgetsFrame::OnSetFont(wxCommandEvent
& WXUNUSED(event
))
710 WidgetsPage
*page
= CurrentPage();
712 m_font
= page
->GetFont();
714 wxFont font
= wxGetFontFromUser(this, m_font
);
720 page
->GetWidget()->SetFont(m_font
);
721 page
->GetWidget()->Refresh();
723 wxControl
*ctrl2
= page
->GetWidget2();
726 ctrl2
->SetFont(m_font
);
730 wxLogMessage(_T("Font selection dialog not available in current build."));
734 void WidgetsFrame::OnEnable(wxCommandEvent
& event
)
736 WidgetsPage
*page
= CurrentPage();
737 page
->GetWidget()->Enable(event
.IsChecked());
740 void WidgetsFrame::OnSetBorder(wxCommandEvent
& event
)
743 switch ( event
.GetId() )
745 case Widgets_BorderNone
: border
= wxBORDER_NONE
; break;
746 case Widgets_BorderStatic
: border
= wxBORDER_STATIC
; break;
747 case Widgets_BorderSimple
: border
= wxBORDER_SIMPLE
; break;
748 case Widgets_BorderRaised
: border
= wxBORDER_RAISED
; break;
749 case Widgets_BorderSunken
: border
= wxBORDER_SUNKEN
; break;
750 case Widgets_BorderDouble
: border
= wxBORDER_DOUBLE
; break;
753 wxFAIL_MSG( _T("unknown border style") );
756 case Widgets_BorderDefault
: border
= wxBORDER_DEFAULT
; break;
759 WidgetsPage::ms_defaultFlags
&= ~wxBORDER_MASK
;
760 WidgetsPage::ms_defaultFlags
|= border
;
762 WidgetsPage
*page
= CurrentPage();
763 page
->RecreateWidget();
766 #endif // wxUSE_MENUS
768 // ----------------------------------------------------------------------------
770 // ----------------------------------------------------------------------------
772 WidgetsPageInfo::WidgetsPageInfo(Constructor ctor
, const wxChar
*label
, int categories
)
774 , m_categories(categories
)
780 // dummy sorting: add and immediately sort in the list according to label
781 if ( WidgetsPage::ms_widgetPages
)
783 WidgetsPageInfo
*node_prev
= WidgetsPage::ms_widgetPages
;
784 if ( wxStrcmp(label
, node_prev
->GetLabel().c_str()) < 0 )
788 WidgetsPage::ms_widgetPages
= this;
792 WidgetsPageInfo
*node_next
;
795 node_next
= node_prev
->GetNext();
798 // add if between two
799 if ( wxStrcmp(label
, node_next
->GetLabel().c_str()) < 0 )
801 node_prev
->SetNext(this);
803 // force to break loop
810 node_prev
->SetNext(this);
813 node_prev
= node_next
;
821 WidgetsPage::ms_widgetPages
= this;
825 // ----------------------------------------------------------------------------
827 // ----------------------------------------------------------------------------
829 int WidgetsPage::ms_defaultFlags
= wxBORDER_DEFAULT
;
830 WidgetsPageInfo
*WidgetsPage::ms_widgetPages
= NULL
;
832 WidgetsPage::WidgetsPage(WidgetsBookCtrl
*book
,
833 wxImageList
*imaglist
,
835 : wxPanel(book
, wxID_ANY
,
836 wxDefaultPosition
, wxDefaultSize
,
837 wxNO_FULL_REPAINT_ON_RESIZE
|
841 #if USE_ICONS_IN_BOOK
842 imaglist
->Add(wxBitmap(icon
));
844 wxUnusedVar(imaglist
);
849 wxSizer
*WidgetsPage::CreateSizerWithText(wxControl
*control
,
853 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
854 wxTextCtrl
*text
= new wxTextCtrl(this, id
, wxEmptyString
,
855 wxDefaultPosition
, wxDefaultSize
, wxTE_PROCESS_ENTER
);
857 sizerRow
->Add(control
, 0, wxRIGHT
| wxALIGN_CENTRE_VERTICAL
, 5);
858 sizerRow
->Add(text
, 1, wxLEFT
| wxALIGN_CENTRE_VERTICAL
, 5);
866 // create a sizer containing a label and a text ctrl
867 wxSizer
*WidgetsPage::CreateSizerWithTextAndLabel(const wxString
& label
,
871 return CreateSizerWithText(new wxStaticText(this, wxID_ANY
, label
),
875 // create a sizer containing a button and a text ctrl
876 wxSizer
*WidgetsPage::CreateSizerWithTextAndButton(wxWindowID idBtn
,
877 const wxString
& label
,
881 return CreateSizerWithText(new wxButton(this, idBtn
, label
), id
, ppText
);
884 wxCheckBox
*WidgetsPage::CreateCheckBoxAndAddToSizer(wxSizer
*sizer
,
885 const wxString
& label
,
888 wxCheckBox
*checkbox
= new wxCheckBox(this, id
, label
);
889 sizer
->Add(checkbox
, 0, wxLEFT
| wxRIGHT
, 5);
890 sizer
->Add(0, 2, 0, wxGROW
); // spacer