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