]> git.saurik.com Git - wxWidgets.git/blob - samples/widgets/widgets.cpp
use wxDC::GetMultiLineTextExtent() instead of duplicating its code in wxButton::DoGet...
[wxWidgets.git] / samples / widgets / widgets.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
3 // Name: widgets.cpp
4 // Purpose: Sample showing most of the simple wxWidgets widgets
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"
31 #include "wx/frame.h"
32 #include "wx/menu.h"
33
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
42 #include "wx/sysopt.h"
43 #include "wx/bookctrl.h"
44 #include "wx/sizer.h"
45 #include "wx/colordlg.h"
46
47 #include "widgets.h"
48
49 // ----------------------------------------------------------------------------
50 // constants
51 // ----------------------------------------------------------------------------
52
53 // control ids
54 enum
55 {
56 Widgets_ClearLog = 100,
57 Widgets_Quit,
58 Widgets_SetFgColour,
59 Widgets_SetBgColour
60 };
61
62 // ----------------------------------------------------------------------------
63 // our classes
64 // ----------------------------------------------------------------------------
65
66 // Define a new application type, each program should derive a class from wxApp
67 class WidgetsApp : public wxApp
68 {
69 public:
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
80 class WidgetsFrame : public wxFrame
81 {
82 public:
83 // ctor(s) and dtor
84 WidgetsFrame(const wxString& title);
85 virtual ~WidgetsFrame();
86
87 protected:
88 // event handlers
89 #if USE_LOG
90 void OnButtonClearLog(wxCommandEvent& event);
91 #endif // USE_LOG
92 void OnExit(wxCommandEvent& event);
93 #if wxUSE_MENUS
94 void OnSetFgCol(wxCommandEvent& event);
95 void OnSetBgCol(wxCommandEvent& event);
96 #endif // wxUSE_MENUS
97
98 // initialize the book: add all pages to it
99 void InitBook();
100
101 private:
102 // the panel containing everything
103 wxPanel *m_panel;
104
105 #if USE_LOG
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;
111 #endif // USE_LOG
112
113 // the book containing the test pages
114 wxBookCtrl *m_book;
115
116 // and the image list for it
117 wxImageList *m_imaglist;
118
119 #if wxUSE_MENUS
120 // last chosen fg/bg colours
121 wxColour m_colFg,
122 m_colBg;
123 #endif // wxUSE_MENUS
124
125 // any class wishing to process wxWidgets events must use this macro
126 DECLARE_EVENT_TABLE()
127 };
128
129 #if USE_LOG
130 // A log target which just redirects the messages to a listbox
131 class LboxLogger : public wxLog
132 {
133 public:
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
146 private:
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
166 virtual void DoLogString(const wxChar *szString, time_t WXUNUSED(t))
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 };
186 #endif // USE_LOG
187
188 // array of pages
189 WX_DEFINE_ARRAY_PTR(WidgetsPage *, ArrayWidgetsPage);
190
191 // ----------------------------------------------------------------------------
192 // misc macros
193 // ----------------------------------------------------------------------------
194
195 IMPLEMENT_APP(WidgetsApp)
196
197 // ----------------------------------------------------------------------------
198 // event tables
199 // ----------------------------------------------------------------------------
200
201 BEGIN_EVENT_TABLE(WidgetsFrame, wxFrame)
202 #if USE_LOG
203 EVT_BUTTON(Widgets_ClearLog, WidgetsFrame::OnButtonClearLog)
204 #endif // USE_LOG
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)
210 END_EVENT_TABLE()
211
212 // ============================================================================
213 // implementation
214 // ============================================================================
215
216 // ----------------------------------------------------------------------------
217 // app class
218 // ----------------------------------------------------------------------------
219
220 bool WidgetsApp::OnInit()
221 {
222 if ( !wxApp::OnInit() )
223 return false;
224
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
227 wxString title;
228 #if defined(__WXUNIVERSAL__)
229 title = _T("wxUniv/");
230 #endif
231
232 #if defined(__WXMSW__)
233 title += _T("wxMSW");
234 #elif defined(__WXGTK__)
235 title += _T("wxGTK");
236 #elif defined(__WXMAC__)
237 title += _T("wxMAC");
238 #elif defined(__WXMOTIF__)
239 title += _T("wxMOTIF");
240 #else
241 title += _T("wxWidgets");
242 #endif
243
244 wxFrame *frame = new WidgetsFrame(title + _T(" widgets demo"));
245 frame->Show();
246
247 //wxLog::AddTraceMask(_T("listbox"));
248 //wxLog::AddTraceMask(_T("scrollbar"));
249 //wxLog::AddTraceMask(_T("focus"));
250
251 return true;
252 }
253
254 // ----------------------------------------------------------------------------
255 // WidgetsFrame construction
256 // ----------------------------------------------------------------------------
257
258 WidgetsFrame::WidgetsFrame(const wxString& title)
259 : wxFrame(NULL, wxID_ANY, title,
260 wxPoint(0, 50), wxDefaultSize,
261 wxDEFAULT_FRAME_STYLE |
262 wxNO_FULL_REPAINT_ON_RESIZE |
263 wxCLIP_CHILDREN |
264 wxTAB_TRAVERSAL)
265 {
266 // init everything
267 #if USE_LOG
268 m_lboxLog = (wxListBox *)NULL;
269 m_logTarget = (wxLog *)NULL;
270 #endif // USE_LOG
271 m_book = (wxBookCtrl *)NULL;
272 m_imaglist = (wxImageList *)NULL;
273
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
286 // create controls
287 m_panel = new wxPanel(this, wxID_ANY,
288 wxDefaultPosition, wxDefaultSize, wxCLIP_CHILDREN);
289
290 wxSizer *sizerTop = new wxBoxSizer(wxVERTICAL);
291
292 // we have 2 panes: book with pages demonstrating the controls in the
293 // upper one and the log window with some buttons in the lower
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;
298
299 m_book = new wxBookCtrl(m_panel, wxID_ANY, wxDefaultPosition,
300 wxDefaultSize, style);
301 InitBook();
302
303 #ifndef __SMARTPHONE__
304 // the lower one only has the log listbox and a button to clear it
305 #if USE_LOG
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);
311 sizerDown->Add(m_lboxLog, 1, wxGROW | wxALL, 5);
312 sizerDown->SetMinSize(100, 150);
313 #else
314 wxSizer *sizerDown = new wxBoxSizer(wxVERTICAL);
315 #endif // USE_LOG
316
317 wxBoxSizer *sizerBtns = new wxBoxSizer(wxHORIZONTAL);
318 wxButton *btn;
319 #if USE_LOG
320 btn = new wxButton(m_panel, Widgets_ClearLog, _T("Clear &log"));
321 sizerBtns->Add(btn);
322 sizerBtns->Add(10, 0); // spacer
323 #endif // USE_LOG
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
329 sizerTop->Add(m_book, 1, wxGROW | (wxALL & ~(wxTOP | wxBOTTOM)), 10);
330 sizerTop->Add(0, 5, 0, wxGROW); // spacer in between
331 sizerTop->Add(sizerDown, 0, wxGROW | (wxALL & ~wxTOP), 10);
332
333 #else // !__SMARTPHONE__/__SMARTPHONE__
334
335 sizerTop->Add(m_book, 1, wxGROW | wxALL );
336
337 #endif // __SMARTPHONE__
338
339 m_panel->SetSizer(sizerTop);
340
341 sizerTop->Fit(this);
342 sizerTop->SetSizeHints(this);
343
344 #if USE_LOG && !defined(__WXCOCOA__)
345 // wxCocoa's listbox is too flakey to use for logging right now
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);
350 #endif
351 }
352
353 void WidgetsFrame::InitBook()
354 {
355 m_imaglist = new wxImageList(32, 32);
356
357 ArrayWidgetsPage pages;
358 wxArrayString labels;
359
360 // we need to first create all pages and only then add them to the book
361 // as we need the image list first
362 WidgetsPageInfo *info = WidgetsPage::ms_widgetPages;
363 while ( info )
364 {
365 WidgetsPage *page = (*info->GetCtor())(m_book, m_imaglist);
366 pages.Add(page);
367
368 labels.Add(info->GetLabel());
369
370 info = info->GetNext();
371 }
372
373 m_book->SetImageList(m_imaglist);
374
375 // now do add them
376 size_t count = pages.GetCount();
377 for ( size_t n = 0; n < count; n++ )
378 {
379 m_book->AddPage(
380 pages[n],
381 labels[n],
382 false, // don't select
383 n // image id
384 );
385
386 /*
387 wxColour colour = m_book->MSWGetBgColourForChild(pages[n]);
388 pages[n]->SetBackgroundColour(colour);
389 */
390 }
391 }
392
393 WidgetsFrame::~WidgetsFrame()
394 {
395 #if USE_LOG
396 delete m_logTarget;
397 #endif // USE_LOG
398 delete m_imaglist;
399 }
400
401 // ----------------------------------------------------------------------------
402 // WidgetsFrame event handlers
403 // ----------------------------------------------------------------------------
404
405 void WidgetsFrame::OnExit(wxCommandEvent& WXUNUSED(event))
406 {
407 Close();
408 }
409
410 #if USE_LOG
411 void WidgetsFrame::OnButtonClearLog(wxCommandEvent& WXUNUSED(event))
412 {
413 m_lboxLog->Clear();
414 }
415 #endif // USE_LOG
416
417 #if wxUSE_MENUS
418
419 void WidgetsFrame::OnSetFgCol(wxCommandEvent& WXUNUSED(event))
420 {
421 #if wxUSE_COLOURDLG
422 wxColour col = wxGetColourFromUser(this, m_colFg);
423 if ( !col.Ok() )
424 return;
425
426 m_colFg = col;
427
428 WidgetsPage *page = wxStaticCast(m_book->GetCurrentPage(), WidgetsPage);
429 page->GetWidget()->SetForegroundColour(m_colFg);
430 page->GetWidget()->Refresh();
431 #else
432 wxLogMessage(_T("None colour dialog available in current build."));
433 #endif
434 }
435
436 void WidgetsFrame::OnSetBgCol(wxCommandEvent& WXUNUSED(event))
437 {
438 #if wxUSE_COLOURDLG
439 wxColour col = wxGetColourFromUser(this, m_colBg);
440 if ( !col.Ok() )
441 return;
442
443 m_colBg = col;
444
445 WidgetsPage *page = wxStaticCast(m_book->GetCurrentPage(), WidgetsPage);
446 page->GetWidget()->SetBackgroundColour(m_colBg);
447 page->GetWidget()->Refresh();
448 #else
449 wxLogMessage(_T("None colour dialog available in current build."));
450 #endif
451 }
452
453 #endif // wxUSE_MENUS
454
455 // ----------------------------------------------------------------------------
456 // WidgetsPageInfo
457 // ----------------------------------------------------------------------------
458
459 WidgetsPageInfo *WidgetsPage::ms_widgetPages = NULL;
460
461 WidgetsPageInfo::WidgetsPageInfo(Constructor ctor, const wxChar *label)
462 : m_label(label)
463 {
464 m_ctor = ctor;
465
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
514 }
515
516 // ----------------------------------------------------------------------------
517 // WidgetsPage
518 // ----------------------------------------------------------------------------
519
520 WidgetsPage::WidgetsPage(wxBookCtrl *book)
521 : wxPanel(book, wxID_ANY,
522 wxDefaultPosition, wxDefaultSize,
523 wxNO_FULL_REPAINT_ON_RESIZE |
524 wxCLIP_CHILDREN |
525 wxTAB_TRAVERSAL)
526 {
527 }
528
529 wxSizer *WidgetsPage::CreateSizerWithText(wxControl *control,
530 wxWindowID id,
531 wxTextCtrl **ppText)
532 {
533 wxSizer *sizerRow = new wxBoxSizer(wxHORIZONTAL);
534 wxTextCtrl *text = new wxTextCtrl(this, id, wxEmptyString,
535 wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER);
536
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
547 wxSizer *WidgetsPage::CreateSizerWithTextAndLabel(const wxString& label,
548 wxWindowID id,
549 wxTextCtrl **ppText)
550 {
551 return CreateSizerWithText(new wxStaticText(this, wxID_ANY, label),
552 id, ppText);
553 }
554
555 // create a sizer containing a button and a text ctrl
556 wxSizer *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
564 wxCheckBox *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