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