1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
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"
42 #include "wx/sysopt.h"
43 #include "wx/bookctrl.h"
45 #include "wx/colordlg.h"
46 #include "wx/fontdlg.h"
47 #include "wx/textdlg.h"
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
58 Widgets_ClearLog
= 100,
62 #endif // wxUSE_TOOLTIPS
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
73 // Define a new application type, each program should derive a class from wxApp
74 class WidgetsApp
: public wxApp
77 // override base class virtuals
78 // ----------------------------
80 // this one is called on application startup and is a good place for the app
81 // initialization (doing it here and not in the ctor allows to have an error
82 // return: if OnInit() returns false, the application terminates)
83 virtual bool OnInit();
86 // Define a new frame type: this is going to be our main frame
87 class WidgetsFrame
: public wxFrame
91 WidgetsFrame(const wxString
& title
);
92 virtual ~WidgetsFrame();
97 void OnButtonClearLog(wxCommandEvent
& event
);
99 void OnExit(wxCommandEvent
& event
);
103 void OnSetTooltip(wxCommandEvent
& event
);
104 #endif // wxUSE_TOOLTIPS
105 void OnSetFgCol(wxCommandEvent
& event
);
106 void OnSetBgCol(wxCommandEvent
& event
);
107 void OnSetFont(wxCommandEvent
& event
);
108 void OnEnable(wxCommandEvent
& event
);
109 #endif // wxUSE_MENUS
111 // initialize the book: add all pages to it
115 // the panel containing everything
119 // the listbox for logging messages
120 wxListBox
*m_lboxLog
;
122 // the log target we use to redirect messages to the listbox
126 // the book containing the test pages
129 // and the image list for it
130 wxImageList
*m_imaglist
;
133 // last chosen fg/bg colours and font
137 #endif // wxUSE_MENUS
139 // any class wishing to process wxWidgets events must use this macro
140 DECLARE_EVENT_TABLE()
144 // A log target which just redirects the messages to a listbox
145 class LboxLogger
: public wxLog
148 LboxLogger(wxListBox
*lbox
, wxLog
*logOld
)
151 //m_lbox->Disable(); -- looks ugly under MSW
155 virtual ~LboxLogger()
157 wxLog::SetActiveTarget(m_logOld
);
161 // implement sink functions
162 virtual void DoLog(wxLogLevel level
, const wxChar
*szString
, time_t t
)
164 // don't put trace messages into listbox or we can get into infinite
166 if ( level
== wxLOG_Trace
)
170 // cast is needed to call protected method
171 ((LboxLogger
*)m_logOld
)->DoLog(level
, szString
, t
);
176 wxLog::DoLog(level
, szString
, t
);
180 virtual void DoLogString(const wxChar
*szString
, time_t WXUNUSED(t
))
186 #ifdef __WXUNIVERSAL__
187 m_lbox
->AppendAndEnsureVisible(msg
);
188 #else // other ports don't have this method yet
190 m_lbox
->SetFirstItem(m_lbox
->GetCount() - 1);
194 // the control we use
197 // the old log target
203 WX_DEFINE_ARRAY_PTR(WidgetsPage
*, ArrayWidgetsPage
);
205 // ----------------------------------------------------------------------------
207 // ----------------------------------------------------------------------------
209 IMPLEMENT_APP(WidgetsApp
)
211 // ----------------------------------------------------------------------------
213 // ----------------------------------------------------------------------------
215 BEGIN_EVENT_TABLE(WidgetsFrame
, wxFrame
)
217 EVT_BUTTON(Widgets_ClearLog
, WidgetsFrame::OnButtonClearLog
)
219 EVT_BUTTON(Widgets_Quit
, WidgetsFrame::OnExit
)
222 EVT_MENU(Widgets_SetTooltip
, WidgetsFrame::OnSetTooltip
)
223 #endif // wxUSE_TOOLTIPS
225 EVT_MENU(Widgets_SetFgColour
, WidgetsFrame::OnSetFgCol
)
226 EVT_MENU(Widgets_SetBgColour
, WidgetsFrame::OnSetBgCol
)
227 EVT_MENU(Widgets_SetFont
, WidgetsFrame::OnSetFont
)
228 EVT_MENU(Widgets_Enable
, WidgetsFrame::OnEnable
)
230 EVT_MENU(wxID_EXIT
, WidgetsFrame::OnExit
)
233 // ============================================================================
235 // ============================================================================
237 // ----------------------------------------------------------------------------
239 // ----------------------------------------------------------------------------
241 bool WidgetsApp::OnInit()
243 if ( !wxApp::OnInit() )
246 // the reason for having these ifdef's is that I often run two copies of
247 // this sample side by side and it is useful to see which one is which
249 #if defined(__WXUNIVERSAL__)
250 title
= _T("wxUniv/");
253 #if defined(__WXMSW__)
254 title
+= _T("wxMSW");
255 #elif defined(__WXGTK__)
256 title
+= _T("wxGTK");
257 #elif defined(__WXMAC__)
258 title
+= _T("wxMAC");
259 #elif defined(__WXMOTIF__)
260 title
+= _T("wxMOTIF");
262 title
+= _T("wxWidgets");
265 wxFrame
*frame
= new WidgetsFrame(title
+ _T(" widgets demo"));
268 //wxLog::AddTraceMask(_T("listbox"));
269 //wxLog::AddTraceMask(_T("scrollbar"));
270 //wxLog::AddTraceMask(_T("focus"));
275 // ----------------------------------------------------------------------------
276 // WidgetsFrame construction
277 // ----------------------------------------------------------------------------
279 WidgetsFrame::WidgetsFrame(const wxString
& title
)
280 : wxFrame(NULL
, wxID_ANY
, title
,
281 wxPoint(0, 50), wxDefaultSize
,
282 wxDEFAULT_FRAME_STYLE
|
283 wxNO_FULL_REPAINT_ON_RESIZE
|
289 m_lboxLog
= (wxListBox
*)NULL
;
290 m_logTarget
= (wxLog
*)NULL
;
292 m_book
= (wxBookCtrl
*)NULL
;
293 m_imaglist
= (wxImageList
*)NULL
;
296 // create the menubar
297 wxMenuBar
*mbar
= new wxMenuBar
;
298 wxMenu
*menuWidget
= new wxMenu
;
300 menuWidget
->Append(Widgets_SetTooltip
, _T("Set &tooltip...\tCtrl-T"));
301 menuWidget
->AppendSeparator();
302 #endif // wxUSE_TOOLTIPS
303 menuWidget
->Append(Widgets_SetFgColour
, _T("Set &foreground...\tCtrl-F"));
304 menuWidget
->Append(Widgets_SetBgColour
, _T("Set &background...\tCtrl-B"));
305 menuWidget
->Append(Widgets_SetFont
, _T("Set f&ont...\tCtrl-O"));
306 menuWidget
->AppendCheckItem(Widgets_Enable
, _T("&Enable/disable\tCtrl-E"));
307 menuWidget
->AppendSeparator();
308 menuWidget
->Append(wxID_EXIT
, _T("&Quit\tCtrl-Q"));
309 mbar
->Append(menuWidget
, _T("&Widget"));
312 mbar
->Check(Widgets_Enable
, true);
313 #endif // wxUSE_MENUS
316 m_panel
= new wxPanel(this, wxID_ANY
,
317 wxDefaultPosition
, wxDefaultSize
, wxCLIP_CHILDREN
);
319 wxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
321 // we have 2 panes: book with pages demonstrating the controls in the
322 // upper one and the log window with some buttons in the lower
324 int style
= wxNO_FULL_REPAINT_ON_RESIZE
|wxCLIP_CHILDREN
|wxBC_DEFAULT
;
325 // Uncomment to suppress page theme (draw in solid colour)
326 //style |= wxNB_NOPAGETHEME;
328 m_book
= new wxBookCtrl(m_panel
, wxID_ANY
, wxDefaultPosition
,
329 wxDefaultSize
, style
);
332 #ifndef __SMARTPHONE__
333 // the lower one only has the log listbox and a button to clear it
335 wxSizer
*sizerDown
= new wxStaticBoxSizer(
336 new wxStaticBox( m_panel
, wxID_ANY
, _T("&Log window") ),
339 m_lboxLog
= new wxListBox(m_panel
, wxID_ANY
);
340 sizerDown
->Add(m_lboxLog
, 1, wxGROW
| wxALL
, 5);
341 sizerDown
->SetMinSize(100, 150);
343 wxSizer
*sizerDown
= new wxBoxSizer(wxVERTICAL
);
346 wxBoxSizer
*sizerBtns
= new wxBoxSizer(wxHORIZONTAL
);
349 btn
= new wxButton(m_panel
, Widgets_ClearLog
, _T("Clear &log"));
351 sizerBtns
->Add(10, 0); // spacer
353 btn
= new wxButton(m_panel
, Widgets_Quit
, _T("E&xit"));
355 sizerDown
->Add(sizerBtns
, 0, wxALL
| wxALIGN_RIGHT
, 5);
357 // put everything together
358 sizerTop
->Add(m_book
, 1, wxGROW
| (wxALL
& ~(wxTOP
| wxBOTTOM
)), 10);
359 sizerTop
->Add(0, 5, 0, wxGROW
); // spacer in between
360 sizerTop
->Add(sizerDown
, 0, wxGROW
| (wxALL
& ~wxTOP
), 10);
362 #else // !__SMARTPHONE__/__SMARTPHONE__
364 sizerTop
->Add(m_book
, 1, wxGROW
| wxALL
);
366 #endif // __SMARTPHONE__
368 m_panel
->SetSizer(sizerTop
);
371 sizerTop
->SetSizeHints(this);
373 #if USE_LOG && !defined(__WXCOCOA__)
374 // wxCocoa's listbox is too flakey to use for logging right now
375 // now that everything is created we can redirect the log messages to the
377 m_logTarget
= new LboxLogger(m_lboxLog
, wxLog::GetActiveTarget());
378 wxLog::SetActiveTarget(m_logTarget
);
382 void WidgetsFrame::InitBook()
384 m_imaglist
= new wxImageList(32, 32);
386 ArrayWidgetsPage pages
;
387 wxArrayString labels
;
389 // we need to first create all pages and only then add them to the book
390 // as we need the image list first
391 WidgetsPageInfo
*info
= WidgetsPage::ms_widgetPages
;
394 WidgetsPage
*page
= (*info
->GetCtor())(m_book
, m_imaglist
);
397 labels
.Add(info
->GetLabel());
399 info
= info
->GetNext();
402 m_book
->SetImageList(m_imaglist
);
405 size_t count
= pages
.GetCount();
406 for ( size_t n
= 0; n
< count
; n
++ )
411 false, // don't select
416 wxColour colour = m_book->MSWGetBgColourForChild(pages[n]);
417 pages[n]->SetBackgroundColour(colour);
422 WidgetsFrame::~WidgetsFrame()
430 // ----------------------------------------------------------------------------
431 // WidgetsFrame event handlers
432 // ----------------------------------------------------------------------------
434 void WidgetsFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
440 void WidgetsFrame::OnButtonClearLog(wxCommandEvent
& WXUNUSED(event
))
450 void WidgetsFrame::OnSetTooltip(wxCommandEvent
& WXUNUSED(event
))
452 static wxString s_tip
= _T("This is a tooltip");
454 wxString s
= wxGetTextFromUser
456 _T("Tooltip text: "),
457 _T("Widgets sample"),
465 WidgetsPage
*page
= wxStaticCast(m_book
->GetCurrentPage(), WidgetsPage
);
466 page
->GetWidget()->SetToolTip(s_tip
= s
);
468 wxControl
*ctrl2
= page
->GetWidget2();
470 ctrl2
->SetToolTip(s
);
473 #endif // wxUSE_TOOLTIPS
475 void WidgetsFrame::OnSetFgCol(wxCommandEvent
& WXUNUSED(event
))
478 // allow for debugging the default colour the first time this is called
479 WidgetsPage
*page
= wxStaticCast(m_book
->GetCurrentPage(), WidgetsPage
);
481 m_colFg
= page
->GetForegroundColour();
483 wxColour col
= wxGetColourFromUser(this, m_colFg
);
489 page
->GetWidget()->SetForegroundColour(m_colFg
);
490 page
->GetWidget()->Refresh();
492 wxControl
*ctrl2
= page
->GetWidget2();
495 ctrl2
->SetForegroundColour(m_colFg
);
499 wxLogMessage(_T("Colour selection dialog not available in current build."));
503 void WidgetsFrame::OnSetBgCol(wxCommandEvent
& WXUNUSED(event
))
506 WidgetsPage
*page
= wxStaticCast(m_book
->GetCurrentPage(), WidgetsPage
);
508 m_colBg
= page
->GetBackgroundColour();
510 wxColour col
= wxGetColourFromUser(this, m_colBg
);
516 page
->GetWidget()->SetBackgroundColour(m_colBg
);
517 page
->GetWidget()->Refresh();
519 wxControl
*ctrl2
= page
->GetWidget2();
522 ctrl2
->SetBackgroundColour(m_colFg
);
526 wxLogMessage(_T("Colour selection dialog not available in current build."));
530 void WidgetsFrame::OnSetFont(wxCommandEvent
& WXUNUSED(event
))
533 WidgetsPage
*page
= wxStaticCast(m_book
->GetCurrentPage(), WidgetsPage
);
535 m_font
= page
->GetFont();
537 wxFont font
= wxGetFontFromUser(this, m_font
);
543 page
->GetWidget()->SetFont(m_font
);
544 page
->GetWidget()->Refresh();
546 wxControl
*ctrl2
= page
->GetWidget2();
549 ctrl2
->SetFont(m_font
);
553 wxLogMessage(_T("Font selection dialog not available in current build."));
557 void WidgetsFrame::OnEnable(wxCommandEvent
& event
)
559 WidgetsPage
*page
= wxStaticCast(m_book
->GetCurrentPage(), WidgetsPage
);
560 page
->GetWidget()->Enable(event
.IsChecked());
563 #endif // wxUSE_MENUS
565 // ----------------------------------------------------------------------------
567 // ----------------------------------------------------------------------------
569 WidgetsPageInfo
*WidgetsPage::ms_widgetPages
= NULL
;
571 WidgetsPageInfo::WidgetsPageInfo(Constructor ctor
, const wxChar
*label
)
578 // dummy sorting: add and immediately sort on list according to label
580 if(WidgetsPage::ms_widgetPages
)
582 WidgetsPageInfo
*node_prev
= WidgetsPage::ms_widgetPages
;
583 if(wxStrcmp(label
,node_prev
->GetLabel().c_str())<0)
587 WidgetsPage::ms_widgetPages
= this;
591 WidgetsPageInfo
*node_next
;
594 node_next
= node_prev
->GetNext();
597 // add if between two
598 if(wxStrcmp(label
,node_next
->GetLabel().c_str())<0)
600 node_prev
->SetNext(this);
602 // force to break loop
609 node_prev
->SetNext(this);
612 node_prev
= node_next
;
620 WidgetsPage::ms_widgetPages
= this;
626 // ----------------------------------------------------------------------------
628 // ----------------------------------------------------------------------------
630 WidgetsPage::WidgetsPage(wxBookCtrl
*book
)
631 : wxPanel(book
, wxID_ANY
,
632 wxDefaultPosition
, wxDefaultSize
,
633 wxNO_FULL_REPAINT_ON_RESIZE
|
639 wxSizer
*WidgetsPage::CreateSizerWithText(wxControl
*control
,
643 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
644 wxTextCtrl
*text
= new wxTextCtrl(this, id
, wxEmptyString
,
645 wxDefaultPosition
, wxDefaultSize
, wxTE_PROCESS_ENTER
);
647 sizerRow
->Add(control
, 0, wxRIGHT
| wxALIGN_CENTRE_VERTICAL
, 5);
648 sizerRow
->Add(text
, 1, wxLEFT
| wxALIGN_CENTRE_VERTICAL
, 5);
656 // create a sizer containing a label and a text ctrl
657 wxSizer
*WidgetsPage::CreateSizerWithTextAndLabel(const wxString
& label
,
661 return CreateSizerWithText(new wxStaticText(this, wxID_ANY
, label
),
665 // create a sizer containing a button and a text ctrl
666 wxSizer
*WidgetsPage::CreateSizerWithTextAndButton(wxWindowID idBtn
,
667 const wxString
& label
,
671 return CreateSizerWithText(new wxButton(this, idBtn
, label
), id
, ppText
);
674 wxCheckBox
*WidgetsPage::CreateCheckBoxAndAddToSizer(wxSizer
*sizer
,
675 const wxString
& label
,
678 wxCheckBox
*checkbox
= new wxCheckBox(this, id
, label
);
679 sizer
->Add(checkbox
, 0, wxLEFT
| wxRIGHT
, 5);
680 sizer
->Add(0, 2, 0, wxGROW
); // spacer