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