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/fontdlg.h"
47 #include "wx/textdlg.h"
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
58 Widgets_ClearLog
= 100,
62 #endif // wxUSE_TOOLTIPS
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
72 // Define a new application type, each program should derive a class from wxApp
73 class WidgetsApp
: public wxApp
76 // override base class virtuals
77 // ----------------------------
79 // this one is called on application startup and is a good place for the app
80 // initialization (doing it here and not in the ctor allows to have an error
81 // return: if OnInit() returns false, the application terminates)
82 virtual bool OnInit();
85 // Define a new frame type: this is going to be our main frame
86 class WidgetsFrame
: public wxFrame
90 WidgetsFrame(const wxString
& title
);
91 virtual ~WidgetsFrame();
96 void OnButtonClearLog(wxCommandEvent
& event
);
98 void OnExit(wxCommandEvent
& event
);
101 void OnSetTooltip(wxCommandEvent
& event
);
102 #endif // wxUSE_TOOLTIPS
103 void OnSetFgCol(wxCommandEvent
& event
);
104 void OnSetBgCol(wxCommandEvent
& event
);
105 void OnSetFont(wxCommandEvent
& event
);
106 #endif // wxUSE_MENUS
108 // initialize the book: add all pages to it
112 // the panel containing everything
116 // the listbox for logging messages
117 wxListBox
*m_lboxLog
;
119 // the log target we use to redirect messages to the listbox
123 // the book containing the test pages
126 // and the image list for it
127 wxImageList
*m_imaglist
;
130 // last chosen fg/bg colours and font
134 #endif // wxUSE_MENUS
136 // any class wishing to process wxWidgets events must use this macro
137 DECLARE_EVENT_TABLE()
141 // A log target which just redirects the messages to a listbox
142 class LboxLogger
: public wxLog
145 LboxLogger(wxListBox
*lbox
, wxLog
*logOld
)
148 //m_lbox->Disable(); -- looks ugly under MSW
152 virtual ~LboxLogger()
154 wxLog::SetActiveTarget(m_logOld
);
158 // implement sink functions
159 virtual void DoLog(wxLogLevel level
, const wxChar
*szString
, time_t t
)
161 // don't put trace messages into listbox or we can get into infinite
163 if ( level
== wxLOG_Trace
)
167 // cast is needed to call protected method
168 ((LboxLogger
*)m_logOld
)->DoLog(level
, szString
, t
);
173 wxLog::DoLog(level
, szString
, t
);
177 virtual void DoLogString(const wxChar
*szString
, time_t WXUNUSED(t
))
183 #ifdef __WXUNIVERSAL__
184 m_lbox
->AppendAndEnsureVisible(msg
);
185 #else // other ports don't have this method yet
187 m_lbox
->SetFirstItem(m_lbox
->GetCount() - 1);
191 // the control we use
194 // the old log target
200 WX_DEFINE_ARRAY_PTR(WidgetsPage
*, ArrayWidgetsPage
);
202 // ----------------------------------------------------------------------------
204 // ----------------------------------------------------------------------------
206 IMPLEMENT_APP(WidgetsApp
)
208 // ----------------------------------------------------------------------------
210 // ----------------------------------------------------------------------------
212 BEGIN_EVENT_TABLE(WidgetsFrame
, wxFrame
)
214 EVT_BUTTON(Widgets_ClearLog
, WidgetsFrame::OnButtonClearLog
)
216 EVT_BUTTON(Widgets_Quit
, WidgetsFrame::OnExit
)
219 EVT_MENU(Widgets_SetTooltip
, WidgetsFrame::OnSetTooltip
)
220 #endif // wxUSE_TOOLTIPS
222 EVT_MENU(Widgets_SetFgColour
, WidgetsFrame::OnSetFgCol
)
223 EVT_MENU(Widgets_SetBgColour
, WidgetsFrame::OnSetBgCol
)
224 EVT_MENU(Widgets_SetFont
, WidgetsFrame::OnSetFont
)
226 EVT_MENU(wxID_EXIT
, WidgetsFrame::OnExit
)
229 // ============================================================================
231 // ============================================================================
233 // ----------------------------------------------------------------------------
235 // ----------------------------------------------------------------------------
237 bool WidgetsApp::OnInit()
239 if ( !wxApp::OnInit() )
242 // the reason for having these ifdef's is that I often run two copies of
243 // this sample side by side and it is useful to see which one is which
245 #if defined(__WXUNIVERSAL__)
246 title
= _T("wxUniv/");
249 #if defined(__WXMSW__)
250 title
+= _T("wxMSW");
251 #elif defined(__WXGTK__)
252 title
+= _T("wxGTK");
253 #elif defined(__WXMAC__)
254 title
+= _T("wxMAC");
255 #elif defined(__WXMOTIF__)
256 title
+= _T("wxMOTIF");
258 title
+= _T("wxWidgets");
261 wxFrame
*frame
= new WidgetsFrame(title
+ _T(" widgets demo"));
264 //wxLog::AddTraceMask(_T("listbox"));
265 //wxLog::AddTraceMask(_T("scrollbar"));
266 //wxLog::AddTraceMask(_T("focus"));
271 // ----------------------------------------------------------------------------
272 // WidgetsFrame construction
273 // ----------------------------------------------------------------------------
275 WidgetsFrame::WidgetsFrame(const wxString
& title
)
276 : wxFrame(NULL
, wxID_ANY
, title
,
277 wxPoint(0, 50), wxDefaultSize
,
278 wxDEFAULT_FRAME_STYLE
|
279 wxNO_FULL_REPAINT_ON_RESIZE
|
285 m_lboxLog
= (wxListBox
*)NULL
;
286 m_logTarget
= (wxLog
*)NULL
;
288 m_book
= (wxBookCtrl
*)NULL
;
289 m_imaglist
= (wxImageList
*)NULL
;
292 // create the menubar
293 wxMenuBar
*mbar
= new wxMenuBar
;
294 wxMenu
*menuWidget
= new wxMenu
;
296 menuWidget
->Append(Widgets_SetTooltip
, _T("Set &tooltip...\tCtrl-T"));
297 menuWidget
->AppendSeparator();
298 #endif // wxUSE_TOOLTIPS
299 menuWidget
->Append(Widgets_SetFgColour
, _T("Set &foreground...\tCtrl-F"));
300 menuWidget
->Append(Widgets_SetBgColour
, _T("Set &background...\tCtrl-B"));
301 menuWidget
->Append(Widgets_SetFont
, _T("Set f&ont...\tCtrl-O"));
302 menuWidget
->AppendSeparator();
303 menuWidget
->Append(wxID_EXIT
, _T("&Quit\tCtrl-Q"));
304 mbar
->Append(menuWidget
, _T("&Widget"));
306 #endif // wxUSE_MENUS
309 m_panel
= new wxPanel(this, wxID_ANY
,
310 wxDefaultPosition
, wxDefaultSize
, wxCLIP_CHILDREN
);
312 wxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
314 // we have 2 panes: book with pages demonstrating the controls in the
315 // upper one and the log window with some buttons in the lower
317 int style
= wxNO_FULL_REPAINT_ON_RESIZE
|wxCLIP_CHILDREN
|wxBC_DEFAULT
;
318 // Uncomment to suppress page theme (draw in solid colour)
319 //style |= wxNB_NOPAGETHEME;
321 m_book
= new wxBookCtrl(m_panel
, wxID_ANY
, wxDefaultPosition
,
322 wxDefaultSize
, style
);
325 #ifndef __SMARTPHONE__
326 // the lower one only has the log listbox and a button to clear it
328 wxSizer
*sizerDown
= new wxStaticBoxSizer(
329 new wxStaticBox( m_panel
, wxID_ANY
, _T("&Log window") ),
332 m_lboxLog
= new wxListBox(m_panel
, wxID_ANY
);
333 sizerDown
->Add(m_lboxLog
, 1, wxGROW
| wxALL
, 5);
334 sizerDown
->SetMinSize(100, 150);
336 wxSizer
*sizerDown
= new wxBoxSizer(wxVERTICAL
);
339 wxBoxSizer
*sizerBtns
= new wxBoxSizer(wxHORIZONTAL
);
342 btn
= new wxButton(m_panel
, Widgets_ClearLog
, _T("Clear &log"));
344 sizerBtns
->Add(10, 0); // spacer
346 btn
= new wxButton(m_panel
, Widgets_Quit
, _T("E&xit"));
348 sizerDown
->Add(sizerBtns
, 0, wxALL
| wxALIGN_RIGHT
, 5);
350 // put everything together
351 sizerTop
->Add(m_book
, 1, wxGROW
| (wxALL
& ~(wxTOP
| wxBOTTOM
)), 10);
352 sizerTop
->Add(0, 5, 0, wxGROW
); // spacer in between
353 sizerTop
->Add(sizerDown
, 0, wxGROW
| (wxALL
& ~wxTOP
), 10);
355 #else // !__SMARTPHONE__/__SMARTPHONE__
357 sizerTop
->Add(m_book
, 1, wxGROW
| wxALL
);
359 #endif // __SMARTPHONE__
361 m_panel
->SetSizer(sizerTop
);
364 sizerTop
->SetSizeHints(this);
366 #if USE_LOG && !defined(__WXCOCOA__)
367 // wxCocoa's listbox is too flakey to use for logging right now
368 // now that everything is created we can redirect the log messages to the
370 m_logTarget
= new LboxLogger(m_lboxLog
, wxLog::GetActiveTarget());
371 wxLog::SetActiveTarget(m_logTarget
);
375 void WidgetsFrame::InitBook()
377 m_imaglist
= new wxImageList(32, 32);
379 ArrayWidgetsPage pages
;
380 wxArrayString labels
;
382 // we need to first create all pages and only then add them to the book
383 // as we need the image list first
384 WidgetsPageInfo
*info
= WidgetsPage::ms_widgetPages
;
387 WidgetsPage
*page
= (*info
->GetCtor())(m_book
, m_imaglist
);
390 labels
.Add(info
->GetLabel());
392 info
= info
->GetNext();
395 m_book
->SetImageList(m_imaglist
);
398 size_t count
= pages
.GetCount();
399 for ( size_t n
= 0; n
< count
; n
++ )
404 false, // don't select
409 wxColour colour = m_book->MSWGetBgColourForChild(pages[n]);
410 pages[n]->SetBackgroundColour(colour);
415 WidgetsFrame::~WidgetsFrame()
423 // ----------------------------------------------------------------------------
424 // WidgetsFrame event handlers
425 // ----------------------------------------------------------------------------
427 void WidgetsFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
433 void WidgetsFrame::OnButtonClearLog(wxCommandEvent
& WXUNUSED(event
))
443 void WidgetsFrame::OnSetTooltip(wxCommandEvent
& WXUNUSED(event
))
445 static wxString s_tip
= _T("This is a tooltip");
447 wxString s
= wxGetTextFromUser
449 _T("Tooltip text: "),
450 _T("Widgets sample"),
458 WidgetsPage
*page
= wxStaticCast(m_book
->GetCurrentPage(), WidgetsPage
);
459 page
->GetWidget()->SetToolTip(s_tip
= s
);
461 wxControl
*ctrl2
= page
->GetWidget2();
463 ctrl2
->SetToolTip(s
);
466 #endif // wxUSE_TOOLTIPS
468 void WidgetsFrame::OnSetFgCol(wxCommandEvent
& WXUNUSED(event
))
471 // allow for debugging the default colour the first time this is called
472 WidgetsPage
*page
= wxStaticCast(m_book
->GetCurrentPage(), WidgetsPage
);
474 m_colFg
= page
->GetForegroundColour();
476 wxColour col
= wxGetColourFromUser(this, m_colFg
);
482 page
->GetWidget()->SetForegroundColour(m_colFg
);
483 page
->GetWidget()->Refresh();
485 wxControl
*ctrl2
= page
->GetWidget2();
488 ctrl2
->SetForegroundColour(m_colFg
);
492 wxLogMessage(_T("Colour selection dialog not available in current build."));
496 void WidgetsFrame::OnSetBgCol(wxCommandEvent
& WXUNUSED(event
))
499 WidgetsPage
*page
= wxStaticCast(m_book
->GetCurrentPage(), WidgetsPage
);
501 m_colBg
= page
->GetBackgroundColour();
503 wxColour col
= wxGetColourFromUser(this, m_colBg
);
509 page
->GetWidget()->SetBackgroundColour(m_colBg
);
510 page
->GetWidget()->Refresh();
512 wxControl
*ctrl2
= page
->GetWidget2();
515 ctrl2
->SetBackgroundColour(m_colFg
);
519 wxLogMessage(_T("Colour selection dialog not available in current build."));
523 void WidgetsFrame::OnSetFont(wxCommandEvent
& WXUNUSED(event
))
526 WidgetsPage
*page
= wxStaticCast(m_book
->GetCurrentPage(), WidgetsPage
);
528 m_font
= page
->GetFont();
530 wxFont font
= wxGetFontFromUser(this, m_font
);
536 page
->GetWidget()->SetFont(m_font
);
537 page
->GetWidget()->Refresh();
539 wxControl
*ctrl2
= page
->GetWidget2();
542 ctrl2
->SetFont(m_font
);
546 wxLogMessage(_T("Font selection dialog not available in current build."));
550 #endif // wxUSE_MENUS
552 // ----------------------------------------------------------------------------
554 // ----------------------------------------------------------------------------
556 WidgetsPageInfo
*WidgetsPage::ms_widgetPages
= NULL
;
558 WidgetsPageInfo::WidgetsPageInfo(Constructor ctor
, const wxChar
*label
)
565 // dummy sorting: add and immediately sort on list according to label
567 if(WidgetsPage::ms_widgetPages
)
569 WidgetsPageInfo
*node_prev
= WidgetsPage::ms_widgetPages
;
570 if(wxStrcmp(label
,node_prev
->GetLabel().c_str())<0)
574 WidgetsPage::ms_widgetPages
= this;
578 WidgetsPageInfo
*node_next
;
581 node_next
= node_prev
->GetNext();
584 // add if between two
585 if(wxStrcmp(label
,node_next
->GetLabel().c_str())<0)
587 node_prev
->SetNext(this);
589 // force to break loop
596 node_prev
->SetNext(this);
599 node_prev
= node_next
;
607 WidgetsPage::ms_widgetPages
= this;
613 // ----------------------------------------------------------------------------
615 // ----------------------------------------------------------------------------
617 WidgetsPage::WidgetsPage(wxBookCtrl
*book
)
618 : wxPanel(book
, wxID_ANY
,
619 wxDefaultPosition
, wxDefaultSize
,
620 wxNO_FULL_REPAINT_ON_RESIZE
|
626 wxSizer
*WidgetsPage::CreateSizerWithText(wxControl
*control
,
630 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
631 wxTextCtrl
*text
= new wxTextCtrl(this, id
, wxEmptyString
,
632 wxDefaultPosition
, wxDefaultSize
, wxTE_PROCESS_ENTER
);
634 sizerRow
->Add(control
, 0, wxRIGHT
| wxALIGN_CENTRE_VERTICAL
, 5);
635 sizerRow
->Add(text
, 1, wxLEFT
| wxALIGN_CENTRE_VERTICAL
, 5);
643 // create a sizer containing a label and a text ctrl
644 wxSizer
*WidgetsPage::CreateSizerWithTextAndLabel(const wxString
& label
,
648 return CreateSizerWithText(new wxStaticText(this, wxID_ANY
, label
),
652 // create a sizer containing a button and a text ctrl
653 wxSizer
*WidgetsPage::CreateSizerWithTextAndButton(wxWindowID idBtn
,
654 const wxString
& label
,
658 return CreateSizerWithText(new wxButton(this, idBtn
, label
), id
, ppText
);
661 wxCheckBox
*WidgetsPage::CreateCheckBoxAndAddToSizer(wxSizer
*sizer
,
662 const wxString
& label
,
665 wxCheckBox
*checkbox
= new wxCheckBox(this, id
, label
);
666 sizer
->Add(checkbox
, 0, wxLEFT
| wxRIGHT
, 5);
667 sizer
->Add(0, 2, 0, wxGROW
); // spacer