/[pcsx2_0.9.7]/trunk/3rdparty/wxWidgets/src/generic/bmpcboxg.cpp
ViewVC logotype

Contents of /trunk/3rdparty/wxWidgets/src/generic/bmpcboxg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 31 - (show annotations) (download)
Tue Sep 7 03:24:11 2010 UTC (10 years, 1 month ago) by william
File size: 13059 byte(s)
committing r3113 initial commit again...
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: bmpcboxg.cpp 44665 2007-03-07 23:29:03Z VZ $
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 #include "wx/dc.h"
39
40 #if wxUSE_IMAGE
41 #include "wx/image.h"
42 #endif
43
44
45 const 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
68 BEGIN_EVENT_TABLE(wxBitmapComboBox, wxOwnerDrawnComboBox)
69 EVT_SIZE(wxBitmapComboBox::OnSize)
70 END_EVENT_TABLE()
71
72
73 IMPLEMENT_DYNAMIC_CLASS(wxBitmapComboBox, wxOwnerDrawnComboBox)
74
75 void wxBitmapComboBox::Init()
76 {
77 m_fontHeight = 0;
78 m_imgAreaWidth = 0;
79 m_inResize = false;
80 }
81
82 wxBitmapComboBox::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
99 bool 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
122 bool 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
146 void wxBitmapComboBox::PostCreate()
147 {
148 m_fontHeight = GetCharHeight() + EXTRA_FONT_HEIGHT;
149
150 while ( m_bitmaps.GetCount() < GetCount() )
151 m_bitmaps.Add( new wxBitmap() );
152 }
153
154 wxBitmapComboBox::~wxBitmapComboBox()
155 {
156 Clear();
157 }
158
159 // ----------------------------------------------------------------------------
160 // Item manipulation
161 // ----------------------------------------------------------------------------
162
163 void wxBitmapComboBox::SetItemBitmap(unsigned int n, const wxBitmap& bitmap)
164 {
165 wxCHECK_RET( n < GetCount(), wxT("invalid item index") );
166 OnAddBitmap(bitmap);
167 *GetBitmapPtr(n) = bitmap;
168
169 if ( (int)n == GetSelection() )
170 Refresh();
171 }
172
173 wxBitmap wxBitmapComboBox::GetItemBitmap(unsigned int n) const
174 {
175 wxCHECK_MSG( n < GetCount(), wxNullBitmap, wxT("invalid item index") );
176 return *GetBitmapPtr(n);
177 }
178
179 int wxBitmapComboBox::Insert(const wxString& item, const wxBitmap& bitmap,
180 unsigned int pos, void *clientData)
181 {
182 int n = DoInsertWithImage(item, bitmap, pos);
183 if ( n != wxNOT_FOUND )
184 SetClientData(n, clientData);
185
186 return n;
187 }
188
189 int wxBitmapComboBox::Insert(const wxString& item, const wxBitmap& bitmap,
190 unsigned int pos, wxClientData *clientData)
191 {
192 int n = DoInsertWithImage(item, bitmap, pos);
193 if ( n != wxNOT_FOUND )
194 SetClientObject(n, clientData);
195
196 return n;
197 }
198
199 bool wxBitmapComboBox::OnAddBitmap(const wxBitmap& bitmap)
200 {
201 if ( bitmap.Ok() )
202 {
203 int width = bitmap.GetWidth();
204 int height = bitmap.GetHeight();
205
206 if ( m_usedImgSize.x <= 0 )
207 {
208 //
209 // If size not yet determined, get it from this image.
210 m_usedImgSize.x = width;
211 m_usedImgSize.y = height;
212
213 InvalidateBestSize();
214 wxSize newSz = GetBestSize();
215 wxSize sz = GetSize();
216 if ( newSz.y > sz.y )
217 SetSize(sz.x, newSz.y);
218 else
219 DetermineIndent();
220 }
221
222 wxCHECK_MSG(width == m_usedImgSize.x && height == m_usedImgSize.y,
223 false,
224 wxT("you can only add images of same size"));
225 }
226
227 return true;
228 }
229
230 bool wxBitmapComboBox::DoInsertBitmap(const wxBitmap& bitmap, unsigned int pos)
231 {
232 if ( !OnAddBitmap(bitmap) )
233 return false;
234
235 // NB: We must try to set the image before DoInsert or
236 // DoAppend because OnMeasureItem might be called
237 // before it returns.
238 m_bitmaps.Insert( new wxBitmap(bitmap), pos);
239
240 return true;
241 }
242
243 int wxBitmapComboBox::DoAppendWithImage(const wxString& item, const wxBitmap& image)
244 {
245 unsigned int pos = m_bitmaps.size();
246
247 if ( !DoInsertBitmap(image, pos) )
248 return wxNOT_FOUND;
249
250 int index = wxOwnerDrawnComboBox::DoAppend(item);
251
252 if ( index < 0 )
253 index = m_bitmaps.size();
254
255 // Need to re-check the index incase DoAppend sorted
256 if ( (unsigned int) index != pos )
257 {
258 wxBitmap* bmp = GetBitmapPtr(pos);
259 m_bitmaps.RemoveAt(pos);
260 m_bitmaps.Insert(bmp, index);
261 }
262
263 return index;
264 }
265
266 int wxBitmapComboBox::DoInsertWithImage(const wxString& item,
267 const wxBitmap& image,
268 unsigned int pos)
269 {
270 wxCHECK_MSG( IsValidInsert(pos), wxNOT_FOUND, wxT("invalid item index") );
271
272 if ( !DoInsertBitmap(image, pos) )
273 return wxNOT_FOUND;
274
275 return wxOwnerDrawnComboBox::DoInsert(item, pos);
276 }
277
278 int wxBitmapComboBox::DoAppend(const wxString& item)
279 {
280 return DoAppendWithImage(item, wxNullBitmap);
281 }
282
283 int wxBitmapComboBox::DoInsert(const wxString& item, unsigned int pos)
284 {
285 return DoInsertWithImage(item, wxNullBitmap, pos);
286 }
287
288 void wxBitmapComboBox::Clear()
289 {
290 wxOwnerDrawnComboBox::Clear();
291
292 unsigned int i;
293
294 for ( i=0; i<m_bitmaps.size(); i++ )
295 delete GetBitmapPtr(i);
296
297 m_bitmaps.Empty();
298
299 m_usedImgSize.x = 0;
300 m_usedImgSize.y = 0;
301
302 DetermineIndent();
303 }
304
305 void wxBitmapComboBox::Delete(unsigned int n)
306 {
307 wxOwnerDrawnComboBox::Delete(n);
308 delete GetBitmapPtr(n);
309 m_bitmaps.RemoveAt(n);
310 }
311
312 // ----------------------------------------------------------------------------
313 // wxBitmapComboBox event handlers and such
314 // ----------------------------------------------------------------------------
315
316 void wxBitmapComboBox::DetermineIndent()
317 {
318 //
319 // Recalculate amount of empty space needed in front of
320 // text in control itself.
321 int indent = m_imgAreaWidth = 0;
322
323 if ( m_usedImgSize.x > 0 )
324 {
325 indent = m_usedImgSize.x + IMAGE_SPACING_LEFT + IMAGE_SPACING_RIGHT;
326 m_imgAreaWidth = indent;
327
328 indent -= 3;
329 }
330
331 SetCustomPaintWidth(indent);
332 }
333
334 void wxBitmapComboBox::OnSize(wxSizeEvent& event)
335 {
336 // Prevent infinite looping
337 if ( !m_inResize )
338 {
339 m_inResize = true;
340 DetermineIndent();
341 m_inResize = false;
342 }
343
344 event.Skip();
345 }
346
347 wxSize wxBitmapComboBox::DoGetBestSize() const
348 {
349 wxSize sz = wxOwnerDrawnComboBox::DoGetBestSize();
350
351 // Scale control to match height of highest image.
352 int h2 = m_usedImgSize.y + IMAGE_SPACING_CTRL_VERTICAL;
353
354 if ( h2 > sz.y )
355 sz.y = h2;
356
357 CacheBestSize(sz);
358 return sz;
359 }
360
361 // ----------------------------------------------------------------------------
362 // wxBitmapComboBox miscellaneous
363 // ----------------------------------------------------------------------------
364
365 bool wxBitmapComboBox::SetFont(const wxFont& font)
366 {
367 bool res = wxOwnerDrawnComboBox::SetFont(font);
368 m_fontHeight = GetCharHeight() + EXTRA_FONT_HEIGHT;
369 return res;
370 }
371
372 // ----------------------------------------------------------------------------
373 // wxBitmapComboBox item drawing and measuring
374 // ----------------------------------------------------------------------------
375
376 void wxBitmapComboBox::OnDrawBackground(wxDC& dc,
377 const wxRect& rect,
378 int item,
379 int flags) const
380 {
381 if ( GetCustomPaintWidth() == 0 ||
382 !(flags & wxODCB_PAINTING_SELECTED) ||
383 item < 0 )
384 {
385 wxOwnerDrawnComboBox::OnDrawBackground(dc, rect, item, flags);
386 return;
387 }
388
389 //
390 // Just paint simple selection background under where is text
391 // (ie. emulate what MSW image choice does).
392 //
393
394 int xPos = 0; // Starting x of selection rectangle
395 const int vSizeDec = 1; // Vertical size reduction of selection rectangle edges
396
397 xPos = GetCustomPaintWidth() + 2;
398
399 wxCoord x, y;
400 GetTextExtent(GetString(item), &x, &y, 0, 0);
401
402 dc.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT));
403
404 wxColour selCol = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT);
405 dc.SetPen(selCol);
406 dc.SetBrush(selCol);
407 dc.DrawRectangle(rect.x+xPos,
408 rect.y+vSizeDec,
409 x + 4,
410 rect.height-(vSizeDec*2));
411 }
412
413 void wxBitmapComboBox::OnDrawItem(wxDC& dc,
414 const wxRect& rect,
415 int item,
416 int flags) const
417 {
418 wxString text;
419 int imgAreaWidth = m_imgAreaWidth;
420 bool drawText;
421
422 if ( imgAreaWidth == 0 )
423 {
424 wxOwnerDrawnComboBox::OnDrawItem(dc, rect, item, flags);
425 return;
426 }
427
428 if ( flags & wxODCB_PAINTING_CONTROL )
429 {
430 text = GetValue();
431 if ( HasFlag(wxCB_READONLY) )
432 drawText = true;
433 else
434 drawText = false;
435 }
436 else
437 {
438 text = GetString(item);
439 drawText = true;
440 }
441
442 const wxBitmap& bmp = *GetBitmapPtr(item);
443 if ( bmp.Ok() )
444 {
445 wxCoord w = bmp.GetWidth();
446 wxCoord h = bmp.GetHeight();
447
448 // Draw the image centered
449 dc.DrawBitmap(bmp,
450 rect.x + (m_usedImgSize.x-w)/2 + IMAGE_SPACING_LEFT,
451 rect.y + (rect.height-h)/2,
452 true);
453 }
454
455 if ( drawText )
456 dc.DrawText(GetString(item),
457 rect.x + imgAreaWidth + 1,
458 rect.y + (rect.height-dc.GetCharHeight())/2);
459 }
460
461 wxCoord wxBitmapComboBox::OnMeasureItem(size_t WXUNUSED(item)) const
462 {
463 int imgHeightArea = m_usedImgSize.y + 2;
464 return imgHeightArea > m_fontHeight ? imgHeightArea : m_fontHeight;
465 }
466
467 wxCoord wxBitmapComboBox::OnMeasureItemWidth(size_t item) const
468 {
469 wxCoord x, y;
470 GetTextExtent(GetString(item), &x, &y, 0, 0);
471 x += m_imgAreaWidth;
472 return x;
473 }
474
475 #endif // defined(wxGENERIC_BITMAPCOMBOBOX)
476
477 #endif // wxUSE_BITMAPCOMBOBOX

  ViewVC Help
Powered by ViewVC 1.1.22