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"
57 #include "icons/bmpcombobox.xpm"
59 // Images loaded from file are reduced this width and height, if larger
60 #define IMG_SIZE_TRUNC 256
63 // ----------------------------------------------------------------------------
65 // ----------------------------------------------------------------------------
70 BitmapComboBoxPage_Reset
= wxID_HIGHEST
,
71 BitmapComboBoxPage_Insert
,
72 BitmapComboBoxPage_InsertText
,
73 BitmapComboBoxPage_ChangeHeight
,
74 BitmapComboBoxPage_LoadFromFile
,
75 BitmapComboBoxPage_SetFromFile
,
76 BitmapComboBoxPage_AddWidgetIcons
,
77 BitmapComboBoxPage_AddSeveralWithImages
,
78 BitmapComboBoxPage_AddSeveral
,
79 BitmapComboBoxPage_AddMany
,
80 BitmapComboBoxPage_Clear
,
81 BitmapComboBoxPage_Change
,
82 BitmapComboBoxPage_Delete
,
83 BitmapComboBoxPage_DeleteText
,
84 BitmapComboBoxPage_DeleteSel
,
85 BitmapComboBoxPage_Combo
89 // ----------------------------------------------------------------------------
90 // BitmapComboBoxWidgetsPage
91 // ----------------------------------------------------------------------------
93 class BitmapComboBoxWidgetsPage
: public WidgetsPage
96 BitmapComboBoxWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
98 virtual wxControl
*GetWidget() const { return m_combobox
; }
99 virtual void RecreateWidget() { CreateCombo(); }
101 // lazy creation of the content
102 virtual void CreateContent();
106 void OnButtonReset(wxCommandEvent
& event
);
107 void OnButtonChange(wxCommandEvent
& event
);
108 void OnButtonDelete(wxCommandEvent
& event
);
109 void OnButtonDeleteSel(wxCommandEvent
& event
);
110 void OnButtonClear(wxCommandEvent
& event
);
111 void OnButtonInsert(wxCommandEvent
&event
);
112 void OnTextChangeHeight(wxCommandEvent
& event
);
113 void OnButtonLoadFromFile(wxCommandEvent
& event
);
114 void OnButtonSetFromFile(wxCommandEvent
& event
);
115 void OnButtonAddSeveral(wxCommandEvent
& event
);
116 void OnButtonAddSeveralWithImages(wxCommandEvent
& event
);
117 void OnButtonAddWidgetIcons(wxCommandEvent
& event
);
118 void OnButtonAddMany(wxCommandEvent
& event
);
120 void OnComboBox(wxCommandEvent
& event
);
121 void OnComboText(wxCommandEvent
& event
);
123 void OnCheckOrRadioBox(wxCommandEvent
& event
);
125 void OnTextPopupWidth(wxCommandEvent
& event
);
126 void OnTextPopupHeight(wxCommandEvent
& event
);
127 void OnTextButtonAll(wxCommandEvent
& event
);
129 void OnUpdateUIInsert(wxUpdateUIEvent
& event
);
130 void OnUpdateUIAddSeveral(wxUpdateUIEvent
& event
);
131 void OnUpdateUIAddSeveralWithImages(wxUpdateUIEvent
& event
);
132 void OnUpdateUIClearButton(wxUpdateUIEvent
& event
);
133 void OnUpdateUIDeleteButton(wxUpdateUIEvent
& event
);
134 void OnUpdateUIItemManipulator(wxUpdateUIEvent
& event
);
135 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
137 // reset the bmpcombobox parameters
140 // (re)create the bmpcombobox
143 // helpers for creating bitmaps
144 wxBitmap
CreateBitmap(const wxColour
& colour
);
145 wxBitmap
CreateRandomBitmap(wxString
* pStr
);
146 wxBitmap
LoadBitmap(const wxString
& filepath
);
147 wxBitmap
QueryBitmap(wxString
* pStr
);
149 void LoadWidgetImages( wxArrayString
* strings
, wxImageList
* images
);
151 wxSizer
*CreateSizerWithSmallTextAndLabel(const wxString
& label
,
153 wxTextCtrl
**ppText
);
156 void RescaleImage(wxImage
& image
, int w
, int h
);
162 // the checkboxes for styles
163 wxCheckBox
*m_chkSort
,
166 // the combobox itself and the sizer it is in
167 wxBitmapComboBox
*m_combobox
;
168 wxSizer
*m_sizerCombo
;
170 // the text entries for "Add/change string" and "Delete" buttons
171 wxTextCtrl
*m_textInsert
,
177 DECLARE_EVENT_TABLE()
178 DECLARE_WIDGETS_PAGE(BitmapComboBoxWidgetsPage
)
181 // ----------------------------------------------------------------------------
183 // ----------------------------------------------------------------------------
185 BEGIN_EVENT_TABLE(BitmapComboBoxWidgetsPage
, WidgetsPage
)
186 EVT_BUTTON(BitmapComboBoxPage_Reset
, BitmapComboBoxWidgetsPage::OnButtonReset
)
187 EVT_BUTTON(BitmapComboBoxPage_Change
, BitmapComboBoxWidgetsPage::OnButtonChange
)
188 EVT_BUTTON(BitmapComboBoxPage_Delete
, BitmapComboBoxWidgetsPage::OnButtonDelete
)
189 EVT_BUTTON(BitmapComboBoxPage_DeleteSel
, BitmapComboBoxWidgetsPage::OnButtonDeleteSel
)
190 EVT_BUTTON(BitmapComboBoxPage_Clear
, BitmapComboBoxWidgetsPage::OnButtonClear
)
191 EVT_BUTTON(BitmapComboBoxPage_Insert
, BitmapComboBoxWidgetsPage::OnButtonInsert
)
192 EVT_BUTTON(BitmapComboBoxPage_AddSeveral
, BitmapComboBoxWidgetsPage::OnButtonAddSeveral
)
193 EVT_BUTTON(BitmapComboBoxPage_AddSeveralWithImages
, BitmapComboBoxWidgetsPage::OnButtonAddSeveralWithImages
)
194 EVT_BUTTON(BitmapComboBoxPage_AddWidgetIcons
, BitmapComboBoxWidgetsPage::OnButtonAddWidgetIcons
)
195 EVT_BUTTON(BitmapComboBoxPage_AddMany
, BitmapComboBoxWidgetsPage::OnButtonAddMany
)
196 EVT_BUTTON(BitmapComboBoxPage_LoadFromFile
, BitmapComboBoxWidgetsPage::OnButtonLoadFromFile
)
197 EVT_BUTTON(BitmapComboBoxPage_SetFromFile
, BitmapComboBoxWidgetsPage::OnButtonSetFromFile
)
199 EVT_TEXT_ENTER(BitmapComboBoxPage_InsertText
, BitmapComboBoxWidgetsPage::OnButtonInsert
)
200 EVT_TEXT(BitmapComboBoxPage_ChangeHeight
, BitmapComboBoxWidgetsPage::OnTextChangeHeight
)
201 EVT_TEXT_ENTER(BitmapComboBoxPage_DeleteText
, BitmapComboBoxWidgetsPage::OnButtonDelete
)
203 EVT_UPDATE_UI(BitmapComboBoxPage_Reset
, BitmapComboBoxWidgetsPage::OnUpdateUIResetButton
)
204 EVT_UPDATE_UI(BitmapComboBoxPage_Insert
, BitmapComboBoxWidgetsPage::OnUpdateUIInsert
)
205 EVT_UPDATE_UI(BitmapComboBoxPage_LoadFromFile
, BitmapComboBoxWidgetsPage::OnUpdateUIInsert
)
206 EVT_UPDATE_UI(BitmapComboBoxPage_AddSeveral
, BitmapComboBoxWidgetsPage::OnUpdateUIAddSeveral
)
207 EVT_UPDATE_UI(BitmapComboBoxPage_AddSeveralWithImages
, BitmapComboBoxWidgetsPage::OnUpdateUIAddSeveralWithImages
)
208 EVT_UPDATE_UI(BitmapComboBoxPage_Clear
, BitmapComboBoxWidgetsPage::OnUpdateUIClearButton
)
209 EVT_UPDATE_UI(BitmapComboBoxPage_DeleteText
, BitmapComboBoxWidgetsPage::OnUpdateUIClearButton
)
210 EVT_UPDATE_UI(BitmapComboBoxPage_Delete
, BitmapComboBoxWidgetsPage::OnUpdateUIDeleteButton
)
211 EVT_UPDATE_UI(BitmapComboBoxPage_Change
, BitmapComboBoxWidgetsPage::OnUpdateUIItemManipulator
)
212 EVT_UPDATE_UI(BitmapComboBoxPage_SetFromFile
, BitmapComboBoxWidgetsPage::OnUpdateUIItemManipulator
)
213 EVT_UPDATE_UI(BitmapComboBoxPage_DeleteSel
, BitmapComboBoxWidgetsPage::OnUpdateUIItemManipulator
)
215 EVT_COMBOBOX(BitmapComboBoxPage_Combo
, BitmapComboBoxWidgetsPage::OnComboBox
)
216 EVT_TEXT(BitmapComboBoxPage_Combo
, BitmapComboBoxWidgetsPage::OnComboText
)
217 EVT_TEXT_ENTER(BitmapComboBoxPage_Combo
, BitmapComboBoxWidgetsPage::OnComboText
)
219 EVT_CHECKBOX(wxID_ANY
, BitmapComboBoxWidgetsPage::OnCheckOrRadioBox
)
220 EVT_RADIOBOX(wxID_ANY
, BitmapComboBoxWidgetsPage::OnCheckOrRadioBox
)
223 // ============================================================================
225 // ============================================================================
229 IMPLEMENT_WIDGETS_PAGE(BitmapComboBoxWidgetsPage
, _T("BitmapCombobox"),
230 GENERIC_CTRLS
| WITH_ITEMS_CTRLS
| COMBO_CTRLS
234 BitmapComboBoxWidgetsPage::BitmapComboBoxWidgetsPage(WidgetsBookCtrl
*book
,
235 wxImageList
*imaglist
)
236 : WidgetsPage(book
, imaglist
, bmpcombobox_xpm
)
240 m_chkReadonly
= (wxCheckBox
*)NULL
;
242 m_combobox
= (wxBitmapComboBox
*)NULL
;
243 m_sizerCombo
= (wxSizer
*)NULL
;
246 // create a sizer containing a label and a small text ctrl
247 wxSizer
*BitmapComboBoxWidgetsPage::CreateSizerWithSmallTextAndLabel(const wxString
& label
,
251 wxControl
* control
= new wxStaticText(this, wxID_ANY
, label
);
252 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
253 wxTextCtrl
*text
= new wxTextCtrl(this, id
, wxEmptyString
,
254 wxDefaultPosition
, wxSize(50,wxDefaultCoord
), wxTE_PROCESS_ENTER
);
256 sizerRow
->Add(control
, 0, wxRIGHT
| wxALIGN_CENTRE_VERTICAL
, 5);
257 sizerRow
->Add(text
, 1, wxFIXED_MINSIZE
| wxLEFT
| wxALIGN_CENTRE_VERTICAL
, 5);
265 void BitmapComboBoxWidgetsPage::CreateContent()
268 What we create here is a frame having 3 panes: style pane is the
269 leftmost one, in the middle the pane with buttons allowing to perform
270 miscellaneous combobox operations and the pane containing the combobox
276 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
278 wxSizer
*sizerLeft
= new wxBoxSizer(wxVERTICAL
);
280 // left pane - style box
281 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
283 wxSizer
*sizerStyle
= new wxStaticBoxSizer(box
, wxVERTICAL
);
285 m_chkSort
= CreateCheckBoxAndAddToSizer(sizerStyle
, _T("&Sort items"));
286 m_chkReadonly
= CreateCheckBoxAndAddToSizer(sizerStyle
, _T("&Read only"));
288 wxButton
*btn
= new wxButton(this, BitmapComboBoxPage_Reset
, _T("&Reset"));
289 sizerStyle
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 3);
291 sizerLeft
->Add(sizerStyle
, 0, wxGROW
| wxALIGN_CENTRE_HORIZONTAL
);
293 // left pane - other options box
294 box
= new wxStaticBox(this, wxID_ANY
, _T("Demo options"));
296 wxSizer
*sizerOptions
= new wxStaticBoxSizer(box
, wxVERTICAL
);
298 sizerRow
= CreateSizerWithSmallTextAndLabel(_T("Control &height:"),
299 BitmapComboBoxPage_ChangeHeight
,
300 &m_textChangeHeight
);
301 m_textChangeHeight
->SetSize(20, wxDefaultCoord
);
302 sizerOptions
->Add(sizerRow
, 0, wxALL
| wxFIXED_MINSIZE
/*| wxGROW*/, 5);
304 sizerLeft
->Add(sizerOptions
, 0, wxGROW
| wxALIGN_CENTRE_HORIZONTAL
| wxTOP
, 2);
307 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
,
308 _T("&Change wxBitmapComboBox contents"));
309 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
312 btn
= new wxButton(this, BitmapComboBoxPage_AddWidgetIcons
, _T("Add &widget icons"));
313 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
315 btn
= new wxButton(this, BitmapComboBoxPage_LoadFromFile
, _T("Insert image from &file"));
316 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
318 btn
= new wxButton(this, BitmapComboBoxPage_SetFromFile
, _T("&Set image from file"));
319 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
322 btn
= new wxButton(this, BitmapComboBoxPage_AddSeveralWithImages
, _T("A&ppend a few strings with images"));
323 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
325 btn
= new wxButton(this, BitmapComboBoxPage_AddSeveral
, _T("Append a &few strings"));
326 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
328 btn
= new wxButton(this, BitmapComboBoxPage_AddMany
, _T("Append &many strings"));
329 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
331 sizerRow
= CreateSizerWithTextAndButton(BitmapComboBoxPage_Delete
,
332 _T("&Delete this item"),
333 BitmapComboBoxPage_DeleteText
,
335 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
337 btn
= new wxButton(this, BitmapComboBoxPage_DeleteSel
, _T("Delete &selection"));
338 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
340 btn
= new wxButton(this, BitmapComboBoxPage_Clear
, _T("&Clear"));
341 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
344 wxInitAllImageHandlers();
348 wxSizer
*sizerRight
= new wxBoxSizer(wxVERTICAL
);
349 m_combobox
= new wxBitmapComboBox();
350 m_combobox
->Create(this, BitmapComboBoxPage_Combo
, wxEmptyString
,
351 wxDefaultPosition
, wxDefaultSize
,
355 #if defined(wxGENERIC_BITMAPCOMBOBOX)
356 // This will sure make the list look nicer when larger images are used.
357 m_combobox
->SetPopupMaxHeight(600);
360 sizerRight
->Add(m_combobox
, 0, wxGROW
| wxALL
, 5);
361 sizerRight
->SetMinSize(150, 0);
362 m_sizerCombo
= sizerRight
; // save it to modify it later
364 // the 3 panes panes compose the window
365 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
366 sizerTop
->Add(sizerMiddle
, 5, wxGROW
| wxALL
, 10);
367 sizerTop
->Add(sizerRight
, 4, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
369 // final initializations
377 // ----------------------------------------------------------------------------
379 // ----------------------------------------------------------------------------
381 void BitmapComboBoxWidgetsPage::Reset()
383 m_chkSort
->SetValue(false);
384 m_chkReadonly
->SetValue(true);
387 void BitmapComboBoxWidgetsPage::CreateCombo()
389 int flags
= ms_defaultFlags
;
391 if ( m_chkSort
->GetValue() )
393 if ( m_chkReadonly
->GetValue() )
394 flags
|= wxCB_READONLY
;
397 wxArrayPtrVoid bitmaps
;
400 unsigned int count
= m_combobox
->GetCount();
401 for ( unsigned int n
= 0; n
< count
; n
++ )
403 items
.Add(m_combobox
->GetString(n
));
404 bitmaps
.Add(new wxBitmap(m_combobox
->GetItemBitmap(n
)));
407 m_sizerCombo
->Detach( m_combobox
);
411 m_combobox
= new wxBitmapComboBox();
412 m_combobox
->Create(this, BitmapComboBoxPage_Combo
, wxEmptyString
,
413 wxDefaultPosition
, wxDefaultSize
,
417 #if defined(wxGENERIC_BITMAPCOMBOBOX)
418 // This will sure make the list look nicer when larger images are used.
419 m_combobox
->SetPopupMaxHeight(600);
422 unsigned int count
= items
.GetCount();
423 for ( unsigned int n
= 0; n
< count
; n
++ )
425 wxBitmap
* bmp
= (wxBitmap
*) bitmaps
[n
];
426 m_combobox
->Append(items
[n
], *bmp
);
430 m_sizerCombo
->Add(m_combobox
, 0, wxGROW
| wxALL
, 5);
431 m_sizerCombo
->Layout();
433 // Allow changing height inorder to demonstrate flexible
434 // size of image "thumbnail" painted in the control itself.
436 m_textChangeHeight
->GetValue().ToLong(&h
);
438 m_combobox
->SetSize(wxDefaultCoord
, h
);
441 // ----------------------------------------------------------------------------
443 // ----------------------------------------------------------------------------
445 void BitmapComboBoxWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
452 void BitmapComboBoxWidgetsPage::OnButtonChange(wxCommandEvent
& WXUNUSED(event
))
454 int sel
= m_combobox
->GetSelection();
455 if ( sel
!= wxNOT_FOUND
)
458 m_combobox
->SetString(sel
, m_textChange
->GetValue());
460 wxLogMessage(_T("Not implemented in wxGTK"));
465 void BitmapComboBoxWidgetsPage::OnButtonDelete(wxCommandEvent
& WXUNUSED(event
))
468 if ( !m_textDelete
->GetValue().ToULong(&n
) ||
469 (n
>= m_combobox
->GetCount()) )
474 m_combobox
->Delete(n
);
477 void BitmapComboBoxWidgetsPage::OnButtonDeleteSel(wxCommandEvent
& WXUNUSED(event
))
479 int sel
= m_combobox
->GetSelection();
480 if ( sel
!= wxNOT_FOUND
)
482 m_combobox
->Delete(sel
);
486 void BitmapComboBoxWidgetsPage::OnButtonClear(wxCommandEvent
& WXUNUSED(event
))
491 void BitmapComboBoxWidgetsPage::OnButtonInsert(wxCommandEvent
& WXUNUSED(event
))
493 static unsigned int s_item
= 0;
495 wxString s
= m_textInsert
->GetValue();
496 if ( !m_textInsert
->IsModified() )
498 // update the default string
499 m_textInsert
->SetValue(wxString::Format(_T("test item %u"), ++s_item
));
502 int sel
= m_combobox
->GetSelection();
503 if ( sel
== wxNOT_FOUND
)
504 sel
= m_combobox
->GetCount();
506 m_combobox
->Insert(s
, wxNullBitmap
, m_combobox
->GetSelection());
509 void BitmapComboBoxWidgetsPage::OnTextChangeHeight(wxCommandEvent
& WXUNUSED(event
))
512 m_textChangeHeight
->GetValue().ToLong(&h
);
515 m_combobox
->SetSize(wxDefaultCoord
, h
);
518 void BitmapComboBoxWidgetsPage::OnButtonLoadFromFile(wxCommandEvent
& WXUNUSED(event
))
521 int sel
= m_combobox
->GetSelection();
522 if ( sel
== wxNOT_FOUND
)
523 sel
= m_combobox
->GetCount();
525 m_combobox
->Insert(s
, QueryBitmap(&s
), sel
);
528 void BitmapComboBoxWidgetsPage::OnButtonSetFromFile(wxCommandEvent
& WXUNUSED(event
))
530 m_combobox
->SetItemBitmap(m_combobox
->GetSelection(), QueryBitmap(NULL
));
533 void BitmapComboBoxWidgetsPage::OnButtonAddMany(wxCommandEvent
& WXUNUSED(event
))
535 // "many" means 1000 here
536 for ( unsigned int n
= 0; n
< 1000; n
++ )
538 m_combobox
->Append(wxString::Format(_T("item #%u"), n
));
542 void BitmapComboBoxWidgetsPage::OnButtonAddSeveral(wxCommandEvent
& WXUNUSED(event
))
544 m_combobox
->Append(_T("First"));
545 m_combobox
->Append(_T("another one"));
546 m_combobox
->Append(_T("and the last (very very very very very very very very very very long) one"));
549 void BitmapComboBoxWidgetsPage::OnButtonAddSeveralWithImages(wxCommandEvent
& WXUNUSED(event
))
553 for ( i
=0; i
<4; i
++ )
556 wxBitmap bmp
= CreateRandomBitmap(&s
);
557 m_combobox
->Append(s
, bmp
);
562 void BitmapComboBoxWidgetsPage::RescaleImage(wxImage
& image
, int w
, int h
)
564 if ( image
.GetWidth() == w
&& image
.GetHeight() == h
)
567 if ( w
<= 0 || h
<= 0 )
570 static bool isFirstScale
= true;
572 if ( isFirstScale
&& m_combobox
->GetCount() > 0 )
574 wxMessageBox( wxT("wxBitmapComboBox normally only supports images of one size. ")
575 wxT("However, for demonstration purposes, loaded bitmaps are scaled to fit ")
576 wxT("using wxImage::Rescale."),
581 isFirstScale
= false;
588 void BitmapComboBoxWidgetsPage::LoadWidgetImages( wxArrayString
* strings
, wxImageList
* images
)
592 fn
.AppendDir(wxT("icons"));
594 wxSetCursor(*wxHOURGLASS_CURSOR
);
596 if ( !wxDir::Exists(fn
.GetFullPath()) ||
597 !wxDir::GetAllFiles(fn
.GetFullPath(),strings
,wxT("*.xpm")) )
601 fn
.AppendDir(wxT("icons"));
602 if ( !wxDir::Exists(fn
.GetFullPath()) ||
603 !wxDir::GetAllFiles(fn
.GetFullPath(),strings
,wxT("*.xpm")) )
605 // Try ../../../samples/widgets/icons
610 fn
.AppendDir(wxT("samples"));
611 fn
.AppendDir(wxT("widgets"));
612 fn
.AppendDir(wxT("icons"));
613 if ( !wxDir::Exists(fn
.GetFullPath()) ||
614 !wxDir::GetAllFiles(fn
.GetFullPath(),strings
,wxT("*.xpm")) )
616 wxLogWarning(wxT("Could not load widget icons."));
617 wxSetCursor(*wxSTANDARD_CURSOR
);
625 // Get size of existing images in list
626 wxSize foundSize
= m_combobox
->GetBitmapSize();
628 for ( i
=0; i
<strings
->size(); i
++ )
630 fn
.SetFullName((*strings
)[i
]);
631 wxString name
=fn
.GetName();
633 // Handle few exceptions
634 if ( name
== wxT("bmpbtn") )
636 strings
->RemoveAt(i
);
642 wxASSERT(fn
.FileExists());
643 wxImage
image(fn
.GetFullPath());
644 wxASSERT(image
.Ok());
645 RescaleImage(image
, foundSize
.x
, foundSize
.y
);
647 wxASSERT( bmp
.Ok() );
649 wxBitmap
bmp(wxNullBitmap
);
652 (*strings
)[i
] = name
;
656 wxSetCursor(*wxSTANDARD_CURSOR
);
659 void BitmapComboBoxWidgetsPage::OnButtonAddWidgetIcons(wxCommandEvent
& WXUNUSED(event
))
661 wxArrayString strings
;
663 wxSize sz
= m_combobox
->GetBitmapSize();
670 wxImageList
images(sz
.x
, sz
.y
);
672 LoadWidgetImages(&strings
, &images
);
676 for ( i
=0; i
<strings
.size(); i
++ )
678 m_combobox
->Append(strings
[i
], images
.GetBitmap(i
));
682 void BitmapComboBoxWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
685 event
.Enable( m_chkSort
->GetValue() || m_chkReadonly
->GetValue() );
688 void BitmapComboBoxWidgetsPage::OnUpdateUIInsert(wxUpdateUIEvent
& event
)
692 bool enable
= !(m_combobox
->GetWindowStyle() & wxCB_SORT
);
694 event
.Enable(enable
);
698 void BitmapComboBoxWidgetsPage::OnUpdateUIDeleteButton(wxUpdateUIEvent
& event
)
703 event
.Enable(m_textDelete
->GetValue().ToULong(&n
) &&
704 (n
< (unsigned)m_combobox
->GetCount()));
708 void BitmapComboBoxWidgetsPage::OnUpdateUIItemManipulator(wxUpdateUIEvent
& event
)
711 event
.Enable(m_combobox
->GetSelection() != wxNOT_FOUND
);
714 void BitmapComboBoxWidgetsPage::OnUpdateUIClearButton(wxUpdateUIEvent
& event
)
717 event
.Enable(m_combobox
->GetCount() != 0);
720 void BitmapComboBoxWidgetsPage::OnUpdateUIAddSeveral(wxUpdateUIEvent
& event
)
723 event
.Enable(!(m_combobox
->GetWindowStyle() & wxCB_SORT
));
726 void BitmapComboBoxWidgetsPage::OnUpdateUIAddSeveralWithImages(wxUpdateUIEvent
& event
)
729 event
.Enable(!(m_combobox
->GetWindowStyle() & wxCB_SORT
));
732 void BitmapComboBoxWidgetsPage::OnComboText(wxCommandEvent
& event
)
737 wxString s
= event
.GetString();
739 wxASSERT_MSG( s
== m_combobox
->GetValue(),
740 _T("event and combobox values should be the same") );
742 if (event
.GetEventType() == wxEVT_COMMAND_TEXT_ENTER
)
743 wxLogMessage(_T("BitmapCombobox enter pressed (now '%s')"), s
.c_str());
745 wxLogMessage(_T("BitmapCombobox text changed (now '%s')"), s
.c_str());
748 void BitmapComboBoxWidgetsPage::OnComboBox(wxCommandEvent
& event
)
750 long sel
= event
.GetInt();
751 m_textDelete
->SetValue(wxString::Format(_T("%ld"), sel
));
753 wxLogMessage(_T("BitmapCombobox item %ld selected"), sel
);
755 wxLogMessage(_T("BitmapCombobox GetValue(): %s"), m_combobox
->GetValue().c_str() );
758 void BitmapComboBoxWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
764 wxBitmap
BitmapComboBoxWidgetsPage::LoadBitmap(const wxString
& filepath
)
766 // Get size of existing images in list
767 wxSize foundSize
= m_combobox
->GetBitmapSize();
769 // Have some reasonable maximum size
770 if ( foundSize
.x
<= 0 )
772 foundSize
.x
= IMG_SIZE_TRUNC
;
773 foundSize
.y
= IMG_SIZE_TRUNC
;
776 wxImage
image(filepath
);
779 // Rescale very large images
780 int ow
= image
.GetWidth();
781 int oh
= image
.GetHeight();
783 if ( foundSize
.x
> 0 &&
784 (ow
!= foundSize
.x
|| oh
!= foundSize
.y
) )
787 if ( w
> foundSize
.x
)
790 if ( h
> foundSize
.y
)
793 RescaleImage(image
, w
, h
);
796 return wxBitmap(image
);
802 wxBitmap
BitmapComboBoxWidgetsPage::LoadBitmap(const wxString
& WXUNUSED(filepath
))
808 wxBitmap
BitmapComboBoxWidgetsPage::QueryBitmap(wxString
* pStr
)
810 wxString filepath
= wxFileSelector(wxT("Choose image file"),
815 wxFD_OPEN
| wxFD_FILE_MUST_EXIST
,
820 ::wxSetCursor( *wxHOURGLASS_CURSOR
);
822 if ( filepath
.length() )
826 *pStr
= wxFileName(filepath
).GetName();
829 bitmap
= LoadBitmap(filepath
);
832 wxLogDebug(wxT("%i, %i"),bitmap
.GetWidth(), bitmap
.GetHeight());
834 ::wxSetCursor( *wxSTANDARD_CURSOR
);
839 wxBitmap
BitmapComboBoxWidgetsPage::CreateBitmap(const wxColour
& colour
)
841 int ch
= m_combobox
->GetBitmapSize().y
;
856 dc
.SelectObject(bmp
);
858 // Draw transparent background
859 wxColour
magic(255,0,255);
860 wxBrush
magicBrush(magic
);
861 dc
.SetBrush(magicBrush
);
862 dc
.SetPen(*wxTRANSPARENT_PEN
);
863 dc
.DrawRectangle(0,0,bmp
.GetWidth(),bmp
.GetHeight());
865 // Draw image content
866 dc
.SetBrush(wxBrush(colour
));
867 dc
.DrawCircle(h
/2,h
/2+1,(h
/2));
869 dc
.SelectObject(wxNullBitmap
);
871 // Finalize transparency with a mask
872 wxMask
*mask
= new wxMask(bmp
, magic
);
878 wxBitmap
BitmapComboBoxWidgetsPage::CreateRandomBitmap( wxString
* pStr
)
881 const wxChar
* str
= wxT("");
886 str
= wxT("Red Circle");
887 bmp
= CreateBitmap( *wxRED
);
891 str
= wxT("Green Circle");
892 bmp
= CreateBitmap( *wxGREEN
);
896 str
= wxT("Blue Circle");
897 bmp
= CreateBitmap( *wxBLUE
);
901 str
= wxT("Black Circle");
902 bmp
= CreateBitmap( *wxBLACK
);
906 str
= wxT("Cyan Circle");
907 bmp
= CreateBitmap( *wxCYAN
);
911 str
= wxT("Light Grey Circle");
912 bmp
= CreateBitmap( *wxLIGHT_GREY
);
921 #endif //wxUSE_BITMAPCOMBOBOX