]> git.saurik.com Git - wxWidgets.git/blame - samples/widgets/widgets.cpp
removed GTK header include
[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)
f2fdc4d5 273 EVT_WIDGETS_PAGE_CHANGED(wxID_ANY, WidgetsFrame::OnPageChanged)
3700e21b
VZ
274 EVT_MENU_RANGE(Widgets_GoToPage, Widgets_GoToPageLast,
275 WidgetsFrame::OnGoToPage)
276
195df7a7
VZ
277 EVT_MENU(Widgets_SetFgColour, WidgetsFrame::OnSetFgCol)
278 EVT_MENU(Widgets_SetBgColour, WidgetsFrame::OnSetBgCol)
822b9009 279 EVT_MENU(Widgets_SetFont, WidgetsFrame::OnSetFont)
a17c1df4 280 EVT_MENU(Widgets_Enable, WidgetsFrame::OnEnable)
1c01dd16 281
1301e228
VZ
282 EVT_MENU_RANGE(Widgets_BorderNone, Widgets_BorderDefault,
283 WidgetsFrame::OnSetBorder)
284
e32bcef6
VZ
285 EVT_MENU(Widgets_GlobalBusyCursor, WidgetsFrame::OnToggleGlobalBusyCursor)
286 EVT_MENU(Widgets_BusyCursor, WidgetsFrame::OnToggleBusyCursor)
287
1c01dd16 288 EVT_MENU(wxID_EXIT, WidgetsFrame::OnExit)
3700e21b 289#endif // wxUSE_MENUS
32b8ec41
VZ
290END_EVENT_TABLE()
291
292// ============================================================================
293// implementation
294// ============================================================================
295
296// ----------------------------------------------------------------------------
297// app class
298// ----------------------------------------------------------------------------
299
300bool WidgetsApp::OnInit()
301{
bf188f1a 302 if ( !wxApp::OnInit() )
206d3a16 303 return false;
1bdd1007 304
32b8ec41
VZ
305 // the reason for having these ifdef's is that I often run two copies of
306 // this sample side by side and it is useful to see which one is which
24e78d27 307 wxString title;
32b8ec41 308#if defined(__WXUNIVERSAL__)
085a1f3c 309 title = _T("wxUniv/");
24e78d27
VZ
310#endif
311
312#if defined(__WXMSW__)
313 title += _T("wxMSW");
32b8ec41 314#elif defined(__WXGTK__)
24e78d27 315 title += _T("wxGTK");
0f9390c3
GD
316#elif defined(__WXMAC__)
317 title += _T("wxMAC");
318#elif defined(__WXMOTIF__)
319 title += _T("wxMOTIF");
32b8ec41 320#else
be5a51fb 321 title += _T("wxWidgets");
32b8ec41 322#endif
32b8ec41
VZ
323
324 wxFrame *frame = new WidgetsFrame(title + _T(" widgets demo"));
325 frame->Show();
326
327 //wxLog::AddTraceMask(_T("listbox"));
328 //wxLog::AddTraceMask(_T("scrollbar"));
58ec2255 329 //wxLog::AddTraceMask(_T("focus"));
32b8ec41 330
206d3a16 331 return true;
32b8ec41
VZ
332}
333
334// ----------------------------------------------------------------------------
335// WidgetsFrame construction
336// ----------------------------------------------------------------------------
337
338WidgetsFrame::WidgetsFrame(const wxString& title)
1e3fc113 339 : wxFrame(NULL, wxID_ANY, title)
32b8ec41 340{
f2fdc4d5
WS
341 // set the frame icon
342 SetIcon(wxICON(sample));
343
32b8ec41 344 // init everything
49abcb2f 345#if USE_LOG
32b8ec41
VZ
346 m_lboxLog = (wxListBox *)NULL;
347 m_logTarget = (wxLog *)NULL;
49abcb2f 348#endif // USE_LOG
f2fdc4d5 349 m_book = (WidgetsBookCtrl *)NULL;
32b8ec41 350
195df7a7
VZ
351#if wxUSE_MENUS
352 // create the menubar
353 wxMenuBar *mbar = new wxMenuBar;
354 wxMenu *menuWidget = new wxMenu;
1c01dd16
VZ
355#if wxUSE_TOOLTIPS
356 menuWidget->Append(Widgets_SetTooltip, _T("Set &tooltip...\tCtrl-T"));
357 menuWidget->AppendSeparator();
358#endif // wxUSE_TOOLTIPS
195df7a7
VZ
359 menuWidget->Append(Widgets_SetFgColour, _T("Set &foreground...\tCtrl-F"));
360 menuWidget->Append(Widgets_SetBgColour, _T("Set &background...\tCtrl-B"));
822b9009 361 menuWidget->Append(Widgets_SetFont, _T("Set f&ont...\tCtrl-O"));
a17c1df4 362 menuWidget->AppendCheckItem(Widgets_Enable, _T("&Enable/disable\tCtrl-E"));
1301e228
VZ
363
364 wxMenu *menuBorders = new wxMenu;
365 menuBorders->AppendRadioItem(Widgets_BorderDefault, _T("De&fault\tCtrl-Shift-9"));
366 menuBorders->AppendRadioItem(Widgets_BorderNone, _T("&None\tCtrl-Shift-0"));
367 menuBorders->AppendRadioItem(Widgets_BorderSimple, _T("&Simple\tCtrl-Shift-1"));
368 menuBorders->AppendRadioItem(Widgets_BorderDouble, _T("&Double\tCtrl-Shift-2"));
369 menuBorders->AppendRadioItem(Widgets_BorderStatic, _T("Stati&c\tCtrl-Shift-3"));
370 menuBorders->AppendRadioItem(Widgets_BorderRaised, _T("&Raised\tCtrl-Shift-4"));
371 menuBorders->AppendRadioItem(Widgets_BorderSunken, _T("S&unken\tCtrl-Shift-5"));
372 menuWidget->AppendSubMenu(menuBorders, _T("Set &border"));
373
e32bcef6
VZ
374 menuWidget->AppendSeparator();
375 menuWidget->AppendCheckItem(Widgets_GlobalBusyCursor,
376 _T("Toggle &global busy cursor\tCtrl-Shift-U"));
377 menuWidget->AppendCheckItem(Widgets_BusyCursor,
378 _T("Toggle b&usy cursor\tCtrl-U"));
379
195df7a7
VZ
380 menuWidget->AppendSeparator();
381 menuWidget->Append(wxID_EXIT, _T("&Quit\tCtrl-Q"));
382 mbar->Append(menuWidget, _T("&Widget"));
383 SetMenuBar(mbar);
a17c1df4
VZ
384
385 mbar->Check(Widgets_Enable, true);
195df7a7
VZ
386#endif // wxUSE_MENUS
387
32b8ec41 388 // create controls
1e3fc113 389 m_panel = new wxPanel(this, wxID_ANY);
32b8ec41
VZ
390
391 wxSizer *sizerTop = new wxBoxSizer(wxVERTICAL);
392
25057aba 393 // we have 2 panes: book with pages demonstrating the controls in the
32b8ec41 394 // upper one and the log window with some buttons in the lower
1bdd1007 395
1e3fc113 396 int style = wxBK_DEFAULT;
25057aba 397 // Uncomment to suppress page theme (draw in solid colour)
8c9f8f91 398 //style |= wxNB_NOPAGETHEME;
32b8ec41 399
f2fdc4d5 400 m_book = new WidgetsBookCtrl(m_panel, Widgets_BookCtrl, wxDefaultPosition,
6bae6726 401#ifdef __WXMOTIF__
2ddb4d13 402 wxSize(500, wxDefaultCoord), // under Motif, height is a function of the width...
6bae6726
MB
403#else
404 wxDefaultSize,
405#endif
406 style);
61c083e7 407 InitBook();
32b8ec41 408
261357eb 409#ifndef __WXHANDHELD__
32b8ec41 410 // the lower one only has the log listbox and a button to clear it
49abcb2f 411#if USE_LOG
206d3a16
JS
412 wxSizer *sizerDown = new wxStaticBoxSizer(
413 new wxStaticBox( m_panel, wxID_ANY, _T("&Log window") ),
414 wxVERTICAL);
415
416 m_lboxLog = new wxListBox(m_panel, wxID_ANY);
32b8ec41 417 sizerDown->Add(m_lboxLog, 1, wxGROW | wxALL, 5);
7b127900 418 sizerDown->SetMinSize(100, 150);
b29903d4
WS
419#else
420 wxSizer *sizerDown = new wxBoxSizer(wxVERTICAL);
49abcb2f 421#endif // USE_LOG
88633ca7 422
32b8ec41 423 wxBoxSizer *sizerBtns = new wxBoxSizer(wxHORIZONTAL);
b29903d4 424 wxButton *btn;
49abcb2f 425#if USE_LOG
b29903d4 426 btn = new wxButton(m_panel, Widgets_ClearLog, _T("Clear &log"));
32b8ec41
VZ
427 sizerBtns->Add(btn);
428 sizerBtns->Add(10, 0); // spacer
49abcb2f 429#endif // USE_LOG
32b8ec41
VZ
430 btn = new wxButton(m_panel, Widgets_Quit, _T("E&xit"));
431 sizerBtns->Add(btn);
432 sizerDown->Add(sizerBtns, 0, wxALL | wxALIGN_RIGHT, 5);
433
434 // put everything together
61c083e7 435 sizerTop->Add(m_book, 1, wxGROW | (wxALL & ~(wxTOP | wxBOTTOM)), 10);
32b8ec41
VZ
436 sizerTop->Add(0, 5, 0, wxGROW); // spacer in between
437 sizerTop->Add(sizerDown, 0, wxGROW | (wxALL & ~wxTOP), 10);
438
261357eb 439#else // !__WXHANDHELD__/__WXHANDHELD__
49abcb2f
WS
440
441 sizerTop->Add(m_book, 1, wxGROW | wxALL );
442
261357eb 443#endif // __WXHANDHELD__
49abcb2f 444
32b8ec41
VZ
445 m_panel->SetSizer(sizerTop);
446
447 sizerTop->Fit(this);
448 sizerTop->SetSizeHints(this);
449
49abcb2f 450#if USE_LOG && !defined(__WXCOCOA__)
7bb70733 451 // wxCocoa's listbox is too flakey to use for logging right now
32b8ec41
VZ
452 // now that everything is created we can redirect the log messages to the
453 // listbox
454 m_logTarget = new LboxLogger(m_lboxLog, wxLog::GetActiveTarget());
455 wxLog::SetActiveTarget(m_logTarget);
b29903d4 456#endif
32b8ec41
VZ
457}
458
61c083e7 459void WidgetsFrame::InitBook()
32b8ec41 460{
261357eb 461#if USE_ICONS_IN_BOOK
f2fdc4d5 462 wxImageList *imageList = new wxImageList(32, 32);
32b8ec41 463
f2fdc4d5 464 imageList->Add(wxBitmap(sample_xpm));
261357eb
WS
465#else
466 wxImageList *imageList = NULL;
467#endif
f2fdc4d5
WS
468
469#if !USE_TREEBOOK
470 WidgetsBookCtrl *books[MAX_PAGES];
471#endif
472
473 ArrayWidgetsPage pages[MAX_PAGES];
474 wxArrayString labels[MAX_PAGES];
32b8ec41 475
3700e21b 476 wxMenu *menuPages = new wxMenu;
f2fdc4d5
WS
477 unsigned int nPage = 0, nFKey = 0;
478 int cat, imageId = 1;
3700e21b 479
61c083e7 480 // we need to first create all pages and only then add them to the book
32b8ec41 481 // as we need the image list first
3700e21b
VZ
482 //
483 // we also construct the pages menu during this first iteration
f2fdc4d5 484 for ( cat = 0; cat < MAX_PAGES; cat++ )
32b8ec41 485 {
f2fdc4d5
WS
486#if USE_TREEBOOK
487 nPage++; // increase for parent page
488#else
261357eb
WS
489 books[cat] = new WidgetsBookCtrl(m_book,
490 wxID_ANY,
491 wxDefaultPosition,
492 wxDefaultSize,
895cc205 493 wxBK_DEFAULT);
f2fdc4d5
WS
494#endif
495
496 for ( WidgetsPageInfo *info = WidgetsPage::ms_widgetPages;
497 info;
498 info = info->GetNext() )
499 {
500 if( (info->GetCategories() & ( 1 << cat )) == 0)
501 continue;
502
503 WidgetsPage *page = (*info->GetCtor())(
504#if USE_TREEBOOK
505 m_book
506#else
507 books[cat]
508#endif
509 , imageList);
510 pages[cat].Add(page);
511
512 labels[cat].Add(info->GetLabel());
513 if ( cat == ALL_PAGE )
514 {
515 wxString radioLabel(info->GetLabel());
516 nFKey++;
517 if ( nFKey <= 12 )
518 {
519 radioLabel << wxT("\tF" ) << nFKey;
520 }
521
522 menuPages->AppendRadioItem(
523 Widgets_GoToPage + nPage,
524 radioLabel
525 );
526#if !USE_TREEBOOK
527 // consider only for book in book architecture
528 nPage++;
529#endif
530 }
531
532#if USE_TREEBOOK
533 // consider only for treebook architecture (with subpages)
534 nPage++;
535#endif
536 }
32b8ec41
VZ
537 }
538
3700e21b
VZ
539 GetMenuBar()->Append(menuPages, _T("&Page"));
540
261357eb 541#if USE_ICONS_IN_BOOK
f2fdc4d5 542 m_book->AssignImageList(imageList);
261357eb 543#endif
32b8ec41 544
f2fdc4d5 545 for ( cat = 0; cat < MAX_PAGES; cat++ )
32b8ec41 546 {
f2fdc4d5
WS
547#if USE_TREEBOOK
548 m_book->AddPage(NULL,WidgetsCategories[cat],false,0);
549#else
550 m_book->AddPage(books[cat],WidgetsCategories[cat],false,0);
261357eb 551#if USE_ICONS_IN_BOOK
f2fdc4d5 552 books[cat]->SetImageList(imageList);
261357eb 553#endif
f2fdc4d5
WS
554#endif
555
556 // now do add them
557 size_t count = pages[cat].GetCount();
558 for ( size_t n = 0; n < count; n++ )
559 {
560#if USE_TREEBOOK
3e859739 561 m_book->AddSubPage
f2fdc4d5 562#else
3e859739 563 books[cat]->AddPage
f2fdc4d5 564#endif
3e859739 565 (
f2fdc4d5
WS
566 pages[cat][n],
567 labels[cat][n],
568 false, // don't select
569 imageId++
570 );
571 }
32b8ec41 572 }
f2fdc4d5
WS
573
574#if USE_TREEBOOK
3e859739
VZ
575 // for treebook page #0 is empty parent page only so select the first page
576 // with some contents
f2fdc4d5 577 m_book->SetSelection(1);
3e859739
VZ
578
579 // but ensure that the top of the tree is shown nevertheless
580 wxTreeCtrl * const tree = m_book->GetTreeCtrl();
23c2b62b
VZ
581
582 wxTreeItemIdValue cookie;
583 tree->EnsureVisible(tree->GetFirstChild(tree->GetRootItem(), cookie));
3e859739 584#endif // USE_TREEBOOK
f2fdc4d5
WS
585}
586
587WidgetsPage *WidgetsFrame::CurrentPage()
588{
453535a7 589 wxWindow *page = m_book->GetCurrentPage();
453535a7 590
3e859739 591#if !USE_TREEBOOK
453535a7 592 WidgetsBookCtrl *subBook = wxStaticCast(page, WidgetsBookCtrl);
3e859739
VZ
593 wxCHECK_MSG( subBook, NULL, _T("no WidgetsBookCtrl?") );
594
453535a7 595 page = subBook->GetCurrentPage();
3e859739
VZ
596#endif // !USE_TREEBOOK
597
453535a7 598 return wxStaticCast(page, WidgetsPage);
32b8ec41
VZ
599}
600
601WidgetsFrame::~WidgetsFrame()
602{
49abcb2f 603#if USE_LOG
32b8ec41 604 delete m_logTarget;
49abcb2f 605#endif // USE_LOG
32b8ec41
VZ
606}
607
608// ----------------------------------------------------------------------------
609// WidgetsFrame event handlers
610// ----------------------------------------------------------------------------
611
195df7a7 612void WidgetsFrame::OnExit(wxCommandEvent& WXUNUSED(event))
32b8ec41
VZ
613{
614 Close();
615}
616
49abcb2f 617#if USE_LOG
c02e5a31 618void WidgetsFrame::OnButtonClearLog(wxCommandEvent& WXUNUSED(event))
32b8ec41
VZ
619{
620 m_lboxLog->Clear();
621}
49abcb2f 622#endif // USE_LOG
32b8ec41 623
195df7a7
VZ
624#if wxUSE_MENUS
625
3e859739
VZ
626void WidgetsFrame::OnPageChanging(WidgetsBookCtrlEvent& event)
627{
628 if ( !m_book->GetPage(event.GetSelection()) )
629 event.Veto();
630}
631
f2fdc4d5 632void WidgetsFrame::OnPageChanged(WidgetsBookCtrlEvent& event)
3700e21b 633{
3e859739
VZ
634 const int sel = event.GetSelection();
635
453535a7 636 // adjust "Page" menu selection
3e859739
VZ
637 wxMenuItem *item = GetMenuBar()->FindItem(Widgets_GoToPage + sel);
638 if ( item )
639 item->Check();
453535a7 640
fe24a945
VZ
641 GetMenuBar()->Check(Widgets_BusyCursor, false);
642
453535a7 643 // lazy creation of the pages
3e859739
VZ
644 WidgetsPage *page = CurrentPage();
645 if ( page->GetChildren().empty() )
453535a7
WS
646 {
647 wxWindowUpdateLocker noUpdates(page);
648 page->CreateContent();
649 WidgetsBookCtrl *book = wxStaticCast(page->GetParent(), WidgetsBookCtrl);
650 wxSize size;
651 for ( size_t i = 0; i < book->GetPageCount(); ++i )
652 {
653 wxWindow *page = book->GetPage(i);
3e859739 654 if ( page )
453535a7
WS
655 {
656 size.IncTo(page->GetSize());
657 }
658 }
659 page->SetSize(size);
660 }
661
3700e21b
VZ
662 event.Skip();
663}
664
665void WidgetsFrame::OnGoToPage(wxCommandEvent& event)
666{
f2fdc4d5 667#if USE_TREEBOOK
3700e21b 668 m_book->SetSelection(event.GetId() - Widgets_GoToPage);
f2fdc4d5
WS
669#else
670 m_book->SetSelection(m_book->GetPageCount()-1);
671 WidgetsBookCtrl *book = wxStaticCast(m_book->GetCurrentPage(), WidgetsBookCtrl);
672 book->SetSelection(event.GetId() - Widgets_GoToPage);
673#endif
3700e21b
VZ
674}
675
1c01dd16
VZ
676#if wxUSE_TOOLTIPS
677
678void WidgetsFrame::OnSetTooltip(wxCommandEvent& WXUNUSED(event))
679{
680 static wxString s_tip = _T("This is a tooltip");
681
60c84f85
VZ
682 wxTextEntryDialog dialog
683 (
684 this,
685 _T("Tooltip text (may use \\n, leave empty to remove): "),
686 _T("Widgets sample"),
687 s_tip
688 );
689
690 if ( dialog.ShowModal() != wxID_OK )
1c01dd16
VZ
691 return;
692
60c84f85 693 s_tip = dialog.GetValue();
0c113874 694 s_tip.Replace(_T("\\n"), _T("\n"));
bd018e7e 695
3e859739
VZ
696 WidgetsPage *page = CurrentPage();
697
60c84f85 698 page->GetWidget()->SetToolTip(s_tip);
1c01dd16
VZ
699
700 wxControl *ctrl2 = page->GetWidget2();
701 if ( ctrl2 )
60c84f85 702 ctrl2->SetToolTip(s_tip);
1c01dd16
VZ
703}
704
705#endif // wxUSE_TOOLTIPS
706
195df7a7
VZ
707void WidgetsFrame::OnSetFgCol(wxCommandEvent& WXUNUSED(event))
708{
923f4e79 709#if wxUSE_COLOURDLG
822b9009 710 // allow for debugging the default colour the first time this is called
f2fdc4d5 711 WidgetsPage *page = CurrentPage();
63da71ba 712
822b9009
VZ
713 if (!m_colFg.Ok())
714 m_colFg = page->GetForegroundColour();
715
195df7a7
VZ
716 wxColour col = wxGetColourFromUser(this, m_colFg);
717 if ( !col.Ok() )
718 return;
719
720 m_colFg = col;
721
195df7a7 722 page->GetWidget()->SetForegroundColour(m_colFg);
750972ab 723 page->GetWidget()->Refresh();
1c01dd16
VZ
724
725 wxControl *ctrl2 = page->GetWidget2();
726 if ( ctrl2 )
727 {
728 ctrl2->SetForegroundColour(m_colFg);
729 ctrl2->Refresh();
730 }
923f4e79 731#else
1c01dd16 732 wxLogMessage(_T("Colour selection dialog not available in current build."));
923f4e79 733#endif
195df7a7
VZ
734}
735
736void WidgetsFrame::OnSetBgCol(wxCommandEvent& WXUNUSED(event))
737{
923f4e79 738#if wxUSE_COLOURDLG
f2fdc4d5 739 WidgetsPage *page = CurrentPage();
63da71ba 740
822b9009
VZ
741 if ( !m_colBg.Ok() )
742 m_colBg = page->GetBackgroundColour();
743
195df7a7
VZ
744 wxColour col = wxGetColourFromUser(this, m_colBg);
745 if ( !col.Ok() )
746 return;
747
748 m_colBg = col;
749
195df7a7 750 page->GetWidget()->SetBackgroundColour(m_colBg);
750972ab 751 page->GetWidget()->Refresh();
1c01dd16
VZ
752
753 wxControl *ctrl2 = page->GetWidget2();
754 if ( ctrl2 )
755 {
756 ctrl2->SetBackgroundColour(m_colFg);
757 ctrl2->Refresh();
758 }
923f4e79 759#else
1c01dd16 760 wxLogMessage(_T("Colour selection dialog not available in current build."));
923f4e79 761#endif
195df7a7
VZ
762}
763
822b9009
VZ
764void WidgetsFrame::OnSetFont(wxCommandEvent& WXUNUSED(event))
765{
766#if wxUSE_FONTDLG
f2fdc4d5 767 WidgetsPage *page = CurrentPage();
63da71ba 768
822b9009
VZ
769 if (!m_font.Ok())
770 m_font = page->GetFont();
771
772 wxFont font = wxGetFontFromUser(this, m_font);
773 if ( !font.Ok() )
774 return;
775
776 m_font = font;
777
778 page->GetWidget()->SetFont(m_font);
779 page->GetWidget()->Refresh();
780
781 wxControl *ctrl2 = page->GetWidget2();
782 if ( ctrl2 )
783 {
784 ctrl2->SetFont(m_font);
785 ctrl2->Refresh();
786 }
787#else
788 wxLogMessage(_T("Font selection dialog not available in current build."));
789#endif
790}
791
a17c1df4
VZ
792void WidgetsFrame::OnEnable(wxCommandEvent& event)
793{
3e859739 794 CurrentPage()->GetWidget()->Enable(event.IsChecked());
a17c1df4
VZ
795}
796
1301e228
VZ
797void WidgetsFrame::OnSetBorder(wxCommandEvent& event)
798{
799 int border;
800 switch ( event.GetId() )
801 {
802 case Widgets_BorderNone: border = wxBORDER_NONE; break;
803 case Widgets_BorderStatic: border = wxBORDER_STATIC; break;
804 case Widgets_BorderSimple: border = wxBORDER_SIMPLE; break;
805 case Widgets_BorderRaised: border = wxBORDER_RAISED; break;
806 case Widgets_BorderSunken: border = wxBORDER_SUNKEN; break;
807 case Widgets_BorderDouble: border = wxBORDER_DOUBLE; break;
808
809 default:
810 wxFAIL_MSG( _T("unknown border style") );
811 // fall through
812
813 case Widgets_BorderDefault: border = wxBORDER_DEFAULT; break;
814 }
815
816 WidgetsPage::ms_defaultFlags &= ~wxBORDER_MASK;
817 WidgetsPage::ms_defaultFlags |= border;
818
f2fdc4d5 819 WidgetsPage *page = CurrentPage();
63da71ba 820
1301e228
VZ
821 page->RecreateWidget();
822}
823
e32bcef6
VZ
824void WidgetsFrame::OnToggleGlobalBusyCursor(wxCommandEvent& event)
825{
826 if ( event.IsChecked() )
827 wxBeginBusyCursor();
828 else
829 wxEndBusyCursor();
830}
831
832void WidgetsFrame::OnToggleBusyCursor(wxCommandEvent& event)
833{
3e859739
VZ
834 CurrentPage()->GetWidget()->SetCursor(*(event.IsChecked()
835 ? wxHOURGLASS_CURSOR
836 : wxSTANDARD_CURSOR));
e32bcef6
VZ
837}
838
195df7a7
VZ
839#endif // wxUSE_MENUS
840
32b8ec41
VZ
841// ----------------------------------------------------------------------------
842// WidgetsPageInfo
843// ----------------------------------------------------------------------------
844
f2fdc4d5 845WidgetsPageInfo::WidgetsPageInfo(Constructor ctor, const wxChar *label, int categories)
32b8ec41 846 : m_label(label)
f2fdc4d5 847 , m_categories(categories)
32b8ec41
VZ
848{
849 m_ctor = ctor;
850
2673bcb0
DS
851 m_next = NULL;
852
fdfe568d 853 // dummy sorting: add and immediately sort in the list according to label
3700e21b 854 if ( WidgetsPage::ms_widgetPages )
2673bcb0
DS
855 {
856 WidgetsPageInfo *node_prev = WidgetsPage::ms_widgetPages;
fdfe568d 857 if ( wxStrcmp(label, node_prev->GetLabel().c_str()) < 0 )
2673bcb0
DS
858 {
859 // add as first
860 m_next = node_prev;
861 WidgetsPage::ms_widgetPages = this;
862 }
863 else
864 {
865 WidgetsPageInfo *node_next;
866 do
867 {
868 node_next = node_prev->GetNext();
fdfe568d 869 if ( node_next )
2673bcb0
DS
870 {
871 // add if between two
fdfe568d 872 if ( wxStrcmp(label, node_next->GetLabel().c_str()) < 0 )
2673bcb0
DS
873 {
874 node_prev->SetNext(this);
875 m_next = node_next;
876 // force to break loop
877 node_next = NULL;
878 }
879 }
880 else
881 {
882 // add as last
883 node_prev->SetNext(this);
884 m_next = node_next;
885 }
886 node_prev = node_next;
fdfe568d
VZ
887 }
888 while ( node_next );
2673bcb0
DS
889 }
890 }
891 else
892 {
893 // add when first
2673bcb0 894 WidgetsPage::ms_widgetPages = this;
2673bcb0 895 }
32b8ec41
VZ
896}
897
898// ----------------------------------------------------------------------------
899// WidgetsPage
900// ----------------------------------------------------------------------------
901
1301e228
VZ
902int WidgetsPage::ms_defaultFlags = wxBORDER_DEFAULT;
903WidgetsPageInfo *WidgetsPage::ms_widgetPages = NULL;
904
261357eb
WS
905WidgetsPage::WidgetsPage(WidgetsBookCtrl *book,
906 wxImageList *imaglist,
907 char* icon[])
61c083e7 908 : wxPanel(book, wxID_ANY,
df0787b6
VZ
909 wxDefaultPosition, wxDefaultSize,
910 wxNO_FULL_REPAINT_ON_RESIZE |
911 wxCLIP_CHILDREN |
912 wxTAB_TRAVERSAL)
32b8ec41 913{
261357eb
WS
914#if USE_ICONS_IN_BOOK
915 imaglist->Add(wxBitmap(icon));
916#else
917 wxUnusedVar(imaglist);
918 wxUnusedVar(icon);
919#endif
32b8ec41
VZ
920}
921
922wxSizer *WidgetsPage::CreateSizerWithText(wxControl *control,
923 wxWindowID id,
924 wxTextCtrl **ppText)
925{
926 wxSizer *sizerRow = new wxBoxSizer(wxHORIZONTAL);
206d3a16
JS
927 wxTextCtrl *text = new wxTextCtrl(this, id, wxEmptyString,
928 wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER);
4589ec39 929
32b8ec41
VZ
930 sizerRow->Add(control, 0, wxRIGHT | wxALIGN_CENTRE_VERTICAL, 5);
931 sizerRow->Add(text, 1, wxLEFT | wxALIGN_CENTRE_VERTICAL, 5);
932
933 if ( ppText )
934 *ppText = text;
935
936 return sizerRow;
937}
938
939// create a sizer containing a label and a text ctrl
940wxSizer *WidgetsPage::CreateSizerWithTextAndLabel(const wxString& label,
941 wxWindowID id,
942 wxTextCtrl **ppText)
943{
206d3a16
JS
944 return CreateSizerWithText(new wxStaticText(this, wxID_ANY, label),
945 id, ppText);
32b8ec41
VZ
946}
947
948// create a sizer containing a button and a text ctrl
949wxSizer *WidgetsPage::CreateSizerWithTextAndButton(wxWindowID idBtn,
950 const wxString& label,
951 wxWindowID id,
952 wxTextCtrl **ppText)
953{
954 return CreateSizerWithText(new wxButton(this, idBtn, label), id, ppText);
955}
956
957wxCheckBox *WidgetsPage::CreateCheckBoxAndAddToSizer(wxSizer *sizer,
958 const wxString& label,
959 wxWindowID id)
960{
961 wxCheckBox *checkbox = new wxCheckBox(this, id, label);
962 sizer->Add(checkbox, 0, wxLEFT | wxRIGHT, 5);
963 sizer->Add(0, 2, 0, wxGROW); // spacer
964
965 return checkbox;
966}