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
32 #include "wx/button.h"
33 #include "wx/checkbox.h"
34 #include "wx/listbox.h"
35 #include "wx/statbox.h"
36 #include "wx/stattext.h"
37 #include "wx/textctrl.h"
40 #include "wx/notebook.h"
42 #include "wx/colordlg.h"
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
53 Widgets_ClearLog
= 100,
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 // Define a new application type, each program should derive a class from wxApp
64 class WidgetsApp
: public wxApp
67 // override base class virtuals
68 // ----------------------------
70 // this one is called on application startup and is a good place for the app
71 // initialization (doing it here and not in the ctor allows to have an error
72 // return: if OnInit() returns false, the application terminates)
73 virtual bool OnInit();
76 // Define a new frame type: this is going to be our main frame
77 class WidgetsFrame
: public wxFrame
81 WidgetsFrame(const wxString
& title
);
82 virtual ~WidgetsFrame();
87 void OnButtonClearLog(wxCommandEvent
& event
);
89 void OnExit(wxCommandEvent
& event
);
91 void OnSetFgCol(wxCommandEvent
& event
);
92 void OnSetBgCol(wxCommandEvent
& event
);
95 // initialize the notebook: add all pages to it
99 // the panel containing everything
103 // the listbox for logging messages
104 wxListBox
*m_lboxLog
;
106 // the log target we use to redirect messages to the listbox
110 // the notebook containing the test pages
111 wxNotebook
*m_notebook
;
113 // and the image list for it
114 wxImageList
*m_imaglist
;
117 // last chosen fg/bg colours
120 #endif // wxUSE_MENUS
122 // any class wishing to process wxWidgets events must use this macro
123 DECLARE_EVENT_TABLE()
127 // A log target which just redirects the messages to a listbox
128 class LboxLogger
: public wxLog
131 LboxLogger(wxListBox
*lbox
, wxLog
*logOld
)
134 //m_lbox->Disable(); -- looks ugly under MSW
138 virtual ~LboxLogger()
140 wxLog::SetActiveTarget(m_logOld
);
144 // implement sink functions
145 virtual void DoLog(wxLogLevel level
, const wxChar
*szString
, time_t t
)
147 // don't put trace messages into listbox or we can get into infinite
149 if ( level
== wxLOG_Trace
)
153 // cast is needed to call protected method
154 ((LboxLogger
*)m_logOld
)->DoLog(level
, szString
, t
);
159 wxLog::DoLog(level
, szString
, t
);
163 virtual void DoLogString(const wxChar
*szString
, time_t WXUNUSED(t
))
169 #ifdef __WXUNIVERSAL__
170 m_lbox
->AppendAndEnsureVisible(msg
);
171 #else // other ports don't have this method yet
173 m_lbox
->SetFirstItem(m_lbox
->GetCount() - 1);
177 // the control we use
180 // the old log target
186 WX_DEFINE_ARRAY_PTR(WidgetsPage
*, ArrayWidgetsPage
);
188 // ----------------------------------------------------------------------------
190 // ----------------------------------------------------------------------------
192 IMPLEMENT_APP(WidgetsApp
)
194 // ----------------------------------------------------------------------------
196 // ----------------------------------------------------------------------------
198 BEGIN_EVENT_TABLE(WidgetsFrame
, wxFrame
)
200 EVT_BUTTON(Widgets_ClearLog
, WidgetsFrame::OnButtonClearLog
)
202 EVT_BUTTON(Widgets_Quit
, WidgetsFrame::OnExit
)
204 EVT_MENU(wxID_EXIT
, WidgetsFrame::OnExit
)
205 EVT_MENU(Widgets_SetFgColour
, WidgetsFrame::OnSetFgCol
)
206 EVT_MENU(Widgets_SetBgColour
, WidgetsFrame::OnSetBgCol
)
209 // ============================================================================
211 // ============================================================================
213 // ----------------------------------------------------------------------------
215 // ----------------------------------------------------------------------------
217 bool WidgetsApp::OnInit()
219 if ( !wxApp::OnInit() )
222 // the reason for having these ifdef's is that I often run two copies of
223 // this sample side by side and it is useful to see which one is which
225 #if defined(__WXUNIVERSAL__)
226 title
= _T("wxUniv/");
229 #if defined(__WXMSW__)
230 title
+= _T("wxMSW");
231 #elif defined(__WXGTK__)
232 title
+= _T("wxGTK");
233 #elif defined(__WXMAC__)
234 title
+= _T("wxMAC");
235 #elif defined(__WXMOTIF__)
236 title
+= _T("wxMOTIF");
238 title
+= _T("wxWidgets");
241 wxFrame
*frame
= new WidgetsFrame(title
+ _T(" widgets demo"));
244 //wxLog::AddTraceMask(_T("listbox"));
245 //wxLog::AddTraceMask(_T("scrollbar"));
246 //wxLog::AddTraceMask(_T("focus"));
251 // ----------------------------------------------------------------------------
252 // WidgetsFrame construction
253 // ----------------------------------------------------------------------------
255 WidgetsFrame::WidgetsFrame(const wxString
& title
)
256 : wxFrame(NULL
, wxID_ANY
, title
,
257 wxPoint(0, 50), wxDefaultSize
,
258 wxDEFAULT_FRAME_STYLE
|
259 wxNO_FULL_REPAINT_ON_RESIZE
|
265 m_lboxLog
= (wxListBox
*)NULL
;
266 m_logTarget
= (wxLog
*)NULL
;
268 m_notebook
= (wxNotebook
*)NULL
;
269 m_imaglist
= (wxImageList
*)NULL
;
272 // create the menubar
273 wxMenuBar
*mbar
= new wxMenuBar
;
274 wxMenu
*menuWidget
= new wxMenu
;
275 menuWidget
->Append(Widgets_SetFgColour
, _T("Set &foreground...\tCtrl-F"));
276 menuWidget
->Append(Widgets_SetBgColour
, _T("Set &background...\tCtrl-B"));
277 menuWidget
->AppendSeparator();
278 menuWidget
->Append(wxID_EXIT
, _T("&Quit\tCtrl-Q"));
279 mbar
->Append(menuWidget
, _T("&Widget"));
281 #endif // wxUSE_MENUS
284 m_panel
= new wxPanel(this, wxID_ANY
,
285 wxDefaultPosition
, wxDefaultSize
, wxCLIP_CHILDREN
);
287 wxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
289 // we have 2 panes: notebook which pages demonstrating the controls in the
290 // upper one and the log window with some buttons in the lower
292 m_notebook
= new wxNotebook(m_panel
, wxID_ANY
, wxDefaultPosition
,
293 wxDefaultSize
, wxNO_FULL_REPAINT_ON_RESIZE
|wxCLIP_CHILDREN
);
296 // the lower one only has the log listbox and a button to clear it
298 wxSizer
*sizerDown
= new wxStaticBoxSizer(
299 new wxStaticBox( m_panel
, wxID_ANY
, _T("&Log window") ),
302 m_lboxLog
= new wxListBox(m_panel
, wxID_ANY
);
303 sizerDown
->Add(m_lboxLog
, 1, wxGROW
| wxALL
, 5);
304 sizerDown
->SetMinSize(100, 150);
306 wxSizer
*sizerDown
= new wxBoxSizer(wxVERTICAL
);
309 wxBoxSizer
*sizerBtns
= new wxBoxSizer(wxHORIZONTAL
);
312 btn
= new wxButton(m_panel
, Widgets_ClearLog
, _T("Clear &log"));
314 sizerBtns
->Add(10, 0); // spacer
316 btn
= new wxButton(m_panel
, Widgets_Quit
, _T("E&xit"));
318 sizerDown
->Add(sizerBtns
, 0, wxALL
| wxALIGN_RIGHT
, 5);
320 // put everything together
321 sizerTop
->Add(m_notebook
, 1, wxGROW
| (wxALL
& ~(wxTOP
| wxBOTTOM
)), 10);
322 sizerTop
->Add(0, 5, 0, wxGROW
); // spacer in between
323 sizerTop
->Add(sizerDown
, 0, wxGROW
| (wxALL
& ~wxTOP
), 10);
325 m_panel
->SetSizer(sizerTop
);
328 sizerTop
->SetSizeHints(this);
330 #if wxUSE_LOG && !defined(__WXCOCOA__)
331 // wxCocoa's listbox is too flakey to use for logging right now
332 // now that everything is created we can redirect the log messages to the
334 m_logTarget
= new LboxLogger(m_lboxLog
, wxLog::GetActiveTarget());
335 wxLog::SetActiveTarget(m_logTarget
);
339 void WidgetsFrame::InitNotebook()
341 m_imaglist
= new wxImageList(32, 32);
343 ArrayWidgetsPage pages
;
344 wxArrayString labels
;
346 // we need to first create all pages and only then add them to the notebook
347 // as we need the image list first
348 WidgetsPageInfo
*info
= WidgetsPage::ms_widgetPages
;
351 WidgetsPage
*page
= (*info
->GetCtor())(m_notebook
, m_imaglist
);
354 labels
.Add(info
->GetLabel());
356 info
= info
->GetNext();
359 m_notebook
->SetImageList(m_imaglist
);
362 size_t count
= pages
.GetCount();
363 for ( size_t n
= 0; n
< count
; n
++ )
368 false, // don't select
374 WidgetsFrame::~WidgetsFrame()
382 // ----------------------------------------------------------------------------
383 // WidgetsFrame event handlers
384 // ----------------------------------------------------------------------------
386 void WidgetsFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
392 void WidgetsFrame::OnButtonClearLog(wxCommandEvent
& WXUNUSED(event
))
400 void WidgetsFrame::OnSetFgCol(wxCommandEvent
& WXUNUSED(event
))
402 wxColour col
= wxGetColourFromUser(this, m_colFg
);
408 WidgetsPage
*page
= wxStaticCast(m_notebook
->GetCurrentPage(), WidgetsPage
);
409 page
->GetWidget()->SetForegroundColour(m_colFg
);
412 void WidgetsFrame::OnSetBgCol(wxCommandEvent
& WXUNUSED(event
))
414 wxColour col
= wxGetColourFromUser(this, m_colBg
);
420 WidgetsPage
*page
= wxStaticCast(m_notebook
->GetCurrentPage(), WidgetsPage
);
421 page
->GetWidget()->SetBackgroundColour(m_colBg
);
424 #endif // wxUSE_MENUS
426 // ----------------------------------------------------------------------------
428 // ----------------------------------------------------------------------------
430 WidgetsPageInfo
*WidgetsPage::ms_widgetPages
= NULL
;
432 WidgetsPageInfo::WidgetsPageInfo(Constructor ctor
, const wxChar
*label
)
439 // dummy sorting: add and immediately sort on list according to label
441 if(WidgetsPage::ms_widgetPages
)
443 WidgetsPageInfo
*node_prev
= WidgetsPage::ms_widgetPages
;
444 if(wxStrcmp(label
,node_prev
->GetLabel().c_str())<0)
448 WidgetsPage::ms_widgetPages
= this;
452 WidgetsPageInfo
*node_next
;
455 node_next
= node_prev
->GetNext();
458 // add if between two
459 if(wxStrcmp(label
,node_next
->GetLabel().c_str())<0)
461 node_prev
->SetNext(this);
463 // force to break loop
470 node_prev
->SetNext(this);
473 node_prev
= node_next
;
481 WidgetsPage::ms_widgetPages
= this;
487 // ----------------------------------------------------------------------------
489 // ----------------------------------------------------------------------------
491 WidgetsPage::WidgetsPage(wxNotebook
*notebook
)
492 : wxPanel(notebook
, wxID_ANY
,
493 wxDefaultPosition
, wxDefaultSize
,
494 wxNO_FULL_REPAINT_ON_RESIZE
|
500 wxSizer
*WidgetsPage::CreateSizerWithText(wxControl
*control
,
504 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
505 wxTextCtrl
*text
= new wxTextCtrl(this, id
, wxEmptyString
,
506 wxDefaultPosition
, wxDefaultSize
, wxTE_PROCESS_ENTER
);
508 sizerRow
->Add(control
, 0, wxRIGHT
| wxALIGN_CENTRE_VERTICAL
, 5);
509 sizerRow
->Add(text
, 1, wxLEFT
| wxALIGN_CENTRE_VERTICAL
, 5);
517 // create a sizer containing a label and a text ctrl
518 wxSizer
*WidgetsPage::CreateSizerWithTextAndLabel(const wxString
& label
,
522 return CreateSizerWithText(new wxStaticText(this, wxID_ANY
, label
),
526 // create a sizer containing a button and a text ctrl
527 wxSizer
*WidgetsPage::CreateSizerWithTextAndButton(wxWindowID idBtn
,
528 const wxString
& label
,
532 return CreateSizerWithText(new wxButton(this, idBtn
, label
), id
, ppText
);
535 wxCheckBox
*WidgetsPage::CreateCheckBoxAndAddToSizer(wxSizer
*sizer
,
536 const wxString
& label
,
539 wxCheckBox
*checkbox
= new wxCheckBox(this, id
, label
);
540 sizer
->Add(checkbox
, 0, wxLEFT
| wxRIGHT
, 5);
541 sizer
->Add(0, 2, 0, wxGROW
); // spacer