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"
35 #include "wx/msw/dcclient.h"
36 #include "wx/msw/private.h"
38 // For wxODCB_XXX flags
39 #include "wx/odcombo.h"
42 #define IMAGE_SPACING_CTRL_VERTICAL 7 // Spacing used in control size calculation
45 // ============================================================================
47 // ============================================================================
50 BEGIN_EVENT_TABLE(wxBitmapComboBox
, wxComboBox
)
51 EVT_SIZE(wxBitmapComboBox::OnSize
)
55 IMPLEMENT_DYNAMIC_CLASS(wxBitmapComboBox
, wxComboBox
)
58 // ----------------------------------------------------------------------------
59 // wxBitmapComboBox creation
60 // ----------------------------------------------------------------------------
62 void wxBitmapComboBox::Init()
67 wxBitmapComboBox::wxBitmapComboBox(wxWindow
*parent
,
69 const wxString
& value
,
72 const wxArrayString
& choices
,
74 const wxValidator
& validator
,
77 wxBitmapComboBoxBase()
81 Create(parent
,id
,value
,pos
,size
,choices
,style
,validator
,name
);
84 bool wxBitmapComboBox::Create(wxWindow
*parent
,
86 const wxString
& value
,
89 const wxArrayString
& choices
,
91 const wxValidator
& validator
,
94 wxCArrayString
chs(choices
);
95 return Create(parent
, id
, value
, pos
, size
, chs
.GetCount(),
96 chs
.GetStrings(), style
, validator
, name
);
99 bool wxBitmapComboBox::Create(wxWindow
*parent
,
101 const wxString
& value
,
105 const wxString choices
[],
107 const wxValidator
& validator
,
108 const wxString
& name
)
110 if ( !wxComboBox::Create(parent
, id
, value
, pos
, size
,
111 n
, choices
, style
, validator
, name
) )
119 WXDWORD
wxBitmapComboBox::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
121 return wxComboBox::MSWGetStyle(style
, exstyle
) | CBS_OWNERDRAWFIXED
| CBS_HASSTRINGS
;
124 void wxBitmapComboBox::RecreateControl()
127 // Recreate control so that WM_MEASUREITEM gets called again.
128 // Can't use CBS_OWNERDRAWVARIABLE because it has odd
129 // mouse-wheel behaviour.
131 wxString value
= GetValue();
132 wxPoint pos
= GetPosition();
133 wxSize size
= GetSize();
134 size
.y
= GetBestSize().y
;
135 wxArrayString strings
= GetStrings();
137 wxComboBox::DoClear();
139 HWND hwnd
= GetHwnd();
141 ::DestroyWindow(hwnd
);
143 if ( !MSWCreateControl(wxT("COMBOBOX"), wxEmptyString
, pos
, size
) )
146 // initialize the controls contents
147 for ( unsigned int i
= 0; i
< strings
.size(); i
++ )
149 wxComboBox::Append(strings
[i
]);
152 // and make sure it has the same attributes as before
155 // calling SetFont(m_font) would do nothing as the code would
156 // notice that the font didn't change, so force it to believe
158 wxFont font
= m_font
;
165 wxColour colFg
= m_foregroundColour
;
166 m_foregroundColour
= wxNullColour
;
167 SetForegroundColour(colFg
);
172 wxColour colBg
= m_backgroundColour
;
173 m_backgroundColour
= wxNullColour
;
174 SetBackgroundColour(colBg
);
178 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
181 ::SendMessage(GetHwnd(), CB_SETITEMHEIGHT
, 0, MeasureItem(0));
183 // Revert the old string value
184 if ( !HasFlag(wxCB_READONLY
) )
188 wxBitmapComboBox::~wxBitmapComboBox()
193 wxSize
wxBitmapComboBox::DoGetBestSize() const
195 wxSize best
= wxComboBox::DoGetBestSize();
196 wxSize bitmapSize
= GetBitmapSize();
198 wxCoord useHeightBitmap
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(bitmapSize
.y
);
199 if ( best
.y
< useHeightBitmap
)
201 best
.y
= useHeightBitmap
;
207 // ----------------------------------------------------------------------------
209 // ----------------------------------------------------------------------------
211 void wxBitmapComboBox::SetItemBitmap(unsigned int n
, const wxBitmap
& bitmap
)
214 DoSetItemBitmap(n
, bitmap
);
216 if ( (int)n
== GetSelection() )
220 int wxBitmapComboBox::Append(const wxString
& item
, const wxBitmap
& bitmap
)
223 const int n
= wxComboBox::Append(item
);
224 if ( n
!= wxNOT_FOUND
)
225 DoSetItemBitmap(n
, bitmap
);
229 int wxBitmapComboBox::Append(const wxString
& item
, const wxBitmap
& bitmap
,
233 const int n
= wxComboBox::Append(item
, clientData
);
234 if ( n
!= wxNOT_FOUND
)
235 DoSetItemBitmap(n
, bitmap
);
239 int wxBitmapComboBox::Append(const wxString
& item
, const wxBitmap
& bitmap
,
240 wxClientData
*clientData
)
243 const int n
= wxComboBox::Append(item
, clientData
);
244 if ( n
!= wxNOT_FOUND
)
245 DoSetItemBitmap(n
, bitmap
);
249 int wxBitmapComboBox::Insert(const wxString
& item
,
250 const wxBitmap
& bitmap
,
254 const int n
= wxComboBox::Insert(item
, pos
);
255 if ( n
!= wxNOT_FOUND
)
256 DoSetItemBitmap(n
, bitmap
);
260 int wxBitmapComboBox::Insert(const wxString
& item
, const wxBitmap
& bitmap
,
261 unsigned int pos
, void *clientData
)
264 const int n
= wxComboBox::Insert(item
, pos
, clientData
);
265 if ( n
!= wxNOT_FOUND
)
266 DoSetItemBitmap(n
, bitmap
);
270 int wxBitmapComboBox::Insert(const wxString
& item
, const wxBitmap
& bitmap
,
271 unsigned int pos
, wxClientData
*clientData
)
274 const int n
= wxComboBox::Insert(item
, pos
, clientData
);
275 if ( n
!= wxNOT_FOUND
)
276 DoSetItemBitmap(n
, bitmap
);
280 int wxBitmapComboBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
282 void **clientData
, wxClientDataType type
)
284 const unsigned int numItems
= items
.GetCount();
285 const unsigned int countNew
= GetCount() + numItems
;
287 wxASSERT( numItems
== 1 || !HasFlag(wxCB_SORT
) ); // Sanity check
289 m_bitmaps
.Alloc(countNew
);
291 for ( unsigned int i
= 0; i
< numItems
; i
++ )
293 m_bitmaps
.Insert(new wxBitmap(wxNullBitmap
), pos
+ i
);
296 const int index
= wxComboBox::DoInsertItems(items
, pos
,
299 if ( index
== wxNOT_FOUND
)
301 for ( int i
= numItems
-1; i
>= 0; i
-- )
302 BCBDoDeleteOneItem(pos
+ i
);
304 else if ( ((unsigned int)index
) != pos
)
306 // Move pre-inserted empty bitmap into correct position
307 // (usually happens when combo box has wxCB_SORT style)
308 wxBitmap
* bmp
= static_cast<wxBitmap
*>(m_bitmaps
[pos
]);
309 m_bitmaps
.RemoveAt(pos
);
310 m_bitmaps
.Insert(bmp
, index
);
316 bool wxBitmapComboBox::OnAddBitmap(const wxBitmap
& bitmap
)
318 if ( wxBitmapComboBoxBase::OnAddBitmap(bitmap
) )
320 // Need to recreate control for a new measureitem call?
321 int prevItemHeight
= ::SendMessage(GetHwnd(), CB_GETITEMHEIGHT
, 0, 0);
323 if ( prevItemHeight
!= MeasureItem(0) )
332 void wxBitmapComboBox::DoClear()
334 wxComboBox::DoClear();
335 wxBitmapComboBoxBase::BCBDoClear();
338 void wxBitmapComboBox::DoDeleteOneItem(unsigned int n
)
340 wxComboBox::DoDeleteOneItem(n
);
341 wxBitmapComboBoxBase::BCBDoDeleteOneItem(n
);
344 // ----------------------------------------------------------------------------
345 // wxBitmapComboBox event handlers and such
346 // ----------------------------------------------------------------------------
348 void wxBitmapComboBox::OnSize(wxSizeEvent
& event
)
350 // Prevent infinite looping
361 // ----------------------------------------------------------------------------
362 // wxBitmapComboBox miscellaneous
363 // ----------------------------------------------------------------------------
365 bool wxBitmapComboBox::SetFont(const wxFont
& font
)
367 bool res
= wxComboBox::SetFont(font
);
372 // ----------------------------------------------------------------------------
373 // wxBitmapComboBox item drawing and measuring
374 // ----------------------------------------------------------------------------
376 bool wxBitmapComboBox::MSWOnDraw(WXDRAWITEMSTRUCT
*item
)
378 LPDRAWITEMSTRUCT lpDrawItem
= (LPDRAWITEMSTRUCT
) item
;
379 int pos
= lpDrawItem
->itemID
;
381 // Draw default for item -1, which means 'focus rect only'
386 if ( lpDrawItem
->itemState
& ODS_COMBOBOXEDIT
)
387 flags
|= wxODCB_PAINTING_CONTROL
;
388 if ( lpDrawItem
->itemState
& ODS_SELECTED
)
389 flags
|= wxODCB_PAINTING_SELECTED
;
393 if ( flags
& wxODCB_PAINTING_CONTROL
)
396 if ( !HasFlag(wxCB_READONLY
) )
401 text
= GetString(pos
);
404 wxPaintDCEx
dc(this, lpDrawItem
->hDC
);
405 wxRect rect
= wxRectFromRECT(lpDrawItem
->rcItem
);
406 wxBitmapComboBoxBase::DrawBackground(dc
, rect
, pos
, flags
);
407 wxBitmapComboBoxBase::DrawItem(dc
, rect
, pos
, text
, flags
);
409 // If the item has the focus, draw focus rectangle.
410 // Commented out since regular combo box doesn't
411 // seem to do it either.
412 //if ( lpDrawItem->itemState & ODS_FOCUS )
413 // DrawFocusRect(lpDrawItem->hDC, &lpDrawItem->rcItem);
418 bool wxBitmapComboBox::MSWOnMeasure(WXMEASUREITEMSTRUCT
*item
)
420 LPMEASUREITEMSTRUCT lpMeasureItem
= (LPMEASUREITEMSTRUCT
) item
;
421 int pos
= lpMeasureItem
->itemID
;
423 lpMeasureItem
->itemHeight
= wxBitmapComboBoxBase::MeasureItem(pos
);
428 #endif // wxUSE_BITMAPCOMBOBOX