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