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