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