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