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