]> git.saurik.com Git - wxWidgets.git/blame - samples/widgets/widgets.cpp
Updates to MIME-types and wxFileDialog code for better
[wxWidgets.git] / samples / widgets / widgets.cpp
CommitLineData
32b8ec41
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Program: wxWindows Widgets Sample
3// Name: widgets.cpp
4// Purpose: Sample showing most of the simple wxWindows widgets
5// Author: Vadim Zeitlin
6// Created: 27.03.01
7// Id: $Id$
8// Copyright: (c) 2001 Vadim Zeitlin
9// License: wxWindows license
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20// for compilers that support precompilation, includes "wx/wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
24 #pragma hdrstop
25#endif
26
27// for all others, include the necessary headers
28#ifndef WX_PRECOMP
29 #include "wx/app.h"
30 #include "wx/log.h"
32b8ec41
VZ
31 #include "wx/frame.h"
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"
38#endif
39
40#include "wx/notebook.h"
41#include "wx/sizer.h"
42
43#include "widgets.h"
44
45// ----------------------------------------------------------------------------
46// constants
47// ----------------------------------------------------------------------------
48
49// control ids
50enum
51{
52 Widgets_ClearLog = 100,
53 Widgets_Quit
54};
55
56// ----------------------------------------------------------------------------
57// our classes
58// ----------------------------------------------------------------------------
59
60// Define a new application type, each program should derive a class from wxApp
61class WidgetsApp : public wxApp
62{
63public:
64 // override base class virtuals
65 // ----------------------------
66
67 // this one is called on application startup and is a good place for the app
68 // initialization (doing it here and not in the ctor allows to have an error
69 // return: if OnInit() returns false, the application terminates)
70 virtual bool OnInit();
71};
72
73// Define a new frame type: this is going to be our main frame
74class WidgetsFrame : public wxFrame
75{
76public:
77 // ctor(s) and dtor
78 WidgetsFrame(const wxString& title);
79 virtual ~WidgetsFrame();
80
81protected:
82 // event handlers
83 void OnButtonClearLog(wxCommandEvent& event);
84 void OnButtonQuit(wxCommandEvent& event);
85
86 // initialize the notebook: add all pages to it
87 void InitNotebook();
88
89private:
90 // the panel containing everything
91 wxPanel *m_panel;
92
93 // the listbox for logging messages
94 wxListBox *m_lboxLog;
95
96 // the log target we use to redirect messages to the listbox
97 wxLog *m_logTarget;
98
99 // the notebook containing the test pages
100 wxNotebook *m_notebook;
101
102 // and the image list for it
103 wxImageList *m_imaglist;
104
105 // any class wishing to process wxWindows events must use this macro
106 DECLARE_EVENT_TABLE()
107};
108
109// A log target which just redirects the messages to a listbox
110class LboxLogger : public wxLog
111{
112public:
113 LboxLogger(wxListBox *lbox, wxLog *logOld)
114 {
115 m_lbox = lbox;
116 //m_lbox->Disable(); -- looks ugly under MSW
117 m_logOld = logOld;
118 }
119
120 virtual ~LboxLogger()
121 {
122 wxLog::SetActiveTarget(m_logOld);
123 }
124
125private:
126 // implement sink functions
127 virtual void DoLog(wxLogLevel level, const wxChar *szString, time_t t)
128 {
129 // don't put trace messages into listbox or we can get into infinite
130 // recursion
131 if ( level == wxLOG_Trace )
132 {
133 if ( m_logOld )
134 {
135 // cast is needed to call protected method
136 ((LboxLogger *)m_logOld)->DoLog(level, szString, t);
137 }
138 }
139 else
140 {
141 wxLog::DoLog(level, szString, t);
142 }
143 }
144
c02e5a31 145 virtual void DoLogString(const wxChar *szString, time_t WXUNUSED(t))
32b8ec41
VZ
146 {
147 wxString msg;
148 TimeStamp(&msg);
149 msg += szString;
150
151 #ifdef __WXUNIVERSAL__
152 m_lbox->AppendAndEnsureVisible(msg);
153 #else // other ports don't have this method yet
154 m_lbox->Append(msg);
155 m_lbox->SetFirstItem(m_lbox->GetCount() - 1);
156 #endif
157 }
158
159 // the control we use
160 wxListBox *m_lbox;
161
162 // the old log target
163 wxLog *m_logOld;
164};
165
166// array of pages
38d6b957 167WX_DEFINE_ARRAY_PTR(WidgetsPage *, ArrayWidgetsPage);
32b8ec41
VZ
168
169// ----------------------------------------------------------------------------
170// misc macros
171// ----------------------------------------------------------------------------
172
173IMPLEMENT_APP(WidgetsApp)
174
32b8ec41
VZ
175// ----------------------------------------------------------------------------
176// event tables
177// ----------------------------------------------------------------------------
178
179BEGIN_EVENT_TABLE(WidgetsFrame, wxFrame)
180 EVT_BUTTON(Widgets_ClearLog, WidgetsFrame::OnButtonClearLog)
181 EVT_BUTTON(Widgets_Quit, WidgetsFrame::OnButtonQuit)
182END_EVENT_TABLE()
183
184// ============================================================================
185// implementation
186// ============================================================================
187
188// ----------------------------------------------------------------------------
189// app class
190// ----------------------------------------------------------------------------
191
192bool WidgetsApp::OnInit()
193{
bf188f1a 194 if ( !wxApp::OnInit() )
206d3a16 195 return false;
bf188f1a 196
32b8ec41
VZ
197 // the reason for having these ifdef's is that I often run two copies of
198 // this sample side by side and it is useful to see which one is which
24e78d27 199 wxString title;
32b8ec41 200#if defined(__WXUNIVERSAL__)
085a1f3c 201 title = _T("wxUniv/");
24e78d27
VZ
202#endif
203
204#if defined(__WXMSW__)
205 title += _T("wxMSW");
32b8ec41 206#elif defined(__WXGTK__)
24e78d27 207 title += _T("wxGTK");
0f9390c3
GD
208#elif defined(__WXMAC__)
209 title += _T("wxMAC");
210#elif defined(__WXMOTIF__)
211 title += _T("wxMOTIF");
32b8ec41 212#else
24e78d27 213 title += _T("wxWindows");
32b8ec41 214#endif
32b8ec41
VZ
215
216 wxFrame *frame = new WidgetsFrame(title + _T(" widgets demo"));
217 frame->Show();
218
219 //wxLog::AddTraceMask(_T("listbox"));
220 //wxLog::AddTraceMask(_T("scrollbar"));
58ec2255 221 //wxLog::AddTraceMask(_T("focus"));
32b8ec41 222
206d3a16 223 return true;
32b8ec41
VZ
224}
225
226// ----------------------------------------------------------------------------
227// WidgetsFrame construction
228// ----------------------------------------------------------------------------
229
230WidgetsFrame::WidgetsFrame(const wxString& title)
206d3a16 231 : wxFrame(NULL, wxID_ANY, title,
df0787b6
VZ
232 wxPoint(0, 50), wxDefaultSize,
233 wxDEFAULT_FRAME_STYLE |
234 wxNO_FULL_REPAINT_ON_RESIZE |
235 wxCLIP_CHILDREN |
236 wxTAB_TRAVERSAL)
32b8ec41
VZ
237{
238 // init everything
239 m_lboxLog = (wxListBox *)NULL;
240 m_logTarget = (wxLog *)NULL;
241 m_notebook = (wxNotebook *)NULL;
242 m_imaglist = (wxImageList *)NULL;
243
244 // create controls
206d3a16
JS
245 m_panel = new wxPanel(this, wxID_ANY,
246 wxDefaultPosition, wxDefaultSize, wxCLIP_CHILDREN);
32b8ec41
VZ
247
248 wxSizer *sizerTop = new wxBoxSizer(wxVERTICAL);
249
250 // we have 2 panes: notebook which pages demonstrating the controls in the
251 // upper one and the log window with some buttons in the lower
252
206d3a16
JS
253 m_notebook = new wxNotebook(m_panel, wxID_ANY, wxDefaultPosition,
254 wxDefaultSize, wxNO_FULL_REPAINT_ON_RESIZE|wxCLIP_CHILDREN);
32b8ec41
VZ
255 InitNotebook();
256 wxSizer *sizerUp = new wxNotebookSizer(m_notebook);
257
258 // the lower one only has the log listbox and a button to clear it
206d3a16
JS
259 wxSizer *sizerDown = new wxStaticBoxSizer(
260 new wxStaticBox( m_panel, wxID_ANY, _T("&Log window") ),
261 wxVERTICAL);
262
263 m_lboxLog = new wxListBox(m_panel, wxID_ANY);
32b8ec41 264 sizerDown->Add(m_lboxLog, 1, wxGROW | wxALL, 5);
7b127900 265 sizerDown->SetMinSize(100, 150);
88633ca7 266
32b8ec41
VZ
267 wxBoxSizer *sizerBtns = new wxBoxSizer(wxHORIZONTAL);
268 wxButton *btn = new wxButton(m_panel, Widgets_ClearLog, _T("Clear &log"));
269 sizerBtns->Add(btn);
270 sizerBtns->Add(10, 0); // spacer
271 btn = new wxButton(m_panel, Widgets_Quit, _T("E&xit"));
272 sizerBtns->Add(btn);
273 sizerDown->Add(sizerBtns, 0, wxALL | wxALIGN_RIGHT, 5);
274
275 // put everything together
276 sizerTop->Add(sizerUp, 1, wxGROW | (wxALL & ~(wxTOP | wxBOTTOM)), 10);
277 sizerTop->Add(0, 5, 0, wxGROW); // spacer in between
278 sizerTop->Add(sizerDown, 0, wxGROW | (wxALL & ~wxTOP), 10);
279
32b8ec41
VZ
280 m_panel->SetSizer(sizerTop);
281
282 sizerTop->Fit(this);
283 sizerTop->SetSizeHints(this);
284
7bb70733
DE
285 // wxCocoa's listbox is too flakey to use for logging right now
286 #if !defined(__WXCOCOA__)
32b8ec41
VZ
287 // now that everything is created we can redirect the log messages to the
288 // listbox
289 m_logTarget = new LboxLogger(m_lboxLog, wxLog::GetActiveTarget());
290 wxLog::SetActiveTarget(m_logTarget);
7bb70733 291 #endif
32b8ec41
VZ
292}
293
294void WidgetsFrame::InitNotebook()
295{
296 m_imaglist = new wxImageList(32, 32);
297
298 ArrayWidgetsPage pages;
299 wxArrayString labels;
300
301 // we need to first create all pages and only then add them to the notebook
302 // as we need the image list first
303 WidgetsPageInfo *info = WidgetsPage::ms_widgetPages;
304 while ( info )
305 {
306 WidgetsPage *page = (*info->GetCtor())(m_notebook, m_imaglist);
307 pages.Add(page);
308
309 labels.Add(info->GetLabel());
310
311 info = info->GetNext();
312 }
313
314 m_notebook->SetImageList(m_imaglist);
315
316 // now do add them
317 size_t count = pages.GetCount();
318 for ( size_t n = 0; n < count; n++ )
319 {
320 m_notebook->AddPage(
321 pages[n],
322 labels[n],
206d3a16 323 false, // don't select
32b8ec41
VZ
324 n // image id
325 );
326 }
327}
328
329WidgetsFrame::~WidgetsFrame()
330{
331 delete m_logTarget;
332 delete m_imaglist;
333}
334
335// ----------------------------------------------------------------------------
336// WidgetsFrame event handlers
337// ----------------------------------------------------------------------------
338
339void WidgetsFrame::OnButtonQuit(wxCommandEvent& WXUNUSED(event))
340{
341 Close();
342}
343
c02e5a31 344void WidgetsFrame::OnButtonClearLog(wxCommandEvent& WXUNUSED(event))
32b8ec41
VZ
345{
346 m_lboxLog->Clear();
347}
348
349// ----------------------------------------------------------------------------
350// WidgetsPageInfo
351// ----------------------------------------------------------------------------
352
353WidgetsPageInfo *WidgetsPage::ms_widgetPages = NULL;
354
355WidgetsPageInfo::WidgetsPageInfo(Constructor ctor, const wxChar *label)
356 : m_label(label)
357{
358 m_ctor = ctor;
359
2673bcb0
DS
360 m_next = NULL;
361
362 // dummy sorting: add and immediately sort on list according to label
363
364 if(WidgetsPage::ms_widgetPages)
365 {
366 WidgetsPageInfo *node_prev = WidgetsPage::ms_widgetPages;
367 if(wxStrcmp(label,node_prev->GetLabel().c_str())<0)
368 {
369 // add as first
370 m_next = node_prev;
371 WidgetsPage::ms_widgetPages = this;
372 }
373 else
374 {
375 WidgetsPageInfo *node_next;
376 do
377 {
378 node_next = node_prev->GetNext();
379 if(node_next)
380 {
381 // add if between two
382 if(wxStrcmp(label,node_next->GetLabel().c_str())<0)
383 {
384 node_prev->SetNext(this);
385 m_next = node_next;
386 // force to break loop
387 node_next = NULL;
388 }
389 }
390 else
391 {
392 // add as last
393 node_prev->SetNext(this);
394 m_next = node_next;
395 }
396 node_prev = node_next;
397 }while(node_next);
398 }
399 }
400 else
401 {
402 // add when first
403
404 WidgetsPage::ms_widgetPages = this;
405
406 }
407
32b8ec41
VZ
408}
409
410// ----------------------------------------------------------------------------
411// WidgetsPage
412// ----------------------------------------------------------------------------
413
414WidgetsPage::WidgetsPage(wxNotebook *notebook)
206d3a16 415 : wxPanel(notebook, wxID_ANY,
df0787b6
VZ
416 wxDefaultPosition, wxDefaultSize,
417 wxNO_FULL_REPAINT_ON_RESIZE |
418 wxCLIP_CHILDREN |
419 wxTAB_TRAVERSAL)
32b8ec41
VZ
420{
421}
422
423wxSizer *WidgetsPage::CreateSizerWithText(wxControl *control,
424 wxWindowID id,
425 wxTextCtrl **ppText)
426{
427 wxSizer *sizerRow = new wxBoxSizer(wxHORIZONTAL);
206d3a16
JS
428 wxTextCtrl *text = new wxTextCtrl(this, id, wxEmptyString,
429 wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER);
4589ec39 430
32b8ec41
VZ
431 sizerRow->Add(control, 0, wxRIGHT | wxALIGN_CENTRE_VERTICAL, 5);
432 sizerRow->Add(text, 1, wxLEFT | wxALIGN_CENTRE_VERTICAL, 5);
433
434 if ( ppText )
435 *ppText = text;
436
437 return sizerRow;
438}
439
440// create a sizer containing a label and a text ctrl
441wxSizer *WidgetsPage::CreateSizerWithTextAndLabel(const wxString& label,
442 wxWindowID id,
443 wxTextCtrl **ppText)
444{
206d3a16
JS
445 return CreateSizerWithText(new wxStaticText(this, wxID_ANY, label),
446 id, ppText);
32b8ec41
VZ
447}
448
449// create a sizer containing a button and a text ctrl
450wxSizer *WidgetsPage::CreateSizerWithTextAndButton(wxWindowID idBtn,
451 const wxString& label,
452 wxWindowID id,
453 wxTextCtrl **ppText)
454{
455 return CreateSizerWithText(new wxButton(this, idBtn, label), id, ppText);
456}
457
458wxCheckBox *WidgetsPage::CreateCheckBoxAndAddToSizer(wxSizer *sizer,
459 const wxString& label,
460 wxWindowID id)
461{
462 wxCheckBox *checkbox = new wxCheckBox(this, id, label);
463 sizer->Add(checkbox, 0, wxLEFT | wxRIGHT, 5);
464 sizer->Add(0, 2, 0, wxGROW); // spacer
465
466 return checkbox;
467}
468