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