1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/bmpcboxg.cpp
3 // Purpose: wxBitmapComboBox
4 // Author: Jaakko Salli
6 // Created: Aug-31-2006
8 // Copyright: (c) 2005 Jaakko Salli
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #include "wx/wxprec.h"
26 #if wxUSE_BITMAPCOMBOBOX
28 #include "wx/bmpcbox.h"
30 #if defined(wxGENERIC_BITMAPCOMBOBOX)
36 #include "wx/odcombo.h"
37 #include "wx/settings.h"
45 #define IMAGE_SPACING_CTRL_VERTICAL 7 // Spacing used in control size calculation
48 // ============================================================================
50 // ============================================================================
53 BEGIN_EVENT_TABLE(wxBitmapComboBox
, wxOwnerDrawnComboBox
)
54 EVT_SIZE(wxBitmapComboBox::OnSize
)
58 IMPLEMENT_DYNAMIC_CLASS(wxBitmapComboBox
, wxOwnerDrawnComboBox
)
60 void wxBitmapComboBox::Init()
65 wxBitmapComboBox::wxBitmapComboBox(wxWindow
*parent
,
67 const wxString
& value
,
70 const wxArrayString
& choices
,
72 const wxValidator
& validator
,
74 : wxOwnerDrawnComboBox(),
75 wxBitmapComboBoxBase()
79 Create(parent
,id
,value
,pos
,size
,choices
,style
,validator
,name
);
82 bool wxBitmapComboBox::Create(wxWindow
*parent
,
84 const wxString
& value
,
87 const wxArrayString
& choices
,
89 const wxValidator
& validator
,
92 if ( !wxOwnerDrawnComboBox::Create(parent
, id
, value
,
105 bool wxBitmapComboBox::Create(wxWindow
*parent
,
107 const wxString
& value
,
111 const wxString choices
[],
113 const wxValidator
& validator
,
114 const wxString
& name
)
116 if ( !wxOwnerDrawnComboBox::Create(parent
, id
, value
,
129 wxBitmapComboBox::~wxBitmapComboBox()
134 // ----------------------------------------------------------------------------
136 // ----------------------------------------------------------------------------
138 void wxBitmapComboBox::SetItemBitmap(unsigned int n
, const wxBitmap
& bitmap
)
141 DoSetItemBitmap(n
, bitmap
);
143 if ( (int)n
== GetSelection() )
147 int wxBitmapComboBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
149 void **clientData
, wxClientDataType type
)
151 const unsigned int numItems
= items
.GetCount();
152 const unsigned int countNew
= GetCount() + numItems
;
154 wxASSERT( numItems
== 1 || !HasFlag(wxCB_SORT
) ); // Sanity check
156 m_bitmaps
.Alloc(countNew
);
158 for ( unsigned int i
= 0; i
< numItems
; i
++ )
160 m_bitmaps
.Insert(new wxBitmap(wxNullBitmap
), pos
+ i
);
163 const int index
= wxOwnerDrawnComboBox::DoInsertItems(items
, pos
,
166 if ( index
== wxNOT_FOUND
)
168 for ( int i
= numItems
-1; i
>= 0; i
-- )
169 BCBDoDeleteOneItem(pos
+ i
);
171 else if ( ((unsigned int)index
) != pos
)
173 // Move pre-inserted empty bitmap into correct position
174 // (usually happens when combo box has wxCB_SORT style)
175 wxBitmap
* bmp
= static_cast<wxBitmap
*>(m_bitmaps
[pos
]);
176 m_bitmaps
.RemoveAt(pos
);
177 m_bitmaps
.Insert(bmp
, index
);
183 int wxBitmapComboBox::Append(const wxString
& item
, const wxBitmap
& bitmap
)
185 const int n
= wxOwnerDrawnComboBox::Append(item
);
187 SetItemBitmap(n
, bitmap
);
191 int wxBitmapComboBox::Append(const wxString
& item
, const wxBitmap
& bitmap
,
194 const int n
= wxOwnerDrawnComboBox::Append(item
, clientData
);
196 SetItemBitmap(n
, bitmap
);
200 int wxBitmapComboBox::Append(const wxString
& item
, const wxBitmap
& bitmap
,
201 wxClientData
*clientData
)
203 const int n
= wxOwnerDrawnComboBox::Append(item
, clientData
);
205 SetItemBitmap(n
, bitmap
);
209 int wxBitmapComboBox::Insert(const wxString
& item
,
210 const wxBitmap
& bitmap
,
213 const int n
= wxOwnerDrawnComboBox::Insert(item
, pos
);
215 SetItemBitmap(n
, bitmap
);
219 int wxBitmapComboBox::Insert(const wxString
& item
, const wxBitmap
& bitmap
,
220 unsigned int pos
, void *clientData
)
222 const int n
= wxOwnerDrawnComboBox::Insert(item
, pos
, clientData
);
224 SetItemBitmap(n
, bitmap
);
228 int wxBitmapComboBox::Insert(const wxString
& item
, const wxBitmap
& bitmap
,
229 unsigned int pos
, wxClientData
*clientData
)
231 const int n
= wxOwnerDrawnComboBox::Insert(item
, pos
, clientData
);
233 SetItemBitmap(n
, bitmap
);
237 void wxBitmapComboBox::DoClear()
239 wxOwnerDrawnComboBox::DoClear();
240 wxBitmapComboBoxBase::BCBDoClear();
243 void wxBitmapComboBox::DoDeleteOneItem(unsigned int n
)
245 wxOwnerDrawnComboBox::DoDeleteOneItem(n
);
246 wxBitmapComboBoxBase::BCBDoDeleteOneItem(n
);
249 // ----------------------------------------------------------------------------
250 // wxBitmapComboBox event handlers and such
251 // ----------------------------------------------------------------------------
253 void wxBitmapComboBox::OnSize(wxSizeEvent
& event
)
255 // Prevent infinite looping
266 wxSize
wxBitmapComboBox::DoGetBestSize() const
268 wxSize sz
= wxOwnerDrawnComboBox::DoGetBestSize();
270 if ( HasFlag(wxCB_READONLY
) )
272 // Scale control to match height of highest image.
273 int h2
= m_usedImgSize
.y
+ IMAGE_SPACING_CTRL_VERTICAL
;
284 // ----------------------------------------------------------------------------
285 // wxBitmapComboBox miscellaneous
286 // ----------------------------------------------------------------------------
288 bool wxBitmapComboBox::SetFont(const wxFont
& font
)
290 bool res
= wxOwnerDrawnComboBox::SetFont(font
);
295 // ----------------------------------------------------------------------------
296 // wxBitmapComboBox item drawing and measuring
297 // ----------------------------------------------------------------------------
299 void wxBitmapComboBox::OnDrawBackground(wxDC
& dc
,
304 if ( GetCustomPaintWidth() == 0 ||
305 !(flags
& wxODCB_PAINTING_SELECTED
) ||
307 ( (flags
& wxODCB_PAINTING_CONTROL
) && (GetInternalFlags() & wxCC_FULL_BUTTON
)) )
309 wxOwnerDrawnComboBox::OnDrawBackground(dc
, rect
, item
, flags
);
313 wxBitmapComboBoxBase::DrawBackground(dc
, rect
, item
, flags
);
316 void wxBitmapComboBox::OnDrawItem(wxDC
& dc
,
322 int imgAreaWidth
= m_imgAreaWidth
;
324 if ( imgAreaWidth
== 0 )
326 wxOwnerDrawnComboBox::OnDrawItem(dc
, rect
, item
, flags
);
330 if ( flags
& wxODCB_PAINTING_CONTROL
)
333 if ( !HasFlag(wxCB_READONLY
) )
338 text
= GetString(item
);
341 wxBitmapComboBoxBase::DrawItem(dc
, rect
, item
, text
, flags
);
344 wxCoord
wxBitmapComboBox::OnMeasureItem(size_t item
) const
346 return wxBitmapComboBoxBase::MeasureItem(item
);
349 wxCoord
wxBitmapComboBox::OnMeasureItemWidth(size_t item
) const
352 GetTextExtent(GetString(item
), &x
, &y
, 0, 0);
357 #endif // defined(wxGENERIC_BITMAPCOMBOBOX)
359 #endif // wxUSE_BITMAPCOMBOBOX