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
;
151 wxBitmapComboBox::~wxBitmapComboBox()
156 // ----------------------------------------------------------------------------
158 // ----------------------------------------------------------------------------
160 void wxBitmapComboBox::SetItemBitmap(unsigned int n
, const wxBitmap
& bitmap
)
162 wxCHECK_RET( n
< m_bitmaps
.size(), wxT("invalid item index") );
164 *GetBitmapPtr(n
) = bitmap
;
166 if ( (int)n
== GetSelection() )
170 wxBitmap
wxBitmapComboBox::GetItemBitmap(unsigned int n
) const
172 wxCHECK_MSG( n
< m_bitmaps
.size(), wxNullBitmap
, wxT("invalid item index") );
173 return *GetBitmapPtr(n
);
176 int wxBitmapComboBox::Insert(const wxString
& item
, const wxBitmap
& bitmap
,
177 unsigned int pos
, void *clientData
)
179 int n
= DoInsertWithImage(item
, bitmap
, pos
);
180 if ( n
!= wxNOT_FOUND
)
181 SetClientData(n
, clientData
);
186 int wxBitmapComboBox::Insert(const wxString
& item
, const wxBitmap
& bitmap
,
187 unsigned int pos
, wxClientData
*clientData
)
189 int n
= DoInsertWithImage(item
, bitmap
, pos
);
190 if ( n
!= wxNOT_FOUND
)
191 SetClientObject(n
, clientData
);
196 bool wxBitmapComboBox::OnAddBitmap(const wxBitmap
& bitmap
)
200 int width
= bitmap
.GetWidth();
201 int height
= bitmap
.GetHeight();
203 if ( m_usedImgSize
.x
<= 0 )
206 // If size not yet determined, get it from this image.
207 m_usedImgSize
.x
= width
;
208 m_usedImgSize
.y
= height
;
210 InvalidateBestSize();
211 wxSize newSz
= GetBestSize();
212 wxSize sz
= GetSize();
213 if ( newSz
.y
> sz
.y
)
214 SetSize(sz
.x
, newSz
.y
);
219 wxCHECK_MSG(width
== m_usedImgSize
.x
&& height
== m_usedImgSize
.y
,
221 wxT("you can only add images of same size"));
227 bool wxBitmapComboBox::DoInsertBitmap(const wxBitmap
& bitmap
, unsigned int pos
)
229 if ( !OnAddBitmap(bitmap
) )
232 // NB: We must try to set the image before DoInsert or
233 // DoAppend because OnMeasureItem might be called
234 // before it returns.
235 m_bitmaps
.Insert( new wxBitmap(bitmap
), pos
);
240 int wxBitmapComboBox::DoAppendWithImage(const wxString
& item
, const wxBitmap
& image
)
242 unsigned int pos
= m_bitmaps
.size();
244 if ( !DoInsertBitmap(image
, pos
) )
247 int index
= wxOwnerDrawnComboBox::DoAppend(item
);
250 index
= m_bitmaps
.size();
252 // Need to re-check the index incase DoAppend sorted
253 if ( (unsigned int) index
!= pos
)
255 wxBitmap
* bmp
= GetBitmapPtr(pos
);
256 m_bitmaps
.RemoveAt(pos
);
257 m_bitmaps
.Insert(bmp
, index
);
263 int wxBitmapComboBox::DoInsertWithImage(const wxString
& item
,
264 const wxBitmap
& image
,
267 wxCHECK_MSG( IsValidInsert(pos
), wxNOT_FOUND
, wxT("invalid item index") );
269 if ( !DoInsertBitmap(image
, pos
) )
272 return wxOwnerDrawnComboBox::DoInsert(item
, pos
);
275 int wxBitmapComboBox::DoAppend(const wxString
& item
)
277 return DoAppendWithImage(item
, wxNullBitmap
);
280 int wxBitmapComboBox::DoInsert(const wxString
& item
, unsigned int pos
)
282 return DoInsertWithImage(item
, wxNullBitmap
, pos
);
285 void wxBitmapComboBox::Clear()
287 wxOwnerDrawnComboBox::Clear();
291 for ( i
=0; i
<m_bitmaps
.size(); i
++ )
292 delete GetBitmapPtr(i
);
302 void wxBitmapComboBox::Delete(unsigned int n
)
304 wxOwnerDrawnComboBox::Delete(n
);
305 delete GetBitmapPtr(n
);
306 m_bitmaps
.RemoveAt(n
);
309 // ----------------------------------------------------------------------------
310 // wxBitmapComboBox event handlers and such
311 // ----------------------------------------------------------------------------
313 void wxBitmapComboBox::DetermineIndent()
316 // Recalculate amount of empty space needed in front of
317 // text in control itself.
318 int indent
= m_imgAreaWidth
= 0;
320 if ( m_usedImgSize
.x
> 0 )
322 indent
= m_usedImgSize
.y
+ IMAGE_SPACING_LEFT
+ IMAGE_SPACING_RIGHT
;
323 m_imgAreaWidth
= indent
;
328 SetCustomPaintWidth(indent
);
331 void wxBitmapComboBox::OnSize(wxSizeEvent
& event
)
333 // Prevent infinite looping
344 wxSize
wxBitmapComboBox::DoGetBestSize() const
346 wxSize sz
= wxOwnerDrawnComboBox::DoGetBestSize();
348 // Scale control to match height of highest image.
349 int h2
= m_usedImgSize
.y
+ IMAGE_SPACING_CTRL_VERTICAL
;
358 // ----------------------------------------------------------------------------
359 // wxBitmapComboBox miscellaneous
360 // ----------------------------------------------------------------------------
362 bool wxBitmapComboBox::SetFont(const wxFont
& font
)
364 bool res
= wxOwnerDrawnComboBox::SetFont(font
);
365 m_fontHeight
= GetCharHeight() + EXTRA_FONT_HEIGHT
;
369 // ----------------------------------------------------------------------------
370 // wxBitmapComboBox item drawing and measuring
371 // ----------------------------------------------------------------------------
373 void wxBitmapComboBox::OnDrawBackground(wxDC
& dc
,
378 if ( GetCustomPaintWidth() == 0 ||
379 !(flags
& wxODCB_PAINTING_SELECTED
) ||
382 wxOwnerDrawnComboBox::OnDrawBackground(dc
, rect
, item
, flags
);
387 // Just paint simple selection background under where is text
388 // (ie. emulate what MSW image choice does).
391 int xPos
= 0; // Starting x of selection rectangle
392 const int vSizeDec
= 1; // Vertical size reduction of selection rectangle edges
394 xPos
= GetCustomPaintWidth() + 2;
397 GetTextExtent(GetString(item
), &x
, &y
, 0, 0);
399 dc
.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
));
401 wxColour selCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
404 dc
.DrawRectangle(rect
.x
+xPos
,
407 rect
.height
-(vSizeDec
*2));
410 void wxBitmapComboBox::OnDrawItem(wxDC
& dc
,
416 int imgAreaWidth
= m_imgAreaWidth
;
419 if ( imgAreaWidth
== 0 )
421 wxOwnerDrawnComboBox::OnDrawItem(dc
, rect
, item
, flags
);
425 if ( flags
& wxODCB_PAINTING_CONTROL
)
428 if ( HasFlag(wxCB_READONLY
) )
435 text
= GetString(item
);
439 const wxBitmap
& bmp
= *GetBitmapPtr(item
);
442 wxCoord w
= bmp
.GetWidth();
443 wxCoord h
= bmp
.GetHeight();
445 // Draw the image centered
447 rect
.x
+ (m_usedImgSize
.x
-w
)/2 + IMAGE_SPACING_LEFT
,
448 rect
.y
+ (rect
.height
-h
)/2,
453 dc
.DrawText(GetString(item
),
454 rect
.x
+ imgAreaWidth
+ 1,
455 rect
.y
+ (rect
.height
-dc
.GetCharHeight())/2);
458 wxCoord
wxBitmapComboBox::OnMeasureItem(size_t WXUNUSED(item
)) const
460 int imgHeightArea
= m_usedImgSize
.y
+ 2;
461 return imgHeightArea
> m_fontHeight
? imgHeightArea
: m_fontHeight
;
464 wxCoord
wxBitmapComboBox::OnMeasureItemWidth(size_t item
) const
467 GetTextExtent(GetString(item
), &x
, &y
, 0, 0);
472 #endif // defined(wxGENERIC_BITMAPCOMBOBOX)
474 #endif // wxUSE_BITMAPCOMBOBOX