1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
4 // Purpose: Sample showing most of the simple wxWidgets widgets
5 // Author: Vadim Zeitlin
8 // Copyright: (c) 2001 Vadim Zeitlin
9 // License: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // for compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
27 // for all others, include the necessary headers
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"
43 #include "wx/sysopt.h"
44 #include "wx/bookctrl.h"
46 #include "wx/colordlg.h"
47 #include "wx/fontdlg.h"
48 #include "wx/textdlg.h"
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
59 Widgets_ClearLog
= 100,
63 #endif // wxUSE_TOOLTIPS
70 // ----------------------------------------------------------------------------
72 // ----------------------------------------------------------------------------
74 // Define a new application type, each program should derive a class from wxApp
75 class WidgetsApp
: public wxApp
78 // override base class virtuals
79 // ----------------------------
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();
87 // Define a new frame type: this is going to be our main frame
88 class WidgetsFrame
: public wxFrame
92 WidgetsFrame(const wxString
& title
);
93 virtual ~WidgetsFrame();
98 void OnButtonClearLog(wxCommandEvent
& event
);
100 void OnExit(wxCommandEvent
& event
);
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
112 // initialize the book: add all pages to it
116 // the panel containing everything
120 // the listbox for logging messages
121 wxListBox
*m_lboxLog
;
123 // the log target we use to redirect messages to the listbox
127 // the book containing the test pages
130 // and the image list for it
131 wxImageList
*m_imaglist
;
134 // last chosen fg/bg colours and font
138 #endif // wxUSE_MENUS
140 // any class wishing to process wxWidgets events must use this macro
141 DECLARE_EVENT_TABLE()
145 // A log target which just redirects the messages to a listbox
146 class LboxLogger
: public wxLog
149 LboxLogger(wxListBox
*lbox
, wxLog
*logOld
)
152 //m_lbox->Disable(); -- looks ugly under MSW
156 virtual ~LboxLogger()
158 wxLog::SetActiveTarget(m_logOld
);
162 // implement sink functions
163 virtual void DoLog(wxLogLevel level
, const wxChar
*szString
, time_t t
)
165 // don't put trace messages into listbox or we can get into infinite
167 if ( level
== wxLOG_Trace
)
171 // cast is needed to call protected method
172 ((LboxLogger
*)m_logOld
)->DoLog(level
, szString
, t
);
177 wxLog::DoLog(level
, szString
, t
);
181 virtual void DoLogString(const wxChar
*szString
, time_t WXUNUSED(t
))
187 #ifdef __WXUNIVERSAL__
188 m_lbox
->AppendAndEnsureVisible(msg
);
189 #else // other ports don't have this method yet
191 m_lbox
->SetFirstItem(m_lbox
->GetCount() - 1);
195 // the control we use
198 // the old log target
204 WX_DEFINE_ARRAY_PTR(WidgetsPage
*, ArrayWidgetsPage
);
206 // ----------------------------------------------------------------------------
208 // ----------------------------------------------------------------------------
210 IMPLEMENT_APP(WidgetsApp
)
212 // ----------------------------------------------------------------------------
214 // ----------------------------------------------------------------------------
216 BEGIN_EVENT_TABLE(WidgetsFrame
, wxFrame
)
218 EVT_BUTTON(Widgets_ClearLog
, WidgetsFrame::OnButtonClearLog
)
220 EVT_BUTTON(Widgets_Quit
, WidgetsFrame::OnExit
)
223 EVT_MENU(Widgets_SetTooltip
, WidgetsFrame::OnSetTooltip
)
224 #endif // wxUSE_TOOLTIPS
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
)
231 EVT_MENU(wxID_EXIT
, WidgetsFrame::OnExit
)
234 // ============================================================================
236 // ============================================================================
238 // ----------------------------------------------------------------------------
240 // ----------------------------------------------------------------------------
242 bool WidgetsApp::OnInit()
244 if ( !wxApp::OnInit() )
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
250 #if defined(__WXUNIVERSAL__)
251 title
= _T("wxUniv/");
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");
263 title
+= _T("wxWidgets");
266 wxFrame
*frame
= new WidgetsFrame(title
+ _T(" widgets demo"));
269 //wxLog::AddTraceMask(_T("listbox"));
270 //wxLog::AddTraceMask(_T("scrollbar"));
271 //wxLog::AddTraceMask(_T("focus"));
276 // ----------------------------------------------------------------------------
277 // WidgetsFrame construction
278 // ----------------------------------------------------------------------------
280 WidgetsFrame::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
|
290 m_lboxLog
= (wxListBox
*)NULL
;
291 m_logTarget
= (wxLog
*)NULL
;
293 m_book
= (wxBookCtrl
*)NULL
;
294 m_imaglist
= (wxImageList
*)NULL
;
297 // create the menubar
298 wxMenuBar
*mbar
= new wxMenuBar
;
299 wxMenu
*menuWidget
= new wxMenu
;
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"));
313 mbar
->Check(Widgets_Enable
, true);
314 #endif // wxUSE_MENUS
317 m_panel
= new wxPanel(this, wxID_ANY
,
318 wxDefaultPosition
, wxDefaultSize
, wxCLIP_CHILDREN
);
320 wxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
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
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;
329 m_book
= new wxBookCtrl(m_panel
, wxID_ANY
, wxDefaultPosition
,
330 wxDefaultSize
, style
);
333 #ifndef __SMARTPHONE__
334 // the lower one only has the log listbox and a button to clear it
336 wxSizer
*sizerDown
= new wxStaticBoxSizer(
337 new wxStaticBox( m_panel
, wxID_ANY
, _T("&Log window") ),
340 m_lboxLog
= new wxListBox(m_panel
, wxID_ANY
);
341 sizerDown
->Add(m_lboxLog
, 1, wxGROW
| wxALL
, 5);
342 sizerDown
->SetMinSize(100, 150);
344 wxSizer
*sizerDown
= new wxBoxSizer(wxVERTICAL
);
347 wxBoxSizer
*sizerBtns
= new wxBoxSizer(wxHORIZONTAL
);
350 btn
= new wxButton(m_panel
, Widgets_ClearLog
, _T("Clear &log"));
352 sizerBtns
->Add(10, 0); // spacer
354 btn
= new wxButton(m_panel
, Widgets_Quit
, _T("E&xit"));
356 sizerDown
->Add(sizerBtns
, 0, wxALL
| wxALIGN_RIGHT
, 5);
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);
363 #else // !__SMARTPHONE__/__SMARTPHONE__
365 sizerTop
->Add(m_book
, 1, wxGROW
| wxALL
);
367 #endif // __SMARTPHONE__
369 m_panel
->SetSizer(sizerTop
);
372 sizerTop
->SetSizeHints(this);
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
378 m_logTarget
= new LboxLogger(m_lboxLog
, wxLog::GetActiveTarget());
379 wxLog::SetActiveTarget(m_logTarget
);
383 void WidgetsFrame::InitBook()
385 m_imaglist
= new wxImageList(32, 32);
387 ArrayWidgetsPage pages
;
388 wxArrayString labels
;
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
;
395 WidgetsPage
*page
= (*info
->GetCtor())(m_book
, m_imaglist
);
398 labels
.Add(info
->GetLabel());
400 info
= info
->GetNext();
403 m_book
->SetImageList(m_imaglist
);
406 size_t count
= pages
.GetCount();
407 for ( size_t n
= 0; n
< count
; n
++ )
412 false, // don't select
417 wxColour colour = m_book->MSWGetBgColourForChild(pages[n]);
418 pages[n]->SetBackgroundColour(colour);
423 WidgetsFrame::~WidgetsFrame()
431 // ----------------------------------------------------------------------------
432 // WidgetsFrame event handlers
433 // ----------------------------------------------------------------------------
435 void WidgetsFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
441 void WidgetsFrame::OnButtonClearLog(wxCommandEvent
& WXUNUSED(event
))
451 void WidgetsFrame::OnSetTooltip(wxCommandEvent
& WXUNUSED(event
))
453 static wxString s_tip
= _T("This is a tooltip");
455 wxString s
= wxGetTextFromUser
457 _T("Tooltip text: "),
458 _T("Widgets sample"),
468 if( wxMessageBox( _T("Test multiline tooltip text?"),
469 _T("Widgets sample"),
474 s
= _T("#1 ") + s_tip
+ _T("\n") + _T("#2 ") + s_tip
;
477 WidgetsPage
*page
= wxStaticCast(m_book
->GetCurrentPage(), WidgetsPage
);
478 page
->GetWidget()->SetToolTip(s
);
480 wxControl
*ctrl2
= page
->GetWidget2();
482 ctrl2
->SetToolTip(s
);
485 #endif // wxUSE_TOOLTIPS
487 void WidgetsFrame::OnSetFgCol(wxCommandEvent
& WXUNUSED(event
))
490 // allow for debugging the default colour the first time this is called
491 WidgetsPage
*page
= wxStaticCast(m_book
->GetCurrentPage(), WidgetsPage
);
493 m_colFg
= page
->GetForegroundColour();
495 wxColour col
= wxGetColourFromUser(this, m_colFg
);
501 page
->GetWidget()->SetForegroundColour(m_colFg
);
502 page
->GetWidget()->Refresh();
504 wxControl
*ctrl2
= page
->GetWidget2();
507 ctrl2
->SetForegroundColour(m_colFg
);
511 wxLogMessage(_T("Colour selection dialog not available in current build."));
515 void WidgetsFrame::OnSetBgCol(wxCommandEvent
& WXUNUSED(event
))
518 WidgetsPage
*page
= wxStaticCast(m_book
->GetCurrentPage(), WidgetsPage
);
520 m_colBg
= page
->GetBackgroundColour();
522 wxColour col
= wxGetColourFromUser(this, m_colBg
);
528 page
->GetWidget()->SetBackgroundColour(m_colBg
);
529 page
->GetWidget()->Refresh();
531 wxControl
*ctrl2
= page
->GetWidget2();
534 ctrl2
->SetBackgroundColour(m_colFg
);
538 wxLogMessage(_T("Colour selection dialog not available in current build."));
542 void WidgetsFrame::OnSetFont(wxCommandEvent
& WXUNUSED(event
))
545 WidgetsPage
*page
= wxStaticCast(m_book
->GetCurrentPage(), WidgetsPage
);
547 m_font
= page
->GetFont();
549 wxFont font
= wxGetFontFromUser(this, m_font
);
555 page
->GetWidget()->SetFont(m_font
);
556 page
->GetWidget()->Refresh();
558 wxControl
*ctrl2
= page
->GetWidget2();
561 ctrl2
->SetFont(m_font
);
565 wxLogMessage(_T("Font selection dialog not available in current build."));
569 void WidgetsFrame::OnEnable(wxCommandEvent
& event
)
571 WidgetsPage
*page
= wxStaticCast(m_book
->GetCurrentPage(), WidgetsPage
);
572 page
->GetWidget()->Enable(event
.IsChecked());
575 #endif // wxUSE_MENUS
577 // ----------------------------------------------------------------------------
579 // ----------------------------------------------------------------------------
581 WidgetsPageInfo
*WidgetsPage::ms_widgetPages
= NULL
;
583 WidgetsPageInfo::WidgetsPageInfo(Constructor ctor
, const wxChar
*label
)
590 // dummy sorting: add and immediately sort on list according to label
592 if(WidgetsPage::ms_widgetPages
)
594 WidgetsPageInfo
*node_prev
= WidgetsPage::ms_widgetPages
;
595 if(wxStrcmp(label
,node_prev
->GetLabel().c_str())<0)
599 WidgetsPage::ms_widgetPages
= this;
603 WidgetsPageInfo
*node_next
;
606 node_next
= node_prev
->GetNext();
609 // add if between two
610 if(wxStrcmp(label
,node_next
->GetLabel().c_str())<0)
612 node_prev
->SetNext(this);
614 // force to break loop
621 node_prev
->SetNext(this);
624 node_prev
= node_next
;
632 WidgetsPage::ms_widgetPages
= this;
638 // ----------------------------------------------------------------------------
640 // ----------------------------------------------------------------------------
642 WidgetsPage::WidgetsPage(wxBookCtrl
*book
)
643 : wxPanel(book
, wxID_ANY
,
644 wxDefaultPosition
, wxDefaultSize
,
645 wxNO_FULL_REPAINT_ON_RESIZE
|
651 wxSizer
*WidgetsPage::CreateSizerWithText(wxControl
*control
,
655 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
656 wxTextCtrl
*text
= new wxTextCtrl(this, id
, wxEmptyString
,
657 wxDefaultPosition
, wxDefaultSize
, wxTE_PROCESS_ENTER
);
659 sizerRow
->Add(control
, 0, wxRIGHT
| wxALIGN_CENTRE_VERTICAL
, 5);
660 sizerRow
->Add(text
, 1, wxLEFT
| wxALIGN_CENTRE_VERTICAL
, 5);
668 // create a sizer containing a label and a text ctrl
669 wxSizer
*WidgetsPage::CreateSizerWithTextAndLabel(const wxString
& label
,
673 return CreateSizerWithText(new wxStaticText(this, wxID_ANY
, label
),
677 // create a sizer containing a button and a text ctrl
678 wxSizer
*WidgetsPage::CreateSizerWithTextAndButton(wxWindowID idBtn
,
679 const wxString
& label
,
683 return CreateSizerWithText(new wxButton(this, idBtn
, label
), id
, ppText
);
686 wxCheckBox
*WidgetsPage::CreateCheckBoxAndAddToSizer(wxSizer
*sizer
,
687 const wxString
& label
,
690 wxCheckBox
*checkbox
= new wxCheckBox(this, id
, label
);
691 sizer
->Add(checkbox
, 0, wxLEFT
| wxRIGHT
, 5);
692 sizer
->Add(0, 2, 0, wxGROW
); // spacer