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
LoadBitmap(const wxString
& filepath
);
149 wxBitmap
QueryBitmap(wxString
* pStr
);
151 void LoadWidgetImages( wxArrayString
* strings
, wxImageList
* images
);
153 wxSizer
*CreateSizerWithSmallTextAndLabel(const wxString
& label
,
155 wxTextCtrl
**ppText
);
158 void RescaleImage(wxImage
& image
, int w
, int h
);
164 // the checkboxes for styles
165 wxCheckBox
*m_chkSort
,
168 // the combobox itself and the sizer it is in
169 wxBitmapComboBox
*m_combobox
;
170 wxSizer
*m_sizerCombo
;
172 // the text entries for "Add/change string" and "Delete" buttons
173 wxTextCtrl
*m_textInsert
,
179 DECLARE_EVENT_TABLE()
180 DECLARE_WIDGETS_PAGE(BitmapComboBoxWidgetsPage
)
183 // ----------------------------------------------------------------------------
185 // ----------------------------------------------------------------------------
187 BEGIN_EVENT_TABLE(BitmapComboBoxWidgetsPage
, WidgetsPage
)
188 EVT_BUTTON(BitmapComboBoxPage_Reset
, BitmapComboBoxWidgetsPage::OnButtonReset
)
189 EVT_BUTTON(BitmapComboBoxPage_Change
, BitmapComboBoxWidgetsPage::OnButtonChange
)
190 EVT_BUTTON(BitmapComboBoxPage_Delete
, BitmapComboBoxWidgetsPage::OnButtonDelete
)
191 EVT_BUTTON(BitmapComboBoxPage_DeleteSel
, BitmapComboBoxWidgetsPage::OnButtonDeleteSel
)
192 EVT_BUTTON(BitmapComboBoxPage_Clear
, BitmapComboBoxWidgetsPage::OnButtonClear
)
193 EVT_BUTTON(BitmapComboBoxPage_Insert
, BitmapComboBoxWidgetsPage::OnButtonInsert
)
194 EVT_BUTTON(BitmapComboBoxPage_AddSeveral
, BitmapComboBoxWidgetsPage::OnButtonAddSeveral
)
195 EVT_BUTTON(BitmapComboBoxPage_AddSeveralWithImages
, BitmapComboBoxWidgetsPage::OnButtonAddSeveralWithImages
)
196 EVT_BUTTON(BitmapComboBoxPage_AddWidgetIcons
, BitmapComboBoxWidgetsPage::OnButtonAddWidgetIcons
)
197 EVT_BUTTON(BitmapComboBoxPage_AddMany
, BitmapComboBoxWidgetsPage::OnButtonAddMany
)
198 EVT_BUTTON(BitmapComboBoxPage_LoadFromFile
, BitmapComboBoxWidgetsPage::OnButtonLoadFromFile
)
199 EVT_BUTTON(BitmapComboBoxPage_SetFromFile
, BitmapComboBoxWidgetsPage::OnButtonSetFromFile
)
200 EVT_BUTTON(BitmapComboBoxPage_ContainerTests
, ItemContainerWidgetsPage::OnButtonTestItemContainer
)
202 EVT_TEXT_ENTER(BitmapComboBoxPage_InsertText
, BitmapComboBoxWidgetsPage::OnButtonInsert
)
203 EVT_TEXT(BitmapComboBoxPage_ChangeHeight
, BitmapComboBoxWidgetsPage::OnTextChangeHeight
)
204 EVT_TEXT_ENTER(BitmapComboBoxPage_DeleteText
, BitmapComboBoxWidgetsPage::OnButtonDelete
)
206 EVT_UPDATE_UI(BitmapComboBoxPage_Reset
, BitmapComboBoxWidgetsPage::OnUpdateUIResetButton
)
207 EVT_UPDATE_UI(BitmapComboBoxPage_Insert
, BitmapComboBoxWidgetsPage::OnUpdateUIInsert
)
208 EVT_UPDATE_UI(BitmapComboBoxPage_LoadFromFile
, BitmapComboBoxWidgetsPage::OnUpdateUIInsert
)
209 EVT_UPDATE_UI(BitmapComboBoxPage_AddSeveral
, BitmapComboBoxWidgetsPage::OnUpdateUIAddSeveral
)
210 EVT_UPDATE_UI(BitmapComboBoxPage_AddSeveralWithImages
, BitmapComboBoxWidgetsPage::OnUpdateUIAddSeveralWithImages
)
211 EVT_UPDATE_UI(BitmapComboBoxPage_Clear
, BitmapComboBoxWidgetsPage::OnUpdateUIClearButton
)
212 EVT_UPDATE_UI(BitmapComboBoxPage_DeleteText
, BitmapComboBoxWidgetsPage::OnUpdateUIClearButton
)
213 EVT_UPDATE_UI(BitmapComboBoxPage_Delete
, BitmapComboBoxWidgetsPage::OnUpdateUIDeleteButton
)
214 EVT_UPDATE_UI(BitmapComboBoxPage_Change
, BitmapComboBoxWidgetsPage::OnUpdateUIItemManipulator
)
215 EVT_UPDATE_UI(BitmapComboBoxPage_SetFromFile
, BitmapComboBoxWidgetsPage::OnUpdateUIItemManipulator
)
216 EVT_UPDATE_UI(BitmapComboBoxPage_DeleteSel
, BitmapComboBoxWidgetsPage::OnUpdateUIItemManipulator
)
218 EVT_COMBOBOX(BitmapComboBoxPage_Combo
, BitmapComboBoxWidgetsPage::OnComboBox
)
219 EVT_TEXT(BitmapComboBoxPage_Combo
, BitmapComboBoxWidgetsPage::OnComboText
)
220 EVT_TEXT_ENTER(BitmapComboBoxPage_Combo
, BitmapComboBoxWidgetsPage::OnComboText
)
222 EVT_CHECKBOX(wxID_ANY
, BitmapComboBoxWidgetsPage::OnCheckOrRadioBox
)
223 EVT_RADIOBOX(wxID_ANY
, BitmapComboBoxWidgetsPage::OnCheckOrRadioBox
)
226 // ============================================================================
228 // ============================================================================
232 IMPLEMENT_WIDGETS_PAGE(BitmapComboBoxWidgetsPage
, _T("BitmapCombobox"),
233 GENERIC_CTRLS
| WITH_ITEMS_CTRLS
| COMBO_CTRLS
237 BitmapComboBoxWidgetsPage::BitmapComboBoxWidgetsPage(WidgetsBookCtrl
*book
,
238 wxImageList
*imaglist
)
239 : ItemContainerWidgetsPage(book
, imaglist
, bmpcombobox_xpm
)
243 m_chkReadonly
= NULL
;
254 // create a sizer containing a label and a small text ctrl
255 wxSizer
*BitmapComboBoxWidgetsPage::CreateSizerWithSmallTextAndLabel(const wxString
& label
,
259 wxControl
* control
= new wxStaticText(this, wxID_ANY
, label
);
260 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
261 wxTextCtrl
*text
= new wxTextCtrl(this, id
, wxEmptyString
,
262 wxDefaultPosition
, wxSize(50,wxDefaultCoord
), wxTE_PROCESS_ENTER
);
264 sizerRow
->Add(control
, 0, wxRIGHT
| wxALIGN_CENTRE_VERTICAL
, 5);
265 sizerRow
->Add(text
, 1, wxFIXED_MINSIZE
| wxLEFT
| wxALIGN_CENTRE_VERTICAL
, 5);
273 void BitmapComboBoxWidgetsPage::CreateContent()
276 What we create here is a frame having 3 panes: style pane is the
277 leftmost one, in the middle the pane with buttons allowing to perform
278 miscellaneous combobox operations and the pane containing the combobox
283 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
285 wxSizer
*sizerLeft
= new wxBoxSizer(wxVERTICAL
);
287 // left pane - style box
288 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
290 wxSizer
*sizerStyle
= new wxStaticBoxSizer(box
, wxVERTICAL
);
292 m_chkSort
= CreateCheckBoxAndAddToSizer(sizerStyle
, _T("&Sort items"));
293 m_chkReadonly
= CreateCheckBoxAndAddToSizer(sizerStyle
, _T("&Read only"));
295 wxButton
*btn
= new wxButton(this, BitmapComboBoxPage_Reset
, _T("&Reset"));
296 sizerStyle
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 3);
298 sizerLeft
->Add(sizerStyle
, 0, wxGROW
| wxALIGN_CENTRE_HORIZONTAL
);
300 // left pane - other options box
301 box
= new wxStaticBox(this, wxID_ANY
, _T("Demo options"));
303 wxSizer
*sizerOptions
= new wxStaticBoxSizer(box
, wxVERTICAL
);
305 sizerRow
= CreateSizerWithSmallTextAndLabel(_T("Control &height:"),
306 BitmapComboBoxPage_ChangeHeight
,
307 &m_textChangeHeight
);
308 m_textChangeHeight
->SetSize(20, wxDefaultCoord
);
309 sizerOptions
->Add(sizerRow
, 0, wxALL
| wxFIXED_MINSIZE
/*| wxGROW*/, 5);
311 sizerLeft
->Add(sizerOptions
, 0, wxGROW
| wxALIGN_CENTRE_HORIZONTAL
| wxTOP
, 2);
314 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
,
315 _T("&Change wxBitmapComboBox contents"));
316 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
318 btn
= new wxButton(this, BitmapComboBoxPage_ContainerTests
, _T("Run &tests"));
319 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
322 btn
= new wxButton(this, BitmapComboBoxPage_AddWidgetIcons
, _T("Add &widget icons"));
323 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
325 btn
= new wxButton(this, BitmapComboBoxPage_LoadFromFile
, _T("Insert image from &file"));
326 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
328 btn
= new wxButton(this, BitmapComboBoxPage_SetFromFile
, _T("&Set image from file"));
329 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
332 btn
= new wxButton(this, BitmapComboBoxPage_AddSeveralWithImages
, _T("A&ppend a few strings with images"));
333 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
335 btn
= new wxButton(this, BitmapComboBoxPage_AddSeveral
, _T("Append a &few strings"));
336 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
338 btn
= new wxButton(this, BitmapComboBoxPage_AddMany
, _T("Append &many strings"));
339 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
341 sizerRow
= CreateSizerWithTextAndButton(BitmapComboBoxPage_Delete
,
342 _T("&Delete this item"),
343 BitmapComboBoxPage_DeleteText
,
345 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
347 btn
= new wxButton(this, BitmapComboBoxPage_DeleteSel
, _T("Delete &selection"));
348 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
350 btn
= new wxButton(this, BitmapComboBoxPage_Clear
, _T("&Clear"));
351 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
354 wxInitAllImageHandlers();
358 wxSizer
*sizerRight
= new wxBoxSizer(wxVERTICAL
);
359 m_combobox
= new wxBitmapComboBox();
360 m_combobox
->Create(this, BitmapComboBoxPage_Combo
, wxEmptyString
,
361 wxDefaultPosition
, wxDefaultSize
,
365 #if defined(wxGENERIC_BITMAPCOMBOBOX)
366 // This will sure make the list look nicer when larger images are used.
367 m_combobox
->SetPopupMaxHeight(600);
370 sizerRight
->Add(m_combobox
, 0, wxGROW
| wxALL
, 5);
371 sizerRight
->SetMinSize(150, 0);
372 m_sizerCombo
= sizerRight
; // save it to modify it later
374 // the 3 panes panes compose the window
375 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
376 sizerTop
->Add(sizerMiddle
, 5, wxGROW
| wxALL
, 10);
377 sizerTop
->Add(sizerRight
, 4, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
379 // final initializations
385 // ----------------------------------------------------------------------------
387 // ----------------------------------------------------------------------------
389 void BitmapComboBoxWidgetsPage::Reset()
391 m_chkSort
->SetValue(false);
392 m_chkReadonly
->SetValue(true);
395 void BitmapComboBoxWidgetsPage::CreateCombo()
397 int flags
= ms_defaultFlags
;
399 if ( m_chkSort
->GetValue() )
401 if ( m_chkReadonly
->GetValue() )
402 flags
|= wxCB_READONLY
;
405 wxArrayPtrVoid bitmaps
;
408 unsigned int count
= m_combobox
->GetCount();
409 for ( unsigned int n
= 0; n
< count
; n
++ )
411 items
.Add(m_combobox
->GetString(n
));
412 bitmaps
.Add(new wxBitmap(m_combobox
->GetItemBitmap(n
)));
415 m_sizerCombo
->Detach( m_combobox
);
419 m_combobox
= new wxBitmapComboBox();
420 m_combobox
->Create(this, BitmapComboBoxPage_Combo
, wxEmptyString
,
421 wxDefaultPosition
, wxDefaultSize
,
425 #if defined(wxGENERIC_BITMAPCOMBOBOX)
426 // This will sure make the list look nicer when larger images are used.
427 m_combobox
->SetPopupMaxHeight(600);
430 unsigned int count
= items
.GetCount();
431 for ( unsigned int n
= 0; n
< count
; n
++ )
433 wxBitmap
* bmp
= (wxBitmap
*) bitmaps
[n
];
434 m_combobox
->Append(items
[n
], *bmp
);
438 m_sizerCombo
->Add(m_combobox
, 0, wxGROW
| wxALL
, 5);
439 m_sizerCombo
->Layout();
441 // Allow changing height inorder to demonstrate flexible
442 // size of image "thumbnail" painted in the control itself.
444 m_textChangeHeight
->GetValue().ToLong(&h
);
446 m_combobox
->SetSize(wxDefaultCoord
, h
);
449 // ----------------------------------------------------------------------------
451 // ----------------------------------------------------------------------------
453 void BitmapComboBoxWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
460 void BitmapComboBoxWidgetsPage::OnButtonChange(wxCommandEvent
& WXUNUSED(event
))
462 int sel
= m_combobox
->GetSelection();
463 if ( sel
!= wxNOT_FOUND
)
466 m_combobox
->SetString(sel
, m_textChange
->GetValue());
468 wxLogMessage(_T("Not implemented in wxGTK"));
473 void BitmapComboBoxWidgetsPage::OnButtonDelete(wxCommandEvent
& WXUNUSED(event
))
476 if ( !m_textDelete
->GetValue().ToULong(&n
) ||
477 (n
>= m_combobox
->GetCount()) )
482 m_combobox
->Delete(n
);
485 void BitmapComboBoxWidgetsPage::OnButtonDeleteSel(wxCommandEvent
& WXUNUSED(event
))
487 int sel
= m_combobox
->GetSelection();
488 if ( sel
!= wxNOT_FOUND
)
490 m_combobox
->Delete(sel
);
494 void BitmapComboBoxWidgetsPage::OnButtonClear(wxCommandEvent
& WXUNUSED(event
))
499 void BitmapComboBoxWidgetsPage::OnButtonInsert(wxCommandEvent
& WXUNUSED(event
))
501 static unsigned int s_item
= 0;
503 wxString s
= m_textInsert
->GetValue();
504 if ( !m_textInsert
->IsModified() )
506 // update the default string
507 m_textInsert
->SetValue(wxString::Format(_T("test item %u"), ++s_item
));
510 int sel
= m_combobox
->GetSelection();
511 if ( sel
== wxNOT_FOUND
)
512 sel
= m_combobox
->GetCount();
514 m_combobox
->Insert(s
, wxNullBitmap
, m_combobox
->GetSelection());
517 void BitmapComboBoxWidgetsPage::OnTextChangeHeight(wxCommandEvent
& WXUNUSED(event
))
520 if ( m_textChangeHeight
)
521 m_textChangeHeight
->GetValue().ToLong(&h
);
524 m_combobox
->SetSize(wxDefaultCoord
, h
);
527 void BitmapComboBoxWidgetsPage::OnButtonLoadFromFile(wxCommandEvent
& WXUNUSED(event
))
530 int sel
= m_combobox
->GetSelection();
531 if ( sel
== wxNOT_FOUND
)
532 sel
= m_combobox
->GetCount();
534 m_combobox
->Insert(s
, QueryBitmap(&s
), sel
);
537 void BitmapComboBoxWidgetsPage::OnButtonSetFromFile(wxCommandEvent
& WXUNUSED(event
))
539 m_combobox
->SetItemBitmap(m_combobox
->GetSelection(), QueryBitmap(NULL
));
542 void BitmapComboBoxWidgetsPage::OnButtonAddMany(wxCommandEvent
& WXUNUSED(event
))
544 // "many" means 1000 here
545 for ( unsigned int n
= 0; n
< 1000; n
++ )
547 m_combobox
->Append(wxString::Format(_T("item #%u"), n
));
551 void BitmapComboBoxWidgetsPage::OnButtonAddSeveral(wxCommandEvent
& WXUNUSED(event
))
553 m_combobox
->Append(_T("First"));
554 m_combobox
->Append(_T("another one"));
555 m_combobox
->Append(_T("and the last (very very very very very very very very very very long) one"));
558 void BitmapComboBoxWidgetsPage::OnButtonAddSeveralWithImages(wxCommandEvent
& WXUNUSED(event
))
560 static const struct TestEntry
566 { "Red circle", 0x0000ff },
567 { "Blue circle", 0xff0000 },
568 { "Green circle", 0x00ff00 },
569 { "Black circle", 0x000000 },
572 for ( unsigned i
= 0; i
< WXSIZEOF(s_entries
); i
++ )
574 const TestEntry
& e
= s_entries
[i
];
575 m_combobox
->Append(e
.text
, CreateBitmap(wxColour(e
.rgb
)));
580 void BitmapComboBoxWidgetsPage::RescaleImage(wxImage
& image
, int w
, int h
)
582 if ( image
.GetWidth() == w
&& image
.GetHeight() == h
)
585 if ( w
<= 0 || h
<= 0 )
588 static bool isFirstScale
= true;
590 if ( isFirstScale
&& m_combobox
->GetCount() > 0 )
592 wxMessageBox( wxT("wxBitmapComboBox normally only supports images of one size. ")
593 wxT("However, for demonstration purposes, loaded bitmaps are scaled to fit ")
594 wxT("using wxImage::Rescale."),
599 isFirstScale
= false;
606 void BitmapComboBoxWidgetsPage::LoadWidgetImages( wxArrayString
* strings
, wxImageList
* images
)
610 fn
.AppendDir(wxT("icons"));
612 wxSetCursor(*wxHOURGLASS_CURSOR
);
614 if ( !wxDir::Exists(fn
.GetFullPath()) ||
615 !wxDir::GetAllFiles(fn
.GetFullPath(),strings
,wxT("*.xpm")) )
619 fn
.AppendDir(wxT("icons"));
620 if ( !wxDir::Exists(fn
.GetFullPath()) ||
621 !wxDir::GetAllFiles(fn
.GetFullPath(),strings
,wxT("*.xpm")) )
623 // Try ../../../samples/widgets/icons
628 fn
.AppendDir(wxT("samples"));
629 fn
.AppendDir(wxT("widgets"));
630 fn
.AppendDir(wxT("icons"));
631 if ( !wxDir::Exists(fn
.GetFullPath()) ||
632 !wxDir::GetAllFiles(fn
.GetFullPath(),strings
,wxT("*.xpm")) )
634 wxLogWarning(wxT("Could not load widget icons."));
635 wxSetCursor(*wxSTANDARD_CURSOR
);
643 // Get size of existing images in list
644 wxSize foundSize
= m_combobox
->GetBitmapSize();
646 for ( i
=0; i
<strings
->size(); i
++ )
648 fn
.SetFullName((*strings
)[i
]);
649 wxString name
=fn
.GetName();
651 // Handle few exceptions
652 if ( name
== wxT("bmpbtn") )
654 strings
->RemoveAt(i
);
660 wxASSERT(fn
.FileExists());
661 wxImage
image(fn
.GetFullPath());
662 wxASSERT(image
.Ok());
663 RescaleImage(image
, foundSize
.x
, foundSize
.y
);
665 wxASSERT( bmp
.Ok() );
667 wxBitmap
bmp(wxNullBitmap
);
670 (*strings
)[i
] = name
;
674 wxSetCursor(*wxSTANDARD_CURSOR
);
677 void BitmapComboBoxWidgetsPage::OnButtonAddWidgetIcons(wxCommandEvent
& WXUNUSED(event
))
679 wxArrayString strings
;
681 wxSize sz
= m_combobox
->GetBitmapSize();
688 wxImageList
images(sz
.x
, sz
.y
);
690 LoadWidgetImages(&strings
, &images
);
694 for ( i
=0; i
<strings
.size(); i
++ )
696 m_combobox
->Append(strings
[i
], images
.GetBitmap(i
));
700 void BitmapComboBoxWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
703 event
.Enable( m_chkSort
->GetValue() || m_chkReadonly
->GetValue() );
706 void BitmapComboBoxWidgetsPage::OnUpdateUIInsert(wxUpdateUIEvent
& event
)
710 bool enable
= !(m_combobox
->GetWindowStyle() & wxCB_SORT
);
712 event
.Enable(enable
);
716 void BitmapComboBoxWidgetsPage::OnUpdateUIDeleteButton(wxUpdateUIEvent
& event
)
721 event
.Enable(m_textDelete
->GetValue().ToULong(&n
) &&
722 (n
< (unsigned)m_combobox
->GetCount()));
726 void BitmapComboBoxWidgetsPage::OnUpdateUIItemManipulator(wxUpdateUIEvent
& event
)
729 event
.Enable(m_combobox
->GetSelection() != wxNOT_FOUND
);
732 void BitmapComboBoxWidgetsPage::OnUpdateUIClearButton(wxUpdateUIEvent
& event
)
735 event
.Enable(m_combobox
->GetCount() != 0);
738 void BitmapComboBoxWidgetsPage::OnUpdateUIAddSeveral(wxUpdateUIEvent
& event
)
741 event
.Enable(!(m_combobox
->GetWindowStyle() & wxCB_SORT
));
744 void BitmapComboBoxWidgetsPage::OnUpdateUIAddSeveralWithImages(wxUpdateUIEvent
& event
)
747 event
.Enable(!(m_combobox
->GetWindowStyle() & wxCB_SORT
));
750 void BitmapComboBoxWidgetsPage::OnComboText(wxCommandEvent
& event
)
755 wxString s
= event
.GetString();
757 wxASSERT_MSG( s
== m_combobox
->GetValue(),
758 _T("event and combobox values should be the same") );
760 if (event
.GetEventType() == wxEVT_COMMAND_TEXT_ENTER
)
761 wxLogMessage(_T("BitmapCombobox enter pressed (now '%s')"), s
.c_str());
763 wxLogMessage(_T("BitmapCombobox text changed (now '%s')"), s
.c_str());
766 void BitmapComboBoxWidgetsPage::OnComboBox(wxCommandEvent
& event
)
768 long sel
= event
.GetInt();
769 m_textDelete
->SetValue(wxString::Format(_T("%ld"), sel
));
771 wxLogMessage(_T("BitmapCombobox item %ld selected"), sel
);
773 wxLogMessage(_T("BitmapCombobox GetValue(): %s"), m_combobox
->GetValue().c_str() );
776 void BitmapComboBoxWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
782 wxBitmap
BitmapComboBoxWidgetsPage::LoadBitmap(const wxString
& filepath
)
784 // Get size of existing images in list
785 wxSize foundSize
= m_combobox
->GetBitmapSize();
787 // Have some reasonable maximum size
788 if ( foundSize
.x
<= 0 )
790 foundSize
.x
= IMG_SIZE_TRUNC
;
791 foundSize
.y
= IMG_SIZE_TRUNC
;
794 wxImage
image(filepath
);
797 // Rescale very large images
798 int ow
= image
.GetWidth();
799 int oh
= image
.GetHeight();
801 if ( foundSize
.x
> 0 &&
802 (ow
!= foundSize
.x
|| oh
!= foundSize
.y
) )
805 if ( w
> foundSize
.x
)
808 if ( h
> foundSize
.y
)
811 RescaleImage(image
, w
, h
);
814 return wxBitmap(image
);
820 wxBitmap
BitmapComboBoxWidgetsPage::LoadBitmap(const wxString
& WXUNUSED(filepath
))
826 wxBitmap
BitmapComboBoxWidgetsPage::QueryBitmap(wxString
* pStr
)
828 wxString filepath
= wxFileSelector(wxT("Choose image file"),
833 wxFD_OPEN
| wxFD_FILE_MUST_EXIST
,
838 ::wxSetCursor( *wxHOURGLASS_CURSOR
);
840 if ( filepath
.length() )
844 *pStr
= wxFileName(filepath
).GetName();
847 bitmap
= LoadBitmap(filepath
);
850 wxLogDebug(wxT("%i, %i"),bitmap
.GetWidth(), bitmap
.GetHeight());
852 ::wxSetCursor( *wxSTANDARD_CURSOR
);
857 wxBitmap
BitmapComboBoxWidgetsPage::CreateBitmap(const wxColour
& colour
)
864 dc
.SelectObject(bmp
);
866 // Draw transparent background
867 wxColour
magic(255, 0, 255);
868 wxBrush
magicBrush(magic
);
869 dc
.SetBrush(magicBrush
);
870 dc
.SetPen(*wxTRANSPARENT_PEN
);
871 dc
.DrawRectangle(0, 0, w
, h
);
873 // Draw image content
874 dc
.SetBrush(wxBrush(colour
));
875 dc
.DrawCircle(h
/2, h
/2+1, h
/2);
877 dc
.SelectObject(wxNullBitmap
);
879 // Finalize transparency with a mask
880 wxMask
*mask
= new wxMask(bmp
, magic
);
886 #endif // wxUSE_BITMAPCOMBOBOX