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,
64 #endif // wxUSE_TOOLTIPS
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
83 // Define a new application type, each program should derive a class from wxApp
84 class WidgetsApp
: public wxApp
87 // override base class virtuals
88 // ----------------------------
90 // this one is called on application startup and is a good place for the app
91 // initialization (doing it here and not in the ctor allows to have an error
92 // return: if OnInit() returns false, the application terminates)
93 virtual bool OnInit();
96 // Define a new frame type: this is going to be our main frame
97 class WidgetsFrame
: public wxFrame
101 WidgetsFrame(const wxString
& title
);
102 virtual ~WidgetsFrame();
107 void OnButtonClearLog(wxCommandEvent
& event
);
109 void OnExit(wxCommandEvent
& event
);
113 void OnSetTooltip(wxCommandEvent
& event
);
114 #endif // wxUSE_TOOLTIPS
115 void OnSetFgCol(wxCommandEvent
& event
);
116 void OnSetBgCol(wxCommandEvent
& event
);
117 void OnSetFont(wxCommandEvent
& event
);
118 void OnEnable(wxCommandEvent
& event
);
119 void OnSetBorder(wxCommandEvent
& event
);
120 #endif // wxUSE_MENUS
122 // initialize the book: add all pages to it
126 // the panel containing everything
130 // the listbox for logging messages
131 wxListBox
*m_lboxLog
;
133 // the log target we use to redirect messages to the listbox
137 // the book containing the test pages
138 wxBookCtrlBase
*m_book
;
140 // and the image list for it
141 wxImageList
*m_imaglist
;
144 // last chosen fg/bg colours and font
148 #endif // wxUSE_MENUS
150 // any class wishing to process wxWidgets events must use this macro
151 DECLARE_EVENT_TABLE()
155 // A log target which just redirects the messages to a listbox
156 class LboxLogger
: public wxLog
159 LboxLogger(wxListBox
*lbox
, wxLog
*logOld
)
162 //m_lbox->Disable(); -- looks ugly under MSW
166 virtual ~LboxLogger()
168 wxLog
::SetActiveTarget(m_logOld
);
172 // implement sink functions
173 virtual void DoLog(wxLogLevel level
, const wxChar
*szString
, time_t t
)
175 // don't put trace messages into listbox or we can get into infinite
177 if ( level
== wxLOG_Trace
)
181 // cast is needed to call protected method
182 ((LboxLogger
*)m_logOld
)->DoLog(level
, szString
, t
);
187 wxLog
::DoLog(level
, szString
, t
);
191 virtual void DoLogString(const wxChar
*szString
, time_t WXUNUSED(t
))
197 #ifdef __WXUNIVERSAL__
198 m_lbox
->AppendAndEnsureVisible(msg
);
199 #else // other ports don't have this method yet
201 m_lbox
->SetFirstItem(m_lbox
->GetCount() - 1);
205 // the control we use
208 // the old log target
214 WX_DEFINE_ARRAY_PTR(WidgetsPage
*, ArrayWidgetsPage
);
216 // ----------------------------------------------------------------------------
218 // ----------------------------------------------------------------------------
220 IMPLEMENT_APP(WidgetsApp
)
222 // ----------------------------------------------------------------------------
224 // ----------------------------------------------------------------------------
226 BEGIN_EVENT_TABLE(WidgetsFrame
, wxFrame
)
228 EVT_BUTTON(Widgets_ClearLog
, WidgetsFrame
::OnButtonClearLog
)
230 EVT_BUTTON(Widgets_Quit
, WidgetsFrame
::OnExit
)
233 EVT_MENU(Widgets_SetTooltip
, WidgetsFrame
::OnSetTooltip
)
234 #endif // wxUSE_TOOLTIPS
236 EVT_MENU(Widgets_SetFgColour
, WidgetsFrame
::OnSetFgCol
)
237 EVT_MENU(Widgets_SetBgColour
, WidgetsFrame
::OnSetBgCol
)
238 EVT_MENU(Widgets_SetFont
, WidgetsFrame
::OnSetFont
)
239 EVT_MENU(Widgets_Enable
, WidgetsFrame
::OnEnable
)
241 EVT_MENU_RANGE(Widgets_BorderNone
, Widgets_BorderDefault
,
242 WidgetsFrame
::OnSetBorder
)
244 EVT_MENU(wxID_EXIT
, WidgetsFrame
::OnExit
)
247 // ============================================================================
249 // ============================================================================
251 // ----------------------------------------------------------------------------
253 // ----------------------------------------------------------------------------
255 bool WidgetsApp
::OnInit()
257 if ( !wxApp
::OnInit() )
260 // the reason for having these ifdef's is that I often run two copies of
261 // this sample side by side and it is useful to see which one is which
263 #if defined(__WXUNIVERSAL__)
264 title
= _T("wxUniv/");
267 #if defined(__WXMSW__)
268 title
+= _T("wxMSW");
269 #elif defined(__WXGTK__)
270 title
+= _T("wxGTK");
271 #elif defined(__WXMAC__)
272 title
+= _T("wxMAC");
273 #elif defined(__WXMOTIF__)
274 title
+= _T("wxMOTIF");
276 title
+= _T("wxWidgets");
279 wxFrame
*frame
= new WidgetsFrame(title
+ _T(" widgets demo"));
282 //wxLog::AddTraceMask(_T("listbox"));
283 //wxLog::AddTraceMask(_T("scrollbar"));
284 //wxLog::AddTraceMask(_T("focus"));
289 // ----------------------------------------------------------------------------
290 // WidgetsFrame construction
291 // ----------------------------------------------------------------------------
293 WidgetsFrame
::WidgetsFrame(const wxString
& title
)
294 : wxFrame(NULL
, wxID_ANY
, title
,
295 wxPoint(0, 50), wxDefaultSize
,
296 wxDEFAULT_FRAME_STYLE
|
297 wxNO_FULL_REPAINT_ON_RESIZE
|
303 m_lboxLog
= (wxListBox
*)NULL
;
304 m_logTarget
= (wxLog
*)NULL
;
306 m_book
= (wxBookCtrlBase
*)NULL
;
307 m_imaglist
= (wxImageList
*)NULL
;
310 // create the menubar
311 wxMenuBar
*mbar
= new wxMenuBar
;
312 wxMenu
*menuWidget
= new wxMenu
;
314 menuWidget
->Append(Widgets_SetTooltip
, _T("Set &tooltip...\tCtrl-T"));
315 menuWidget
->AppendSeparator();
316 #endif // wxUSE_TOOLTIPS
317 menuWidget
->Append(Widgets_SetFgColour
, _T("Set &foreground...\tCtrl-F"));
318 menuWidget
->Append(Widgets_SetBgColour
, _T("Set &background...\tCtrl-B"));
319 menuWidget
->Append(Widgets_SetFont
, _T("Set f&ont...\tCtrl-O"));
320 menuWidget
->AppendCheckItem(Widgets_Enable
, _T("&Enable/disable\tCtrl-E"));
322 wxMenu
*menuBorders
= new wxMenu
;
323 menuBorders
->AppendRadioItem(Widgets_BorderDefault
, _T("De&fault\tCtrl-Shift-9"));
324 menuBorders
->AppendRadioItem(Widgets_BorderNone
, _T("&None\tCtrl-Shift-0"));
325 menuBorders
->AppendRadioItem(Widgets_BorderSimple
, _T("&Simple\tCtrl-Shift-1"));
326 menuBorders
->AppendRadioItem(Widgets_BorderDouble
, _T("&Double\tCtrl-Shift-2"));
327 menuBorders
->AppendRadioItem(Widgets_BorderStatic
, _T("Stati&c\tCtrl-Shift-3"));
328 menuBorders
->AppendRadioItem(Widgets_BorderRaised
, _T("&Raised\tCtrl-Shift-4"));
329 menuBorders
->AppendRadioItem(Widgets_BorderSunken
, _T("S&unken\tCtrl-Shift-5"));
330 menuWidget
->AppendSubMenu(menuBorders
, _T("Set &border"));
332 menuWidget
->AppendSeparator();
333 menuWidget
->Append(wxID_EXIT
, _T("&Quit\tCtrl-Q"));
334 mbar
->Append(menuWidget
, _T("&Widget"));
337 mbar
->Check(Widgets_Enable
, true);
338 #endif // wxUSE_MENUS
341 m_panel
= new wxPanel(this, wxID_ANY
,
342 wxDefaultPosition
, wxDefaultSize
, wxCLIP_CHILDREN
);
344 wxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
346 // we have 2 panes: book with pages demonstrating the controls in the
347 // upper one and the log window with some buttons in the lower
349 int style
= wxNO_FULL_REPAINT_ON_RESIZE
|wxCLIP_CHILDREN
|wxBK_DEFAULT
;
350 // Uncomment to suppress page theme (draw in solid colour)
351 //style |= wxNB_NOPAGETHEME;
353 m_book
= new wxBookCtrl(m_panel
, wxID_ANY
, wxDefaultPosition
,
355 wxSize(500, wxDefaultCoord
), // under Motif, height is a function of the width...
362 #ifndef __SMARTPHONE__
363 // the lower one only has the log listbox and a button to clear it
365 wxSizer
*sizerDown
= new wxStaticBoxSizer(
366 new wxStaticBox( m_panel
, wxID_ANY
, _T("&Log window") ),
369 m_lboxLog
= new wxListBox(m_panel
, wxID_ANY
);
370 sizerDown
->Add(m_lboxLog
, 1, wxGROW
| wxALL
, 5);
371 sizerDown
->SetMinSize(100, 150);
373 wxSizer
*sizerDown
= new wxBoxSizer(wxVERTICAL
);
376 wxBoxSizer
*sizerBtns
= new wxBoxSizer(wxHORIZONTAL
);
379 btn
= new wxButton(m_panel
, Widgets_ClearLog
, _T("Clear &log"));
381 sizerBtns
->Add(10, 0); // spacer
383 btn
= new wxButton(m_panel
, Widgets_Quit
, _T("E&xit"));
385 sizerDown
->Add(sizerBtns
, 0, wxALL
| wxALIGN_RIGHT
, 5);
387 // put everything together
388 sizerTop
->Add(m_book
, 1, wxGROW
| (wxALL
& ~(wxTOP
| wxBOTTOM
)), 10);
389 sizerTop
->Add(0, 5, 0, wxGROW
); // spacer in between
390 sizerTop
->Add(sizerDown
, 0, wxGROW
| (wxALL
& ~wxTOP
), 10);
392 #else // !__SMARTPHONE__/__SMARTPHONE__
394 sizerTop
->Add(m_book
, 1, wxGROW
| wxALL
);
396 #endif // __SMARTPHONE__
398 m_panel
->SetSizer(sizerTop
);
401 sizerTop
->SetSizeHints(this);
403 #if USE_LOG && !defined(__WXCOCOA__)
404 // wxCocoa's listbox is too flakey to use for logging right now
405 // now that everything is created we can redirect the log messages to the
407 m_logTarget
= new LboxLogger(m_lboxLog
, wxLog
::GetActiveTarget());
408 wxLog
::SetActiveTarget(m_logTarget
);
412 void WidgetsFrame
::InitBook()
414 m_imaglist
= new wxImageList(32, 32);
416 ArrayWidgetsPage pages
;
417 wxArrayString labels
;
419 // we need to first create all pages and only then add them to the book
420 // as we need the image list first
421 WidgetsPageInfo
*info
= WidgetsPage
::ms_widgetPages
;
424 WidgetsPage
*page
= (*info
->GetCtor())(m_book
, m_imaglist
);
427 labels
.Add(info
->GetLabel());
429 info
= info
->GetNext();
432 m_book
->SetImageList(m_imaglist
);
435 size_t count
= pages
.GetCount();
436 for ( size_t n
= 0; n
< count
; n
++ )
441 false, // don't select
447 WidgetsFrame
::~WidgetsFrame()
455 // ----------------------------------------------------------------------------
456 // WidgetsFrame event handlers
457 // ----------------------------------------------------------------------------
459 void WidgetsFrame
::OnExit(wxCommandEvent
& WXUNUSED(event
))
465 void WidgetsFrame
::OnButtonClearLog(wxCommandEvent
& WXUNUSED(event
))
475 void WidgetsFrame
::OnSetTooltip(wxCommandEvent
& WXUNUSED(event
))
477 static wxString s_tip
= _T("This is a tooltip");
479 wxString s
= wxGetTextFromUser
481 _T("Tooltip text: "),
482 _T("Widgets sample"),
492 if( wxMessageBox( _T("Test multiline tooltip text?"),
493 _T("Widgets sample"),
498 s
= _T("#1 ") + s_tip
+ _T("\n") + _T("#2 ") + s_tip
;
501 WidgetsPage
*page
= wxStaticCast(m_book
->GetCurrentPage(), WidgetsPage
);
502 page
->GetWidget()->SetToolTip(s
);
504 wxControl
*ctrl2
= page
->GetWidget2();
506 ctrl2
->SetToolTip(s
);
509 #endif // wxUSE_TOOLTIPS
511 void WidgetsFrame
::OnSetFgCol(wxCommandEvent
& WXUNUSED(event
))
514 // allow for debugging the default colour the first time this is called
515 WidgetsPage
*page
= wxStaticCast(m_book
->GetCurrentPage(), WidgetsPage
);
517 m_colFg
= page
->GetForegroundColour();
519 wxColour col
= wxGetColourFromUser(this, m_colFg
);
525 page
->GetWidget()->SetForegroundColour(m_colFg
);
526 page
->GetWidget()->Refresh();
528 wxControl
*ctrl2
= page
->GetWidget2();
531 ctrl2
->SetForegroundColour(m_colFg
);
535 wxLogMessage(_T("Colour selection dialog not available in current build."));
539 void WidgetsFrame
::OnSetBgCol(wxCommandEvent
& WXUNUSED(event
))
542 WidgetsPage
*page
= wxStaticCast(m_book
->GetCurrentPage(), WidgetsPage
);
544 m_colBg
= page
->GetBackgroundColour();
546 wxColour col
= wxGetColourFromUser(this, m_colBg
);
552 page
->GetWidget()->SetBackgroundColour(m_colBg
);
553 page
->GetWidget()->Refresh();
555 wxControl
*ctrl2
= page
->GetWidget2();
558 ctrl2
->SetBackgroundColour(m_colFg
);
562 wxLogMessage(_T("Colour selection dialog not available in current build."));
566 void WidgetsFrame
::OnSetFont(wxCommandEvent
& WXUNUSED(event
))
569 WidgetsPage
*page
= wxStaticCast(m_book
->GetCurrentPage(), WidgetsPage
);
571 m_font
= page
->GetFont();
573 wxFont font
= wxGetFontFromUser(this, m_font
);
579 page
->GetWidget()->SetFont(m_font
);
580 page
->GetWidget()->Refresh();
582 wxControl
*ctrl2
= page
->GetWidget2();
585 ctrl2
->SetFont(m_font
);
589 wxLogMessage(_T("Font selection dialog not available in current build."));
593 void WidgetsFrame
::OnEnable(wxCommandEvent
& event
)
595 WidgetsPage
*page
= wxStaticCast(m_book
->GetCurrentPage(), WidgetsPage
);
596 page
->GetWidget()->Enable(event
.IsChecked());
599 void WidgetsFrame
::OnSetBorder(wxCommandEvent
& event
)
602 switch ( event
.GetId() )
604 case Widgets_BorderNone
: border
= wxBORDER_NONE
; break;
605 case Widgets_BorderStatic
: border
= wxBORDER_STATIC
; break;
606 case Widgets_BorderSimple
: border
= wxBORDER_SIMPLE
; break;
607 case Widgets_BorderRaised
: border
= wxBORDER_RAISED
; break;
608 case Widgets_BorderSunken
: border
= wxBORDER_SUNKEN
; break;
609 case Widgets_BorderDouble
: border
= wxBORDER_DOUBLE
; break;
612 wxFAIL_MSG( _T("unknown border style") );
615 case Widgets_BorderDefault
: border
= wxBORDER_DEFAULT
; break;
618 WidgetsPage
::ms_defaultFlags
&= ~wxBORDER_MASK
;
619 WidgetsPage
::ms_defaultFlags
|= border
;
621 WidgetsPage
*page
= wxStaticCast(m_book
->GetCurrentPage(), WidgetsPage
);
622 page
->RecreateWidget();
625 #endif // wxUSE_MENUS
627 // ----------------------------------------------------------------------------
629 // ----------------------------------------------------------------------------
631 WidgetsPageInfo
::WidgetsPageInfo(Constructor ctor
, const wxChar
*label
)
638 // dummy sorting: add and immediately sort in the list according to label
639 if(WidgetsPage
::ms_widgetPages
)
641 WidgetsPageInfo
*node_prev
= WidgetsPage
::ms_widgetPages
;
642 if ( wxStrcmp(label
, node_prev
->GetLabel().c_str()) < 0 )
646 WidgetsPage
::ms_widgetPages
= this;
650 WidgetsPageInfo
*node_next
;
653 node_next
= node_prev
->GetNext();
656 // add if between two
657 if ( wxStrcmp(label
, node_next
->GetLabel().c_str()) < 0 )
659 node_prev
->SetNext(this);
661 // force to break loop
668 node_prev
->SetNext(this);
671 node_prev
= node_next
;
679 WidgetsPage
::ms_widgetPages
= this;
683 // ----------------------------------------------------------------------------
685 // ----------------------------------------------------------------------------
687 int WidgetsPage
::ms_defaultFlags
= wxBORDER_DEFAULT
;
688 WidgetsPageInfo
*WidgetsPage
::ms_widgetPages
= NULL
;
690 WidgetsPage
::WidgetsPage(wxBookCtrlBase
*book
)
691 : wxPanel(book
, wxID_ANY
,
692 wxDefaultPosition
, wxDefaultSize
,
693 wxNO_FULL_REPAINT_ON_RESIZE
|
699 wxSizer
*WidgetsPage
::CreateSizerWithText(wxControl
*control
,
703 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
704 wxTextCtrl
*text
= new wxTextCtrl(this, id
, wxEmptyString
,
705 wxDefaultPosition
, wxDefaultSize
, wxTE_PROCESS_ENTER
);
707 sizerRow
->Add(control
, 0, wxRIGHT
| wxALIGN_CENTRE_VERTICAL
, 5);
708 sizerRow
->Add(text
, 1, wxLEFT
| wxALIGN_CENTRE_VERTICAL
, 5);
716 // create a sizer containing a label and a text ctrl
717 wxSizer
*WidgetsPage
::CreateSizerWithTextAndLabel(const wxString
& label
,
721 return CreateSizerWithText(new wxStaticText(this, wxID_ANY
, label
),
725 // create a sizer containing a button and a text ctrl
726 wxSizer
*WidgetsPage
::CreateSizerWithTextAndButton(wxWindowID idBtn
,
727 const wxString
& label
,
731 return CreateSizerWithText(new wxButton(this, idBtn
, label
), id
, ppText
);
734 wxCheckBox
*WidgetsPage
::CreateCheckBoxAndAddToSizer(wxSizer
*sizer
,
735 const wxString
& label
,
738 wxCheckBox
*checkbox
= new wxCheckBox(this, id
, label
);
739 sizer
->Add(checkbox
, 0, wxLEFT
| wxRIGHT
, 5);
740 sizer
->Add(0, 2, 0, wxGROW
); // spacer