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"
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
56 Widgets_ClearLog
= 100,
60 #endif // wxUSE_TOOLTIPS
65 // ----------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
69 // Define a new application type, each program should derive a class from wxApp
70 class WidgetsApp
: public wxApp
73 // override base class virtuals
74 // ----------------------------
76 // this one is called on application startup and is a good place for the app
77 // initialization (doing it here and not in the ctor allows to have an error
78 // return: if OnInit() returns false, the application terminates)
79 virtual bool OnInit();
82 // Define a new frame type: this is going to be our main frame
83 class WidgetsFrame
: public wxFrame
87 WidgetsFrame(const wxString
& title
);
88 virtual ~WidgetsFrame();
93 void OnButtonClearLog(wxCommandEvent
& event
);
95 void OnExit(wxCommandEvent
& event
);
98 void OnSetTooltip(wxCommandEvent
& event
);
99 #endif // wxUSE_TOOLTIPS
100 void OnSetFgCol(wxCommandEvent
& event
);
101 void OnSetBgCol(wxCommandEvent
& event
);
102 #endif // wxUSE_MENUS
104 // initialize the book: add all pages to it
108 // the panel containing everything
112 // the listbox for logging messages
113 wxListBox
*m_lboxLog
;
115 // the log target we use to redirect messages to the listbox
119 // the book containing the test pages
122 // and the image list for it
123 wxImageList
*m_imaglist
;
126 // last chosen fg/bg colours
129 #endif // wxUSE_MENUS
131 // any class wishing to process wxWidgets events must use this macro
132 DECLARE_EVENT_TABLE()
136 // A log target which just redirects the messages to a listbox
137 class LboxLogger
: public wxLog
140 LboxLogger(wxListBox
*lbox
, wxLog
*logOld
)
143 //m_lbox->Disable(); -- looks ugly under MSW
147 virtual ~LboxLogger()
149 wxLog::SetActiveTarget(m_logOld
);
153 // implement sink functions
154 virtual void DoLog(wxLogLevel level
, const wxChar
*szString
, time_t t
)
156 // don't put trace messages into listbox or we can get into infinite
158 if ( level
== wxLOG_Trace
)
162 // cast is needed to call protected method
163 ((LboxLogger
*)m_logOld
)->DoLog(level
, szString
, t
);
168 wxLog::DoLog(level
, szString
, t
);
172 virtual void DoLogString(const wxChar
*szString
, time_t WXUNUSED(t
))
178 #ifdef __WXUNIVERSAL__
179 m_lbox
->AppendAndEnsureVisible(msg
);
180 #else // other ports don't have this method yet
182 m_lbox
->SetFirstItem(m_lbox
->GetCount() - 1);
186 // the control we use
189 // the old log target
195 WX_DEFINE_ARRAY_PTR(WidgetsPage
*, ArrayWidgetsPage
);
197 // ----------------------------------------------------------------------------
199 // ----------------------------------------------------------------------------
201 IMPLEMENT_APP(WidgetsApp
)
203 // ----------------------------------------------------------------------------
205 // ----------------------------------------------------------------------------
207 BEGIN_EVENT_TABLE(WidgetsFrame
, wxFrame
)
209 EVT_BUTTON(Widgets_ClearLog
, WidgetsFrame::OnButtonClearLog
)
211 EVT_BUTTON(Widgets_Quit
, WidgetsFrame::OnExit
)
214 EVT_MENU(Widgets_SetTooltip
, WidgetsFrame::OnSetTooltip
)
215 #endif // wxUSE_TOOLTIPS
217 EVT_MENU(Widgets_SetFgColour
, WidgetsFrame::OnSetFgCol
)
218 EVT_MENU(Widgets_SetBgColour
, WidgetsFrame::OnSetBgCol
)
220 EVT_MENU(wxID_EXIT
, WidgetsFrame::OnExit
)
223 // ============================================================================
225 // ============================================================================
227 // ----------------------------------------------------------------------------
229 // ----------------------------------------------------------------------------
231 bool WidgetsApp::OnInit()
233 if ( !wxApp::OnInit() )
236 // the reason for having these ifdef's is that I often run two copies of
237 // this sample side by side and it is useful to see which one is which
239 #if defined(__WXUNIVERSAL__)
240 title
= _T("wxUniv/");
243 #if defined(__WXMSW__)
244 title
+= _T("wxMSW");
245 #elif defined(__WXGTK__)
246 title
+= _T("wxGTK");
247 #elif defined(__WXMAC__)
248 title
+= _T("wxMAC");
249 #elif defined(__WXMOTIF__)
250 title
+= _T("wxMOTIF");
252 title
+= _T("wxWidgets");
255 wxFrame
*frame
= new WidgetsFrame(title
+ _T(" widgets demo"));
258 //wxLog::AddTraceMask(_T("listbox"));
259 //wxLog::AddTraceMask(_T("scrollbar"));
260 //wxLog::AddTraceMask(_T("focus"));
265 // ----------------------------------------------------------------------------
266 // WidgetsFrame construction
267 // ----------------------------------------------------------------------------
269 WidgetsFrame::WidgetsFrame(const wxString
& title
)
270 : wxFrame(NULL
, wxID_ANY
, title
,
271 wxPoint(0, 50), wxDefaultSize
,
272 wxDEFAULT_FRAME_STYLE
|
273 wxNO_FULL_REPAINT_ON_RESIZE
|
279 m_lboxLog
= (wxListBox
*)NULL
;
280 m_logTarget
= (wxLog
*)NULL
;
282 m_book
= (wxBookCtrl
*)NULL
;
283 m_imaglist
= (wxImageList
*)NULL
;
286 // create the menubar
287 wxMenuBar
*mbar
= new wxMenuBar
;
288 wxMenu
*menuWidget
= new wxMenu
;
290 menuWidget
->Append(Widgets_SetTooltip
, _T("Set &tooltip...\tCtrl-T"));
291 menuWidget
->AppendSeparator();
292 #endif // wxUSE_TOOLTIPS
293 menuWidget
->Append(Widgets_SetFgColour
, _T("Set &foreground...\tCtrl-F"));
294 menuWidget
->Append(Widgets_SetBgColour
, _T("Set &background...\tCtrl-B"));
295 menuWidget
->AppendSeparator();
296 menuWidget
->Append(wxID_EXIT
, _T("&Quit\tCtrl-Q"));
297 mbar
->Append(menuWidget
, _T("&Widget"));
299 #endif // wxUSE_MENUS
302 m_panel
= new wxPanel(this, wxID_ANY
,
303 wxDefaultPosition
, wxDefaultSize
, wxCLIP_CHILDREN
);
305 wxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
307 // we have 2 panes: book with pages demonstrating the controls in the
308 // upper one and the log window with some buttons in the lower
310 int style
= wxNO_FULL_REPAINT_ON_RESIZE
|wxCLIP_CHILDREN
|wxBC_DEFAULT
;
311 // Uncomment to suppress page theme (draw in solid colour)
312 // style |= wxNB_NOPAGETHEME;
314 m_book
= new wxBookCtrl(m_panel
, wxID_ANY
, wxDefaultPosition
,
315 wxDefaultSize
, style
);
318 #ifndef __SMARTPHONE__
319 // the lower one only has the log listbox and a button to clear it
321 wxSizer
*sizerDown
= new wxStaticBoxSizer(
322 new wxStaticBox( m_panel
, wxID_ANY
, _T("&Log window") ),
325 m_lboxLog
= new wxListBox(m_panel
, wxID_ANY
);
326 sizerDown
->Add(m_lboxLog
, 1, wxGROW
| wxALL
, 5);
327 sizerDown
->SetMinSize(100, 150);
329 wxSizer
*sizerDown
= new wxBoxSizer(wxVERTICAL
);
332 wxBoxSizer
*sizerBtns
= new wxBoxSizer(wxHORIZONTAL
);
335 btn
= new wxButton(m_panel
, Widgets_ClearLog
, _T("Clear &log"));
337 sizerBtns
->Add(10, 0); // spacer
339 btn
= new wxButton(m_panel
, Widgets_Quit
, _T("E&xit"));
341 sizerDown
->Add(sizerBtns
, 0, wxALL
| wxALIGN_RIGHT
, 5);
343 // put everything together
344 sizerTop
->Add(m_book
, 1, wxGROW
| (wxALL
& ~(wxTOP
| wxBOTTOM
)), 10);
345 sizerTop
->Add(0, 5, 0, wxGROW
); // spacer in between
346 sizerTop
->Add(sizerDown
, 0, wxGROW
| (wxALL
& ~wxTOP
), 10);
348 #else // !__SMARTPHONE__/__SMARTPHONE__
350 sizerTop
->Add(m_book
, 1, wxGROW
| wxALL
);
352 #endif // __SMARTPHONE__
354 m_panel
->SetSizer(sizerTop
);
357 sizerTop
->SetSizeHints(this);
359 #if USE_LOG && !defined(__WXCOCOA__)
360 // wxCocoa's listbox is too flakey to use for logging right now
361 // now that everything is created we can redirect the log messages to the
363 m_logTarget
= new LboxLogger(m_lboxLog
, wxLog::GetActiveTarget());
364 wxLog::SetActiveTarget(m_logTarget
);
368 void WidgetsFrame::InitBook()
370 m_imaglist
= new wxImageList(32, 32);
372 ArrayWidgetsPage pages
;
373 wxArrayString labels
;
375 // we need to first create all pages and only then add them to the book
376 // as we need the image list first
377 WidgetsPageInfo
*info
= WidgetsPage::ms_widgetPages
;
380 WidgetsPage
*page
= (*info
->GetCtor())(m_book
, m_imaglist
);
383 labels
.Add(info
->GetLabel());
385 info
= info
->GetNext();
388 m_book
->SetImageList(m_imaglist
);
391 size_t count
= pages
.GetCount();
392 for ( size_t n
= 0; n
< count
; n
++ )
397 false, // don't select
402 wxColour colour = m_book->MSWGetBgColourForChild(pages[n]);
403 pages[n]->SetBackgroundColour(colour);
408 WidgetsFrame::~WidgetsFrame()
416 // ----------------------------------------------------------------------------
417 // WidgetsFrame event handlers
418 // ----------------------------------------------------------------------------
420 void WidgetsFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
426 void WidgetsFrame::OnButtonClearLog(wxCommandEvent
& WXUNUSED(event
))
436 void WidgetsFrame::OnSetTooltip(wxCommandEvent
& WXUNUSED(event
))
438 static wxString s_tip
= _T("This is a tooltip");
440 wxString s
= wxGetTextFromUser
442 _T("Tooltip text: "),
443 _T("Widgets sample"),
451 WidgetsPage
*page
= wxStaticCast(m_book
->GetCurrentPage(), WidgetsPage
);
452 page
->GetWidget()->SetToolTip(s_tip
= s
);
454 wxControl
*ctrl2
= page
->GetWidget2();
456 ctrl2
->SetToolTip(s
);
459 #endif // wxUSE_TOOLTIPS
461 void WidgetsFrame::OnSetFgCol(wxCommandEvent
& WXUNUSED(event
))
464 wxColour col
= wxGetColourFromUser(this, m_colFg
);
470 WidgetsPage
*page
= wxStaticCast(m_book
->GetCurrentPage(), WidgetsPage
);
471 page
->GetWidget()->SetForegroundColour(m_colFg
);
472 page
->GetWidget()->Refresh();
474 wxControl
*ctrl2
= page
->GetWidget2();
477 ctrl2
->SetForegroundColour(m_colFg
);
481 wxLogMessage(_T("Colour selection dialog not available in current build."));
485 void WidgetsFrame::OnSetBgCol(wxCommandEvent
& WXUNUSED(event
))
488 wxColour col
= wxGetColourFromUser(this, m_colBg
);
494 WidgetsPage
*page
= wxStaticCast(m_book
->GetCurrentPage(), WidgetsPage
);
495 page
->GetWidget()->SetBackgroundColour(m_colBg
);
496 page
->GetWidget()->Refresh();
498 wxControl
*ctrl2
= page
->GetWidget2();
501 ctrl2
->SetBackgroundColour(m_colFg
);
505 wxLogMessage(_T("Colour selection dialog not available in current build."));
509 #endif // wxUSE_MENUS
511 // ----------------------------------------------------------------------------
513 // ----------------------------------------------------------------------------
515 WidgetsPageInfo
*WidgetsPage::ms_widgetPages
= NULL
;
517 WidgetsPageInfo::WidgetsPageInfo(Constructor ctor
, const wxChar
*label
)
524 // dummy sorting: add and immediately sort on list according to label
526 if(WidgetsPage::ms_widgetPages
)
528 WidgetsPageInfo
*node_prev
= WidgetsPage::ms_widgetPages
;
529 if(wxStrcmp(label
,node_prev
->GetLabel().c_str())<0)
533 WidgetsPage::ms_widgetPages
= this;
537 WidgetsPageInfo
*node_next
;
540 node_next
= node_prev
->GetNext();
543 // add if between two
544 if(wxStrcmp(label
,node_next
->GetLabel().c_str())<0)
546 node_prev
->SetNext(this);
548 // force to break loop
555 node_prev
->SetNext(this);
558 node_prev
= node_next
;
566 WidgetsPage::ms_widgetPages
= this;
572 // ----------------------------------------------------------------------------
574 // ----------------------------------------------------------------------------
576 WidgetsPage::WidgetsPage(wxBookCtrl
*book
)
577 : wxPanel(book
, wxID_ANY
,
578 wxDefaultPosition
, wxDefaultSize
,
579 wxNO_FULL_REPAINT_ON_RESIZE
|
585 wxSizer
*WidgetsPage::CreateSizerWithText(wxControl
*control
,
589 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
590 wxTextCtrl
*text
= new wxTextCtrl(this, id
, wxEmptyString
,
591 wxDefaultPosition
, wxDefaultSize
, wxTE_PROCESS_ENTER
);
593 sizerRow
->Add(control
, 0, wxRIGHT
| wxALIGN_CENTRE_VERTICAL
, 5);
594 sizerRow
->Add(text
, 1, wxLEFT
| wxALIGN_CENTRE_VERTICAL
, 5);
602 // create a sizer containing a label and a text ctrl
603 wxSizer
*WidgetsPage::CreateSizerWithTextAndLabel(const wxString
& label
,
607 return CreateSizerWithText(new wxStaticText(this, wxID_ANY
, label
),
611 // create a sizer containing a button and a text ctrl
612 wxSizer
*WidgetsPage::CreateSizerWithTextAndButton(wxWindowID idBtn
,
613 const wxString
& label
,
617 return CreateSizerWithText(new wxButton(this, idBtn
, label
), id
, ppText
);
620 wxCheckBox
*WidgetsPage::CreateCheckBoxAndAddToSizer(wxSizer
*sizer
,
621 const wxString
& label
,
624 wxCheckBox
*checkbox
= new wxCheckBox(this, id
, label
);
625 sizer
->Add(checkbox
, 0, wxLEFT
| wxRIGHT
, 5);
626 sizer
->Add(0, 2, 0, wxGROW
); // spacer