1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/bmpcboxcmn.cpp
3 // Purpose: wxBitmapComboBox
4 // Author: Jaakko Salli
7 // Copyright: (c) 2008 Jaakko Salli
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 #include "wx/wxprec.h"
25 #include "wx/bmpcbox.h"
27 #if wxUSE_BITMAPCOMBOBOX
33 #include "wx/settings.h"
35 // For wxODCB_XXX flags
36 #include "wx/odcombo.h"
39 const wxChar wxBitmapComboBoxNameStr
[] = wxT("bitmapComboBox");
41 #if defined(wxBITMAPCOMBOBOX_OWNERDRAWN_BASED)
43 #define IMAGE_SPACING_RIGHT 4 // Space left of image
45 #define IMAGE_SPACING_LEFT 4 // Space right of image, left of text
47 #define EXTRA_FONT_HEIGHT 0 // Add to increase min. height of list items
49 #define wxBCB_DEFAULT_ITEM_HEIGHT 13
52 // This macros allows wxArrayPtrVoid to be used in more convenient manner
53 #define GetBitmapPtr(n) ((wxBitmap*)m_bitmaps[n])
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 void wxBitmapComboBoxBase::Init()
65 m_usedImgSize
= wxSize(-1, -1);
68 void wxBitmapComboBoxBase::UpdateInternals()
70 m_fontHeight
= GetControl()->GetCharHeight() + EXTRA_FONT_HEIGHT
;
72 while ( m_bitmaps
.GetCount() < GetItemContainer()->GetCount() )
73 m_bitmaps
.Add( new wxBitmap() );
76 // ----------------------------------------------------------------------------
78 // ----------------------------------------------------------------------------
80 void wxBitmapComboBoxBase::DoSetItemBitmap(unsigned int n
, const wxBitmap
& bitmap
)
82 wxCHECK_RET( n
< m_bitmaps
.size(), "invalid item index" );
83 *GetBitmapPtr(n
) = bitmap
;
86 wxBitmap
wxBitmapComboBoxBase::GetItemBitmap(unsigned int n
) const
88 wxCHECK_MSG( n
< m_bitmaps
.size(), wxNullBitmap
, "invalid item index" );
89 return *GetBitmapPtr(n
);
92 // ----------------------------------------------------------------------------
93 // wxItemContainer methods
94 // ----------------------------------------------------------------------------
96 void wxBitmapComboBoxBase::BCBDoClear()
98 for ( unsigned i
= 0; i
< m_bitmaps
.size(); i
++ )
99 delete GetBitmapPtr(i
);
103 m_usedImgSize
.x
= -1;
104 m_usedImgSize
.y
= -1;
109 void wxBitmapComboBoxBase::BCBDoDeleteOneItem(unsigned int n
)
111 delete GetBitmapPtr(n
);
112 m_bitmaps
.RemoveAt(n
);
115 // ----------------------------------------------------------------------------
116 // Preparation and Calculations
117 // ----------------------------------------------------------------------------
119 bool wxBitmapComboBoxBase::OnAddBitmap(const wxBitmap
& bitmap
)
123 int width
= bitmap
.GetWidth();
124 int height
= bitmap
.GetHeight();
126 if ( m_usedImgSize
.x
< 0 )
128 // If size not yet determined, get it from this image.
129 m_usedImgSize
.x
= width
;
130 m_usedImgSize
.y
= height
;
132 // Adjust control size to vertically fit the bitmap
133 wxWindow
* ctrl
= GetControl();
134 ctrl
->InvalidateBestSize();
135 wxSize newSz
= ctrl
->GetBestSize();
136 wxSize sz
= ctrl
->GetSize();
137 if ( newSz
.y
> sz
.y
)
138 ctrl
->SetSize(sz
.x
, newSz
.y
);
143 wxCHECK_MSG( width
== m_usedImgSize
.x
&& height
== m_usedImgSize
.y
,
145 "you can only add images of same size" );
153 int wxBitmapComboBoxBase::DetermineIndent()
155 // Recalculate amount of empty space needed in front of
156 // text in control itself.
157 int indent
= m_imgAreaWidth
= 0;
159 if ( m_usedImgSize
.x
> 0 )
161 indent
= m_usedImgSize
.x
+ IMAGE_SPACING_LEFT
+ IMAGE_SPACING_RIGHT
;
162 m_imgAreaWidth
= indent
;
170 // ----------------------------------------------------------------------------
171 // Item drawing and measuring
172 // ----------------------------------------------------------------------------
174 void wxBitmapComboBoxBase::DrawBackground(wxDC
& dc
,
179 if ( flags
& wxODCB_PAINTING_SELECTED
)
181 const int vSizeDec
= 0; // Vertical size reduction of selection rectangle edges
183 dc
.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
));
185 wxColour selCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
188 dc
.DrawRectangle(rect
.x
,
191 rect
.height
-(vSizeDec
*2));
195 dc
.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
197 wxColour selCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
200 dc
.DrawRectangle(rect
);
204 void wxBitmapComboBoxBase::DrawItem(wxDC
& dc
,
207 const wxString
& text
,
208 int WXUNUSED(flags
)) const
210 const wxBitmap
& bmp
= *GetBitmapPtr(item
);
213 wxCoord w
= bmp
.GetWidth();
214 wxCoord h
= bmp
.GetHeight();
216 // Draw the image centered
218 rect
.x
+ (m_usedImgSize
.x
-w
)/2 + IMAGE_SPACING_LEFT
,
219 rect
.y
+ (rect
.height
-h
)/2,
225 rect
.x
+ m_imgAreaWidth
+ 1,
226 rect
.y
+ (rect
.height
-dc
.GetCharHeight())/2);
229 wxCoord
wxBitmapComboBoxBase::MeasureItem(size_t WXUNUSED(item
)) const
231 if ( m_usedImgSize
.y
>= 0 )
233 int imgHeightArea
= m_usedImgSize
.y
+ 2;
234 return imgHeightArea
> m_fontHeight
? imgHeightArea
: m_fontHeight
;
237 return wxBCB_DEFAULT_ITEM_HEIGHT
;
240 #endif // wxBITMAPCOMBOBOX_OWNERDRAWN_BASED
242 #endif // wxUSE_BITMAPCOMBOBOX