]> git.saurik.com Git - wxWidgets.git/blob - samples/widgets/widgets.cpp
refresh widget after changing its colour
[wxWidgets.git] / samples / widgets / widgets.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
3 // Name: widgets.cpp
4 // Purpose: Sample showing most of the simple wxWidgets widgets
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"
31 #include "wx/frame.h"
32 #include "wx/button.h"
33 #include "wx/checkbox.h"
34 #include "wx/listbox.h"
35 #include "wx/statbox.h"
36 #include "wx/stattext.h"
37 #include "wx/textctrl.h"
38 #endif
39
40 #include "wx/notebook.h"
41 #include "wx/sizer.h"
42 #include "wx/colordlg.h"
43
44 #include "widgets.h"
45
46 // ----------------------------------------------------------------------------
47 // constants
48 // ----------------------------------------------------------------------------
49
50 // control ids
51 enum
52 {
53 Widgets_ClearLog = 100,
54 Widgets_Quit,
55 Widgets_SetFgColour,
56 Widgets_SetBgColour
57 };
58
59 // ----------------------------------------------------------------------------
60 // our classes
61 // ----------------------------------------------------------------------------
62
63 // Define a new application type, each program should derive a class from wxApp
64 class WidgetsApp : public wxApp
65 {
66 public:
67 // override base class virtuals
68 // ----------------------------
69
70 // this one is called on application startup and is a good place for the app
71 // initialization (doing it here and not in the ctor allows to have an error
72 // return: if OnInit() returns false, the application terminates)
73 virtual bool OnInit();
74 };
75
76 // Define a new frame type: this is going to be our main frame
77 class WidgetsFrame : public wxFrame
78 {
79 public:
80 // ctor(s) and dtor
81 WidgetsFrame(const wxString& title);
82 virtual ~WidgetsFrame();
83
84 protected:
85 // event handlers
86 #if wxUSE_LOG
87 void OnButtonClearLog(wxCommandEvent& event);
88 #endif // wxUSE_LOG
89 void OnExit(wxCommandEvent& event);
90 #if wxUSE_MENUS
91 void OnSetFgCol(wxCommandEvent& event);
92 void OnSetBgCol(wxCommandEvent& event);
93 #endif // wxUSE_MENUS
94
95 // initialize the notebook: add all pages to it
96 void InitNotebook();
97
98 private:
99 // the panel containing everything
100 wxPanel *m_panel;
101
102 #if wxUSE_LOG
103 // the listbox for logging messages
104 wxListBox *m_lboxLog;
105
106 // the log target we use to redirect messages to the listbox
107 wxLog *m_logTarget;
108 #endif // wxUSE_LOG
109
110 // the notebook containing the test pages
111 wxNotebook *m_notebook;
112
113 // and the image list for it
114 wxImageList *m_imaglist;
115
116 #if wxUSE_MENUS
117 // last chosen fg/bg colours
118 wxColour m_colFg,
119 m_colBg;
120 #endif // wxUSE_MENUS
121
122 // any class wishing to process wxWidgets events must use this macro
123 DECLARE_EVENT_TABLE()
124 };
125
126 #if wxUSE_LOG
127 // A log target which just redirects the messages to a listbox
128 class LboxLogger : public wxLog
129 {
130 public:
131 LboxLogger(wxListBox *lbox, wxLog *logOld)
132 {
133 m_lbox = lbox;
134 //m_lbox->Disable(); -- looks ugly under MSW
135 m_logOld = logOld;
136 }
137
138 virtual ~LboxLogger()
139 {
140 wxLog::SetActiveTarget(m_logOld);
141 }
142
143 private:
144 // implement sink functions
145 virtual void DoLog(wxLogLevel level, const wxChar *szString, time_t t)
146 {
147 // don't put trace messages into listbox or we can get into infinite
148 // recursion
149 if ( level == wxLOG_Trace )
150 {
151 if ( m_logOld )
152 {
153 // cast is needed to call protected method
154 ((LboxLogger *)m_logOld)->DoLog(level, szString, t);
155 }
156 }
157 else
158 {
159 wxLog::DoLog(level, szString, t);
160 }
161 }
162
163 virtual void DoLogString(const wxChar *szString, time_t WXUNUSED(t))
164 {
165 wxString msg;
166 TimeStamp(&msg);
167 msg += szString;
168
169 #ifdef __WXUNIVERSAL__
170 m_lbox->AppendAndEnsureVisible(msg);
171 #else // other ports don't have this method yet
172 m_lbox->Append(msg);
173 m_lbox->SetFirstItem(m_lbox->GetCount() - 1);
174 #endif
175 }
176
177 // the control we use
178 wxListBox *m_lbox;
179
180 // the old log target
181 wxLog *m_logOld;
182 };
183 #endif // wxUSE_LOG
184
185 // array of pages
186 WX_DEFINE_ARRAY_PTR(WidgetsPage *, ArrayWidgetsPage);
187
188 // ----------------------------------------------------------------------------
189 // misc macros
190 // ----------------------------------------------------------------------------
191
192 IMPLEMENT_APP(WidgetsApp)
193
194 // ----------------------------------------------------------------------------
195 // event tables
196 // ----------------------------------------------------------------------------
197
198 BEGIN_EVENT_TABLE(WidgetsFrame, wxFrame)
199 #if wxUSE_LOG
200 EVT_BUTTON(Widgets_ClearLog, WidgetsFrame::OnButtonClearLog)
201 #endif // wxUSE_LOG
202 EVT_BUTTON(Widgets_Quit, WidgetsFrame::OnExit)
203
204 EVT_MENU(wxID_EXIT, WidgetsFrame::OnExit)
205 EVT_MENU(Widgets_SetFgColour, WidgetsFrame::OnSetFgCol)
206 EVT_MENU(Widgets_SetBgColour, WidgetsFrame::OnSetBgCol)
207 END_EVENT_TABLE()
208
209 // ============================================================================
210 // implementation
211 // ============================================================================
212
213 // ----------------------------------------------------------------------------
214 // app class
215 // ----------------------------------------------------------------------------
216
217 bool WidgetsApp::OnInit()
218 {
219 if ( !wxApp::OnInit() )
220 return false;
221
222 // the reason for having these ifdef's is that I often run two copies of
223 // this sample side by side and it is useful to see which one is which
224 wxString title;
225 #if defined(__WXUNIVERSAL__)
226 title = _T("wxUniv/");
227 #endif
228
229 #if defined(__WXMSW__)
230 title += _T("wxMSW");
231 #elif defined(__WXGTK__)
232 title += _T("wxGTK");
233 #elif defined(__WXMAC__)
234 title += _T("wxMAC");
235 #elif defined(__WXMOTIF__)
236 title += _T("wxMOTIF");
237 #else
238 title += _T("wxWidgets");
239 #endif
240
241 wxFrame *frame = new WidgetsFrame(title + _T(" widgets demo"));
242 frame->Show();
243
244 //wxLog::AddTraceMask(_T("listbox"));
245 //wxLog::AddTraceMask(_T("scrollbar"));
246 //wxLog::AddTraceMask(_T("focus"));
247
248 return true;
249 }
250
251 // ----------------------------------------------------------------------------
252 // WidgetsFrame construction
253 // ----------------------------------------------------------------------------
254
255 WidgetsFrame::WidgetsFrame(const wxString& title)
256 : wxFrame(NULL, wxID_ANY, title,
257 wxPoint(0, 50), wxDefaultSize,
258 wxDEFAULT_FRAME_STYLE |
259 wxNO_FULL_REPAINT_ON_RESIZE |
260 wxCLIP_CHILDREN |
261 wxTAB_TRAVERSAL)
262 {
263 // init everything
264 #if wxUSE_LOG
265 m_lboxLog = (wxListBox *)NULL;
266 m_logTarget = (wxLog *)NULL;
267 #endif // wxUSE_LOG
268 m_notebook = (wxNotebook *)NULL;
269 m_imaglist = (wxImageList *)NULL;
270
271 #if wxUSE_MENUS
272 // create the menubar
273 wxMenuBar *mbar = new wxMenuBar;
274 wxMenu *menuWidget = new wxMenu;
275 menuWidget->Append(Widgets_SetFgColour, _T("Set &foreground...\tCtrl-F"));
276 menuWidget->Append(Widgets_SetBgColour, _T("Set &background...\tCtrl-B"));
277 menuWidget->AppendSeparator();
278 menuWidget->Append(wxID_EXIT, _T("&Quit\tCtrl-Q"));
279 mbar->Append(menuWidget, _T("&Widget"));
280 SetMenuBar(mbar);
281 #endif // wxUSE_MENUS
282
283 // create controls
284 m_panel = new wxPanel(this, wxID_ANY,
285 wxDefaultPosition, wxDefaultSize, wxCLIP_CHILDREN);
286
287 wxSizer *sizerTop = new wxBoxSizer(wxVERTICAL);
288
289 // we have 2 panes: notebook which pages demonstrating the controls in the
290 // upper one and the log window with some buttons in the lower
291
292 m_notebook = new wxNotebook(m_panel, wxID_ANY, wxDefaultPosition,
293 wxDefaultSize, wxNO_FULL_REPAINT_ON_RESIZE|wxCLIP_CHILDREN);
294 InitNotebook();
295
296 // the lower one only has the log listbox and a button to clear it
297 #if wxUSE_LOG
298 wxSizer *sizerDown = new wxStaticBoxSizer(
299 new wxStaticBox( m_panel, wxID_ANY, _T("&Log window") ),
300 wxVERTICAL);
301
302 m_lboxLog = new wxListBox(m_panel, wxID_ANY);
303 sizerDown->Add(m_lboxLog, 1, wxGROW | wxALL, 5);
304 sizerDown->SetMinSize(100, 150);
305 #else
306 wxSizer *sizerDown = new wxBoxSizer(wxVERTICAL);
307 #endif // wxUSE_LOG
308
309 wxBoxSizer *sizerBtns = new wxBoxSizer(wxHORIZONTAL);
310 wxButton *btn;
311 #if wxUSE_LOG
312 btn = new wxButton(m_panel, Widgets_ClearLog, _T("Clear &log"));
313 sizerBtns->Add(btn);
314 sizerBtns->Add(10, 0); // spacer
315 #endif // wxUSE_LOG
316 btn = new wxButton(m_panel, Widgets_Quit, _T("E&xit"));
317 sizerBtns->Add(btn);
318 sizerDown->Add(sizerBtns, 0, wxALL | wxALIGN_RIGHT, 5);
319
320 // put everything together
321 sizerTop->Add(m_notebook, 1, wxGROW | (wxALL & ~(wxTOP | wxBOTTOM)), 10);
322 sizerTop->Add(0, 5, 0, wxGROW); // spacer in between
323 sizerTop->Add(sizerDown, 0, wxGROW | (wxALL & ~wxTOP), 10);
324
325 m_panel->SetSizer(sizerTop);
326
327 sizerTop->Fit(this);
328 sizerTop->SetSizeHints(this);
329
330 #if wxUSE_LOG && !defined(__WXCOCOA__)
331 // wxCocoa's listbox is too flakey to use for logging right now
332 // now that everything is created we can redirect the log messages to the
333 // listbox
334 m_logTarget = new LboxLogger(m_lboxLog, wxLog::GetActiveTarget());
335 wxLog::SetActiveTarget(m_logTarget);
336 #endif
337 }
338
339 void WidgetsFrame::InitNotebook()
340 {
341 m_imaglist = new wxImageList(32, 32);
342
343 ArrayWidgetsPage pages;
344 wxArrayString labels;
345
346 // we need to first create all pages and only then add them to the notebook
347 // as we need the image list first
348 WidgetsPageInfo *info = WidgetsPage::ms_widgetPages;
349 while ( info )
350 {
351 WidgetsPage *page = (*info->GetCtor())(m_notebook, m_imaglist);
352 pages.Add(page);
353
354 labels.Add(info->GetLabel());
355
356 info = info->GetNext();
357 }
358
359 m_notebook->SetImageList(m_imaglist);
360
361 // now do add them
362 size_t count = pages.GetCount();
363 for ( size_t n = 0; n < count; n++ )
364 {
365 m_notebook->AddPage(
366 pages[n],
367 labels[n],
368 false, // don't select
369 n // image id
370 );
371 }
372 }
373
374 WidgetsFrame::~WidgetsFrame()
375 {
376 #if wxUSE_LOG
377 delete m_logTarget;
378 #endif // wxUSE_LOG
379 delete m_imaglist;
380 }
381
382 // ----------------------------------------------------------------------------
383 // WidgetsFrame event handlers
384 // ----------------------------------------------------------------------------
385
386 void WidgetsFrame::OnExit(wxCommandEvent& WXUNUSED(event))
387 {
388 Close();
389 }
390
391 #if wxUSE_LOG
392 void WidgetsFrame::OnButtonClearLog(wxCommandEvent& WXUNUSED(event))
393 {
394 m_lboxLog->Clear();
395 }
396 #endif // wxUSE_LOG
397
398 #if wxUSE_MENUS
399
400 void WidgetsFrame::OnSetFgCol(wxCommandEvent& WXUNUSED(event))
401 {
402 wxColour col = wxGetColourFromUser(this, m_colFg);
403 if ( !col.Ok() )
404 return;
405
406 m_colFg = col;
407
408 WidgetsPage *page = wxStaticCast(m_notebook->GetCurrentPage(), WidgetsPage);
409 page->GetWidget()->SetForegroundColour(m_colFg);
410 page->GetWidget()->Refresh();
411 }
412
413 void WidgetsFrame::OnSetBgCol(wxCommandEvent& WXUNUSED(event))
414 {
415 wxColour col = wxGetColourFromUser(this, m_colBg);
416 if ( !col.Ok() )
417 return;
418
419 m_colBg = col;
420
421 WidgetsPage *page = wxStaticCast(m_notebook->GetCurrentPage(), WidgetsPage);
422 page->GetWidget()->SetBackgroundColour(m_colBg);
423 page->GetWidget()->Refresh();
424 }
425
426 #endif // wxUSE_MENUS
427
428 // ----------------------------------------------------------------------------
429 // WidgetsPageInfo
430 // ----------------------------------------------------------------------------
431
432 WidgetsPageInfo *WidgetsPage::ms_widgetPages = NULL;
433
434 WidgetsPageInfo::WidgetsPageInfo(Constructor ctor, const wxChar *label)
435 : m_label(label)
436 {
437 m_ctor = ctor;
438
439 m_next = NULL;
440
441 // dummy sorting: add and immediately sort on list according to label
442
443 if(WidgetsPage::ms_widgetPages)
444 {
445 WidgetsPageInfo *node_prev = WidgetsPage::ms_widgetPages;
446 if(wxStrcmp(label,node_prev->GetLabel().c_str())<0)
447 {
448 // add as first
449 m_next = node_prev;
450 WidgetsPage::ms_widgetPages = this;
451 }
452 else
453 {
454 WidgetsPageInfo *node_next;
455 do
456 {
457 node_next = node_prev->GetNext();
458 if(node_next)
459 {
460 // add if between two
461 if(wxStrcmp(label,node_next->GetLabel().c_str())<0)
462 {
463 node_prev->SetNext(this);
464 m_next = node_next;
465 // force to break loop
466 node_next = NULL;
467 }
468 }
469 else
470 {
471 // add as last
472 node_prev->SetNext(this);
473 m_next = node_next;
474 }
475 node_prev = node_next;
476 }while(node_next);
477 }
478 }
479 else
480 {
481 // add when first
482
483 WidgetsPage::ms_widgetPages = this;
484
485 }
486
487 }
488
489 // ----------------------------------------------------------------------------
490 // WidgetsPage
491 // ----------------------------------------------------------------------------
492
493 WidgetsPage::WidgetsPage(wxNotebook *notebook)
494 : wxPanel(notebook, wxID_ANY,
495 wxDefaultPosition, wxDefaultSize,
496 wxNO_FULL_REPAINT_ON_RESIZE |
497 wxCLIP_CHILDREN |
498 wxTAB_TRAVERSAL)
499 {
500 }
501
502 wxSizer *WidgetsPage::CreateSizerWithText(wxControl *control,
503 wxWindowID id,
504 wxTextCtrl **ppText)
505 {
506 wxSizer *sizerRow = new wxBoxSizer(wxHORIZONTAL);
507 wxTextCtrl *text = new wxTextCtrl(this, id, wxEmptyString,
508 wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER);
509
510 sizerRow->Add(control, 0, wxRIGHT | wxALIGN_CENTRE_VERTICAL, 5);
511 sizerRow->Add(text, 1, wxLEFT | wxALIGN_CENTRE_VERTICAL, 5);
512
513 if ( ppText )
514 *ppText = text;
515
516 return sizerRow;
517 }
518
519 // create a sizer containing a label and a text ctrl
520 wxSizer *WidgetsPage::CreateSizerWithTextAndLabel(const wxString& label,
521 wxWindowID id,
522 wxTextCtrl **ppText)
523 {
524 return CreateSizerWithText(new wxStaticText(this, wxID_ANY, label),
525 id, ppText);
526 }
527
528 // create a sizer containing a button and a text ctrl
529 wxSizer *WidgetsPage::CreateSizerWithTextAndButton(wxWindowID idBtn,
530 const wxString& label,
531 wxWindowID id,
532 wxTextCtrl **ppText)
533 {
534 return CreateSizerWithText(new wxButton(this, idBtn, label), id, ppText);
535 }
536
537 wxCheckBox *WidgetsPage::CreateCheckBoxAndAddToSizer(wxSizer *sizer,
538 const wxString& label,
539 wxWindowID id)
540 {
541 wxCheckBox *checkbox = new wxCheckBox(this, id, label);
542 sizer->Add(checkbox, 0, wxLEFT | wxRIGHT, 5);
543 sizer->Add(0, 2, 0, wxGROW); // spacer
544
545 return checkbox;
546 }
547