]> git.saurik.com Git - wxWidgets.git/blob - samples/widgets/odcombobox.cpp
extending docs according #9713
[wxWidgets.git] / samples / widgets / odcombobox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
3 // Name: odcombobox.cpp
4 // Purpose: Part of the widgets sample showing wxOwnerDrawnComboBox
5 // Author: Jaakko Salli (based on combobox page by Vadim Zeitlin)
6 // Created: Jul-28-2006
7 // Id: $Id$
8 // Copyright: (c) 2006 Jaakko Salli
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 #if wxUSE_ODCOMBOBOX
28
29 // for all others, include the necessary headers
30 #ifndef WX_PRECOMP
31 #include "wx/log.h"
32
33 #include "wx/bitmap.h"
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/dc.h"
43 #include "wx/dcmemory.h"
44 #include "wx/sizer.h"
45 #include "wx/odcombo.h"
46
47
48 #include "itemcontainer.h"
49 #include "widgets.h"
50
51 #include "icons/odcombobox.xpm"
52
53 // ----------------------------------------------------------------------------
54 // constants
55 // ----------------------------------------------------------------------------
56
57 // control ids
58 enum
59 {
60 ODComboPage_Reset = wxID_HIGHEST,
61 ODComboPage_PopupMinWidth,
62 ODComboPage_PopupHeight,
63 ODComboPage_ButtonWidth,
64 ODComboPage_ButtonHeight,
65 ODComboPage_ButtonSpacing,
66 ODComboPage_CurText,
67 ODComboPage_InsertionPointText,
68 ODComboPage_Insert,
69 ODComboPage_InsertText,
70 ODComboPage_Add,
71 ODComboPage_AddText,
72 ODComboPage_AddSeveral,
73 ODComboPage_AddMany,
74 ODComboPage_Clear,
75 ODComboPage_Change,
76 ODComboPage_ChangeText,
77 ODComboPage_Delete,
78 ODComboPage_DeleteText,
79 ODComboPage_DeleteSel,
80 ODComboPage_Combo,
81 ODComboPage_ContainerTests
82 };
83
84
85 // ----------------------------------------------------------------------------
86 // ODComboboxWidgetsPage
87 // ----------------------------------------------------------------------------
88
89 class ODComboboxWidgetsPage : public ItemContainerWidgetsPage
90 {
91 public:
92 ODComboboxWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist);
93
94 virtual wxControl *GetWidget() const { return m_combobox; }
95 virtual wxTextEntryBase *GetTextEntry() const
96 { return m_combobox ? m_combobox->GetTextCtrl() : NULL; }
97 virtual wxItemContainer* GetContainer() const { return m_combobox; }
98 virtual void RecreateWidget() { CreateCombo(); }
99
100 // lazy creation of the content
101 virtual void CreateContent();
102
103 protected:
104 // event handlers
105 void OnButtonReset(wxCommandEvent& event);
106 void OnButtonChange(wxCommandEvent& event);
107 void OnButtonDelete(wxCommandEvent& event);
108 void OnButtonDeleteSel(wxCommandEvent& event);
109 void OnButtonClear(wxCommandEvent& event);
110 void OnButtonInsert(wxCommandEvent &event);
111 void OnButtonAdd(wxCommandEvent& event);
112 void OnButtonAddSeveral(wxCommandEvent& event);
113 void OnButtonAddMany(wxCommandEvent& event);
114
115 void OnComboBox(wxCommandEvent& event);
116 void OnComboText(wxCommandEvent& event);
117
118 void OnCheckOrRadioBox(wxCommandEvent& event);
119
120 void OnTextPopupWidth(wxCommandEvent& event);
121 void OnTextPopupHeight(wxCommandEvent& event);
122 void OnTextButtonAll(wxCommandEvent& event);
123
124 void OnUpdateUICurText(wxUpdateUIEvent& event);
125 void OnUpdateUIInsertionPointText(wxUpdateUIEvent& event);
126
127 void OnUpdateUIInsert(wxUpdateUIEvent& event);
128 void OnUpdateUIAddSeveral(wxUpdateUIEvent& event);
129 void OnUpdateUIClearButton(wxUpdateUIEvent& event);
130 void OnUpdateUIDeleteButton(wxUpdateUIEvent& event);
131 void OnUpdateUIDeleteSelButton(wxUpdateUIEvent& event);
132 void OnUpdateUIResetButton(wxUpdateUIEvent& event);
133
134 // reset the odcombobox parameters
135 void Reset();
136
137 // (re)create the odcombobox
138 void CreateCombo();
139
140 // helper that gets all button values from controls and calls SetButtonPosition
141 void GetButtonPosition();
142
143 // helper to create the button bitmap
144 wxBitmap CreateBitmap(const wxColour& colour);
145
146 // the controls
147 // ------------
148
149 // the checkboxes for styles
150 wxCheckBox *m_chkSort,
151 *m_chkReadonly,
152 *m_chkDclickcycles,
153 *m_chkBitmapbutton,
154 *m_chkStdbutton;
155
156 // the text entries for popup and button adjustment
157 wxTextCtrl *m_textPopupMinWidth,
158 *m_textPopupHeight,
159 *m_textButtonWidth,
160 *m_textButtonHeight,
161 *m_textButtonSpacing;
162
163 // the checkboxes for same
164 wxCheckBox *m_chkAlignpopupright,
165 *m_chkAlignbutleft;
166
167 // the combobox itself and the sizer it is in
168 wxOwnerDrawnComboBox *m_combobox;
169 wxSizer *m_sizerCombo;
170
171 // the text entries for "Add/change string" and "Delete" buttons
172 wxTextCtrl *m_textInsert,
173 *m_textAdd,
174 *m_textChange,
175 *m_textDelete;
176
177 private:
178 DECLARE_EVENT_TABLE()
179 DECLARE_WIDGETS_PAGE(ODComboboxWidgetsPage)
180 };
181
182 // ----------------------------------------------------------------------------
183 // event tables
184 // ----------------------------------------------------------------------------
185
186 BEGIN_EVENT_TABLE(ODComboboxWidgetsPage, WidgetsPage)
187 EVT_BUTTON(ODComboPage_Reset, ODComboboxWidgetsPage::OnButtonReset)
188 EVT_BUTTON(ODComboPage_Change, ODComboboxWidgetsPage::OnButtonChange)
189 EVT_BUTTON(ODComboPage_Delete, ODComboboxWidgetsPage::OnButtonDelete)
190 EVT_BUTTON(ODComboPage_DeleteSel, ODComboboxWidgetsPage::OnButtonDeleteSel)
191 EVT_BUTTON(ODComboPage_Clear, ODComboboxWidgetsPage::OnButtonClear)
192 EVT_BUTTON(ODComboPage_Insert, ODComboboxWidgetsPage::OnButtonInsert)
193 EVT_BUTTON(ODComboPage_Add, ODComboboxWidgetsPage::OnButtonAdd)
194 EVT_BUTTON(ODComboPage_AddSeveral, ODComboboxWidgetsPage::OnButtonAddSeveral)
195 EVT_BUTTON(ODComboPage_AddMany, ODComboboxWidgetsPage::OnButtonAddMany)
196 EVT_BUTTON(ODComboPage_ContainerTests, ItemContainerWidgetsPage::OnButtonTestItemContainer)
197
198 EVT_TEXT_ENTER(ODComboPage_InsertText, ODComboboxWidgetsPage::OnButtonInsert)
199 EVT_TEXT_ENTER(ODComboPage_AddText, ODComboboxWidgetsPage::OnButtonAdd)
200 EVT_TEXT_ENTER(ODComboPage_DeleteText, ODComboboxWidgetsPage::OnButtonDelete)
201
202 EVT_TEXT(ODComboPage_PopupMinWidth, ODComboboxWidgetsPage::OnTextPopupWidth)
203 EVT_TEXT(ODComboPage_PopupHeight, ODComboboxWidgetsPage::OnTextPopupHeight)
204 EVT_TEXT(ODComboPage_ButtonWidth, ODComboboxWidgetsPage::OnTextButtonAll)
205 EVT_TEXT(ODComboPage_ButtonHeight, ODComboboxWidgetsPage::OnTextButtonAll)
206 EVT_TEXT(ODComboPage_ButtonSpacing, ODComboboxWidgetsPage::OnTextButtonAll)
207
208 EVT_UPDATE_UI(ODComboPage_CurText, ODComboboxWidgetsPage::OnUpdateUICurText)
209 EVT_UPDATE_UI(ODComboPage_InsertionPointText, ODComboboxWidgetsPage::OnUpdateUIInsertionPointText)
210
211 EVT_UPDATE_UI(ODComboPage_Reset, ODComboboxWidgetsPage::OnUpdateUIResetButton)
212 EVT_UPDATE_UI(ODComboPage_Insert, ODComboboxWidgetsPage::OnUpdateUIInsert)
213 EVT_UPDATE_UI(ODComboPage_AddSeveral, ODComboboxWidgetsPage::OnUpdateUIAddSeveral)
214 EVT_UPDATE_UI(ODComboPage_Clear, ODComboboxWidgetsPage::OnUpdateUIClearButton)
215 EVT_UPDATE_UI(ODComboPage_DeleteText, ODComboboxWidgetsPage::OnUpdateUIClearButton)
216 EVT_UPDATE_UI(ODComboPage_Delete, ODComboboxWidgetsPage::OnUpdateUIDeleteButton)
217 EVT_UPDATE_UI(ODComboPage_Change, ODComboboxWidgetsPage::OnUpdateUIDeleteSelButton)
218 EVT_UPDATE_UI(ODComboPage_ChangeText, ODComboboxWidgetsPage::OnUpdateUIDeleteSelButton)
219 EVT_UPDATE_UI(ODComboPage_DeleteSel, ODComboboxWidgetsPage::OnUpdateUIDeleteSelButton)
220
221 EVT_COMBOBOX(ODComboPage_Combo, ODComboboxWidgetsPage::OnComboBox)
222 EVT_TEXT(ODComboPage_Combo, ODComboboxWidgetsPage::OnComboText)
223 EVT_TEXT_ENTER(ODComboPage_Combo, ODComboboxWidgetsPage::OnComboText)
224
225 EVT_CHECKBOX(wxID_ANY, ODComboboxWidgetsPage::OnCheckOrRadioBox)
226 EVT_RADIOBOX(wxID_ANY, ODComboboxWidgetsPage::OnCheckOrRadioBox)
227 END_EVENT_TABLE()
228
229 // ============================================================================
230 // implementation
231 // ============================================================================
232
233 //
234 // wxOwnerDrawnComboBox needs to subclassed so that owner-drawing
235 // callbacks can be implemented.
236 class DemoODComboBox : public wxOwnerDrawnComboBox
237 {
238 public:
239 virtual void OnDrawItem(wxDC& dc,
240 const wxRect& rect,
241 int item,
242 int WXUNUSED(flags)) const
243 {
244 if ( item == wxNOT_FOUND )
245 return;
246
247 wxColour txtCol;
248 int mod = item % 4;
249
250 if ( mod == 0 )
251 txtCol = *wxBLACK;
252 else if ( mod == 1 )
253 txtCol = *wxRED;
254 else if ( mod == 2 )
255 txtCol = *wxGREEN;
256 else
257 txtCol = *wxBLUE;
258
259 dc.SetTextForeground(txtCol);
260
261 dc.DrawText(GetString(item),
262 rect.x + 3,
263 rect.y + ((rect.height - dc.GetCharHeight())/2)
264 );
265 }
266
267 virtual void OnDrawBackground(wxDC& dc, const wxRect& rect,
268 int item, int flags ) const
269 {
270
271 // If item is selected or even, or we are painting the
272 // combo control itself, use the default rendering.
273 if ( (flags & (wxODCB_PAINTING_CONTROL|wxODCB_PAINTING_SELECTED)) ||
274 (item & 1) == 0 )
275 {
276 wxOwnerDrawnComboBox::OnDrawBackground(dc,rect,item,flags);
277 return;
278 }
279
280 // Otherwise, draw every other background with different colour.
281 wxColour bgCol(240,240,250);
282 dc.SetBrush(wxBrush(bgCol));
283 dc.SetPen(wxPen(bgCol));
284 dc.DrawRectangle(rect);
285 }
286
287 virtual wxCoord OnMeasureItem(size_t WXUNUSED(item)) const
288 {
289 return 48;
290 }
291
292 virtual wxCoord OnMeasureItemWidth(size_t WXUNUSED(item)) const
293 {
294 return -1; // default - will be measured from text width
295 }
296 };
297
298
299 IMPLEMENT_WIDGETS_PAGE(ODComboboxWidgetsPage, _T("OwnerDrawnCombobox"),
300 GENERIC_CTRLS | WITH_ITEMS_CTRLS | COMBO_CTRLS
301 );
302
303 ODComboboxWidgetsPage::ODComboboxWidgetsPage(WidgetsBookCtrl *book,
304 wxImageList *imaglist)
305 : ItemContainerWidgetsPage(book, imaglist, odcombobox_xpm)
306 {
307 // init everything
308 m_chkSort =
309 m_chkReadonly =
310 m_chkDclickcycles = (wxCheckBox *)NULL;
311
312 m_combobox = (wxOwnerDrawnComboBox *)NULL;
313 m_sizerCombo = (wxSizer *)NULL;
314 }
315
316 void ODComboboxWidgetsPage::CreateContent()
317 {
318 /*
319 What we create here is a frame having 3 panes: style pane is the
320 leftmost one, in the middle the pane with buttons allowing to perform
321 miscellaneous combobox operations and the pane containing the combobox
322 itself to the right
323 */
324 wxTextCtrl *text;
325 wxSizer *sizerRow;
326
327 wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
328
329 wxSizer *sizerLeft = new wxBoxSizer(wxVERTICAL);
330
331 // left pane - style box
332 wxStaticBox *box = new wxStaticBox(this, wxID_ANY, _T("&Set style"));
333
334 wxSizer *sizerStyle = new wxStaticBoxSizer(box, wxVERTICAL);
335
336 m_chkSort = CreateCheckBoxAndAddToSizer(sizerStyle, _T("&Sort items"));
337 m_chkReadonly = CreateCheckBoxAndAddToSizer(sizerStyle, _T("&Read only"));
338 m_chkDclickcycles = CreateCheckBoxAndAddToSizer(sizerStyle, _T("&Double-click Cycles"));
339
340 sizerStyle->AddSpacer(4);
341
342 m_chkBitmapbutton = CreateCheckBoxAndAddToSizer(sizerStyle, _T("&Bitmap button"));
343 m_chkStdbutton = CreateCheckBoxAndAddToSizer(sizerStyle, _T("B&lank button background"));
344
345 wxButton *btn = new wxButton(this, ODComboPage_Reset, _T("&Reset"));
346 sizerStyle->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 3);
347
348 sizerLeft->Add(sizerStyle, 0, wxGROW | wxALIGN_CENTRE_HORIZONTAL);
349
350 // left pane - popup adjustment box
351 box = new wxStaticBox(this, wxID_ANY, _T("Adjust &popup"));
352
353 wxSizer *sizerPopupPos = new wxStaticBoxSizer(box, wxVERTICAL);
354
355 sizerRow = CreateSizerWithTextAndLabel(_T("Min. Width:"),
356 ODComboPage_PopupMinWidth,
357 &m_textPopupMinWidth);
358 m_textPopupMinWidth->SetValue(wxT("-1"));
359 sizerPopupPos->Add(sizerRow, 0, wxALL | wxGROW, 5);
360
361 sizerRow = CreateSizerWithTextAndLabel(_T("Max. Height:"),
362 ODComboPage_PopupHeight,
363 &m_textPopupHeight);
364 m_textPopupHeight->SetValue(wxT("-1"));
365 sizerPopupPos->Add(sizerRow, 0, wxALL | wxGROW, 5);
366
367 m_chkAlignpopupright = CreateCheckBoxAndAddToSizer(sizerPopupPos, _T("Align Right"));
368
369 sizerLeft->Add(sizerPopupPos, 0, wxGROW | wxALIGN_CENTRE_HORIZONTAL | wxTOP, 2);
370
371 // left pane - button adjustment box
372 box = new wxStaticBox(this, wxID_ANY, _T("Adjust &button"));
373
374 wxSizer *sizerButtonPos = new wxStaticBoxSizer(box, wxVERTICAL);
375
376 sizerRow = CreateSizerWithTextAndLabel(_T("Width:"),
377 ODComboPage_ButtonWidth,
378 &m_textButtonWidth);
379 m_textButtonWidth->SetValue(wxT("-1"));
380 sizerButtonPos->Add(sizerRow, 0, wxALL | wxGROW, 5);
381
382 sizerRow = CreateSizerWithTextAndLabel(_T("VSpacing:"),
383 ODComboPage_ButtonSpacing,
384 &m_textButtonSpacing);
385 m_textButtonSpacing->SetValue(wxT("0"));
386 sizerButtonPos->Add(sizerRow, 0, wxALL | wxGROW, 5);
387
388 sizerRow = CreateSizerWithTextAndLabel(_T("Height:"),
389 ODComboPage_ButtonHeight,
390 &m_textButtonHeight);
391 m_textButtonHeight->SetValue(wxT("-1"));
392 sizerButtonPos->Add(sizerRow, 0, wxALL | wxGROW, 5);
393
394 m_chkAlignbutleft = CreateCheckBoxAndAddToSizer(sizerButtonPos, _T("Align Left"));
395
396 sizerLeft->Add(sizerButtonPos, 0, wxGROW | wxALIGN_CENTRE_HORIZONTAL | wxTOP, 2);
397
398 // middle pane
399 wxStaticBox *box2 = new wxStaticBox(this, wxID_ANY,
400 _T("&Change combobox contents"));
401 wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
402
403 btn = new wxButton(this, ODComboPage_ContainerTests, _T("Run &tests"));
404 sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
405
406 sizerRow = CreateSizerWithTextAndLabel(_T("Current selection"),
407 ODComboPage_CurText,
408 &text);
409 text->SetEditable(false);
410
411 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
412
413 sizerRow = CreateSizerWithTextAndLabel(_T("Insertion Point"),
414 ODComboPage_InsertionPointText,
415 &text);
416 text->SetEditable(false);
417
418 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
419
420 sizerRow = CreateSizerWithTextAndButton(ODComboPage_Insert,
421 _T("&Insert this string"),
422 ODComboPage_InsertText,
423 &m_textInsert);
424 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
425
426 sizerRow = CreateSizerWithTextAndButton(ODComboPage_Add,
427 _T("&Add this string"),
428 ODComboPage_AddText,
429 &m_textAdd);
430 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
431
432 btn = new wxButton(this, ODComboPage_AddSeveral, _T("&Append a few strings"));
433 sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
434
435 btn = new wxButton(this, ODComboPage_AddMany, _T("Append &many strings"));
436 sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
437
438 sizerRow = CreateSizerWithTextAndButton(ODComboPage_Change,
439 _T("C&hange current"),
440 ODComboPage_ChangeText,
441 &m_textChange);
442 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
443
444 sizerRow = CreateSizerWithTextAndButton(ODComboPage_Delete,
445 _T("&Delete this item"),
446 ODComboPage_DeleteText,
447 &m_textDelete);
448 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
449
450 btn = new wxButton(this, ODComboPage_DeleteSel, _T("Delete &selection"));
451 sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
452
453 btn = new wxButton(this, ODComboPage_Clear, _T("&Clear"));
454 sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
455
456 // right pane
457 wxSizer *sizerRight = new wxBoxSizer(wxVERTICAL);
458 m_combobox = new DemoODComboBox();
459 m_combobox->Create(this, ODComboPage_Combo, wxEmptyString,
460 wxDefaultPosition, wxDefaultSize,
461 0, NULL,
462 0);
463 sizerRight->Add(m_combobox, 0, wxGROW | wxALL, 5);
464 sizerRight->SetMinSize(150, 0);
465 m_sizerCombo = sizerRight; // save it to modify it later
466
467 // the 3 panes panes compose the window
468 sizerTop->Add(sizerLeft, 4, wxGROW | (wxALL & ~wxLEFT), 10);
469 sizerTop->Add(sizerMiddle, 5, wxGROW | wxALL, 10);
470 sizerTop->Add(sizerRight, 4, wxGROW | (wxALL & ~wxRIGHT), 10);
471
472 // final initializations
473 Reset();
474
475 SetSizer(sizerTop);
476 }
477
478 // ----------------------------------------------------------------------------
479 // operations
480 // ----------------------------------------------------------------------------
481
482 void ODComboboxWidgetsPage::Reset()
483 {
484 m_chkSort->SetValue(false);
485 m_chkReadonly->SetValue(false);
486 m_chkDclickcycles->SetValue(false);
487 m_chkDclickcycles->Enable(false);
488 m_chkBitmapbutton->SetValue(false);
489 m_chkStdbutton->SetValue(false);
490 m_chkStdbutton->Enable(false);
491 }
492
493 void ODComboboxWidgetsPage::CreateCombo()
494 {
495 int flags = ms_defaultFlags;
496
497 if ( m_chkSort->GetValue() )
498 flags |= wxCB_SORT;
499 if ( m_chkReadonly->GetValue() )
500 flags |= wxCB_READONLY;
501 if ( m_chkDclickcycles->GetValue() )
502 flags |= wxODCB_DCLICK_CYCLES;
503
504 wxArrayString items;
505 if ( m_combobox )
506 {
507 unsigned int count = m_combobox->GetCount();
508 for ( unsigned int n = 0; n < count; n++ )
509 {
510 items.Add(m_combobox->GetString(n));
511 }
512
513 m_sizerCombo->Detach( m_combobox );
514 delete m_combobox;
515 }
516
517 m_combobox = new DemoODComboBox();
518 m_combobox->Create(this, ODComboPage_Combo, wxEmptyString,
519 wxDefaultPosition, wxDefaultSize,
520 0, NULL,
521 flags);
522
523 unsigned int count = items.GetCount();
524 for ( unsigned int n = 0; n < count; n++ )
525 {
526 m_combobox->Append(items[n]);
527 }
528
529 // Update from controls that edit popup position etc.
530
531 wxUpdateUIEvent tempEvt;
532 OnTextPopupWidth(tempEvt);
533 OnTextPopupHeight(tempEvt);
534 GetButtonPosition();
535
536 m_combobox->SetPopupAnchor( m_chkAlignpopupright->GetValue() ? wxRIGHT : wxLEFT );
537
538 if ( m_chkBitmapbutton->GetValue() )
539 {
540 wxBitmap bmpNormal = CreateBitmap(wxColour(0,0,255));
541 wxBitmap bmpPressed = CreateBitmap(wxColour(0,0,128));
542 wxBitmap bmpHover = CreateBitmap(wxColour(128,128,255));
543 m_combobox->SetButtonBitmaps(bmpNormal,m_chkStdbutton->GetValue(),bmpPressed,bmpHover);
544 }
545
546 m_sizerCombo->Add(m_combobox, 0, wxGROW | wxALL, 5);
547 m_sizerCombo->Layout();
548 }
549
550 // ----------------------------------------------------------------------------
551 // event handlers
552 // ----------------------------------------------------------------------------
553
554 void ODComboboxWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
555 {
556 Reset();
557
558 CreateCombo();
559 }
560
561 void ODComboboxWidgetsPage::OnButtonChange(wxCommandEvent& WXUNUSED(event))
562 {
563 int sel = m_combobox->GetSelection();
564 if ( sel != wxNOT_FOUND )
565 {
566 #ifndef __WXGTK__
567 m_combobox->SetString(sel, m_textChange->GetValue());
568 #else
569 wxLogMessage(_T("Not implemented in wxGTK"));
570 #endif
571 }
572 }
573
574 void ODComboboxWidgetsPage::OnButtonDelete(wxCommandEvent& WXUNUSED(event))
575 {
576 unsigned long n;
577 if ( !m_textDelete->GetValue().ToULong(&n) ||
578 (n >= m_combobox->GetCount()) )
579 {
580 return;
581 }
582
583 m_combobox->Delete(n);
584 }
585
586 void ODComboboxWidgetsPage::OnButtonDeleteSel(wxCommandEvent& WXUNUSED(event))
587 {
588 int sel = m_combobox->GetSelection();
589 if ( sel != wxNOT_FOUND )
590 {
591 m_combobox->Delete(sel);
592 }
593 }
594
595 void ODComboboxWidgetsPage::OnButtonClear(wxCommandEvent& WXUNUSED(event))
596 {
597 m_combobox->Clear();
598 }
599
600 void ODComboboxWidgetsPage::OnButtonInsert(wxCommandEvent& WXUNUSED(event))
601 {
602 static unsigned int s_item = 0;
603
604 wxString s = m_textInsert->GetValue();
605 if ( !m_textInsert->IsModified() )
606 {
607 // update the default string
608 m_textInsert->SetValue(wxString::Format(_T("test item %u"), ++s_item));
609 }
610
611 if (m_combobox->GetSelection() >= 0)
612 m_combobox->Insert(s, m_combobox->GetSelection());
613 }
614
615 void ODComboboxWidgetsPage::OnButtonAdd(wxCommandEvent& WXUNUSED(event))
616 {
617 static unsigned int s_item = 0;
618
619 wxString s = m_textAdd->GetValue();
620 if ( !m_textAdd->IsModified() )
621 {
622 // update the default string
623 m_textAdd->SetValue(wxString::Format(_T("test item %u"), ++s_item));
624 }
625
626 m_combobox->Append(s);
627 }
628
629 void ODComboboxWidgetsPage::OnButtonAddMany(wxCommandEvent& WXUNUSED(event))
630 {
631 // "many" means 1000 here
632 for ( unsigned int n = 0; n < 1000; n++ )
633 {
634 m_combobox->Append(wxString::Format(_T("item #%u"), n));
635 }
636 }
637
638 void ODComboboxWidgetsPage::OnButtonAddSeveral(wxCommandEvent& WXUNUSED(event))
639 {
640 m_combobox->Append(_T("First"));
641 m_combobox->Append(_T("another one"));
642 m_combobox->Append(_T("and the last (very very very very very very very very very very long) one"));
643 }
644
645 void ODComboboxWidgetsPage::OnTextPopupWidth(wxCommandEvent& WXUNUSED(event))
646 {
647 long l = 0;
648
649 m_textPopupMinWidth->GetValue().ToLong(&l);
650
651 if (m_combobox && l > 0)
652 {
653 m_combobox->SetPopupMinWidth(l);
654 }
655 }
656
657 void ODComboboxWidgetsPage::OnTextPopupHeight(wxCommandEvent& WXUNUSED(event))
658 {
659 long l = 0;
660
661 m_textPopupHeight->GetValue().ToLong(&l);
662
663 if (m_combobox && l > 0)
664 {
665 m_combobox->SetPopupMaxHeight(l);
666 }
667 }
668
669 void ODComboboxWidgetsPage::GetButtonPosition()
670 {
671 long w = -1;
672 long h = -1;
673 long spacing = 0;
674
675 m_textButtonWidth->GetValue().ToLong(&w);
676 m_textButtonSpacing->GetValue().ToLong(&spacing);
677 m_textButtonHeight->GetValue().ToLong(&h);
678 int align = m_chkAlignbutleft->GetValue() ?
679 wxLEFT : wxRIGHT;
680
681 m_combobox->SetButtonPosition(w,h,align,spacing);
682 }
683
684 void ODComboboxWidgetsPage::OnTextButtonAll(wxCommandEvent& WXUNUSED(event))
685 {
686 if (m_combobox)
687 {
688 if ( m_chkBitmapbutton->GetValue() )
689 CreateCombo();
690 else
691 GetButtonPosition();
692 }
693 }
694
695 void ODComboboxWidgetsPage::OnUpdateUICurText(wxUpdateUIEvent& event)
696 {
697 if (m_combobox)
698 event.SetText( wxString::Format(_T("%d"), m_combobox->GetSelection()) );
699 }
700
701 void ODComboboxWidgetsPage::OnUpdateUIInsertionPointText(wxUpdateUIEvent& event)
702 {
703 if (m_combobox)
704 event.SetText( wxString::Format(_T("%ld"), m_combobox->GetInsertionPoint()) );
705 }
706
707 void ODComboboxWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent& event)
708 {
709 if (m_combobox)
710 event.Enable( m_chkSort->GetValue() || m_chkReadonly->GetValue() ||
711 m_chkBitmapbutton->GetValue() );
712 }
713
714 void ODComboboxWidgetsPage::OnUpdateUIInsert(wxUpdateUIEvent& event)
715 {
716 if (m_combobox)
717 {
718 bool enable = !(m_combobox->GetWindowStyle() & wxCB_SORT) &&
719 (m_combobox->GetSelection() >= 0);
720
721 event.Enable(enable);
722 }
723 }
724
725 void ODComboboxWidgetsPage::OnUpdateUIDeleteButton(wxUpdateUIEvent& event)
726 {
727 if (m_combobox)
728 {
729 unsigned long n;
730 event.Enable(m_textDelete->GetValue().ToULong(&n) &&
731 (n < (unsigned)m_combobox->GetCount()));
732 }
733 }
734
735 void ODComboboxWidgetsPage::OnUpdateUIDeleteSelButton(wxUpdateUIEvent& event)
736 {
737 if (m_combobox)
738 event.Enable(m_combobox->GetSelection() != wxNOT_FOUND);
739 }
740
741 void ODComboboxWidgetsPage::OnUpdateUIClearButton(wxUpdateUIEvent& event)
742 {
743 if (m_combobox)
744 event.Enable(m_combobox->GetCount() != 0);
745 }
746
747 void ODComboboxWidgetsPage::OnUpdateUIAddSeveral(wxUpdateUIEvent& event)
748 {
749 if (m_combobox)
750 event.Enable(!(m_combobox->GetWindowStyle() & wxCB_SORT));
751 }
752
753 void ODComboboxWidgetsPage::OnComboText(wxCommandEvent& event)
754 {
755 if (!m_combobox)
756 return;
757
758 wxString s = event.GetString();
759
760 wxASSERT_MSG( s == m_combobox->GetValue(),
761 _T("event and combobox values should be the same") );
762
763 if (event.GetEventType() == wxEVT_COMMAND_TEXT_ENTER)
764 wxLogMessage(_T("OwnerDrawnCombobox enter pressed (now '%s')"), s.c_str());
765 else
766 wxLogMessage(_T("OwnerDrawnCombobox text changed (now '%s')"), s.c_str());
767 }
768
769 void ODComboboxWidgetsPage::OnComboBox(wxCommandEvent& event)
770 {
771 long sel = event.GetInt();
772 m_textDelete->SetValue(wxString::Format(_T("%ld"), sel));
773
774 wxLogMessage(_T("OwnerDrawnCombobox item %ld selected"), sel);
775
776 wxLogMessage(_T("OwnerDrawnCombobox GetValue(): %s"), m_combobox->GetValue().c_str() );
777 }
778
779 void ODComboboxWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& event)
780 {
781 wxObject* ctrl = event.GetEventObject();
782
783 // Double-click cycles only applies to read-only combobox
784 if ( ctrl == (wxObject*) m_chkReadonly )
785 {
786 m_chkDclickcycles->Enable( m_chkReadonly->GetValue() );
787 }
788 else if ( ctrl == (wxObject*) m_chkBitmapbutton )
789 {
790 m_chkStdbutton->Enable( m_chkBitmapbutton->GetValue() );
791 }
792 else if ( ctrl == (wxObject*) m_chkAlignbutleft )
793 {
794 wxUpdateUIEvent tempEvt;
795 OnTextButtonAll(tempEvt);
796 }
797
798 CreateCombo();
799 }
800
801 wxBitmap ODComboboxWidgetsPage::CreateBitmap(const wxColour& colour)
802 {
803 int ch = m_combobox->GetClientSize().y - 1;
804 int h0 = ch - 5;
805
806 long w = -1;
807 long h = -1;
808
809 m_textButtonWidth->GetValue().ToLong(&w);
810 m_textButtonHeight->GetValue().ToLong(&h);
811
812 if ( w <= 0 )
813 w = h0 - 1;
814 if ( h <= 0 )
815 h = h0;
816 if ( h > ch )
817 h = ch;
818
819 wxMemoryDC dc;
820 wxBitmap bmp(w,h);
821 dc.SelectObject(bmp);
822
823 // Draw transparent background
824 wxColour magic(255,0,255);
825 wxBrush magicBrush(magic);
826 dc.SetBrush(magicBrush);
827 dc.SetPen(*wxTRANSPARENT_PEN);
828 dc.DrawRectangle(0,0,bmp.GetWidth(),bmp.GetHeight());
829
830 // Draw image content
831 dc.SetBrush(wxBrush(colour));
832 dc.DrawCircle(h/2,h/2+1,(h/2));
833
834 dc.SelectObject(wxNullBitmap);
835
836 // Finalize transparency with a mask
837 wxMask *mask = new wxMask(bmp, magic);
838 bmp.SetMask(mask);
839
840 return bmp;
841 }
842
843 #endif //wxUSE_ODCOMBOBOX