]>
Commit | Line | Data |
---|---|---|
f696015c VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: src/msw/bmpcboxg.cpp | |
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(); | |
134 | wxArrayString strings = GetStrings(); | |
135 | ||
136 | wxComboBox::DoClear(); | |
137 | ||
138 | HWND hwnd = GetHwnd(); | |
139 | DissociateHandle(); | |
140 | ::DestroyWindow(hwnd); | |
141 | ||
142 | if ( !MSWCreateControl(wxT("COMBOBOX"), value, pos, size) ) | |
143 | return; | |
144 | ||
145 | // initialize the controls contents | |
146 | for ( unsigned int i = 0; i < strings.size(); i++ ) | |
147 | { | |
148 | wxComboBox::Append(strings[i]); | |
149 | } | |
150 | ||
151 | // and make sure it has the same attributes as before | |
152 | if ( m_hasFont ) | |
153 | { | |
154 | // calling SetFont(m_font) would do nothing as the code would | |
155 | // notice that the font didn't change, so force it to believe | |
156 | // that it did | |
157 | wxFont font = m_font; | |
158 | m_font = wxNullFont; | |
159 | SetFont(font); | |
160 | } | |
161 | ||
162 | if ( m_hasFgCol ) | |
163 | { | |
164 | wxColour colFg = m_foregroundColour; | |
165 | m_foregroundColour = wxNullColour; | |
166 | SetForegroundColour(colFg); | |
167 | } | |
168 | ||
169 | if ( m_hasBgCol ) | |
170 | { | |
171 | wxColour colBg = m_backgroundColour; | |
172 | m_backgroundColour = wxNullColour; | |
173 | SetBackgroundColour(colBg); | |
174 | } | |
175 | else | |
176 | { | |
177 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); | |
178 | } | |
179 | } | |
180 | ||
181 | wxBitmapComboBox::~wxBitmapComboBox() | |
182 | { | |
183 | Clear(); | |
184 | } | |
185 | ||
41ce9ae1 JS |
186 | wxSize wxBitmapComboBox::DoGetBestSize() const |
187 | { | |
188 | wxSize best = wxComboBox::DoGetBestSize(); | |
189 | wxSize bitmapSize = GetBitmapSize(); | |
190 | ||
191 | wxCoord useHeightBitmap = EDIT_HEIGHT_FROM_CHAR_HEIGHT(bitmapSize.y); | |
192 | if ( best.y < useHeightBitmap ) | |
193 | { | |
194 | best.y = useHeightBitmap; | |
195 | CacheBestSize(best); | |
196 | } | |
197 | return best; | |
198 | } | |
199 | ||
f696015c VZ |
200 | // ---------------------------------------------------------------------------- |
201 | // Item manipulation | |
202 | // ---------------------------------------------------------------------------- | |
203 | ||
204 | void wxBitmapComboBox::SetItemBitmap(unsigned int n, const wxBitmap& bitmap) | |
205 | { | |
206 | OnAddBitmap(bitmap); | |
207 | DoSetItemBitmap(n, bitmap); | |
208 | ||
209 | if ( (int)n == GetSelection() ) | |
210 | Refresh(); | |
211 | } | |
212 | ||
213 | int wxBitmapComboBox::Append(const wxString& item, const wxBitmap& bitmap) | |
214 | { | |
215 | OnAddBitmap(bitmap); | |
216 | const int n = wxComboBox::Append(item); | |
217 | if ( n != wxNOT_FOUND ) | |
218 | DoSetItemBitmap(n, bitmap); | |
219 | return n; | |
220 | } | |
221 | ||
222 | int wxBitmapComboBox::Append(const wxString& item, const wxBitmap& bitmap, | |
223 | void *clientData) | |
224 | { | |
225 | OnAddBitmap(bitmap); | |
226 | const int n = wxComboBox::Append(item, clientData); | |
227 | if ( n != wxNOT_FOUND ) | |
228 | DoSetItemBitmap(n, bitmap); | |
229 | return n; | |
230 | } | |
231 | ||
232 | int wxBitmapComboBox::Append(const wxString& item, const wxBitmap& bitmap, | |
233 | wxClientData *clientData) | |
234 | { | |
235 | OnAddBitmap(bitmap); | |
236 | const int n = wxComboBox::Append(item, clientData); | |
237 | if ( n != wxNOT_FOUND ) | |
238 | DoSetItemBitmap(n, bitmap); | |
239 | return n; | |
240 | } | |
241 | ||
242 | int wxBitmapComboBox::Insert(const wxString& item, | |
243 | const wxBitmap& bitmap, | |
244 | unsigned int pos) | |
245 | { | |
246 | OnAddBitmap(bitmap); | |
247 | const int n = wxComboBox::Insert(item, pos); | |
248 | if ( n != wxNOT_FOUND ) | |
249 | DoSetItemBitmap(n, bitmap); | |
250 | return n; | |
251 | } | |
252 | ||
253 | int wxBitmapComboBox::Insert(const wxString& item, const wxBitmap& bitmap, | |
254 | unsigned int pos, wxClientData *clientData) | |
255 | { | |
256 | OnAddBitmap(bitmap); | |
257 | const int n = wxComboBox::Insert(item, pos, clientData); | |
258 | if ( n != wxNOT_FOUND ) | |
259 | DoSetItemBitmap(n, bitmap); | |
260 | return n; | |
261 | } | |
262 | ||
263 | int wxBitmapComboBox::DoInsertItems(const wxArrayStringsAdapter & items, | |
264 | unsigned int pos, | |
265 | void **clientData, wxClientDataType type) | |
266 | { | |
267 | const unsigned int numItems = items.GetCount(); | |
268 | const unsigned int countNew = GetCount() + numItems; | |
269 | ||
befee9b7 JS |
270 | wxASSERT( numItems == 1 || !HasFlag(wxCB_SORT) ); // Sanity check |
271 | ||
f696015c VZ |
272 | m_bitmaps.Alloc(countNew); |
273 | ||
274 | for ( unsigned int i = 0; i < numItems; i++ ) | |
275 | { | |
276 | m_bitmaps.Insert(new wxBitmap(wxNullBitmap), pos + i); | |
277 | } | |
278 | ||
279 | const int index = wxComboBox::DoInsertItems(items, pos, | |
280 | clientData, type); | |
281 | ||
282 | if ( index == wxNOT_FOUND ) | |
283 | { | |
284 | for ( int i = numItems-1; i >= 0; i-- ) | |
285 | BCBDoDeleteOneItem(pos + i); | |
286 | } | |
befee9b7 JS |
287 | else if ( ((unsigned int)index) != pos ) |
288 | { | |
289 | // Move pre-inserted empty bitmap into correct position | |
290 | // (usually happens when combo box has wxCB_SORT style) | |
291 | wxBitmap* bmp = static_cast<wxBitmap*>(m_bitmaps[pos]); | |
292 | m_bitmaps.RemoveAt(pos); | |
293 | m_bitmaps.Insert(bmp, index); | |
294 | } | |
f696015c VZ |
295 | |
296 | return index; | |
297 | } | |
298 | ||
299 | bool wxBitmapComboBox::OnAddBitmap(const wxBitmap& bitmap) | |
300 | { | |
301 | if ( wxBitmapComboBoxBase::OnAddBitmap(bitmap) ) | |
302 | { | |
303 | // Need to recreate control for a new measureitem call? | |
304 | int prevItemHeight = ::SendMessage(GetHwnd(), CB_GETITEMHEIGHT, 0, 0); | |
305 | ||
306 | if ( prevItemHeight != MeasureItem(0) ) | |
307 | RecreateControl(); | |
308 | ||
309 | return true; | |
310 | } | |
03647350 | 311 | |
f696015c VZ |
312 | return false; |
313 | } | |
314 | ||
315 | void wxBitmapComboBox::DoClear() | |
316 | { | |
317 | wxComboBox::DoClear(); | |
318 | wxBitmapComboBoxBase::BCBDoClear(); | |
319 | } | |
320 | ||
321 | void wxBitmapComboBox::DoDeleteOneItem(unsigned int n) | |
322 | { | |
323 | wxComboBox::DoDeleteOneItem(n); | |
324 | wxBitmapComboBoxBase::BCBDoDeleteOneItem(n); | |
325 | } | |
326 | ||
327 | // ---------------------------------------------------------------------------- | |
328 | // wxBitmapComboBox event handlers and such | |
329 | // ---------------------------------------------------------------------------- | |
330 | ||
331 | void wxBitmapComboBox::OnSize(wxSizeEvent& event) | |
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 | // ---------------------------------------------------------------------------- | |
345 | // wxBitmapComboBox miscellaneous | |
346 | // ---------------------------------------------------------------------------- | |
347 | ||
348 | bool wxBitmapComboBox::SetFont(const wxFont& font) | |
349 | { | |
350 | bool res = wxComboBox::SetFont(font); | |
351 | UpdateInternals(); | |
352 | return res; | |
353 | } | |
354 | ||
355 | // ---------------------------------------------------------------------------- | |
356 | // wxBitmapComboBox item drawing and measuring | |
357 | // ---------------------------------------------------------------------------- | |
358 | ||
359 | bool wxBitmapComboBox::MSWOnDraw(WXDRAWITEMSTRUCT *item) | |
360 | { | |
361 | LPDRAWITEMSTRUCT lpDrawItem = (LPDRAWITEMSTRUCT) item; | |
362 | int pos = lpDrawItem->itemID; | |
363 | ||
364 | // Draw default for item -1, which means 'focus rect only' | |
365 | if ( pos == -1 ) | |
366 | return FALSE; | |
367 | ||
03647350 | 368 | int flags = 0; |
f696015c VZ |
369 | if ( lpDrawItem->itemState & ODS_COMBOBOXEDIT ) |
370 | flags |= wxODCB_PAINTING_CONTROL; | |
371 | if ( lpDrawItem->itemState & ODS_SELECTED ) | |
372 | flags |= wxODCB_PAINTING_SELECTED; | |
373 | ||
374 | wxString text; | |
375 | ||
376 | if ( flags & wxODCB_PAINTING_CONTROL ) | |
377 | { | |
378 | text = GetValue(); | |
379 | if ( !HasFlag(wxCB_READONLY) ) | |
380 | text.clear(); | |
381 | } | |
382 | else | |
383 | { | |
384 | text = GetString(pos); | |
385 | } | |
386 | ||
387 | wxPaintDCEx dc(this, lpDrawItem->hDC); | |
388 | wxRect rect = wxRectFromRECT(lpDrawItem->rcItem); | |
389 | wxBitmapComboBoxBase::DrawBackground(dc, rect, pos, flags); | |
390 | wxBitmapComboBoxBase::DrawItem(dc, rect, pos, text, flags); | |
391 | ||
392 | // If the item has the focus, draw focus rectangle. | |
393 | // Commented out since regular combo box doesn't | |
394 | // seem to do it either. | |
395 | //if ( lpDrawItem->itemState & ODS_FOCUS ) | |
396 | // DrawFocusRect(lpDrawItem->hDC, &lpDrawItem->rcItem); | |
397 | ||
398 | return TRUE; | |
399 | } | |
400 | ||
401 | bool wxBitmapComboBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item) | |
402 | { | |
403 | LPMEASUREITEMSTRUCT lpMeasureItem = (LPMEASUREITEMSTRUCT) item; | |
404 | int pos = lpMeasureItem->itemID; | |
405 | ||
406 | lpMeasureItem->itemHeight = wxBitmapComboBoxBase::MeasureItem(pos); | |
407 | ||
408 | return TRUE; | |
409 | } | |
410 | ||
411 | #endif // wxUSE_BITMAPCOMBOBOX |