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,
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 // Define a new application type, each program should derive a class from wxApp
67 class WidgetsApp
: public wxApp
70 // override base class virtuals
71 // ----------------------------
73 // this one is called on application startup and is a good place for the app
74 // initialization (doing it here and not in the ctor allows to have an error
75 // return: if OnInit() returns false, the application terminates)
76 virtual bool OnInit();
79 // Define a new frame type: this is going to be our main frame
80 class WidgetsFrame
: public wxFrame
84 WidgetsFrame(const wxString
& title
);
85 virtual ~WidgetsFrame();
90 void OnButtonClearLog(wxCommandEvent
& event
);
92 void OnExit(wxCommandEvent
& event
);
94 void OnSetFgCol(wxCommandEvent
& event
);
95 void OnSetBgCol(wxCommandEvent
& event
);
98 // initialize the book: add all pages to it
102 // the panel containing everything
106 // the listbox for logging messages
107 wxListBox
*m_lboxLog
;
109 // the log target we use to redirect messages to the listbox
113 // the book containing the test pages
116 // and the image list for it
117 wxImageList
*m_imaglist
;
120 // last chosen fg/bg colours
123 #endif // wxUSE_MENUS
125 // any class wishing to process wxWidgets events must use this macro
126 DECLARE_EVENT_TABLE()
130 // A log target which just redirects the messages to a listbox
131 class LboxLogger
: public wxLog
134 LboxLogger(wxListBox
*lbox
, wxLog
*logOld
)
137 //m_lbox->Disable(); -- looks ugly under MSW
141 virtual ~LboxLogger()
143 wxLog::SetActiveTarget(m_logOld
);
147 // implement sink functions
148 virtual void DoLog(wxLogLevel level
, const wxChar
*szString
, time_t t
)
150 // don't put trace messages into listbox or we can get into infinite
152 if ( level
== wxLOG_Trace
)
156 // cast is needed to call protected method
157 ((LboxLogger
*)m_logOld
)->DoLog(level
, szString
, t
);
162 wxLog::DoLog(level
, szString
, t
);
166 virtual void DoLogString(const wxChar
*szString
, time_t WXUNUSED(t
))
172 #ifdef __WXUNIVERSAL__
173 m_lbox
->AppendAndEnsureVisible(msg
);
174 #else // other ports don't have this method yet
176 m_lbox
->SetFirstItem(m_lbox
->GetCount() - 1);
180 // the control we use
183 // the old log target
189 WX_DEFINE_ARRAY_PTR(WidgetsPage
*, ArrayWidgetsPage
);
191 // ----------------------------------------------------------------------------
193 // ----------------------------------------------------------------------------
195 IMPLEMENT_APP(WidgetsApp
)
197 // ----------------------------------------------------------------------------
199 // ----------------------------------------------------------------------------
201 BEGIN_EVENT_TABLE(WidgetsFrame
, wxFrame
)
203 EVT_BUTTON(Widgets_ClearLog
, WidgetsFrame::OnButtonClearLog
)
205 EVT_BUTTON(Widgets_Quit
, WidgetsFrame::OnExit
)
207 EVT_MENU(wxID_EXIT
, WidgetsFrame::OnExit
)
208 EVT_MENU(Widgets_SetFgColour
, WidgetsFrame::OnSetFgCol
)
209 EVT_MENU(Widgets_SetBgColour
, WidgetsFrame::OnSetBgCol
)
212 // ============================================================================
214 // ============================================================================
216 // ----------------------------------------------------------------------------
218 // ----------------------------------------------------------------------------
220 bool WidgetsApp::OnInit()
222 if ( !wxApp::OnInit() )
225 // the reason for having these ifdef's is that I often run two copies of
226 // this sample side by side and it is useful to see which one is which
228 #if defined(__WXUNIVERSAL__)
229 title
= _T("wxUniv/");
232 #if defined(__WXMSW__)
233 title
+= _T("wxMSW");
234 #elif defined(__WXGTK__)
235 title
+= _T("wxGTK");
236 #elif defined(__WXMAC__)
237 title
+= _T("wxMAC");
238 #elif defined(__WXMOTIF__)
239 title
+= _T("wxMOTIF");
241 title
+= _T("wxWidgets");
244 wxFrame
*frame
= new WidgetsFrame(title
+ _T(" widgets demo"));
247 //wxLog::AddTraceMask(_T("listbox"));
248 //wxLog::AddTraceMask(_T("scrollbar"));
249 //wxLog::AddTraceMask(_T("focus"));
254 // ----------------------------------------------------------------------------
255 // WidgetsFrame construction
256 // ----------------------------------------------------------------------------
258 WidgetsFrame::WidgetsFrame(const wxString
& title
)
259 : wxFrame(NULL
, wxID_ANY
, title
,
260 wxPoint(0, 50), wxDefaultSize
,
261 wxDEFAULT_FRAME_STYLE
|
262 wxNO_FULL_REPAINT_ON_RESIZE
|
268 m_lboxLog
= (wxListBox
*)NULL
;
269 m_logTarget
= (wxLog
*)NULL
;
271 m_book
= (wxBookCtrl
*)NULL
;
272 m_imaglist
= (wxImageList
*)NULL
;
275 // create the menubar
276 wxMenuBar
*mbar
= new wxMenuBar
;
277 wxMenu
*menuWidget
= new wxMenu
;
278 menuWidget
->Append(Widgets_SetFgColour
, _T("Set &foreground...\tCtrl-F"));
279 menuWidget
->Append(Widgets_SetBgColour
, _T("Set &background...\tCtrl-B"));
280 menuWidget
->AppendSeparator();
281 menuWidget
->Append(wxID_EXIT
, _T("&Quit\tCtrl-Q"));
282 mbar
->Append(menuWidget
, _T("&Widget"));
284 #endif // wxUSE_MENUS
287 m_panel
= new wxPanel(this, wxID_ANY
,
288 wxDefaultPosition
, wxDefaultSize
, wxCLIP_CHILDREN
);
290 wxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
292 // we have 2 panes: book with pages demonstrating the controls in the
293 // upper one and the log window with some buttons in the lower
295 int style
= wxNO_FULL_REPAINT_ON_RESIZE
|wxCLIP_CHILDREN
|wxBC_DEFAULT
;
296 // Uncomment to suppress page theme (draw in solid colour)
297 // style |= wxNB_NOPAGETHEME;
299 m_book
= new wxBookCtrl(m_panel
, wxID_ANY
, wxDefaultPosition
,
300 wxDefaultSize
, style
);
303 #ifndef __SMARTPHONE__
304 // the lower one only has the log listbox and a button to clear it
306 wxSizer
*sizerDown
= new wxStaticBoxSizer(
307 new wxStaticBox( m_panel
, wxID_ANY
, _T("&Log window") ),
310 m_lboxLog
= new wxListBox(m_panel
, wxID_ANY
);
311 sizerDown
->Add(m_lboxLog
, 1, wxGROW
| wxALL
, 5);
312 sizerDown
->SetMinSize(100, 150);
314 wxSizer
*sizerDown
= new wxBoxSizer(wxVERTICAL
);
317 wxBoxSizer
*sizerBtns
= new wxBoxSizer(wxHORIZONTAL
);
320 btn
= new wxButton(m_panel
, Widgets_ClearLog
, _T("Clear &log"));
322 sizerBtns
->Add(10, 0); // spacer
324 btn
= new wxButton(m_panel
, Widgets_Quit
, _T("E&xit"));
326 sizerDown
->Add(sizerBtns
, 0, wxALL
| wxALIGN_RIGHT
, 5);
328 // put everything together
329 sizerTop
->Add(m_book
, 1, wxGROW
| (wxALL
& ~(wxTOP
| wxBOTTOM
)), 10);
330 sizerTop
->Add(0, 5, 0, wxGROW
); // spacer in between
331 sizerTop
->Add(sizerDown
, 0, wxGROW
| (wxALL
& ~wxTOP
), 10);
333 #else // !__SMARTPHONE__/__SMARTPHONE__
335 sizerTop
->Add(m_book
, 1, wxGROW
| wxALL
);
337 #endif // __SMARTPHONE__
339 m_panel
->SetSizer(sizerTop
);
342 sizerTop
->SetSizeHints(this);
344 #if USE_LOG && !defined(__WXCOCOA__)
345 // wxCocoa's listbox is too flakey to use for logging right now
346 // now that everything is created we can redirect the log messages to the
348 m_logTarget
= new LboxLogger(m_lboxLog
, wxLog::GetActiveTarget());
349 wxLog::SetActiveTarget(m_logTarget
);
353 void WidgetsFrame::InitBook()
355 m_imaglist
= new wxImageList(32, 32);
357 ArrayWidgetsPage pages
;
358 wxArrayString labels
;
360 // we need to first create all pages and only then add them to the book
361 // as we need the image list first
362 WidgetsPageInfo
*info
= WidgetsPage::ms_widgetPages
;
365 WidgetsPage
*page
= (*info
->GetCtor())(m_book
, m_imaglist
);
368 labels
.Add(info
->GetLabel());
370 info
= info
->GetNext();
373 m_book
->SetImageList(m_imaglist
);
376 size_t count
= pages
.GetCount();
377 for ( size_t n
= 0; n
< count
; n
++ )
382 false, // don't select
387 wxColour colour = m_book->MSWGetBgColourForChild(pages[n]);
388 pages[n]->SetBackgroundColour(colour);
393 WidgetsFrame::~WidgetsFrame()
401 // ----------------------------------------------------------------------------
402 // WidgetsFrame event handlers
403 // ----------------------------------------------------------------------------
405 void WidgetsFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
411 void WidgetsFrame::OnButtonClearLog(wxCommandEvent
& WXUNUSED(event
))
419 void WidgetsFrame::OnSetFgCol(wxCommandEvent
& WXUNUSED(event
))
422 wxColour col
= wxGetColourFromUser(this, m_colFg
);
428 WidgetsPage
*page
= wxStaticCast(m_book
->GetCurrentPage(), WidgetsPage
);
429 page
->GetWidget()->SetForegroundColour(m_colFg
);
430 page
->GetWidget()->Refresh();
432 wxLogMessage(_T("None colour dialog available in current build."));
436 void WidgetsFrame::OnSetBgCol(wxCommandEvent
& WXUNUSED(event
))
439 wxColour col
= wxGetColourFromUser(this, m_colBg
);
445 WidgetsPage
*page
= wxStaticCast(m_book
->GetCurrentPage(), WidgetsPage
);
446 page
->GetWidget()->SetBackgroundColour(m_colBg
);
447 page
->GetWidget()->Refresh();
449 wxLogMessage(_T("None colour dialog available in current build."));
453 #endif // wxUSE_MENUS
455 // ----------------------------------------------------------------------------
457 // ----------------------------------------------------------------------------
459 WidgetsPageInfo
*WidgetsPage::ms_widgetPages
= NULL
;
461 WidgetsPageInfo::WidgetsPageInfo(Constructor ctor
, const wxChar
*label
)
468 // dummy sorting: add and immediately sort on list according to label
470 if(WidgetsPage::ms_widgetPages
)
472 WidgetsPageInfo
*node_prev
= WidgetsPage::ms_widgetPages
;
473 if(wxStrcmp(label
,node_prev
->GetLabel().c_str())<0)
477 WidgetsPage::ms_widgetPages
= this;
481 WidgetsPageInfo
*node_next
;
484 node_next
= node_prev
->GetNext();
487 // add if between two
488 if(wxStrcmp(label
,node_next
->GetLabel().c_str())<0)
490 node_prev
->SetNext(this);
492 // force to break loop
499 node_prev
->SetNext(this);
502 node_prev
= node_next
;
510 WidgetsPage::ms_widgetPages
= this;
516 // ----------------------------------------------------------------------------
518 // ----------------------------------------------------------------------------
520 WidgetsPage::WidgetsPage(wxBookCtrl
*book
)
521 : wxPanel(book
, wxID_ANY
,
522 wxDefaultPosition
, wxDefaultSize
,
523 wxNO_FULL_REPAINT_ON_RESIZE
|
529 wxSizer
*WidgetsPage::CreateSizerWithText(wxControl
*control
,
533 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
534 wxTextCtrl
*text
= new wxTextCtrl(this, id
, wxEmptyString
,
535 wxDefaultPosition
, wxDefaultSize
, wxTE_PROCESS_ENTER
);
537 sizerRow
->Add(control
, 0, wxRIGHT
| wxALIGN_CENTRE_VERTICAL
, 5);
538 sizerRow
->Add(text
, 1, wxLEFT
| wxALIGN_CENTRE_VERTICAL
, 5);
546 // create a sizer containing a label and a text ctrl
547 wxSizer
*WidgetsPage::CreateSizerWithTextAndLabel(const wxString
& label
,
551 return CreateSizerWithText(new wxStaticText(this, wxID_ANY
, label
),
555 // create a sizer containing a button and a text ctrl
556 wxSizer
*WidgetsPage::CreateSizerWithTextAndButton(wxWindowID idBtn
,
557 const wxString
& label
,
561 return CreateSizerWithText(new wxButton(this, idBtn
, label
), id
, ppText
);
564 wxCheckBox
*WidgetsPage::CreateCheckBoxAndAddToSizer(wxSizer
*sizer
,
565 const wxString
& label
,
568 wxCheckBox
*checkbox
= new wxCheckBox(this, id
, label
);
569 sizer
->Add(checkbox
, 0, wxLEFT
| wxRIGHT
, 5);
570 sizer
->Add(0, 2, 0, wxGROW
); // spacer