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
= (wxCheckBox
*)NULL
;
245 m_combobox
= (wxBitmapComboBox
*)NULL
;
246 m_sizerCombo
= (wxSizer
*)NULL
;
249 // create a sizer containing a label and a small text ctrl
250 wxSizer
*BitmapComboBoxWidgetsPage::CreateSizerWithSmallTextAndLabel(const wxString
& label
,
254 wxControl
* control
= new wxStaticText(this, wxID_ANY
, label
);
255 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
256 wxTextCtrl
*text
= new wxTextCtrl(this, id
, wxEmptyString
,
257 wxDefaultPosition
, wxSize(50,wxDefaultCoord
), wxTE_PROCESS_ENTER
);
259 sizerRow
->Add(control
, 0, wxRIGHT
| wxALIGN_CENTRE_VERTICAL
, 5);
260 sizerRow
->Add(text
, 1, wxFIXED_MINSIZE
| wxLEFT
| wxALIGN_CENTRE_VERTICAL
, 5);
268 void BitmapComboBoxWidgetsPage::CreateContent()
271 What we create here is a frame having 3 panes: style pane is the
272 leftmost one, in the middle the pane with buttons allowing to perform
273 miscellaneous combobox operations and the pane containing the combobox
279 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
281 wxSizer
*sizerLeft
= new wxBoxSizer(wxVERTICAL
);
283 // left pane - style box
284 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
286 wxSizer
*sizerStyle
= new wxStaticBoxSizer(box
, wxVERTICAL
);
288 m_chkSort
= CreateCheckBoxAndAddToSizer(sizerStyle
, _T("&Sort items"));
289 m_chkReadonly
= CreateCheckBoxAndAddToSizer(sizerStyle
, _T("&Read only"));
291 wxButton
*btn
= new wxButton(this, BitmapComboBoxPage_Reset
, _T("&Reset"));
292 sizerStyle
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 3);
294 sizerLeft
->Add(sizerStyle
, 0, wxGROW
| wxALIGN_CENTRE_HORIZONTAL
);
296 // left pane - other options box
297 box
= new wxStaticBox(this, wxID_ANY
, _T("Demo options"));
299 wxSizer
*sizerOptions
= new wxStaticBoxSizer(box
, wxVERTICAL
);
301 sizerRow
= CreateSizerWithSmallTextAndLabel(_T("Control &height:"),
302 BitmapComboBoxPage_ChangeHeight
,
303 &m_textChangeHeight
);
304 m_textChangeHeight
->SetSize(20, wxDefaultCoord
);
305 sizerOptions
->Add(sizerRow
, 0, wxALL
| wxFIXED_MINSIZE
/*| wxGROW*/, 5);
307 sizerLeft
->Add(sizerOptions
, 0, wxGROW
| wxALIGN_CENTRE_HORIZONTAL
| wxTOP
, 2);
310 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
,
311 _T("&Change wxBitmapComboBox contents"));
312 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
314 btn
= new wxButton(this, BitmapComboBoxPage_ContainerTests
, _T("Run &tests"));
315 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
318 btn
= new wxButton(this, BitmapComboBoxPage_AddWidgetIcons
, _T("Add &widget icons"));
319 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
321 btn
= new wxButton(this, BitmapComboBoxPage_LoadFromFile
, _T("Insert image from &file"));
322 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
324 btn
= new wxButton(this, BitmapComboBoxPage_SetFromFile
, _T("&Set image from file"));
325 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
328 btn
= new wxButton(this, BitmapComboBoxPage_AddSeveralWithImages
, _T("A&ppend a few strings with images"));
329 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
331 btn
= new wxButton(this, BitmapComboBoxPage_AddSeveral
, _T("Append a &few strings"));
332 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
334 btn
= new wxButton(this, BitmapComboBoxPage_AddMany
, _T("Append &many strings"));
335 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
337 sizerRow
= CreateSizerWithTextAndButton(BitmapComboBoxPage_Delete
,
338 _T("&Delete this item"),
339 BitmapComboBoxPage_DeleteText
,
341 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
343 btn
= new wxButton(this, BitmapComboBoxPage_DeleteSel
, _T("Delete &selection"));
344 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
346 btn
= new wxButton(this, BitmapComboBoxPage_Clear
, _T("&Clear"));
347 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
350 wxInitAllImageHandlers();
354 wxSizer
*sizerRight
= new wxBoxSizer(wxVERTICAL
);
355 m_combobox
= new wxBitmapComboBox();
356 m_combobox
->Create(this, BitmapComboBoxPage_Combo
, wxEmptyString
,
357 wxDefaultPosition
, wxDefaultSize
,
361 #if defined(wxGENERIC_BITMAPCOMBOBOX)
362 // This will sure make the list look nicer when larger images are used.
363 m_combobox
->SetPopupMaxHeight(600);
366 sizerRight
->Add(m_combobox
, 0, wxGROW
| wxALL
, 5);
367 sizerRight
->SetMinSize(150, 0);
368 m_sizerCombo
= sizerRight
; // save it to modify it later
370 // the 3 panes panes compose the window
371 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
372 sizerTop
->Add(sizerMiddle
, 5, wxGROW
| wxALL
, 10);
373 sizerTop
->Add(sizerRight
, 4, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
375 // final initializations
381 // ----------------------------------------------------------------------------
383 // ----------------------------------------------------------------------------
385 void BitmapComboBoxWidgetsPage::Reset()
387 m_chkSort
->SetValue(false);
388 m_chkReadonly
->SetValue(true);
391 void BitmapComboBoxWidgetsPage::CreateCombo()
393 int flags
= ms_defaultFlags
;
395 if ( m_chkSort
->GetValue() )
397 if ( m_chkReadonly
->GetValue() )
398 flags
|= wxCB_READONLY
;
401 wxArrayPtrVoid bitmaps
;
404 unsigned int count
= m_combobox
->GetCount();
405 for ( unsigned int n
= 0; n
< count
; n
++ )
407 items
.Add(m_combobox
->GetString(n
));
408 bitmaps
.Add(new wxBitmap(m_combobox
->GetItemBitmap(n
)));
411 m_sizerCombo
->Detach( m_combobox
);
415 m_combobox
= new wxBitmapComboBox();
416 m_combobox
->Create(this, BitmapComboBoxPage_Combo
, wxEmptyString
,
417 wxDefaultPosition
, wxDefaultSize
,
421 #if defined(wxGENERIC_BITMAPCOMBOBOX)
422 // This will sure make the list look nicer when larger images are used.
423 m_combobox
->SetPopupMaxHeight(600);
426 unsigned int count
= items
.GetCount();
427 for ( unsigned int n
= 0; n
< count
; n
++ )
429 wxBitmap
* bmp
= (wxBitmap
*) bitmaps
[n
];
430 m_combobox
->Append(items
[n
], *bmp
);
434 m_sizerCombo
->Add(m_combobox
, 0, wxGROW
| wxALL
, 5);
435 m_sizerCombo
->Layout();
437 // Allow changing height inorder to demonstrate flexible
438 // size of image "thumbnail" painted in the control itself.
440 m_textChangeHeight
->GetValue().ToLong(&h
);
442 m_combobox
->SetSize(wxDefaultCoord
, h
);
445 // ----------------------------------------------------------------------------
447 // ----------------------------------------------------------------------------
449 void BitmapComboBoxWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
456 void BitmapComboBoxWidgetsPage::OnButtonChange(wxCommandEvent
& WXUNUSED(event
))
458 int sel
= m_combobox
->GetSelection();
459 if ( sel
!= wxNOT_FOUND
)
462 m_combobox
->SetString(sel
, m_textChange
->GetValue());
464 wxLogMessage(_T("Not implemented in wxGTK"));
469 void BitmapComboBoxWidgetsPage::OnButtonDelete(wxCommandEvent
& WXUNUSED(event
))
472 if ( !m_textDelete
->GetValue().ToULong(&n
) ||
473 (n
>= m_combobox
->GetCount()) )
478 m_combobox
->Delete(n
);
481 void BitmapComboBoxWidgetsPage::OnButtonDeleteSel(wxCommandEvent
& WXUNUSED(event
))
483 int sel
= m_combobox
->GetSelection();
484 if ( sel
!= wxNOT_FOUND
)
486 m_combobox
->Delete(sel
);
490 void BitmapComboBoxWidgetsPage::OnButtonClear(wxCommandEvent
& WXUNUSED(event
))
495 void BitmapComboBoxWidgetsPage::OnButtonInsert(wxCommandEvent
& WXUNUSED(event
))
497 static unsigned int s_item
= 0;
499 wxString s
= m_textInsert
->GetValue();
500 if ( !m_textInsert
->IsModified() )
502 // update the default string
503 m_textInsert
->SetValue(wxString::Format(_T("test item %u"), ++s_item
));
506 int sel
= m_combobox
->GetSelection();
507 if ( sel
== wxNOT_FOUND
)
508 sel
= m_combobox
->GetCount();
510 m_combobox
->Insert(s
, wxNullBitmap
, m_combobox
->GetSelection());
513 void BitmapComboBoxWidgetsPage::OnTextChangeHeight(wxCommandEvent
& WXUNUSED(event
))
516 m_textChangeHeight
->GetValue().ToLong(&h
);
519 m_combobox
->SetSize(wxDefaultCoord
, h
);
522 void BitmapComboBoxWidgetsPage::OnButtonLoadFromFile(wxCommandEvent
& WXUNUSED(event
))
525 int sel
= m_combobox
->GetSelection();
526 if ( sel
== wxNOT_FOUND
)
527 sel
= m_combobox
->GetCount();
529 m_combobox
->Insert(s
, QueryBitmap(&s
), sel
);
532 void BitmapComboBoxWidgetsPage::OnButtonSetFromFile(wxCommandEvent
& WXUNUSED(event
))
534 m_combobox
->SetItemBitmap(m_combobox
->GetSelection(), QueryBitmap(NULL
));
537 void BitmapComboBoxWidgetsPage::OnButtonAddMany(wxCommandEvent
& WXUNUSED(event
))
539 // "many" means 1000 here
540 for ( unsigned int n
= 0; n
< 1000; n
++ )
542 m_combobox
->Append(wxString::Format(_T("item #%u"), n
));
546 void BitmapComboBoxWidgetsPage::OnButtonAddSeveral(wxCommandEvent
& WXUNUSED(event
))
548 m_combobox
->Append(_T("First"));
549 m_combobox
->Append(_T("another one"));
550 m_combobox
->Append(_T("and the last (very very very very very very very very very very long) one"));
553 void BitmapComboBoxWidgetsPage::OnButtonAddSeveralWithImages(wxCommandEvent
& WXUNUSED(event
))
555 static const struct TestEntry
561 { "Red circle", 0x0000ff },
562 { "Blue circle", 0xff0000 },
563 { "Green circle", 0x00ff00 },
564 { "Black circle", 0x000000 },
567 for ( unsigned i
= 0; i
< WXSIZEOF(s_entries
); i
++ )
569 const TestEntry
& e
= s_entries
[i
];
570 m_combobox
->Append(e
.text
, CreateBitmap(wxColour(e
.rgb
)));
575 void BitmapComboBoxWidgetsPage::RescaleImage(wxImage
& image
, int w
, int h
)
577 if ( image
.GetWidth() == w
&& image
.GetHeight() == h
)
580 if ( w
<= 0 || h
<= 0 )
583 static bool isFirstScale
= true;
585 if ( isFirstScale
&& m_combobox
->GetCount() > 0 )
587 wxMessageBox( wxT("wxBitmapComboBox normally only supports images of one size. ")
588 wxT("However, for demonstration purposes, loaded bitmaps are scaled to fit ")
589 wxT("using wxImage::Rescale."),
594 isFirstScale
= false;
601 void BitmapComboBoxWidgetsPage::LoadWidgetImages( wxArrayString
* strings
, wxImageList
* images
)
605 fn
.AppendDir(wxT("icons"));
607 wxSetCursor(*wxHOURGLASS_CURSOR
);
609 if ( !wxDir::Exists(fn
.GetFullPath()) ||
610 !wxDir::GetAllFiles(fn
.GetFullPath(),strings
,wxT("*.xpm")) )
614 fn
.AppendDir(wxT("icons"));
615 if ( !wxDir::Exists(fn
.GetFullPath()) ||
616 !wxDir::GetAllFiles(fn
.GetFullPath(),strings
,wxT("*.xpm")) )
618 // Try ../../../samples/widgets/icons
623 fn
.AppendDir(wxT("samples"));
624 fn
.AppendDir(wxT("widgets"));
625 fn
.AppendDir(wxT("icons"));
626 if ( !wxDir::Exists(fn
.GetFullPath()) ||
627 !wxDir::GetAllFiles(fn
.GetFullPath(),strings
,wxT("*.xpm")) )
629 wxLogWarning(wxT("Could not load widget icons."));
630 wxSetCursor(*wxSTANDARD_CURSOR
);
638 // Get size of existing images in list
639 wxSize foundSize
= m_combobox
->GetBitmapSize();
641 for ( i
=0; i
<strings
->size(); i
++ )
643 fn
.SetFullName((*strings
)[i
]);
644 wxString name
=fn
.GetName();
646 // Handle few exceptions
647 if ( name
== wxT("bmpbtn") )
649 strings
->RemoveAt(i
);
655 wxASSERT(fn
.FileExists());
656 wxImage
image(fn
.GetFullPath());
657 wxASSERT(image
.Ok());
658 RescaleImage(image
, foundSize
.x
, foundSize
.y
);
660 wxASSERT( bmp
.Ok() );
662 wxBitmap
bmp(wxNullBitmap
);
665 (*strings
)[i
] = name
;
669 wxSetCursor(*wxSTANDARD_CURSOR
);
672 void BitmapComboBoxWidgetsPage::OnButtonAddWidgetIcons(wxCommandEvent
& WXUNUSED(event
))
674 wxArrayString strings
;
676 wxSize sz
= m_combobox
->GetBitmapSize();
683 wxImageList
images(sz
.x
, sz
.y
);
685 LoadWidgetImages(&strings
, &images
);
689 for ( i
=0; i
<strings
.size(); i
++ )
691 m_combobox
->Append(strings
[i
], images
.GetBitmap(i
));
695 void BitmapComboBoxWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
698 event
.Enable( m_chkSort
->GetValue() || m_chkReadonly
->GetValue() );
701 void BitmapComboBoxWidgetsPage::OnUpdateUIInsert(wxUpdateUIEvent
& event
)
705 bool enable
= !(m_combobox
->GetWindowStyle() & wxCB_SORT
);
707 event
.Enable(enable
);
711 void BitmapComboBoxWidgetsPage::OnUpdateUIDeleteButton(wxUpdateUIEvent
& event
)
716 event
.Enable(m_textDelete
->GetValue().ToULong(&n
) &&
717 (n
< (unsigned)m_combobox
->GetCount()));
721 void BitmapComboBoxWidgetsPage::OnUpdateUIItemManipulator(wxUpdateUIEvent
& event
)
724 event
.Enable(m_combobox
->GetSelection() != wxNOT_FOUND
);
727 void BitmapComboBoxWidgetsPage::OnUpdateUIClearButton(wxUpdateUIEvent
& event
)
730 event
.Enable(m_combobox
->GetCount() != 0);
733 void BitmapComboBoxWidgetsPage::OnUpdateUIAddSeveral(wxUpdateUIEvent
& event
)
736 event
.Enable(!(m_combobox
->GetWindowStyle() & wxCB_SORT
));
739 void BitmapComboBoxWidgetsPage::OnUpdateUIAddSeveralWithImages(wxUpdateUIEvent
& event
)
742 event
.Enable(!(m_combobox
->GetWindowStyle() & wxCB_SORT
));
745 void BitmapComboBoxWidgetsPage::OnComboText(wxCommandEvent
& event
)
750 wxString s
= event
.GetString();
752 wxASSERT_MSG( s
== m_combobox
->GetValue(),
753 _T("event and combobox values should be the same") );
755 if (event
.GetEventType() == wxEVT_COMMAND_TEXT_ENTER
)
756 wxLogMessage(_T("BitmapCombobox enter pressed (now '%s')"), s
.c_str());
758 wxLogMessage(_T("BitmapCombobox text changed (now '%s')"), s
.c_str());
761 void BitmapComboBoxWidgetsPage::OnComboBox(wxCommandEvent
& event
)
763 long sel
= event
.GetInt();
764 m_textDelete
->SetValue(wxString::Format(_T("%ld"), sel
));
766 wxLogMessage(_T("BitmapCombobox item %ld selected"), sel
);
768 wxLogMessage(_T("BitmapCombobox GetValue(): %s"), m_combobox
->GetValue().c_str() );
771 void BitmapComboBoxWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
777 wxBitmap
BitmapComboBoxWidgetsPage::LoadBitmap(const wxString
& filepath
)
779 // Get size of existing images in list
780 wxSize foundSize
= m_combobox
->GetBitmapSize();
782 // Have some reasonable maximum size
783 if ( foundSize
.x
<= 0 )
785 foundSize
.x
= IMG_SIZE_TRUNC
;
786 foundSize
.y
= IMG_SIZE_TRUNC
;
789 wxImage
image(filepath
);
792 // Rescale very large images
793 int ow
= image
.GetWidth();
794 int oh
= image
.GetHeight();
796 if ( foundSize
.x
> 0 &&
797 (ow
!= foundSize
.x
|| oh
!= foundSize
.y
) )
800 if ( w
> foundSize
.x
)
803 if ( h
> foundSize
.y
)
806 RescaleImage(image
, w
, h
);
809 return wxBitmap(image
);
815 wxBitmap
BitmapComboBoxWidgetsPage::LoadBitmap(const wxString
& WXUNUSED(filepath
))
821 wxBitmap
BitmapComboBoxWidgetsPage::QueryBitmap(wxString
* pStr
)
823 wxString filepath
= wxFileSelector(wxT("Choose image file"),
828 wxFD_OPEN
| wxFD_FILE_MUST_EXIST
,
833 ::wxSetCursor( *wxHOURGLASS_CURSOR
);
835 if ( filepath
.length() )
839 *pStr
= wxFileName(filepath
).GetName();
842 bitmap
= LoadBitmap(filepath
);
845 wxLogDebug(wxT("%i, %i"),bitmap
.GetWidth(), bitmap
.GetHeight());
847 ::wxSetCursor( *wxSTANDARD_CURSOR
);
852 wxBitmap
BitmapComboBoxWidgetsPage::CreateBitmap(const wxColour
& colour
)
859 dc
.SelectObject(bmp
);
861 // Draw transparent background
862 wxColour
magic(255, 0, 255);
863 wxBrush
magicBrush(magic
);
864 dc
.SetBrush(magicBrush
);
865 dc
.SetPen(*wxTRANSPARENT_PEN
);
866 dc
.DrawRectangle(0, 0, w
, h
);
868 // Draw image content
869 dc
.SetBrush(wxBrush(colour
));
870 dc
.DrawCircle(h
/2, h
/2+1, h
/2);
872 dc
.SelectObject(wxNullBitmap
);
874 // Finalize transparency with a mask
875 wxMask
*mask
= new wxMask(bmp
, magic
);
881 #endif // wxUSE_BITMAPCOMBOBOX