]> git.saurik.com Git - wxWidgets.git/blob - samples/widgets/widgets.cpp
f432060c34198ea2c80d2c29d1ce94d0445708a1
[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 #endif
548 }
549
550 WidgetsPage *WidgetsFrame::CurrentPage()
551 {
552 #if USE_TREEBOOK
553 return wxStaticCast(m_book->GetCurrentPage(), WidgetsPage);
554 #else
555 WidgetsBookCtrl *book = wxStaticCast(m_book->GetCurrentPage(), WidgetsBookCtrl);
556 if (!book) return NULL;
557 return wxStaticCast(book->GetCurrentPage(), WidgetsPage);
558 #endif
559 }
560
561 WidgetsFrame::~WidgetsFrame()
562 {
563 #if USE_LOG
564 delete m_logTarget;
565 #endif // USE_LOG
566 }
567
568 // ----------------------------------------------------------------------------
569 // WidgetsFrame event handlers
570 // ----------------------------------------------------------------------------
571
572 void WidgetsFrame::OnExit(wxCommandEvent& WXUNUSED(event))
573 {
574 Close();
575 }
576
577 #if USE_LOG
578 void WidgetsFrame::OnButtonClearLog(wxCommandEvent& WXUNUSED(event))
579 {
580 m_lboxLog->Clear();
581 }
582 #endif // USE_LOG
583
584 #if wxUSE_MENUS
585
586 void WidgetsFrame::OnPageChanged(WidgetsBookCtrlEvent& event)
587 {
588 wxMenuItem *item = GetMenuBar()->FindItem(Widgets_GoToPage + event.GetSelection());
589 if (item) item->Check();
590 event.Skip();
591 }
592
593 void WidgetsFrame::OnGoToPage(wxCommandEvent& event)
594 {
595 #if USE_TREEBOOK
596 m_book->SetSelection(event.GetId() - Widgets_GoToPage);
597 #else
598 m_book->SetSelection(m_book->GetPageCount()-1);
599 WidgetsBookCtrl *book = wxStaticCast(m_book->GetCurrentPage(), WidgetsBookCtrl);
600 book->SetSelection(event.GetId() - Widgets_GoToPage);
601 #endif
602 }
603
604 #if wxUSE_TOOLTIPS
605
606 void WidgetsFrame::OnSetTooltip(wxCommandEvent& WXUNUSED(event))
607 {
608 static wxString s_tip = _T("This is a tooltip");
609
610 wxTextEntryDialog dialog
611 (
612 this,
613 _T("Tooltip text (may use \\n, leave empty to remove): "),
614 _T("Widgets sample"),
615 s_tip
616 );
617
618 if ( dialog.ShowModal() != wxID_OK )
619 return;
620
621 s_tip = dialog.GetValue();
622 s_tip.Replace(_T("\\n"), _T("\n"));
623
624 WidgetsPage *page = CurrentPage();
625 page->GetWidget()->SetToolTip(s_tip);
626
627 wxControl *ctrl2 = page->GetWidget2();
628 if ( ctrl2 )
629 ctrl2->SetToolTip(s_tip);
630 }
631
632 #endif // wxUSE_TOOLTIPS
633
634 void WidgetsFrame::OnSetFgCol(wxCommandEvent& WXUNUSED(event))
635 {
636 #if wxUSE_COLOURDLG
637 // allow for debugging the default colour the first time this is called
638 WidgetsPage *page = CurrentPage();
639 if (!m_colFg.Ok())
640 m_colFg = page->GetForegroundColour();
641
642 wxColour col = wxGetColourFromUser(this, m_colFg);
643 if ( !col.Ok() )
644 return;
645
646 m_colFg = col;
647
648 page->GetWidget()->SetForegroundColour(m_colFg);
649 page->GetWidget()->Refresh();
650
651 wxControl *ctrl2 = page->GetWidget2();
652 if ( ctrl2 )
653 {
654 ctrl2->SetForegroundColour(m_colFg);
655 ctrl2->Refresh();
656 }
657 #else
658 wxLogMessage(_T("Colour selection dialog not available in current build."));
659 #endif
660 }
661
662 void WidgetsFrame::OnSetBgCol(wxCommandEvent& WXUNUSED(event))
663 {
664 #if wxUSE_COLOURDLG
665 WidgetsPage *page = CurrentPage();
666 if ( !m_colBg.Ok() )
667 m_colBg = page->GetBackgroundColour();
668
669 wxColour col = wxGetColourFromUser(this, m_colBg);
670 if ( !col.Ok() )
671 return;
672
673 m_colBg = col;
674
675 page->GetWidget()->SetBackgroundColour(m_colBg);
676 page->GetWidget()->Refresh();
677
678 wxControl *ctrl2 = page->GetWidget2();
679 if ( ctrl2 )
680 {
681 ctrl2->SetBackgroundColour(m_colFg);
682 ctrl2->Refresh();
683 }
684 #else
685 wxLogMessage(_T("Colour selection dialog not available in current build."));
686 #endif
687 }
688
689 void WidgetsFrame::OnSetFont(wxCommandEvent& WXUNUSED(event))
690 {
691 #if wxUSE_FONTDLG
692 WidgetsPage *page = CurrentPage();
693 if (!m_font.Ok())
694 m_font = page->GetFont();
695
696 wxFont font = wxGetFontFromUser(this, m_font);
697 if ( !font.Ok() )
698 return;
699
700 m_font = font;
701
702 page->GetWidget()->SetFont(m_font);
703 page->GetWidget()->Refresh();
704
705 wxControl *ctrl2 = page->GetWidget2();
706 if ( ctrl2 )
707 {
708 ctrl2->SetFont(m_font);
709 ctrl2->Refresh();
710 }
711 #else
712 wxLogMessage(_T("Font selection dialog not available in current build."));
713 #endif
714 }
715
716 void WidgetsFrame::OnEnable(wxCommandEvent& event)
717 {
718 WidgetsPage *page = CurrentPage();
719 page->GetWidget()->Enable(event.IsChecked());
720 }
721
722 void WidgetsFrame::OnSetBorder(wxCommandEvent& event)
723 {
724 int border;
725 switch ( event.GetId() )
726 {
727 case Widgets_BorderNone: border = wxBORDER_NONE; break;
728 case Widgets_BorderStatic: border = wxBORDER_STATIC; break;
729 case Widgets_BorderSimple: border = wxBORDER_SIMPLE; break;
730 case Widgets_BorderRaised: border = wxBORDER_RAISED; break;
731 case Widgets_BorderSunken: border = wxBORDER_SUNKEN; break;
732 case Widgets_BorderDouble: border = wxBORDER_DOUBLE; break;
733
734 default:
735 wxFAIL_MSG( _T("unknown border style") );
736 // fall through
737
738 case Widgets_BorderDefault: border = wxBORDER_DEFAULT; break;
739 }
740
741 WidgetsPage::ms_defaultFlags &= ~wxBORDER_MASK;
742 WidgetsPage::ms_defaultFlags |= border;
743
744 WidgetsPage *page = CurrentPage();
745 page->RecreateWidget();
746 }
747
748 #endif // wxUSE_MENUS
749
750 // ----------------------------------------------------------------------------
751 // WidgetsPageInfo
752 // ----------------------------------------------------------------------------
753
754 WidgetsPageInfo::WidgetsPageInfo(Constructor ctor, const wxChar *label, int categories)
755 : m_label(label)
756 , m_categories(categories)
757 {
758 m_ctor = ctor;
759
760 m_next = NULL;
761
762 // dummy sorting: add and immediately sort in the list according to label
763 if ( WidgetsPage::ms_widgetPages )
764 {
765 WidgetsPageInfo *node_prev = WidgetsPage::ms_widgetPages;
766 if ( wxStrcmp(label, node_prev->GetLabel().c_str()) < 0 )
767 {
768 // add as first
769 m_next = node_prev;
770 WidgetsPage::ms_widgetPages = this;
771 }
772 else
773 {
774 WidgetsPageInfo *node_next;
775 do
776 {
777 node_next = node_prev->GetNext();
778 if ( node_next )
779 {
780 // add if between two
781 if ( wxStrcmp(label, node_next->GetLabel().c_str()) < 0 )
782 {
783 node_prev->SetNext(this);
784 m_next = node_next;
785 // force to break loop
786 node_next = NULL;
787 }
788 }
789 else
790 {
791 // add as last
792 node_prev->SetNext(this);
793 m_next = node_next;
794 }
795 node_prev = node_next;
796 }
797 while ( node_next );
798 }
799 }
800 else
801 {
802 // add when first
803 WidgetsPage::ms_widgetPages = this;
804 }
805 }
806
807 // ----------------------------------------------------------------------------
808 // WidgetsPage
809 // ----------------------------------------------------------------------------
810
811 int WidgetsPage::ms_defaultFlags = wxBORDER_DEFAULT;
812 WidgetsPageInfo *WidgetsPage::ms_widgetPages = NULL;
813
814 WidgetsPage::WidgetsPage(WidgetsBookCtrl *book)
815 : wxPanel(book, wxID_ANY,
816 wxDefaultPosition, wxDefaultSize,
817 wxNO_FULL_REPAINT_ON_RESIZE |
818 wxCLIP_CHILDREN |
819 wxTAB_TRAVERSAL)
820 {
821 }
822
823 wxSizer *WidgetsPage::CreateSizerWithText(wxControl *control,
824 wxWindowID id,
825 wxTextCtrl **ppText)
826 {
827 wxSizer *sizerRow = new wxBoxSizer(wxHORIZONTAL);
828 wxTextCtrl *text = new wxTextCtrl(this, id, wxEmptyString,
829 wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER);
830
831 sizerRow->Add(control, 0, wxRIGHT | wxALIGN_CENTRE_VERTICAL, 5);
832 sizerRow->Add(text, 1, wxLEFT | wxALIGN_CENTRE_VERTICAL, 5);
833
834 if ( ppText )
835 *ppText = text;
836
837 return sizerRow;
838 }
839
840 // create a sizer containing a label and a text ctrl
841 wxSizer *WidgetsPage::CreateSizerWithTextAndLabel(const wxString& label,
842 wxWindowID id,
843 wxTextCtrl **ppText)
844 {
845 return CreateSizerWithText(new wxStaticText(this, wxID_ANY, label),
846 id, ppText);
847 }
848
849 // create a sizer containing a button and a text ctrl
850 wxSizer *WidgetsPage::CreateSizerWithTextAndButton(wxWindowID idBtn,
851 const wxString& label,
852 wxWindowID id,
853 wxTextCtrl **ppText)
854 {
855 return CreateSizerWithText(new wxButton(this, idBtn, label), id, ppText);
856 }
857
858 wxCheckBox *WidgetsPage::CreateCheckBoxAndAddToSizer(wxSizer *sizer,
859 const wxString& label,
860 wxWindowID id)
861 {
862 wxCheckBox *checkbox = new wxCheckBox(this, id, label);
863 sizer->Add(checkbox, 0, wxLEFT | wxRIGHT, 5);
864 sizer->Add(0, 2, 0, wxGROW); // spacer
865
866 return checkbox;
867 }