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::DoInsertItems(const wxArrayStringsAdapter
& items
,
181 void **clientData
, wxClientDataType type
)
183 const unsigned int numItems
= items
.GetCount();
184 const unsigned int countNew
= GetCount() + numItems
;
186 m_bitmaps
.Alloc(countNew
);
188 for ( unsigned int i
= 0; i
< numItems
; ++i
)
190 m_bitmaps
.Insert(new wxBitmap(wxNullBitmap
), pos
+ i
);
193 const int index
= wxOwnerDrawnComboBox::DoInsertItems(items
, pos
,
196 if ( index
== wxNOT_FOUND
)
198 for ( int i
= countNew
- GetCount(); i
> 0; --i
)
200 wxBitmap
*bmp
= GetBitmapPtr(pos
);
201 m_bitmaps
.RemoveAt(pos
);
208 int wxBitmapComboBox::Append(const wxString
& item
, const wxBitmap
& bitmap
)
210 const int n
= wxOwnerDrawnComboBox::Append(item
);
212 SetItemBitmap(n
, bitmap
);
216 int wxBitmapComboBox::Append(const wxString
& item
, const wxBitmap
& bitmap
,
219 const int n
= wxOwnerDrawnComboBox::Append(item
, clientData
);
221 SetItemBitmap(n
, bitmap
);
225 int wxBitmapComboBox::Append(const wxString
& item
, const wxBitmap
& bitmap
,
226 wxClientData
*clientData
)
228 const int n
= wxOwnerDrawnComboBox::Append(item
, clientData
);
230 SetItemBitmap(n
, bitmap
);
234 int wxBitmapComboBox::Insert(const wxString
& item
,
235 const wxBitmap
& bitmap
,
238 const int n
= wxOwnerDrawnComboBox::Insert(item
, pos
);
240 SetItemBitmap(n
, bitmap
);
244 int wxBitmapComboBox::Insert(const wxString
& item
, const wxBitmap
& bitmap
,
245 unsigned int pos
, void *clientData
)
247 const int n
= wxOwnerDrawnComboBox::Insert(item
, pos
, clientData
);
249 SetItemBitmap(n
, bitmap
);
253 int wxBitmapComboBox::Insert(const wxString
& item
, const wxBitmap
& bitmap
,
254 unsigned int pos
, wxClientData
*clientData
)
256 const int n
= wxOwnerDrawnComboBox::Insert(item
, pos
, clientData
);
258 SetItemBitmap(n
, bitmap
);
262 bool wxBitmapComboBox::OnAddBitmap(const wxBitmap
& bitmap
)
266 int width
= bitmap
.GetWidth();
267 int height
= bitmap
.GetHeight();
269 if ( m_usedImgSize
.x
<= 0 )
272 // If size not yet determined, get it from this image.
273 m_usedImgSize
.x
= width
;
274 m_usedImgSize
.y
= height
;
276 InvalidateBestSize();
277 wxSize newSz
= GetBestSize();
278 wxSize sz
= GetSize();
279 if ( newSz
.y
> sz
.y
)
280 SetSize(sz
.x
, newSz
.y
);
285 wxCHECK_MSG(width
== m_usedImgSize
.x
&& height
== m_usedImgSize
.y
,
287 wxT("you can only add images of same size"));
293 void wxBitmapComboBox::DoClear()
295 wxOwnerDrawnComboBox::DoClear();
299 for ( i
=0; i
<m_bitmaps
.size(); i
++ )
300 delete GetBitmapPtr(i
);
310 void wxBitmapComboBox::DoDeleteOneItem(unsigned int n
)
312 wxOwnerDrawnComboBox::DoDeleteOneItem(n
);
313 delete GetBitmapPtr(n
);
314 m_bitmaps
.RemoveAt(n
);
317 // ----------------------------------------------------------------------------
318 // wxBitmapComboBox event handlers and such
319 // ----------------------------------------------------------------------------
321 void wxBitmapComboBox::DetermineIndent()
324 // Recalculate amount of empty space needed in front of
325 // text in control itself.
326 int indent
= m_imgAreaWidth
= 0;
328 if ( m_usedImgSize
.x
> 0 )
330 indent
= m_usedImgSize
.x
+ IMAGE_SPACING_LEFT
+ IMAGE_SPACING_RIGHT
;
331 m_imgAreaWidth
= indent
;
336 SetCustomPaintWidth(indent
);
339 void wxBitmapComboBox::OnSize(wxSizeEvent
& event
)
341 // Prevent infinite looping
352 wxSize
wxBitmapComboBox::DoGetBestSize() const
354 wxSize sz
= wxOwnerDrawnComboBox::DoGetBestSize();
356 // Scale control to match height of highest image.
357 int h2
= m_usedImgSize
.y
+ IMAGE_SPACING_CTRL_VERTICAL
;
366 // ----------------------------------------------------------------------------
367 // wxBitmapComboBox miscellaneous
368 // ----------------------------------------------------------------------------
370 bool wxBitmapComboBox::SetFont(const wxFont
& font
)
372 bool res
= wxOwnerDrawnComboBox::SetFont(font
);
373 m_fontHeight
= GetCharHeight() + EXTRA_FONT_HEIGHT
;
377 // ----------------------------------------------------------------------------
378 // wxBitmapComboBox item drawing and measuring
379 // ----------------------------------------------------------------------------
381 void wxBitmapComboBox::OnDrawBackground(wxDC
& dc
,
386 if ( GetCustomPaintWidth() == 0 ||
387 !(flags
& wxODCB_PAINTING_SELECTED
) ||
389 ( (flags
& wxODCB_PAINTING_CONTROL
) && (GetInternalFlags() & wxCC_FULL_BUTTON
)) )
391 wxOwnerDrawnComboBox::OnDrawBackground(dc
, rect
, item
, flags
);
396 // Just paint simple selection background under where is text
397 // (ie. emulate what MSW image choice does).
400 int xPos
= 0; // Starting x of selection rectangle
401 const int vSizeDec
= 1; // Vertical size reduction of selection rectangle edges
403 xPos
= GetCustomPaintWidth() + 2;
406 GetTextExtent(GetString(item
), &x
, &y
, 0, 0);
408 dc
.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
));
410 wxColour selCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
413 dc
.DrawRectangle(rect
.x
+xPos
,
416 rect
.height
-(vSizeDec
*2));
419 void wxBitmapComboBox::OnDrawItem(wxDC
& dc
,
425 int imgAreaWidth
= m_imgAreaWidth
;
428 if ( imgAreaWidth
== 0 )
430 wxOwnerDrawnComboBox::OnDrawItem(dc
, rect
, item
, flags
);
434 if ( flags
& wxODCB_PAINTING_CONTROL
)
437 if ( HasFlag(wxCB_READONLY
) )
444 text
= GetString(item
);
448 const wxBitmap
& bmp
= *GetBitmapPtr(item
);
451 wxCoord w
= bmp
.GetWidth();
452 wxCoord h
= bmp
.GetHeight();
454 // Draw the image centered
456 rect
.x
+ (m_usedImgSize
.x
-w
)/2 + IMAGE_SPACING_LEFT
,
457 rect
.y
+ (rect
.height
-h
)/2,
462 dc
.DrawText(GetString(item
),
463 rect
.x
+ imgAreaWidth
+ 1,
464 rect
.y
+ (rect
.height
-dc
.GetCharHeight())/2);
467 wxCoord
wxBitmapComboBox::OnMeasureItem(size_t WXUNUSED(item
)) const
469 int imgHeightArea
= m_usedImgSize
.y
+ 2;
470 return imgHeightArea
> m_fontHeight
? imgHeightArea
: m_fontHeight
;
473 wxCoord
wxBitmapComboBox::OnMeasureItemWidth(size_t item
) const
476 GetTextExtent(GetString(item
), &x
, &y
, 0, 0);
481 #endif // defined(wxGENERIC_BITMAPCOMBOBOX)
483 #endif // wxUSE_BITMAPCOMBOBOX