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"
42 #include "wx/sysopt.h"
43 #include "wx/bookctrl.h"
45 #include "wx/colordlg.h"
46 #include "wx/textdlg.h"
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
57 Widgets_ClearLog
= 100,
61 #endif // wxUSE_TOOLTIPS
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
70 // Define a new application type, each program should derive a class from wxApp
71 class WidgetsApp
: public wxApp
74 // override base class virtuals
75 // ----------------------------
77 // this one is called on application startup and is a good place for the app
78 // initialization (doing it here and not in the ctor allows to have an error
79 // return: if OnInit() returns false, the application terminates)
80 virtual bool OnInit();
83 // Define a new frame type: this is going to be our main frame
84 class WidgetsFrame
: public wxFrame
88 WidgetsFrame(const wxString
& title
);
89 virtual ~WidgetsFrame();
94 void OnButtonClearLog(wxCommandEvent
& event
);
96 void OnExit(wxCommandEvent
& event
);
99 void OnSetTooltip(wxCommandEvent
& event
);
100 #endif // wxUSE_TOOLTIPS
101 void OnSetFgCol(wxCommandEvent
& event
);
102 void OnSetBgCol(wxCommandEvent
& event
);
103 #endif // wxUSE_MENUS
105 // initialize the book: add all pages to it
109 // the panel containing everything
113 // the listbox for logging messages
114 wxListBox
*m_lboxLog
;
116 // the log target we use to redirect messages to the listbox
120 // the book containing the test pages
123 // and the image list for it
124 wxImageList
*m_imaglist
;
127 // last chosen fg/bg colours
130 #endif // wxUSE_MENUS
132 // any class wishing to process wxWidgets events must use this macro
133 DECLARE_EVENT_TABLE()
137 // A log target which just redirects the messages to a listbox
138 class LboxLogger
: public wxLog
141 LboxLogger(wxListBox
*lbox
, wxLog
*logOld
)
144 //m_lbox->Disable(); -- looks ugly under MSW
148 virtual ~LboxLogger()
150 wxLog::SetActiveTarget(m_logOld
);
154 // implement sink functions
155 virtual void DoLog(wxLogLevel level
, const wxChar
*szString
, time_t t
)
157 // don't put trace messages into listbox or we can get into infinite
159 if ( level
== wxLOG_Trace
)
163 // cast is needed to call protected method
164 ((LboxLogger
*)m_logOld
)->DoLog(level
, szString
, t
);
169 wxLog::DoLog(level
, szString
, t
);
173 virtual void DoLogString(const wxChar
*szString
, time_t WXUNUSED(t
))
179 #ifdef __WXUNIVERSAL__
180 m_lbox
->AppendAndEnsureVisible(msg
);
181 #else // other ports don't have this method yet
183 m_lbox
->SetFirstItem(m_lbox
->GetCount() - 1);
187 // the control we use
190 // the old log target
196 WX_DEFINE_ARRAY_PTR(WidgetsPage
*, ArrayWidgetsPage
);
198 // ----------------------------------------------------------------------------
200 // ----------------------------------------------------------------------------
202 IMPLEMENT_APP(WidgetsApp
)
204 // ----------------------------------------------------------------------------
206 // ----------------------------------------------------------------------------
208 BEGIN_EVENT_TABLE(WidgetsFrame
, wxFrame
)
210 EVT_BUTTON(Widgets_ClearLog
, WidgetsFrame::OnButtonClearLog
)
212 EVT_BUTTON(Widgets_Quit
, WidgetsFrame::OnExit
)
215 EVT_MENU(Widgets_SetTooltip
, WidgetsFrame::OnSetTooltip
)
216 #endif // wxUSE_TOOLTIPS
218 EVT_MENU(Widgets_SetFgColour
, WidgetsFrame::OnSetFgCol
)
219 EVT_MENU(Widgets_SetBgColour
, WidgetsFrame::OnSetBgCol
)
221 EVT_MENU(wxID_EXIT
, WidgetsFrame::OnExit
)
224 // ============================================================================
226 // ============================================================================
228 // ----------------------------------------------------------------------------
230 // ----------------------------------------------------------------------------
232 bool WidgetsApp::OnInit()
234 if ( !wxApp::OnInit() )
237 // the reason for having these ifdef's is that I often run two copies of
238 // this sample side by side and it is useful to see which one is which
240 #if defined(__WXUNIVERSAL__)
241 title
= _T("wxUniv/");
244 #if defined(__WXMSW__)
245 title
+= _T("wxMSW");
246 #elif defined(__WXGTK__)
247 title
+= _T("wxGTK");
248 #elif defined(__WXMAC__)
249 title
+= _T("wxMAC");
250 #elif defined(__WXMOTIF__)
251 title
+= _T("wxMOTIF");
253 title
+= _T("wxWidgets");
256 wxFrame
*frame
= new WidgetsFrame(title
+ _T(" widgets demo"));
259 //wxLog::AddTraceMask(_T("listbox"));
260 //wxLog::AddTraceMask(_T("scrollbar"));
261 //wxLog::AddTraceMask(_T("focus"));
266 // ----------------------------------------------------------------------------
267 // WidgetsFrame construction
268 // ----------------------------------------------------------------------------
270 WidgetsFrame::WidgetsFrame(const wxString
& title
)
271 : wxFrame(NULL
, wxID_ANY
, title
,
272 wxPoint(0, 50), wxDefaultSize
,
273 wxDEFAULT_FRAME_STYLE
|
274 wxNO_FULL_REPAINT_ON_RESIZE
|
280 m_lboxLog
= (wxListBox
*)NULL
;
281 m_logTarget
= (wxLog
*)NULL
;
283 m_book
= (wxBookCtrl
*)NULL
;
284 m_imaglist
= (wxImageList
*)NULL
;
287 // create the menubar
288 wxMenuBar
*mbar
= new wxMenuBar
;
289 wxMenu
*menuWidget
= new wxMenu
;
291 menuWidget
->Append(Widgets_SetTooltip
, _T("Set &tooltip...\tCtrl-T"));
292 menuWidget
->AppendSeparator();
293 #endif // wxUSE_TOOLTIPS
294 menuWidget
->Append(Widgets_SetFgColour
, _T("Set &foreground...\tCtrl-F"));
295 menuWidget
->Append(Widgets_SetBgColour
, _T("Set &background...\tCtrl-B"));
296 menuWidget
->AppendSeparator();
297 menuWidget
->Append(wxID_EXIT
, _T("&Quit\tCtrl-Q"));
298 mbar
->Append(menuWidget
, _T("&Widget"));
300 #endif // wxUSE_MENUS
303 m_panel
= new wxPanel(this, wxID_ANY
,
304 wxDefaultPosition
, wxDefaultSize
, wxCLIP_CHILDREN
);
306 wxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
308 // we have 2 panes: book with pages demonstrating the controls in the
309 // upper one and the log window with some buttons in the lower
311 int style
= wxNO_FULL_REPAINT_ON_RESIZE
|wxCLIP_CHILDREN
|wxBC_DEFAULT
;
312 // Uncomment to suppress page theme (draw in solid colour)
313 //style |= wxNB_NOPAGETHEME;
315 m_book
= new wxBookCtrl(m_panel
, wxID_ANY
, wxDefaultPosition
,
316 wxDefaultSize
, style
);
319 #ifndef __SMARTPHONE__
320 // the lower one only has the log listbox and a button to clear it
322 wxSizer
*sizerDown
= new wxStaticBoxSizer(
323 new wxStaticBox( m_panel
, wxID_ANY
, _T("&Log window") ),
326 m_lboxLog
= new wxListBox(m_panel
, wxID_ANY
);
327 sizerDown
->Add(m_lboxLog
, 1, wxGROW
| wxALL
, 5);
328 sizerDown
->SetMinSize(100, 150);
330 wxSizer
*sizerDown
= new wxBoxSizer(wxVERTICAL
);
333 wxBoxSizer
*sizerBtns
= new wxBoxSizer(wxHORIZONTAL
);
336 btn
= new wxButton(m_panel
, Widgets_ClearLog
, _T("Clear &log"));
338 sizerBtns
->Add(10, 0); // spacer
340 btn
= new wxButton(m_panel
, Widgets_Quit
, _T("E&xit"));
342 sizerDown
->Add(sizerBtns
, 0, wxALL
| wxALIGN_RIGHT
, 5);
344 // put everything together
345 sizerTop
->Add(m_book
, 1, wxGROW
| (wxALL
& ~(wxTOP
| wxBOTTOM
)), 10);
346 sizerTop
->Add(0, 5, 0, wxGROW
); // spacer in between
347 sizerTop
->Add(sizerDown
, 0, wxGROW
| (wxALL
& ~wxTOP
), 10);
349 #else // !__SMARTPHONE__/__SMARTPHONE__
351 sizerTop
->Add(m_book
, 1, wxGROW
| wxALL
);
353 #endif // __SMARTPHONE__
355 m_panel
->SetSizer(sizerTop
);
358 sizerTop
->SetSizeHints(this);
360 #if USE_LOG && !defined(__WXCOCOA__)
361 // wxCocoa's listbox is too flakey to use for logging right now
362 // now that everything is created we can redirect the log messages to the
364 m_logTarget
= new LboxLogger(m_lboxLog
, wxLog::GetActiveTarget());
365 wxLog::SetActiveTarget(m_logTarget
);
369 void WidgetsFrame::InitBook()
371 m_imaglist
= new wxImageList(32, 32);
373 ArrayWidgetsPage pages
;
374 wxArrayString labels
;
376 // we need to first create all pages and only then add them to the book
377 // as we need the image list first
378 WidgetsPageInfo
*info
= WidgetsPage::ms_widgetPages
;
381 WidgetsPage
*page
= (*info
->GetCtor())(m_book
, m_imaglist
);
384 labels
.Add(info
->GetLabel());
386 info
= info
->GetNext();
389 m_book
->SetImageList(m_imaglist
);
392 size_t count
= pages
.GetCount();
393 for ( size_t n
= 0; n
< count
; n
++ )
398 false, // don't select
403 wxColour colour = m_book->MSWGetBgColourForChild(pages[n]);
404 pages[n]->SetBackgroundColour(colour);
409 WidgetsFrame::~WidgetsFrame()
417 // ----------------------------------------------------------------------------
418 // WidgetsFrame event handlers
419 // ----------------------------------------------------------------------------
421 void WidgetsFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
427 void WidgetsFrame::OnButtonClearLog(wxCommandEvent
& WXUNUSED(event
))
437 void WidgetsFrame::OnSetTooltip(wxCommandEvent
& WXUNUSED(event
))
439 static wxString s_tip
= _T("This is a tooltip");
441 wxString s
= wxGetTextFromUser
443 _T("Tooltip text: "),
444 _T("Widgets sample"),
452 WidgetsPage
*page
= wxStaticCast(m_book
->GetCurrentPage(), WidgetsPage
);
453 page
->GetWidget()->SetToolTip(s_tip
= s
);
455 wxControl
*ctrl2
= page
->GetWidget2();
457 ctrl2
->SetToolTip(s
);
460 #endif // wxUSE_TOOLTIPS
462 void WidgetsFrame::OnSetFgCol(wxCommandEvent
& WXUNUSED(event
))
465 wxColour col
= wxGetColourFromUser(this, m_colFg
);
471 WidgetsPage
*page
= wxStaticCast(m_book
->GetCurrentPage(), WidgetsPage
);
472 page
->GetWidget()->SetForegroundColour(m_colFg
);
473 page
->GetWidget()->Refresh();
475 wxControl
*ctrl2
= page
->GetWidget2();
478 ctrl2
->SetForegroundColour(m_colFg
);
482 wxLogMessage(_T("Colour selection dialog not available in current build."));
486 void WidgetsFrame::OnSetBgCol(wxCommandEvent
& WXUNUSED(event
))
489 wxColour col
= wxGetColourFromUser(this, m_colBg
);
495 WidgetsPage
*page
= wxStaticCast(m_book
->GetCurrentPage(), WidgetsPage
);
496 page
->GetWidget()->SetBackgroundColour(m_colBg
);
497 page
->GetWidget()->Refresh();
499 wxControl
*ctrl2
= page
->GetWidget2();
502 ctrl2
->SetBackgroundColour(m_colFg
);
506 wxLogMessage(_T("Colour selection dialog not available in current build."));
510 #endif // wxUSE_MENUS
512 // ----------------------------------------------------------------------------
514 // ----------------------------------------------------------------------------
516 WidgetsPageInfo
*WidgetsPage::ms_widgetPages
= NULL
;
518 WidgetsPageInfo::WidgetsPageInfo(Constructor ctor
, const wxChar
*label
)
525 // dummy sorting: add and immediately sort on list according to label
527 if(WidgetsPage::ms_widgetPages
)
529 WidgetsPageInfo
*node_prev
= WidgetsPage::ms_widgetPages
;
530 if(wxStrcmp(label
,node_prev
->GetLabel().c_str())<0)
534 WidgetsPage::ms_widgetPages
= this;
538 WidgetsPageInfo
*node_next
;
541 node_next
= node_prev
->GetNext();
544 // add if between two
545 if(wxStrcmp(label
,node_next
->GetLabel().c_str())<0)
547 node_prev
->SetNext(this);
549 // force to break loop
556 node_prev
->SetNext(this);
559 node_prev
= node_next
;
567 WidgetsPage::ms_widgetPages
= this;
573 // ----------------------------------------------------------------------------
575 // ----------------------------------------------------------------------------
577 WidgetsPage::WidgetsPage(wxBookCtrl
*book
)
578 : wxPanel(book
, wxID_ANY
,
579 wxDefaultPosition
, wxDefaultSize
,
580 wxNO_FULL_REPAINT_ON_RESIZE
|
586 wxSizer
*WidgetsPage::CreateSizerWithText(wxControl
*control
,
590 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
591 wxTextCtrl
*text
= new wxTextCtrl(this, id
, wxEmptyString
,
592 wxDefaultPosition
, wxDefaultSize
, wxTE_PROCESS_ENTER
);
594 sizerRow
->Add(control
, 0, wxRIGHT
| wxALIGN_CENTRE_VERTICAL
, 5);
595 sizerRow
->Add(text
, 1, wxLEFT
| wxALIGN_CENTRE_VERTICAL
, 5);
603 // create a sizer containing a label and a text ctrl
604 wxSizer
*WidgetsPage::CreateSizerWithTextAndLabel(const wxString
& label
,
608 return CreateSizerWithText(new wxStaticText(this, wxID_ANY
, label
),
612 // create a sizer containing a button and a text ctrl
613 wxSizer
*WidgetsPage::CreateSizerWithTextAndButton(wxWindowID idBtn
,
614 const wxString
& label
,
618 return CreateSizerWithText(new wxButton(this, idBtn
, label
), id
, ppText
);
621 wxCheckBox
*WidgetsPage::CreateCheckBoxAndAddToSizer(wxSizer
*sizer
,
622 const wxString
& label
,
625 wxCheckBox
*checkbox
= new wxCheckBox(this, id
, label
);
626 sizer
->Add(checkbox
, 0, wxLEFT
| wxRIGHT
, 5);
627 sizer
->Add(0, 2, 0, wxGROW
); // spacer