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