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