]> git.saurik.com Git - wxWidgets.git/blob - samples/widgets/widgets.cpp
Fixed HitTest for columns > 1
[wxWidgets.git] / samples / widgets / widgets.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
3 // Name: widgets.cpp
4 // Purpose: Sample showing most of the simple wxWidgets 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"
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
50 enum
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
61 class WidgetsApp : public wxApp
62 {
63 public:
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
74 class WidgetsFrame : public wxFrame
75 {
76 public:
77 // ctor(s) and dtor
78 WidgetsFrame(const wxString& title);
79 virtual ~WidgetsFrame();
80
81 protected:
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
89 private:
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 wxWidgets events must use this macro
106 DECLARE_EVENT_TABLE()
107 };
108
109 // A log target which just redirects the messages to a listbox
110 class LboxLogger : public wxLog
111 {
112 public:
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
125 private:
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
145 virtual void DoLogString(const wxChar *szString, time_t WXUNUSED(t))
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
167 WX_DEFINE_ARRAY_PTR(WidgetsPage *, ArrayWidgetsPage);
168
169 // ----------------------------------------------------------------------------
170 // misc macros
171 // ----------------------------------------------------------------------------
172
173 IMPLEMENT_APP(WidgetsApp)
174
175 // ----------------------------------------------------------------------------
176 // event tables
177 // ----------------------------------------------------------------------------
178
179 BEGIN_EVENT_TABLE(WidgetsFrame, wxFrame)
180 EVT_BUTTON(Widgets_ClearLog, WidgetsFrame::OnButtonClearLog)
181 EVT_BUTTON(Widgets_Quit, WidgetsFrame::OnButtonQuit)
182 END_EVENT_TABLE()
183
184 // ============================================================================
185 // implementation
186 // ============================================================================
187
188 // ----------------------------------------------------------------------------
189 // app class
190 // ----------------------------------------------------------------------------
191
192 bool WidgetsApp::OnInit()
193 {
194 if ( !wxApp::OnInit() )
195 return false;
196
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
199 wxString title;
200 #if defined(__WXUNIVERSAL__)
201 title = _T("wxUniv/");
202 #endif
203
204 #if defined(__WXMSW__)
205 title += _T("wxMSW");
206 #elif defined(__WXGTK__)
207 title += _T("wxGTK");
208 #elif defined(__WXMAC__)
209 title += _T("wxMAC");
210 #elif defined(__WXMOTIF__)
211 title += _T("wxMOTIF");
212 #else
213 title += _T("wxWidgets");
214 #endif
215
216 wxFrame *frame = new WidgetsFrame(title + _T(" widgets demo"));
217 frame->Show();
218
219 //wxLog::AddTraceMask(_T("listbox"));
220 //wxLog::AddTraceMask(_T("scrollbar"));
221 //wxLog::AddTraceMask(_T("focus"));
222
223 return true;
224 }
225
226 // ----------------------------------------------------------------------------
227 // WidgetsFrame construction
228 // ----------------------------------------------------------------------------
229
230 WidgetsFrame::WidgetsFrame(const wxString& title)
231 : wxFrame(NULL, wxID_ANY, title,
232 wxPoint(0, 50), wxDefaultSize,
233 wxDEFAULT_FRAME_STYLE |
234 wxNO_FULL_REPAINT_ON_RESIZE |
235 wxCLIP_CHILDREN |
236 wxTAB_TRAVERSAL)
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
245 m_panel = new wxPanel(this, wxID_ANY,
246 wxDefaultPosition, wxDefaultSize, wxCLIP_CHILDREN);
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
253 m_notebook = new wxNotebook(m_panel, wxID_ANY, wxDefaultPosition,
254 wxDefaultSize, wxNO_FULL_REPAINT_ON_RESIZE|wxCLIP_CHILDREN);
255 InitNotebook();
256
257 // the lower one only has the log listbox and a button to clear it
258 wxSizer *sizerDown = new wxStaticBoxSizer(
259 new wxStaticBox( m_panel, wxID_ANY, _T("&Log window") ),
260 wxVERTICAL);
261
262 m_lboxLog = new wxListBox(m_panel, wxID_ANY);
263 sizerDown->Add(m_lboxLog, 1, wxGROW | wxALL, 5);
264 sizerDown->SetMinSize(100, 150);
265
266 wxBoxSizer *sizerBtns = new wxBoxSizer(wxHORIZONTAL);
267 wxButton *btn = new wxButton(m_panel, Widgets_ClearLog, _T("Clear &log"));
268 sizerBtns->Add(btn);
269 sizerBtns->Add(10, 0); // spacer
270 btn = new wxButton(m_panel, Widgets_Quit, _T("E&xit"));
271 sizerBtns->Add(btn);
272 sizerDown->Add(sizerBtns, 0, wxALL | wxALIGN_RIGHT, 5);
273
274 // put everything together
275 sizerTop->Add(m_notebook, 1, wxGROW | (wxALL & ~(wxTOP | wxBOTTOM)), 10);
276 sizerTop->Add(0, 5, 0, wxGROW); // spacer in between
277 sizerTop->Add(sizerDown, 0, wxGROW | (wxALL & ~wxTOP), 10);
278
279 m_panel->SetSizer(sizerTop);
280
281 sizerTop->Fit(this);
282 sizerTop->SetSizeHints(this);
283
284 // wxCocoa's listbox is too flakey to use for logging right now
285 #if !defined(__WXCOCOA__)
286 // now that everything is created we can redirect the log messages to the
287 // listbox
288 m_logTarget = new LboxLogger(m_lboxLog, wxLog::GetActiveTarget());
289 wxLog::SetActiveTarget(m_logTarget);
290 #endif
291 }
292
293 void WidgetsFrame::InitNotebook()
294 {
295 m_imaglist = new wxImageList(32, 32);
296
297 ArrayWidgetsPage pages;
298 wxArrayString labels;
299
300 // we need to first create all pages and only then add them to the notebook
301 // as we need the image list first
302 WidgetsPageInfo *info = WidgetsPage::ms_widgetPages;
303 while ( info )
304 {
305 WidgetsPage *page = (*info->GetCtor())(m_notebook, m_imaglist);
306 pages.Add(page);
307
308 labels.Add(info->GetLabel());
309
310 info = info->GetNext();
311 }
312
313 m_notebook->SetImageList(m_imaglist);
314
315 // now do add them
316 size_t count = pages.GetCount();
317 for ( size_t n = 0; n < count; n++ )
318 {
319 m_notebook->AddPage(
320 pages[n],
321 labels[n],
322 false, // don't select
323 n // image id
324 );
325 }
326 }
327
328 WidgetsFrame::~WidgetsFrame()
329 {
330 delete m_logTarget;
331 delete m_imaglist;
332 }
333
334 // ----------------------------------------------------------------------------
335 // WidgetsFrame event handlers
336 // ----------------------------------------------------------------------------
337
338 void WidgetsFrame::OnButtonQuit(wxCommandEvent& WXUNUSED(event))
339 {
340 Close();
341 }
342
343 void WidgetsFrame::OnButtonClearLog(wxCommandEvent& WXUNUSED(event))
344 {
345 m_lboxLog->Clear();
346 }
347
348 // ----------------------------------------------------------------------------
349 // WidgetsPageInfo
350 // ----------------------------------------------------------------------------
351
352 WidgetsPageInfo *WidgetsPage::ms_widgetPages = NULL;
353
354 WidgetsPageInfo::WidgetsPageInfo(Constructor ctor, const wxChar *label)
355 : m_label(label)
356 {
357 m_ctor = ctor;
358
359 m_next = NULL;
360
361 // dummy sorting: add and immediately sort on list according to label
362
363 if(WidgetsPage::ms_widgetPages)
364 {
365 WidgetsPageInfo *node_prev = WidgetsPage::ms_widgetPages;
366 if(wxStrcmp(label,node_prev->GetLabel().c_str())<0)
367 {
368 // add as first
369 m_next = node_prev;
370 WidgetsPage::ms_widgetPages = this;
371 }
372 else
373 {
374 WidgetsPageInfo *node_next;
375 do
376 {
377 node_next = node_prev->GetNext();
378 if(node_next)
379 {
380 // add if between two
381 if(wxStrcmp(label,node_next->GetLabel().c_str())<0)
382 {
383 node_prev->SetNext(this);
384 m_next = node_next;
385 // force to break loop
386 node_next = NULL;
387 }
388 }
389 else
390 {
391 // add as last
392 node_prev->SetNext(this);
393 m_next = node_next;
394 }
395 node_prev = node_next;
396 }while(node_next);
397 }
398 }
399 else
400 {
401 // add when first
402
403 WidgetsPage::ms_widgetPages = this;
404
405 }
406
407 }
408
409 // ----------------------------------------------------------------------------
410 // WidgetsPage
411 // ----------------------------------------------------------------------------
412
413 WidgetsPage::WidgetsPage(wxNotebook *notebook)
414 : wxPanel(notebook, wxID_ANY,
415 wxDefaultPosition, wxDefaultSize,
416 wxNO_FULL_REPAINT_ON_RESIZE |
417 wxCLIP_CHILDREN |
418 wxTAB_TRAVERSAL)
419 {
420 }
421
422 wxSizer *WidgetsPage::CreateSizerWithText(wxControl *control,
423 wxWindowID id,
424 wxTextCtrl **ppText)
425 {
426 wxSizer *sizerRow = new wxBoxSizer(wxHORIZONTAL);
427 wxTextCtrl *text = new wxTextCtrl(this, id, wxEmptyString,
428 wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER);
429
430 sizerRow->Add(control, 0, wxRIGHT | wxALIGN_CENTRE_VERTICAL, 5);
431 sizerRow->Add(text, 1, wxLEFT | wxALIGN_CENTRE_VERTICAL, 5);
432
433 if ( ppText )
434 *ppText = text;
435
436 return sizerRow;
437 }
438
439 // create a sizer containing a label and a text ctrl
440 wxSizer *WidgetsPage::CreateSizerWithTextAndLabel(const wxString& label,
441 wxWindowID id,
442 wxTextCtrl **ppText)
443 {
444 return CreateSizerWithText(new wxStaticText(this, wxID_ANY, label),
445 id, ppText);
446 }
447
448 // create a sizer containing a button and a text ctrl
449 wxSizer *WidgetsPage::CreateSizerWithTextAndButton(wxWindowID idBtn,
450 const wxString& label,
451 wxWindowID id,
452 wxTextCtrl **ppText)
453 {
454 return CreateSizerWithText(new wxButton(this, idBtn, label), id, ppText);
455 }
456
457 wxCheckBox *WidgetsPage::CreateCheckBoxAndAddToSizer(wxSizer *sizer,
458 const wxString& label,
459 wxWindowID id)
460 {
461 wxCheckBox *checkbox = new wxCheckBox(this, id, label);
462 sizer->Add(checkbox, 0, wxLEFT | wxRIGHT, 5);
463 sizer->Add(0, 2, 0, wxGROW); // spacer
464
465 return checkbox;
466 }
467