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