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 const wxChar wxBitmapComboBoxNameStr
[] = wxT("bitmapComboBox");
48 // These macros allow wxArrayPtrVoid to be used in more convenient manner
49 #define GetBitmapPtr(n) ((wxBitmap*)m_bitmaps[n])
52 #define IMAGE_SPACING_RIGHT 4 // Space left of image
54 #define IMAGE_SPACING_LEFT 4 // Space right of image, left of text
56 #define IMAGE_SPACING_VERTICAL 2 // Space top and bottom of image
58 #define IMAGE_SPACING_CTRL_VERTICAL 7 // Spacing used in control size calculation
60 #define EXTRA_FONT_HEIGHT 0 // Add to increase min. height of list items
63 // ============================================================================
65 // ============================================================================
68 BEGIN_EVENT_TABLE(wxBitmapComboBox
, wxOwnerDrawnComboBox
)
69 EVT_SIZE(wxBitmapComboBox::OnSize
)
73 IMPLEMENT_DYNAMIC_CLASS(wxBitmapComboBox
, wxOwnerDrawnComboBox
)
75 void wxBitmapComboBox::Init()
82 wxBitmapComboBox::wxBitmapComboBox(wxWindow
*parent
,
84 const wxString
& value
,
87 const wxArrayString
& choices
,
89 const wxValidator
& validator
,
91 : wxOwnerDrawnComboBox(),
92 wxBitmapComboBoxBase()
96 Create(parent
,id
,value
,pos
,size
,choices
,style
,validator
,name
);
99 bool wxBitmapComboBox::Create(wxWindow
*parent
,
101 const wxString
& value
,
104 const wxArrayString
& choices
,
106 const wxValidator
& validator
,
107 const wxString
& name
)
109 if ( !wxOwnerDrawnComboBox::Create(parent
, id
, value
,
122 bool wxBitmapComboBox::Create(wxWindow
*parent
,
124 const wxString
& value
,
128 const wxString choices
[],
130 const wxValidator
& validator
,
131 const wxString
& name
)
133 if ( !wxOwnerDrawnComboBox::Create(parent
, id
, value
,
146 void wxBitmapComboBox::PostCreate()
148 m_fontHeight
= GetCharHeight() + EXTRA_FONT_HEIGHT
;
150 while ( m_bitmaps
.GetCount() < GetCount() )
151 m_bitmaps
.Add( new wxBitmap() );
154 wxBitmapComboBox::~wxBitmapComboBox()
159 // ----------------------------------------------------------------------------
161 // ----------------------------------------------------------------------------
163 void wxBitmapComboBox::SetItemBitmap(unsigned int n
, const wxBitmap
& bitmap
)
165 wxCHECK_RET( n
< GetCount(), wxT("invalid item index") );
167 *GetBitmapPtr(n
) = bitmap
;
169 if ( (int)n
== GetSelection() )
173 wxBitmap
wxBitmapComboBox::GetItemBitmap(unsigned int n
) const
175 wxCHECK_MSG( n
< GetCount(), wxNullBitmap
, wxT("invalid item index") );
176 return *GetBitmapPtr(n
);
179 int wxBitmapComboBox::Insert(const wxString
& item
, const wxBitmap
& bitmap
,
180 unsigned int pos
, void *clientData
)
182 int n
= DoInsertWithImage(item
, bitmap
, pos
);
183 if ( n
!= wxNOT_FOUND
)
184 SetClientData(n
, clientData
);
189 int wxBitmapComboBox::Insert(const wxString
& item
, const wxBitmap
& bitmap
,
190 unsigned int pos
, wxClientData
*clientData
)
192 int n
= DoInsertWithImage(item
, bitmap
, pos
);
193 if ( n
!= wxNOT_FOUND
)
194 SetClientObject(n
, clientData
);
199 bool wxBitmapComboBox::OnAddBitmap(const wxBitmap
& bitmap
)
203 int width
= bitmap
.GetWidth();
204 int height
= bitmap
.GetHeight();
206 if ( m_usedImgSize
.x
<= 0 )
209 // If size not yet determined, get it from this image.
210 m_usedImgSize
.x
= width
;
211 m_usedImgSize
.y
= height
;
213 InvalidateBestSize();
214 wxSize newSz
= GetBestSize();
215 wxSize sz
= GetSize();
216 if ( newSz
.y
> sz
.y
)
217 SetSize(sz
.x
, newSz
.y
);
222 wxCHECK_MSG(width
== m_usedImgSize
.x
&& height
== m_usedImgSize
.y
,
224 wxT("you can only add images of same size"));
230 bool wxBitmapComboBox::DoInsertBitmap(const wxBitmap
& bitmap
, unsigned int pos
)
232 if ( !OnAddBitmap(bitmap
) )
235 // NB: We must try to set the image before DoInsert or
236 // DoAppend because OnMeasureItem might be called
237 // before it returns.
238 m_bitmaps
.Insert( new wxBitmap(bitmap
), pos
);
243 int wxBitmapComboBox::DoAppendWithImage(const wxString
& item
, const wxBitmap
& image
)
245 unsigned int pos
= m_bitmaps
.size();
247 if ( !DoInsertBitmap(image
, pos
) )
250 int index
= wxOwnerDrawnComboBox::DoAppend(item
);
253 index
= m_bitmaps
.size();
255 // Need to re-check the index incase DoAppend sorted
256 if ( (unsigned int) index
!= pos
)
258 wxBitmap
* bmp
= GetBitmapPtr(pos
);
259 m_bitmaps
.RemoveAt(pos
);
260 m_bitmaps
.Insert(bmp
, index
);
266 int wxBitmapComboBox::DoInsertWithImage(const wxString
& item
,
267 const wxBitmap
& image
,
270 wxCHECK_MSG( IsValidInsert(pos
), wxNOT_FOUND
, wxT("invalid item index") );
272 if ( !DoInsertBitmap(image
, pos
) )
275 return wxOwnerDrawnComboBox::DoInsert(item
, pos
);
278 int wxBitmapComboBox::DoAppend(const wxString
& item
)
280 return DoAppendWithImage(item
, wxNullBitmap
);
283 int wxBitmapComboBox::DoInsert(const wxString
& item
, unsigned int pos
)
285 return DoInsertWithImage(item
, wxNullBitmap
, pos
);
288 void wxBitmapComboBox::Clear()
290 wxOwnerDrawnComboBox::Clear();
294 for ( i
=0; i
<m_bitmaps
.size(); i
++ )
295 delete GetBitmapPtr(i
);
305 void wxBitmapComboBox::Delete(unsigned int n
)
307 wxOwnerDrawnComboBox::Delete(n
);
308 delete GetBitmapPtr(n
);
309 m_bitmaps
.RemoveAt(n
);
312 // ----------------------------------------------------------------------------
313 // wxBitmapComboBox event handlers and such
314 // ----------------------------------------------------------------------------
316 void wxBitmapComboBox::DetermineIndent()
319 // Recalculate amount of empty space needed in front of
320 // text in control itself.
321 int indent
= m_imgAreaWidth
= 0;
323 if ( m_usedImgSize
.x
> 0 )
325 indent
= m_usedImgSize
.x
+ IMAGE_SPACING_LEFT
+ IMAGE_SPACING_RIGHT
;
326 m_imgAreaWidth
= indent
;
331 SetCustomPaintWidth(indent
);
334 void wxBitmapComboBox::OnSize(wxSizeEvent
& event
)
336 // Prevent infinite looping
347 wxSize
wxBitmapComboBox::DoGetBestSize() const
349 wxSize sz
= wxOwnerDrawnComboBox::DoGetBestSize();
351 // Scale control to match height of highest image.
352 int h2
= m_usedImgSize
.y
+ IMAGE_SPACING_CTRL_VERTICAL
;
361 // ----------------------------------------------------------------------------
362 // wxBitmapComboBox miscellaneous
363 // ----------------------------------------------------------------------------
365 bool wxBitmapComboBox::SetFont(const wxFont
& font
)
367 bool res
= wxOwnerDrawnComboBox::SetFont(font
);
368 m_fontHeight
= GetCharHeight() + EXTRA_FONT_HEIGHT
;
372 // ----------------------------------------------------------------------------
373 // wxBitmapComboBox item drawing and measuring
374 // ----------------------------------------------------------------------------
376 void wxBitmapComboBox::OnDrawBackground(wxDC
& dc
,
381 if ( GetCustomPaintWidth() == 0 ||
382 !(flags
& wxODCB_PAINTING_SELECTED
) ||
384 ( (flags
& wxODCB_PAINTING_CONTROL
) && (GetInternalFlags() & wxCC_FULL_BUTTON
)) )
386 wxOwnerDrawnComboBox::OnDrawBackground(dc
, rect
, item
, flags
);
391 // Just paint simple selection background under where is text
392 // (ie. emulate what MSW image choice does).
395 int xPos
= 0; // Starting x of selection rectangle
396 const int vSizeDec
= 1; // Vertical size reduction of selection rectangle edges
398 xPos
= GetCustomPaintWidth() + 2;
401 GetTextExtent(GetString(item
), &x
, &y
, 0, 0);
403 dc
.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
));
405 wxColour selCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
408 dc
.DrawRectangle(rect
.x
+xPos
,
411 rect
.height
-(vSizeDec
*2));
414 void wxBitmapComboBox::OnDrawItem(wxDC
& dc
,
420 int imgAreaWidth
= m_imgAreaWidth
;
423 if ( imgAreaWidth
== 0 )
425 wxOwnerDrawnComboBox::OnDrawItem(dc
, rect
, item
, flags
);
429 if ( flags
& wxODCB_PAINTING_CONTROL
)
432 if ( HasFlag(wxCB_READONLY
) )
439 text
= GetString(item
);
443 const wxBitmap
& bmp
= *GetBitmapPtr(item
);
446 wxCoord w
= bmp
.GetWidth();
447 wxCoord h
= bmp
.GetHeight();
449 // Draw the image centered
451 rect
.x
+ (m_usedImgSize
.x
-w
)/2 + IMAGE_SPACING_LEFT
,
452 rect
.y
+ (rect
.height
-h
)/2,
457 dc
.DrawText(GetString(item
),
458 rect
.x
+ imgAreaWidth
+ 1,
459 rect
.y
+ (rect
.height
-dc
.GetCharHeight())/2);
462 wxCoord
wxBitmapComboBox::OnMeasureItem(size_t WXUNUSED(item
)) const
464 int imgHeightArea
= m_usedImgSize
.y
+ 2;
465 return imgHeightArea
> m_fontHeight
? imgHeightArea
: m_fontHeight
;
468 wxCoord
wxBitmapComboBox::OnMeasureItemWidth(size_t item
) const
471 GetTextExtent(GetString(item
), &x
, &y
, 0, 0);
476 #endif // defined(wxGENERIC_BITMAPCOMBOBOX)
478 #endif // wxUSE_BITMAPCOMBOBOX