]> git.saurik.com Git - wxWidgets.git/blame - samples/widgets/widgets.cpp
reSWIGged
[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 31 #include "wx/frame.h"
e6f3cbd2
VZ
32 #include "wx/menu.h"
33
32b8ec41
VZ
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"
40#endif
41
25057aba 42#include "wx/sysopt.h"
61c083e7 43#include "wx/bookctrl.h"
32b8ec41 44#include "wx/sizer.h"
195df7a7 45#include "wx/colordlg.h"
32b8ec41
VZ
46
47#include "widgets.h"
48
49// ----------------------------------------------------------------------------
50// constants
51// ----------------------------------------------------------------------------
52
53// control ids
54enum
55{
56 Widgets_ClearLog = 100,
195df7a7
VZ
57 Widgets_Quit,
58 Widgets_SetFgColour,
59 Widgets_SetBgColour
32b8ec41
VZ
60};
61
62// ----------------------------------------------------------------------------
63// our classes
64// ----------------------------------------------------------------------------
65
66// Define a new application type, each program should derive a class from wxApp
67class WidgetsApp : public wxApp
68{
69public:
70 // override base class virtuals
71 // ----------------------------
72
73 // this one is called on application startup and is a good place for the app
74 // initialization (doing it here and not in the ctor allows to have an error
75 // return: if OnInit() returns false, the application terminates)
76 virtual bool OnInit();
77};
78
79// Define a new frame type: this is going to be our main frame
80class WidgetsFrame : public wxFrame
81{
82public:
83 // ctor(s) and dtor
84 WidgetsFrame(const wxString& title);
85 virtual ~WidgetsFrame();
86
87protected:
88 // event handlers
49abcb2f 89#if USE_LOG
32b8ec41 90 void OnButtonClearLog(wxCommandEvent& event);
49abcb2f 91#endif // USE_LOG
195df7a7
VZ
92 void OnExit(wxCommandEvent& event);
93#if wxUSE_MENUS
94 void OnSetFgCol(wxCommandEvent& event);
95 void OnSetBgCol(wxCommandEvent& event);
96#endif // wxUSE_MENUS
32b8ec41 97
61c083e7
WS
98 // initialize the book: add all pages to it
99 void InitBook();
32b8ec41
VZ
100
101private:
102 // the panel containing everything
103 wxPanel *m_panel;
104
49abcb2f 105#if USE_LOG
32b8ec41
VZ
106 // the listbox for logging messages
107 wxListBox *m_lboxLog;
108
109 // the log target we use to redirect messages to the listbox
110 wxLog *m_logTarget;
49abcb2f 111#endif // USE_LOG
32b8ec41 112
61c083e7
WS
113 // the book containing the test pages
114 wxBookCtrl *m_book;
32b8ec41
VZ
115
116 // and the image list for it
117 wxImageList *m_imaglist;
118
195df7a7
VZ
119#if wxUSE_MENUS
120 // last chosen fg/bg colours
121 wxColour m_colFg,
122 m_colBg;
123#endif // wxUSE_MENUS
124
be5a51fb 125 // any class wishing to process wxWidgets events must use this macro
32b8ec41
VZ
126 DECLARE_EVENT_TABLE()
127};
128
49abcb2f 129#if USE_LOG
32b8ec41
VZ
130// A log target which just redirects the messages to a listbox
131class LboxLogger : public wxLog
132{
133public:
134 LboxLogger(wxListBox *lbox, wxLog *logOld)
135 {
136 m_lbox = lbox;
137 //m_lbox->Disable(); -- looks ugly under MSW
138 m_logOld = logOld;
139 }
140
141 virtual ~LboxLogger()
142 {
143 wxLog::SetActiveTarget(m_logOld);
144 }
145
146private:
147 // implement sink functions
148 virtual void DoLog(wxLogLevel level, const wxChar *szString, time_t t)
149 {
150 // don't put trace messages into listbox or we can get into infinite
151 // recursion
152 if ( level == wxLOG_Trace )
153 {
154 if ( m_logOld )
155 {
156 // cast is needed to call protected method
157 ((LboxLogger *)m_logOld)->DoLog(level, szString, t);
158 }
159 }
160 else
161 {
162 wxLog::DoLog(level, szString, t);
163 }
164 }
165
c02e5a31 166 virtual void DoLogString(const wxChar *szString, time_t WXUNUSED(t))
32b8ec41
VZ
167 {
168 wxString msg;
169 TimeStamp(&msg);
170 msg += szString;
171
172 #ifdef __WXUNIVERSAL__
173 m_lbox->AppendAndEnsureVisible(msg);
174 #else // other ports don't have this method yet
175 m_lbox->Append(msg);
176 m_lbox->SetFirstItem(m_lbox->GetCount() - 1);
177 #endif
178 }
179
180 // the control we use
181 wxListBox *m_lbox;
182
183 // the old log target
184 wxLog *m_logOld;
185};
49abcb2f 186#endif // USE_LOG
32b8ec41
VZ
187
188// array of pages
38d6b957 189WX_DEFINE_ARRAY_PTR(WidgetsPage *, ArrayWidgetsPage);
32b8ec41
VZ
190
191// ----------------------------------------------------------------------------
192// misc macros
193// ----------------------------------------------------------------------------
194
195IMPLEMENT_APP(WidgetsApp)
196
32b8ec41
VZ
197// ----------------------------------------------------------------------------
198// event tables
199// ----------------------------------------------------------------------------
200
201BEGIN_EVENT_TABLE(WidgetsFrame, wxFrame)
49abcb2f 202#if USE_LOG
32b8ec41 203 EVT_BUTTON(Widgets_ClearLog, WidgetsFrame::OnButtonClearLog)
49abcb2f 204#endif // USE_LOG
195df7a7
VZ
205 EVT_BUTTON(Widgets_Quit, WidgetsFrame::OnExit)
206
207 EVT_MENU(wxID_EXIT, WidgetsFrame::OnExit)
208 EVT_MENU(Widgets_SetFgColour, WidgetsFrame::OnSetFgCol)
209 EVT_MENU(Widgets_SetBgColour, WidgetsFrame::OnSetBgCol)
32b8ec41
VZ
210END_EVENT_TABLE()
211
212// ============================================================================
213// implementation
214// ============================================================================
215
216// ----------------------------------------------------------------------------
217// app class
218// ----------------------------------------------------------------------------
219
220bool WidgetsApp::OnInit()
221{
bf188f1a 222 if ( !wxApp::OnInit() )
206d3a16 223 return false;
25057aba 224
32b8ec41
VZ
225 // the reason for having these ifdef's is that I often run two copies of
226 // this sample side by side and it is useful to see which one is which
24e78d27 227 wxString title;
32b8ec41 228#if defined(__WXUNIVERSAL__)
085a1f3c 229 title = _T("wxUniv/");
24e78d27
VZ
230#endif
231
232#if defined(__WXMSW__)
233 title += _T("wxMSW");
32b8ec41 234#elif defined(__WXGTK__)
24e78d27 235 title += _T("wxGTK");
0f9390c3
GD
236#elif defined(__WXMAC__)
237 title += _T("wxMAC");
238#elif defined(__WXMOTIF__)
239 title += _T("wxMOTIF");
32b8ec41 240#else
be5a51fb 241 title += _T("wxWidgets");
32b8ec41 242#endif
32b8ec41
VZ
243
244 wxFrame *frame = new WidgetsFrame(title + _T(" widgets demo"));
245 frame->Show();
246
247 //wxLog::AddTraceMask(_T("listbox"));
248 //wxLog::AddTraceMask(_T("scrollbar"));
58ec2255 249 //wxLog::AddTraceMask(_T("focus"));
32b8ec41 250
206d3a16 251 return true;
32b8ec41
VZ
252}
253
254// ----------------------------------------------------------------------------
255// WidgetsFrame construction
256// ----------------------------------------------------------------------------
257
258WidgetsFrame::WidgetsFrame(const wxString& title)
206d3a16 259 : wxFrame(NULL, wxID_ANY, title,
df0787b6
VZ
260 wxPoint(0, 50), wxDefaultSize,
261 wxDEFAULT_FRAME_STYLE |
262 wxNO_FULL_REPAINT_ON_RESIZE |
263 wxCLIP_CHILDREN |
264 wxTAB_TRAVERSAL)
32b8ec41
VZ
265{
266 // init everything
49abcb2f 267#if USE_LOG
32b8ec41
VZ
268 m_lboxLog = (wxListBox *)NULL;
269 m_logTarget = (wxLog *)NULL;
49abcb2f 270#endif // USE_LOG
61c083e7 271 m_book = (wxBookCtrl *)NULL;
32b8ec41
VZ
272 m_imaglist = (wxImageList *)NULL;
273
195df7a7
VZ
274#if wxUSE_MENUS
275 // create the menubar
276 wxMenuBar *mbar = new wxMenuBar;
277 wxMenu *menuWidget = new wxMenu;
278 menuWidget->Append(Widgets_SetFgColour, _T("Set &foreground...\tCtrl-F"));
279 menuWidget->Append(Widgets_SetBgColour, _T("Set &background...\tCtrl-B"));
280 menuWidget->AppendSeparator();
281 menuWidget->Append(wxID_EXIT, _T("&Quit\tCtrl-Q"));
282 mbar->Append(menuWidget, _T("&Widget"));
283 SetMenuBar(mbar);
284#endif // wxUSE_MENUS
285
32b8ec41 286 // create controls
206d3a16
JS
287 m_panel = new wxPanel(this, wxID_ANY,
288 wxDefaultPosition, wxDefaultSize, wxCLIP_CHILDREN);
32b8ec41
VZ
289
290 wxSizer *sizerTop = new wxBoxSizer(wxVERTICAL);
291
25057aba 292 // we have 2 panes: book with pages demonstrating the controls in the
32b8ec41 293 // upper one and the log window with some buttons in the lower
25057aba
JS
294
295 int style = wxNO_FULL_REPAINT_ON_RESIZE|wxCLIP_CHILDREN|wxBC_DEFAULT;
296 // Uncomment to suppress page theme (draw in solid colour)
297 // style |= wxNB_NOPAGETHEME;
32b8ec41 298
61c083e7 299 m_book = new wxBookCtrl(m_panel, wxID_ANY, wxDefaultPosition,
25057aba 300 wxDefaultSize, style);
61c083e7 301 InitBook();
32b8ec41 302
49abcb2f 303#ifndef __SMARTPHONE__
32b8ec41 304 // the lower one only has the log listbox and a button to clear it
49abcb2f 305#if USE_LOG
206d3a16
JS
306 wxSizer *sizerDown = new wxStaticBoxSizer(
307 new wxStaticBox( m_panel, wxID_ANY, _T("&Log window") ),
308 wxVERTICAL);
309
310 m_lboxLog = new wxListBox(m_panel, wxID_ANY);
32b8ec41 311 sizerDown->Add(m_lboxLog, 1, wxGROW | wxALL, 5);
7b127900 312 sizerDown->SetMinSize(100, 150);
b29903d4
WS
313#else
314 wxSizer *sizerDown = new wxBoxSizer(wxVERTICAL);
49abcb2f 315#endif // USE_LOG
88633ca7 316
32b8ec41 317 wxBoxSizer *sizerBtns = new wxBoxSizer(wxHORIZONTAL);
b29903d4 318 wxButton *btn;
49abcb2f 319#if USE_LOG
b29903d4 320 btn = new wxButton(m_panel, Widgets_ClearLog, _T("Clear &log"));
32b8ec41
VZ
321 sizerBtns->Add(btn);
322 sizerBtns->Add(10, 0); // spacer
49abcb2f 323#endif // USE_LOG
32b8ec41
VZ
324 btn = new wxButton(m_panel, Widgets_Quit, _T("E&xit"));
325 sizerBtns->Add(btn);
326 sizerDown->Add(sizerBtns, 0, wxALL | wxALIGN_RIGHT, 5);
327
328 // put everything together
61c083e7 329 sizerTop->Add(m_book, 1, wxGROW | (wxALL & ~(wxTOP | wxBOTTOM)), 10);
32b8ec41
VZ
330 sizerTop->Add(0, 5, 0, wxGROW); // spacer in between
331 sizerTop->Add(sizerDown, 0, wxGROW | (wxALL & ~wxTOP), 10);
332
49abcb2f
WS
333#else // !__SMARTPHONE__/__SMARTPHONE__
334
335 sizerTop->Add(m_book, 1, wxGROW | wxALL );
336
337#endif // __SMARTPHONE__
338
32b8ec41
VZ
339 m_panel->SetSizer(sizerTop);
340
341 sizerTop->Fit(this);
342 sizerTop->SetSizeHints(this);
343
49abcb2f 344#if USE_LOG && !defined(__WXCOCOA__)
7bb70733 345 // wxCocoa's listbox is too flakey to use for logging right now
32b8ec41
VZ
346 // now that everything is created we can redirect the log messages to the
347 // listbox
348 m_logTarget = new LboxLogger(m_lboxLog, wxLog::GetActiveTarget());
349 wxLog::SetActiveTarget(m_logTarget);
b29903d4 350#endif
32b8ec41
VZ
351}
352
61c083e7 353void WidgetsFrame::InitBook()
32b8ec41
VZ
354{
355 m_imaglist = new wxImageList(32, 32);
356
357 ArrayWidgetsPage pages;
358 wxArrayString labels;
359
61c083e7 360 // we need to first create all pages and only then add them to the book
32b8ec41
VZ
361 // as we need the image list first
362 WidgetsPageInfo *info = WidgetsPage::ms_widgetPages;
363 while ( info )
364 {
61c083e7 365 WidgetsPage *page = (*info->GetCtor())(m_book, m_imaglist);
32b8ec41
VZ
366 pages.Add(page);
367
368 labels.Add(info->GetLabel());
369
370 info = info->GetNext();
371 }
372
61c083e7 373 m_book->SetImageList(m_imaglist);
32b8ec41
VZ
374
375 // now do add them
376 size_t count = pages.GetCount();
377 for ( size_t n = 0; n < count; n++ )
378 {
61c083e7
WS
379 m_book->AddPage(
380 pages[n],
381 labels[n],
382 false, // don't select
383 n // image id
384 );
25057aba
JS
385
386/*
387 wxColour colour = m_book->MSWGetBgColourForChild(pages[n]);
388 pages[n]->SetBackgroundColour(colour);
389*/
32b8ec41
VZ
390 }
391}
392
393WidgetsFrame::~WidgetsFrame()
394{
49abcb2f 395#if USE_LOG
32b8ec41 396 delete m_logTarget;
49abcb2f 397#endif // USE_LOG
32b8ec41
VZ
398 delete m_imaglist;
399}
400
401// ----------------------------------------------------------------------------
402// WidgetsFrame event handlers
403// ----------------------------------------------------------------------------
404
195df7a7 405void WidgetsFrame::OnExit(wxCommandEvent& WXUNUSED(event))
32b8ec41
VZ
406{
407 Close();
408}
409
49abcb2f 410#if USE_LOG
c02e5a31 411void WidgetsFrame::OnButtonClearLog(wxCommandEvent& WXUNUSED(event))
32b8ec41
VZ
412{
413 m_lboxLog->Clear();
414}
49abcb2f 415#endif // USE_LOG
32b8ec41 416
195df7a7
VZ
417#if wxUSE_MENUS
418
419void WidgetsFrame::OnSetFgCol(wxCommandEvent& WXUNUSED(event))
420{
923f4e79 421#if wxUSE_COLOURDLG
195df7a7
VZ
422 wxColour col = wxGetColourFromUser(this, m_colFg);
423 if ( !col.Ok() )
424 return;
425
426 m_colFg = col;
427
61c083e7 428 WidgetsPage *page = wxStaticCast(m_book->GetCurrentPage(), WidgetsPage);
195df7a7 429 page->GetWidget()->SetForegroundColour(m_colFg);
750972ab 430 page->GetWidget()->Refresh();
923f4e79
WS
431#else
432 wxLogMessage(_T("None colour dialog available in current build."));
433#endif
195df7a7
VZ
434}
435
436void WidgetsFrame::OnSetBgCol(wxCommandEvent& WXUNUSED(event))
437{
923f4e79 438#if wxUSE_COLOURDLG
195df7a7
VZ
439 wxColour col = wxGetColourFromUser(this, m_colBg);
440 if ( !col.Ok() )
441 return;
442
443 m_colBg = col;
444
61c083e7 445 WidgetsPage *page = wxStaticCast(m_book->GetCurrentPage(), WidgetsPage);
195df7a7 446 page->GetWidget()->SetBackgroundColour(m_colBg);
750972ab 447 page->GetWidget()->Refresh();
923f4e79
WS
448#else
449 wxLogMessage(_T("None colour dialog available in current build."));
450#endif
195df7a7
VZ
451}
452
453#endif // wxUSE_MENUS
454
32b8ec41
VZ
455// ----------------------------------------------------------------------------
456// WidgetsPageInfo
457// ----------------------------------------------------------------------------
458
459WidgetsPageInfo *WidgetsPage::ms_widgetPages = NULL;
460
461WidgetsPageInfo::WidgetsPageInfo(Constructor ctor, const wxChar *label)
462 : m_label(label)
463{
464 m_ctor = ctor;
465
2673bcb0
DS
466 m_next = NULL;
467
468 // dummy sorting: add and immediately sort on list according to label
469
470 if(WidgetsPage::ms_widgetPages)
471 {
472 WidgetsPageInfo *node_prev = WidgetsPage::ms_widgetPages;
473 if(wxStrcmp(label,node_prev->GetLabel().c_str())<0)
474 {
475 // add as first
476 m_next = node_prev;
477 WidgetsPage::ms_widgetPages = this;
478 }
479 else
480 {
481 WidgetsPageInfo *node_next;
482 do
483 {
484 node_next = node_prev->GetNext();
485 if(node_next)
486 {
487 // add if between two
488 if(wxStrcmp(label,node_next->GetLabel().c_str())<0)
489 {
490 node_prev->SetNext(this);
491 m_next = node_next;
492 // force to break loop
493 node_next = NULL;
494 }
495 }
496 else
497 {
498 // add as last
499 node_prev->SetNext(this);
500 m_next = node_next;
501 }
502 node_prev = node_next;
503 }while(node_next);
504 }
505 }
506 else
507 {
508 // add when first
509
510 WidgetsPage::ms_widgetPages = this;
511
512 }
513
32b8ec41
VZ
514}
515
516// ----------------------------------------------------------------------------
517// WidgetsPage
518// ----------------------------------------------------------------------------
519
61c083e7
WS
520WidgetsPage::WidgetsPage(wxBookCtrl *book)
521 : wxPanel(book, wxID_ANY,
df0787b6
VZ
522 wxDefaultPosition, wxDefaultSize,
523 wxNO_FULL_REPAINT_ON_RESIZE |
524 wxCLIP_CHILDREN |
525 wxTAB_TRAVERSAL)
32b8ec41
VZ
526{
527}
528
529wxSizer *WidgetsPage::CreateSizerWithText(wxControl *control,
530 wxWindowID id,
531 wxTextCtrl **ppText)
532{
533 wxSizer *sizerRow = new wxBoxSizer(wxHORIZONTAL);
206d3a16
JS
534 wxTextCtrl *text = new wxTextCtrl(this, id, wxEmptyString,
535 wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER);
4589ec39 536
32b8ec41
VZ
537 sizerRow->Add(control, 0, wxRIGHT | wxALIGN_CENTRE_VERTICAL, 5);
538 sizerRow->Add(text, 1, wxLEFT | wxALIGN_CENTRE_VERTICAL, 5);
539
540 if ( ppText )
541 *ppText = text;
542
543 return sizerRow;
544}
545
546// create a sizer containing a label and a text ctrl
547wxSizer *WidgetsPage::CreateSizerWithTextAndLabel(const wxString& label,
548 wxWindowID id,
549 wxTextCtrl **ppText)
550{
206d3a16
JS
551 return CreateSizerWithText(new wxStaticText(this, wxID_ANY, label),
552 id, ppText);
32b8ec41
VZ
553}
554
555// create a sizer containing a button and a text ctrl
556wxSizer *WidgetsPage::CreateSizerWithTextAndButton(wxWindowID idBtn,
557 const wxString& label,
558 wxWindowID id,
559 wxTextCtrl **ppText)
560{
561 return CreateSizerWithText(new wxButton(this, idBtn, label), id, ppText);
562}
563
564wxCheckBox *WidgetsPage::CreateCheckBoxAndAddToSizer(wxSizer *sizer,
565 const wxString& label,
566 wxWindowID id)
567{
568 wxCheckBox *checkbox = new wxCheckBox(this, id, label);
569 sizer->Add(checkbox, 0, wxLEFT | wxRIGHT, 5);
570 sizer->Add(0, 2, 0, wxGROW); // spacer
571
572 return checkbox;
573}
574