1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/bmpcbox.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 #if wxUSE_BITMAPCOMBOBOX
27 #include "wx/bmpcbox.h"
33 #include "wx/settings.h"
34 #include "wx/vector.h"
36 #include "wx/msw/dcclient.h"
37 #include "wx/msw/private.h"
39 // For wxODCB_XXX flags
40 #include "wx/odcombo.h"
43 #define IMAGE_SPACING_CTRL_VERTICAL 7 // Spacing used in control size calculation
46 // ============================================================================
48 // ============================================================================
51 BEGIN_EVENT_TABLE(wxBitmapComboBox
, wxComboBox
)
52 EVT_SIZE(wxBitmapComboBox::OnSize
)
56 IMPLEMENT_DYNAMIC_CLASS(wxBitmapComboBox
, wxComboBox
)
59 // ----------------------------------------------------------------------------
60 // wxBitmapComboBox creation
61 // ----------------------------------------------------------------------------
63 void wxBitmapComboBox::Init()
68 wxBitmapComboBox::wxBitmapComboBox(wxWindow
*parent
,
70 const wxString
& value
,
73 const wxArrayString
& choices
,
75 const wxValidator
& validator
,
78 wxBitmapComboBoxBase()
82 Create(parent
,id
,value
,pos
,size
,choices
,style
,validator
,name
);
85 bool wxBitmapComboBox::Create(wxWindow
*parent
,
87 const wxString
& value
,
90 const wxArrayString
& choices
,
92 const wxValidator
& validator
,
95 wxCArrayString
chs(choices
);
96 return Create(parent
, id
, value
, pos
, size
, chs
.GetCount(),
97 chs
.GetStrings(), style
, validator
, name
);
100 bool wxBitmapComboBox::Create(wxWindow
*parent
,
102 const wxString
& value
,
106 const wxString choices
[],
108 const wxValidator
& validator
,
109 const wxString
& name
)
111 if ( !wxComboBox::Create(parent
, id
, value
, pos
, size
,
112 n
, choices
, style
, validator
, name
) )
120 WXDWORD
wxBitmapComboBox::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
122 return wxComboBox::MSWGetStyle(style
, exstyle
) | CBS_OWNERDRAWFIXED
| CBS_HASSTRINGS
;
125 void wxBitmapComboBox::RecreateControl()
128 // Recreate control so that WM_MEASUREITEM gets called again.
129 // Can't use CBS_OWNERDRAWVARIABLE because it has odd
130 // mouse-wheel behaviour.
132 wxString value
= GetValue();
133 wxPoint pos
= GetPosition();
134 wxSize size
= GetSize();
135 size
.y
= GetBestSize().y
;
136 wxArrayString strings
= GetStrings();
138 // Save the client data pointers before clearing the control.
139 wxVector
<wxClientData
*> clientData
;
140 clientData
.reserve(strings
.size());
141 for ( size_t n
= 0; n
< strings
.size(); ++n
)
142 clientData
.push_back(GetClientObject(n
));
144 wxComboBox::DoClear();
146 HWND hwnd
= GetHwnd();
148 ::DestroyWindow(hwnd
);
150 if ( !MSWCreateControl(wxT("COMBOBOX"), wxEmptyString
, pos
, size
) )
153 // initialize the controls contents
154 for ( unsigned int i
= 0; i
< strings
.size(); i
++ )
156 wxComboBox::Append(strings
[i
], clientData
[i
]);
159 // and make sure it has the same attributes as before
162 // calling SetFont(m_font) would do nothing as the code would
163 // notice that the font didn't change, so force it to believe
165 wxFont font
= m_font
;
172 wxColour colFg
= m_foregroundColour
;
173 m_foregroundColour
= wxNullColour
;
174 SetForegroundColour(colFg
);
179 wxColour colBg
= m_backgroundColour
;
180 m_backgroundColour
= wxNullColour
;
181 SetBackgroundColour(colBg
);
185 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
188 ::SendMessage(GetHwnd(), CB_SETITEMHEIGHT
, 0, MeasureItem(0));
190 // Revert the old string value
191 if ( !HasFlag(wxCB_READONLY
) )
195 wxBitmapComboBox::~wxBitmapComboBox()
200 wxSize
wxBitmapComboBox::DoGetBestSize() const
202 wxSize best
= wxComboBox::DoGetBestSize();
203 wxSize bitmapSize
= GetBitmapSize();
205 wxCoord useHeightBitmap
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(bitmapSize
.y
);
206 if ( best
.y
< useHeightBitmap
)
208 best
.y
= useHeightBitmap
;
214 // ----------------------------------------------------------------------------
216 // ----------------------------------------------------------------------------
218 void wxBitmapComboBox::SetItemBitmap(unsigned int n
, const wxBitmap
& bitmap
)
221 DoSetItemBitmap(n
, bitmap
);
223 if ( (int)n
== GetSelection() )
227 int wxBitmapComboBox::Append(const wxString
& item
, const wxBitmap
& bitmap
)
230 const int n
= wxComboBox::Append(item
);
231 if ( n
!= wxNOT_FOUND
)
232 DoSetItemBitmap(n
, bitmap
);
236 int wxBitmapComboBox::Append(const wxString
& item
, const wxBitmap
& bitmap
,
240 const int n
= wxComboBox::Append(item
, clientData
);
241 if ( n
!= wxNOT_FOUND
)
242 DoSetItemBitmap(n
, bitmap
);
246 int wxBitmapComboBox::Append(const wxString
& item
, const wxBitmap
& bitmap
,
247 wxClientData
*clientData
)
250 const int n
= wxComboBox::Append(item
, clientData
);
251 if ( n
!= wxNOT_FOUND
)
252 DoSetItemBitmap(n
, bitmap
);
256 int wxBitmapComboBox::Insert(const wxString
& item
,
257 const wxBitmap
& bitmap
,
261 const int n
= wxComboBox::Insert(item
, pos
);
262 if ( n
!= wxNOT_FOUND
)
263 DoSetItemBitmap(n
, bitmap
);
267 int wxBitmapComboBox::Insert(const wxString
& item
, const wxBitmap
& bitmap
,
268 unsigned int pos
, void *clientData
)
271 const int n
= wxComboBox::Insert(item
, pos
, clientData
);
272 if ( n
!= wxNOT_FOUND
)
273 DoSetItemBitmap(n
, bitmap
);
277 int wxBitmapComboBox::Insert(const wxString
& item
, const wxBitmap
& bitmap
,
278 unsigned int pos
, wxClientData
*clientData
)
281 const int n
= wxComboBox::Insert(item
, pos
, clientData
);
282 if ( n
!= wxNOT_FOUND
)
283 DoSetItemBitmap(n
, bitmap
);
287 int wxBitmapComboBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
289 void **clientData
, wxClientDataType type
)
291 const unsigned int numItems
= items
.GetCount();
292 const unsigned int countNew
= GetCount() + numItems
;
294 wxASSERT( numItems
== 1 || !HasFlag(wxCB_SORT
) ); // Sanity check
296 m_bitmaps
.Alloc(countNew
);
298 for ( unsigned int i
= 0; i
< numItems
; i
++ )
300 m_bitmaps
.Insert(new wxBitmap(wxNullBitmap
), pos
+ i
);
303 const int index
= wxComboBox::DoInsertItems(items
, pos
,
306 if ( index
== wxNOT_FOUND
)
308 for ( int i
= numItems
-1; i
>= 0; i
-- )
309 BCBDoDeleteOneItem(pos
+ i
);
311 else if ( ((unsigned int)index
) != pos
)
313 // Move pre-inserted empty bitmap into correct position
314 // (usually happens when combo box has wxCB_SORT style)
315 wxBitmap
* bmp
= static_cast<wxBitmap
*>(m_bitmaps
[pos
]);
316 m_bitmaps
.RemoveAt(pos
);
317 m_bitmaps
.Insert(bmp
, index
);
323 bool wxBitmapComboBox::OnAddBitmap(const wxBitmap
& bitmap
)
325 if ( wxBitmapComboBoxBase::OnAddBitmap(bitmap
) )
327 // Need to recreate control for a new measureitem call?
328 int prevItemHeight
= ::SendMessage(GetHwnd(), CB_GETITEMHEIGHT
, 0, 0);
330 if ( prevItemHeight
!= MeasureItem(0) )
339 void wxBitmapComboBox::DoClear()
341 wxComboBox::DoClear();
342 wxBitmapComboBoxBase::BCBDoClear();
345 void wxBitmapComboBox::DoDeleteOneItem(unsigned int n
)
347 wxComboBox::DoDeleteOneItem(n
);
348 wxBitmapComboBoxBase::BCBDoDeleteOneItem(n
);
351 // ----------------------------------------------------------------------------
352 // wxBitmapComboBox event handlers and such
353 // ----------------------------------------------------------------------------
355 void wxBitmapComboBox::OnSize(wxSizeEvent
& event
)
357 // Prevent infinite looping
368 // ----------------------------------------------------------------------------
369 // wxBitmapComboBox miscellaneous
370 // ----------------------------------------------------------------------------
372 bool wxBitmapComboBox::SetFont(const wxFont
& font
)
374 bool res
= wxComboBox::SetFont(font
);
379 // ----------------------------------------------------------------------------
380 // wxBitmapComboBox item drawing and measuring
381 // ----------------------------------------------------------------------------
383 bool wxBitmapComboBox::MSWOnDraw(WXDRAWITEMSTRUCT
*item
)
385 LPDRAWITEMSTRUCT lpDrawItem
= (LPDRAWITEMSTRUCT
) item
;
386 int pos
= lpDrawItem
->itemID
;
388 // Draw default for item -1, which means 'focus rect only'
393 if ( lpDrawItem
->itemState
& ODS_COMBOBOXEDIT
)
394 flags
|= wxODCB_PAINTING_CONTROL
;
395 if ( lpDrawItem
->itemState
& ODS_SELECTED
)
396 flags
|= wxODCB_PAINTING_SELECTED
;
400 if ( flags
& wxODCB_PAINTING_CONTROL
)
403 if ( !HasFlag(wxCB_READONLY
) )
408 text
= GetString(pos
);
411 wxPaintDCEx
dc(this, lpDrawItem
->hDC
);
412 wxRect rect
= wxRectFromRECT(lpDrawItem
->rcItem
);
413 wxBitmapComboBoxBase::DrawBackground(dc
, rect
, pos
, flags
);
414 wxBitmapComboBoxBase::DrawItem(dc
, rect
, pos
, text
, flags
);
416 // If the item has the focus, draw focus rectangle.
417 // Commented out since regular combo box doesn't
418 // seem to do it either.
419 //if ( lpDrawItem->itemState & ODS_FOCUS )
420 // DrawFocusRect(lpDrawItem->hDC, &lpDrawItem->rcItem);
425 bool wxBitmapComboBox::MSWOnMeasure(WXMEASUREITEMSTRUCT
*item
)
427 LPMEASUREITEMSTRUCT lpMeasureItem
= (LPMEASUREITEMSTRUCT
) item
;
428 int pos
= lpMeasureItem
->itemID
;
430 lpMeasureItem
->itemHeight
= wxBitmapComboBoxBase::MeasureItem(pos
);
435 #endif // wxUSE_BITMAPCOMBOBOX