1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/bmpcboxcmn.cpp
3 // Purpose: wxBitmapComboBox
4 // Author: Jaakko Salli
6 // Copyright: (c) 2008 Jaakko Salli
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // ============================================================================
12 // ============================================================================
14 // ----------------------------------------------------------------------------
16 // ----------------------------------------------------------------------------
18 #include "wx/wxprec.h"
24 #include "wx/bmpcbox.h"
26 #if wxUSE_BITMAPCOMBOBOX
31 #include "wx/ctrlsub.h"
34 #include "wx/settings.h"
36 // For wxODCB_XXX flags
37 #include "wx/odcombo.h"
40 const char wxBitmapComboBoxNameStr
[] = "bitmapComboBox";
42 #if defined(wxBITMAPCOMBOBOX_OWNERDRAWN_BASED)
44 #define IMAGE_SPACING_RIGHT 4 // Space left of image
46 #define IMAGE_SPACING_LEFT 4 // Space right of image, left of text
48 #define EXTRA_FONT_HEIGHT 0 // Add to increase min. height of list items
50 #define wxBCB_DEFAULT_ITEM_HEIGHT 13
53 // This macros allows wxArrayPtrVoid to be used in more convenient manner
54 #define GetBitmapPtr(n) ((wxBitmap*)m_bitmaps[n])
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 void wxBitmapComboBoxBase::Init()
66 m_usedImgSize
= wxSize(-1, -1);
69 void wxBitmapComboBoxBase::UpdateInternals()
71 m_fontHeight
= GetControl()->GetCharHeight() + EXTRA_FONT_HEIGHT
;
73 while ( m_bitmaps
.GetCount() < GetItemContainer()->GetCount() )
74 m_bitmaps
.Add( new wxBitmap() );
77 // ----------------------------------------------------------------------------
79 // ----------------------------------------------------------------------------
81 void wxBitmapComboBoxBase::DoSetItemBitmap(unsigned int n
, const wxBitmap
& bitmap
)
83 wxCHECK_RET( n
< m_bitmaps
.size(), "invalid item index" );
84 *GetBitmapPtr(n
) = bitmap
;
87 wxBitmap
wxBitmapComboBoxBase::GetItemBitmap(unsigned int n
) const
89 wxCHECK_MSG( n
< m_bitmaps
.size(), wxNullBitmap
, "invalid item index" );
90 return *GetBitmapPtr(n
);
93 // ----------------------------------------------------------------------------
94 // wxItemContainer methods
95 // ----------------------------------------------------------------------------
97 void wxBitmapComboBoxBase::BCBDoClear()
99 for ( unsigned i
= 0; i
< m_bitmaps
.size(); i
++ )
100 delete GetBitmapPtr(i
);
104 m_usedImgSize
.x
= -1;
105 m_usedImgSize
.y
= -1;
110 void wxBitmapComboBoxBase::BCBDoDeleteOneItem(unsigned int n
)
112 delete GetBitmapPtr(n
);
113 m_bitmaps
.RemoveAt(n
);
116 // ----------------------------------------------------------------------------
117 // Preparation and Calculations
118 // ----------------------------------------------------------------------------
120 bool wxBitmapComboBoxBase::OnAddBitmap(const wxBitmap
& bitmap
)
124 int width
= bitmap
.GetWidth();
125 int height
= bitmap
.GetHeight();
127 if ( m_usedImgSize
.x
< 0 )
129 // If size not yet determined, get it from this image.
130 m_usedImgSize
.x
= width
;
131 m_usedImgSize
.y
= height
;
133 // Adjust control size to vertically fit the bitmap
134 wxWindow
* ctrl
= GetControl();
135 ctrl
->InvalidateBestSize();
136 wxSize newSz
= ctrl
->GetBestSize();
137 wxSize sz
= ctrl
->GetSize();
138 if ( newSz
.y
> sz
.y
)
139 ctrl
->SetSize(sz
.x
, newSz
.y
);
144 wxCHECK_MSG( width
== m_usedImgSize
.x
&& height
== m_usedImgSize
.y
,
146 "you can only add images of same size" );
154 int wxBitmapComboBoxBase::DetermineIndent()
156 // Recalculate amount of empty space needed in front of
157 // text in control itself.
158 int indent
= m_imgAreaWidth
= 0;
160 if ( m_usedImgSize
.x
> 0 )
162 indent
= m_usedImgSize
.x
+ IMAGE_SPACING_LEFT
+ IMAGE_SPACING_RIGHT
;
163 m_imgAreaWidth
= indent
;
171 // ----------------------------------------------------------------------------
172 // Item drawing and measuring
173 // ----------------------------------------------------------------------------
175 void wxBitmapComboBoxBase::DrawBackground(wxDC
& dc
,
180 if ( flags
& wxODCB_PAINTING_SELECTED
)
182 const int vSizeDec
= 0; // Vertical size reduction of selection rectangle edges
184 dc
.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
));
186 wxColour selCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
189 dc
.DrawRectangle(rect
.x
,
192 rect
.height
-(vSizeDec
*2));
196 dc
.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
198 wxColour selCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
201 dc
.DrawRectangle(rect
);
205 void wxBitmapComboBoxBase::DrawItem(wxDC
& dc
,
208 const wxString
& text
,
209 int WXUNUSED(flags
)) const
211 const wxBitmap
& bmp
= *GetBitmapPtr(item
);
214 wxCoord w
= bmp
.GetWidth();
215 wxCoord h
= bmp
.GetHeight();
217 // Draw the image centered
219 rect
.x
+ (m_usedImgSize
.x
-w
)/2 + IMAGE_SPACING_LEFT
,
220 rect
.y
+ (rect
.height
-h
)/2,
226 rect
.x
+ m_imgAreaWidth
+ 1,
227 rect
.y
+ (rect
.height
-dc
.GetCharHeight())/2);
230 wxCoord
wxBitmapComboBoxBase::MeasureItem(size_t WXUNUSED(item
)) const
232 if ( m_usedImgSize
.y
>= 0 )
234 int imgHeightArea
= m_usedImgSize
.y
+ 2;
235 return imgHeightArea
> m_fontHeight
? imgHeightArea
: m_fontHeight
;
238 return wxBCB_DEFAULT_ITEM_HEIGHT
;
241 #endif // wxBITMAPCOMBOBOX_OWNERDRAWN_BASED
243 #endif // wxUSE_BITMAPCOMBOBOX