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