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