1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
3 // Name: bmpcombobox.cpp
4 // Purpose: Part of the widgets sample showing wxBitmapComboBox
5 // Author: Jaakko Salli
6 // Created: Sep-01-2006
8 // Copyright: (c) 2006 Jaakko Salli
9 // License: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // for compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
27 #if wxUSE_BITMAPCOMBOBOX
29 // for all others, include the necessary headers
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 #include "wx/filedlg.h"
43 #include "wx/stattext.h"
45 #include "wx/dcmemory.h"
49 #include "wx/msgdlg.h"
50 #include "wx/filename.h"
52 #include "wx/imaglist.h"
53 #include "wx/bmpcbox.h"
55 #include "itemcontainer.h"
58 #include "icons/bmpcombobox.xpm"
60 // Images loaded from file are reduced this width and height, if larger
61 #define IMG_SIZE_TRUNC 256
64 // ----------------------------------------------------------------------------
66 // ----------------------------------------------------------------------------
71 BitmapComboBoxPage_Reset
= wxID_HIGHEST
,
72 BitmapComboBoxPage_Insert
,
73 BitmapComboBoxPage_InsertText
,
74 BitmapComboBoxPage_ChangeHeight
,
75 BitmapComboBoxPage_LoadFromFile
,
76 BitmapComboBoxPage_SetFromFile
,
77 BitmapComboBoxPage_AddWidgetIcons
,
78 BitmapComboBoxPage_AddSeveralWithImages
,
79 BitmapComboBoxPage_AddSeveral
,
80 BitmapComboBoxPage_AddMany
,
81 BitmapComboBoxPage_Clear
,
82 BitmapComboBoxPage_Change
,
83 BitmapComboBoxPage_Delete
,
84 BitmapComboBoxPage_DeleteText
,
85 BitmapComboBoxPage_DeleteSel
,
86 BitmapComboBoxPage_Combo
,
87 BitmapComboBoxPage_ContainerTests
90 // kinds of comboboxes
98 // ----------------------------------------------------------------------------
99 // BitmapComboBoxWidgetsPage
100 // ----------------------------------------------------------------------------
102 class BitmapComboBoxWidgetsPage
: public ItemContainerWidgetsPage
105 BitmapComboBoxWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
107 virtual wxControl
*GetWidget() const { return m_combobox
; }
108 virtual wxItemContainer
* GetContainer() const { return m_combobox
; }
109 virtual void RecreateWidget() { CreateCombo(); }
111 // lazy creation of the content
112 virtual void CreateContent();
116 void OnButtonReset(wxCommandEvent
& event
);
117 void OnButtonChange(wxCommandEvent
& event
);
118 void OnButtonDelete(wxCommandEvent
& event
);
119 void OnButtonDeleteSel(wxCommandEvent
& event
);
120 void OnButtonClear(wxCommandEvent
& event
);
121 void OnButtonInsert(wxCommandEvent
&event
);
122 void OnTextChangeHeight(wxCommandEvent
& event
);
123 void OnButtonLoadFromFile(wxCommandEvent
& event
);
124 void OnButtonSetFromFile(wxCommandEvent
& event
);
125 void OnButtonAddSeveral(wxCommandEvent
& event
);
126 void OnButtonAddSeveralWithImages(wxCommandEvent
& event
);
127 void OnButtonAddWidgetIcons(wxCommandEvent
& event
);
128 void OnButtonAddMany(wxCommandEvent
& event
);
130 void OnComboBox(wxCommandEvent
& event
);
131 void OnDropDown(wxCommandEvent
& event
);
132 void OnCloseUp(wxCommandEvent
& event
);
133 void OnComboText(wxCommandEvent
& event
);
135 void OnCheckOrRadioBox(wxCommandEvent
& event
);
137 void OnTextPopupWidth(wxCommandEvent
& event
);
138 void OnTextPopupHeight(wxCommandEvent
& event
);
139 void OnTextButtonAll(wxCommandEvent
& event
);
141 void OnUpdateUIInsert(wxUpdateUIEvent
& event
);
142 void OnUpdateUIAddSeveral(wxUpdateUIEvent
& event
);
143 void OnUpdateUIAddSeveralWithImages(wxUpdateUIEvent
& event
);
144 void OnUpdateUIClearButton(wxUpdateUIEvent
& event
);
145 void OnUpdateUIDeleteButton(wxUpdateUIEvent
& event
);
146 void OnUpdateUIItemManipulator(wxUpdateUIEvent
& event
);
147 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
149 // reset the bmpcombobox parameters
152 // (re)create the bmpcombobox
155 // helpers for creating bitmaps
156 wxBitmap
CreateBitmap(const wxColour
& colour
);
157 wxBitmap
LoadBitmap(const wxString
& filepath
);
158 wxBitmap
QueryBitmap(wxString
* pStr
);
160 void LoadWidgetImages( wxArrayString
* strings
, wxImageList
* images
);
162 wxSizer
*CreateSizerWithSmallTextAndLabel(const wxString
& label
,
164 wxTextCtrl
**ppText
);
167 void RescaleImage(wxImage
& image
, int w
, int h
);
173 // the sel mode radiobox
174 wxRadioBox
*m_radioKind
;
176 // the checkboxes for styles
177 wxCheckBox
*m_chkSort
,
180 // the combobox itself and the sizer it is in
181 wxBitmapComboBox
*m_combobox
;
182 wxSizer
*m_sizerCombo
;
184 // the text entries for "Add/change string" and "Delete" buttons
185 wxTextCtrl
*m_textInsert
,
191 DECLARE_EVENT_TABLE()
192 DECLARE_WIDGETS_PAGE(BitmapComboBoxWidgetsPage
)
195 // ----------------------------------------------------------------------------
197 // ----------------------------------------------------------------------------
199 BEGIN_EVENT_TABLE(BitmapComboBoxWidgetsPage
, WidgetsPage
)
200 EVT_BUTTON(BitmapComboBoxPage_Reset
, BitmapComboBoxWidgetsPage::OnButtonReset
)
201 EVT_BUTTON(BitmapComboBoxPage_Change
, BitmapComboBoxWidgetsPage::OnButtonChange
)
202 EVT_BUTTON(BitmapComboBoxPage_Delete
, BitmapComboBoxWidgetsPage::OnButtonDelete
)
203 EVT_BUTTON(BitmapComboBoxPage_DeleteSel
, BitmapComboBoxWidgetsPage::OnButtonDeleteSel
)
204 EVT_BUTTON(BitmapComboBoxPage_Clear
, BitmapComboBoxWidgetsPage::OnButtonClear
)
205 EVT_BUTTON(BitmapComboBoxPage_Insert
, BitmapComboBoxWidgetsPage::OnButtonInsert
)
206 EVT_BUTTON(BitmapComboBoxPage_AddSeveral
, BitmapComboBoxWidgetsPage::OnButtonAddSeveral
)
207 EVT_BUTTON(BitmapComboBoxPage_AddSeveralWithImages
, BitmapComboBoxWidgetsPage::OnButtonAddSeveralWithImages
)
208 EVT_BUTTON(BitmapComboBoxPage_AddWidgetIcons
, BitmapComboBoxWidgetsPage::OnButtonAddWidgetIcons
)
209 EVT_BUTTON(BitmapComboBoxPage_AddMany
, BitmapComboBoxWidgetsPage::OnButtonAddMany
)
210 EVT_BUTTON(BitmapComboBoxPage_LoadFromFile
, BitmapComboBoxWidgetsPage::OnButtonLoadFromFile
)
211 EVT_BUTTON(BitmapComboBoxPage_SetFromFile
, BitmapComboBoxWidgetsPage::OnButtonSetFromFile
)
212 EVT_BUTTON(BitmapComboBoxPage_ContainerTests
, ItemContainerWidgetsPage::OnButtonTestItemContainer
)
214 EVT_TEXT_ENTER(BitmapComboBoxPage_InsertText
, BitmapComboBoxWidgetsPage::OnButtonInsert
)
215 EVT_TEXT(BitmapComboBoxPage_ChangeHeight
, BitmapComboBoxWidgetsPage::OnTextChangeHeight
)
216 EVT_TEXT_ENTER(BitmapComboBoxPage_DeleteText
, BitmapComboBoxWidgetsPage::OnButtonDelete
)
218 EVT_UPDATE_UI(BitmapComboBoxPage_Reset
, BitmapComboBoxWidgetsPage::OnUpdateUIResetButton
)
219 EVT_UPDATE_UI(BitmapComboBoxPage_Insert
, BitmapComboBoxWidgetsPage::OnUpdateUIInsert
)
220 EVT_UPDATE_UI(BitmapComboBoxPage_LoadFromFile
, BitmapComboBoxWidgetsPage::OnUpdateUIInsert
)
221 EVT_UPDATE_UI(BitmapComboBoxPage_AddSeveral
, BitmapComboBoxWidgetsPage::OnUpdateUIAddSeveral
)
222 EVT_UPDATE_UI(BitmapComboBoxPage_AddSeveralWithImages
, BitmapComboBoxWidgetsPage::OnUpdateUIAddSeveralWithImages
)
223 EVT_UPDATE_UI(BitmapComboBoxPage_Clear
, BitmapComboBoxWidgetsPage::OnUpdateUIClearButton
)
224 EVT_UPDATE_UI(BitmapComboBoxPage_DeleteText
, BitmapComboBoxWidgetsPage::OnUpdateUIClearButton
)
225 EVT_UPDATE_UI(BitmapComboBoxPage_Delete
, BitmapComboBoxWidgetsPage::OnUpdateUIDeleteButton
)
226 EVT_UPDATE_UI(BitmapComboBoxPage_Change
, BitmapComboBoxWidgetsPage::OnUpdateUIItemManipulator
)
227 EVT_UPDATE_UI(BitmapComboBoxPage_SetFromFile
, BitmapComboBoxWidgetsPage::OnUpdateUIItemManipulator
)
228 EVT_UPDATE_UI(BitmapComboBoxPage_DeleteSel
, BitmapComboBoxWidgetsPage::OnUpdateUIItemManipulator
)
230 EVT_COMBOBOX_DROPDOWN(BitmapComboBoxPage_Combo
, BitmapComboBoxWidgetsPage::OnDropDown
)
231 EVT_COMBOBOX_CLOSEUP(BitmapComboBoxPage_Combo
, BitmapComboBoxWidgetsPage::OnCloseUp
)
232 EVT_COMBOBOX(BitmapComboBoxPage_Combo
, BitmapComboBoxWidgetsPage::OnComboBox
)
233 EVT_TEXT(BitmapComboBoxPage_Combo
, BitmapComboBoxWidgetsPage::OnComboText
)
234 EVT_TEXT_ENTER(BitmapComboBoxPage_Combo
, BitmapComboBoxWidgetsPage::OnComboText
)
236 EVT_CHECKBOX(wxID_ANY
, BitmapComboBoxWidgetsPage::OnCheckOrRadioBox
)
237 EVT_RADIOBOX(wxID_ANY
, BitmapComboBoxWidgetsPage::OnCheckOrRadioBox
)
240 // ============================================================================
242 // ============================================================================
244 #if defined(__WXMSW__) || defined(__WXGTK__)
245 #define NATIVE_OR_GENERIC_CTRLS NATIVE_CTRLS
247 #define NATIVE_OR_GENERIC_CTRLS GENERIC_CTRLS
250 IMPLEMENT_WIDGETS_PAGE(BitmapComboBoxWidgetsPage
, _T("BitmapCombobox"),
251 NATIVE_OR_GENERIC_CTRLS
| WITH_ITEMS_CTRLS
| COMBO_CTRLS
255 BitmapComboBoxWidgetsPage::BitmapComboBoxWidgetsPage(WidgetsBookCtrl
*book
,
256 wxImageList
*imaglist
)
257 : ItemContainerWidgetsPage(book
, imaglist
, bmpcombobox_xpm
)
261 m_chkReadonly
= NULL
;
272 // create a sizer containing a label and a small text ctrl
273 wxSizer
*BitmapComboBoxWidgetsPage::CreateSizerWithSmallTextAndLabel(const wxString
& label
,
277 wxControl
* control
= new wxStaticText(this, wxID_ANY
, label
);
278 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
279 wxTextCtrl
*text
= new wxTextCtrl(this, id
, wxEmptyString
,
280 wxDefaultPosition
, wxSize(50,wxDefaultCoord
), wxTE_PROCESS_ENTER
);
282 sizerRow
->Add(control
, 0, wxRIGHT
| wxALIGN_CENTRE_VERTICAL
, 5);
283 sizerRow
->Add(text
, 1, wxFIXED_MINSIZE
| wxLEFT
| wxALIGN_CENTRE_VERTICAL
, 5);
291 void BitmapComboBoxWidgetsPage::CreateContent()
294 What we create here is a frame having 3 panes: style pane is the
295 leftmost one, in the middle the pane with buttons allowing to perform
296 miscellaneous combobox operations and the pane containing the combobox
301 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
303 wxSizer
*sizerLeft
= new wxBoxSizer(wxVERTICAL
);
305 // left pane - style box
306 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
309 // should be in sync with ComboKind_XXX values
310 static const wxString kinds
[] =
317 m_radioKind
= new wxRadioBox(this, wxID_ANY
, _T("Combobox &kind:"),
318 wxDefaultPosition
, wxDefaultSize
,
319 WXSIZEOF(kinds
), kinds
,
320 1, wxRA_SPECIFY_COLS
);
322 wxSizer
*sizerStyle
= new wxStaticBoxSizer(box
, wxVERTICAL
);
324 m_chkSort
= CreateCheckBoxAndAddToSizer(sizerStyle
, _T("&Sort items"));
325 m_chkReadonly
= CreateCheckBoxAndAddToSizer(sizerStyle
, _T("&Read only"));
327 wxButton
*btn
= new wxButton(this, BitmapComboBoxPage_Reset
, _T("&Reset"));
328 sizerStyle
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 3);
330 sizerLeft
->Add(sizerStyle
, 0, wxGROW
| wxALIGN_CENTRE_HORIZONTAL
);
331 sizerLeft
->Add(m_radioKind
, 0, wxGROW
| wxALL
, 5);
333 // left pane - other options box
334 box
= new wxStaticBox(this, wxID_ANY
, _T("Demo options"));
336 wxSizer
*sizerOptions
= new wxStaticBoxSizer(box
, wxVERTICAL
);
338 sizerRow
= CreateSizerWithSmallTextAndLabel(_T("Control &height:"),
339 BitmapComboBoxPage_ChangeHeight
,
340 &m_textChangeHeight
);
341 m_textChangeHeight
->SetSize(20, wxDefaultCoord
);
342 sizerOptions
->Add(sizerRow
, 0, wxALL
| wxFIXED_MINSIZE
/*| wxGROW*/, 5);
344 sizerLeft
->Add(sizerOptions
, 0, wxGROW
| wxALIGN_CENTRE_HORIZONTAL
| wxTOP
, 2);
347 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
,
348 _T("&Change wxBitmapComboBox contents"));
349 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
351 btn
= new wxButton(this, BitmapComboBoxPage_ContainerTests
, _T("Run &tests"));
352 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
355 btn
= new wxButton(this, BitmapComboBoxPage_AddWidgetIcons
, _T("Add &widget icons"));
356 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
358 btn
= new wxButton(this, BitmapComboBoxPage_LoadFromFile
, _T("Insert image from &file"));
359 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
361 btn
= new wxButton(this, BitmapComboBoxPage_SetFromFile
, _T("&Set image from file"));
362 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
365 btn
= new wxButton(this, BitmapComboBoxPage_AddSeveralWithImages
, _T("A&ppend a few strings with images"));
366 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
368 btn
= new wxButton(this, BitmapComboBoxPage_AddSeveral
, _T("Append a &few strings"));
369 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
371 btn
= new wxButton(this, BitmapComboBoxPage_AddMany
, _T("Append &many strings"));
372 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
374 sizerRow
= CreateSizerWithTextAndButton(BitmapComboBoxPage_Delete
,
375 _T("&Delete this item"),
376 BitmapComboBoxPage_DeleteText
,
378 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
380 btn
= new wxButton(this, BitmapComboBoxPage_DeleteSel
, _T("Delete &selection"));
381 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
383 btn
= new wxButton(this, BitmapComboBoxPage_Clear
, _T("&Clear"));
384 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
387 wxInitAllImageHandlers();
391 wxSizer
*sizerRight
= new wxBoxSizer(wxVERTICAL
);
392 m_combobox
= new wxBitmapComboBox();
393 m_combobox
->Create(this, BitmapComboBoxPage_Combo
, wxEmptyString
,
394 wxDefaultPosition
, wxDefaultSize
,
398 #if defined(wxGENERIC_BITMAPCOMBOBOX)
399 // This will sure make the list look nicer when larger images are used.
400 m_combobox
->SetPopupMaxHeight(600);
403 sizerRight
->Add(m_combobox
, 0, wxGROW
| wxALL
, 5);
404 sizerRight
->SetMinSize(150, 0);
405 m_sizerCombo
= sizerRight
; // save it to modify it later
407 // the 3 panes panes compose the window
408 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
409 sizerTop
->Add(sizerMiddle
, 5, wxGROW
| wxALL
, 10);
410 sizerTop
->Add(sizerRight
, 4, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
412 // final initializations
418 // ----------------------------------------------------------------------------
420 // ----------------------------------------------------------------------------
422 void BitmapComboBoxWidgetsPage::Reset()
424 m_chkSort
->SetValue(false);
425 m_chkReadonly
->SetValue(true);
428 void BitmapComboBoxWidgetsPage::CreateCombo()
430 int flags
= ms_defaultFlags
;
432 if ( m_chkSort
->GetValue() )
434 if ( m_chkReadonly
->GetValue() )
435 flags
|= wxCB_READONLY
;
437 switch ( m_radioKind
->GetSelection() )
440 wxFAIL_MSG( _T("unknown combo kind") );
443 case ComboKind_Default
:
446 case ComboKind_Simple
:
447 flags
|= wxCB_SIMPLE
;
450 case ComboKind_DropDown
:
451 flags
= wxCB_DROPDOWN
;
456 wxArrayPtrVoid bitmaps
;
459 unsigned int count
= m_combobox
->GetCount();
460 for ( unsigned int n
= 0; n
< count
; n
++ )
462 items
.Add(m_combobox
->GetString(n
));
463 bitmaps
.Add(new wxBitmap(m_combobox
->GetItemBitmap(n
)));
466 m_sizerCombo
->Detach( m_combobox
);
470 m_combobox
= new wxBitmapComboBox();
471 m_combobox
->Create(this, BitmapComboBoxPage_Combo
, wxEmptyString
,
472 wxDefaultPosition
, wxDefaultSize
,
476 #if defined(wxGENERIC_BITMAPCOMBOBOX)
477 // This will sure make the list look nicer when larger images are used.
478 m_combobox
->SetPopupMaxHeight(600);
481 unsigned int count
= items
.GetCount();
482 for ( unsigned int n
= 0; n
< count
; n
++ )
484 wxBitmap
* bmp
= (wxBitmap
*) bitmaps
[n
];
485 m_combobox
->Append(items
[n
], *bmp
);
489 m_sizerCombo
->Add(m_combobox
, 0, wxGROW
| wxALL
, 5);
490 m_sizerCombo
->Layout();
492 // Allow changing height inorder to demonstrate flexible
493 // size of image "thumbnail" painted in the control itself.
495 m_textChangeHeight
->GetValue().ToLong(&h
);
497 m_combobox
->SetSize(wxDefaultCoord
, h
);
500 // ----------------------------------------------------------------------------
502 // ----------------------------------------------------------------------------
504 void BitmapComboBoxWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
511 void BitmapComboBoxWidgetsPage::OnButtonChange(wxCommandEvent
& WXUNUSED(event
))
513 int sel
= m_combobox
->GetSelection();
514 if ( sel
!= wxNOT_FOUND
)
517 m_combobox
->SetString(sel
, m_textChange
->GetValue());
519 wxLogMessage(_T("Not implemented in wxGTK"));
524 void BitmapComboBoxWidgetsPage::OnButtonDelete(wxCommandEvent
& WXUNUSED(event
))
527 if ( !m_textDelete
->GetValue().ToULong(&n
) ||
528 (n
>= m_combobox
->GetCount()) )
533 m_combobox
->Delete(n
);
536 void BitmapComboBoxWidgetsPage::OnButtonDeleteSel(wxCommandEvent
& WXUNUSED(event
))
538 int sel
= m_combobox
->GetSelection();
539 if ( sel
!= wxNOT_FOUND
)
541 m_combobox
->Delete(sel
);
545 void BitmapComboBoxWidgetsPage::OnButtonClear(wxCommandEvent
& WXUNUSED(event
))
550 void BitmapComboBoxWidgetsPage::OnButtonInsert(wxCommandEvent
& WXUNUSED(event
))
552 static unsigned int s_item
= 0;
554 wxString s
= m_textInsert
->GetValue();
555 if ( !m_textInsert
->IsModified() )
557 // update the default string
558 m_textInsert
->SetValue(wxString::Format(_T("test item %u"), ++s_item
));
561 int sel
= m_combobox
->GetSelection();
562 if ( sel
== wxNOT_FOUND
)
563 sel
= m_combobox
->GetCount();
565 m_combobox
->Insert(s
, wxNullBitmap
, m_combobox
->GetSelection());
568 void BitmapComboBoxWidgetsPage::OnTextChangeHeight(wxCommandEvent
& WXUNUSED(event
))
571 if ( m_textChangeHeight
)
572 m_textChangeHeight
->GetValue().ToLong(&h
);
575 m_combobox
->SetSize(wxDefaultCoord
, h
);
578 void BitmapComboBoxWidgetsPage::OnButtonLoadFromFile(wxCommandEvent
& WXUNUSED(event
))
581 int sel
= m_combobox
->GetSelection();
582 if ( sel
== wxNOT_FOUND
)
583 sel
= m_combobox
->GetCount();
585 wxBitmap bmp
= QueryBitmap(&s
);
587 m_combobox
->Insert(s
, bmp
, sel
);
590 void BitmapComboBoxWidgetsPage::OnButtonSetFromFile(wxCommandEvent
& WXUNUSED(event
))
592 wxBitmap bmp
= QueryBitmap(NULL
);
594 m_combobox
->SetItemBitmap(m_combobox
->GetSelection(), bmp
);
597 void BitmapComboBoxWidgetsPage::OnButtonAddMany(wxCommandEvent
& WXUNUSED(event
))
599 // "many" means 1000 here
600 for ( unsigned int n
= 0; n
< 1000; n
++ )
602 m_combobox
->Append(wxString::Format(_T("item #%u"), n
));
606 void BitmapComboBoxWidgetsPage::OnButtonAddSeveral(wxCommandEvent
& WXUNUSED(event
))
608 m_combobox
->Append(_T("First"));
609 m_combobox
->Append(_T("another one"));
610 m_combobox
->Append(_T("and the last (very very very very very very very very very very long) one"));
613 void BitmapComboBoxWidgetsPage::OnButtonAddSeveralWithImages(wxCommandEvent
& WXUNUSED(event
))
615 static const struct TestEntry
621 { "Red circle", 0x0000ff },
622 { "Blue circle", 0xff0000 },
623 { "Green circle", 0x00ff00 },
624 { "Black circle", 0x000000 },
627 for ( unsigned i
= 0; i
< WXSIZEOF(s_entries
); i
++ )
629 const TestEntry
& e
= s_entries
[i
];
630 m_combobox
->Append(e
.text
, CreateBitmap(wxColour(e
.rgb
)));
635 void BitmapComboBoxWidgetsPage::RescaleImage(wxImage
& image
, int w
, int h
)
637 if ( image
.GetWidth() == w
&& image
.GetHeight() == h
)
640 if ( w
<= 0 || h
<= 0 )
643 static bool isFirstScale
= true;
645 if ( isFirstScale
&& m_combobox
->GetCount() > 0 )
647 wxMessageBox( wxT("wxBitmapComboBox normally only supports images of one size. ")
648 wxT("However, for demonstration purposes, loaded bitmaps are scaled to fit ")
649 wxT("using wxImage::Rescale."),
654 isFirstScale
= false;
661 void BitmapComboBoxWidgetsPage::LoadWidgetImages( wxArrayString
* strings
, wxImageList
* images
)
665 fn
.AppendDir(wxT("icons"));
667 wxSetCursor(*wxHOURGLASS_CURSOR
);
669 if ( !wxDir::Exists(fn
.GetFullPath()) ||
670 !wxDir::GetAllFiles(fn
.GetFullPath(),strings
,wxT("*.xpm")) )
672 // Try ../../samples/widgets/icons
675 fn
.AppendDir(wxT("icons"));
676 if ( !wxDir::Exists(fn
.GetFullPath()) ||
677 !wxDir::GetAllFiles(fn
.GetFullPath(),strings
,wxT("*.xpm")) )
679 // Try ../../../samples/widgets/icons
684 fn
.AppendDir(wxT("samples"));
685 fn
.AppendDir(wxT("widgets"));
686 fn
.AppendDir(wxT("icons"));
687 if ( !wxDir::Exists(fn
.GetFullPath()) ||
688 !wxDir::GetAllFiles(fn
.GetFullPath(),strings
,wxT("*.xpm")) )
690 wxLogWarning(wxT("Could not load widget icons."));
691 wxSetCursor(*wxSTANDARD_CURSOR
);
699 // Get size of existing images in list
700 wxSize foundSize
= m_combobox
->GetBitmapSize();
702 for ( i
=0; i
<strings
->size(); i
++ )
704 fn
.SetFullName((*strings
)[i
]);
705 wxString name
= fn
.GetName();
707 // Handle few exceptions
708 if ( name
== wxT("bmpbtn") )
710 strings
->RemoveAt(i
);
716 wxASSERT(fn
.FileExists());
717 wxImage
image(fn
.GetFullPath());
718 wxASSERT(image
.Ok());
719 RescaleImage(image
, foundSize
.x
, foundSize
.y
);
721 wxASSERT( bmp
.Ok() );
723 wxBitmap
bmp(wxNullBitmap
);
726 (*strings
)[i
] = name
;
728 // if the combobox is empty, use as bitmap size of the image list
729 // the size of the first valid image loaded
730 if (foundSize
== wxDefaultSize
)
731 foundSize
= bmp
.GetSize();
735 wxSetCursor(*wxSTANDARD_CURSOR
);
738 void BitmapComboBoxWidgetsPage::OnButtonAddWidgetIcons(wxCommandEvent
& WXUNUSED(event
))
740 wxArrayString strings
;
742 wxSize sz
= m_combobox
->GetBitmapSize();
749 wxImageList
images(sz
.x
, sz
.y
);
751 LoadWidgetImages(&strings
, &images
);
755 for ( i
=0; i
<strings
.size(); i
++ )
757 m_combobox
->Append(strings
[i
], images
.GetBitmap(i
));
761 void BitmapComboBoxWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
764 event
.Enable( m_chkSort
->GetValue() || m_chkReadonly
->GetValue() );
767 void BitmapComboBoxWidgetsPage::OnUpdateUIInsert(wxUpdateUIEvent
& event
)
771 bool enable
= !(m_combobox
->GetWindowStyle() & wxCB_SORT
);
773 event
.Enable(enable
);
777 void BitmapComboBoxWidgetsPage::OnUpdateUIDeleteButton(wxUpdateUIEvent
& event
)
782 event
.Enable(m_textDelete
->GetValue().ToULong(&n
) &&
783 (n
< (unsigned)m_combobox
->GetCount()));
787 void BitmapComboBoxWidgetsPage::OnUpdateUIItemManipulator(wxUpdateUIEvent
& event
)
790 event
.Enable(m_combobox
->GetSelection() != wxNOT_FOUND
);
793 void BitmapComboBoxWidgetsPage::OnUpdateUIClearButton(wxUpdateUIEvent
& event
)
796 event
.Enable(m_combobox
->GetCount() != 0);
799 void BitmapComboBoxWidgetsPage::OnUpdateUIAddSeveral(wxUpdateUIEvent
& event
)
802 event
.Enable(!(m_combobox
->GetWindowStyle() & wxCB_SORT
));
805 void BitmapComboBoxWidgetsPage::OnUpdateUIAddSeveralWithImages(wxUpdateUIEvent
& event
)
808 event
.Enable(!(m_combobox
->GetWindowStyle() & wxCB_SORT
));
811 void BitmapComboBoxWidgetsPage::OnComboText(wxCommandEvent
& event
)
816 wxString s
= event
.GetString();
818 wxASSERT_MSG( s
== m_combobox
->GetValue(),
819 _T("event and combobox values should be the same") );
821 if (event
.GetEventType() == wxEVT_COMMAND_TEXT_ENTER
)
822 wxLogMessage(_T("BitmapCombobox enter pressed (now '%s')"), s
.c_str());
824 wxLogMessage(_T("BitmapCombobox text changed (now '%s')"), s
.c_str());
827 void BitmapComboBoxWidgetsPage::OnComboBox(wxCommandEvent
& event
)
829 long sel
= event
.GetInt();
830 m_textDelete
->SetValue(wxString::Format(_T("%ld"), sel
));
832 wxLogMessage(_T("BitmapCombobox item %ld selected"), sel
);
834 wxLogMessage(_T("BitmapCombobox GetValue(): %s"), m_combobox
->GetValue().c_str() );
837 void BitmapComboBoxWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
843 wxBitmap
BitmapComboBoxWidgetsPage::LoadBitmap(const wxString
& filepath
)
845 // Get size of existing images in list
846 wxSize foundSize
= m_combobox
->GetBitmapSize();
848 // Have some reasonable maximum size
849 if ( foundSize
.x
<= 0 )
851 foundSize
.x
= IMG_SIZE_TRUNC
;
852 foundSize
.y
= IMG_SIZE_TRUNC
;
855 wxImage
image(filepath
);
858 // Rescale very large images
859 int ow
= image
.GetWidth();
860 int oh
= image
.GetHeight();
862 if ( foundSize
.x
> 0 &&
863 (ow
!= foundSize
.x
|| oh
!= foundSize
.y
) )
866 if ( w
> foundSize
.x
)
869 if ( h
> foundSize
.y
)
872 RescaleImage(image
, w
, h
);
875 return wxBitmap(image
);
881 wxBitmap
BitmapComboBoxWidgetsPage::LoadBitmap(const wxString
& WXUNUSED(filepath
))
887 wxBitmap
BitmapComboBoxWidgetsPage::QueryBitmap(wxString
* pStr
)
889 wxString filepath
= wxLoadFileSelector(wxT("image"),
896 ::wxSetCursor( *wxHOURGLASS_CURSOR
);
898 if ( filepath
.length() )
902 *pStr
= wxFileName(filepath
).GetName();
905 bitmap
= LoadBitmap(filepath
);
909 wxLogDebug(wxT("%i, %i"),bitmap
.GetWidth(), bitmap
.GetHeight());
911 ::wxSetCursor( *wxSTANDARD_CURSOR
);
916 wxBitmap
BitmapComboBoxWidgetsPage::CreateBitmap(const wxColour
& colour
)
923 dc
.SelectObject(bmp
);
925 // Draw transparent background
926 wxColour
magic(255, 0, 255);
927 wxBrush
magicBrush(magic
);
928 dc
.SetBrush(magicBrush
);
929 dc
.SetPen(*wxTRANSPARENT_PEN
);
930 dc
.DrawRectangle(0, 0, w
, h
);
932 // Draw image content
933 dc
.SetBrush(wxBrush(colour
));
934 dc
.DrawCircle(h
/2, h
/2+1, h
/2);
936 dc
.SelectObject(wxNullBitmap
);
938 // Finalize transparency with a mask
939 wxMask
*mask
= new wxMask(bmp
, magic
);
945 void BitmapComboBoxWidgetsPage::OnDropDown(wxCommandEvent
& event
)
947 wxLogMessage(_T("Combobox dropped down"));
950 void BitmapComboBoxWidgetsPage::OnCloseUp(wxCommandEvent
& event
)
952 wxLogMessage(_T("Combobox closed up"));
955 #endif // wxUSE_BITMAPCOMBOBOX