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