1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/bmpcboxg.cpp
3 // Purpose: wxBitmapComboBox
4 // Author: Jaakko Salli
6 // Created: Aug-31-2006
7 // Copyright: (c) 2005 Jaakko Salli
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 #include "wx/wxprec.h"
25 #if wxUSE_BITMAPCOMBOBOX
27 #include "wx/bmpcbox.h"
29 #if defined(wxGENERIC_BITMAPCOMBOBOX)
35 #include "wx/odcombo.h"
36 #include "wx/settings.h"
44 #define IMAGE_SPACING_CTRL_VERTICAL 7 // Spacing used in control size calculation
47 // ============================================================================
49 // ============================================================================
52 BEGIN_EVENT_TABLE(wxBitmapComboBox
, wxOwnerDrawnComboBox
)
53 EVT_SIZE(wxBitmapComboBox::OnSize
)
57 IMPLEMENT_DYNAMIC_CLASS(wxBitmapComboBox
, wxOwnerDrawnComboBox
)
59 void wxBitmapComboBox::Init()
64 wxBitmapComboBox::wxBitmapComboBox(wxWindow
*parent
,
66 const wxString
& value
,
69 const wxArrayString
& choices
,
71 const wxValidator
& validator
,
73 : wxOwnerDrawnComboBox(),
74 wxBitmapComboBoxBase()
78 Create(parent
,id
,value
,pos
,size
,choices
,style
,validator
,name
);
81 bool wxBitmapComboBox::Create(wxWindow
*parent
,
83 const wxString
& value
,
86 const wxArrayString
& choices
,
88 const wxValidator
& validator
,
91 if ( !wxOwnerDrawnComboBox::Create(parent
, id
, value
,
104 bool wxBitmapComboBox::Create(wxWindow
*parent
,
106 const wxString
& value
,
110 const wxString choices
[],
112 const wxValidator
& validator
,
113 const wxString
& name
)
115 if ( !wxOwnerDrawnComboBox::Create(parent
, id
, value
,
128 wxBitmapComboBox::~wxBitmapComboBox()
133 // ----------------------------------------------------------------------------
135 // ----------------------------------------------------------------------------
137 void wxBitmapComboBox::SetItemBitmap(unsigned int n
, const wxBitmap
& bitmap
)
140 DoSetItemBitmap(n
, bitmap
);
142 if ( (int)n
== GetSelection() )
146 int wxBitmapComboBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
148 void **clientData
, wxClientDataType type
)
150 const unsigned int numItems
= items
.GetCount();
151 const unsigned int countNew
= GetCount() + numItems
;
153 wxASSERT( numItems
== 1 || !HasFlag(wxCB_SORT
) ); // Sanity check
155 m_bitmaps
.Alloc(countNew
);
157 for ( unsigned int i
= 0; i
< numItems
; i
++ )
159 m_bitmaps
.Insert(new wxBitmap(wxNullBitmap
), pos
+ i
);
162 const int index
= wxOwnerDrawnComboBox::DoInsertItems(items
, pos
,
165 if ( index
== wxNOT_FOUND
)
167 for ( int i
= numItems
-1; i
>= 0; i
-- )
168 BCBDoDeleteOneItem(pos
+ i
);
170 else if ( ((unsigned int)index
) != pos
)
172 // Move pre-inserted empty bitmap into correct position
173 // (usually happens when combo box has wxCB_SORT style)
174 wxBitmap
* bmp
= static_cast<wxBitmap
*>(m_bitmaps
[pos
]);
175 m_bitmaps
.RemoveAt(pos
);
176 m_bitmaps
.Insert(bmp
, index
);
182 int wxBitmapComboBox::Append(const wxString
& item
, const wxBitmap
& bitmap
)
184 const int n
= wxOwnerDrawnComboBox::Append(item
);
186 SetItemBitmap(n
, bitmap
);
190 int wxBitmapComboBox::Append(const wxString
& item
, const wxBitmap
& bitmap
,
193 const int n
= wxOwnerDrawnComboBox::Append(item
, clientData
);
195 SetItemBitmap(n
, bitmap
);
199 int wxBitmapComboBox::Append(const wxString
& item
, const wxBitmap
& bitmap
,
200 wxClientData
*clientData
)
202 const int n
= wxOwnerDrawnComboBox::Append(item
, clientData
);
204 SetItemBitmap(n
, bitmap
);
208 int wxBitmapComboBox::Insert(const wxString
& item
,
209 const wxBitmap
& bitmap
,
212 const int n
= wxOwnerDrawnComboBox::Insert(item
, pos
);
214 SetItemBitmap(n
, bitmap
);
218 int wxBitmapComboBox::Insert(const wxString
& item
, const wxBitmap
& bitmap
,
219 unsigned int pos
, void *clientData
)
221 const int n
= wxOwnerDrawnComboBox::Insert(item
, pos
, clientData
);
223 SetItemBitmap(n
, bitmap
);
227 int wxBitmapComboBox::Insert(const wxString
& item
, const wxBitmap
& bitmap
,
228 unsigned int pos
, wxClientData
*clientData
)
230 const int n
= wxOwnerDrawnComboBox::Insert(item
, pos
, clientData
);
232 SetItemBitmap(n
, bitmap
);
236 void wxBitmapComboBox::DoClear()
238 wxOwnerDrawnComboBox::DoClear();
239 wxBitmapComboBoxBase::BCBDoClear();
242 void wxBitmapComboBox::DoDeleteOneItem(unsigned int n
)
244 wxOwnerDrawnComboBox::DoDeleteOneItem(n
);
245 wxBitmapComboBoxBase::BCBDoDeleteOneItem(n
);
248 // ----------------------------------------------------------------------------
249 // wxBitmapComboBox event handlers and such
250 // ----------------------------------------------------------------------------
252 void wxBitmapComboBox::OnSize(wxSizeEvent
& event
)
254 // Prevent infinite looping
265 wxSize
wxBitmapComboBox::DoGetBestSize() const
267 wxSize sz
= wxOwnerDrawnComboBox::DoGetBestSize();
269 if ( HasFlag(wxCB_READONLY
) )
271 // Scale control to match height of highest image.
272 int h2
= m_usedImgSize
.y
+ IMAGE_SPACING_CTRL_VERTICAL
;
283 // ----------------------------------------------------------------------------
284 // wxBitmapComboBox miscellaneous
285 // ----------------------------------------------------------------------------
287 bool wxBitmapComboBox::SetFont(const wxFont
& font
)
289 bool res
= wxOwnerDrawnComboBox::SetFont(font
);
294 // ----------------------------------------------------------------------------
295 // wxBitmapComboBox item drawing and measuring
296 // ----------------------------------------------------------------------------
298 void wxBitmapComboBox::OnDrawBackground(wxDC
& dc
,
303 if ( GetCustomPaintWidth() == 0 ||
304 !(flags
& wxODCB_PAINTING_SELECTED
) ||
306 ( (flags
& wxODCB_PAINTING_CONTROL
) && (GetInternalFlags() & wxCC_FULL_BUTTON
)) )
308 wxOwnerDrawnComboBox::OnDrawBackground(dc
, rect
, item
, flags
);
312 wxBitmapComboBoxBase::DrawBackground(dc
, rect
, item
, flags
);
315 void wxBitmapComboBox::OnDrawItem(wxDC
& dc
,
321 int imgAreaWidth
= m_imgAreaWidth
;
323 if ( imgAreaWidth
== 0 )
325 wxOwnerDrawnComboBox::OnDrawItem(dc
, rect
, item
, flags
);
329 if ( flags
& wxODCB_PAINTING_CONTROL
)
332 if ( !HasFlag(wxCB_READONLY
) )
337 text
= GetString(item
);
340 wxBitmapComboBoxBase::DrawItem(dc
, rect
, item
, text
, flags
);
343 wxCoord
wxBitmapComboBox::OnMeasureItem(size_t item
) const
345 return wxBitmapComboBoxBase::MeasureItem(item
);
348 wxCoord
wxBitmapComboBox::OnMeasureItemWidth(size_t item
) const
351 GetTextExtent(GetString(item
), &x
, &y
, 0, 0);
356 #endif // defined(wxGENERIC_BITMAPCOMBOBOX)
358 #endif // wxUSE_BITMAPCOMBOBOX