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