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