]> git.saurik.com Git - wxWidgets.git/blame_incremental - samples/widgets/widgets.cpp
added tests for setting fg/bg colours
[wxWidgets.git] / samples / widgets / widgets.cpp
... / ...
CommitLineData
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
51enum
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
64class WidgetsApp : public wxApp
65{
66public:
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
77class WidgetsFrame : public wxFrame
78{
79public:
80 // ctor(s) and dtor
81 WidgetsFrame(const wxString& title);
82 virtual ~WidgetsFrame();
83
84protected:
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
98private:
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
128class LboxLogger : public wxLog
129{
130public:
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
143private:
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
186WX_DEFINE_ARRAY_PTR(WidgetsPage *, ArrayWidgetsPage);
187
188// ----------------------------------------------------------------------------
189// misc macros
190// ----------------------------------------------------------------------------
191
192IMPLEMENT_APP(WidgetsApp)
193
194// ----------------------------------------------------------------------------
195// event tables
196// ----------------------------------------------------------------------------
197
198BEGIN_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)
207END_EVENT_TABLE()
208
209// ============================================================================
210// implementation
211// ============================================================================
212
213// ----------------------------------------------------------------------------
214// app class
215// ----------------------------------------------------------------------------
216
217bool 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
255WidgetsFrame::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
339void 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
374WidgetsFrame::~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
386void WidgetsFrame::OnExit(wxCommandEvent& WXUNUSED(event))
387{
388 Close();
389}
390
391#if wxUSE_LOG
392void WidgetsFrame::OnButtonClearLog(wxCommandEvent& WXUNUSED(event))
393{
394 m_lboxLog->Clear();
395}
396#endif // wxUSE_LOG
397
398#if wxUSE_MENUS
399
400void 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}
411
412void WidgetsFrame::OnSetBgCol(wxCommandEvent& WXUNUSED(event))
413{
414 wxColour col = wxGetColourFromUser(this, m_colBg);
415 if ( !col.Ok() )
416 return;
417
418 m_colBg = col;
419
420 WidgetsPage *page = wxStaticCast(m_notebook->GetCurrentPage(), WidgetsPage);
421 page->GetWidget()->SetBackgroundColour(m_colBg);
422}
423
424#endif // wxUSE_MENUS
425
426// ----------------------------------------------------------------------------
427// WidgetsPageInfo
428// ----------------------------------------------------------------------------
429
430WidgetsPageInfo *WidgetsPage::ms_widgetPages = NULL;
431
432WidgetsPageInfo::WidgetsPageInfo(Constructor ctor, const wxChar *label)
433 : m_label(label)
434{
435 m_ctor = ctor;
436
437 m_next = NULL;
438
439 // dummy sorting: add and immediately sort on list according to label
440
441 if(WidgetsPage::ms_widgetPages)
442 {
443 WidgetsPageInfo *node_prev = WidgetsPage::ms_widgetPages;
444 if(wxStrcmp(label,node_prev->GetLabel().c_str())<0)
445 {
446 // add as first
447 m_next = node_prev;
448 WidgetsPage::ms_widgetPages = this;
449 }
450 else
451 {
452 WidgetsPageInfo *node_next;
453 do
454 {
455 node_next = node_prev->GetNext();
456 if(node_next)
457 {
458 // add if between two
459 if(wxStrcmp(label,node_next->GetLabel().c_str())<0)
460 {
461 node_prev->SetNext(this);
462 m_next = node_next;
463 // force to break loop
464 node_next = NULL;
465 }
466 }
467 else
468 {
469 // add as last
470 node_prev->SetNext(this);
471 m_next = node_next;
472 }
473 node_prev = node_next;
474 }while(node_next);
475 }
476 }
477 else
478 {
479 // add when first
480
481 WidgetsPage::ms_widgetPages = this;
482
483 }
484
485}
486
487// ----------------------------------------------------------------------------
488// WidgetsPage
489// ----------------------------------------------------------------------------
490
491WidgetsPage::WidgetsPage(wxNotebook *notebook)
492 : wxPanel(notebook, wxID_ANY,
493 wxDefaultPosition, wxDefaultSize,
494 wxNO_FULL_REPAINT_ON_RESIZE |
495 wxCLIP_CHILDREN |
496 wxTAB_TRAVERSAL)
497{
498}
499
500wxSizer *WidgetsPage::CreateSizerWithText(wxControl *control,
501 wxWindowID id,
502 wxTextCtrl **ppText)
503{
504 wxSizer *sizerRow = new wxBoxSizer(wxHORIZONTAL);
505 wxTextCtrl *text = new wxTextCtrl(this, id, wxEmptyString,
506 wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER);
507
508 sizerRow->Add(control, 0, wxRIGHT | wxALIGN_CENTRE_VERTICAL, 5);
509 sizerRow->Add(text, 1, wxLEFT | wxALIGN_CENTRE_VERTICAL, 5);
510
511 if ( ppText )
512 *ppText = text;
513
514 return sizerRow;
515}
516
517// create a sizer containing a label and a text ctrl
518wxSizer *WidgetsPage::CreateSizerWithTextAndLabel(const wxString& label,
519 wxWindowID id,
520 wxTextCtrl **ppText)
521{
522 return CreateSizerWithText(new wxStaticText(this, wxID_ANY, label),
523 id, ppText);
524}
525
526// create a sizer containing a button and a text ctrl
527wxSizer *WidgetsPage::CreateSizerWithTextAndButton(wxWindowID idBtn,
528 const wxString& label,
529 wxWindowID id,
530 wxTextCtrl **ppText)
531{
532 return CreateSizerWithText(new wxButton(this, idBtn, label), id, ppText);
533}
534
535wxCheckBox *WidgetsPage::CreateCheckBoxAndAddToSizer(wxSizer *sizer,
536 const wxString& label,
537 wxWindowID id)
538{
539 wxCheckBox *checkbox = new wxCheckBox(this, id, label);
540 sizer->Add(checkbox, 0, wxLEFT | wxRIGHT, 5);
541 sizer->Add(0, 2, 0, wxGROW); // spacer
542
543 return checkbox;
544}
545