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