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