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