]>
Commit | Line | Data |
---|---|---|
f696015c | 1 | ///////////////////////////////////////////////////////////////////////////// |
80fdcdb9 | 2 | // Name: src/msw/bmpcbox.cpp |
f696015c VZ |
3 | // Purpose: wxBitmapComboBox |
4 | // Author: Jaakko Salli | |
5 | // Created: 2008-04-06 | |
41fb3475 | 6 | // RCS-ID: $Id$ |
f696015c VZ |
7 | // Copyright: (c) 2008 Jaakko Salli |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | // ============================================================================ | |
12 | // declarations | |
13 | // ============================================================================ | |
14 | ||
15 | // ---------------------------------------------------------------------------- | |
16 | // headers | |
17 | // ---------------------------------------------------------------------------- | |
18 | ||
19 | #include "wx/wxprec.h" | |
20 | ||
21 | #ifdef __BORLANDC__ | |
22 | #pragma hdrstop | |
23 | #endif | |
24 | ||
25 | #if wxUSE_BITMAPCOMBOBOX | |
26 | ||
27 | #include "wx/bmpcbox.h" | |
28 | ||
29 | #ifndef WX_PRECOMP | |
30 | #include "wx/log.h" | |
31 | #endif | |
32 | ||
33 | #include "wx/settings.h" | |
34 | ||
35 | #include "wx/msw/dcclient.h" | |
36 | #include "wx/msw/private.h" | |
37 | ||
38 | // For wxODCB_XXX flags | |
39 | #include "wx/odcombo.h" | |
40 | ||
41 | ||
42 | #define IMAGE_SPACING_CTRL_VERTICAL 7 // Spacing used in control size calculation | |
43 | ||
44 | ||
45 | // ============================================================================ | |
46 | // implementation | |
47 | // ============================================================================ | |
48 | ||
49 | ||
50 | BEGIN_EVENT_TABLE(wxBitmapComboBox, wxComboBox) | |
51 | EVT_SIZE(wxBitmapComboBox::OnSize) | |
52 | END_EVENT_TABLE() | |
53 | ||
54 | ||
55 | IMPLEMENT_DYNAMIC_CLASS(wxBitmapComboBox, wxComboBox) | |
56 | ||
57 | ||
58 | // ---------------------------------------------------------------------------- | |
59 | // wxBitmapComboBox creation | |
60 | // ---------------------------------------------------------------------------- | |
61 | ||
62 | void wxBitmapComboBox::Init() | |
63 | { | |
64 | m_inResize = false; | |
65 | } | |
66 | ||
67 | wxBitmapComboBox::wxBitmapComboBox(wxWindow *parent, | |
68 | wxWindowID id, | |
69 | const wxString& value, | |
70 | const wxPoint& pos, | |
71 | const wxSize& size, | |
72 | const wxArrayString& choices, | |
73 | long style, | |
74 | const wxValidator& validator, | |
75 | const wxString& name) | |
76 | : wxComboBox(), | |
77 | wxBitmapComboBoxBase() | |
78 | { | |
79 | Init(); | |
80 | ||
81 | Create(parent,id,value,pos,size,choices,style,validator,name); | |
82 | } | |
83 | ||
84 | bool wxBitmapComboBox::Create(wxWindow *parent, | |
85 | wxWindowID id, | |
86 | const wxString& value, | |
87 | const wxPoint& pos, | |
88 | const wxSize& size, | |
89 | const wxArrayString& choices, | |
90 | long style, | |
91 | const wxValidator& validator, | |
92 | const wxString& name) | |
93 | { | |
94 | wxCArrayString chs(choices); | |
95 | return Create(parent, id, value, pos, size, chs.GetCount(), | |
96 | chs.GetStrings(), 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 | int n, | |
105 | const wxString choices[], | |
106 | long style, | |
107 | const wxValidator& validator, | |
108 | const wxString& name) | |
109 | { | |
110 | if ( !wxComboBox::Create(parent, id, value, pos, size, | |
111 | n, choices, style, validator, name) ) | |
112 | return false; | |
113 | ||
114 | UpdateInternals(); | |
115 | ||
116 | return true; | |
117 | } | |
118 | ||
119 | WXDWORD wxBitmapComboBox::MSWGetStyle(long style, WXDWORD *exstyle) const | |
120 | { | |
121 | return wxComboBox::MSWGetStyle(style, exstyle) | CBS_OWNERDRAWFIXED | CBS_HASSTRINGS; | |
122 | } | |
123 | ||
124 | void wxBitmapComboBox::RecreateControl() | |
125 | { | |
126 | // | |
127 | // Recreate control so that WM_MEASUREITEM gets called again. | |
128 | // Can't use CBS_OWNERDRAWVARIABLE because it has odd | |
129 | // mouse-wheel behaviour. | |
130 | // | |
131 | wxString value = GetValue(); | |
132 | wxPoint pos = GetPosition(); | |
133 | wxSize size = GetSize(); | |
a9afb057 | 134 | size.y = GetBestSize().y; |
f696015c VZ |
135 | wxArrayString strings = GetStrings(); |
136 | ||
137 | wxComboBox::DoClear(); | |
138 | ||
139 | HWND hwnd = GetHwnd(); | |
140 | DissociateHandle(); | |
141 | ::DestroyWindow(hwnd); | |
142 | ||
4f320d7c | 143 | if ( !MSWCreateControl(wxT("COMBOBOX"), wxEmptyString, pos, size) ) |
f696015c VZ |
144 | return; |
145 | ||
146 | // initialize the controls contents | |
147 | for ( unsigned int i = 0; i < strings.size(); i++ ) | |
148 | { | |
149 | wxComboBox::Append(strings[i]); | |
150 | } | |
151 | ||
152 | // and make sure it has the same attributes as before | |
153 | if ( m_hasFont ) | |
154 | { | |
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 | |
157 | // that it did | |
158 | wxFont font = m_font; | |
159 | m_font = wxNullFont; | |
160 | SetFont(font); | |
161 | } | |
162 | ||
163 | if ( m_hasFgCol ) | |
164 | { | |
165 | wxColour colFg = m_foregroundColour; | |
166 | m_foregroundColour = wxNullColour; | |
167 | SetForegroundColour(colFg); | |
168 | } | |
169 | ||
170 | if ( m_hasBgCol ) | |
171 | { | |
172 | wxColour colBg = m_backgroundColour; | |
173 | m_backgroundColour = wxNullColour; | |
174 | SetBackgroundColour(colBg); | |
175 | } | |
176 | else | |
177 | { | |
178 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); | |
179 | } | |
a9afb057 JS |
180 | |
181 | ::SendMessage(GetHwnd(), CB_SETITEMHEIGHT, 0, MeasureItem(0)); | |
4f320d7c JS |
182 | |
183 | // Revert the old string value | |
df541b86 JS |
184 | if ( !HasFlag(wxCB_READONLY) ) |
185 | ChangeValue(value); | |
f696015c VZ |
186 | } |
187 | ||
188 | wxBitmapComboBox::~wxBitmapComboBox() | |
189 | { | |
190 | Clear(); | |
191 | } | |
192 | ||
41ce9ae1 JS |
193 | wxSize wxBitmapComboBox::DoGetBestSize() const |
194 | { | |
195 | wxSize best = wxComboBox::DoGetBestSize(); | |
196 | wxSize bitmapSize = GetBitmapSize(); | |
197 | ||
198 | wxCoord useHeightBitmap = EDIT_HEIGHT_FROM_CHAR_HEIGHT(bitmapSize.y); | |
199 | if ( best.y < useHeightBitmap ) | |
200 | { | |
201 | best.y = useHeightBitmap; | |
202 | CacheBestSize(best); | |
203 | } | |
204 | return best; | |
205 | } | |
206 | ||
f696015c VZ |
207 | // ---------------------------------------------------------------------------- |
208 | // Item manipulation | |
209 | // ---------------------------------------------------------------------------- | |
210 | ||
211 | void wxBitmapComboBox::SetItemBitmap(unsigned int n, const wxBitmap& bitmap) | |
212 | { | |
213 | OnAddBitmap(bitmap); | |
214 | DoSetItemBitmap(n, bitmap); | |
215 | ||
216 | if ( (int)n == GetSelection() ) | |
217 | Refresh(); | |
218 | } | |
219 | ||
220 | int wxBitmapComboBox::Append(const wxString& item, const wxBitmap& bitmap) | |
221 | { | |
222 | OnAddBitmap(bitmap); | |
223 | const int n = wxComboBox::Append(item); | |
224 | if ( n != wxNOT_FOUND ) | |
225 | DoSetItemBitmap(n, bitmap); | |
226 | return n; | |
227 | } | |
228 | ||
229 | int wxBitmapComboBox::Append(const wxString& item, const wxBitmap& bitmap, | |
230 | void *clientData) | |
231 | { | |
232 | OnAddBitmap(bitmap); | |
233 | const int n = wxComboBox::Append(item, clientData); | |
234 | if ( n != wxNOT_FOUND ) | |
235 | DoSetItemBitmap(n, bitmap); | |
236 | return n; | |
237 | } | |
238 | ||
239 | int wxBitmapComboBox::Append(const wxString& item, const wxBitmap& bitmap, | |
240 | wxClientData *clientData) | |
241 | { | |
242 | OnAddBitmap(bitmap); | |
243 | const int n = wxComboBox::Append(item, clientData); | |
244 | if ( n != wxNOT_FOUND ) | |
245 | DoSetItemBitmap(n, bitmap); | |
246 | return n; | |
247 | } | |
248 | ||
249 | int wxBitmapComboBox::Insert(const wxString& item, | |
250 | const wxBitmap& bitmap, | |
251 | unsigned int pos) | |
252 | { | |
253 | OnAddBitmap(bitmap); | |
254 | const int n = wxComboBox::Insert(item, pos); | |
255 | if ( n != wxNOT_FOUND ) | |
256 | DoSetItemBitmap(n, bitmap); | |
257 | return n; | |
258 | } | |
259 | ||
05f5b3f7 VZ |
260 | int wxBitmapComboBox::Insert(const wxString& item, const wxBitmap& bitmap, |
261 | unsigned int pos, void *clientData) | |
262 | { | |
263 | OnAddBitmap(bitmap); | |
264 | const int n = wxComboBox::Insert(item, pos, clientData); | |
265 | if ( n != wxNOT_FOUND ) | |
266 | DoSetItemBitmap(n, bitmap); | |
267 | return n; | |
268 | } | |
269 | ||
f696015c VZ |
270 | int wxBitmapComboBox::Insert(const wxString& item, const wxBitmap& bitmap, |
271 | unsigned int pos, wxClientData *clientData) | |
272 | { | |
273 | OnAddBitmap(bitmap); | |
274 | const int n = wxComboBox::Insert(item, pos, clientData); | |
275 | if ( n != wxNOT_FOUND ) | |
276 | DoSetItemBitmap(n, bitmap); | |
277 | return n; | |
278 | } | |
279 | ||
280 | int wxBitmapComboBox::DoInsertItems(const wxArrayStringsAdapter & items, | |
281 | unsigned int pos, | |
282 | void **clientData, wxClientDataType type) | |
283 | { | |
284 | const unsigned int numItems = items.GetCount(); | |
285 | const unsigned int countNew = GetCount() + numItems; | |
286 | ||
befee9b7 JS |
287 | wxASSERT( numItems == 1 || !HasFlag(wxCB_SORT) ); // Sanity check |
288 | ||
f696015c VZ |
289 | m_bitmaps.Alloc(countNew); |
290 | ||
291 | for ( unsigned int i = 0; i < numItems; i++ ) | |
292 | { | |
293 | m_bitmaps.Insert(new wxBitmap(wxNullBitmap), pos + i); | |
294 | } | |
295 | ||
296 | const int index = wxComboBox::DoInsertItems(items, pos, | |
297 | clientData, type); | |
298 | ||
299 | if ( index == wxNOT_FOUND ) | |
300 | { | |
301 | for ( int i = numItems-1; i >= 0; i-- ) | |
302 | BCBDoDeleteOneItem(pos + i); | |
303 | } | |
befee9b7 JS |
304 | else if ( ((unsigned int)index) != pos ) |
305 | { | |
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); | |
311 | } | |
f696015c VZ |
312 | |
313 | return index; | |
314 | } | |
315 | ||
316 | bool wxBitmapComboBox::OnAddBitmap(const wxBitmap& bitmap) | |
317 | { | |
318 | if ( wxBitmapComboBoxBase::OnAddBitmap(bitmap) ) | |
319 | { | |
320 | // Need to recreate control for a new measureitem call? | |
321 | int prevItemHeight = ::SendMessage(GetHwnd(), CB_GETITEMHEIGHT, 0, 0); | |
322 | ||
323 | if ( prevItemHeight != MeasureItem(0) ) | |
324 | RecreateControl(); | |
325 | ||
326 | return true; | |
327 | } | |
03647350 | 328 | |
f696015c VZ |
329 | return false; |
330 | } | |
331 | ||
332 | void wxBitmapComboBox::DoClear() | |
333 | { | |
334 | wxComboBox::DoClear(); | |
335 | wxBitmapComboBoxBase::BCBDoClear(); | |
336 | } | |
337 | ||
338 | void wxBitmapComboBox::DoDeleteOneItem(unsigned int n) | |
339 | { | |
340 | wxComboBox::DoDeleteOneItem(n); | |
341 | wxBitmapComboBoxBase::BCBDoDeleteOneItem(n); | |
342 | } | |
343 | ||
344 | // ---------------------------------------------------------------------------- | |
345 | // wxBitmapComboBox event handlers and such | |
346 | // ---------------------------------------------------------------------------- | |
347 | ||
348 | void wxBitmapComboBox::OnSize(wxSizeEvent& event) | |
349 | { | |
350 | // Prevent infinite looping | |
351 | if ( !m_inResize ) | |
352 | { | |
353 | m_inResize = true; | |
354 | DetermineIndent(); | |
355 | m_inResize = false; | |
356 | } | |
357 | ||
358 | event.Skip(); | |
359 | } | |
360 | ||
361 | // ---------------------------------------------------------------------------- | |
362 | // wxBitmapComboBox miscellaneous | |
363 | // ---------------------------------------------------------------------------- | |
364 | ||
365 | bool wxBitmapComboBox::SetFont(const wxFont& font) | |
366 | { | |
367 | bool res = wxComboBox::SetFont(font); | |
368 | UpdateInternals(); | |
369 | return res; | |
370 | } | |
371 | ||
372 | // ---------------------------------------------------------------------------- | |
373 | // wxBitmapComboBox item drawing and measuring | |
374 | // ---------------------------------------------------------------------------- | |
375 | ||
376 | bool wxBitmapComboBox::MSWOnDraw(WXDRAWITEMSTRUCT *item) | |
377 | { | |
378 | LPDRAWITEMSTRUCT lpDrawItem = (LPDRAWITEMSTRUCT) item; | |
379 | int pos = lpDrawItem->itemID; | |
380 | ||
381 | // Draw default for item -1, which means 'focus rect only' | |
382 | if ( pos == -1 ) | |
383 | return FALSE; | |
384 | ||
03647350 | 385 | int flags = 0; |
f696015c VZ |
386 | if ( lpDrawItem->itemState & ODS_COMBOBOXEDIT ) |
387 | flags |= wxODCB_PAINTING_CONTROL; | |
388 | if ( lpDrawItem->itemState & ODS_SELECTED ) | |
389 | flags |= wxODCB_PAINTING_SELECTED; | |
390 | ||
391 | wxString text; | |
392 | ||
393 | if ( flags & wxODCB_PAINTING_CONTROL ) | |
394 | { | |
395 | text = GetValue(); | |
396 | if ( !HasFlag(wxCB_READONLY) ) | |
397 | text.clear(); | |
398 | } | |
399 | else | |
400 | { | |
401 | text = GetString(pos); | |
402 | } | |
403 | ||
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); | |
408 | ||
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); | |
414 | ||
415 | return TRUE; | |
416 | } | |
417 | ||
418 | bool wxBitmapComboBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item) | |
419 | { | |
420 | LPMEASUREITEMSTRUCT lpMeasureItem = (LPMEASUREITEMSTRUCT) item; | |
421 | int pos = lpMeasureItem->itemID; | |
422 | ||
423 | lpMeasureItem->itemHeight = wxBitmapComboBoxBase::MeasureItem(pos); | |
424 | ||
425 | return TRUE; | |
426 | } | |
427 | ||
428 | #endif // wxUSE_BITMAPCOMBOBOX |