1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWindows Widgets Sample
4 // Purpose: Sample showing most of the simple wxWindows 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();
83 void OnButtonClearLog(wxCommandEvent
& event
);
84 void OnButtonQuit(wxCommandEvent
& event
);
86 // initialize the notebook: add all pages to it
90 // the panel containing everything
93 // the listbox for logging messages
96 // the log target we use to redirect messages to the listbox
99 // the notebook containing the test pages
100 wxNotebook
*m_notebook
;
102 // and the image list for it
103 wxImageList
*m_imaglist
;
105 // any class wishing to process wxWindows events must use this macro
106 DECLARE_EVENT_TABLE()
109 // A log target which just redirects the messages to a listbox
110 class LboxLogger
: public wxLog
113 LboxLogger(wxListBox
*lbox
, wxLog
*logOld
)
116 //m_lbox->Disable(); -- looks ugly under MSW
120 virtual ~LboxLogger()
122 wxLog::SetActiveTarget(m_logOld
);
126 // implement sink functions
127 virtual void DoLog(wxLogLevel level
, const wxChar
*szString
, time_t t
)
129 // don't put trace messages into listbox or we can get into infinite
131 if ( level
== wxLOG_Trace
)
135 // cast is needed to call protected method
136 ((LboxLogger
*)m_logOld
)->DoLog(level
, szString
, t
);
141 wxLog::DoLog(level
, szString
, t
);
145 virtual void DoLogString(const wxChar
*szString
, time_t WXUNUSED(t
))
151 #ifdef __WXUNIVERSAL__
152 m_lbox
->AppendAndEnsureVisible(msg
);
153 #else // other ports don't have this method yet
155 m_lbox
->SetFirstItem(m_lbox
->GetCount() - 1);
159 // the control we use
162 // the old log target
167 WX_DEFINE_ARRAY_NO_PTR(WidgetsPage
*, ArrayWidgetsPage
);
169 // ----------------------------------------------------------------------------
171 // ----------------------------------------------------------------------------
173 IMPLEMENT_APP(WidgetsApp
)
175 // ----------------------------------------------------------------------------
177 // ----------------------------------------------------------------------------
179 BEGIN_EVENT_TABLE(WidgetsFrame
, wxFrame
)
180 EVT_BUTTON(Widgets_ClearLog
, WidgetsFrame::OnButtonClearLog
)
181 EVT_BUTTON(Widgets_Quit
, WidgetsFrame::OnButtonQuit
)
184 // ============================================================================
186 // ============================================================================
188 // ----------------------------------------------------------------------------
190 // ----------------------------------------------------------------------------
192 bool WidgetsApp::OnInit()
194 if ( !wxApp::OnInit() )
197 // the reason for having these ifdef's is that I often run two copies of
198 // this sample side by side and it is useful to see which one is which
200 #if defined(__WXUNIVERSAL__)
201 title
= _T("wxUniv/");
204 #if defined(__WXMSW__)
205 title
+= _T("wxMSW");
206 #elif defined(__WXGTK__)
207 title
+= _T("wxGTK");
208 #elif defined(__WXMAC__)
209 title
+= _T("wxMAC");
210 #elif defined(__WXMOTIF__)
211 title
+= _T("wxMOTIF");
213 title
+= _T("wxWindows");
216 wxFrame
*frame
= new WidgetsFrame(title
+ _T(" widgets demo"));
219 //wxLog::AddTraceMask(_T("listbox"));
220 //wxLog::AddTraceMask(_T("scrollbar"));
221 //wxLog::AddTraceMask(_T("focus"));
226 // ----------------------------------------------------------------------------
227 // WidgetsFrame construction
228 // ----------------------------------------------------------------------------
230 WidgetsFrame::WidgetsFrame(const wxString
& title
)
231 : wxFrame(NULL
, -1, title
, wxPoint(0, 50), wxDefaultSize
, wxDEFAULT_FRAME_STYLE
|wxNO_FULL_REPAINT_ON_RESIZE
|wxCLIP_CHILDREN
)
234 m_lboxLog
= (wxListBox
*)NULL
;
235 m_logTarget
= (wxLog
*)NULL
;
236 m_notebook
= (wxNotebook
*)NULL
;
237 m_imaglist
= (wxImageList
*)NULL
;
240 m_panel
= new wxPanel(this, -1, wxDefaultPosition
, wxDefaultSize
, wxCLIP_CHILDREN
);
242 wxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
244 // we have 2 panes: notebook which pages demonstrating the controls in the
245 // upper one and the log window with some buttons in the lower
247 m_notebook
= new wxNotebook(m_panel
, -1, wxDefaultPosition
, wxDefaultSize
, wxNO_FULL_REPAINT_ON_RESIZE
|wxCLIP_CHILDREN
);
249 wxSizer
*sizerUp
= new wxNotebookSizer(m_notebook
);
251 // the lower one only has the log listbox and a button to clear it
252 wxSizer
*sizerDown
= new wxStaticBoxSizer
254 new wxStaticBox(m_panel
, -1, _T("&Log window")),
257 m_lboxLog
= new wxListBox(m_panel
, -1);
258 sizerDown
->Add(m_lboxLog
, 1, wxGROW
| wxALL
, 5);
259 sizerDown
->SetMinSize(100, 150);
261 wxBoxSizer
*sizerBtns
= new wxBoxSizer(wxHORIZONTAL
);
262 wxButton
*btn
= new wxButton(m_panel
, Widgets_ClearLog
, _T("Clear &log"));
264 sizerBtns
->Add(10, 0); // spacer
265 btn
= new wxButton(m_panel
, Widgets_Quit
, _T("E&xit"));
267 sizerDown
->Add(sizerBtns
, 0, wxALL
| wxALIGN_RIGHT
, 5);
269 // put everything together
270 sizerTop
->Add(sizerUp
, 1, wxGROW
| (wxALL
& ~(wxTOP
| wxBOTTOM
)), 10);
271 sizerTop
->Add(0, 5, 0, wxGROW
); // spacer in between
272 sizerTop
->Add(sizerDown
, 0, wxGROW
| (wxALL
& ~wxTOP
), 10);
274 m_panel
->SetAutoLayout(TRUE
);
275 m_panel
->SetSizer(sizerTop
);
278 sizerTop
->SetSizeHints(this);
280 // now that everything is created we can redirect the log messages to the
282 m_logTarget
= new LboxLogger(m_lboxLog
, wxLog::GetActiveTarget());
283 wxLog::SetActiveTarget(m_logTarget
);
286 void WidgetsFrame::InitNotebook()
288 m_imaglist
= new wxImageList(32, 32);
290 ArrayWidgetsPage pages
;
291 wxArrayString labels
;
293 // we need to first create all pages and only then add them to the notebook
294 // as we need the image list first
295 WidgetsPageInfo
*info
= WidgetsPage::ms_widgetPages
;
298 WidgetsPage
*page
= (*info
->GetCtor())(m_notebook
, m_imaglist
);
301 labels
.Add(info
->GetLabel());
303 info
= info
->GetNext();
306 m_notebook
->SetImageList(m_imaglist
);
309 size_t count
= pages
.GetCount();
310 for ( size_t n
= 0; n
< count
; n
++ )
315 FALSE
, // don't select
321 WidgetsFrame::~WidgetsFrame()
327 // ----------------------------------------------------------------------------
328 // WidgetsFrame event handlers
329 // ----------------------------------------------------------------------------
331 void WidgetsFrame::OnButtonQuit(wxCommandEvent
& WXUNUSED(event
))
336 void WidgetsFrame::OnButtonClearLog(wxCommandEvent
& WXUNUSED(event
))
341 // ----------------------------------------------------------------------------
343 // ----------------------------------------------------------------------------
345 WidgetsPageInfo
*WidgetsPage::ms_widgetPages
= NULL
;
347 WidgetsPageInfo::WidgetsPageInfo(Constructor ctor
, const wxChar
*label
)
352 m_next
= WidgetsPage::ms_widgetPages
;
353 WidgetsPage::ms_widgetPages
= this;
356 // ----------------------------------------------------------------------------
358 // ----------------------------------------------------------------------------
360 WidgetsPage::WidgetsPage(wxNotebook
*notebook
)
361 : wxPanel(notebook
, -1, wxDefaultPosition
, wxDefaultSize
, wxNO_FULL_REPAINT_ON_RESIZE
|wxCLIP_CHILDREN
)
365 wxSizer
*WidgetsPage::CreateSizerWithText(wxControl
*control
,
369 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
370 wxTextCtrl
*text
= new wxTextCtrl(this, id
, _T(""), wxDefaultPosition
, wxDefaultSize
, wxTE_PROCESS_ENTER
);
372 sizerRow
->Add(control
, 0, wxRIGHT
| wxALIGN_CENTRE_VERTICAL
, 5);
373 sizerRow
->Add(text
, 1, wxLEFT
| wxALIGN_CENTRE_VERTICAL
, 5);
381 // create a sizer containing a label and a text ctrl
382 wxSizer
*WidgetsPage::CreateSizerWithTextAndLabel(const wxString
& label
,
386 return CreateSizerWithText(new wxStaticText(this, -1, label
), id
, ppText
);
389 // create a sizer containing a button and a text ctrl
390 wxSizer
*WidgetsPage::CreateSizerWithTextAndButton(wxWindowID idBtn
,
391 const wxString
& label
,
395 return CreateSizerWithText(new wxButton(this, idBtn
, label
), id
, ppText
);
398 wxCheckBox
*WidgetsPage::CreateCheckBoxAndAddToSizer(wxSizer
*sizer
,
399 const wxString
& label
,
402 wxCheckBox
*checkbox
= new wxCheckBox(this, id
, label
);
403 sizer
->Add(checkbox
, 0, wxLEFT
| wxRIGHT
, 5);
404 sizer
->Add(0, 2, 0, wxGROW
); // spacer