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
446 wxColour colour = m_book->MSWGetBgColourForChild(pages[n]);
447 pages[n]->SetBackgroundColour(colour);
452 WidgetsFrame::~WidgetsFrame()
460 // ----------------------------------------------------------------------------
461 // WidgetsFrame event handlers
462 // ----------------------------------------------------------------------------
464 void WidgetsFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
470 void WidgetsFrame::OnButtonClearLog(wxCommandEvent
& WXUNUSED(event
))
480 void WidgetsFrame::OnSetTooltip(wxCommandEvent
& WXUNUSED(event
))
482 static wxString s_tip
= _T("This is a tooltip");
484 wxString s
= wxGetTextFromUser
486 _T("Tooltip text: "),
487 _T("Widgets sample"),
497 if( wxMessageBox( _T("Test multiline tooltip text?"),
498 _T("Widgets sample"),
503 s
= _T("#1 ") + s_tip
+ _T("\n") + _T("#2 ") + s_tip
;
506 WidgetsPage
*page
= wxStaticCast(m_book
->GetCurrentPage(), WidgetsPage
);
507 page
->GetWidget()->SetToolTip(s
);
509 wxControl
*ctrl2
= page
->GetWidget2();
511 ctrl2
->SetToolTip(s
);
514 #endif // wxUSE_TOOLTIPS
516 void WidgetsFrame::OnSetFgCol(wxCommandEvent
& WXUNUSED(event
))
519 // allow for debugging the default colour the first time this is called
520 WidgetsPage
*page
= wxStaticCast(m_book
->GetCurrentPage(), WidgetsPage
);
522 m_colFg
= page
->GetForegroundColour();
524 wxColour col
= wxGetColourFromUser(this, m_colFg
);
530 page
->GetWidget()->SetForegroundColour(m_colFg
);
531 page
->GetWidget()->Refresh();
533 wxControl
*ctrl2
= page
->GetWidget2();
536 ctrl2
->SetForegroundColour(m_colFg
);
540 wxLogMessage(_T("Colour selection dialog not available in current build."));
544 void WidgetsFrame::OnSetBgCol(wxCommandEvent
& WXUNUSED(event
))
547 WidgetsPage
*page
= wxStaticCast(m_book
->GetCurrentPage(), WidgetsPage
);
549 m_colBg
= page
->GetBackgroundColour();
551 wxColour col
= wxGetColourFromUser(this, m_colBg
);
557 page
->GetWidget()->SetBackgroundColour(m_colBg
);
558 page
->GetWidget()->Refresh();
560 wxControl
*ctrl2
= page
->GetWidget2();
563 ctrl2
->SetBackgroundColour(m_colFg
);
567 wxLogMessage(_T("Colour selection dialog not available in current build."));
571 void WidgetsFrame::OnSetFont(wxCommandEvent
& WXUNUSED(event
))
574 WidgetsPage
*page
= wxStaticCast(m_book
->GetCurrentPage(), WidgetsPage
);
576 m_font
= page
->GetFont();
578 wxFont font
= wxGetFontFromUser(this, m_font
);
584 page
->GetWidget()->SetFont(m_font
);
585 page
->GetWidget()->Refresh();
587 wxControl
*ctrl2
= page
->GetWidget2();
590 ctrl2
->SetFont(m_font
);
594 wxLogMessage(_T("Font selection dialog not available in current build."));
598 void WidgetsFrame::OnEnable(wxCommandEvent
& event
)
600 WidgetsPage
*page
= wxStaticCast(m_book
->GetCurrentPage(), WidgetsPage
);
601 page
->GetWidget()->Enable(event
.IsChecked());
604 void WidgetsFrame::OnSetBorder(wxCommandEvent
& event
)
607 switch ( event
.GetId() )
609 case Widgets_BorderNone
: border
= wxBORDER_NONE
; break;
610 case Widgets_BorderStatic
: border
= wxBORDER_STATIC
; break;
611 case Widgets_BorderSimple
: border
= wxBORDER_SIMPLE
; break;
612 case Widgets_BorderRaised
: border
= wxBORDER_RAISED
; break;
613 case Widgets_BorderSunken
: border
= wxBORDER_SUNKEN
; break;
614 case Widgets_BorderDouble
: border
= wxBORDER_DOUBLE
; break;
617 wxFAIL_MSG( _T("unknown border style") );
620 case Widgets_BorderDefault
: border
= wxBORDER_DEFAULT
; break;
623 WidgetsPage::ms_defaultFlags
&= ~wxBORDER_MASK
;
624 WidgetsPage::ms_defaultFlags
|= border
;
626 WidgetsPage
*page
= wxStaticCast(m_book
->GetCurrentPage(), WidgetsPage
);
627 page
->RecreateWidget();
630 #endif // wxUSE_MENUS
632 // ----------------------------------------------------------------------------
634 // ----------------------------------------------------------------------------
636 WidgetsPageInfo::WidgetsPageInfo(Constructor ctor
, const wxChar
*label
)
643 // dummy sorting: add and immediately sort on list according to label
645 if(WidgetsPage::ms_widgetPages
)
647 WidgetsPageInfo
*node_prev
= WidgetsPage::ms_widgetPages
;
648 if(wxStrcmp(label
,node_prev
->GetLabel().c_str())<0)
652 WidgetsPage::ms_widgetPages
= this;
656 WidgetsPageInfo
*node_next
;
659 node_next
= node_prev
->GetNext();
662 // add if between two
663 if(wxStrcmp(label
,node_next
->GetLabel().c_str())<0)
665 node_prev
->SetNext(this);
667 // force to break loop
674 node_prev
->SetNext(this);
677 node_prev
= node_next
;
685 WidgetsPage::ms_widgetPages
= this;
691 // ----------------------------------------------------------------------------
693 // ----------------------------------------------------------------------------
695 int WidgetsPage::ms_defaultFlags
= wxBORDER_DEFAULT
;
696 WidgetsPageInfo
*WidgetsPage::ms_widgetPages
= NULL
;
698 WidgetsPage::WidgetsPage(wxBookCtrlBase
*book
)
699 : wxPanel(book
, wxID_ANY
,
700 wxDefaultPosition
, wxDefaultSize
,
701 wxNO_FULL_REPAINT_ON_RESIZE
|
707 wxSizer
*WidgetsPage::CreateSizerWithText(wxControl
*control
,
711 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
712 wxTextCtrl
*text
= new wxTextCtrl(this, id
, wxEmptyString
,
713 wxDefaultPosition
, wxDefaultSize
, wxTE_PROCESS_ENTER
);
715 sizerRow
->Add(control
, 0, wxRIGHT
| wxALIGN_CENTRE_VERTICAL
, 5);
716 sizerRow
->Add(text
, 1, wxLEFT
| wxALIGN_CENTRE_VERTICAL
, 5);
724 // create a sizer containing a label and a text ctrl
725 wxSizer
*WidgetsPage::CreateSizerWithTextAndLabel(const wxString
& label
,
729 return CreateSizerWithText(new wxStaticText(this, wxID_ANY
, label
),
733 // create a sizer containing a button and a text ctrl
734 wxSizer
*WidgetsPage::CreateSizerWithTextAndButton(wxWindowID idBtn
,
735 const wxString
& label
,
739 return CreateSizerWithText(new wxButton(this, idBtn
, label
), id
, ppText
);
742 wxCheckBox
*WidgetsPage::CreateCheckBoxAndAddToSizer(wxSizer
*sizer
,
743 const wxString
& label
,
746 wxCheckBox
*checkbox
= new wxCheckBox(this, id
, label
);
747 sizer
->Add(checkbox
, 0, wxLEFT
| wxRIGHT
, 5);
748 sizer
->Add(0, 2, 0, wxGROW
); // spacer