[ 1559479 ] wxImageComboBox
[wxWidgets.git] / src / generic / bmpcboxg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/bmpcboxg.cpp
3 // Purpose: wxBitmapComboBox
4 // Author: Jaakko Salli
5 // Modified by:
6 // Created: Aug-31-2006
7 // RCS-ID: $Id:
8 // Copyright: (c) 2005 Jaakko Salli
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #if wxUSE_BITMAPCOMBOBOX
27
28 #include "wx/bmpcbox.h"
29
30 #if defined(wxGENERIC_BITMAPCOMBOBOX)
31
32 #ifndef WX_PRECOMP
33 #include "wx/log.h"
34 #endif
35
36 #include "wx/odcombo.h"
37 #include "wx/settings.h"
38
39 #if wxUSE_IMAGE
40 #include "wx/image.h"
41 #endif
42
43
44 const wxChar wxBitmapComboBoxNameStr[] = wxT("bitmapComboBox");
45
46
47 // These macros allow wxArrayPtrVoid to be used in more convenient manner
48 #define GetBitmapPtr(n) ((wxBitmap*)m_bitmaps[n])
49
50
51 #define IMAGE_SPACING_RIGHT 4 // Space left of image
52
53 #define IMAGE_SPACING_LEFT 4 // Space right of image, left of text
54
55 #define IMAGE_SPACING_VERTICAL 2 // Space top and bottom of image
56
57 #define IMAGE_SPACING_CTRL_VERTICAL 7 // Spacing used in control size calculation
58
59 #define EXTRA_FONT_HEIGHT 0 // Add to increase min. height of list items
60
61
62 // ============================================================================
63 // implementation
64 // ============================================================================
65
66
67 BEGIN_EVENT_TABLE(wxBitmapComboBox, wxOwnerDrawnComboBox)
68 EVT_SIZE(wxBitmapComboBox::OnResize)
69 END_EVENT_TABLE()
70
71
72 IMPLEMENT_DYNAMIC_CLASS(wxBitmapComboBox, wxOwnerDrawnComboBox)
73
74 void wxBitmapComboBox::Init()
75 {
76 m_fontHeight = 0;
77 m_imgAreaWidth = 0;
78 m_inResize = false;
79 }
80
81 wxBitmapComboBox::wxBitmapComboBox(wxWindow *parent,
82 wxWindowID id,
83 const wxString& value,
84 const wxPoint& pos,
85 const wxSize& size,
86 const wxArrayString& choices,
87 long style,
88 const wxValidator& validator,
89 const wxString& name)
90 : wxOwnerDrawnComboBox(),
91 wxBitmapComboBoxBase()
92 {
93 Init();
94
95 Create(parent,id,value,pos,size,choices,style,validator,name);
96 }
97
98 bool wxBitmapComboBox::Create(wxWindow *parent,
99 wxWindowID id,
100 const wxString& value,
101 const wxPoint& pos,
102 const wxSize& size,
103 const wxArrayString& choices,
104 long style,
105 const wxValidator& validator,
106 const wxString& name)
107 {
108 if ( !wxOwnerDrawnComboBox::Create(parent, id, value,
109 pos, size,
110 choices, style,
111 validator, name) )
112 {
113 return false;
114 }
115
116 PostCreate();
117
118 return true;
119 }
120
121 bool wxBitmapComboBox::Create(wxWindow *parent,
122 wxWindowID id,
123 const wxString& value,
124 const wxPoint& pos,
125 const wxSize& size,
126 int n,
127 const wxString choices[],
128 long style,
129 const wxValidator& validator,
130 const wxString& name)
131 {
132 if ( !wxOwnerDrawnComboBox::Create(parent, id, value,
133 pos, size, n,
134 choices, style,
135 validator, name) )
136 {
137 return false;
138 }
139
140 PostCreate();
141
142 return true;
143 }
144
145 void wxBitmapComboBox::PostCreate()
146 {
147 m_fontHeight = GetCharHeight() + EXTRA_FONT_HEIGHT;
148 }
149
150 wxBitmapComboBox::~wxBitmapComboBox()
151 {
152 Clear();
153 }
154
155 // ----------------------------------------------------------------------------
156 // Item manipulation
157 // ----------------------------------------------------------------------------
158
159 void wxBitmapComboBox::SetItemBitmap(unsigned int n, const wxBitmap& bitmap)
160 {
161 wxCHECK_RET( n < m_bitmaps.size(), wxT("invalid item index") );
162 OnAddBitmap(bitmap);
163 *GetBitmapPtr(n) = bitmap;
164
165 if ( (int)n == GetSelection() )
166 Refresh();
167 }
168
169 wxBitmap wxBitmapComboBox::GetItemBitmap(unsigned int n) const
170 {
171 wxCHECK_MSG( n < m_bitmaps.size(), wxNullBitmap, wxT("invalid item index") );
172 return *GetBitmapPtr(n);
173 }
174
175 int wxBitmapComboBox::Insert(const wxString& item, const wxBitmap& bitmap,
176 unsigned int pos, void *clientData)
177 {
178 int n = DoInsertWithImage(item, bitmap, pos);
179 if ( n != wxNOT_FOUND )
180 SetClientData(n, clientData);
181
182 return n;
183 }
184
185 int wxBitmapComboBox::Insert(const wxString& item, const wxBitmap& bitmap,
186 unsigned int pos, wxClientData *clientData)
187 {
188 int n = DoInsertWithImage(item, bitmap, pos);
189 if ( n != wxNOT_FOUND )
190 SetClientObject(n, clientData);
191
192 return n;
193 }
194
195 bool wxBitmapComboBox::OnAddBitmap(const wxBitmap& bitmap)
196 {
197 if ( bitmap.Ok() )
198 {
199 int width = bitmap.GetWidth();
200 int height = bitmap.GetHeight();
201
202 if ( m_usedImgSize.x <= 0 )
203 {
204 //
205 // If size not yet determined, get it from this image.
206 m_usedImgSize.x = width;
207 m_usedImgSize.y = height;
208
209 InvalidateBestSize();
210 wxSize newSz = GetBestSize();
211 wxSize sz = GetSize();
212 if ( newSz.y > sz.y )
213 SetSize(sz.x, newSz.y);
214 else
215 DetermineIndent();
216 }
217
218 wxCHECK_MSG(width == m_usedImgSize.x && height == m_usedImgSize.y,
219 false,
220 wxT("you can only add images of same size"));
221 }
222
223 return true;
224 }
225
226 bool wxBitmapComboBox::DoInsertBitmap(const wxBitmap& bitmap, unsigned int pos)
227 {
228 if ( !OnAddBitmap(bitmap) )
229 return false;
230
231 // NB: We must try to set the image before DoInsert or
232 // DoAppend because OnMeasureItem might be called
233 // before it returns.
234 m_bitmaps.Insert( new wxBitmap(bitmap), pos);
235
236 return true;
237 }
238
239 int wxBitmapComboBox::DoAppendWithImage(const wxString& item, const wxBitmap& image)
240 {
241 unsigned int pos = m_bitmaps.size();
242
243 if ( !DoInsertBitmap(image, pos) )
244 return wxNOT_FOUND;
245
246 int index = wxOwnerDrawnComboBox::DoAppend(item);
247
248 if ( index < 0 )
249 index = m_bitmaps.size();
250
251 // Need to re-check the index incase DoAppend sorted
252 if ( (unsigned int) index != pos )
253 {
254 wxBitmap* bmp = GetBitmapPtr(pos);
255 m_bitmaps.RemoveAt(pos);
256 m_bitmaps.Insert(bmp, index);
257 }
258
259 return index;
260 }
261
262 int wxBitmapComboBox::DoInsertWithImage(const wxString& item,
263 const wxBitmap& image,
264 unsigned int pos)
265 {
266 if ( !DoInsertBitmap(image, pos) )
267 return wxNOT_FOUND;
268
269 return wxOwnerDrawnComboBox::DoInsert(item, pos);
270 }
271
272 int wxBitmapComboBox::DoAppend(const wxString& item)
273 {
274 return DoAppendWithImage(item, wxNullBitmap);
275 }
276
277 int wxBitmapComboBox::DoInsert(const wxString& item, unsigned int pos)
278 {
279 return DoInsertWithImage(item, wxNullBitmap, pos);
280 }
281
282 void wxBitmapComboBox::Clear()
283 {
284 wxOwnerDrawnComboBox::Clear();
285
286 unsigned int i;
287
288 for ( i=0; i<m_bitmaps.size(); i++ )
289 delete GetBitmapPtr(i);
290
291 m_bitmaps.Empty();
292
293 m_usedImgSize.x = 0;
294 m_usedImgSize.y = 0;
295
296 DetermineIndent();
297 }
298
299 void wxBitmapComboBox::Delete(unsigned int n)
300 {
301 wxOwnerDrawnComboBox::Delete(n);
302 delete GetBitmapPtr(n);
303 m_bitmaps.RemoveAt(n);
304 }
305
306 // ----------------------------------------------------------------------------
307 // wxBitmapComboBox event handlers and such
308 // ----------------------------------------------------------------------------
309
310 void wxBitmapComboBox::DetermineIndent()
311 {
312 //
313 // Recalculate amount of empty space needed in front of
314 // text in control itself.
315 int indent = m_imgAreaWidth = 0;
316
317 if ( m_usedImgSize.x > 0 )
318 {
319 indent = m_usedImgSize.y + IMAGE_SPACING_LEFT + IMAGE_SPACING_RIGHT;
320 m_imgAreaWidth = indent;
321
322 indent -= 3;
323 }
324
325 SetCustomPaintWidth(indent);
326 }
327
328 void wxBitmapComboBox::OnResize(wxSizeEvent& event)
329 {
330 // Prevent infinite looping
331 if ( !m_inResize )
332 {
333 m_inResize = true;
334 DetermineIndent();
335 m_inResize = false;
336 }
337
338 event.Skip();
339 }
340
341 wxSize wxBitmapComboBox::DoGetBestSize() const
342 {
343 wxSize sz = wxOwnerDrawnComboBox::DoGetBestSize();
344
345 // Scale control to match height of highest image.
346 int h2 = m_usedImgSize.y + IMAGE_SPACING_CTRL_VERTICAL;
347
348 if ( h2 > sz.y )
349 sz.y = h2;
350
351 CacheBestSize(sz);
352 return sz;
353 }
354
355 // ----------------------------------------------------------------------------
356 // wxBitmapComboBox miscellaneous
357 // ----------------------------------------------------------------------------
358
359 bool wxBitmapComboBox::SetFont(const wxFont& font)
360 {
361 bool res = wxOwnerDrawnComboBox::SetFont(font);
362 m_fontHeight = GetCharHeight() + EXTRA_FONT_HEIGHT;
363 return res;
364 }
365
366 // ----------------------------------------------------------------------------
367 // wxBitmapComboBox item drawing and measuring
368 // ----------------------------------------------------------------------------
369
370 void wxBitmapComboBox::OnDrawBackground(wxDC& dc,
371 const wxRect& rect,
372 int item,
373 int flags) const
374 {
375 if ( GetCustomPaintWidth() == 0 ||
376 !(flags & wxODCB_PAINTING_SELECTED) ||
377 item < 0 )
378 {
379 wxOwnerDrawnComboBox::OnDrawBackground(dc, rect, item, flags);
380 return;
381 }
382
383 //
384 // Just paint simple selection background under where is text
385 // (ie. emulate what MSW image choice does).
386 //
387
388 int xPos = 0; // Starting x of selection rectangle
389 const int vSizeDec = 1; // Vertical size reduction of selection rectangle edges
390
391 xPos = GetCustomPaintWidth() + 2;
392
393 wxCoord x, y;
394 GetTextExtent(GetString(item), &x, &y, 0, 0);
395
396 dc.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT));
397
398 wxColour selCol = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT);
399 dc.SetPen(selCol);
400 dc.SetBrush(selCol);
401 dc.DrawRectangle(rect.x+xPos,
402 rect.y+vSizeDec,
403 x + 4,
404 rect.height-(vSizeDec*2));
405 }
406
407 void wxBitmapComboBox::OnDrawItem(wxDC& dc,
408 const wxRect& rect,
409 int item,
410 int flags) const
411 {
412 wxString text;
413 int imgAreaWidth = m_imgAreaWidth;
414 bool drawText;
415
416 if ( imgAreaWidth == 0 )
417 {
418 wxOwnerDrawnComboBox::OnDrawItem(dc, rect, item, flags);
419 return;
420 }
421
422 if ( flags & wxODCB_PAINTING_CONTROL )
423 {
424 text = GetValue();
425 if ( HasFlag(wxCB_READONLY) )
426 drawText = true;
427 else
428 drawText = false;
429 }
430 else
431 {
432 text = GetString(item);
433 drawText = true;
434 }
435
436 const wxBitmap& bmp = *GetBitmapPtr(item);
437 if ( bmp.Ok() )
438 {
439 wxCoord w = bmp.GetWidth();
440 wxCoord h = bmp.GetHeight();
441
442 // Draw the image centered
443 dc.DrawBitmap(bmp,
444 rect.x + (m_usedImgSize.x-w)/2 + IMAGE_SPACING_LEFT,
445 rect.y + (rect.height-h)/2,
446 true);
447 }
448
449 if ( drawText )
450 dc.DrawText(GetString(item),
451 rect.x + imgAreaWidth + 1,
452 rect.y + (rect.height-dc.GetCharHeight())/2);
453 }
454
455 wxCoord wxBitmapComboBox::OnMeasureItem(size_t WXUNUSED(item)) const
456 {
457 int imgHeightArea = m_usedImgSize.y + 2;
458 return imgHeightArea > m_fontHeight ? imgHeightArea : m_fontHeight;
459 }
460
461 wxCoord wxBitmapComboBox::OnMeasureItemWidth(size_t item) const
462 {
463 wxCoord x, y;
464 GetTextExtent(GetString(item), &x, &y, 0, 0);
465 x += m_imgAreaWidth;
466 return x;
467 }
468
469 #endif // defined(wxGENERIC_BITMAPCOMBOBOX)
470
471 #endif // wxUSE_BITMAPCOMBOBOX