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"
44 const wxChar wxBitmapComboBoxNameStr
[] = wxT("bitmapComboBox");
47 // These macros allow wxArrayPtrVoid to be used in more convenient manner
48 #define GetBitmapPtr(n) ((wxBitmap*)m_bitmaps[n])
51 #define IMAGE_SPACING_RIGHT 4 // Space left of image
53 #define IMAGE_SPACING_LEFT 4 // Space right of image, left of text
55 #define IMAGE_SPACING_VERTICAL 2 // Space top and bottom of image
57 #define IMAGE_SPACING_CTRL_VERTICAL 7 // Spacing used in control size calculation
59 #define EXTRA_FONT_HEIGHT 0 // Add to increase min. height of list items
62 // ============================================================================
64 // ============================================================================
67 BEGIN_EVENT_TABLE(wxBitmapComboBox
, wxOwnerDrawnComboBox
)
68 EVT_SIZE(wxBitmapComboBox::OnResize
)
72 IMPLEMENT_DYNAMIC_CLASS(wxBitmapComboBox
, wxOwnerDrawnComboBox
)
74 void wxBitmapComboBox::Init()
81 wxBitmapComboBox::wxBitmapComboBox(wxWindow
*parent
,
83 const wxString
& value
,
86 const wxArrayString
& choices
,
88 const wxValidator
& validator
,
90 : wxOwnerDrawnComboBox(),
91 wxBitmapComboBoxBase()
95 Create(parent
,id
,value
,pos
,size
,choices
,style
,validator
,name
);
98 bool wxBitmapComboBox::Create(wxWindow
*parent
,
100 const wxString
& value
,
103 const wxArrayString
& choices
,
105 const wxValidator
& validator
,
106 const wxString
& name
)
108 if ( !wxOwnerDrawnComboBox::Create(parent
, id
, value
,
121 bool wxBitmapComboBox::Create(wxWindow
*parent
,
123 const wxString
& value
,
127 const wxString choices
[],
129 const wxValidator
& validator
,
130 const wxString
& name
)
132 if ( !wxOwnerDrawnComboBox::Create(parent
, id
, value
,
145 void wxBitmapComboBox::PostCreate()
147 m_fontHeight
= GetCharHeight() + EXTRA_FONT_HEIGHT
;
150 wxBitmapComboBox::~wxBitmapComboBox()
155 // ----------------------------------------------------------------------------
157 // ----------------------------------------------------------------------------
159 void wxBitmapComboBox::SetItemBitmap(unsigned int n
, const wxBitmap
& bitmap
)
161 wxCHECK_RET( n
< m_bitmaps
.size(), wxT("invalid item index") );
163 *GetBitmapPtr(n
) = bitmap
;
165 if ( (int)n
== GetSelection() )
169 wxBitmap
wxBitmapComboBox::GetItemBitmap(unsigned int n
) const
171 wxCHECK_MSG( n
< m_bitmaps
.size(), wxNullBitmap
, wxT("invalid item index") );
172 return *GetBitmapPtr(n
);
175 int wxBitmapComboBox::Insert(const wxString
& item
, const wxBitmap
& bitmap
,
176 unsigned int pos
, void *clientData
)
178 int n
= DoInsertWithImage(item
, bitmap
, pos
);
179 if ( n
!= wxNOT_FOUND
)
180 SetClientData(n
, clientData
);
185 int wxBitmapComboBox::Insert(const wxString
& item
, const wxBitmap
& bitmap
,
186 unsigned int pos
, wxClientData
*clientData
)
188 int n
= DoInsertWithImage(item
, bitmap
, pos
);
189 if ( n
!= wxNOT_FOUND
)
190 SetClientObject(n
, clientData
);
195 bool wxBitmapComboBox::OnAddBitmap(const wxBitmap
& bitmap
)
199 int width
= bitmap
.GetWidth();
200 int height
= bitmap
.GetHeight();
202 if ( m_usedImgSize
.x
<= 0 )
205 // If size not yet determined, get it from this image.
206 m_usedImgSize
.x
= width
;
207 m_usedImgSize
.y
= height
;
209 InvalidateBestSize();
210 wxSize newSz
= GetBestSize();
211 wxSize sz
= GetSize();
212 if ( newSz
.y
> sz
.y
)
213 SetSize(sz
.x
, newSz
.y
);
218 wxCHECK_MSG(width
== m_usedImgSize
.x
&& height
== m_usedImgSize
.y
,
220 wxT("you can only add images of same size"));
226 bool wxBitmapComboBox::DoInsertBitmap(const wxBitmap
& bitmap
, unsigned int pos
)
228 if ( !OnAddBitmap(bitmap
) )
231 // NB: We must try to set the image before DoInsert or
232 // DoAppend because OnMeasureItem might be called
233 // before it returns.
234 m_bitmaps
.Insert( new wxBitmap(bitmap
), pos
);
239 int wxBitmapComboBox::DoAppendWithImage(const wxString
& item
, const wxBitmap
& image
)
241 unsigned int pos
= m_bitmaps
.size();
243 if ( !DoInsertBitmap(image
, pos
) )
246 int index
= wxOwnerDrawnComboBox::DoAppend(item
);
249 index
= m_bitmaps
.size();
251 // Need to re-check the index incase DoAppend sorted
252 if ( (unsigned int) index
!= pos
)
254 wxBitmap
* bmp
= GetBitmapPtr(pos
);
255 m_bitmaps
.RemoveAt(pos
);
256 m_bitmaps
.Insert(bmp
, index
);
262 int wxBitmapComboBox::DoInsertWithImage(const wxString
& item
,
263 const wxBitmap
& image
,
266 if ( !DoInsertBitmap(image
, pos
) )
269 return wxOwnerDrawnComboBox::DoInsert(item
, pos
);
272 int wxBitmapComboBox::DoAppend(const wxString
& item
)
274 return DoAppendWithImage(item
, wxNullBitmap
);
277 int wxBitmapComboBox::DoInsert(const wxString
& item
, unsigned int pos
)
279 return DoInsertWithImage(item
, wxNullBitmap
, pos
);
282 void wxBitmapComboBox::Clear()
284 wxOwnerDrawnComboBox::Clear();
288 for ( i
=0; i
<m_bitmaps
.size(); i
++ )
289 delete GetBitmapPtr(i
);
299 void wxBitmapComboBox::Delete(unsigned int n
)
301 wxOwnerDrawnComboBox::Delete(n
);
302 delete GetBitmapPtr(n
);
303 m_bitmaps
.RemoveAt(n
);
306 // ----------------------------------------------------------------------------
307 // wxBitmapComboBox event handlers and such
308 // ----------------------------------------------------------------------------
310 void wxBitmapComboBox::DetermineIndent()
313 // Recalculate amount of empty space needed in front of
314 // text in control itself.
315 int indent
= m_imgAreaWidth
= 0;
317 if ( m_usedImgSize
.x
> 0 )
319 indent
= m_usedImgSize
.y
+ IMAGE_SPACING_LEFT
+ IMAGE_SPACING_RIGHT
;
320 m_imgAreaWidth
= indent
;
325 SetCustomPaintWidth(indent
);
328 void wxBitmapComboBox::OnResize(wxSizeEvent
& event
)
330 // Prevent infinite looping
341 wxSize
wxBitmapComboBox::DoGetBestSize() const
343 wxSize sz
= wxOwnerDrawnComboBox::DoGetBestSize();
345 // Scale control to match height of highest image.
346 int h2
= m_usedImgSize
.y
+ IMAGE_SPACING_CTRL_VERTICAL
;
355 // ----------------------------------------------------------------------------
356 // wxBitmapComboBox miscellaneous
357 // ----------------------------------------------------------------------------
359 bool wxBitmapComboBox::SetFont(const wxFont
& font
)
361 bool res
= wxOwnerDrawnComboBox::SetFont(font
);
362 m_fontHeight
= GetCharHeight() + EXTRA_FONT_HEIGHT
;
366 // ----------------------------------------------------------------------------
367 // wxBitmapComboBox item drawing and measuring
368 // ----------------------------------------------------------------------------
370 void wxBitmapComboBox::OnDrawBackground(wxDC
& dc
,
375 if ( GetCustomPaintWidth() == 0 ||
376 !(flags
& wxODCB_PAINTING_SELECTED
) ||
379 wxOwnerDrawnComboBox::OnDrawBackground(dc
, rect
, item
, flags
);
384 // Just paint simple selection background under where is text
385 // (ie. emulate what MSW image choice does).
388 int xPos
= 0; // Starting x of selection rectangle
389 const int vSizeDec
= 1; // Vertical size reduction of selection rectangle edges
391 xPos
= GetCustomPaintWidth() + 2;
394 GetTextExtent(GetString(item
), &x
, &y
, 0, 0);
396 dc
.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
));
398 wxColour selCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
401 dc
.DrawRectangle(rect
.x
+xPos
,
404 rect
.height
-(vSizeDec
*2));
407 void wxBitmapComboBox::OnDrawItem(wxDC
& dc
,
413 int imgAreaWidth
= m_imgAreaWidth
;
416 if ( imgAreaWidth
== 0 )
418 wxOwnerDrawnComboBox::OnDrawItem(dc
, rect
, item
, flags
);
422 if ( flags
& wxODCB_PAINTING_CONTROL
)
425 if ( HasFlag(wxCB_READONLY
) )
432 text
= GetString(item
);
436 const wxBitmap
& bmp
= *GetBitmapPtr(item
);
439 wxCoord w
= bmp
.GetWidth();
440 wxCoord h
= bmp
.GetHeight();
442 // Draw the image centered
444 rect
.x
+ (m_usedImgSize
.x
-w
)/2 + IMAGE_SPACING_LEFT
,
445 rect
.y
+ (rect
.height
-h
)/2,
450 dc
.DrawText(GetString(item
),
451 rect
.x
+ imgAreaWidth
+ 1,
452 rect
.y
+ (rect
.height
-dc
.GetCharHeight())/2);
455 wxCoord
wxBitmapComboBox::OnMeasureItem(size_t WXUNUSED(item
)) const
457 int imgHeightArea
= m_usedImgSize
.y
+ 2;
458 return imgHeightArea
> m_fontHeight
? imgHeightArea
: m_fontHeight
;
461 wxCoord
wxBitmapComboBox::OnMeasureItemWidth(size_t item
) const
464 GetTextExtent(GetString(item
), &x
, &y
, 0, 0);
469 #endif // defined(wxGENERIC_BITMAPCOMBOBOX)
471 #endif // wxUSE_BITMAPCOMBOBOX