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,
63 #endif // wxUSE_TOOLTIPS
70 // ----------------------------------------------------------------------------
72 // ----------------------------------------------------------------------------
74 // Define a new application type, each program should derive a class from wxApp
75 class WidgetsApp
: public wxApp
78 // override base class virtuals
79 // ----------------------------
81 // this one is called on application startup and is a good place for the app
82 // initialization (doing it here and not in the ctor allows to have an error
83 // return: if OnInit() returns false, the application terminates)
84 virtual bool OnInit();
87 // Define a new frame type: this is going to be our main frame
88 class WidgetsFrame
: public wxFrame
92 WidgetsFrame(const wxString
& title
);
93 virtual ~WidgetsFrame();
98 void OnButtonClearLog(wxCommandEvent
& event
);
100 void OnExit(wxCommandEvent
& event
);
104 void OnSetTooltip(wxCommandEvent
& event
);
105 #endif // wxUSE_TOOLTIPS
106 void OnSetFgCol(wxCommandEvent
& event
);
107 void OnSetBgCol(wxCommandEvent
& event
);
108 void OnSetFont(wxCommandEvent
& event
);
109 void OnEnable(wxCommandEvent
& event
);
110 #endif // wxUSE_MENUS
112 // initialize the book: add all pages to it
116 // the panel containing everything
120 // the listbox for logging messages
121 wxListBox
*m_lboxLog
;
123 // the log target we use to redirect messages to the listbox
127 // the book containing the test pages
128 wxBookCtrlBase
*m_book
;
130 // and the image list for it
131 wxImageList
*m_imaglist
;
134 // last chosen fg/bg colours and font
138 #endif // wxUSE_MENUS
140 // any class wishing to process wxWidgets events must use this macro
141 DECLARE_EVENT_TABLE()
145 // A log target which just redirects the messages to a listbox
146 class LboxLogger
: public wxLog
149 LboxLogger(wxListBox
*lbox
, wxLog
*logOld
)
152 //m_lbox->Disable(); -- looks ugly under MSW
156 virtual ~LboxLogger()
158 wxLog::SetActiveTarget(m_logOld
);
162 // implement sink functions
163 virtual void DoLog(wxLogLevel level
, const wxChar
*szString
, time_t t
)
165 // don't put trace messages into listbox or we can get into infinite
167 if ( level
== wxLOG_Trace
)
171 // cast is needed to call protected method
172 ((LboxLogger
*)m_logOld
)->DoLog(level
, szString
, t
);
177 wxLog::DoLog(level
, szString
, t
);
181 virtual void DoLogString(const wxChar
*szString
, time_t WXUNUSED(t
))
187 #ifdef __WXUNIVERSAL__
188 m_lbox
->AppendAndEnsureVisible(msg
);
189 #else // other ports don't have this method yet
191 m_lbox
->SetFirstItem(m_lbox
->GetCount() - 1);
195 // the control we use
198 // the old log target
204 WX_DEFINE_ARRAY_PTR(WidgetsPage
*, ArrayWidgetsPage
);
206 // ----------------------------------------------------------------------------
208 // ----------------------------------------------------------------------------
210 IMPLEMENT_APP(WidgetsApp
)
212 // ----------------------------------------------------------------------------
214 // ----------------------------------------------------------------------------
216 BEGIN_EVENT_TABLE(WidgetsFrame
, wxFrame
)
218 EVT_BUTTON(Widgets_ClearLog
, WidgetsFrame::OnButtonClearLog
)
220 EVT_BUTTON(Widgets_Quit
, WidgetsFrame::OnExit
)
223 EVT_MENU(Widgets_SetTooltip
, WidgetsFrame::OnSetTooltip
)
224 #endif // wxUSE_TOOLTIPS
226 EVT_MENU(Widgets_SetFgColour
, WidgetsFrame::OnSetFgCol
)
227 EVT_MENU(Widgets_SetBgColour
, WidgetsFrame::OnSetBgCol
)
228 EVT_MENU(Widgets_SetFont
, WidgetsFrame::OnSetFont
)
229 EVT_MENU(Widgets_Enable
, WidgetsFrame::OnEnable
)
231 EVT_MENU(wxID_EXIT
, WidgetsFrame::OnExit
)
234 // ============================================================================
236 // ============================================================================
238 // ----------------------------------------------------------------------------
240 // ----------------------------------------------------------------------------
242 bool WidgetsApp::OnInit()
244 if ( !wxApp::OnInit() )
247 // the reason for having these ifdef's is that I often run two copies of
248 // this sample side by side and it is useful to see which one is which
250 #if defined(__WXUNIVERSAL__)
251 title
= _T("wxUniv/");
254 #if defined(__WXMSW__)
255 title
+= _T("wxMSW");
256 #elif defined(__WXGTK__)
257 title
+= _T("wxGTK");
258 #elif defined(__WXMAC__)
259 title
+= _T("wxMAC");
260 #elif defined(__WXMOTIF__)
261 title
+= _T("wxMOTIF");
263 title
+= _T("wxWidgets");
266 wxFrame
*frame
= new WidgetsFrame(title
+ _T(" widgets demo"));
269 //wxLog::AddTraceMask(_T("listbox"));
270 //wxLog::AddTraceMask(_T("scrollbar"));
271 //wxLog::AddTraceMask(_T("focus"));
276 // ----------------------------------------------------------------------------
277 // WidgetsFrame construction
278 // ----------------------------------------------------------------------------
280 WidgetsFrame::WidgetsFrame(const wxString
& title
)
281 : wxFrame(NULL
, wxID_ANY
, title
,
282 wxPoint(0, 50), wxDefaultSize
,
283 wxDEFAULT_FRAME_STYLE
|
284 wxNO_FULL_REPAINT_ON_RESIZE
|
290 m_lboxLog
= (wxListBox
*)NULL
;
291 m_logTarget
= (wxLog
*)NULL
;
293 m_book
= (wxBookCtrlBase
*)NULL
;
294 m_imaglist
= (wxImageList
*)NULL
;
297 // create the menubar
298 wxMenuBar
*mbar
= new wxMenuBar
;
299 wxMenu
*menuWidget
= new wxMenu
;
301 menuWidget
->Append(Widgets_SetTooltip
, _T("Set &tooltip...\tCtrl-T"));
302 menuWidget
->AppendSeparator();
303 #endif // wxUSE_TOOLTIPS
304 menuWidget
->Append(Widgets_SetFgColour
, _T("Set &foreground...\tCtrl-F"));
305 menuWidget
->Append(Widgets_SetBgColour
, _T("Set &background...\tCtrl-B"));
306 menuWidget
->Append(Widgets_SetFont
, _T("Set f&ont...\tCtrl-O"));
307 menuWidget
->AppendCheckItem(Widgets_Enable
, _T("&Enable/disable\tCtrl-E"));
308 menuWidget
->AppendSeparator();
309 menuWidget
->Append(wxID_EXIT
, _T("&Quit\tCtrl-Q"));
310 mbar
->Append(menuWidget
, _T("&Widget"));
313 mbar
->Check(Widgets_Enable
, true);
314 #endif // wxUSE_MENUS
317 m_panel
= new wxPanel(this, wxID_ANY
,
318 wxDefaultPosition
, wxDefaultSize
, wxCLIP_CHILDREN
);
320 wxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
322 // we have 2 panes: book with pages demonstrating the controls in the
323 // upper one and the log window with some buttons in the lower
325 int style
= wxNO_FULL_REPAINT_ON_RESIZE
|wxCLIP_CHILDREN
|wxBK_DEFAULT
;
326 // Uncomment to suppress page theme (draw in solid colour)
327 //style |= wxNB_NOPAGETHEME;
329 m_book
= new wxBookCtrl(m_panel
, wxID_ANY
, wxDefaultPosition
,
331 wxSize(500, wxDefaultCoord
), // under Motif, height is a function of the width...
338 #ifndef __SMARTPHONE__
339 // the lower one only has the log listbox and a button to clear it
341 wxSizer
*sizerDown
= new wxStaticBoxSizer(
342 new wxStaticBox( m_panel
, wxID_ANY
, _T("&Log window") ),
345 m_lboxLog
= new wxListBox(m_panel
, wxID_ANY
);
346 sizerDown
->Add(m_lboxLog
, 1, wxGROW
| wxALL
, 5);
347 sizerDown
->SetMinSize(100, 150);
349 wxSizer
*sizerDown
= new wxBoxSizer(wxVERTICAL
);
352 wxBoxSizer
*sizerBtns
= new wxBoxSizer(wxHORIZONTAL
);
355 btn
= new wxButton(m_panel
, Widgets_ClearLog
, _T("Clear &log"));
357 sizerBtns
->Add(10, 0); // spacer
359 btn
= new wxButton(m_panel
, Widgets_Quit
, _T("E&xit"));
361 sizerDown
->Add(sizerBtns
, 0, wxALL
| wxALIGN_RIGHT
, 5);
363 // put everything together
364 sizerTop
->Add(m_book
, 1, wxGROW
| (wxALL
& ~(wxTOP
| wxBOTTOM
)), 10);
365 sizerTop
->Add(0, 5, 0, wxGROW
); // spacer in between
366 sizerTop
->Add(sizerDown
, 0, wxGROW
| (wxALL
& ~wxTOP
), 10);
368 #else // !__SMARTPHONE__/__SMARTPHONE__
370 sizerTop
->Add(m_book
, 1, wxGROW
| wxALL
);
372 #endif // __SMARTPHONE__
374 m_panel
->SetSizer(sizerTop
);
377 sizerTop
->SetSizeHints(this);
379 #if USE_LOG && !defined(__WXCOCOA__)
380 // wxCocoa's listbox is too flakey to use for logging right now
381 // now that everything is created we can redirect the log messages to the
383 m_logTarget
= new LboxLogger(m_lboxLog
, wxLog::GetActiveTarget());
384 wxLog::SetActiveTarget(m_logTarget
);
388 void WidgetsFrame::InitBook()
390 m_imaglist
= new wxImageList(32, 32);
392 ArrayWidgetsPage pages
;
393 wxArrayString labels
;
395 // we need to first create all pages and only then add them to the book
396 // as we need the image list first
397 WidgetsPageInfo
*info
= WidgetsPage::ms_widgetPages
;
400 WidgetsPage
*page
= (*info
->GetCtor())(m_book
, m_imaglist
);
403 labels
.Add(info
->GetLabel());
405 info
= info
->GetNext();
408 m_book
->SetImageList(m_imaglist
);
411 size_t count
= pages
.GetCount();
412 for ( size_t n
= 0; n
< count
; n
++ )
417 false, // don't select
422 wxColour colour = m_book->MSWGetBgColourForChild(pages[n]);
423 pages[n]->SetBackgroundColour(colour);
428 WidgetsFrame::~WidgetsFrame()
436 // ----------------------------------------------------------------------------
437 // WidgetsFrame event handlers
438 // ----------------------------------------------------------------------------
440 void WidgetsFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
446 void WidgetsFrame::OnButtonClearLog(wxCommandEvent
& WXUNUSED(event
))
456 void WidgetsFrame::OnSetTooltip(wxCommandEvent
& WXUNUSED(event
))
458 static wxString s_tip
= _T("This is a tooltip");
460 wxString s
= wxGetTextFromUser
462 _T("Tooltip text: "),
463 _T("Widgets sample"),
473 if( wxMessageBox( _T("Test multiline tooltip text?"),
474 _T("Widgets sample"),
479 s
= _T("#1 ") + s_tip
+ _T("\n") + _T("#2 ") + s_tip
;
482 WidgetsPage
*page
= wxStaticCast(m_book
->GetCurrentPage(), WidgetsPage
);
483 page
->GetWidget()->SetToolTip(s
);
485 wxControl
*ctrl2
= page
->GetWidget2();
487 ctrl2
->SetToolTip(s
);
490 #endif // wxUSE_TOOLTIPS
492 void WidgetsFrame::OnSetFgCol(wxCommandEvent
& WXUNUSED(event
))
495 // allow for debugging the default colour the first time this is called
496 WidgetsPage
*page
= wxStaticCast(m_book
->GetCurrentPage(), WidgetsPage
);
498 m_colFg
= page
->GetForegroundColour();
500 wxColour col
= wxGetColourFromUser(this, m_colFg
);
506 page
->GetWidget()->SetForegroundColour(m_colFg
);
507 page
->GetWidget()->Refresh();
509 wxControl
*ctrl2
= page
->GetWidget2();
512 ctrl2
->SetForegroundColour(m_colFg
);
516 wxLogMessage(_T("Colour selection dialog not available in current build."));
520 void WidgetsFrame::OnSetBgCol(wxCommandEvent
& WXUNUSED(event
))
523 WidgetsPage
*page
= wxStaticCast(m_book
->GetCurrentPage(), WidgetsPage
);
525 m_colBg
= page
->GetBackgroundColour();
527 wxColour col
= wxGetColourFromUser(this, m_colBg
);
533 page
->GetWidget()->SetBackgroundColour(m_colBg
);
534 page
->GetWidget()->Refresh();
536 wxControl
*ctrl2
= page
->GetWidget2();
539 ctrl2
->SetBackgroundColour(m_colFg
);
543 wxLogMessage(_T("Colour selection dialog not available in current build."));
547 void WidgetsFrame::OnSetFont(wxCommandEvent
& WXUNUSED(event
))
550 WidgetsPage
*page
= wxStaticCast(m_book
->GetCurrentPage(), WidgetsPage
);
552 m_font
= page
->GetFont();
554 wxFont font
= wxGetFontFromUser(this, m_font
);
560 page
->GetWidget()->SetFont(m_font
);
561 page
->GetWidget()->Refresh();
563 wxControl
*ctrl2
= page
->GetWidget2();
566 ctrl2
->SetFont(m_font
);
570 wxLogMessage(_T("Font selection dialog not available in current build."));
574 void WidgetsFrame::OnEnable(wxCommandEvent
& event
)
576 WidgetsPage
*page
= wxStaticCast(m_book
->GetCurrentPage(), WidgetsPage
);
577 page
->GetWidget()->Enable(event
.IsChecked());
580 #endif // wxUSE_MENUS
582 // ----------------------------------------------------------------------------
584 // ----------------------------------------------------------------------------
586 WidgetsPageInfo
*WidgetsPage::ms_widgetPages
= NULL
;
588 WidgetsPageInfo::WidgetsPageInfo(Constructor ctor
, const wxChar
*label
)
595 // dummy sorting: add and immediately sort on list according to label
597 if(WidgetsPage::ms_widgetPages
)
599 WidgetsPageInfo
*node_prev
= WidgetsPage::ms_widgetPages
;
600 if(wxStrcmp(label
,node_prev
->GetLabel().c_str())<0)
604 WidgetsPage::ms_widgetPages
= this;
608 WidgetsPageInfo
*node_next
;
611 node_next
= node_prev
->GetNext();
614 // add if between two
615 if(wxStrcmp(label
,node_next
->GetLabel().c_str())<0)
617 node_prev
->SetNext(this);
619 // force to break loop
626 node_prev
->SetNext(this);
629 node_prev
= node_next
;
637 WidgetsPage::ms_widgetPages
= this;
643 // ----------------------------------------------------------------------------
645 // ----------------------------------------------------------------------------
647 WidgetsPage::WidgetsPage(wxBookCtrlBase
*book
)
648 : wxPanel(book
, wxID_ANY
,
649 wxDefaultPosition
, wxDefaultSize
,
650 wxNO_FULL_REPAINT_ON_RESIZE
|
656 wxSizer
*WidgetsPage::CreateSizerWithText(wxControl
*control
,
660 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
661 wxTextCtrl
*text
= new wxTextCtrl(this, id
, wxEmptyString
,
662 wxDefaultPosition
, wxDefaultSize
, wxTE_PROCESS_ENTER
);
664 sizerRow
->Add(control
, 0, wxRIGHT
| wxALIGN_CENTRE_VERTICAL
, 5);
665 sizerRow
->Add(text
, 1, wxLEFT
| wxALIGN_CENTRE_VERTICAL
, 5);
673 // create a sizer containing a label and a text ctrl
674 wxSizer
*WidgetsPage::CreateSizerWithTextAndLabel(const wxString
& label
,
678 return CreateSizerWithText(new wxStaticText(this, wxID_ANY
, label
),
682 // create a sizer containing a button and a text ctrl
683 wxSizer
*WidgetsPage::CreateSizerWithTextAndButton(wxWindowID idBtn
,
684 const wxString
& label
,
688 return CreateSizerWithText(new wxButton(this, idBtn
, label
), id
, ppText
);
691 wxCheckBox
*WidgetsPage::CreateCheckBoxAndAddToSizer(wxSizer
*sizer
,
692 const wxString
& label
,
695 wxCheckBox
*checkbox
= new wxCheckBox(this, id
, label
);
696 sizer
->Add(checkbox
, 0, wxLEFT
| wxRIGHT
, 5);
697 sizer
->Add(0, 2, 0, wxGROW
); // spacer