1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
3 // Name: samples/widgets/widgets.cpp
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"
40 #include "wx/msgdlg.h"
43 #include "wx/sysopt.h"
44 #include "wx/bookctrl.h"
45 #include "wx/treebook.h"
47 #include "wx/colordlg.h"
48 #include "wx/fontdlg.h"
49 #include "wx/textdlg.h"
53 #include "../sample.xpm"
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
62 Widgets_ClearLog
= 100,
69 #endif // wxUSE_TOOLTIPS
81 Widgets_BorderDefault
,
84 Widgets_GoToPageLast
= Widgets_GoToPage
+ 100
87 const wxChar
*WidgetsCategories
[MAX_PAGES
] = {
88 #if defined(__WXUNIVERSAL__)
102 // ----------------------------------------------------------------------------
104 // ----------------------------------------------------------------------------
106 // Define a new application type, each program should derive a class from wxApp
107 class WidgetsApp
: public wxApp
110 // override base class virtuals
111 // ----------------------------
113 // this one is called on application startup and is a good place for the app
114 // initialization (doing it here and not in the ctor allows to have an error
115 // return: if OnInit() returns false, the application terminates)
116 virtual bool OnInit();
119 // Define a new frame type: this is going to be our main frame
120 class WidgetsFrame
: public wxFrame
124 WidgetsFrame(const wxString
& title
);
125 virtual ~WidgetsFrame();
130 void OnButtonClearLog(wxCommandEvent
& event
);
132 void OnExit(wxCommandEvent
& event
);
135 void OnPageChanged(WidgetsBookCtrlEvent
& event
);
136 void OnGoToPage(wxCommandEvent
& event
);
139 void OnSetTooltip(wxCommandEvent
& event
);
140 #endif // wxUSE_TOOLTIPS
141 void OnSetFgCol(wxCommandEvent
& event
);
142 void OnSetBgCol(wxCommandEvent
& event
);
143 void OnSetFont(wxCommandEvent
& event
);
144 void OnEnable(wxCommandEvent
& event
);
145 void OnSetBorder(wxCommandEvent
& event
);
146 #endif // wxUSE_MENUS
148 // initialize the book: add all pages to it
151 // finding current page assuming book inside book
152 WidgetsPage
*CurrentPage();
155 // the panel containing everything
159 // the listbox for logging messages
160 wxListBox
*m_lboxLog
;
162 // the log target we use to redirect messages to the listbox
166 // the book containing the test pages
167 WidgetsBookCtrl
*m_book
;
170 // last chosen fg/bg colours and font
174 #endif // wxUSE_MENUS
176 // any class wishing to process wxWidgets events must use this macro
177 DECLARE_EVENT_TABLE()
181 // A log target which just redirects the messages to a listbox
182 class LboxLogger
: public wxLog
185 LboxLogger(wxListBox
*lbox
, wxLog
*logOld
)
188 //m_lbox->Disable(); -- looks ugly under MSW
192 virtual ~LboxLogger()
194 wxLog::SetActiveTarget(m_logOld
);
198 // implement sink functions
199 virtual void DoLog(wxLogLevel level
, const wxChar
*szString
, time_t t
)
201 // don't put trace messages into listbox or we can get into infinite
203 if ( level
== wxLOG_Trace
)
207 // cast is needed to call protected method
208 ((LboxLogger
*)m_logOld
)->DoLog(level
, szString
, t
);
213 wxLog::DoLog(level
, szString
, t
);
217 virtual void DoLogString(const wxChar
*szString
, time_t WXUNUSED(t
))
223 #ifdef __WXUNIVERSAL__
224 m_lbox
->AppendAndEnsureVisible(msg
);
225 #else // other ports don't have this method yet
227 m_lbox
->SetFirstItem(m_lbox
->GetCount() - 1);
231 // the control we use
234 // the old log target
240 WX_DEFINE_ARRAY_PTR(WidgetsPage
*, ArrayWidgetsPage
);
242 // ----------------------------------------------------------------------------
244 // ----------------------------------------------------------------------------
246 IMPLEMENT_APP(WidgetsApp
)
248 // ----------------------------------------------------------------------------
250 // ----------------------------------------------------------------------------
252 BEGIN_EVENT_TABLE(WidgetsFrame
, wxFrame
)
254 EVT_BUTTON(Widgets_ClearLog
, WidgetsFrame::OnButtonClearLog
)
256 EVT_BUTTON(Widgets_Quit
, WidgetsFrame::OnExit
)
259 EVT_MENU(Widgets_SetTooltip
, WidgetsFrame::OnSetTooltip
)
260 #endif // wxUSE_TOOLTIPS
263 EVT_WIDGETS_PAGE_CHANGED(wxID_ANY
, WidgetsFrame::OnPageChanged
)
264 EVT_MENU_RANGE(Widgets_GoToPage
, Widgets_GoToPageLast
,
265 WidgetsFrame::OnGoToPage
)
267 EVT_MENU(Widgets_SetFgColour
, WidgetsFrame::OnSetFgCol
)
268 EVT_MENU(Widgets_SetBgColour
, WidgetsFrame::OnSetBgCol
)
269 EVT_MENU(Widgets_SetFont
, WidgetsFrame::OnSetFont
)
270 EVT_MENU(Widgets_Enable
, WidgetsFrame::OnEnable
)
272 EVT_MENU_RANGE(Widgets_BorderNone
, Widgets_BorderDefault
,
273 WidgetsFrame::OnSetBorder
)
275 EVT_MENU(wxID_EXIT
, WidgetsFrame::OnExit
)
276 #endif // wxUSE_MENUS
279 // ============================================================================
281 // ============================================================================
283 // ----------------------------------------------------------------------------
285 // ----------------------------------------------------------------------------
287 bool WidgetsApp::OnInit()
289 if ( !wxApp::OnInit() )
292 // the reason for having these ifdef's is that I often run two copies of
293 // this sample side by side and it is useful to see which one is which
295 #if defined(__WXUNIVERSAL__)
296 title
= _T("wxUniv/");
299 #if defined(__WXMSW__)
300 title
+= _T("wxMSW");
301 #elif defined(__WXGTK__)
302 title
+= _T("wxGTK");
303 #elif defined(__WXMAC__)
304 title
+= _T("wxMAC");
305 #elif defined(__WXMOTIF__)
306 title
+= _T("wxMOTIF");
308 title
+= _T("wxWidgets");
311 wxFrame
*frame
= new WidgetsFrame(title
+ _T(" widgets demo"));
314 //wxLog::AddTraceMask(_T("listbox"));
315 //wxLog::AddTraceMask(_T("scrollbar"));
316 //wxLog::AddTraceMask(_T("focus"));
321 // ----------------------------------------------------------------------------
322 // WidgetsFrame construction
323 // ----------------------------------------------------------------------------
325 WidgetsFrame::WidgetsFrame(const wxString
& title
)
326 : wxFrame(NULL
, wxID_ANY
, title
,
327 wxPoint(0, 50), wxDefaultSize
,
328 wxDEFAULT_FRAME_STYLE
|
329 wxNO_FULL_REPAINT_ON_RESIZE
|
333 // set the frame icon
334 SetIcon(wxICON(sample
));
338 m_lboxLog
= (wxListBox
*)NULL
;
339 m_logTarget
= (wxLog
*)NULL
;
341 m_book
= (WidgetsBookCtrl
*)NULL
;
344 // create the menubar
345 wxMenuBar
*mbar
= new wxMenuBar
;
346 wxMenu
*menuWidget
= new wxMenu
;
348 menuWidget
->Append(Widgets_SetTooltip
, _T("Set &tooltip...\tCtrl-T"));
349 menuWidget
->AppendSeparator();
350 #endif // wxUSE_TOOLTIPS
351 menuWidget
->Append(Widgets_SetFgColour
, _T("Set &foreground...\tCtrl-F"));
352 menuWidget
->Append(Widgets_SetBgColour
, _T("Set &background...\tCtrl-B"));
353 menuWidget
->Append(Widgets_SetFont
, _T("Set f&ont...\tCtrl-O"));
354 menuWidget
->AppendCheckItem(Widgets_Enable
, _T("&Enable/disable\tCtrl-E"));
356 wxMenu
*menuBorders
= new wxMenu
;
357 menuBorders
->AppendRadioItem(Widgets_BorderDefault
, _T("De&fault\tCtrl-Shift-9"));
358 menuBorders
->AppendRadioItem(Widgets_BorderNone
, _T("&None\tCtrl-Shift-0"));
359 menuBorders
->AppendRadioItem(Widgets_BorderSimple
, _T("&Simple\tCtrl-Shift-1"));
360 menuBorders
->AppendRadioItem(Widgets_BorderDouble
, _T("&Double\tCtrl-Shift-2"));
361 menuBorders
->AppendRadioItem(Widgets_BorderStatic
, _T("Stati&c\tCtrl-Shift-3"));
362 menuBorders
->AppendRadioItem(Widgets_BorderRaised
, _T("&Raised\tCtrl-Shift-4"));
363 menuBorders
->AppendRadioItem(Widgets_BorderSunken
, _T("S&unken\tCtrl-Shift-5"));
364 menuWidget
->AppendSubMenu(menuBorders
, _T("Set &border"));
366 menuWidget
->AppendSeparator();
367 menuWidget
->Append(wxID_EXIT
, _T("&Quit\tCtrl-Q"));
368 mbar
->Append(menuWidget
, _T("&Widget"));
371 mbar
->Check(Widgets_Enable
, true);
372 #endif // wxUSE_MENUS
375 m_panel
= new wxPanel(this, wxID_ANY
,
376 wxDefaultPosition
, wxDefaultSize
, wxCLIP_CHILDREN
);
378 wxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
380 // we have 2 panes: book with pages demonstrating the controls in the
381 // upper one and the log window with some buttons in the lower
383 int style
= wxNO_FULL_REPAINT_ON_RESIZE
|wxCLIP_CHILDREN
|wxBK_DEFAULT
;
384 // Uncomment to suppress page theme (draw in solid colour)
385 //style |= wxNB_NOPAGETHEME;
387 m_book
= new WidgetsBookCtrl(m_panel
, Widgets_BookCtrl
, wxDefaultPosition
,
389 wxSize(500, wxDefaultCoord
), // under Motif, height is a function of the width...
396 #ifndef __SMARTPHONE__
397 // the lower one only has the log listbox and a button to clear it
399 wxSizer
*sizerDown
= new wxStaticBoxSizer(
400 new wxStaticBox( m_panel
, wxID_ANY
, _T("&Log window") ),
403 m_lboxLog
= new wxListBox(m_panel
, wxID_ANY
);
404 sizerDown
->Add(m_lboxLog
, 1, wxGROW
| wxALL
, 5);
405 sizerDown
->SetMinSize(100, 150);
407 wxSizer
*sizerDown
= new wxBoxSizer(wxVERTICAL
);
410 wxBoxSizer
*sizerBtns
= new wxBoxSizer(wxHORIZONTAL
);
413 btn
= new wxButton(m_panel
, Widgets_ClearLog
, _T("Clear &log"));
415 sizerBtns
->Add(10, 0); // spacer
417 btn
= new wxButton(m_panel
, Widgets_Quit
, _T("E&xit"));
419 sizerDown
->Add(sizerBtns
, 0, wxALL
| wxALIGN_RIGHT
, 5);
421 // put everything together
422 sizerTop
->Add(m_book
, 1, wxGROW
| (wxALL
& ~(wxTOP
| wxBOTTOM
)), 10);
423 sizerTop
->Add(0, 5, 0, wxGROW
); // spacer in between
424 sizerTop
->Add(sizerDown
, 0, wxGROW
| (wxALL
& ~wxTOP
), 10);
426 #else // !__SMARTPHONE__/__SMARTPHONE__
428 sizerTop
->Add(m_book
, 1, wxGROW
| wxALL
);
430 #endif // __SMARTPHONE__
432 m_panel
->SetSizer(sizerTop
);
435 sizerTop
->SetSizeHints(this);
437 #if USE_LOG && !defined(__WXCOCOA__)
438 // wxCocoa's listbox is too flakey to use for logging right now
439 // now that everything is created we can redirect the log messages to the
441 m_logTarget
= new LboxLogger(m_lboxLog
, wxLog::GetActiveTarget());
442 wxLog::SetActiveTarget(m_logTarget
);
446 void WidgetsFrame::InitBook()
448 wxImageList
*imageList
= new wxImageList(32, 32);
450 imageList
->Add(wxBitmap(sample_xpm
));
453 WidgetsBookCtrl
*books
[MAX_PAGES
];
456 ArrayWidgetsPage pages
[MAX_PAGES
];
457 wxArrayString labels
[MAX_PAGES
];
459 wxMenu
*menuPages
= new wxMenu
;
460 unsigned int nPage
= 0, nFKey
= 0;
461 int cat
, imageId
= 1;
463 // we need to first create all pages and only then add them to the book
464 // as we need the image list first
466 // we also construct the pages menu during this first iteration
467 for ( cat
= 0; cat
< MAX_PAGES
; cat
++ )
470 nPage
++; // increase for parent page
472 books
[cat
] = new WidgetsBookCtrl( m_book
, wxID_ANY
);
475 for ( WidgetsPageInfo
*info
= WidgetsPage::ms_widgetPages
;
477 info
= info
->GetNext() )
479 if( (info
->GetCategories() & ( 1 << cat
)) == 0)
482 WidgetsPage
*page
= (*info
->GetCtor())(
489 pages
[cat
].Add(page
);
491 labels
[cat
].Add(info
->GetLabel());
492 if ( cat
== ALL_PAGE
)
494 wxString
radioLabel(info
->GetLabel());
498 radioLabel
<< wxT("\tF" ) << nFKey
;
501 menuPages
->AppendRadioItem(
502 Widgets_GoToPage
+ nPage
,
506 // consider only for book in book architecture
512 // consider only for treebook architecture (with subpages)
518 GetMenuBar()->Append(menuPages
, _T("&Page"));
520 m_book
->AssignImageList(imageList
);
522 for ( cat
= 0; cat
< MAX_PAGES
; cat
++ )
525 m_book
->AddPage(NULL
,WidgetsCategories
[cat
],false,0);
527 m_book
->AddPage(books
[cat
],WidgetsCategories
[cat
],false,0);
528 books
[cat
]->SetImageList(imageList
);
532 size_t count
= pages
[cat
].GetCount();
533 for ( size_t n
= 0; n
< count
; n
++ )
542 false, // don't select
549 // for treebook page #0 is empty parent page only
550 m_book
->SetSelection(1);
551 m_book
->SetSelection(0);
555 WidgetsPage
*WidgetsFrame::CurrentPage()
558 return wxStaticCast(m_book
->GetCurrentPage(), WidgetsPage
);
560 WidgetsBookCtrl
*book
= wxStaticCast(m_book
->GetCurrentPage(), WidgetsBookCtrl
);
561 if (!book
) return NULL
;
562 return wxStaticCast(book
->GetCurrentPage(), WidgetsPage
);
566 WidgetsFrame::~WidgetsFrame()
573 // ----------------------------------------------------------------------------
574 // WidgetsFrame event handlers
575 // ----------------------------------------------------------------------------
577 void WidgetsFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
583 void WidgetsFrame::OnButtonClearLog(wxCommandEvent
& WXUNUSED(event
))
591 void WidgetsFrame::OnPageChanged(WidgetsBookCtrlEvent
& event
)
593 wxMenuItem
*item
= GetMenuBar()->FindItem(Widgets_GoToPage
+ event
.GetSelection());
594 if (item
) item
->Check();
598 void WidgetsFrame::OnGoToPage(wxCommandEvent
& event
)
601 m_book
->SetSelection(event
.GetId() - Widgets_GoToPage
);
603 m_book
->SetSelection(m_book
->GetPageCount()-1);
604 WidgetsBookCtrl
*book
= wxStaticCast(m_book
->GetCurrentPage(), WidgetsBookCtrl
);
605 book
->SetSelection(event
.GetId() - Widgets_GoToPage
);
611 void WidgetsFrame::OnSetTooltip(wxCommandEvent
& WXUNUSED(event
))
613 static wxString s_tip
= _T("This is a tooltip");
615 wxTextEntryDialog dialog
618 _T("Tooltip text (may use \\n, leave empty to remove): "),
619 _T("Widgets sample"),
623 if ( dialog
.ShowModal() != wxID_OK
)
626 s_tip
= dialog
.GetValue();
627 s_tip
.Replace(_T("\\n"), _T("\n"));
629 WidgetsPage
*page
= CurrentPage();
630 page
->GetWidget()->SetToolTip(s_tip
);
632 wxControl
*ctrl2
= page
->GetWidget2();
634 ctrl2
->SetToolTip(s_tip
);
637 #endif // wxUSE_TOOLTIPS
639 void WidgetsFrame::OnSetFgCol(wxCommandEvent
& WXUNUSED(event
))
642 // allow for debugging the default colour the first time this is called
643 WidgetsPage
*page
= CurrentPage();
645 m_colFg
= page
->GetForegroundColour();
647 wxColour col
= wxGetColourFromUser(this, m_colFg
);
653 page
->GetWidget()->SetForegroundColour(m_colFg
);
654 page
->GetWidget()->Refresh();
656 wxControl
*ctrl2
= page
->GetWidget2();
659 ctrl2
->SetForegroundColour(m_colFg
);
663 wxLogMessage(_T("Colour selection dialog not available in current build."));
667 void WidgetsFrame::OnSetBgCol(wxCommandEvent
& WXUNUSED(event
))
670 WidgetsPage
*page
= CurrentPage();
672 m_colBg
= page
->GetBackgroundColour();
674 wxColour col
= wxGetColourFromUser(this, m_colBg
);
680 page
->GetWidget()->SetBackgroundColour(m_colBg
);
681 page
->GetWidget()->Refresh();
683 wxControl
*ctrl2
= page
->GetWidget2();
686 ctrl2
->SetBackgroundColour(m_colFg
);
690 wxLogMessage(_T("Colour selection dialog not available in current build."));
694 void WidgetsFrame::OnSetFont(wxCommandEvent
& WXUNUSED(event
))
697 WidgetsPage
*page
= CurrentPage();
699 m_font
= page
->GetFont();
701 wxFont font
= wxGetFontFromUser(this, m_font
);
707 page
->GetWidget()->SetFont(m_font
);
708 page
->GetWidget()->Refresh();
710 wxControl
*ctrl2
= page
->GetWidget2();
713 ctrl2
->SetFont(m_font
);
717 wxLogMessage(_T("Font selection dialog not available in current build."));
721 void WidgetsFrame::OnEnable(wxCommandEvent
& event
)
723 WidgetsPage
*page
= CurrentPage();
724 page
->GetWidget()->Enable(event
.IsChecked());
727 void WidgetsFrame::OnSetBorder(wxCommandEvent
& event
)
730 switch ( event
.GetId() )
732 case Widgets_BorderNone
: border
= wxBORDER_NONE
; break;
733 case Widgets_BorderStatic
: border
= wxBORDER_STATIC
; break;
734 case Widgets_BorderSimple
: border
= wxBORDER_SIMPLE
; break;
735 case Widgets_BorderRaised
: border
= wxBORDER_RAISED
; break;
736 case Widgets_BorderSunken
: border
= wxBORDER_SUNKEN
; break;
737 case Widgets_BorderDouble
: border
= wxBORDER_DOUBLE
; break;
740 wxFAIL_MSG( _T("unknown border style") );
743 case Widgets_BorderDefault
: border
= wxBORDER_DEFAULT
; break;
746 WidgetsPage::ms_defaultFlags
&= ~wxBORDER_MASK
;
747 WidgetsPage::ms_defaultFlags
|= border
;
749 WidgetsPage
*page
= CurrentPage();
750 page
->RecreateWidget();
753 #endif // wxUSE_MENUS
755 // ----------------------------------------------------------------------------
757 // ----------------------------------------------------------------------------
759 WidgetsPageInfo::WidgetsPageInfo(Constructor ctor
, const wxChar
*label
, int categories
)
761 , m_categories(categories
)
767 // dummy sorting: add and immediately sort in the list according to label
768 if ( WidgetsPage::ms_widgetPages
)
770 WidgetsPageInfo
*node_prev
= WidgetsPage::ms_widgetPages
;
771 if ( wxStrcmp(label
, node_prev
->GetLabel().c_str()) < 0 )
775 WidgetsPage::ms_widgetPages
= this;
779 WidgetsPageInfo
*node_next
;
782 node_next
= node_prev
->GetNext();
785 // add if between two
786 if ( wxStrcmp(label
, node_next
->GetLabel().c_str()) < 0 )
788 node_prev
->SetNext(this);
790 // force to break loop
797 node_prev
->SetNext(this);
800 node_prev
= node_next
;
808 WidgetsPage::ms_widgetPages
= this;
812 // ----------------------------------------------------------------------------
814 // ----------------------------------------------------------------------------
816 int WidgetsPage::ms_defaultFlags
= wxBORDER_DEFAULT
;
817 WidgetsPageInfo
*WidgetsPage::ms_widgetPages
= NULL
;
819 WidgetsPage::WidgetsPage(WidgetsBookCtrl
*book
)
820 : wxPanel(book
, wxID_ANY
,
821 wxDefaultPosition
, wxDefaultSize
,
822 wxNO_FULL_REPAINT_ON_RESIZE
|
828 wxSizer
*WidgetsPage::CreateSizerWithText(wxControl
*control
,
832 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
833 wxTextCtrl
*text
= new wxTextCtrl(this, id
, wxEmptyString
,
834 wxDefaultPosition
, wxDefaultSize
, wxTE_PROCESS_ENTER
);
836 sizerRow
->Add(control
, 0, wxRIGHT
| wxALIGN_CENTRE_VERTICAL
, 5);
837 sizerRow
->Add(text
, 1, wxLEFT
| wxALIGN_CENTRE_VERTICAL
, 5);
845 // create a sizer containing a label and a text ctrl
846 wxSizer
*WidgetsPage::CreateSizerWithTextAndLabel(const wxString
& label
,
850 return CreateSizerWithText(new wxStaticText(this, wxID_ANY
, label
),
854 // create a sizer containing a button and a text ctrl
855 wxSizer
*WidgetsPage::CreateSizerWithTextAndButton(wxWindowID idBtn
,
856 const wxString
& label
,
860 return CreateSizerWithText(new wxButton(this, idBtn
, label
), id
, ppText
);
863 wxCheckBox
*WidgetsPage::CreateCheckBoxAndAddToSizer(wxSizer
*sizer
,
864 const wxString
& label
,
867 wxCheckBox
*checkbox
= new wxCheckBox(this, id
, label
);
868 sizer
->Add(checkbox
, 0, wxLEFT
| wxRIGHT
, 5);
869 sizer
->Add(0, 2, 0, wxGROW
); // spacer