]> git.saurik.com Git - wxWidgets.git/blame - samples/widgets/combobox.cpp
Moved Clear() implementation into wxDC using a new virtual CocoaGetBounds()
[wxWidgets.git] / samples / widgets / combobox.cpp
CommitLineData
32b8ec41 1/////////////////////////////////////////////////////////////////////////////
be5a51fb 2// Program: wxWidgets Widgets Sample
32b8ec41
VZ
3// Name: combobox.cpp
4// Purpose: Part of the widgets sample showing wxComboBox
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
3379ed37
VZ
27#if wxUSE_COMBOBOX
28
32b8ec41
VZ
29// for all others, include the necessary headers
30#ifndef WX_PRECOMP
31 #include "wx/log.h"
32
3bb70c40 33 #include "wx/bitmap.h"
32b8ec41
VZ
34 #include "wx/button.h"
35 #include "wx/checkbox.h"
36 #include "wx/combobox.h"
37 #include "wx/radiobox.h"
38 #include "wx/statbox.h"
39 #include "wx/textctrl.h"
40#endif
41
42#include "wx/sizer.h"
43
44#include "widgets.h"
7b127900 45#if 1
32b8ec41
VZ
46#include "icons/combobox.xpm"
47
48// ----------------------------------------------------------------------------
49// constants
50// ----------------------------------------------------------------------------
51
52// control ids
53enum
54{
55 ComboPage_Reset = 100,
56 ComboPage_CurText,
243dbf1a
VZ
57 ComboPage_Insert,
58 ComboPage_InsertText,
32b8ec41
VZ
59 ComboPage_Add,
60 ComboPage_AddText,
61 ComboPage_AddSeveral,
62 ComboPage_AddMany,
63 ComboPage_Clear,
64 ComboPage_Change,
65 ComboPage_ChangeText,
66 ComboPage_Delete,
67 ComboPage_DeleteText,
68 ComboPage_DeleteSel,
69 ComboPage_Combo
70};
71
72// kinds of comboboxes
73enum
74{
75 ComboKind_Default,
76 ComboKind_Simple,
77 ComboKind_DropDown
78};
79
80// ----------------------------------------------------------------------------
81// ComboboxWidgetsPage
82// ----------------------------------------------------------------------------
83
84class ComboboxWidgetsPage : public WidgetsPage
85{
86public:
61c083e7 87 ComboboxWidgetsPage(wxBookCtrl *book, wxImageList *imaglist);
32b8ec41 88
195df7a7
VZ
89 virtual wxControl *GetWidget() const { return m_combobox; }
90
32b8ec41
VZ
91protected:
92 // event handlers
93 void OnButtonReset(wxCommandEvent& event);
94 void OnButtonChange(wxCommandEvent& event);
95 void OnButtonDelete(wxCommandEvent& event);
96 void OnButtonDeleteSel(wxCommandEvent& event);
97 void OnButtonClear(wxCommandEvent& event);
243dbf1a 98 void OnButtonInsert(wxCommandEvent &event);
32b8ec41
VZ
99 void OnButtonAdd(wxCommandEvent& event);
100 void OnButtonAddSeveral(wxCommandEvent& event);
101 void OnButtonAddMany(wxCommandEvent& event);
102
103 void OnComboBox(wxCommandEvent& event);
104 void OnComboText(wxCommandEvent& event);
105
106 void OnCheckOrRadioBox(wxCommandEvent& event);
107
108 void OnUpdateUICurText(wxUpdateUIEvent& event);
109
243dbf1a 110 void OnUpdateUIInsert(wxUpdateUIEvent& event);
32b8ec41
VZ
111 void OnUpdateUIAddSeveral(wxUpdateUIEvent& event);
112 void OnUpdateUIClearButton(wxUpdateUIEvent& event);
113 void OnUpdateUIDeleteButton(wxUpdateUIEvent& event);
114 void OnUpdateUIDeleteSelButton(wxUpdateUIEvent& event);
115 void OnUpdateUIResetButton(wxUpdateUIEvent& event);
116
117 // reset the combobox parameters
118 void Reset();
119
120 // (re)create the combobox
121 void CreateCombo();
122
123 // the controls
124 // ------------
125
126 // the sel mode radiobox
127 wxRadioBox *m_radioKind;
128
129 // the checkboxes for styles
130 wxCheckBox *m_chkSort,
131 *m_chkReadonly;
132
133 // the combobox itself and the sizer it is in
134 wxComboBox *m_combobox;
135 wxSizer *m_sizerCombo;
136
137 // the text entries for "Add/change string" and "Delete" buttons
243dbf1a
VZ
138 wxTextCtrl *m_textInsert,
139 *m_textAdd,
32b8ec41
VZ
140 *m_textChange,
141 *m_textDelete;
142
143private:
5e173f35
GD
144 DECLARE_EVENT_TABLE()
145 DECLARE_WIDGETS_PAGE(ComboboxWidgetsPage)
32b8ec41
VZ
146};
147
148// ----------------------------------------------------------------------------
149// event tables
150// ----------------------------------------------------------------------------
151
152BEGIN_EVENT_TABLE(ComboboxWidgetsPage, WidgetsPage)
153 EVT_BUTTON(ComboPage_Reset, ComboboxWidgetsPage::OnButtonReset)
154 EVT_BUTTON(ComboPage_Change, ComboboxWidgetsPage::OnButtonChange)
155 EVT_BUTTON(ComboPage_Delete, ComboboxWidgetsPage::OnButtonDelete)
156 EVT_BUTTON(ComboPage_DeleteSel, ComboboxWidgetsPage::OnButtonDeleteSel)
157 EVT_BUTTON(ComboPage_Clear, ComboboxWidgetsPage::OnButtonClear)
243dbf1a 158 EVT_BUTTON(ComboPage_Insert, ComboboxWidgetsPage::OnButtonInsert)
32b8ec41
VZ
159 EVT_BUTTON(ComboPage_Add, ComboboxWidgetsPage::OnButtonAdd)
160 EVT_BUTTON(ComboPage_AddSeveral, ComboboxWidgetsPage::OnButtonAddSeveral)
161 EVT_BUTTON(ComboPage_AddMany, ComboboxWidgetsPage::OnButtonAddMany)
162
243dbf1a 163 EVT_TEXT_ENTER(ComboPage_InsertText, ComboboxWidgetsPage::OnButtonInsert)
32b8ec41
VZ
164 EVT_TEXT_ENTER(ComboPage_AddText, ComboboxWidgetsPage::OnButtonAdd)
165 EVT_TEXT_ENTER(ComboPage_DeleteText, ComboboxWidgetsPage::OnButtonDelete)
166
167 EVT_UPDATE_UI(ComboPage_CurText, ComboboxWidgetsPage::OnUpdateUICurText)
168
169 EVT_UPDATE_UI(ComboPage_Reset, ComboboxWidgetsPage::OnUpdateUIResetButton)
243dbf1a 170 EVT_UPDATE_UI(ComboPage_Insert, ComboboxWidgetsPage::OnUpdateUIInsert)
32b8ec41
VZ
171 EVT_UPDATE_UI(ComboPage_AddSeveral, ComboboxWidgetsPage::OnUpdateUIAddSeveral)
172 EVT_UPDATE_UI(ComboPage_Clear, ComboboxWidgetsPage::OnUpdateUIClearButton)
173 EVT_UPDATE_UI(ComboPage_DeleteText, ComboboxWidgetsPage::OnUpdateUIClearButton)
174 EVT_UPDATE_UI(ComboPage_Delete, ComboboxWidgetsPage::OnUpdateUIDeleteButton)
175 EVT_UPDATE_UI(ComboPage_Change, ComboboxWidgetsPage::OnUpdateUIDeleteSelButton)
176 EVT_UPDATE_UI(ComboPage_ChangeText, ComboboxWidgetsPage::OnUpdateUIDeleteSelButton)
177 EVT_UPDATE_UI(ComboPage_DeleteSel, ComboboxWidgetsPage::OnUpdateUIDeleteSelButton)
178
179 EVT_COMBOBOX(ComboPage_Combo, ComboboxWidgetsPage::OnComboBox)
180 EVT_TEXT(ComboPage_Combo, ComboboxWidgetsPage::OnComboText)
243dbf1a 181 EVT_TEXT_ENTER(ComboPage_Combo, ComboboxWidgetsPage::OnComboText)
32b8ec41 182
206d3a16
JS
183 EVT_CHECKBOX(wxID_ANY, ComboboxWidgetsPage::OnCheckOrRadioBox)
184 EVT_RADIOBOX(wxID_ANY, ComboboxWidgetsPage::OnCheckOrRadioBox)
32b8ec41
VZ
185END_EVENT_TABLE()
186
187// ============================================================================
188// implementation
189// ============================================================================
190
191IMPLEMENT_WIDGETS_PAGE(ComboboxWidgetsPage, _T("Combobox"));
192
61c083e7
WS
193ComboboxWidgetsPage::ComboboxWidgetsPage(wxBookCtrl *book,
194 wxImageList *imaglist)
195 : WidgetsPage(book)
32b8ec41 196{
32b8ec41
VZ
197 // init everything
198 m_chkSort =
199 m_chkReadonly = (wxCheckBox *)NULL;
200
201 m_combobox = (wxComboBox *)NULL;
202 m_sizerCombo = (wxSizer *)NULL;
203
e640f823
JS
204 imaglist->Add(wxBitmap(combobox_xpm));
205
32b8ec41
VZ
206 /*
207 What we create here is a frame having 3 panes: style pane is the
208 leftmost one, in the middle the pane with buttons allowing to perform
209 miscellaneous combobox operations and the pane containing the combobox
210 itself to the right
211 */
212 wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
213
214 // left pane
206d3a16 215 wxStaticBox *box = new wxStaticBox(this, wxID_ANY, _T("&Set style"));
32b8ec41
VZ
216
217 // should be in sync with ComboKind_XXX values
218 static const wxString kinds[] =
219 {
220 _T("default"),
221 _T("simple"),
222 _T("drop down"),
223 };
224
206d3a16 225 m_radioKind = new wxRadioBox(this, wxID_ANY, _T("Combobox &kind:"),
32b8ec41
VZ
226 wxDefaultPosition, wxDefaultSize,
227 WXSIZEOF(kinds), kinds,
228 1, wxRA_SPECIFY_COLS);
229
230 wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
231
232 m_chkSort = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Sort items"));
233 m_chkReadonly = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Read only"));
234
235 sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
236 sizerLeft->Add(m_radioKind, 0, wxGROW | wxALL, 5);
237
238 wxButton *btn = new wxButton(this, ComboPage_Reset, _T("&Reset"));
239 sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
240
241 // middle pane
206d3a16
JS
242 wxStaticBox *box2 = new wxStaticBox(this, wxID_ANY,
243 _T("&Change combobox contents"));
32b8ec41
VZ
244 wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
245
246 wxSizer *sizerRow;
247
248 wxTextCtrl *text;
249 sizerRow = CreateSizerWithTextAndLabel(_T("Current selection"),
250 ComboPage_CurText,
251 &text);
206d3a16 252 text->SetEditable(false);
32b8ec41
VZ
253
254 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
255
243dbf1a
VZ
256 sizerRow = CreateSizerWithTextAndButton(ComboPage_Insert,
257 _T("&Insert this string"),
258 ComboPage_InsertText,
259 &m_textInsert);
260 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
261
32b8ec41
VZ
262 sizerRow = CreateSizerWithTextAndButton(ComboPage_Add,
263 _T("&Add this string"),
264 ComboPage_AddText,
265 &m_textAdd);
266 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
267
243dbf1a 268 btn = new wxButton(this, ComboPage_AddSeveral, _T("&Append a few strings"));
32b8ec41
VZ
269 sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
270
243dbf1a 271 btn = new wxButton(this, ComboPage_AddMany, _T("Append &many strings"));
32b8ec41
VZ
272 sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
273
274 sizerRow = CreateSizerWithTextAndButton(ComboPage_Change,
275 _T("C&hange current"),
276 ComboPage_ChangeText,
277 &m_textChange);
278 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
279
280 sizerRow = CreateSizerWithTextAndButton(ComboPage_Delete,
281 _T("&Delete this item"),
282 ComboPage_DeleteText,
283 &m_textDelete);
284 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
285
286 btn = new wxButton(this, ComboPage_DeleteSel, _T("Delete &selection"));
287 sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
288
289 btn = new wxButton(this, ComboPage_Clear, _T("&Clear"));
290 sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
291
292 // right pane
293 wxSizer *sizerRight = new wxBoxSizer(wxVERTICAL);
206d3a16 294 m_combobox = new wxComboBox(this, ComboPage_Combo, wxEmptyString,
32b8ec41
VZ
295 wxDefaultPosition, wxDefaultSize,
296 0, NULL,
297 0);
298 sizerRight->Add(m_combobox, 1, wxGROW | wxALL, 5);
7b127900 299 sizerRight->SetMinSize(150, 0);
32b8ec41
VZ
300 m_sizerCombo = sizerRight; // save it to modify it later
301
302 // the 3 panes panes compose the window
303 sizerTop->Add(sizerLeft, 0, wxGROW | (wxALL & ~wxLEFT), 10);
304 sizerTop->Add(sizerMiddle, 1, wxGROW | wxALL, 10);
305 sizerTop->Add(sizerRight, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
306
307 // final initializations
308 Reset();
309
32b8ec41
VZ
310 SetSizer(sizerTop);
311
312 sizerTop->Fit(this);
313}
314
315// ----------------------------------------------------------------------------
316// operations
317// ----------------------------------------------------------------------------
318
319void ComboboxWidgetsPage::Reset()
320{
206d3a16
JS
321 m_chkSort->SetValue(false);
322 m_chkReadonly->SetValue(false);
32b8ec41
VZ
323}
324
325void ComboboxWidgetsPage::CreateCombo()
326{
327 int flags = 0;
328
329 if ( m_chkSort->GetValue() )
330 flags |= wxCB_SORT;
331 if ( m_chkReadonly->GetValue() )
332 flags |= wxCB_READONLY;
333
334 switch ( m_radioKind->GetSelection() )
335 {
336 default:
337 wxFAIL_MSG( _T("unknown combo kind") );
338 // fall through
339
340 case ComboKind_Default:
341 break;
342
343 case ComboKind_Simple:
344 flags |= wxCB_SIMPLE;
345 break;
346
347 case ComboKind_DropDown:
348 flags = wxCB_DROPDOWN;
349 break;
350 }
351
352 wxArrayString items;
353 if ( m_combobox )
354 {
355 int count = m_combobox->GetCount();
356 for ( int n = 0; n < count; n++ )
357 {
358 items.Add(m_combobox->GetString(n));
359 }
360
12a3f227 361 m_sizerCombo->Detach( m_combobox );
32b8ec41
VZ
362 delete m_combobox;
363 }
364
206d3a16 365 m_combobox = new wxComboBox(this, ComboPage_Combo, wxEmptyString,
32b8ec41
VZ
366 wxDefaultPosition, wxDefaultSize,
367 0, NULL,
368 flags);
369
370 size_t count = items.GetCount();
371 for ( size_t n = 0; n < count; n++ )
372 {
373 m_combobox->Append(items[n]);
374 }
375
376 m_sizerCombo->Add(m_combobox, 1, wxGROW | wxALL, 5);
377 m_sizerCombo->Layout();
378}
379
380// ----------------------------------------------------------------------------
381// event handlers
382// ----------------------------------------------------------------------------
383
384void ComboboxWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
385{
386 Reset();
387
388 CreateCombo();
389}
390
391void ComboboxWidgetsPage::OnButtonChange(wxCommandEvent& WXUNUSED(event))
392{
393 int sel = m_combobox->GetSelection();
394 if ( sel != -1 )
395 {
396#ifndef __WXGTK__
397 m_combobox->SetString(sel, m_textChange->GetValue());
398#else
399 wxLogMessage(_T("Not implemented in wxGTK"));
400#endif
401 }
402}
403
404void ComboboxWidgetsPage::OnButtonDelete(wxCommandEvent& WXUNUSED(event))
405{
406 unsigned long n;
407 if ( !m_textDelete->GetValue().ToULong(&n) ||
408 (n >= (unsigned)m_combobox->GetCount()) )
409 {
410 return;
411 }
412
413 m_combobox->Delete(n);
414}
415
416void ComboboxWidgetsPage::OnButtonDeleteSel(wxCommandEvent& WXUNUSED(event))
417{
418 int sel = m_combobox->GetSelection();
419 if ( sel != -1 )
420 {
421 m_combobox->Delete(sel);
422 }
423}
424
c02e5a31 425void ComboboxWidgetsPage::OnButtonClear(wxCommandEvent& WXUNUSED(event))
32b8ec41
VZ
426{
427 m_combobox->Clear();
428}
429
c02e5a31 430void ComboboxWidgetsPage::OnButtonInsert(wxCommandEvent& WXUNUSED(event))
243dbf1a
VZ
431{
432 static unsigned int s_item = 0;
433
434 wxString s = m_textInsert->GetValue();
435 if ( !m_textInsert->IsModified() )
436 {
437 // update the default string
438 m_textInsert->SetValue(wxString::Format(_T("test item %u"), ++s_item));
439 }
440
441 if (m_combobox->GetSelection() >= 0)
442 m_combobox->Insert(s, m_combobox->GetSelection());
443}
444
c02e5a31 445void ComboboxWidgetsPage::OnButtonAdd(wxCommandEvent& WXUNUSED(event))
32b8ec41 446{
0c61716c 447 static unsigned int s_item = 0;
32b8ec41
VZ
448
449 wxString s = m_textAdd->GetValue();
450 if ( !m_textAdd->IsModified() )
451 {
452 // update the default string
453 m_textAdd->SetValue(wxString::Format(_T("test item %u"), ++s_item));
454 }
455
456 m_combobox->Append(s);
457}
458
459void ComboboxWidgetsPage::OnButtonAddMany(wxCommandEvent& WXUNUSED(event))
460{
461 // "many" means 1000 here
0c61716c 462 for ( unsigned int n = 0; n < 1000; n++ )
32b8ec41
VZ
463 {
464 m_combobox->Append(wxString::Format(_T("item #%u"), n));
465 }
466}
467
c02e5a31 468void ComboboxWidgetsPage::OnButtonAddSeveral(wxCommandEvent& WXUNUSED(event))
32b8ec41
VZ
469{
470 m_combobox->Append(_T("First"));
471 m_combobox->Append(_T("another one"));
472 m_combobox->Append(_T("and the last (very very very very very very very very very very long) one"));
473}
474
475void ComboboxWidgetsPage::OnUpdateUICurText(wxUpdateUIEvent& event)
476{
e640f823
JS
477 if (m_combobox)
478 event.SetText( wxString::Format(_T("%d"), m_combobox->GetSelection()) );
32b8ec41
VZ
479}
480
481void ComboboxWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent& event)
482{
e640f823
JS
483 if (m_combobox)
484 event.Enable( m_chkSort->GetValue() || m_chkReadonly->GetValue() );
32b8ec41
VZ
485}
486
243dbf1a
VZ
487void ComboboxWidgetsPage::OnUpdateUIInsert(wxUpdateUIEvent& event)
488{
489 if (m_combobox)
490 {
491 bool enable = !(m_combobox->GetWindowStyle() & wxCB_SORT) &&
492 (m_combobox->GetSelection() >= 0);
493
494 event.Enable(enable);
495 }
496}
497
32b8ec41
VZ
498void ComboboxWidgetsPage::OnUpdateUIDeleteButton(wxUpdateUIEvent& event)
499{
e640f823
JS
500 if (m_combobox)
501 {
502 unsigned long n;
503 event.Enable(m_textDelete->GetValue().ToULong(&n) &&
2f6c54eb 504 (n < (unsigned)m_combobox->GetCount()));
e640f823 505 }
32b8ec41
VZ
506}
507
508void ComboboxWidgetsPage::OnUpdateUIDeleteSelButton(wxUpdateUIEvent& event)
509{
e640f823
JS
510 if (m_combobox)
511 event.Enable(m_combobox->GetSelection() != -1);
32b8ec41
VZ
512}
513
514void ComboboxWidgetsPage::OnUpdateUIClearButton(wxUpdateUIEvent& event)
515{
e640f823
JS
516 if (m_combobox)
517 event.Enable(m_combobox->GetCount() != 0);
32b8ec41
VZ
518}
519
520void ComboboxWidgetsPage::OnUpdateUIAddSeveral(wxUpdateUIEvent& event)
521{
e640f823
JS
522 if (m_combobox)
523 event.Enable(!(m_combobox->GetWindowStyle() & wxCB_SORT));
32b8ec41
VZ
524}
525
526void ComboboxWidgetsPage::OnComboText(wxCommandEvent& event)
527{
e640f823 528 if (!m_combobox)
2f6c54eb
VZ
529 return;
530
32b8ec41
VZ
531 wxString s = event.GetString();
532
533 wxASSERT_MSG( s == m_combobox->GetValue(),
534 _T("event and combobox values should be the same") );
535
243dbf1a
VZ
536 if (event.GetEventType() == wxEVT_COMMAND_TEXT_ENTER)
537 wxLogMessage(_T("Combobox enter pressed (now '%s')"), s.c_str());
538 else
32b8ec41
VZ
539 wxLogMessage(_T("Combobox text changed (now '%s')"), s.c_str());
540}
541
542void ComboboxWidgetsPage::OnComboBox(wxCommandEvent& event)
543{
aec18ff7 544 long sel = event.GetInt();
32b8ec41
VZ
545 m_textDelete->SetValue(wxString::Format(_T("%ld"), sel));
546
aec18ff7 547 wxLogMessage(_T("Combobox item %ld selected"), sel);
78b3b018
RR
548
549 wxLogMessage(_T("Combobox GetValue(): %s"), m_combobox->GetValue().c_str() );
32b8ec41
VZ
550}
551
c02e5a31 552void ComboboxWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& WXUNUSED(event))
32b8ec41
VZ
553{
554 CreateCombo();
555}
556
3379ed37
VZ
557#endif //wxUSE_COMBOBOX
558
7b127900 559#endif