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