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"
46 #include "wx/colordlg.h"
47 #include "wx/fontdlg.h"
48 #include "wx/textdlg.h"
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
59 Widgets_ClearLog
= 100,
66 #endif // wxUSE_TOOLTIPS
78 Widgets_BorderDefault
,
81 Widgets_GoToPageLast
= Widgets_GoToPage
+ 100
84 // ----------------------------------------------------------------------------
86 // ----------------------------------------------------------------------------
88 // Define a new application type, each program should derive a class from wxApp
89 class WidgetsApp
: public wxApp
92 // override base class virtuals
93 // ----------------------------
95 // this one is called on application startup and is a good place for the app
96 // initialization (doing it here and not in the ctor allows to have an error
97 // return: if OnInit() returns false, the application terminates)
98 virtual bool OnInit();
101 // Define a new frame type: this is going to be our main frame
102 class WidgetsFrame
: public wxFrame
106 WidgetsFrame(const wxString
& title
);
107 virtual ~WidgetsFrame();
112 void OnButtonClearLog(wxCommandEvent
& event
);
114 void OnExit(wxCommandEvent
& event
);
117 void OnPageChanged(wxBookCtrlEvent
& event
);
118 void OnGoToPage(wxCommandEvent
& event
);
121 void OnSetTooltip(wxCommandEvent
& event
);
122 #endif // wxUSE_TOOLTIPS
123 void OnSetFgCol(wxCommandEvent
& event
);
124 void OnSetBgCol(wxCommandEvent
& event
);
125 void OnSetFont(wxCommandEvent
& event
);
126 void OnEnable(wxCommandEvent
& event
);
127 void OnSetBorder(wxCommandEvent
& event
);
128 #endif // wxUSE_MENUS
130 // initialize the book: add all pages to it
134 // the panel containing everything
138 // the listbox for logging messages
139 wxListBox
*m_lboxLog
;
141 // the log target we use to redirect messages to the listbox
145 // the book containing the test pages
146 wxBookCtrlBase
*m_book
;
148 // and the image list for it
149 wxImageList
*m_imaglist
;
152 // last chosen fg/bg colours and font
156 #endif // wxUSE_MENUS
158 // any class wishing to process wxWidgets events must use this macro
159 DECLARE_EVENT_TABLE()
163 // A log target which just redirects the messages to a listbox
164 class LboxLogger
: public wxLog
167 LboxLogger(wxListBox
*lbox
, wxLog
*logOld
)
170 //m_lbox->Disable(); -- looks ugly under MSW
174 virtual ~LboxLogger()
176 wxLog::SetActiveTarget(m_logOld
);
180 // implement sink functions
181 virtual void DoLog(wxLogLevel level
, const wxChar
*szString
, time_t t
)
183 // don't put trace messages into listbox or we can get into infinite
185 if ( level
== wxLOG_Trace
)
189 // cast is needed to call protected method
190 ((LboxLogger
*)m_logOld
)->DoLog(level
, szString
, t
);
195 wxLog::DoLog(level
, szString
, t
);
199 virtual void DoLogString(const wxChar
*szString
, time_t WXUNUSED(t
))
205 #ifdef __WXUNIVERSAL__
206 m_lbox
->AppendAndEnsureVisible(msg
);
207 #else // other ports don't have this method yet
209 m_lbox
->SetFirstItem(m_lbox
->GetCount() - 1);
213 // the control we use
216 // the old log target
222 WX_DEFINE_ARRAY_PTR(WidgetsPage
*, ArrayWidgetsPage
);
224 // ----------------------------------------------------------------------------
226 // ----------------------------------------------------------------------------
228 IMPLEMENT_APP(WidgetsApp
)
230 // ----------------------------------------------------------------------------
232 // ----------------------------------------------------------------------------
234 BEGIN_EVENT_TABLE(WidgetsFrame
, wxFrame
)
236 EVT_BUTTON(Widgets_ClearLog
, WidgetsFrame::OnButtonClearLog
)
238 EVT_BUTTON(Widgets_Quit
, WidgetsFrame::OnExit
)
241 EVT_MENU(Widgets_SetTooltip
, WidgetsFrame::OnSetTooltip
)
242 #endif // wxUSE_TOOLTIPS
245 EVT_BOOKCTRL_PAGE_CHANGED(Widgets_BookCtrl
, WidgetsFrame::OnPageChanged
)
246 EVT_MENU_RANGE(Widgets_GoToPage
, Widgets_GoToPageLast
,
247 WidgetsFrame::OnGoToPage
)
249 EVT_MENU(Widgets_SetFgColour
, WidgetsFrame::OnSetFgCol
)
250 EVT_MENU(Widgets_SetBgColour
, WidgetsFrame::OnSetBgCol
)
251 EVT_MENU(Widgets_SetFont
, WidgetsFrame::OnSetFont
)
252 EVT_MENU(Widgets_Enable
, WidgetsFrame::OnEnable
)
254 EVT_MENU_RANGE(Widgets_BorderNone
, Widgets_BorderDefault
,
255 WidgetsFrame::OnSetBorder
)
257 EVT_MENU(wxID_EXIT
, WidgetsFrame::OnExit
)
258 #endif // wxUSE_MENUS
261 // ============================================================================
263 // ============================================================================
265 // ----------------------------------------------------------------------------
267 // ----------------------------------------------------------------------------
269 bool WidgetsApp::OnInit()
271 if ( !wxApp::OnInit() )
274 // the reason for having these ifdef's is that I often run two copies of
275 // this sample side by side and it is useful to see which one is which
277 #if defined(__WXUNIVERSAL__)
278 title
= _T("wxUniv/");
281 #if defined(__WXMSW__)
282 title
+= _T("wxMSW");
283 #elif defined(__WXGTK__)
284 title
+= _T("wxGTK");
285 #elif defined(__WXMAC__)
286 title
+= _T("wxMAC");
287 #elif defined(__WXMOTIF__)
288 title
+= _T("wxMOTIF");
290 title
+= _T("wxWidgets");
293 wxFrame
*frame
= new WidgetsFrame(title
+ _T(" widgets demo"));
296 //wxLog::AddTraceMask(_T("listbox"));
297 //wxLog::AddTraceMask(_T("scrollbar"));
298 //wxLog::AddTraceMask(_T("focus"));
303 // ----------------------------------------------------------------------------
304 // WidgetsFrame construction
305 // ----------------------------------------------------------------------------
307 WidgetsFrame::WidgetsFrame(const wxString
& title
)
308 : wxFrame(NULL
, wxID_ANY
, title
,
309 wxPoint(0, 50), wxDefaultSize
,
310 wxDEFAULT_FRAME_STYLE
|
311 wxNO_FULL_REPAINT_ON_RESIZE
|
317 m_lboxLog
= (wxListBox
*)NULL
;
318 m_logTarget
= (wxLog
*)NULL
;
320 m_book
= (wxBookCtrlBase
*)NULL
;
321 m_imaglist
= (wxImageList
*)NULL
;
324 // create the menubar
325 wxMenuBar
*mbar
= new wxMenuBar
;
326 wxMenu
*menuWidget
= new wxMenu
;
328 menuWidget
->Append(Widgets_SetTooltip
, _T("Set &tooltip...\tCtrl-T"));
329 menuWidget
->AppendSeparator();
330 #endif // wxUSE_TOOLTIPS
331 menuWidget
->Append(Widgets_SetFgColour
, _T("Set &foreground...\tCtrl-F"));
332 menuWidget
->Append(Widgets_SetBgColour
, _T("Set &background...\tCtrl-B"));
333 menuWidget
->Append(Widgets_SetFont
, _T("Set f&ont...\tCtrl-O"));
334 menuWidget
->AppendCheckItem(Widgets_Enable
, _T("&Enable/disable\tCtrl-E"));
336 wxMenu
*menuBorders
= new wxMenu
;
337 menuBorders
->AppendRadioItem(Widgets_BorderDefault
, _T("De&fault\tCtrl-Shift-9"));
338 menuBorders
->AppendRadioItem(Widgets_BorderNone
, _T("&None\tCtrl-Shift-0"));
339 menuBorders
->AppendRadioItem(Widgets_BorderSimple
, _T("&Simple\tCtrl-Shift-1"));
340 menuBorders
->AppendRadioItem(Widgets_BorderDouble
, _T("&Double\tCtrl-Shift-2"));
341 menuBorders
->AppendRadioItem(Widgets_BorderStatic
, _T("Stati&c\tCtrl-Shift-3"));
342 menuBorders
->AppendRadioItem(Widgets_BorderRaised
, _T("&Raised\tCtrl-Shift-4"));
343 menuBorders
->AppendRadioItem(Widgets_BorderSunken
, _T("S&unken\tCtrl-Shift-5"));
344 menuWidget
->AppendSubMenu(menuBorders
, _T("Set &border"));
346 menuWidget
->AppendSeparator();
347 menuWidget
->Append(wxID_EXIT
, _T("&Quit\tCtrl-Q"));
348 mbar
->Append(menuWidget
, _T("&Widget"));
351 mbar
->Check(Widgets_Enable
, true);
352 #endif // wxUSE_MENUS
355 m_panel
= new wxPanel(this, wxID_ANY
,
356 wxDefaultPosition
, wxDefaultSize
, wxCLIP_CHILDREN
);
358 wxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
360 // we have 2 panes: book with pages demonstrating the controls in the
361 // upper one and the log window with some buttons in the lower
363 int style
= wxNO_FULL_REPAINT_ON_RESIZE
|wxCLIP_CHILDREN
|wxBK_DEFAULT
;
364 // Uncomment to suppress page theme (draw in solid colour)
365 //style |= wxNB_NOPAGETHEME;
367 m_book
= new wxBookCtrl(m_panel
, Widgets_BookCtrl
, wxDefaultPosition
,
369 wxSize(500, wxDefaultCoord
), // under Motif, height is a function of the width...
376 #ifndef __SMARTPHONE__
377 // the lower one only has the log listbox and a button to clear it
379 wxSizer
*sizerDown
= new wxStaticBoxSizer(
380 new wxStaticBox( m_panel
, wxID_ANY
, _T("&Log window") ),
383 m_lboxLog
= new wxListBox(m_panel
, wxID_ANY
);
384 sizerDown
->Add(m_lboxLog
, 1, wxGROW
| wxALL
, 5);
385 sizerDown
->SetMinSize(100, 150);
387 wxSizer
*sizerDown
= new wxBoxSizer(wxVERTICAL
);
390 wxBoxSizer
*sizerBtns
= new wxBoxSizer(wxHORIZONTAL
);
393 btn
= new wxButton(m_panel
, Widgets_ClearLog
, _T("Clear &log"));
395 sizerBtns
->Add(10, 0); // spacer
397 btn
= new wxButton(m_panel
, Widgets_Quit
, _T("E&xit"));
399 sizerDown
->Add(sizerBtns
, 0, wxALL
| wxALIGN_RIGHT
, 5);
401 // put everything together
402 sizerTop
->Add(m_book
, 1, wxGROW
| (wxALL
& ~(wxTOP
| wxBOTTOM
)), 10);
403 sizerTop
->Add(0, 5, 0, wxGROW
); // spacer in between
404 sizerTop
->Add(sizerDown
, 0, wxGROW
| (wxALL
& ~wxTOP
), 10);
406 #else // !__SMARTPHONE__/__SMARTPHONE__
408 sizerTop
->Add(m_book
, 1, wxGROW
| wxALL
);
410 #endif // __SMARTPHONE__
412 m_panel
->SetSizer(sizerTop
);
415 sizerTop
->SetSizeHints(this);
417 #if USE_LOG && !defined(__WXCOCOA__)
418 // wxCocoa's listbox is too flakey to use for logging right now
419 // now that everything is created we can redirect the log messages to the
421 m_logTarget
= new LboxLogger(m_lboxLog
, wxLog::GetActiveTarget());
422 wxLog::SetActiveTarget(m_logTarget
);
426 void WidgetsFrame::InitBook()
428 m_imaglist
= new wxImageList(32, 32);
430 ArrayWidgetsPage pages
;
431 wxArrayString labels
;
433 wxMenu
*menuPages
= new wxMenu
;
436 // we need to first create all pages and only then add them to the book
437 // as we need the image list first
439 // we also construct the pages menu during this first iteration
440 for ( WidgetsPageInfo
*info
= WidgetsPage::ms_widgetPages
;
442 info
= info
->GetNext(), nPage
++ )
444 WidgetsPage
*page
= (*info
->GetCtor())(m_book
, m_imaglist
);
447 labels
.Add(info
->GetLabel());
448 menuPages
->AppendRadioItem
450 Widgets_GoToPage
+ nPage
,
451 wxString::Format("%s\tF%u",
452 info
->GetLabel().c_str(), nPage
+ 1)
456 GetMenuBar()->Append(menuPages
, _T("&Page"));
458 m_book
->SetImageList(m_imaglist
);
461 size_t count
= pages
.GetCount();
462 for ( size_t n
= 0; n
< count
; n
++ )
467 false, // don't select
473 WidgetsFrame::~WidgetsFrame()
481 // ----------------------------------------------------------------------------
482 // WidgetsFrame event handlers
483 // ----------------------------------------------------------------------------
485 void WidgetsFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
491 void WidgetsFrame::OnButtonClearLog(wxCommandEvent
& WXUNUSED(event
))
499 void WidgetsFrame::OnPageChanged(wxBookCtrlEvent
& event
)
501 GetMenuBar()->Check(Widgets_GoToPage
+ event
.GetSelection(), true);
505 void WidgetsFrame::OnGoToPage(wxCommandEvent
& event
)
507 m_book
->SetSelection(event
.GetId() - Widgets_GoToPage
);
512 void WidgetsFrame::OnSetTooltip(wxCommandEvent
& WXUNUSED(event
))
514 static wxString s_tip
= _T("This is a tooltip");
516 wxString s
= wxGetTextFromUser
518 _T("Tooltip text: "),
519 _T("Widgets sample"),
529 if( wxMessageBox( _T("Test multiline tooltip text?"),
530 _T("Widgets sample"),
535 s
= _T("#1 ") + s_tip
+ _T("\n") + _T("#2 ") + s_tip
;
538 WidgetsPage
*page
= wxStaticCast(m_book
->GetCurrentPage(), WidgetsPage
);
539 page
->GetWidget()->SetToolTip(s
);
541 wxControl
*ctrl2
= page
->GetWidget2();
543 ctrl2
->SetToolTip(s
);
546 #endif // wxUSE_TOOLTIPS
548 void WidgetsFrame::OnSetFgCol(wxCommandEvent
& WXUNUSED(event
))
551 // allow for debugging the default colour the first time this is called
552 WidgetsPage
*page
= wxStaticCast(m_book
->GetCurrentPage(), WidgetsPage
);
554 m_colFg
= page
->GetForegroundColour();
556 wxColour col
= wxGetColourFromUser(this, m_colFg
);
562 page
->GetWidget()->SetForegroundColour(m_colFg
);
563 page
->GetWidget()->Refresh();
565 wxControl
*ctrl2
= page
->GetWidget2();
568 ctrl2
->SetForegroundColour(m_colFg
);
572 wxLogMessage(_T("Colour selection dialog not available in current build."));
576 void WidgetsFrame::OnSetBgCol(wxCommandEvent
& WXUNUSED(event
))
579 WidgetsPage
*page
= wxStaticCast(m_book
->GetCurrentPage(), WidgetsPage
);
581 m_colBg
= page
->GetBackgroundColour();
583 wxColour col
= wxGetColourFromUser(this, m_colBg
);
589 page
->GetWidget()->SetBackgroundColour(m_colBg
);
590 page
->GetWidget()->Refresh();
592 wxControl
*ctrl2
= page
->GetWidget2();
595 ctrl2
->SetBackgroundColour(m_colFg
);
599 wxLogMessage(_T("Colour selection dialog not available in current build."));
603 void WidgetsFrame::OnSetFont(wxCommandEvent
& WXUNUSED(event
))
606 WidgetsPage
*page
= wxStaticCast(m_book
->GetCurrentPage(), WidgetsPage
);
608 m_font
= page
->GetFont();
610 wxFont font
= wxGetFontFromUser(this, m_font
);
616 page
->GetWidget()->SetFont(m_font
);
617 page
->GetWidget()->Refresh();
619 wxControl
*ctrl2
= page
->GetWidget2();
622 ctrl2
->SetFont(m_font
);
626 wxLogMessage(_T("Font selection dialog not available in current build."));
630 void WidgetsFrame::OnEnable(wxCommandEvent
& event
)
632 WidgetsPage
*page
= wxStaticCast(m_book
->GetCurrentPage(), WidgetsPage
);
633 page
->GetWidget()->Enable(event
.IsChecked());
636 void WidgetsFrame::OnSetBorder(wxCommandEvent
& event
)
639 switch ( event
.GetId() )
641 case Widgets_BorderNone
: border
= wxBORDER_NONE
; break;
642 case Widgets_BorderStatic
: border
= wxBORDER_STATIC
; break;
643 case Widgets_BorderSimple
: border
= wxBORDER_SIMPLE
; break;
644 case Widgets_BorderRaised
: border
= wxBORDER_RAISED
; break;
645 case Widgets_BorderSunken
: border
= wxBORDER_SUNKEN
; break;
646 case Widgets_BorderDouble
: border
= wxBORDER_DOUBLE
; break;
649 wxFAIL_MSG( _T("unknown border style") );
652 case Widgets_BorderDefault
: border
= wxBORDER_DEFAULT
; break;
655 WidgetsPage::ms_defaultFlags
&= ~wxBORDER_MASK
;
656 WidgetsPage::ms_defaultFlags
|= border
;
658 WidgetsPage
*page
= wxStaticCast(m_book
->GetCurrentPage(), WidgetsPage
);
659 page
->RecreateWidget();
662 #endif // wxUSE_MENUS
664 // ----------------------------------------------------------------------------
666 // ----------------------------------------------------------------------------
668 WidgetsPageInfo::WidgetsPageInfo(Constructor ctor
, const wxChar
*label
)
675 // dummy sorting: add and immediately sort in the list according to label
676 if ( WidgetsPage::ms_widgetPages
)
678 WidgetsPageInfo
*node_prev
= WidgetsPage::ms_widgetPages
;
679 if ( wxStrcmp(label
, node_prev
->GetLabel().c_str()) < 0 )
683 WidgetsPage::ms_widgetPages
= this;
687 WidgetsPageInfo
*node_next
;
690 node_next
= node_prev
->GetNext();
693 // add if between two
694 if ( wxStrcmp(label
, node_next
->GetLabel().c_str()) < 0 )
696 node_prev
->SetNext(this);
698 // force to break loop
705 node_prev
->SetNext(this);
708 node_prev
= node_next
;
716 WidgetsPage::ms_widgetPages
= this;
720 // ----------------------------------------------------------------------------
722 // ----------------------------------------------------------------------------
724 int WidgetsPage::ms_defaultFlags
= wxBORDER_DEFAULT
;
725 WidgetsPageInfo
*WidgetsPage::ms_widgetPages
= NULL
;
727 WidgetsPage::WidgetsPage(wxBookCtrlBase
*book
)
728 : wxPanel(book
, wxID_ANY
,
729 wxDefaultPosition
, wxDefaultSize
,
730 wxNO_FULL_REPAINT_ON_RESIZE
|
736 wxSizer
*WidgetsPage::CreateSizerWithText(wxControl
*control
,
740 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
741 wxTextCtrl
*text
= new wxTextCtrl(this, id
, wxEmptyString
,
742 wxDefaultPosition
, wxDefaultSize
, wxTE_PROCESS_ENTER
);
744 sizerRow
->Add(control
, 0, wxRIGHT
| wxALIGN_CENTRE_VERTICAL
, 5);
745 sizerRow
->Add(text
, 1, wxLEFT
| wxALIGN_CENTRE_VERTICAL
, 5);
753 // create a sizer containing a label and a text ctrl
754 wxSizer
*WidgetsPage::CreateSizerWithTextAndLabel(const wxString
& label
,
758 return CreateSizerWithText(new wxStaticText(this, wxID_ANY
, label
),
762 // create a sizer containing a button and a text ctrl
763 wxSizer
*WidgetsPage::CreateSizerWithTextAndButton(wxWindowID idBtn
,
764 const wxString
& label
,
768 return CreateSizerWithText(new wxButton(this, idBtn
, label
), id
, ppText
);
771 wxCheckBox
*WidgetsPage::CreateCheckBoxAndAddToSizer(wxSizer
*sizer
,
772 const wxString
& label
,
775 wxCheckBox
*checkbox
= new wxCheckBox(this, id
, label
);
776 sizer
->Add(checkbox
, 0, wxLEFT
| wxRIGHT
, 5);
777 sizer
->Add(0, 2, 0, wxGROW
); // spacer