]> git.saurik.com Git - wxWidgets.git/blob - samples/widgets/widgets.cpp
1e06e50c3bba8a54086be67da30bfc023b415dc8
[wxWidgets.git] / samples / widgets / widgets.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
3 // Name: samples/widgets/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 #include "wx/image.h"
34
35 #include "wx/button.h"
36 #include "wx/checkbox.h"
37 #include "wx/listbox.h"
38 #include "wx/statbox.h"
39 #include "wx/stattext.h"
40 #include "wx/textctrl.h"
41 #include "wx/msgdlg.h"
42 #endif
43
44 #include "wx/sysopt.h"
45 #include "wx/bookctrl.h"
46 #include "wx/treebook.h"
47 #include "wx/sizer.h"
48 #include "wx/colordlg.h"
49 #include "wx/fontdlg.h"
50 #include "wx/textdlg.h"
51 #include "wx/imaglist.h"
52 #include "wx/wupdlock.h"
53
54 #include "widgets.h"
55
56 #include "../sample.xpm"
57
58 // ----------------------------------------------------------------------------
59 // constants
60 // ----------------------------------------------------------------------------
61
62 // control ids
63 enum
64 {
65 Widgets_ClearLog = 100,
66 Widgets_Quit,
67
68 Widgets_BookCtrl,
69
70 #if wxUSE_TOOLTIPS
71 Widgets_SetTooltip,
72 #endif // wxUSE_TOOLTIPS
73 Widgets_SetFgColour,
74 Widgets_SetBgColour,
75 Widgets_SetFont,
76 Widgets_Enable,
77
78 Widgets_BorderNone,
79 Widgets_BorderStatic,
80 Widgets_BorderSimple,
81 Widgets_BorderRaised,
82 Widgets_BorderSunken,
83 Widgets_BorderDouble,
84 Widgets_BorderDefault,
85
86 Widgets_GlobalBusyCursor,
87 Widgets_BusyCursor,
88
89 Widgets_GoToPage,
90 Widgets_GoToPageLast = Widgets_GoToPage + 100
91 };
92
93 const wxChar *WidgetsCategories[MAX_PAGES] = {
94 #if defined(__WXUNIVERSAL__)
95 wxT("Universal"),
96 #else
97 wxT("Native"),
98 #endif
99 wxT("Generic"),
100 wxT("Pickers"),
101 wxT("Comboboxes"),
102 wxT("With items"),
103 wxT("Editable"),
104 wxT("Books"),
105 wxT("All controls")
106 };
107
108 // ----------------------------------------------------------------------------
109 // our classes
110 // ----------------------------------------------------------------------------
111
112 // Define a new application type, each program should derive a class from wxApp
113 class WidgetsApp : public wxApp
114 {
115 public:
116 // override base class virtuals
117 // ----------------------------
118
119 // this one is called on application startup and is a good place for the app
120 // initialization (doing it here and not in the ctor allows to have an error
121 // return: if OnInit() returns false, the application terminates)
122 virtual bool OnInit();
123 };
124
125 // Define a new frame type: this is going to be our main frame
126 class WidgetsFrame : public wxFrame
127 {
128 public:
129 // ctor(s) and dtor
130 WidgetsFrame(const wxString& title);
131 virtual ~WidgetsFrame();
132
133 protected:
134 // event handlers
135 #if USE_LOG
136 void OnButtonClearLog(wxCommandEvent& event);
137 #endif // USE_LOG
138 void OnExit(wxCommandEvent& event);
139
140 #if wxUSE_MENUS
141 void OnPageChanging(WidgetsBookCtrlEvent& event);
142 void OnPageChanged(WidgetsBookCtrlEvent& event);
143 void OnGoToPage(wxCommandEvent& event);
144
145 #if wxUSE_TOOLTIPS
146 void OnSetTooltip(wxCommandEvent& event);
147 #endif // wxUSE_TOOLTIPS
148 void OnSetFgCol(wxCommandEvent& event);
149 void OnSetBgCol(wxCommandEvent& event);
150 void OnSetFont(wxCommandEvent& event);
151 void OnEnable(wxCommandEvent& event);
152 void OnSetBorder(wxCommandEvent& event);
153
154 void OnToggleGlobalBusyCursor(wxCommandEvent& event);
155 void OnToggleBusyCursor(wxCommandEvent& event);
156 #endif // wxUSE_MENUS
157
158 // initialize the book: add all pages to it
159 void InitBook();
160
161 // return the currently selected page (never NULL)
162 WidgetsPage *CurrentPage();
163
164 private:
165 // the panel containing everything
166 wxPanel *m_panel;
167
168 #if USE_LOG
169 // the listbox for logging messages
170 wxListBox *m_lboxLog;
171
172 // the log target we use to redirect messages to the listbox
173 wxLog *m_logTarget;
174 #endif // USE_LOG
175
176 // the book containing the test pages
177 WidgetsBookCtrl *m_book;
178
179 #if wxUSE_MENUS
180 // last chosen fg/bg colours and font
181 wxColour m_colFg,
182 m_colBg;
183 wxFont m_font;
184 #endif // wxUSE_MENUS
185
186 // any class wishing to process wxWidgets events must use this macro
187 DECLARE_EVENT_TABLE()
188 };
189
190 #if USE_LOG
191 // A log target which just redirects the messages to a listbox
192 class LboxLogger : public wxLog
193 {
194 public:
195 LboxLogger(wxListBox *lbox, wxLog *logOld)
196 {
197 m_lbox = lbox;
198 //m_lbox->Disable(); -- looks ugly under MSW
199 m_logOld = logOld;
200 }
201
202 virtual ~LboxLogger()
203 {
204 wxLog::SetActiveTarget(m_logOld);
205 }
206
207 private:
208 // implement sink functions
209 virtual void DoLog(wxLogLevel level, const wxString& str, time_t t)
210 {
211 // don't put trace messages into listbox or we can get into infinite
212 // recursion
213 if ( level == wxLOG_Trace )
214 {
215 if ( m_logOld )
216 {
217 // cast is needed to call protected method
218 ((LboxLogger *)m_logOld)->DoLog(level, str, t);
219 }
220 }
221 else
222 {
223 wxLog::DoLog(level, str, t);
224 }
225 }
226
227 virtual void DoLogString(const wxString& str, time_t WXUNUSED(t))
228 {
229 wxString msg;
230 TimeStamp(&msg);
231 msg += str;
232
233 #ifdef __WXUNIVERSAL__
234 m_lbox->AppendAndEnsureVisible(msg);
235 #else // other ports don't have this method yet
236 m_lbox->Append(msg);
237 m_lbox->SetFirstItem(m_lbox->GetCount() - 1);
238 #endif
239 }
240
241 // the control we use
242 wxListBox *m_lbox;
243
244 // the old log target
245 wxLog *m_logOld;
246 };
247 #endif // USE_LOG
248
249 // array of pages
250 WX_DEFINE_ARRAY_PTR(WidgetsPage *, ArrayWidgetsPage);
251
252 // ----------------------------------------------------------------------------
253 // misc macros
254 // ----------------------------------------------------------------------------
255
256 IMPLEMENT_APP(WidgetsApp)
257
258 // ----------------------------------------------------------------------------
259 // event tables
260 // ----------------------------------------------------------------------------
261
262 BEGIN_EVENT_TABLE(WidgetsFrame, wxFrame)
263 #if USE_LOG
264 EVT_BUTTON(Widgets_ClearLog, WidgetsFrame::OnButtonClearLog)
265 #endif // USE_LOG
266 EVT_BUTTON(Widgets_Quit, WidgetsFrame::OnExit)
267
268 #if wxUSE_TOOLTIPS
269 EVT_MENU(Widgets_SetTooltip, WidgetsFrame::OnSetTooltip)
270 #endif // wxUSE_TOOLTIPS
271
272 #if wxUSE_MENUS
273 EVT_WIDGETS_PAGE_CHANGING(wxID_ANY, WidgetsFrame::OnPageChanging)
274 EVT_MENU_RANGE(Widgets_GoToPage, Widgets_GoToPageLast,
275 WidgetsFrame::OnGoToPage)
276
277 EVT_MENU(Widgets_SetFgColour, WidgetsFrame::OnSetFgCol)
278 EVT_MENU(Widgets_SetBgColour, WidgetsFrame::OnSetBgCol)
279 EVT_MENU(Widgets_SetFont, WidgetsFrame::OnSetFont)
280 EVT_MENU(Widgets_Enable, WidgetsFrame::OnEnable)
281
282 EVT_MENU_RANGE(Widgets_BorderNone, Widgets_BorderDefault,
283 WidgetsFrame::OnSetBorder)
284
285 EVT_MENU(Widgets_GlobalBusyCursor, WidgetsFrame::OnToggleGlobalBusyCursor)
286 EVT_MENU(Widgets_BusyCursor, WidgetsFrame::OnToggleBusyCursor)
287
288 EVT_MENU(wxID_EXIT, WidgetsFrame::OnExit)
289 #endif // wxUSE_MENUS
290 END_EVENT_TABLE()
291
292 // ============================================================================
293 // implementation
294 // ============================================================================
295
296 // ----------------------------------------------------------------------------
297 // app class
298 // ----------------------------------------------------------------------------
299
300 bool WidgetsApp::OnInit()
301 {
302 if ( !wxApp::OnInit() )
303 return false;
304
305 // the reason for having these ifdef's is that I often run two copies of
306 // this sample side by side and it is useful to see which one is which
307 wxString title;
308 #if defined(__WXUNIVERSAL__)
309 title = _T("wxUniv/");
310 #endif
311
312 #if defined(__WXMSW__)
313 title += _T("wxMSW");
314 #elif defined(__WXGTK__)
315 title += _T("wxGTK");
316 #elif defined(__WXMAC__)
317 title += _T("wxMAC");
318 #elif defined(__WXMOTIF__)
319 title += _T("wxMOTIF");
320 #elif __WXPALMOS5__
321 title += _T("wxPALMOS5");
322 #elif __WXPALMOS6__
323 title += _T("wxPALMOS6");
324 #else
325 title += _T("wxWidgets");
326 #endif
327
328 wxFrame *frame = new WidgetsFrame(title + _T(" widgets demo"));
329 frame->Show();
330
331 //wxLog::AddTraceMask(_T("listbox"));
332 //wxLog::AddTraceMask(_T("scrollbar"));
333 //wxLog::AddTraceMask(_T("focus"));
334
335 return true;
336 }
337
338 // ----------------------------------------------------------------------------
339 // WidgetsFrame construction
340 // ----------------------------------------------------------------------------
341
342 WidgetsFrame::WidgetsFrame(const wxString& title)
343 : wxFrame(NULL, wxID_ANY, title)
344 {
345 // set the frame icon
346 SetIcon(wxICON(sample));
347
348 // init everything
349 #if USE_LOG
350 m_lboxLog = (wxListBox *)NULL;
351 m_logTarget = (wxLog *)NULL;
352 #endif // USE_LOG
353 m_book = (WidgetsBookCtrl *)NULL;
354
355 #if wxUSE_MENUS
356 // create the menubar
357 wxMenuBar *mbar = new wxMenuBar;
358 wxMenu *menuWidget = new wxMenu;
359 #if wxUSE_TOOLTIPS
360 menuWidget->Append(Widgets_SetTooltip, _T("Set &tooltip...\tCtrl-T"));
361 menuWidget->AppendSeparator();
362 #endif // wxUSE_TOOLTIPS
363 menuWidget->Append(Widgets_SetFgColour, _T("Set &foreground...\tCtrl-F"));
364 menuWidget->Append(Widgets_SetBgColour, _T("Set &background...\tCtrl-B"));
365 menuWidget->Append(Widgets_SetFont, _T("Set f&ont...\tCtrl-O"));
366 menuWidget->AppendCheckItem(Widgets_Enable, _T("&Enable/disable\tCtrl-E"));
367
368 wxMenu *menuBorders = new wxMenu;
369 menuBorders->AppendRadioItem(Widgets_BorderDefault, _T("De&fault\tCtrl-Shift-9"));
370 menuBorders->AppendRadioItem(Widgets_BorderNone, _T("&None\tCtrl-Shift-0"));
371 menuBorders->AppendRadioItem(Widgets_BorderSimple, _T("&Simple\tCtrl-Shift-1"));
372 menuBorders->AppendRadioItem(Widgets_BorderDouble, _T("&Double\tCtrl-Shift-2"));
373 menuBorders->AppendRadioItem(Widgets_BorderStatic, _T("Stati&c\tCtrl-Shift-3"));
374 menuBorders->AppendRadioItem(Widgets_BorderRaised, _T("&Raised\tCtrl-Shift-4"));
375 menuBorders->AppendRadioItem(Widgets_BorderSunken, _T("S&unken\tCtrl-Shift-5"));
376 menuWidget->AppendSubMenu(menuBorders, _T("Set &border"));
377
378 menuWidget->AppendSeparator();
379 menuWidget->AppendCheckItem(Widgets_GlobalBusyCursor,
380 _T("Toggle &global busy cursor\tCtrl-Shift-U"));
381 menuWidget->AppendCheckItem(Widgets_BusyCursor,
382 _T("Toggle b&usy cursor\tCtrl-U"));
383
384 menuWidget->AppendSeparator();
385 menuWidget->Append(wxID_EXIT, _T("&Quit\tCtrl-Q"));
386 mbar->Append(menuWidget, _T("&Widget"));
387 SetMenuBar(mbar);
388
389 mbar->Check(Widgets_Enable, true);
390 #endif // wxUSE_MENUS
391
392 // create controls
393 m_panel = new wxPanel(this, wxID_ANY);
394
395 wxSizer *sizerTop = new wxBoxSizer(wxVERTICAL);
396
397 // we have 2 panes: book with pages demonstrating the controls in the
398 // upper one and the log window with some buttons in the lower
399
400 int style = wxBK_DEFAULT;
401 // Uncomment to suppress page theme (draw in solid colour)
402 //style |= wxNB_NOPAGETHEME;
403
404 m_book = new WidgetsBookCtrl(m_panel, Widgets_BookCtrl, wxDefaultPosition,
405 #ifdef __WXMOTIF__
406 wxSize(500, wxDefaultCoord), // under Motif, height is a function of the width...
407 #else
408 wxDefaultSize,
409 #endif
410 style);
411 InitBook();
412
413 #ifndef __WXHANDHELD__
414 // the lower one only has the log listbox and a button to clear it
415 #if USE_LOG
416 wxSizer *sizerDown = new wxStaticBoxSizer(
417 new wxStaticBox( m_panel, wxID_ANY, _T("&Log window") ),
418 wxVERTICAL);
419
420 m_lboxLog = new wxListBox(m_panel, wxID_ANY);
421 sizerDown->Add(m_lboxLog, 1, wxGROW | wxALL, 5);
422 sizerDown->SetMinSize(100, 150);
423 #else
424 wxSizer *sizerDown = new wxBoxSizer(wxVERTICAL);
425 #endif // USE_LOG
426
427 wxBoxSizer *sizerBtns = new wxBoxSizer(wxHORIZONTAL);
428 wxButton *btn;
429 #if USE_LOG
430 btn = new wxButton(m_panel, Widgets_ClearLog, _T("Clear &log"));
431 sizerBtns->Add(btn);
432 sizerBtns->Add(10, 0); // spacer
433 #endif // USE_LOG
434 btn = new wxButton(m_panel, Widgets_Quit, _T("E&xit"));
435 sizerBtns->Add(btn);
436 sizerDown->Add(sizerBtns, 0, wxALL | wxALIGN_RIGHT, 5);
437
438 // put everything together
439 sizerTop->Add(m_book, 1, wxGROW | (wxALL & ~(wxTOP | wxBOTTOM)), 10);
440 sizerTop->Add(0, 5, 0, wxGROW); // spacer in between
441 sizerTop->Add(sizerDown, 0, wxGROW | (wxALL & ~wxTOP), 10);
442
443 #else // !__WXHANDHELD__/__WXHANDHELD__
444
445 sizerTop->Add(m_book, 1, wxGROW | wxALL );
446
447 #endif // __WXHANDHELD__
448
449 m_panel->SetSizer(sizerTop);
450
451 sizerTop->Fit(this);
452 sizerTop->SetSizeHints(this);
453
454 #if USE_LOG && !defined(__WXCOCOA__)
455 // wxCocoa's listbox is too flakey to use for logging right now
456 // now that everything is created we can redirect the log messages to the
457 // listbox
458 m_logTarget = new LboxLogger(m_lboxLog, wxLog::GetActiveTarget());
459 wxLog::SetActiveTarget(m_logTarget);
460 #endif
461 }
462
463 void WidgetsFrame::InitBook()
464 {
465 #if USE_ICONS_IN_BOOK
466 wxImageList *imageList = new wxImageList(ICON_SIZE, ICON_SIZE);
467
468 wxImage img(sample_xpm);
469 imageList->Add(wxBitmap(img.Scale(ICON_SIZE, ICON_SIZE)));
470 #else
471 wxImageList *imageList = NULL;
472 #endif
473
474 #if !USE_TREEBOOK
475 WidgetsBookCtrl *books[MAX_PAGES];
476 #endif
477
478 ArrayWidgetsPage pages[MAX_PAGES];
479 wxArrayString labels[MAX_PAGES];
480
481 wxMenu *menuPages = new wxMenu;
482 unsigned int nPage = 0, nFKey = 0;
483 int cat, imageId = 1;
484
485 // we need to first create all pages and only then add them to the book
486 // as we need the image list first
487 //
488 // we also construct the pages menu during this first iteration
489 for ( cat = 0; cat < MAX_PAGES; cat++ )
490 {
491 #if USE_TREEBOOK
492 nPage++; // increase for parent page
493 #else
494 books[cat] = new WidgetsBookCtrl(m_book,
495 wxID_ANY,
496 wxDefaultPosition,
497 wxDefaultSize,
498 wxBK_DEFAULT);
499 #endif
500
501 for ( WidgetsPageInfo *info = WidgetsPage::ms_widgetPages;
502 info;
503 info = info->GetNext() )
504 {
505 if( (info->GetCategories() & ( 1 << cat )) == 0)
506 continue;
507
508 WidgetsPage *page = (*info->GetCtor())(
509 #if USE_TREEBOOK
510 m_book
511 #else
512 books[cat]
513 #endif
514 , imageList);
515 pages[cat].Add(page);
516
517 labels[cat].Add(info->GetLabel());
518 if ( cat == ALL_PAGE )
519 {
520 wxString radioLabel(info->GetLabel());
521 nFKey++;
522 if ( nFKey <= 12 )
523 {
524 radioLabel << wxT("\tF" ) << nFKey;
525 }
526
527 menuPages->AppendRadioItem(
528 Widgets_GoToPage + nPage,
529 radioLabel
530 );
531 #if !USE_TREEBOOK
532 // consider only for book in book architecture
533 nPage++;
534 #endif
535 }
536
537 #if USE_TREEBOOK
538 // consider only for treebook architecture (with subpages)
539 nPage++;
540 #endif
541 }
542 }
543
544 GetMenuBar()->Append(menuPages, _T("&Page"));
545
546 #if USE_ICONS_IN_BOOK
547 m_book->AssignImageList(imageList);
548 #endif
549
550 for ( cat = 0; cat < MAX_PAGES; cat++ )
551 {
552 #if USE_TREEBOOK
553 m_book->AddPage(NULL,WidgetsCategories[cat],false,0);
554 #else
555 m_book->AddPage(books[cat],WidgetsCategories[cat],false,0);
556 #if USE_ICONS_IN_BOOK
557 books[cat]->SetImageList(imageList);
558 #endif
559 #endif
560
561 // now do add them
562 size_t count = pages[cat].GetCount();
563 for ( size_t n = 0; n < count; n++ )
564 {
565 #if USE_TREEBOOK
566 m_book->AddSubPage
567 #else
568 books[cat]->AddPage
569 #endif
570 (
571 pages[cat][n],
572 labels[cat][n],
573 false, // don't select
574 imageId++
575 );
576 }
577 }
578
579 Connect( wxID_ANY,
580 wxEVT_COMMAND_WIDGETS_PAGE_CHANGED,
581 wxWidgetsbookEventHandler(WidgetsFrame::OnPageChanged) );
582
583 #if USE_TREEBOOK
584 // for treebook page #0 is empty parent page only so select the first page
585 // with some contents
586 m_book->SetSelection(1);
587
588 // but ensure that the top of the tree is shown nevertheless
589 wxTreeCtrl * const tree = m_book->GetTreeCtrl();
590
591 wxTreeItemIdValue cookie;
592 tree->EnsureVisible(tree->GetFirstChild(tree->GetRootItem(), cookie));
593 #else
594 // for other books set selection twice to force connected event handler
595 // to force lazy creation of initial visible content
596 m_book->SetSelection(1);
597 m_book->SetSelection(0);
598 #endif // USE_TREEBOOK
599 }
600
601 WidgetsPage *WidgetsFrame::CurrentPage()
602 {
603 wxWindow *page = m_book->GetCurrentPage();
604
605 #if !USE_TREEBOOK
606 WidgetsBookCtrl *subBook = wxStaticCast(page, WidgetsBookCtrl);
607 wxCHECK_MSG( subBook, NULL, _T("no WidgetsBookCtrl?") );
608
609 page = subBook->GetCurrentPage();
610 #endif // !USE_TREEBOOK
611
612 return wxStaticCast(page, WidgetsPage);
613 }
614
615 WidgetsFrame::~WidgetsFrame()
616 {
617 #if USE_LOG
618 delete m_logTarget;
619 #endif // USE_LOG
620 }
621
622 // ----------------------------------------------------------------------------
623 // WidgetsFrame event handlers
624 // ----------------------------------------------------------------------------
625
626 void WidgetsFrame::OnExit(wxCommandEvent& WXUNUSED(event))
627 {
628 Close();
629 }
630
631 #if USE_LOG
632 void WidgetsFrame::OnButtonClearLog(wxCommandEvent& WXUNUSED(event))
633 {
634 m_lboxLog->Clear();
635 }
636 #endif // USE_LOG
637
638 #if wxUSE_MENUS
639
640 void WidgetsFrame::OnPageChanging(WidgetsBookCtrlEvent& event)
641 {
642 #if USE_TREEBOOK
643 // don't allow selection of entries without pages (categories)
644 if ( !m_book->GetPage(event.GetSelection()) )
645 event.Veto();
646 #else
647 wxUnusedVar(event);
648 #endif
649 }
650
651 void WidgetsFrame::OnPageChanged(WidgetsBookCtrlEvent& event)
652 {
653 const int sel = event.GetSelection();
654
655 // adjust "Page" menu selection
656 wxMenuItem *item = GetMenuBar()->FindItem(Widgets_GoToPage + sel);
657 if ( item )
658 item->Check();
659
660 GetMenuBar()->Check(Widgets_BusyCursor, false);
661
662 // create the pages on demand, otherwise the sample startup is too slow as
663 // it creates hundreds of controls
664 WidgetsPage *page = CurrentPage();
665 if ( page->GetChildren().empty() )
666 {
667 wxWindowUpdateLocker noUpdates(page);
668 page->CreateContent();
669 //page->Layout();
670 page->GetSizer()->Fit(page);
671
672 WidgetsBookCtrl *book = wxStaticCast(page->GetParent(), WidgetsBookCtrl);
673 wxSize size;
674 for ( size_t i = 0; i < book->GetPageCount(); ++i )
675 {
676 wxWindow *page = book->GetPage(i);
677 if ( page )
678 {
679 size.IncTo(page->GetSize());
680 }
681 }
682 page->SetSize(size);
683 }
684
685 event.Skip();
686 }
687
688 void WidgetsFrame::OnGoToPage(wxCommandEvent& event)
689 {
690 #if USE_TREEBOOK
691 m_book->SetSelection(event.GetId() - Widgets_GoToPage);
692 #else
693 m_book->SetSelection(m_book->GetPageCount()-1);
694 WidgetsBookCtrl *book = wxStaticCast(m_book->GetCurrentPage(), WidgetsBookCtrl);
695 book->SetSelection(event.GetId() - Widgets_GoToPage);
696 #endif
697 }
698
699 #if wxUSE_TOOLTIPS
700
701 void WidgetsFrame::OnSetTooltip(wxCommandEvent& WXUNUSED(event))
702 {
703 static wxString s_tip = _T("This is a tooltip");
704
705 wxTextEntryDialog dialog
706 (
707 this,
708 _T("Tooltip text (may use \\n, leave empty to remove): "),
709 _T("Widgets sample"),
710 s_tip
711 );
712
713 if ( dialog.ShowModal() != wxID_OK )
714 return;
715
716 s_tip = dialog.GetValue();
717 s_tip.Replace(_T("\\n"), _T("\n"));
718
719 WidgetsPage *page = CurrentPage();
720
721 page->GetWidget()->SetToolTip(s_tip);
722
723 wxControl *ctrl2 = page->GetWidget2();
724 if ( ctrl2 )
725 ctrl2->SetToolTip(s_tip);
726 }
727
728 #endif // wxUSE_TOOLTIPS
729
730 void WidgetsFrame::OnSetFgCol(wxCommandEvent& WXUNUSED(event))
731 {
732 #if wxUSE_COLOURDLG
733 // allow for debugging the default colour the first time this is called
734 WidgetsPage *page = CurrentPage();
735
736 if (!m_colFg.Ok())
737 m_colFg = page->GetForegroundColour();
738
739 wxColour col = wxGetColourFromUser(this, m_colFg);
740 if ( !col.Ok() )
741 return;
742
743 m_colFg = col;
744
745 page->GetWidget()->SetForegroundColour(m_colFg);
746 page->GetWidget()->Refresh();
747
748 wxControl *ctrl2 = page->GetWidget2();
749 if ( ctrl2 )
750 {
751 ctrl2->SetForegroundColour(m_colFg);
752 ctrl2->Refresh();
753 }
754 #else
755 wxLogMessage(_T("Colour selection dialog not available in current build."));
756 #endif
757 }
758
759 void WidgetsFrame::OnSetBgCol(wxCommandEvent& WXUNUSED(event))
760 {
761 #if wxUSE_COLOURDLG
762 WidgetsPage *page = CurrentPage();
763
764 if ( !m_colBg.Ok() )
765 m_colBg = page->GetBackgroundColour();
766
767 wxColour col = wxGetColourFromUser(this, m_colBg);
768 if ( !col.Ok() )
769 return;
770
771 m_colBg = col;
772
773 page->GetWidget()->SetBackgroundColour(m_colBg);
774 page->GetWidget()->Refresh();
775
776 wxControl *ctrl2 = page->GetWidget2();
777 if ( ctrl2 )
778 {
779 ctrl2->SetBackgroundColour(m_colFg);
780 ctrl2->Refresh();
781 }
782 #else
783 wxLogMessage(_T("Colour selection dialog not available in current build."));
784 #endif
785 }
786
787 void WidgetsFrame::OnSetFont(wxCommandEvent& WXUNUSED(event))
788 {
789 #if wxUSE_FONTDLG
790 WidgetsPage *page = CurrentPage();
791
792 if (!m_font.Ok())
793 m_font = page->GetFont();
794
795 wxFont font = wxGetFontFromUser(this, m_font);
796 if ( !font.Ok() )
797 return;
798
799 m_font = font;
800
801 page->GetWidget()->SetFont(m_font);
802 page->GetWidget()->Refresh();
803
804 wxControl *ctrl2 = page->GetWidget2();
805 if ( ctrl2 )
806 {
807 ctrl2->SetFont(m_font);
808 ctrl2->Refresh();
809 }
810 #else
811 wxLogMessage(_T("Font selection dialog not available in current build."));
812 #endif
813 }
814
815 void WidgetsFrame::OnEnable(wxCommandEvent& event)
816 {
817 CurrentPage()->GetWidget()->Enable(event.IsChecked());
818 }
819
820 void WidgetsFrame::OnSetBorder(wxCommandEvent& event)
821 {
822 int border;
823 switch ( event.GetId() )
824 {
825 case Widgets_BorderNone: border = wxBORDER_NONE; break;
826 case Widgets_BorderStatic: border = wxBORDER_STATIC; break;
827 case Widgets_BorderSimple: border = wxBORDER_SIMPLE; break;
828 case Widgets_BorderRaised: border = wxBORDER_RAISED; break;
829 case Widgets_BorderSunken: border = wxBORDER_SUNKEN; break;
830 case Widgets_BorderDouble: border = wxBORDER_DOUBLE; break;
831
832 default:
833 wxFAIL_MSG( _T("unknown border style") );
834 // fall through
835
836 case Widgets_BorderDefault: border = wxBORDER_DEFAULT; break;
837 }
838
839 WidgetsPage::ms_defaultFlags &= ~wxBORDER_MASK;
840 WidgetsPage::ms_defaultFlags |= border;
841
842 WidgetsPage *page = CurrentPage();
843
844 page->RecreateWidget();
845 }
846
847 void WidgetsFrame::OnToggleGlobalBusyCursor(wxCommandEvent& event)
848 {
849 if ( event.IsChecked() )
850 wxBeginBusyCursor();
851 else
852 wxEndBusyCursor();
853 }
854
855 void WidgetsFrame::OnToggleBusyCursor(wxCommandEvent& event)
856 {
857 CurrentPage()->GetWidget()->SetCursor(*(event.IsChecked()
858 ? wxHOURGLASS_CURSOR
859 : wxSTANDARD_CURSOR));
860 }
861
862 #endif // wxUSE_MENUS
863
864 // ----------------------------------------------------------------------------
865 // WidgetsPageInfo
866 // ----------------------------------------------------------------------------
867
868 WidgetsPageInfo::WidgetsPageInfo(Constructor ctor, const wxChar *label, int categories)
869 : m_label(label)
870 , m_categories(categories)
871 {
872 m_ctor = ctor;
873
874 m_next = NULL;
875
876 // dummy sorting: add and immediately sort in the list according to label
877 if ( WidgetsPage::ms_widgetPages )
878 {
879 WidgetsPageInfo *node_prev = WidgetsPage::ms_widgetPages;
880 if ( wxStrcmp(label, node_prev->GetLabel().c_str()) < 0 )
881 {
882 // add as first
883 m_next = node_prev;
884 WidgetsPage::ms_widgetPages = this;
885 }
886 else
887 {
888 WidgetsPageInfo *node_next;
889 do
890 {
891 node_next = node_prev->GetNext();
892 if ( node_next )
893 {
894 // add if between two
895 if ( wxStrcmp(label, node_next->GetLabel().c_str()) < 0 )
896 {
897 node_prev->SetNext(this);
898 m_next = node_next;
899 // force to break loop
900 node_next = NULL;
901 }
902 }
903 else
904 {
905 // add as last
906 node_prev->SetNext(this);
907 m_next = node_next;
908 }
909 node_prev = node_next;
910 }
911 while ( node_next );
912 }
913 }
914 else
915 {
916 // add when first
917 WidgetsPage::ms_widgetPages = this;
918 }
919 }
920
921 // ----------------------------------------------------------------------------
922 // WidgetsPage
923 // ----------------------------------------------------------------------------
924
925 int WidgetsPage::ms_defaultFlags = wxBORDER_DEFAULT;
926 WidgetsPageInfo *WidgetsPage::ms_widgetPages = NULL;
927
928 WidgetsPage::WidgetsPage(WidgetsBookCtrl *book,
929 wxImageList *imaglist,
930 const char *const icon[])
931 : wxPanel(book, wxID_ANY,
932 wxDefaultPosition, wxDefaultSize,
933 wxNO_FULL_REPAINT_ON_RESIZE |
934 wxCLIP_CHILDREN |
935 wxTAB_TRAVERSAL)
936 {
937 #if USE_ICONS_IN_BOOK
938 imaglist->Add(wxBitmap(wxImage(icon).Scale(ICON_SIZE, ICON_SIZE)));
939 #else
940 wxUnusedVar(imaglist);
941 wxUnusedVar(icon);
942 #endif
943 }
944
945 wxSizer *WidgetsPage::CreateSizerWithText(wxControl *control,
946 wxWindowID id,
947 wxTextCtrl **ppText)
948 {
949 wxSizer *sizerRow = new wxBoxSizer(wxHORIZONTAL);
950 wxTextCtrl *text = new wxTextCtrl(this, id, wxEmptyString,
951 wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER);
952
953 sizerRow->Add(control, 0, wxRIGHT | wxALIGN_CENTRE_VERTICAL, 5);
954 sizerRow->Add(text, 1, wxLEFT | wxALIGN_CENTRE_VERTICAL, 5);
955
956 if ( ppText )
957 *ppText = text;
958
959 return sizerRow;
960 }
961
962 // create a sizer containing a label and a text ctrl
963 wxSizer *WidgetsPage::CreateSizerWithTextAndLabel(const wxString& label,
964 wxWindowID id,
965 wxTextCtrl **ppText)
966 {
967 return CreateSizerWithText(new wxStaticText(this, wxID_ANY, label),
968 id, ppText);
969 }
970
971 // create a sizer containing a button and a text ctrl
972 wxSizer *WidgetsPage::CreateSizerWithTextAndButton(wxWindowID idBtn,
973 const wxString& label,
974 wxWindowID id,
975 wxTextCtrl **ppText)
976 {
977 return CreateSizerWithText(new wxButton(this, idBtn, label), id, ppText);
978 }
979
980 wxCheckBox *WidgetsPage::CreateCheckBoxAndAddToSizer(wxSizer *sizer,
981 const wxString& label,
982 wxWindowID id)
983 {
984 wxCheckBox *checkbox = new wxCheckBox(this, id, label);
985 sizer->Add(checkbox, 0, wxLEFT | wxRIGHT, 5);
986 sizer->Add(0, 2, 0, wxGROW); // spacer
987
988 return checkbox;
989 }