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