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"
53 #include "../sample.xpm"
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
62 Widgets_ClearLog
= 100,
69 #endif // wxUSE_TOOLTIPS
81 Widgets_BorderDefault
,
84 Widgets_GoToPageLast
= Widgets_GoToPage
+ 100
87 const wxChar
*WidgetsCategories
[MAX_PAGES
] = {
98 // ----------------------------------------------------------------------------
100 // ----------------------------------------------------------------------------
102 // Define a new application type, each program should derive a class from wxApp
103 class WidgetsApp
: public wxApp
106 // override base class virtuals
107 // ----------------------------
109 // this one is called on application startup and is a good place for the app
110 // initialization (doing it here and not in the ctor allows to have an error
111 // return: if OnInit() returns false, the application terminates)
112 virtual bool OnInit();
115 // Define a new frame type: this is going to be our main frame
116 class WidgetsFrame
: public wxFrame
120 WidgetsFrame(const wxString
& title
);
121 virtual ~WidgetsFrame();
126 void OnButtonClearLog(wxCommandEvent
& event
);
128 void OnExit(wxCommandEvent
& event
);
131 void OnPageChanged(WidgetsBookCtrlEvent
& event
);
132 void OnGoToPage(wxCommandEvent
& event
);
135 void OnSetTooltip(wxCommandEvent
& event
);
136 #endif // wxUSE_TOOLTIPS
137 void OnSetFgCol(wxCommandEvent
& event
);
138 void OnSetBgCol(wxCommandEvent
& event
);
139 void OnSetFont(wxCommandEvent
& event
);
140 void OnEnable(wxCommandEvent
& event
);
141 void OnSetBorder(wxCommandEvent
& event
);
142 #endif // wxUSE_MENUS
144 // initialize the book: add all pages to it
147 // finding current page assuming book inside book
148 WidgetsPage
*CurrentPage();
151 // the panel containing everything
155 // the listbox for logging messages
156 wxListBox
*m_lboxLog
;
158 // the log target we use to redirect messages to the listbox
162 // the book containing the test pages
163 WidgetsBookCtrl
*m_book
;
166 // last chosen fg/bg colours and font
170 #endif // wxUSE_MENUS
172 // any class wishing to process wxWidgets events must use this macro
173 DECLARE_EVENT_TABLE()
177 // A log target which just redirects the messages to a listbox
178 class LboxLogger
: public wxLog
181 LboxLogger(wxListBox
*lbox
, wxLog
*logOld
)
184 //m_lbox->Disable(); -- looks ugly under MSW
188 virtual ~LboxLogger()
190 wxLog::SetActiveTarget(m_logOld
);
194 // implement sink functions
195 virtual void DoLog(wxLogLevel level
, const wxChar
*szString
, time_t t
)
197 // don't put trace messages into listbox or we can get into infinite
199 if ( level
== wxLOG_Trace
)
203 // cast is needed to call protected method
204 ((LboxLogger
*)m_logOld
)->DoLog(level
, szString
, t
);
209 wxLog::DoLog(level
, szString
, t
);
213 virtual void DoLogString(const wxChar
*szString
, time_t WXUNUSED(t
))
219 #ifdef __WXUNIVERSAL__
220 m_lbox
->AppendAndEnsureVisible(msg
);
221 #else // other ports don't have this method yet
223 m_lbox
->SetFirstItem(m_lbox
->GetCount() - 1);
227 // the control we use
230 // the old log target
236 WX_DEFINE_ARRAY_PTR(WidgetsPage
*, ArrayWidgetsPage
);
238 // ----------------------------------------------------------------------------
240 // ----------------------------------------------------------------------------
242 IMPLEMENT_APP(WidgetsApp
)
244 // ----------------------------------------------------------------------------
246 // ----------------------------------------------------------------------------
248 BEGIN_EVENT_TABLE(WidgetsFrame
, wxFrame
)
250 EVT_BUTTON(Widgets_ClearLog
, WidgetsFrame::OnButtonClearLog
)
252 EVT_BUTTON(Widgets_Quit
, WidgetsFrame::OnExit
)
255 EVT_MENU(Widgets_SetTooltip
, WidgetsFrame::OnSetTooltip
)
256 #endif // wxUSE_TOOLTIPS
259 EVT_WIDGETS_PAGE_CHANGED(wxID_ANY
, WidgetsFrame::OnPageChanged
)
260 EVT_MENU_RANGE(Widgets_GoToPage
, Widgets_GoToPageLast
,
261 WidgetsFrame::OnGoToPage
)
263 EVT_MENU(Widgets_SetFgColour
, WidgetsFrame::OnSetFgCol
)
264 EVT_MENU(Widgets_SetBgColour
, WidgetsFrame::OnSetBgCol
)
265 EVT_MENU(Widgets_SetFont
, WidgetsFrame::OnSetFont
)
266 EVT_MENU(Widgets_Enable
, WidgetsFrame::OnEnable
)
268 EVT_MENU_RANGE(Widgets_BorderNone
, Widgets_BorderDefault
,
269 WidgetsFrame::OnSetBorder
)
271 EVT_MENU(wxID_EXIT
, WidgetsFrame::OnExit
)
272 #endif // wxUSE_MENUS
275 // ============================================================================
277 // ============================================================================
279 // ----------------------------------------------------------------------------
281 // ----------------------------------------------------------------------------
283 bool WidgetsApp::OnInit()
285 if ( !wxApp::OnInit() )
288 // the reason for having these ifdef's is that I often run two copies of
289 // this sample side by side and it is useful to see which one is which
291 #if defined(__WXUNIVERSAL__)
292 title
= _T("wxUniv/");
295 #if defined(__WXMSW__)
296 title
+= _T("wxMSW");
297 #elif defined(__WXGTK__)
298 title
+= _T("wxGTK");
299 #elif defined(__WXMAC__)
300 title
+= _T("wxMAC");
301 #elif defined(__WXMOTIF__)
302 title
+= _T("wxMOTIF");
304 title
+= _T("wxWidgets");
307 wxFrame
*frame
= new WidgetsFrame(title
+ _T(" widgets demo"));
310 //wxLog::AddTraceMask(_T("listbox"));
311 //wxLog::AddTraceMask(_T("scrollbar"));
312 //wxLog::AddTraceMask(_T("focus"));
317 // ----------------------------------------------------------------------------
318 // WidgetsFrame construction
319 // ----------------------------------------------------------------------------
321 WidgetsFrame::WidgetsFrame(const wxString
& title
)
322 : wxFrame(NULL
, wxID_ANY
, title
,
323 wxPoint(0, 50), wxDefaultSize
,
324 wxDEFAULT_FRAME_STYLE
|
325 wxNO_FULL_REPAINT_ON_RESIZE
|
329 // set the frame icon
330 SetIcon(wxICON(sample
));
334 m_lboxLog
= (wxListBox
*)NULL
;
335 m_logTarget
= (wxLog
*)NULL
;
337 m_book
= (WidgetsBookCtrl
*)NULL
;
340 // create the menubar
341 wxMenuBar
*mbar
= new wxMenuBar
;
342 wxMenu
*menuWidget
= new wxMenu
;
344 menuWidget
->Append(Widgets_SetTooltip
, _T("Set &tooltip...\tCtrl-T"));
345 menuWidget
->AppendSeparator();
346 #endif // wxUSE_TOOLTIPS
347 menuWidget
->Append(Widgets_SetFgColour
, _T("Set &foreground...\tCtrl-F"));
348 menuWidget
->Append(Widgets_SetBgColour
, _T("Set &background...\tCtrl-B"));
349 menuWidget
->Append(Widgets_SetFont
, _T("Set f&ont...\tCtrl-O"));
350 menuWidget
->AppendCheckItem(Widgets_Enable
, _T("&Enable/disable\tCtrl-E"));
352 wxMenu
*menuBorders
= new wxMenu
;
353 menuBorders
->AppendRadioItem(Widgets_BorderDefault
, _T("De&fault\tCtrl-Shift-9"));
354 menuBorders
->AppendRadioItem(Widgets_BorderNone
, _T("&None\tCtrl-Shift-0"));
355 menuBorders
->AppendRadioItem(Widgets_BorderSimple
, _T("&Simple\tCtrl-Shift-1"));
356 menuBorders
->AppendRadioItem(Widgets_BorderDouble
, _T("&Double\tCtrl-Shift-2"));
357 menuBorders
->AppendRadioItem(Widgets_BorderStatic
, _T("Stati&c\tCtrl-Shift-3"));
358 menuBorders
->AppendRadioItem(Widgets_BorderRaised
, _T("&Raised\tCtrl-Shift-4"));
359 menuBorders
->AppendRadioItem(Widgets_BorderSunken
, _T("S&unken\tCtrl-Shift-5"));
360 menuWidget
->AppendSubMenu(menuBorders
, _T("Set &border"));
362 menuWidget
->AppendSeparator();
363 menuWidget
->Append(wxID_EXIT
, _T("&Quit\tCtrl-Q"));
364 mbar
->Append(menuWidget
, _T("&Widget"));
367 mbar
->Check(Widgets_Enable
, true);
368 #endif // wxUSE_MENUS
371 m_panel
= new wxPanel(this, wxID_ANY
,
372 wxDefaultPosition
, wxDefaultSize
, wxCLIP_CHILDREN
);
374 wxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
376 // we have 2 panes: book with pages demonstrating the controls in the
377 // upper one and the log window with some buttons in the lower
379 int style
= wxNO_FULL_REPAINT_ON_RESIZE
|wxCLIP_CHILDREN
|wxBK_DEFAULT
;
380 // Uncomment to suppress page theme (draw in solid colour)
381 //style |= wxNB_NOPAGETHEME;
383 m_book
= new WidgetsBookCtrl(m_panel
, Widgets_BookCtrl
, wxDefaultPosition
,
385 wxSize(500, wxDefaultCoord
), // under Motif, height is a function of the width...
392 #ifndef __SMARTPHONE__
393 // the lower one only has the log listbox and a button to clear it
395 wxSizer
*sizerDown
= new wxStaticBoxSizer(
396 new wxStaticBox( m_panel
, wxID_ANY
, _T("&Log window") ),
399 m_lboxLog
= new wxListBox(m_panel
, wxID_ANY
);
400 sizerDown
->Add(m_lboxLog
, 1, wxGROW
| wxALL
, 5);
401 sizerDown
->SetMinSize(100, 150);
403 wxSizer
*sizerDown
= new wxBoxSizer(wxVERTICAL
);
406 wxBoxSizer
*sizerBtns
= new wxBoxSizer(wxHORIZONTAL
);
409 btn
= new wxButton(m_panel
, Widgets_ClearLog
, _T("Clear &log"));
411 sizerBtns
->Add(10, 0); // spacer
413 btn
= new wxButton(m_panel
, Widgets_Quit
, _T("E&xit"));
415 sizerDown
->Add(sizerBtns
, 0, wxALL
| wxALIGN_RIGHT
, 5);
417 // put everything together
418 sizerTop
->Add(m_book
, 1, wxGROW
| (wxALL
& ~(wxTOP
| wxBOTTOM
)), 10);
419 sizerTop
->Add(0, 5, 0, wxGROW
); // spacer in between
420 sizerTop
->Add(sizerDown
, 0, wxGROW
| (wxALL
& ~wxTOP
), 10);
422 #else // !__SMARTPHONE__/__SMARTPHONE__
424 sizerTop
->Add(m_book
, 1, wxGROW
| wxALL
);
426 #endif // __SMARTPHONE__
428 m_panel
->SetSizer(sizerTop
);
431 sizerTop
->SetSizeHints(this);
433 #if USE_LOG && !defined(__WXCOCOA__)
434 // wxCocoa's listbox is too flakey to use for logging right now
435 // now that everything is created we can redirect the log messages to the
437 m_logTarget
= new LboxLogger(m_lboxLog
, wxLog::GetActiveTarget());
438 wxLog::SetActiveTarget(m_logTarget
);
442 void WidgetsFrame::InitBook()
444 wxImageList
*imageList
= new wxImageList(32, 32);
446 imageList
->Add(wxBitmap(sample_xpm
));
449 WidgetsBookCtrl
*books
[MAX_PAGES
];
452 ArrayWidgetsPage pages
[MAX_PAGES
];
453 wxArrayString labels
[MAX_PAGES
];
455 wxMenu
*menuPages
= new wxMenu
;
456 unsigned int nPage
= 0, nFKey
= 0;
457 int cat
, imageId
= 1;
459 // we need to first create all pages and only then add them to the book
460 // as we need the image list first
462 // we also construct the pages menu during this first iteration
463 for ( cat
= 0; cat
< MAX_PAGES
; cat
++ )
466 nPage
++; // increase for parent page
468 books
[cat
] = new WidgetsBookCtrl( m_book
, wxID_ANY
);
471 for ( WidgetsPageInfo
*info
= WidgetsPage::ms_widgetPages
;
473 info
= info
->GetNext() )
475 if( (info
->GetCategories() & ( 1 << cat
)) == 0)
478 WidgetsPage
*page
= (*info
->GetCtor())(
485 pages
[cat
].Add(page
);
487 labels
[cat
].Add(info
->GetLabel());
488 if ( cat
== ALL_PAGE
)
490 wxString
radioLabel(info
->GetLabel());
494 radioLabel
<< wxT("\tF" ) << nFKey
;
497 menuPages
->AppendRadioItem(
498 Widgets_GoToPage
+ nPage
,
502 // consider only for book in book architecture
508 // consider only for treebook architecture (with subpages)
514 GetMenuBar()->Append(menuPages
, _T("&Page"));
516 m_book
->AssignImageList(imageList
);
518 for ( cat
= 0; cat
< MAX_PAGES
; cat
++ )
521 m_book
->AddPage(NULL
,WidgetsCategories
[cat
],false,0);
523 m_book
->AddPage(books
[cat
],WidgetsCategories
[cat
],false,0);
524 books
[cat
]->SetImageList(imageList
);
528 size_t count
= pages
[cat
].GetCount();
529 for ( size_t n
= 0; n
< count
; n
++ )
538 false, // don't select
545 // for treebook page #0 is empty parent page only
546 m_book
->SetSelection(1);
550 WidgetsPage
*WidgetsFrame::CurrentPage()
553 return wxStaticCast(m_book
->GetCurrentPage(), WidgetsPage
);
555 WidgetsBookCtrl
*book
= wxStaticCast(m_book
->GetCurrentPage(), WidgetsBookCtrl
);
556 if (!book
) return NULL
;
557 return wxStaticCast(book
->GetCurrentPage(), WidgetsPage
);
561 WidgetsFrame::~WidgetsFrame()
568 // ----------------------------------------------------------------------------
569 // WidgetsFrame event handlers
570 // ----------------------------------------------------------------------------
572 void WidgetsFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
578 void WidgetsFrame::OnButtonClearLog(wxCommandEvent
& WXUNUSED(event
))
586 void WidgetsFrame::OnPageChanged(WidgetsBookCtrlEvent
& event
)
588 wxMenuItem
*item
= GetMenuBar()->FindItem(Widgets_GoToPage
+ event
.GetSelection());
589 if (item
) item
->Check();
593 void WidgetsFrame::OnGoToPage(wxCommandEvent
& event
)
596 m_book
->SetSelection(event
.GetId() - Widgets_GoToPage
);
598 m_book
->SetSelection(m_book
->GetPageCount()-1);
599 WidgetsBookCtrl
*book
= wxStaticCast(m_book
->GetCurrentPage(), WidgetsBookCtrl
);
600 book
->SetSelection(event
.GetId() - Widgets_GoToPage
);
606 void WidgetsFrame::OnSetTooltip(wxCommandEvent
& WXUNUSED(event
))
608 static wxString s_tip
= _T("This is a tooltip");
610 wxTextEntryDialog dialog
613 _T("Tooltip text (may use \\n, leave empty to remove): "),
614 _T("Widgets sample"),
618 if ( dialog
.ShowModal() != wxID_OK
)
621 s_tip
= dialog
.GetValue();
622 s_tip
.Replace(_T("\\n"), _T("\n"));
624 WidgetsPage
*page
= CurrentPage();
625 page
->GetWidget()->SetToolTip(s_tip
);
627 wxControl
*ctrl2
= page
->GetWidget2();
629 ctrl2
->SetToolTip(s_tip
);
632 #endif // wxUSE_TOOLTIPS
634 void WidgetsFrame::OnSetFgCol(wxCommandEvent
& WXUNUSED(event
))
637 // allow for debugging the default colour the first time this is called
638 WidgetsPage
*page
= CurrentPage();
640 m_colFg
= page
->GetForegroundColour();
642 wxColour col
= wxGetColourFromUser(this, m_colFg
);
648 page
->GetWidget()->SetForegroundColour(m_colFg
);
649 page
->GetWidget()->Refresh();
651 wxControl
*ctrl2
= page
->GetWidget2();
654 ctrl2
->SetForegroundColour(m_colFg
);
658 wxLogMessage(_T("Colour selection dialog not available in current build."));
662 void WidgetsFrame::OnSetBgCol(wxCommandEvent
& WXUNUSED(event
))
665 WidgetsPage
*page
= CurrentPage();
667 m_colBg
= page
->GetBackgroundColour();
669 wxColour col
= wxGetColourFromUser(this, m_colBg
);
675 page
->GetWidget()->SetBackgroundColour(m_colBg
);
676 page
->GetWidget()->Refresh();
678 wxControl
*ctrl2
= page
->GetWidget2();
681 ctrl2
->SetBackgroundColour(m_colFg
);
685 wxLogMessage(_T("Colour selection dialog not available in current build."));
689 void WidgetsFrame::OnSetFont(wxCommandEvent
& WXUNUSED(event
))
692 WidgetsPage
*page
= CurrentPage();
694 m_font
= page
->GetFont();
696 wxFont font
= wxGetFontFromUser(this, m_font
);
702 page
->GetWidget()->SetFont(m_font
);
703 page
->GetWidget()->Refresh();
705 wxControl
*ctrl2
= page
->GetWidget2();
708 ctrl2
->SetFont(m_font
);
712 wxLogMessage(_T("Font selection dialog not available in current build."));
716 void WidgetsFrame::OnEnable(wxCommandEvent
& event
)
718 WidgetsPage
*page
= CurrentPage();
719 page
->GetWidget()->Enable(event
.IsChecked());
722 void WidgetsFrame::OnSetBorder(wxCommandEvent
& event
)
725 switch ( event
.GetId() )
727 case Widgets_BorderNone
: border
= wxBORDER_NONE
; break;
728 case Widgets_BorderStatic
: border
= wxBORDER_STATIC
; break;
729 case Widgets_BorderSimple
: border
= wxBORDER_SIMPLE
; break;
730 case Widgets_BorderRaised
: border
= wxBORDER_RAISED
; break;
731 case Widgets_BorderSunken
: border
= wxBORDER_SUNKEN
; break;
732 case Widgets_BorderDouble
: border
= wxBORDER_DOUBLE
; break;
735 wxFAIL_MSG( _T("unknown border style") );
738 case Widgets_BorderDefault
: border
= wxBORDER_DEFAULT
; break;
741 WidgetsPage::ms_defaultFlags
&= ~wxBORDER_MASK
;
742 WidgetsPage::ms_defaultFlags
|= border
;
744 WidgetsPage
*page
= CurrentPage();
745 page
->RecreateWidget();
748 #endif // wxUSE_MENUS
750 // ----------------------------------------------------------------------------
752 // ----------------------------------------------------------------------------
754 WidgetsPageInfo::WidgetsPageInfo(Constructor ctor
, const wxChar
*label
, int categories
)
756 , m_categories(categories
)
762 // dummy sorting: add and immediately sort in the list according to label
763 if ( WidgetsPage::ms_widgetPages
)
765 WidgetsPageInfo
*node_prev
= WidgetsPage::ms_widgetPages
;
766 if ( wxStrcmp(label
, node_prev
->GetLabel().c_str()) < 0 )
770 WidgetsPage::ms_widgetPages
= this;
774 WidgetsPageInfo
*node_next
;
777 node_next
= node_prev
->GetNext();
780 // add if between two
781 if ( wxStrcmp(label
, node_next
->GetLabel().c_str()) < 0 )
783 node_prev
->SetNext(this);
785 // force to break loop
792 node_prev
->SetNext(this);
795 node_prev
= node_next
;
803 WidgetsPage::ms_widgetPages
= this;
807 // ----------------------------------------------------------------------------
809 // ----------------------------------------------------------------------------
811 int WidgetsPage::ms_defaultFlags
= wxBORDER_DEFAULT
;
812 WidgetsPageInfo
*WidgetsPage::ms_widgetPages
= NULL
;
814 WidgetsPage::WidgetsPage(WidgetsBookCtrl
*book
)
815 : wxPanel(book
, wxID_ANY
,
816 wxDefaultPosition
, wxDefaultSize
,
817 wxNO_FULL_REPAINT_ON_RESIZE
|
823 wxSizer
*WidgetsPage::CreateSizerWithText(wxControl
*control
,
827 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
828 wxTextCtrl
*text
= new wxTextCtrl(this, id
, wxEmptyString
,
829 wxDefaultPosition
, wxDefaultSize
, wxTE_PROCESS_ENTER
);
831 sizerRow
->Add(control
, 0, wxRIGHT
| wxALIGN_CENTRE_VERTICAL
, 5);
832 sizerRow
->Add(text
, 1, wxLEFT
| wxALIGN_CENTRE_VERTICAL
, 5);
840 // create a sizer containing a label and a text ctrl
841 wxSizer
*WidgetsPage::CreateSizerWithTextAndLabel(const wxString
& label
,
845 return CreateSizerWithText(new wxStaticText(this, wxID_ANY
, label
),
849 // create a sizer containing a button and a text ctrl
850 wxSizer
*WidgetsPage::CreateSizerWithTextAndButton(wxWindowID idBtn
,
851 const wxString
& label
,
855 return CreateSizerWithText(new wxButton(this, idBtn
, label
), id
, ppText
);
858 wxCheckBox
*WidgetsPage::CreateCheckBoxAndAddToSizer(wxSizer
*sizer
,
859 const wxString
& label
,
862 wxCheckBox
*checkbox
= new wxCheckBox(this, id
, label
);
863 sizer
->Add(checkbox
, 0, wxLEFT
| wxRIGHT
, 5);
864 sizer
->Add(0, 2, 0, wxGROW
); // spacer