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