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
32 #include "wx/button.h"
33 #include "wx/checkbox.h"
34 #include "wx/listbox.h"
35 #include "wx/statbox.h"
36 #include "wx/stattext.h"
37 #include "wx/textctrl.h"
40 #include "wx/notebook.h"
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
52 Widgets_ClearLog
= 100,
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 // Define a new application type, each program should derive a class from wxApp
61 class WidgetsApp
: public wxApp
64 // override base class virtuals
65 // ----------------------------
67 // this one is called on application startup and is a good place for the app
68 // initialization (doing it here and not in the ctor allows to have an error
69 // return: if OnInit() returns false, the application terminates)
70 virtual bool OnInit();
73 // Define a new frame type: this is going to be our main frame
74 class WidgetsFrame
: public wxFrame
78 WidgetsFrame(const wxString
& title
);
79 virtual ~WidgetsFrame();
84 void OnButtonClearLog(wxCommandEvent
& event
);
86 void OnButtonQuit(wxCommandEvent
& event
);
88 // initialize the notebook: add all pages to it
92 // the panel containing everything
96 // the listbox for logging messages
99 // the log target we use to redirect messages to the listbox
103 // the notebook containing the test pages
104 wxNotebook
*m_notebook
;
106 // and the image list for it
107 wxImageList
*m_imaglist
;
109 // any class wishing to process wxWidgets events must use this macro
110 DECLARE_EVENT_TABLE()
114 // A log target which just redirects the messages to a listbox
115 class LboxLogger
: public wxLog
118 LboxLogger(wxListBox
*lbox
, wxLog
*logOld
)
121 //m_lbox->Disable(); -- looks ugly under MSW
125 virtual ~LboxLogger()
127 wxLog::SetActiveTarget(m_logOld
);
131 // implement sink functions
132 virtual void DoLog(wxLogLevel level
, const wxChar
*szString
, time_t t
)
134 // don't put trace messages into listbox or we can get into infinite
136 if ( level
== wxLOG_Trace
)
140 // cast is needed to call protected method
141 ((LboxLogger
*)m_logOld
)->DoLog(level
, szString
, t
);
146 wxLog::DoLog(level
, szString
, t
);
150 virtual void DoLogString(const wxChar
*szString
, time_t WXUNUSED(t
))
156 #ifdef __WXUNIVERSAL__
157 m_lbox
->AppendAndEnsureVisible(msg
);
158 #else // other ports don't have this method yet
160 m_lbox
->SetFirstItem(m_lbox
->GetCount() - 1);
164 // the control we use
167 // the old log target
173 WX_DEFINE_ARRAY_PTR(WidgetsPage
*, ArrayWidgetsPage
);
175 // ----------------------------------------------------------------------------
177 // ----------------------------------------------------------------------------
179 IMPLEMENT_APP(WidgetsApp
)
181 // ----------------------------------------------------------------------------
183 // ----------------------------------------------------------------------------
185 BEGIN_EVENT_TABLE(WidgetsFrame
, wxFrame
)
187 EVT_BUTTON(Widgets_ClearLog
, WidgetsFrame::OnButtonClearLog
)
189 EVT_BUTTON(Widgets_Quit
, WidgetsFrame::OnButtonQuit
)
192 // ============================================================================
194 // ============================================================================
196 // ----------------------------------------------------------------------------
198 // ----------------------------------------------------------------------------
200 bool WidgetsApp::OnInit()
202 if ( !wxApp::OnInit() )
205 // the reason for having these ifdef's is that I often run two copies of
206 // this sample side by side and it is useful to see which one is which
208 #if defined(__WXUNIVERSAL__)
209 title
= _T("wxUniv/");
212 #if defined(__WXMSW__)
213 title
+= _T("wxMSW");
214 #elif defined(__WXGTK__)
215 title
+= _T("wxGTK");
216 #elif defined(__WXMAC__)
217 title
+= _T("wxMAC");
218 #elif defined(__WXMOTIF__)
219 title
+= _T("wxMOTIF");
221 title
+= _T("wxWidgets");
224 wxFrame
*frame
= new WidgetsFrame(title
+ _T(" widgets demo"));
227 //wxLog::AddTraceMask(_T("listbox"));
228 //wxLog::AddTraceMask(_T("scrollbar"));
229 //wxLog::AddTraceMask(_T("focus"));
234 // ----------------------------------------------------------------------------
235 // WidgetsFrame construction
236 // ----------------------------------------------------------------------------
238 WidgetsFrame::WidgetsFrame(const wxString
& title
)
239 : wxFrame(NULL
, wxID_ANY
, title
,
240 wxPoint(0, 50), wxDefaultSize
,
241 wxDEFAULT_FRAME_STYLE
|
242 wxNO_FULL_REPAINT_ON_RESIZE
|
248 m_lboxLog
= (wxListBox
*)NULL
;
249 m_logTarget
= (wxLog
*)NULL
;
251 m_notebook
= (wxNotebook
*)NULL
;
252 m_imaglist
= (wxImageList
*)NULL
;
255 m_panel
= new wxPanel(this, wxID_ANY
,
256 wxDefaultPosition
, wxDefaultSize
, wxCLIP_CHILDREN
);
258 wxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
260 // we have 2 panes: notebook which pages demonstrating the controls in the
261 // upper one and the log window with some buttons in the lower
263 m_notebook
= new wxNotebook(m_panel
, wxID_ANY
, wxDefaultPosition
,
264 wxDefaultSize
, wxNO_FULL_REPAINT_ON_RESIZE
|wxCLIP_CHILDREN
);
267 // the lower one only has the log listbox and a button to clear it
269 wxSizer
*sizerDown
= new wxStaticBoxSizer(
270 new wxStaticBox( m_panel
, wxID_ANY
, _T("&Log window") ),
273 m_lboxLog
= new wxListBox(m_panel
, wxID_ANY
);
274 sizerDown
->Add(m_lboxLog
, 1, wxGROW
| wxALL
, 5);
275 sizerDown
->SetMinSize(100, 150);
277 wxSizer
*sizerDown
= new wxBoxSizer(wxVERTICAL
);
280 wxBoxSizer
*sizerBtns
= new wxBoxSizer(wxHORIZONTAL
);
283 btn
= new wxButton(m_panel
, Widgets_ClearLog
, _T("Clear &log"));
285 sizerBtns
->Add(10, 0); // spacer
287 btn
= new wxButton(m_panel
, Widgets_Quit
, _T("E&xit"));
289 sizerDown
->Add(sizerBtns
, 0, wxALL
| wxALIGN_RIGHT
, 5);
291 // put everything together
292 sizerTop
->Add(m_notebook
, 1, wxGROW
| (wxALL
& ~(wxTOP
| wxBOTTOM
)), 10);
293 sizerTop
->Add(0, 5, 0, wxGROW
); // spacer in between
294 sizerTop
->Add(sizerDown
, 0, wxGROW
| (wxALL
& ~wxTOP
), 10);
296 m_panel
->SetSizer(sizerTop
);
299 sizerTop
->SetSizeHints(this);
301 #if wxUSE_LOG && !defined(__WXCOCOA__)
302 // wxCocoa's listbox is too flakey to use for logging right now
303 // now that everything is created we can redirect the log messages to the
305 m_logTarget
= new LboxLogger(m_lboxLog
, wxLog::GetActiveTarget());
306 wxLog::SetActiveTarget(m_logTarget
);
310 void WidgetsFrame::InitNotebook()
312 m_imaglist
= new wxImageList(32, 32);
314 ArrayWidgetsPage pages
;
315 wxArrayString labels
;
317 // we need to first create all pages and only then add them to the notebook
318 // as we need the image list first
319 WidgetsPageInfo
*info
= WidgetsPage::ms_widgetPages
;
322 WidgetsPage
*page
= (*info
->GetCtor())(m_notebook
, m_imaglist
);
325 labels
.Add(info
->GetLabel());
327 info
= info
->GetNext();
330 m_notebook
->SetImageList(m_imaglist
);
333 size_t count
= pages
.GetCount();
334 for ( size_t n
= 0; n
< count
; n
++ )
339 false, // don't select
345 WidgetsFrame::~WidgetsFrame()
353 // ----------------------------------------------------------------------------
354 // WidgetsFrame event handlers
355 // ----------------------------------------------------------------------------
357 void WidgetsFrame::OnButtonQuit(wxCommandEvent
& WXUNUSED(event
))
363 void WidgetsFrame::OnButtonClearLog(wxCommandEvent
& WXUNUSED(event
))
369 // ----------------------------------------------------------------------------
371 // ----------------------------------------------------------------------------
373 WidgetsPageInfo
*WidgetsPage::ms_widgetPages
= NULL
;
375 WidgetsPageInfo::WidgetsPageInfo(Constructor ctor
, const wxChar
*label
)
382 // dummy sorting: add and immediately sort on list according to label
384 if(WidgetsPage::ms_widgetPages
)
386 WidgetsPageInfo
*node_prev
= WidgetsPage::ms_widgetPages
;
387 if(wxStrcmp(label
,node_prev
->GetLabel().c_str())<0)
391 WidgetsPage::ms_widgetPages
= this;
395 WidgetsPageInfo
*node_next
;
398 node_next
= node_prev
->GetNext();
401 // add if between two
402 if(wxStrcmp(label
,node_next
->GetLabel().c_str())<0)
404 node_prev
->SetNext(this);
406 // force to break loop
413 node_prev
->SetNext(this);
416 node_prev
= node_next
;
424 WidgetsPage::ms_widgetPages
= this;
430 // ----------------------------------------------------------------------------
432 // ----------------------------------------------------------------------------
434 WidgetsPage::WidgetsPage(wxNotebook
*notebook
)
435 : wxPanel(notebook
, wxID_ANY
,
436 wxDefaultPosition
, wxDefaultSize
,
437 wxNO_FULL_REPAINT_ON_RESIZE
|
443 wxSizer
*WidgetsPage::CreateSizerWithText(wxControl
*control
,
447 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
448 wxTextCtrl
*text
= new wxTextCtrl(this, id
, wxEmptyString
,
449 wxDefaultPosition
, wxDefaultSize
, wxTE_PROCESS_ENTER
);
451 sizerRow
->Add(control
, 0, wxRIGHT
| wxALIGN_CENTRE_VERTICAL
, 5);
452 sizerRow
->Add(text
, 1, wxLEFT
| wxALIGN_CENTRE_VERTICAL
, 5);
460 // create a sizer containing a label and a text ctrl
461 wxSizer
*WidgetsPage::CreateSizerWithTextAndLabel(const wxString
& label
,
465 return CreateSizerWithText(new wxStaticText(this, wxID_ANY
, label
),
469 // create a sizer containing a button and a text ctrl
470 wxSizer
*WidgetsPage::CreateSizerWithTextAndButton(wxWindowID idBtn
,
471 const wxString
& label
,
475 return CreateSizerWithText(new wxButton(this, idBtn
, label
), id
, ppText
);
478 wxCheckBox
*WidgetsPage::CreateCheckBoxAndAddToSizer(wxSizer
*sizer
,
479 const wxString
& label
,
482 wxCheckBox
*checkbox
= new wxCheckBox(this, id
, label
);
483 sizer
->Add(checkbox
, 0, wxLEFT
| wxRIGHT
, 5);
484 sizer
->Add(0, 2, 0, wxGROW
); // spacer