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
91 // ----------------------------------------------------------------------------
92 // BitmapComboBoxWidgetsPage
93 // ----------------------------------------------------------------------------
95 class BitmapComboBoxWidgetsPage
: public ItemContainerWidgetsPage
98 BitmapComboBoxWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
100 virtual wxControl
*GetWidget() const { return m_combobox
; }
101 virtual wxItemContainer
* GetContainer() const { return m_combobox
; }
102 virtual void RecreateWidget() { CreateCombo(); }
104 // lazy creation of the content
105 virtual void CreateContent();
109 void OnButtonReset(wxCommandEvent
& event
);
110 void OnButtonChange(wxCommandEvent
& event
);
111 void OnButtonDelete(wxCommandEvent
& event
);
112 void OnButtonDeleteSel(wxCommandEvent
& event
);
113 void OnButtonClear(wxCommandEvent
& event
);
114 void OnButtonInsert(wxCommandEvent
&event
);
115 void OnTextChangeHeight(wxCommandEvent
& event
);
116 void OnButtonLoadFromFile(wxCommandEvent
& event
);
117 void OnButtonSetFromFile(wxCommandEvent
& event
);
118 void OnButtonAddSeveral(wxCommandEvent
& event
);
119 void OnButtonAddSeveralWithImages(wxCommandEvent
& event
);
120 void OnButtonAddWidgetIcons(wxCommandEvent
& event
);
121 void OnButtonAddMany(wxCommandEvent
& event
);
123 void OnComboBox(wxCommandEvent
& event
);
124 void OnComboText(wxCommandEvent
& event
);
126 void OnCheckOrRadioBox(wxCommandEvent
& event
);
128 void OnTextPopupWidth(wxCommandEvent
& event
);
129 void OnTextPopupHeight(wxCommandEvent
& event
);
130 void OnTextButtonAll(wxCommandEvent
& event
);
132 void OnUpdateUIInsert(wxUpdateUIEvent
& event
);
133 void OnUpdateUIAddSeveral(wxUpdateUIEvent
& event
);
134 void OnUpdateUIAddSeveralWithImages(wxUpdateUIEvent
& event
);
135 void OnUpdateUIClearButton(wxUpdateUIEvent
& event
);
136 void OnUpdateUIDeleteButton(wxUpdateUIEvent
& event
);
137 void OnUpdateUIItemManipulator(wxUpdateUIEvent
& event
);
138 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
140 // reset the bmpcombobox parameters
143 // (re)create the bmpcombobox
146 // helpers for creating bitmaps
147 wxBitmap
CreateBitmap(const wxColour
& colour
);
148 wxBitmap
CreateRandomBitmap(wxString
* pStr
);
149 wxBitmap
LoadBitmap(const wxString
& filepath
);
150 wxBitmap
QueryBitmap(wxString
* pStr
);
152 void LoadWidgetImages( wxArrayString
* strings
, wxImageList
* images
);
154 wxSizer
*CreateSizerWithSmallTextAndLabel(const wxString
& label
,
156 wxTextCtrl
**ppText
);
159 void RescaleImage(wxImage
& image
, int w
, int h
);
165 // the checkboxes for styles
166 wxCheckBox
*m_chkSort
,
169 // the combobox itself and the sizer it is in
170 wxBitmapComboBox
*m_combobox
;
171 wxSizer
*m_sizerCombo
;
173 // the text entries for "Add/change string" and "Delete" buttons
174 wxTextCtrl
*m_textInsert
,
180 DECLARE_EVENT_TABLE()
181 DECLARE_WIDGETS_PAGE(BitmapComboBoxWidgetsPage
)
184 // ----------------------------------------------------------------------------
186 // ----------------------------------------------------------------------------
188 BEGIN_EVENT_TABLE(BitmapComboBoxWidgetsPage
, WidgetsPage
)
189 EVT_BUTTON(BitmapComboBoxPage_Reset
, BitmapComboBoxWidgetsPage::OnButtonReset
)
190 EVT_BUTTON(BitmapComboBoxPage_Change
, BitmapComboBoxWidgetsPage::OnButtonChange
)
191 EVT_BUTTON(BitmapComboBoxPage_Delete
, BitmapComboBoxWidgetsPage::OnButtonDelete
)
192 EVT_BUTTON(BitmapComboBoxPage_DeleteSel
, BitmapComboBoxWidgetsPage::OnButtonDeleteSel
)
193 EVT_BUTTON(BitmapComboBoxPage_Clear
, BitmapComboBoxWidgetsPage::OnButtonClear
)
194 EVT_BUTTON(BitmapComboBoxPage_Insert
, BitmapComboBoxWidgetsPage::OnButtonInsert
)
195 EVT_BUTTON(BitmapComboBoxPage_AddSeveral
, BitmapComboBoxWidgetsPage::OnButtonAddSeveral
)
196 EVT_BUTTON(BitmapComboBoxPage_AddSeveralWithImages
, BitmapComboBoxWidgetsPage::OnButtonAddSeveralWithImages
)
197 EVT_BUTTON(BitmapComboBoxPage_AddWidgetIcons
, BitmapComboBoxWidgetsPage::OnButtonAddWidgetIcons
)
198 EVT_BUTTON(BitmapComboBoxPage_AddMany
, BitmapComboBoxWidgetsPage::OnButtonAddMany
)
199 EVT_BUTTON(BitmapComboBoxPage_LoadFromFile
, BitmapComboBoxWidgetsPage::OnButtonLoadFromFile
)
200 EVT_BUTTON(BitmapComboBoxPage_SetFromFile
, BitmapComboBoxWidgetsPage::OnButtonSetFromFile
)
201 EVT_BUTTON(BitmapComboBoxPage_ContainerTests
, ItemContainerWidgetsPage::OnButtonTestItemContainer
)
203 EVT_TEXT_ENTER(BitmapComboBoxPage_InsertText
, BitmapComboBoxWidgetsPage::OnButtonInsert
)
204 EVT_TEXT(BitmapComboBoxPage_ChangeHeight
, BitmapComboBoxWidgetsPage::OnTextChangeHeight
)
205 EVT_TEXT_ENTER(BitmapComboBoxPage_DeleteText
, BitmapComboBoxWidgetsPage::OnButtonDelete
)
207 EVT_UPDATE_UI(BitmapComboBoxPage_Reset
, BitmapComboBoxWidgetsPage::OnUpdateUIResetButton
)
208 EVT_UPDATE_UI(BitmapComboBoxPage_Insert
, BitmapComboBoxWidgetsPage::OnUpdateUIInsert
)
209 EVT_UPDATE_UI(BitmapComboBoxPage_LoadFromFile
, BitmapComboBoxWidgetsPage::OnUpdateUIInsert
)
210 EVT_UPDATE_UI(BitmapComboBoxPage_AddSeveral
, BitmapComboBoxWidgetsPage::OnUpdateUIAddSeveral
)
211 EVT_UPDATE_UI(BitmapComboBoxPage_AddSeveralWithImages
, BitmapComboBoxWidgetsPage::OnUpdateUIAddSeveralWithImages
)
212 EVT_UPDATE_UI(BitmapComboBoxPage_Clear
, BitmapComboBoxWidgetsPage::OnUpdateUIClearButton
)
213 EVT_UPDATE_UI(BitmapComboBoxPage_DeleteText
, BitmapComboBoxWidgetsPage::OnUpdateUIClearButton
)
214 EVT_UPDATE_UI(BitmapComboBoxPage_Delete
, BitmapComboBoxWidgetsPage::OnUpdateUIDeleteButton
)
215 EVT_UPDATE_UI(BitmapComboBoxPage_Change
, BitmapComboBoxWidgetsPage::OnUpdateUIItemManipulator
)
216 EVT_UPDATE_UI(BitmapComboBoxPage_SetFromFile
, BitmapComboBoxWidgetsPage::OnUpdateUIItemManipulator
)
217 EVT_UPDATE_UI(BitmapComboBoxPage_DeleteSel
, BitmapComboBoxWidgetsPage::OnUpdateUIItemManipulator
)
219 EVT_COMBOBOX(BitmapComboBoxPage_Combo
, BitmapComboBoxWidgetsPage::OnComboBox
)
220 EVT_TEXT(BitmapComboBoxPage_Combo
, BitmapComboBoxWidgetsPage::OnComboText
)
221 EVT_TEXT_ENTER(BitmapComboBoxPage_Combo
, BitmapComboBoxWidgetsPage::OnComboText
)
223 EVT_CHECKBOX(wxID_ANY
, BitmapComboBoxWidgetsPage::OnCheckOrRadioBox
)
224 EVT_RADIOBOX(wxID_ANY
, BitmapComboBoxWidgetsPage::OnCheckOrRadioBox
)
227 // ============================================================================
229 // ============================================================================
233 IMPLEMENT_WIDGETS_PAGE(BitmapComboBoxWidgetsPage
, _T("BitmapCombobox"),
234 GENERIC_CTRLS
| WITH_ITEMS_CTRLS
| COMBO_CTRLS
238 BitmapComboBoxWidgetsPage::BitmapComboBoxWidgetsPage(WidgetsBookCtrl
*book
,
239 wxImageList
*imaglist
)
240 : ItemContainerWidgetsPage(book
, imaglist
, bmpcombobox_xpm
)
244 m_chkReadonly
= (wxCheckBox
*)NULL
;
246 m_combobox
= (wxBitmapComboBox
*)NULL
;
247 m_sizerCombo
= (wxSizer
*)NULL
;
250 // create a sizer containing a label and a small text ctrl
251 wxSizer
*BitmapComboBoxWidgetsPage::CreateSizerWithSmallTextAndLabel(const wxString
& label
,
255 wxControl
* control
= new wxStaticText(this, wxID_ANY
, label
);
256 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
257 wxTextCtrl
*text
= new wxTextCtrl(this, id
, wxEmptyString
,
258 wxDefaultPosition
, wxSize(50,wxDefaultCoord
), wxTE_PROCESS_ENTER
);
260 sizerRow
->Add(control
, 0, wxRIGHT
| wxALIGN_CENTRE_VERTICAL
, 5);
261 sizerRow
->Add(text
, 1, wxFIXED_MINSIZE
| wxLEFT
| wxALIGN_CENTRE_VERTICAL
, 5);
269 void BitmapComboBoxWidgetsPage::CreateContent()
272 What we create here is a frame having 3 panes: style pane is the
273 leftmost one, in the middle the pane with buttons allowing to perform
274 miscellaneous combobox operations and the pane containing the combobox
280 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
282 wxSizer
*sizerLeft
= new wxBoxSizer(wxVERTICAL
);
284 // left pane - style box
285 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
287 wxSizer
*sizerStyle
= new wxStaticBoxSizer(box
, wxVERTICAL
);
289 m_chkSort
= CreateCheckBoxAndAddToSizer(sizerStyle
, _T("&Sort items"));
290 m_chkReadonly
= CreateCheckBoxAndAddToSizer(sizerStyle
, _T("&Read only"));
292 wxButton
*btn
= new wxButton(this, BitmapComboBoxPage_Reset
, _T("&Reset"));
293 sizerStyle
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 3);
295 sizerLeft
->Add(sizerStyle
, 0, wxGROW
| wxALIGN_CENTRE_HORIZONTAL
);
297 // left pane - other options box
298 box
= new wxStaticBox(this, wxID_ANY
, _T("Demo options"));
300 wxSizer
*sizerOptions
= new wxStaticBoxSizer(box
, wxVERTICAL
);
302 sizerRow
= CreateSizerWithSmallTextAndLabel(_T("Control &height:"),
303 BitmapComboBoxPage_ChangeHeight
,
304 &m_textChangeHeight
);
305 m_textChangeHeight
->SetSize(20, wxDefaultCoord
);
306 sizerOptions
->Add(sizerRow
, 0, wxALL
| wxFIXED_MINSIZE
/*| wxGROW*/, 5);
308 sizerLeft
->Add(sizerOptions
, 0, wxGROW
| wxALIGN_CENTRE_HORIZONTAL
| wxTOP
, 2);
311 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
,
312 _T("&Change wxBitmapComboBox contents"));
313 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
315 btn
= new wxButton(this, BitmapComboBoxPage_ContainerTests
, _T("Run &tests"));
316 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
319 btn
= new wxButton(this, BitmapComboBoxPage_AddWidgetIcons
, _T("Add &widget icons"));
320 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
322 btn
= new wxButton(this, BitmapComboBoxPage_LoadFromFile
, _T("Insert image from &file"));
323 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
325 btn
= new wxButton(this, BitmapComboBoxPage_SetFromFile
, _T("&Set image from file"));
326 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
329 btn
= new wxButton(this, BitmapComboBoxPage_AddSeveralWithImages
, _T("A&ppend a few strings with images"));
330 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
332 btn
= new wxButton(this, BitmapComboBoxPage_AddSeveral
, _T("Append a &few strings"));
333 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
335 btn
= new wxButton(this, BitmapComboBoxPage_AddMany
, _T("Append &many strings"));
336 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
338 sizerRow
= CreateSizerWithTextAndButton(BitmapComboBoxPage_Delete
,
339 _T("&Delete this item"),
340 BitmapComboBoxPage_DeleteText
,
342 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
344 btn
= new wxButton(this, BitmapComboBoxPage_DeleteSel
, _T("Delete &selection"));
345 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
347 btn
= new wxButton(this, BitmapComboBoxPage_Clear
, _T("&Clear"));
348 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
351 wxInitAllImageHandlers();
355 wxSizer
*sizerRight
= new wxBoxSizer(wxVERTICAL
);
356 m_combobox
= new wxBitmapComboBox();
357 m_combobox
->Create(this, BitmapComboBoxPage_Combo
, wxEmptyString
,
358 wxDefaultPosition
, wxDefaultSize
,
362 #if defined(wxGENERIC_BITMAPCOMBOBOX)
363 // This will sure make the list look nicer when larger images are used.
364 m_combobox
->SetPopupMaxHeight(600);
367 sizerRight
->Add(m_combobox
, 0, wxGROW
| wxALL
, 5);
368 sizerRight
->SetMinSize(150, 0);
369 m_sizerCombo
= sizerRight
; // save it to modify it later
371 // the 3 panes panes compose the window
372 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
373 sizerTop
->Add(sizerMiddle
, 5, wxGROW
| wxALL
, 10);
374 sizerTop
->Add(sizerRight
, 4, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
376 // final initializations
382 // ----------------------------------------------------------------------------
384 // ----------------------------------------------------------------------------
386 void BitmapComboBoxWidgetsPage::Reset()
388 m_chkSort
->SetValue(false);
389 m_chkReadonly
->SetValue(true);
392 void BitmapComboBoxWidgetsPage::CreateCombo()
394 int flags
= ms_defaultFlags
;
396 if ( m_chkSort
->GetValue() )
398 if ( m_chkReadonly
->GetValue() )
399 flags
|= wxCB_READONLY
;
402 wxArrayPtrVoid bitmaps
;
405 unsigned int count
= m_combobox
->GetCount();
406 for ( unsigned int n
= 0; n
< count
; n
++ )
408 items
.Add(m_combobox
->GetString(n
));
409 bitmaps
.Add(new wxBitmap(m_combobox
->GetItemBitmap(n
)));
412 m_sizerCombo
->Detach( m_combobox
);
416 m_combobox
= new wxBitmapComboBox();
417 m_combobox
->Create(this, BitmapComboBoxPage_Combo
, wxEmptyString
,
418 wxDefaultPosition
, wxDefaultSize
,
422 #if defined(wxGENERIC_BITMAPCOMBOBOX)
423 // This will sure make the list look nicer when larger images are used.
424 m_combobox
->SetPopupMaxHeight(600);
427 unsigned int count
= items
.GetCount();
428 for ( unsigned int n
= 0; n
< count
; n
++ )
430 wxBitmap
* bmp
= (wxBitmap
*) bitmaps
[n
];
431 m_combobox
->Append(items
[n
], *bmp
);
435 m_sizerCombo
->Add(m_combobox
, 0, wxGROW
| wxALL
, 5);
436 m_sizerCombo
->Layout();
438 // Allow changing height inorder to demonstrate flexible
439 // size of image "thumbnail" painted in the control itself.
441 m_textChangeHeight
->GetValue().ToLong(&h
);
443 m_combobox
->SetSize(wxDefaultCoord
, h
);
446 // ----------------------------------------------------------------------------
448 // ----------------------------------------------------------------------------
450 void BitmapComboBoxWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
457 void BitmapComboBoxWidgetsPage::OnButtonChange(wxCommandEvent
& WXUNUSED(event
))
459 int sel
= m_combobox
->GetSelection();
460 if ( sel
!= wxNOT_FOUND
)
463 m_combobox
->SetString(sel
, m_textChange
->GetValue());
465 wxLogMessage(_T("Not implemented in wxGTK"));
470 void BitmapComboBoxWidgetsPage::OnButtonDelete(wxCommandEvent
& WXUNUSED(event
))
473 if ( !m_textDelete
->GetValue().ToULong(&n
) ||
474 (n
>= m_combobox
->GetCount()) )
479 m_combobox
->Delete(n
);
482 void BitmapComboBoxWidgetsPage::OnButtonDeleteSel(wxCommandEvent
& WXUNUSED(event
))
484 int sel
= m_combobox
->GetSelection();
485 if ( sel
!= wxNOT_FOUND
)
487 m_combobox
->Delete(sel
);
491 void BitmapComboBoxWidgetsPage::OnButtonClear(wxCommandEvent
& WXUNUSED(event
))
496 void BitmapComboBoxWidgetsPage::OnButtonInsert(wxCommandEvent
& WXUNUSED(event
))
498 static unsigned int s_item
= 0;
500 wxString s
= m_textInsert
->GetValue();
501 if ( !m_textInsert
->IsModified() )
503 // update the default string
504 m_textInsert
->SetValue(wxString::Format(_T("test item %u"), ++s_item
));
507 int sel
= m_combobox
->GetSelection();
508 if ( sel
== wxNOT_FOUND
)
509 sel
= m_combobox
->GetCount();
511 m_combobox
->Insert(s
, wxNullBitmap
, m_combobox
->GetSelection());
514 void BitmapComboBoxWidgetsPage::OnTextChangeHeight(wxCommandEvent
& WXUNUSED(event
))
517 m_textChangeHeight
->GetValue().ToLong(&h
);
520 m_combobox
->SetSize(wxDefaultCoord
, h
);
523 void BitmapComboBoxWidgetsPage::OnButtonLoadFromFile(wxCommandEvent
& WXUNUSED(event
))
526 int sel
= m_combobox
->GetSelection();
527 if ( sel
== wxNOT_FOUND
)
528 sel
= m_combobox
->GetCount();
530 m_combobox
->Insert(s
, QueryBitmap(&s
), sel
);
533 void BitmapComboBoxWidgetsPage::OnButtonSetFromFile(wxCommandEvent
& WXUNUSED(event
))
535 m_combobox
->SetItemBitmap(m_combobox
->GetSelection(), QueryBitmap(NULL
));
538 void BitmapComboBoxWidgetsPage::OnButtonAddMany(wxCommandEvent
& WXUNUSED(event
))
540 // "many" means 1000 here
541 for ( unsigned int n
= 0; n
< 1000; n
++ )
543 m_combobox
->Append(wxString::Format(_T("item #%u"), n
));
547 void BitmapComboBoxWidgetsPage::OnButtonAddSeveral(wxCommandEvent
& WXUNUSED(event
))
549 m_combobox
->Append(_T("First"));
550 m_combobox
->Append(_T("another one"));
551 m_combobox
->Append(_T("and the last (very very very very very very very very very very long) one"));
554 void BitmapComboBoxWidgetsPage::OnButtonAddSeveralWithImages(wxCommandEvent
& WXUNUSED(event
))
558 for ( i
=0; i
<4; i
++ )
561 wxBitmap bmp
= CreateRandomBitmap(&s
);
562 m_combobox
->Append(s
, bmp
);
567 void BitmapComboBoxWidgetsPage::RescaleImage(wxImage
& image
, int w
, int h
)
569 if ( image
.GetWidth() == w
&& image
.GetHeight() == h
)
572 if ( w
<= 0 || h
<= 0 )
575 static bool isFirstScale
= true;
577 if ( isFirstScale
&& m_combobox
->GetCount() > 0 )
579 wxMessageBox( wxT("wxBitmapComboBox normally only supports images of one size. ")
580 wxT("However, for demonstration purposes, loaded bitmaps are scaled to fit ")
581 wxT("using wxImage::Rescale."),
586 isFirstScale
= false;
593 void BitmapComboBoxWidgetsPage::LoadWidgetImages( wxArrayString
* strings
, wxImageList
* images
)
597 fn
.AppendDir(wxT("icons"));
599 wxSetCursor(*wxHOURGLASS_CURSOR
);
601 if ( !wxDir::Exists(fn
.GetFullPath()) ||
602 !wxDir::GetAllFiles(fn
.GetFullPath(),strings
,wxT("*.xpm")) )
606 fn
.AppendDir(wxT("icons"));
607 if ( !wxDir::Exists(fn
.GetFullPath()) ||
608 !wxDir::GetAllFiles(fn
.GetFullPath(),strings
,wxT("*.xpm")) )
610 // Try ../../../samples/widgets/icons
615 fn
.AppendDir(wxT("samples"));
616 fn
.AppendDir(wxT("widgets"));
617 fn
.AppendDir(wxT("icons"));
618 if ( !wxDir::Exists(fn
.GetFullPath()) ||
619 !wxDir::GetAllFiles(fn
.GetFullPath(),strings
,wxT("*.xpm")) )
621 wxLogWarning(wxT("Could not load widget icons."));
622 wxSetCursor(*wxSTANDARD_CURSOR
);
630 // Get size of existing images in list
631 wxSize foundSize
= m_combobox
->GetBitmapSize();
633 for ( i
=0; i
<strings
->size(); i
++ )
635 fn
.SetFullName((*strings
)[i
]);
636 wxString name
=fn
.GetName();
638 // Handle few exceptions
639 if ( name
== wxT("bmpbtn") )
641 strings
->RemoveAt(i
);
647 wxASSERT(fn
.FileExists());
648 wxImage
image(fn
.GetFullPath());
649 wxASSERT(image
.Ok());
650 RescaleImage(image
, foundSize
.x
, foundSize
.y
);
652 wxASSERT( bmp
.Ok() );
654 wxBitmap
bmp(wxNullBitmap
);
657 (*strings
)[i
] = name
;
661 wxSetCursor(*wxSTANDARD_CURSOR
);
664 void BitmapComboBoxWidgetsPage::OnButtonAddWidgetIcons(wxCommandEvent
& WXUNUSED(event
))
666 wxArrayString strings
;
668 wxSize sz
= m_combobox
->GetBitmapSize();
675 wxImageList
images(sz
.x
, sz
.y
);
677 LoadWidgetImages(&strings
, &images
);
681 for ( i
=0; i
<strings
.size(); i
++ )
683 m_combobox
->Append(strings
[i
], images
.GetBitmap(i
));
687 void BitmapComboBoxWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
690 event
.Enable( m_chkSort
->GetValue() || m_chkReadonly
->GetValue() );
693 void BitmapComboBoxWidgetsPage::OnUpdateUIInsert(wxUpdateUIEvent
& event
)
697 bool enable
= !(m_combobox
->GetWindowStyle() & wxCB_SORT
);
699 event
.Enable(enable
);
703 void BitmapComboBoxWidgetsPage::OnUpdateUIDeleteButton(wxUpdateUIEvent
& event
)
708 event
.Enable(m_textDelete
->GetValue().ToULong(&n
) &&
709 (n
< (unsigned)m_combobox
->GetCount()));
713 void BitmapComboBoxWidgetsPage::OnUpdateUIItemManipulator(wxUpdateUIEvent
& event
)
716 event
.Enable(m_combobox
->GetSelection() != wxNOT_FOUND
);
719 void BitmapComboBoxWidgetsPage::OnUpdateUIClearButton(wxUpdateUIEvent
& event
)
722 event
.Enable(m_combobox
->GetCount() != 0);
725 void BitmapComboBoxWidgetsPage::OnUpdateUIAddSeveral(wxUpdateUIEvent
& event
)
728 event
.Enable(!(m_combobox
->GetWindowStyle() & wxCB_SORT
));
731 void BitmapComboBoxWidgetsPage::OnUpdateUIAddSeveralWithImages(wxUpdateUIEvent
& event
)
734 event
.Enable(!(m_combobox
->GetWindowStyle() & wxCB_SORT
));
737 void BitmapComboBoxWidgetsPage::OnComboText(wxCommandEvent
& event
)
742 wxString s
= event
.GetString();
744 wxASSERT_MSG( s
== m_combobox
->GetValue(),
745 _T("event and combobox values should be the same") );
747 if (event
.GetEventType() == wxEVT_COMMAND_TEXT_ENTER
)
748 wxLogMessage(_T("BitmapCombobox enter pressed (now '%s')"), s
.c_str());
750 wxLogMessage(_T("BitmapCombobox text changed (now '%s')"), s
.c_str());
753 void BitmapComboBoxWidgetsPage::OnComboBox(wxCommandEvent
& event
)
755 long sel
= event
.GetInt();
756 m_textDelete
->SetValue(wxString::Format(_T("%ld"), sel
));
758 wxLogMessage(_T("BitmapCombobox item %ld selected"), sel
);
760 wxLogMessage(_T("BitmapCombobox GetValue(): %s"), m_combobox
->GetValue().c_str() );
763 void BitmapComboBoxWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
769 wxBitmap
BitmapComboBoxWidgetsPage::LoadBitmap(const wxString
& filepath
)
771 // Get size of existing images in list
772 wxSize foundSize
= m_combobox
->GetBitmapSize();
774 // Have some reasonable maximum size
775 if ( foundSize
.x
<= 0 )
777 foundSize
.x
= IMG_SIZE_TRUNC
;
778 foundSize
.y
= IMG_SIZE_TRUNC
;
781 wxImage
image(filepath
);
784 // Rescale very large images
785 int ow
= image
.GetWidth();
786 int oh
= image
.GetHeight();
788 if ( foundSize
.x
> 0 &&
789 (ow
!= foundSize
.x
|| oh
!= foundSize
.y
) )
792 if ( w
> foundSize
.x
)
795 if ( h
> foundSize
.y
)
798 RescaleImage(image
, w
, h
);
801 return wxBitmap(image
);
807 wxBitmap
BitmapComboBoxWidgetsPage::LoadBitmap(const wxString
& WXUNUSED(filepath
))
813 wxBitmap
BitmapComboBoxWidgetsPage::QueryBitmap(wxString
* pStr
)
815 wxString filepath
= wxFileSelector(wxT("Choose image file"),
820 wxFD_OPEN
| wxFD_FILE_MUST_EXIST
,
825 ::wxSetCursor( *wxHOURGLASS_CURSOR
);
827 if ( filepath
.length() )
831 *pStr
= wxFileName(filepath
).GetName();
834 bitmap
= LoadBitmap(filepath
);
837 wxLogDebug(wxT("%i, %i"),bitmap
.GetWidth(), bitmap
.GetHeight());
839 ::wxSetCursor( *wxSTANDARD_CURSOR
);
844 wxBitmap
BitmapComboBoxWidgetsPage::CreateBitmap(const wxColour
& colour
)
846 int ch
= m_combobox
->GetBitmapSize().y
;
861 dc
.SelectObject(bmp
);
863 // Draw transparent background
864 wxColour
magic(255,0,255);
865 wxBrush
magicBrush(magic
);
866 dc
.SetBrush(magicBrush
);
867 dc
.SetPen(*wxTRANSPARENT_PEN
);
868 dc
.DrawRectangle(0,0,bmp
.GetWidth(),bmp
.GetHeight());
870 // Draw image content
871 dc
.SetBrush(wxBrush(colour
));
872 dc
.DrawCircle(h
/2,h
/2+1,(h
/2));
874 dc
.SelectObject(wxNullBitmap
);
876 // Finalize transparency with a mask
877 wxMask
*mask
= new wxMask(bmp
, magic
);
883 wxBitmap
BitmapComboBoxWidgetsPage::CreateRandomBitmap( wxString
* pStr
)
886 const wxChar
* str
= wxT("");
891 str
= wxT("Red Circle");
892 bmp
= CreateBitmap( *wxRED
);
896 str
= wxT("Green Circle");
897 bmp
= CreateBitmap( *wxGREEN
);
901 str
= wxT("Blue Circle");
902 bmp
= CreateBitmap( *wxBLUE
);
906 str
= wxT("Black Circle");
907 bmp
= CreateBitmap( *wxBLACK
);
911 str
= wxT("Cyan Circle");
912 bmp
= CreateBitmap( *wxCYAN
);
916 str
= wxT("Light Grey Circle");
917 bmp
= CreateBitmap( *wxLIGHT_GREY
);
926 #endif //wxUSE_BITMAPCOMBOBOX