]> git.saurik.com Git - wxWidgets.git/blame - samples/widgets/widgets.cpp
don't preselect the first item if the array with the initial selections is empty...
[wxWidgets.git] / samples / widgets / widgets.cpp
CommitLineData
32b8ec41 1/////////////////////////////////////////////////////////////////////////////
be5a51fb 2// Program: wxWidgets Widgets Sample
32b8ec41 3// Name: widgets.cpp
be5a51fb 4// Purpose: Sample showing most of the simple wxWidgets widgets
32b8ec41
VZ
5// Author: Vadim Zeitlin
6// Created: 27.03.01
7// Id: $Id$
8// Copyright: (c) 2001 Vadim Zeitlin
9// License: wxWindows license
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20// for compilers that support precompilation, includes "wx/wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
24 #pragma hdrstop
25#endif
26
27// for all others, include the necessary headers
28#ifndef WX_PRECOMP
29 #include "wx/app.h"
30 #include "wx/log.h"
32b8ec41
VZ
31 #include "wx/frame.h"
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"
38#endif
39
40#include "wx/notebook.h"
41#include "wx/sizer.h"
42
43#include "widgets.h"
44
45// ----------------------------------------------------------------------------
46// constants
47// ----------------------------------------------------------------------------
48
49// control ids
50enum
51{
52 Widgets_ClearLog = 100,
53 Widgets_Quit
54};
55
56// ----------------------------------------------------------------------------
57// our classes
58// ----------------------------------------------------------------------------
59
60// Define a new application type, each program should derive a class from wxApp
61class WidgetsApp : public wxApp
62{
63public:
64 // override base class virtuals
65 // ----------------------------
66
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();
71};
72
73// Define a new frame type: this is going to be our main frame
74class WidgetsFrame : public wxFrame
75{
76public:
77 // ctor(s) and dtor
78 WidgetsFrame(const wxString& title);
79 virtual ~WidgetsFrame();
80
81protected:
82 // event handlers
b29903d4 83#if wxUSE_LOG
32b8ec41 84 void OnButtonClearLog(wxCommandEvent& event);
b29903d4 85#endif // wxUSE_LOG
32b8ec41
VZ
86 void OnButtonQuit(wxCommandEvent& event);
87
88 // initialize the notebook: add all pages to it
89 void InitNotebook();
90
91private:
92 // the panel containing everything
93 wxPanel *m_panel;
94
b29903d4 95#if wxUSE_LOG
32b8ec41
VZ
96 // the listbox for logging messages
97 wxListBox *m_lboxLog;
98
99 // the log target we use to redirect messages to the listbox
100 wxLog *m_logTarget;
b29903d4 101#endif // wxUSE_LOG
32b8ec41
VZ
102
103 // the notebook containing the test pages
104 wxNotebook *m_notebook;
105
106 // and the image list for it
107 wxImageList *m_imaglist;
108
be5a51fb 109 // any class wishing to process wxWidgets events must use this macro
32b8ec41
VZ
110 DECLARE_EVENT_TABLE()
111};
112
b29903d4 113#if wxUSE_LOG
32b8ec41
VZ
114// A log target which just redirects the messages to a listbox
115class LboxLogger : public wxLog
116{
117public:
118 LboxLogger(wxListBox *lbox, wxLog *logOld)
119 {
120 m_lbox = lbox;
121 //m_lbox->Disable(); -- looks ugly under MSW
122 m_logOld = logOld;
123 }
124
125 virtual ~LboxLogger()
126 {
127 wxLog::SetActiveTarget(m_logOld);
128 }
129
130private:
131 // implement sink functions
132 virtual void DoLog(wxLogLevel level, const wxChar *szString, time_t t)
133 {
134 // don't put trace messages into listbox or we can get into infinite
135 // recursion
136 if ( level == wxLOG_Trace )
137 {
138 if ( m_logOld )
139 {
140 // cast is needed to call protected method
141 ((LboxLogger *)m_logOld)->DoLog(level, szString, t);
142 }
143 }
144 else
145 {
146 wxLog::DoLog(level, szString, t);
147 }
148 }
149
c02e5a31 150 virtual void DoLogString(const wxChar *szString, time_t WXUNUSED(t))
32b8ec41
VZ
151 {
152 wxString msg;
153 TimeStamp(&msg);
154 msg += szString;
155
156 #ifdef __WXUNIVERSAL__
157 m_lbox->AppendAndEnsureVisible(msg);
158 #else // other ports don't have this method yet
159 m_lbox->Append(msg);
160 m_lbox->SetFirstItem(m_lbox->GetCount() - 1);
161 #endif
162 }
163
164 // the control we use
165 wxListBox *m_lbox;
166
167 // the old log target
168 wxLog *m_logOld;
169};
b29903d4 170#endif // wxUSE_LOG
32b8ec41
VZ
171
172// array of pages
38d6b957 173WX_DEFINE_ARRAY_PTR(WidgetsPage *, ArrayWidgetsPage);
32b8ec41
VZ
174
175// ----------------------------------------------------------------------------
176// misc macros
177// ----------------------------------------------------------------------------
178
179IMPLEMENT_APP(WidgetsApp)
180
32b8ec41
VZ
181// ----------------------------------------------------------------------------
182// event tables
183// ----------------------------------------------------------------------------
184
185BEGIN_EVENT_TABLE(WidgetsFrame, wxFrame)
b29903d4 186#if wxUSE_LOG
32b8ec41 187 EVT_BUTTON(Widgets_ClearLog, WidgetsFrame::OnButtonClearLog)
b29903d4 188#endif // wxUSE_LOG
32b8ec41
VZ
189 EVT_BUTTON(Widgets_Quit, WidgetsFrame::OnButtonQuit)
190END_EVENT_TABLE()
191
192// ============================================================================
193// implementation
194// ============================================================================
195
196// ----------------------------------------------------------------------------
197// app class
198// ----------------------------------------------------------------------------
199
200bool WidgetsApp::OnInit()
201{
bf188f1a 202 if ( !wxApp::OnInit() )
206d3a16 203 return false;
bf188f1a 204
32b8ec41
VZ
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
24e78d27 207 wxString title;
32b8ec41 208#if defined(__WXUNIVERSAL__)
085a1f3c 209 title = _T("wxUniv/");
24e78d27
VZ
210#endif
211
212#if defined(__WXMSW__)
213 title += _T("wxMSW");
32b8ec41 214#elif defined(__WXGTK__)
24e78d27 215 title += _T("wxGTK");
0f9390c3
GD
216#elif defined(__WXMAC__)
217 title += _T("wxMAC");
218#elif defined(__WXMOTIF__)
219 title += _T("wxMOTIF");
32b8ec41 220#else
be5a51fb 221 title += _T("wxWidgets");
32b8ec41 222#endif
32b8ec41
VZ
223
224 wxFrame *frame = new WidgetsFrame(title + _T(" widgets demo"));
225 frame->Show();
226
227 //wxLog::AddTraceMask(_T("listbox"));
228 //wxLog::AddTraceMask(_T("scrollbar"));
58ec2255 229 //wxLog::AddTraceMask(_T("focus"));
32b8ec41 230
206d3a16 231 return true;
32b8ec41
VZ
232}
233
234// ----------------------------------------------------------------------------
235// WidgetsFrame construction
236// ----------------------------------------------------------------------------
237
238WidgetsFrame::WidgetsFrame(const wxString& title)
206d3a16 239 : wxFrame(NULL, wxID_ANY, title,
df0787b6
VZ
240 wxPoint(0, 50), wxDefaultSize,
241 wxDEFAULT_FRAME_STYLE |
242 wxNO_FULL_REPAINT_ON_RESIZE |
243 wxCLIP_CHILDREN |
244 wxTAB_TRAVERSAL)
32b8ec41
VZ
245{
246 // init everything
b29903d4 247#if wxUSE_LOG
32b8ec41
VZ
248 m_lboxLog = (wxListBox *)NULL;
249 m_logTarget = (wxLog *)NULL;
b29903d4 250#endif // wxUSE_LOG
32b8ec41
VZ
251 m_notebook = (wxNotebook *)NULL;
252 m_imaglist = (wxImageList *)NULL;
253
254 // create controls
206d3a16
JS
255 m_panel = new wxPanel(this, wxID_ANY,
256 wxDefaultPosition, wxDefaultSize, wxCLIP_CHILDREN);
32b8ec41
VZ
257
258 wxSizer *sizerTop = new wxBoxSizer(wxVERTICAL);
259
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
262
206d3a16
JS
263 m_notebook = new wxNotebook(m_panel, wxID_ANY, wxDefaultPosition,
264 wxDefaultSize, wxNO_FULL_REPAINT_ON_RESIZE|wxCLIP_CHILDREN);
32b8ec41 265 InitNotebook();
32b8ec41
VZ
266
267 // the lower one only has the log listbox and a button to clear it
b29903d4 268#if wxUSE_LOG
206d3a16
JS
269 wxSizer *sizerDown = new wxStaticBoxSizer(
270 new wxStaticBox( m_panel, wxID_ANY, _T("&Log window") ),
271 wxVERTICAL);
272
273 m_lboxLog = new wxListBox(m_panel, wxID_ANY);
32b8ec41 274 sizerDown->Add(m_lboxLog, 1, wxGROW | wxALL, 5);
7b127900 275 sizerDown->SetMinSize(100, 150);
b29903d4
WS
276#else
277 wxSizer *sizerDown = new wxBoxSizer(wxVERTICAL);
278#endif // wxUSE_LOG
88633ca7 279
32b8ec41 280 wxBoxSizer *sizerBtns = new wxBoxSizer(wxHORIZONTAL);
b29903d4
WS
281 wxButton *btn;
282#if wxUSE_LOG
283 btn = new wxButton(m_panel, Widgets_ClearLog, _T("Clear &log"));
32b8ec41
VZ
284 sizerBtns->Add(btn);
285 sizerBtns->Add(10, 0); // spacer
b29903d4 286#endif // wxUSE_LOG
32b8ec41
VZ
287 btn = new wxButton(m_panel, Widgets_Quit, _T("E&xit"));
288 sizerBtns->Add(btn);
289 sizerDown->Add(sizerBtns, 0, wxALL | wxALIGN_RIGHT, 5);
290
291 // put everything together
adbf2d73 292 sizerTop->Add(m_notebook, 1, wxGROW | (wxALL & ~(wxTOP | wxBOTTOM)), 10);
32b8ec41
VZ
293 sizerTop->Add(0, 5, 0, wxGROW); // spacer in between
294 sizerTop->Add(sizerDown, 0, wxGROW | (wxALL & ~wxTOP), 10);
295
32b8ec41
VZ
296 m_panel->SetSizer(sizerTop);
297
298 sizerTop->Fit(this);
299 sizerTop->SetSizeHints(this);
300
b29903d4 301#if wxUSE_LOG && !defined(__WXCOCOA__)
7bb70733 302 // wxCocoa's listbox is too flakey to use for logging right now
32b8ec41
VZ
303 // now that everything is created we can redirect the log messages to the
304 // listbox
305 m_logTarget = new LboxLogger(m_lboxLog, wxLog::GetActiveTarget());
306 wxLog::SetActiveTarget(m_logTarget);
b29903d4 307#endif
32b8ec41
VZ
308}
309
310void WidgetsFrame::InitNotebook()
311{
312 m_imaglist = new wxImageList(32, 32);
313
314 ArrayWidgetsPage pages;
315 wxArrayString labels;
316
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;
320 while ( info )
321 {
322 WidgetsPage *page = (*info->GetCtor())(m_notebook, m_imaglist);
323 pages.Add(page);
324
325 labels.Add(info->GetLabel());
326
327 info = info->GetNext();
328 }
329
330 m_notebook->SetImageList(m_imaglist);
331
332 // now do add them
333 size_t count = pages.GetCount();
334 for ( size_t n = 0; n < count; n++ )
335 {
336 m_notebook->AddPage(
337 pages[n],
338 labels[n],
206d3a16 339 false, // don't select
32b8ec41
VZ
340 n // image id
341 );
342 }
343}
344
345WidgetsFrame::~WidgetsFrame()
346{
b29903d4 347#if wxUSE_LOG
32b8ec41 348 delete m_logTarget;
b29903d4 349#endif // wxUSE_LOG
32b8ec41
VZ
350 delete m_imaglist;
351}
352
353// ----------------------------------------------------------------------------
354// WidgetsFrame event handlers
355// ----------------------------------------------------------------------------
356
357void WidgetsFrame::OnButtonQuit(wxCommandEvent& WXUNUSED(event))
358{
359 Close();
360}
361
b29903d4 362#if wxUSE_LOG
c02e5a31 363void WidgetsFrame::OnButtonClearLog(wxCommandEvent& WXUNUSED(event))
32b8ec41
VZ
364{
365 m_lboxLog->Clear();
366}
b29903d4 367#endif // wxUSE_LOG
32b8ec41
VZ
368
369// ----------------------------------------------------------------------------
370// WidgetsPageInfo
371// ----------------------------------------------------------------------------
372
373WidgetsPageInfo *WidgetsPage::ms_widgetPages = NULL;
374
375WidgetsPageInfo::WidgetsPageInfo(Constructor ctor, const wxChar *label)
376 : m_label(label)
377{
378 m_ctor = ctor;
379
2673bcb0
DS
380 m_next = NULL;
381
382 // dummy sorting: add and immediately sort on list according to label
383
384 if(WidgetsPage::ms_widgetPages)
385 {
386 WidgetsPageInfo *node_prev = WidgetsPage::ms_widgetPages;
387 if(wxStrcmp(label,node_prev->GetLabel().c_str())<0)
388 {
389 // add as first
390 m_next = node_prev;
391 WidgetsPage::ms_widgetPages = this;
392 }
393 else
394 {
395 WidgetsPageInfo *node_next;
396 do
397 {
398 node_next = node_prev->GetNext();
399 if(node_next)
400 {
401 // add if between two
402 if(wxStrcmp(label,node_next->GetLabel().c_str())<0)
403 {
404 node_prev->SetNext(this);
405 m_next = node_next;
406 // force to break loop
407 node_next = NULL;
408 }
409 }
410 else
411 {
412 // add as last
413 node_prev->SetNext(this);
414 m_next = node_next;
415 }
416 node_prev = node_next;
417 }while(node_next);
418 }
419 }
420 else
421 {
422 // add when first
423
424 WidgetsPage::ms_widgetPages = this;
425
426 }
427
32b8ec41
VZ
428}
429
430// ----------------------------------------------------------------------------
431// WidgetsPage
432// ----------------------------------------------------------------------------
433
434WidgetsPage::WidgetsPage(wxNotebook *notebook)
206d3a16 435 : wxPanel(notebook, wxID_ANY,
df0787b6
VZ
436 wxDefaultPosition, wxDefaultSize,
437 wxNO_FULL_REPAINT_ON_RESIZE |
438 wxCLIP_CHILDREN |
439 wxTAB_TRAVERSAL)
32b8ec41
VZ
440{
441}
442
443wxSizer *WidgetsPage::CreateSizerWithText(wxControl *control,
444 wxWindowID id,
445 wxTextCtrl **ppText)
446{
447 wxSizer *sizerRow = new wxBoxSizer(wxHORIZONTAL);
206d3a16
JS
448 wxTextCtrl *text = new wxTextCtrl(this, id, wxEmptyString,
449 wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER);
4589ec39 450
32b8ec41
VZ
451 sizerRow->Add(control, 0, wxRIGHT | wxALIGN_CENTRE_VERTICAL, 5);
452 sizerRow->Add(text, 1, wxLEFT | wxALIGN_CENTRE_VERTICAL, 5);
453
454 if ( ppText )
455 *ppText = text;
456
457 return sizerRow;
458}
459
460// create a sizer containing a label and a text ctrl
461wxSizer *WidgetsPage::CreateSizerWithTextAndLabel(const wxString& label,
462 wxWindowID id,
463 wxTextCtrl **ppText)
464{
206d3a16
JS
465 return CreateSizerWithText(new wxStaticText(this, wxID_ANY, label),
466 id, ppText);
32b8ec41
VZ
467}
468
469// create a sizer containing a button and a text ctrl
470wxSizer *WidgetsPage::CreateSizerWithTextAndButton(wxWindowID idBtn,
471 const wxString& label,
472 wxWindowID id,
473 wxTextCtrl **ppText)
474{
475 return CreateSizerWithText(new wxButton(this, idBtn, label), id, ppText);
476}
477
478wxCheckBox *WidgetsPage::CreateCheckBoxAndAddToSizer(wxSizer *sizer,
479 const wxString& label,
480 wxWindowID id)
481{
482 wxCheckBox *checkbox = new wxCheckBox(this, id, label);
483 sizer->Add(checkbox, 0, wxLEFT | wxRIGHT, 5);
484 sizer->Add(0, 2, 0, wxGROW); // spacer
485
486 return checkbox;
487}
488