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/bookctrl.h"
44 #include "wx/colordlg.h"
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
55 Widgets_ClearLog
= 100,
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
65 // Define a new application type, each program should derive a class from wxApp
66 class WidgetsApp
: public wxApp
69 // override base class virtuals
70 // ----------------------------
72 // this one is called on application startup and is a good place for the app
73 // initialization (doing it here and not in the ctor allows to have an error
74 // return: if OnInit() returns false, the application terminates)
75 virtual bool OnInit();
78 // Define a new frame type: this is going to be our main frame
79 class WidgetsFrame
: public wxFrame
83 WidgetsFrame(const wxString
& title
);
84 virtual ~WidgetsFrame();
89 void OnButtonClearLog(wxCommandEvent
& event
);
91 void OnExit(wxCommandEvent
& event
);
93 void OnSetFgCol(wxCommandEvent
& event
);
94 void OnSetBgCol(wxCommandEvent
& event
);
97 // initialize the book: add all pages to it
101 // the panel containing everything
105 // the listbox for logging messages
106 wxListBox
*m_lboxLog
;
108 // the log target we use to redirect messages to the listbox
112 // the book containing the test pages
115 // and the image list for it
116 wxImageList
*m_imaglist
;
119 // last chosen fg/bg colours
122 #endif // wxUSE_MENUS
124 // any class wishing to process wxWidgets events must use this macro
125 DECLARE_EVENT_TABLE()
129 // A log target which just redirects the messages to a listbox
130 class LboxLogger
: public wxLog
133 LboxLogger(wxListBox
*lbox
, wxLog
*logOld
)
136 //m_lbox->Disable(); -- looks ugly under MSW
140 virtual ~LboxLogger()
142 wxLog::SetActiveTarget(m_logOld
);
146 // implement sink functions
147 virtual void DoLog(wxLogLevel level
, const wxChar
*szString
, time_t t
)
149 // don't put trace messages into listbox or we can get into infinite
151 if ( level
== wxLOG_Trace
)
155 // cast is needed to call protected method
156 ((LboxLogger
*)m_logOld
)->DoLog(level
, szString
, t
);
161 wxLog::DoLog(level
, szString
, t
);
165 virtual void DoLogString(const wxChar
*szString
, time_t WXUNUSED(t
))
171 #ifdef __WXUNIVERSAL__
172 m_lbox
->AppendAndEnsureVisible(msg
);
173 #else // other ports don't have this method yet
175 m_lbox
->SetFirstItem(m_lbox
->GetCount() - 1);
179 // the control we use
182 // the old log target
188 WX_DEFINE_ARRAY_PTR(WidgetsPage
*, ArrayWidgetsPage
);
190 // ----------------------------------------------------------------------------
192 // ----------------------------------------------------------------------------
194 IMPLEMENT_APP(WidgetsApp
)
196 // ----------------------------------------------------------------------------
198 // ----------------------------------------------------------------------------
200 BEGIN_EVENT_TABLE(WidgetsFrame
, wxFrame
)
202 EVT_BUTTON(Widgets_ClearLog
, WidgetsFrame::OnButtonClearLog
)
204 EVT_BUTTON(Widgets_Quit
, WidgetsFrame::OnExit
)
206 EVT_MENU(wxID_EXIT
, WidgetsFrame::OnExit
)
207 EVT_MENU(Widgets_SetFgColour
, WidgetsFrame::OnSetFgCol
)
208 EVT_MENU(Widgets_SetBgColour
, WidgetsFrame::OnSetBgCol
)
211 // ============================================================================
213 // ============================================================================
215 // ----------------------------------------------------------------------------
217 // ----------------------------------------------------------------------------
219 bool WidgetsApp::OnInit()
221 if ( !wxApp::OnInit() )
224 // the reason for having these ifdef's is that I often run two copies of
225 // this sample side by side and it is useful to see which one is which
227 #if defined(__WXUNIVERSAL__)
228 title
= _T("wxUniv/");
231 #if defined(__WXMSW__)
232 title
+= _T("wxMSW");
233 #elif defined(__WXGTK__)
234 title
+= _T("wxGTK");
235 #elif defined(__WXMAC__)
236 title
+= _T("wxMAC");
237 #elif defined(__WXMOTIF__)
238 title
+= _T("wxMOTIF");
240 title
+= _T("wxWidgets");
243 wxFrame
*frame
= new WidgetsFrame(title
+ _T(" widgets demo"));
246 //wxLog::AddTraceMask(_T("listbox"));
247 //wxLog::AddTraceMask(_T("scrollbar"));
248 //wxLog::AddTraceMask(_T("focus"));
253 // ----------------------------------------------------------------------------
254 // WidgetsFrame construction
255 // ----------------------------------------------------------------------------
257 WidgetsFrame::WidgetsFrame(const wxString
& title
)
258 : wxFrame(NULL
, wxID_ANY
, title
,
259 wxPoint(0, 50), wxDefaultSize
,
260 wxDEFAULT_FRAME_STYLE
|
261 wxNO_FULL_REPAINT_ON_RESIZE
|
267 m_lboxLog
= (wxListBox
*)NULL
;
268 m_logTarget
= (wxLog
*)NULL
;
270 m_book
= (wxBookCtrl
*)NULL
;
271 m_imaglist
= (wxImageList
*)NULL
;
274 // create the menubar
275 wxMenuBar
*mbar
= new wxMenuBar
;
276 wxMenu
*menuWidget
= new wxMenu
;
277 menuWidget
->Append(Widgets_SetFgColour
, _T("Set &foreground...\tCtrl-F"));
278 menuWidget
->Append(Widgets_SetBgColour
, _T("Set &background...\tCtrl-B"));
279 menuWidget
->AppendSeparator();
280 menuWidget
->Append(wxID_EXIT
, _T("&Quit\tCtrl-Q"));
281 mbar
->Append(menuWidget
, _T("&Widget"));
283 #endif // wxUSE_MENUS
286 m_panel
= new wxPanel(this, wxID_ANY
,
287 wxDefaultPosition
, wxDefaultSize
, wxCLIP_CHILDREN
);
289 wxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
291 // we have 2 panes: book which pages demonstrating the controls in the
292 // upper one and the log window with some buttons in the lower
294 m_book
= new wxBookCtrl(m_panel
, wxID_ANY
, wxDefaultPosition
,
295 wxDefaultSize
, wxNO_FULL_REPAINT_ON_RESIZE
|wxCLIP_CHILDREN
|wxBC_DEFAULT
);
298 #ifndef __SMARTPHONE__
299 // the lower one only has the log listbox and a button to clear it
301 wxSizer
*sizerDown
= new wxStaticBoxSizer(
302 new wxStaticBox( m_panel
, wxID_ANY
, _T("&Log window") ),
305 m_lboxLog
= new wxListBox(m_panel
, wxID_ANY
);
306 sizerDown
->Add(m_lboxLog
, 1, wxGROW
| wxALL
, 5);
307 sizerDown
->SetMinSize(100, 150);
309 wxSizer
*sizerDown
= new wxBoxSizer(wxVERTICAL
);
312 wxBoxSizer
*sizerBtns
= new wxBoxSizer(wxHORIZONTAL
);
315 btn
= new wxButton(m_panel
, Widgets_ClearLog
, _T("Clear &log"));
317 sizerBtns
->Add(10, 0); // spacer
319 btn
= new wxButton(m_panel
, Widgets_Quit
, _T("E&xit"));
321 sizerDown
->Add(sizerBtns
, 0, wxALL
| wxALIGN_RIGHT
, 5);
323 // put everything together
324 sizerTop
->Add(m_book
, 1, wxGROW
| (wxALL
& ~(wxTOP
| wxBOTTOM
)), 10);
325 sizerTop
->Add(0, 5, 0, wxGROW
); // spacer in between
326 sizerTop
->Add(sizerDown
, 0, wxGROW
| (wxALL
& ~wxTOP
), 10);
328 #else // !__SMARTPHONE__/__SMARTPHONE__
330 sizerTop
->Add(m_book
, 1, wxGROW
| wxALL
);
332 #endif // __SMARTPHONE__
334 m_panel
->SetSizer(sizerTop
);
337 sizerTop
->SetSizeHints(this);
339 #if USE_LOG && !defined(__WXCOCOA__)
340 // wxCocoa's listbox is too flakey to use for logging right now
341 // now that everything is created we can redirect the log messages to the
343 m_logTarget
= new LboxLogger(m_lboxLog
, wxLog::GetActiveTarget());
344 wxLog::SetActiveTarget(m_logTarget
);
348 void WidgetsFrame::InitBook()
350 m_imaglist
= new wxImageList(32, 32);
352 ArrayWidgetsPage pages
;
353 wxArrayString labels
;
355 // we need to first create all pages and only then add them to the book
356 // as we need the image list first
357 WidgetsPageInfo
*info
= WidgetsPage::ms_widgetPages
;
360 WidgetsPage
*page
= (*info
->GetCtor())(m_book
, m_imaglist
);
363 labels
.Add(info
->GetLabel());
365 info
= info
->GetNext();
368 m_book
->SetImageList(m_imaglist
);
371 size_t count
= pages
.GetCount();
372 for ( size_t n
= 0; n
< count
; n
++ )
377 false, // don't select
383 WidgetsFrame::~WidgetsFrame()
391 // ----------------------------------------------------------------------------
392 // WidgetsFrame event handlers
393 // ----------------------------------------------------------------------------
395 void WidgetsFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
401 void WidgetsFrame::OnButtonClearLog(wxCommandEvent
& WXUNUSED(event
))
409 void WidgetsFrame::OnSetFgCol(wxCommandEvent
& WXUNUSED(event
))
412 wxColour col
= wxGetColourFromUser(this, m_colFg
);
418 WidgetsPage
*page
= wxStaticCast(m_book
->GetCurrentPage(), WidgetsPage
);
419 page
->GetWidget()->SetForegroundColour(m_colFg
);
420 page
->GetWidget()->Refresh();
422 wxLogMessage(_T("None colour dialog available in current build."));
426 void WidgetsFrame::OnSetBgCol(wxCommandEvent
& WXUNUSED(event
))
429 wxColour col
= wxGetColourFromUser(this, m_colBg
);
435 WidgetsPage
*page
= wxStaticCast(m_book
->GetCurrentPage(), WidgetsPage
);
436 page
->GetWidget()->SetBackgroundColour(m_colBg
);
437 page
->GetWidget()->Refresh();
439 wxLogMessage(_T("None colour dialog available in current build."));
443 #endif // wxUSE_MENUS
445 // ----------------------------------------------------------------------------
447 // ----------------------------------------------------------------------------
449 WidgetsPageInfo
*WidgetsPage::ms_widgetPages
= NULL
;
451 WidgetsPageInfo::WidgetsPageInfo(Constructor ctor
, const wxChar
*label
)
458 // dummy sorting: add and immediately sort on list according to label
460 if(WidgetsPage::ms_widgetPages
)
462 WidgetsPageInfo
*node_prev
= WidgetsPage::ms_widgetPages
;
463 if(wxStrcmp(label
,node_prev
->GetLabel().c_str())<0)
467 WidgetsPage::ms_widgetPages
= this;
471 WidgetsPageInfo
*node_next
;
474 node_next
= node_prev
->GetNext();
477 // add if between two
478 if(wxStrcmp(label
,node_next
->GetLabel().c_str())<0)
480 node_prev
->SetNext(this);
482 // force to break loop
489 node_prev
->SetNext(this);
492 node_prev
= node_next
;
500 WidgetsPage::ms_widgetPages
= this;
506 // ----------------------------------------------------------------------------
508 // ----------------------------------------------------------------------------
510 WidgetsPage::WidgetsPage(wxBookCtrl
*book
)
511 : wxPanel(book
, wxID_ANY
,
512 wxDefaultPosition
, wxDefaultSize
,
513 wxNO_FULL_REPAINT_ON_RESIZE
|
519 wxSizer
*WidgetsPage::CreateSizerWithText(wxControl
*control
,
523 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
524 wxTextCtrl
*text
= new wxTextCtrl(this, id
, wxEmptyString
,
525 wxDefaultPosition
, wxDefaultSize
, wxTE_PROCESS_ENTER
);
527 sizerRow
->Add(control
, 0, wxRIGHT
| wxALIGN_CENTRE_VERTICAL
, 5);
528 sizerRow
->Add(text
, 1, wxLEFT
| wxALIGN_CENTRE_VERTICAL
, 5);
536 // create a sizer containing a label and a text ctrl
537 wxSizer
*WidgetsPage::CreateSizerWithTextAndLabel(const wxString
& label
,
541 return CreateSizerWithText(new wxStaticText(this, wxID_ANY
, label
),
545 // create a sizer containing a button and a text ctrl
546 wxSizer
*WidgetsPage::CreateSizerWithTextAndButton(wxWindowID idBtn
,
547 const wxString
& label
,
551 return CreateSizerWithText(new wxButton(this, idBtn
, label
), id
, ppText
);
554 wxCheckBox
*WidgetsPage::CreateCheckBoxAndAddToSizer(wxSizer
*sizer
,
555 const wxString
& label
,
558 wxCheckBox
*checkbox
= new wxCheckBox(this, id
, label
);
559 sizer
->Add(checkbox
, 0, wxLEFT
| wxRIGHT
, 5);
560 sizer
->Add(0, 2, 0, wxGROW
); // spacer