]>
git.saurik.com Git - wxWidgets.git/blob - samples/widgets/widgets.cpp
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
33 #include "wx/button.h"
34 #include "wx/checkbox.h"
35 #include "wx/listbox.h"
36 #include "wx/statbox.h"
37 #include "wx/stattext.h"
38 #include "wx/textctrl.h"
41 #include "wx/notebook.h"
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
53 Widgets_ClearLog
= 100,
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 // Define a new application type, each program should derive a class from wxApp
62 class WidgetsApp
: public wxApp
65 // override base class virtuals
66 // ----------------------------
68 // this one is called on application startup and is a good place for the app
69 // initialization (doing it here and not in the ctor allows to have an error
70 // return: if OnInit() returns false, the application terminates)
71 virtual bool OnInit();
74 // Define a new frame type: this is going to be our main frame
75 class WidgetsFrame
: public wxFrame
79 WidgetsFrame(const wxString
& title
);
80 virtual ~WidgetsFrame();
84 void OnButtonClearLog(wxCommandEvent
& event
);
85 void OnButtonQuit(wxCommandEvent
& event
);
87 // initialize the notebook: add all pages to it
91 // the panel containing everything
94 // the listbox for logging messages
97 // the log target we use to redirect messages to the listbox
100 // the notebook containing the test pages
101 wxNotebook
*m_notebook
;
103 // and the image list for it
104 wxImageList
*m_imaglist
;
106 // any class wishing to process wxWindows events must use this macro
107 DECLARE_EVENT_TABLE()
110 // A log target which just redirects the messages to a listbox
111 class LboxLogger
: public wxLog
114 LboxLogger(wxListBox
*lbox
, wxLog
*logOld
)
117 //m_lbox->Disable(); -- looks ugly under MSW
121 virtual ~LboxLogger()
123 wxLog::SetActiveTarget(m_logOld
);
127 // implement sink functions
128 virtual void DoLog(wxLogLevel level
, const wxChar
*szString
, time_t t
)
130 // don't put trace messages into listbox or we can get into infinite
132 if ( level
== wxLOG_Trace
)
136 // cast is needed to call protected method
137 ((LboxLogger
*)m_logOld
)->DoLog(level
, szString
, t
);
142 wxLog::DoLog(level
, szString
, t
);
146 virtual void DoLogString(const wxChar
*szString
, time_t t
)
152 #ifdef __WXUNIVERSAL__
153 m_lbox
->AppendAndEnsureVisible(msg
);
154 #else // other ports don't have this method yet
156 m_lbox
->SetFirstItem(m_lbox
->GetCount() - 1);
160 // the control we use
163 // the old log target
168 WX_DEFINE_ARRAY(WidgetsPage
*, ArrayWidgetsPage
);
170 // ----------------------------------------------------------------------------
172 // ----------------------------------------------------------------------------
174 IMPLEMENT_APP(WidgetsApp
)
176 // ----------------------------------------------------------------------------
178 // ----------------------------------------------------------------------------
180 BEGIN_EVENT_TABLE(WidgetsFrame
, wxFrame
)
181 EVT_BUTTON(Widgets_ClearLog
, WidgetsFrame::OnButtonClearLog
)
182 EVT_BUTTON(Widgets_Quit
, WidgetsFrame::OnButtonQuit
)
185 // ============================================================================
187 // ============================================================================
189 // ----------------------------------------------------------------------------
191 // ----------------------------------------------------------------------------
193 bool WidgetsApp::OnInit()
195 if ( !wxApp::OnInit() )
198 // the reason for having these ifdef's is that I often run two copies of
199 // this sample side by side and it is useful to see which one is which
201 #if defined(__WXUNIVERSAL__)
202 title
= _T("wxUniv/");
205 #if defined(__WXMSW__)
206 title
+= _T("wxMSW");
207 #elif defined(__WXGTK__)
208 title
+= _T("wxGTK");
210 title
+= _T("wxWindows");
213 wxFrame
*frame
= new WidgetsFrame(title
+ _T(" widgets demo"));
216 //wxLog::AddTraceMask(_T("listbox"));
217 //wxLog::AddTraceMask(_T("scrollbar"));
222 // ----------------------------------------------------------------------------
223 // WidgetsFrame construction
224 // ----------------------------------------------------------------------------
226 WidgetsFrame::WidgetsFrame(const wxString
& title
)
227 : wxFrame(NULL
, -1, title
, wxPoint(0, 50))
230 m_lboxLog
= (wxListBox
*)NULL
;
231 m_logTarget
= (wxLog
*)NULL
;
232 m_notebook
= (wxNotebook
*)NULL
;
233 m_imaglist
= (wxImageList
*)NULL
;
236 m_panel
= new wxPanel(this, -1);
238 wxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
240 // we have 2 panes: notebook which pages demonstrating the controls in the
241 // upper one and the log window with some buttons in the lower
243 m_notebook
= new wxNotebook(m_panel
, -1);
245 wxSizer
*sizerUp
= new wxNotebookSizer(m_notebook
);
247 // the lower one only has the log listbox and a button to clear it
248 wxSizer
*sizerDown
= new wxStaticBoxSizer
250 new wxStaticBox(m_panel
, -1, _T("&Log window")),
253 m_lboxLog
= new wxListBox(m_panel
, -1);
254 sizerDown
->Add(m_lboxLog
, 1, wxGROW
| wxALL
, 5);
255 wxBoxSizer
*sizerBtns
= new wxBoxSizer(wxHORIZONTAL
);
256 wxButton
*btn
= new wxButton(m_panel
, Widgets_ClearLog
, _T("Clear &log"));
258 sizerBtns
->Add(10, 0); // spacer
259 btn
= new wxButton(m_panel
, Widgets_Quit
, _T("E&xit"));
261 sizerDown
->Add(sizerBtns
, 0, wxALL
| wxALIGN_RIGHT
, 5);
263 // put everything together
264 sizerTop
->Add(sizerUp
, 1, wxGROW
| (wxALL
& ~(wxTOP
| wxBOTTOM
)), 10);
265 sizerTop
->Add(0, 5, 0, wxGROW
); // spacer in between
266 sizerTop
->Add(sizerDown
, 0, wxGROW
| (wxALL
& ~wxTOP
), 10);
268 m_panel
->SetAutoLayout(TRUE
);
269 m_panel
->SetSizer(sizerTop
);
272 sizerTop
->SetSizeHints(this);
274 // now that everything is created we can redirect the log messages to the
276 m_logTarget
= new LboxLogger(m_lboxLog
, wxLog::GetActiveTarget());
277 wxLog::SetActiveTarget(m_logTarget
);
280 void WidgetsFrame::InitNotebook()
282 m_imaglist
= new wxImageList(32, 32);
284 ArrayWidgetsPage pages
;
285 wxArrayString labels
;
287 // we need to first create all pages and only then add them to the notebook
288 // as we need the image list first
289 WidgetsPageInfo
*info
= WidgetsPage::ms_widgetPages
;
292 WidgetsPage
*page
= (*info
->GetCtor())(m_notebook
, m_imaglist
);
295 labels
.Add(info
->GetLabel());
297 info
= info
->GetNext();
300 m_notebook
->SetImageList(m_imaglist
);
303 size_t count
= pages
.GetCount();
304 for ( size_t n
= 0; n
< count
; n
++ )
309 FALSE
, // don't select
315 WidgetsFrame::~WidgetsFrame()
321 // ----------------------------------------------------------------------------
322 // WidgetsFrame event handlers
323 // ----------------------------------------------------------------------------
325 void WidgetsFrame::OnButtonQuit(wxCommandEvent
& WXUNUSED(event
))
330 void WidgetsFrame::OnButtonClearLog(wxCommandEvent
& event
)
335 // ----------------------------------------------------------------------------
337 // ----------------------------------------------------------------------------
339 WidgetsPageInfo
*WidgetsPage::ms_widgetPages
= NULL
;
341 WidgetsPageInfo::WidgetsPageInfo(Constructor ctor
, const wxChar
*label
)
346 m_next
= WidgetsPage::ms_widgetPages
;
347 WidgetsPage::ms_widgetPages
= this;
350 // ----------------------------------------------------------------------------
352 // ----------------------------------------------------------------------------
354 WidgetsPage::WidgetsPage(wxNotebook
*notebook
)
355 : wxPanel(notebook
, -1)
359 wxSizer
*WidgetsPage::CreateSizerWithText(wxControl
*control
,
363 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
364 wxTextCtrl
*text
= new wxTextCtrl(this, id
, _T(""));
365 sizerRow
->Add(control
, 0, wxRIGHT
| wxALIGN_CENTRE_VERTICAL
, 5);
366 sizerRow
->Add(text
, 1, wxLEFT
| wxALIGN_CENTRE_VERTICAL
, 5);
374 // create a sizer containing a label and a text ctrl
375 wxSizer
*WidgetsPage::CreateSizerWithTextAndLabel(const wxString
& label
,
379 return CreateSizerWithText(new wxStaticText(this, -1, label
), id
, ppText
);
382 // create a sizer containing a button and a text ctrl
383 wxSizer
*WidgetsPage::CreateSizerWithTextAndButton(wxWindowID idBtn
,
384 const wxString
& label
,
388 return CreateSizerWithText(new wxButton(this, idBtn
, label
), id
, ppText
);
391 wxCheckBox
*WidgetsPage::CreateCheckBoxAndAddToSizer(wxSizer
*sizer
,
392 const wxString
& label
,
395 wxCheckBox
*checkbox
= new wxCheckBox(this, id
, label
);
396 sizer
->Add(checkbox
, 0, wxLEFT
| wxRIGHT
, 5);
397 sizer
->Add(0, 2, 0, wxGROW
); // spacer