]>
Commit | Line | Data |
---|---|---|
a340b80d | 1 | ///////////////////////////////////////////////////////////////////////////// |
85fed18c | 2 | // Name: src/generic/odcombo.cpp |
a340b80d VZ |
3 | // Purpose: wxOwnerDrawnComboBox, wxVListBoxComboPopup |
4 | // Author: Jaakko Salli | |
5 | // Modified by: | |
6 | // Created: Apr-30-2006 | |
7 | // RCS-ID: $Id$ | |
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 | ||
a57d600f | 26 | #if wxUSE_ODCOMBOBOX |
a340b80d | 27 | |
85fed18c WS |
28 | #include "wx/odcombo.h" |
29 | ||
a340b80d VZ |
30 | #ifndef WX_PRECOMP |
31 | #include "wx/log.h" | |
32 | #include "wx/combobox.h" | |
33 | #include "wx/dcclient.h" | |
34 | #include "wx/settings.h" | |
35 | #include "wx/dialog.h" | |
36 | #endif | |
37 | ||
38 | #include "wx/combo.h" | |
a340b80d VZ |
39 | |
40 | // ============================================================================ | |
41 | // implementation | |
42 | // ============================================================================ | |
43 | ||
44 | ||
45 | // ---------------------------------------------------------------------------- | |
46 | // wxVListBoxComboPopup is a wxVListBox customized to act as a popup control | |
47 | // | |
48 | // ---------------------------------------------------------------------------- | |
49 | ||
50 | ||
51 | BEGIN_EVENT_TABLE(wxVListBoxComboPopup, wxVListBox) | |
52 | EVT_MOTION(wxVListBoxComboPopup::OnMouseMove) | |
53 | EVT_KEY_DOWN(wxVListBoxComboPopup::OnKey) | |
54 | EVT_LEFT_UP(wxVListBoxComboPopup::OnLeftClick) | |
55 | END_EVENT_TABLE() | |
56 | ||
57 | ||
6d0ce565 | 58 | void wxVListBoxComboPopup::Init() |
a340b80d VZ |
59 | { |
60 | m_widestWidth = 0; | |
61 | m_avgCharWidth = 0; | |
62 | m_baseImageWidth = 0; | |
63 | m_itemHeight = 0; | |
64 | m_value = -1; | |
65 | m_itemHover = -1; | |
66 | m_clientDataItemsType = wxClientData_None; | |
67 | } | |
68 | ||
69 | bool wxVListBoxComboPopup::Create(wxWindow* parent) | |
70 | { | |
71 | if ( !wxVListBox::Create(parent, | |
72 | wxID_ANY, | |
73 | wxDefaultPosition, | |
74 | wxDefaultSize, | |
75 | wxBORDER_SIMPLE | wxLB_INT_HEIGHT | wxWANTS_CHARS) ) | |
76 | return false; | |
77 | ||
6d0ce565 | 78 | m_useFont = m_combo->GetFont(); |
a340b80d VZ |
79 | |
80 | wxVListBox::SetItemCount(m_strings.GetCount()); | |
81 | ||
82 | // TODO: Move this to SetFont | |
83 | m_itemHeight = GetCharHeight() + 0; | |
84 | ||
85 | return true; | |
86 | } | |
87 | ||
88 | wxVListBoxComboPopup::~wxVListBoxComboPopup() | |
89 | { | |
90 | Clear(); | |
91 | } | |
92 | ||
93 | bool wxVListBoxComboPopup::LazyCreate() | |
94 | { | |
95 | // NB: There is a bug with wxVListBox that can be avoided by creating | |
96 | // it later (bug causes empty space to be shown if initial selection | |
97 | // is at the end of a list longer than the control can show at once). | |
98 | return true; | |
99 | } | |
100 | ||
101 | // paint the control itself | |
102 | void wxVListBoxComboPopup::PaintComboControl( wxDC& dc, const wxRect& rect ) | |
103 | { | |
104 | if ( !(m_combo->GetWindowStyle() & wxODCB_STD_CONTROL_PAINT) ) | |
105 | { | |
40b26d75 | 106 | OnDrawBg(dc,rect,m_value,wxODCB_PAINTING_CONTROL); |
a340b80d VZ |
107 | if ( m_value >= 0 ) |
108 | { | |
40b26d75 | 109 | OnDrawItem(dc,rect,m_value,wxODCB_PAINTING_CONTROL); |
6d0ce565 | 110 | return; |
a340b80d VZ |
111 | } |
112 | } | |
113 | ||
114 | wxComboPopup::PaintComboControl(dc,rect); | |
115 | } | |
116 | ||
117 | void wxVListBoxComboPopup::OnDrawItem(wxDC& dc, const wxRect& rect, size_t n) const | |
118 | { | |
6d0ce565 VZ |
119 | // TODO: Maybe this code could be moved to wxVListBox::OnPaint? |
120 | dc.SetFont(m_useFont); | |
a340b80d VZ |
121 | |
122 | // Set correct text colour for selected items | |
6d0ce565 | 123 | if ( wxVListBox::GetSelection() == (int) n ) |
a340b80d VZ |
124 | dc.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT) ); |
125 | else | |
126 | dc.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT) ); | |
127 | ||
6d0ce565 | 128 | OnDrawItem(dc,rect,(int)n,0); |
a340b80d VZ |
129 | } |
130 | ||
40b26d75 | 131 | wxCoord wxVListBoxComboPopup::OnMeasureItem(size_t n) const |
a340b80d | 132 | { |
40b26d75 WS |
133 | wxOwnerDrawnComboBox* combo = (wxOwnerDrawnComboBox*) m_combo; |
134 | ||
135 | wxASSERT_MSG( combo->IsKindOf(CLASSINFO(wxOwnerDrawnComboBox)), | |
136 | wxT("you must subclass wxVListBoxComboPopup for drawing and measuring methods") ); | |
137 | ||
138 | wxCoord h = combo->OnMeasureItem(n); | |
139 | if ( h < 0 ) | |
140 | h = m_itemHeight; | |
141 | return h; | |
6d0ce565 | 142 | } |
a340b80d | 143 | |
40b26d75 | 144 | wxCoord wxVListBoxComboPopup::OnMeasureItemWidth(size_t n) const |
6d0ce565 | 145 | { |
40b26d75 WS |
146 | wxOwnerDrawnComboBox* combo = (wxOwnerDrawnComboBox*) m_combo; |
147 | ||
148 | wxASSERT_MSG( combo->IsKindOf(CLASSINFO(wxOwnerDrawnComboBox)), | |
149 | wxT("you must subclass wxVListBoxComboPopup for drawing and measuring methods") ); | |
150 | ||
151 | return combo->OnMeasureItemWidth(n); | |
152 | } | |
153 | ||
154 | void wxVListBoxComboPopup::OnDrawBg( wxDC& dc, | |
155 | const wxRect& rect, | |
156 | int item, | |
157 | int flags ) const | |
158 | { | |
159 | wxOwnerDrawnComboBox* combo = (wxOwnerDrawnComboBox*) m_combo; | |
160 | ||
161 | wxASSERT_MSG( combo->IsKindOf(CLASSINFO(wxOwnerDrawnComboBox)), | |
162 | wxT("you must subclass wxVListBoxComboPopup for drawing and measuring methods") ); | |
163 | ||
164 | combo->OnDrawBackground(dc,rect,item,flags); | |
a340b80d VZ |
165 | } |
166 | ||
167 | void wxVListBoxComboPopup::OnDrawBackground(wxDC& dc, const wxRect& rect, size_t n) const | |
168 | { | |
40b26d75 | 169 | OnDrawBg(dc,rect,(int)n,0); |
a340b80d VZ |
170 | } |
171 | ||
6d0ce565 VZ |
172 | // This is called from wxVListBoxComboPopup::OnDrawItem, with text colour and font prepared |
173 | void wxVListBoxComboPopup::OnDrawItem( wxDC& dc, const wxRect& rect, int item, int flags ) const | |
174 | { | |
40b26d75 WS |
175 | wxOwnerDrawnComboBox* combo = (wxOwnerDrawnComboBox*) m_combo; |
176 | ||
177 | wxASSERT_MSG( combo->IsKindOf(CLASSINFO(wxOwnerDrawnComboBox)), | |
178 | wxT("you must subclass wxVListBoxComboPopup for drawing and measuring methods") ); | |
179 | ||
180 | combo->OnDrawItem(dc,rect,item,flags); | |
6d0ce565 VZ |
181 | } |
182 | ||
183 | void wxVListBoxComboPopup::DismissWithEvent() | |
184 | { | |
185 | int selection = wxVListBox::GetSelection(); | |
186 | ||
187 | Dismiss(); | |
188 | ||
189 | wxString valStr; | |
190 | if ( selection != wxNOT_FOUND ) | |
191 | valStr = m_strings[selection]; | |
192 | else | |
193 | valStr = wxEmptyString; | |
194 | ||
195 | m_value = selection; | |
196 | ||
197 | if ( valStr != m_combo->GetValue() ) | |
198 | m_combo->SetValue(valStr); | |
199 | ||
200 | SendComboBoxEvent(selection); | |
201 | } | |
202 | ||
203 | void wxVListBoxComboPopup::SendComboBoxEvent( int selection ) | |
a340b80d VZ |
204 | { |
205 | wxCommandEvent evt(wxEVT_COMMAND_COMBOBOX_SELECTED,m_combo->GetId()); | |
a340b80d VZ |
206 | |
207 | evt.SetEventObject(m_combo); | |
6d0ce565 | 208 | |
a340b80d VZ |
209 | evt.SetInt(selection); |
210 | ||
211 | // Set client data, if any | |
212 | if ( selection >= 0 && (int)m_clientDatas.GetCount() > selection ) | |
213 | { | |
214 | void* clientData = m_clientDatas[selection]; | |
215 | if ( m_clientDataItemsType == wxClientData_Object ) | |
216 | evt.SetClientObject((wxClientData*)clientData); | |
217 | else | |
218 | evt.SetClientData(clientData); | |
219 | } | |
220 | ||
221 | m_combo->GetEventHandler()->AddPendingEvent(evt); | |
222 | } | |
223 | ||
224 | // returns true if key was consumed | |
225 | bool wxVListBoxComboPopup::HandleKey( int keycode, bool saturate ) | |
226 | { | |
227 | int value = m_value; | |
228 | int itemCount = GetCount(); | |
229 | ||
230 | if ( keycode == WXK_DOWN || keycode == WXK_RIGHT ) | |
231 | { | |
232 | value++; | |
233 | } | |
234 | else if ( keycode == WXK_UP || keycode == WXK_LEFT ) | |
235 | { | |
236 | value--; | |
237 | } | |
238 | else if ( keycode == WXK_PAGEDOWN ) | |
239 | { | |
240 | value+=10; | |
241 | } | |
242 | else if ( keycode == WXK_PAGEUP ) | |
243 | { | |
244 | value-=10; | |
245 | } | |
a340b80d VZ |
246 | else |
247 | return false; | |
248 | ||
249 | if ( saturate ) | |
250 | { | |
251 | if ( value >= itemCount ) | |
252 | value = itemCount - 1; | |
253 | else if ( value < 0 ) | |
254 | value = 0; | |
255 | } | |
256 | else | |
257 | { | |
258 | if ( value >= itemCount ) | |
259 | value -= itemCount; | |
260 | else if ( value < 0 ) | |
261 | value += itemCount; | |
262 | } | |
263 | ||
264 | if ( value == m_value ) | |
265 | // Even if value was same, don't skip the event | |
266 | // (good for consistency) | |
267 | return true; | |
268 | ||
269 | m_value = value; | |
270 | ||
a340b80d VZ |
271 | if ( value >= 0 ) |
272 | m_combo->SetValue(m_strings[value]); | |
273 | ||
6d0ce565 | 274 | SendComboBoxEvent(m_value); |
a340b80d VZ |
275 | |
276 | return true; | |
277 | } | |
278 | ||
279 | void wxVListBoxComboPopup::OnComboDoubleClick() | |
280 | { | |
281 | // Cycle on dclick (disable saturation to allow true cycling). | |
282 | if ( !::wxGetKeyState(WXK_SHIFT) ) | |
283 | HandleKey(WXK_DOWN,false); | |
284 | else | |
285 | HandleKey(WXK_UP,false); | |
286 | } | |
287 | ||
288 | void wxVListBoxComboPopup::OnComboKeyEvent( wxKeyEvent& event ) | |
289 | { | |
290 | // Saturated key movement on | |
291 | if ( !HandleKey(event.GetKeyCode(),true) ) | |
292 | event.Skip(); | |
293 | } | |
294 | ||
295 | void wxVListBoxComboPopup::OnPopup() | |
296 | { | |
297 | // *must* set value after size is set (this is because of a vlbox bug) | |
298 | wxVListBox::SetSelection(m_value); | |
299 | } | |
300 | ||
301 | void wxVListBoxComboPopup::OnMouseMove(wxMouseEvent& event) | |
302 | { | |
303 | // Move selection to cursor if it is inside the popup | |
304 | int itemHere = GetItemAtPosition(event.GetPosition()); | |
305 | if ( itemHere >= 0 ) | |
306 | wxVListBox::SetSelection(itemHere); | |
307 | ||
308 | event.Skip(); | |
309 | } | |
310 | ||
311 | void wxVListBoxComboPopup::OnLeftClick(wxMouseEvent& WXUNUSED(event)) | |
312 | { | |
6d0ce565 | 313 | DismissWithEvent(); |
a340b80d VZ |
314 | } |
315 | ||
316 | void wxVListBoxComboPopup::OnKey(wxKeyEvent& event) | |
317 | { | |
318 | // Select item if ENTER is pressed | |
319 | if ( event.GetKeyCode() == WXK_RETURN || event.GetKeyCode() == WXK_NUMPAD_ENTER ) | |
320 | { | |
6d0ce565 | 321 | DismissWithEvent(); |
a340b80d VZ |
322 | } |
323 | // Hide popup if ESC is pressed | |
324 | else if ( event.GetKeyCode() == WXK_ESCAPE ) | |
325 | Dismiss(); | |
326 | else | |
327 | event.Skip(); | |
328 | } | |
329 | ||
330 | void wxVListBoxComboPopup::CheckWidth( int pos ) | |
331 | { | |
6d0ce565 | 332 | wxCoord x = OnMeasureItemWidth(pos); |
a340b80d VZ |
333 | |
334 | if ( x < 0 ) | |
335 | { | |
6d0ce565 VZ |
336 | if ( !m_useFont.Ok() ) |
337 | m_useFont = m_combo->GetFont(); | |
a340b80d VZ |
338 | |
339 | wxCoord y; | |
6d0ce565 | 340 | m_combo->GetTextExtent(m_strings[pos], &x, &y, 0, 0, &m_useFont); |
a340b80d VZ |
341 | x += 4; |
342 | } | |
343 | ||
344 | if ( m_widestWidth < x ) | |
345 | { | |
346 | m_widestWidth = x; | |
347 | } | |
348 | } | |
349 | ||
350 | void wxVListBoxComboPopup::Insert( const wxString& item, int pos ) | |
351 | { | |
352 | // Need to change selection? | |
353 | wxString strValue; | |
354 | if ( !(m_combo->GetWindowStyle() & wxCB_READONLY) && | |
355 | m_combo->GetValue() == item ) | |
6d0ce565 | 356 | { |
a340b80d | 357 | m_value = pos; |
6d0ce565 | 358 | } |
a340b80d VZ |
359 | |
360 | m_strings.Insert(item,pos); | |
361 | ||
362 | if ( IsCreated() ) | |
363 | wxVListBox::SetItemCount( wxVListBox::GetItemCount()+1 ); | |
364 | ||
365 | // Calculate width | |
366 | CheckWidth(pos); | |
367 | } | |
368 | ||
369 | int wxVListBoxComboPopup::Append(const wxString& item) | |
370 | { | |
371 | int pos = (int)m_strings.GetCount(); | |
372 | ||
373 | if ( m_combo->GetWindowStyle() & wxCB_SORT ) | |
374 | { | |
375 | // Find position | |
376 | // TODO: Could be optimized with binary search | |
377 | wxArrayString strings = m_strings; | |
378 | unsigned int i; | |
379 | ||
380 | for ( i=0; i<strings.GetCount(); i++ ) | |
381 | { | |
382 | if ( item.Cmp(strings.Item(i)) < 0 ) | |
383 | { | |
384 | pos = (int)i; | |
385 | break; | |
386 | } | |
387 | } | |
388 | } | |
389 | ||
390 | Insert(item,pos); | |
391 | ||
392 | return pos; | |
393 | } | |
394 | ||
395 | void wxVListBoxComboPopup::Clear() | |
396 | { | |
397 | wxASSERT(m_combo); | |
398 | ||
399 | m_strings.Empty(); | |
400 | ||
401 | ClearClientDatas(); | |
402 | ||
cdc99ddd WS |
403 | m_value = wxNOT_FOUND; |
404 | ||
a340b80d VZ |
405 | if ( IsCreated() ) |
406 | wxVListBox::SetItemCount(0); | |
407 | } | |
408 | ||
409 | void wxVListBoxComboPopup::ClearClientDatas() | |
410 | { | |
411 | if ( m_clientDataItemsType == wxClientData_Object ) | |
412 | { | |
413 | size_t i; | |
414 | for ( i=0; i<m_clientDatas.GetCount(); i++ ) | |
415 | delete (wxClientData*) m_clientDatas[i]; | |
416 | } | |
417 | ||
418 | m_clientDatas.Empty(); | |
419 | } | |
420 | ||
421 | void wxVListBoxComboPopup::SetItemClientData( unsigned int n, | |
422 | void* clientData, | |
423 | wxClientDataType clientDataItemsType ) | |
424 | { | |
425 | // It should be sufficient to update this variable only here | |
426 | m_clientDataItemsType = clientDataItemsType; | |
427 | ||
428 | m_clientDatas.SetCount(n+1,NULL); | |
429 | m_clientDatas[n] = clientData; | |
430 | } | |
431 | ||
432 | void* wxVListBoxComboPopup::GetItemClientData(unsigned int n) const | |
433 | { | |
434 | if ( m_clientDatas.GetCount() > n ) | |
435 | return m_clientDatas[n]; | |
436 | ||
437 | return NULL; | |
438 | } | |
439 | ||
440 | void wxVListBoxComboPopup::Delete( unsigned int item ) | |
441 | { | |
442 | // Remove client data, if set | |
443 | if ( m_clientDatas.GetCount() ) | |
444 | { | |
445 | if ( m_clientDataItemsType == wxClientData_Object ) | |
446 | delete (wxClientData*) m_clientDatas[item]; | |
447 | ||
448 | m_clientDatas.RemoveAt(item); | |
449 | } | |
450 | ||
451 | m_strings.RemoveAt(item); | |
452 | ||
453 | if ( IsCreated() ) | |
454 | wxVListBox::SetItemCount( wxVListBox::GetItemCount()-1 ); | |
455 | } | |
456 | ||
9e6aca68 | 457 | int wxVListBoxComboPopup::FindString(const wxString& s, bool bCase) const |
a340b80d | 458 | { |
9e6aca68 | 459 | return m_strings.Index(s, bCase); |
a340b80d VZ |
460 | } |
461 | ||
462 | unsigned int wxVListBoxComboPopup::GetCount() const | |
463 | { | |
464 | return m_strings.GetCount(); | |
465 | } | |
466 | ||
467 | wxString wxVListBoxComboPopup::GetString( int item ) const | |
468 | { | |
469 | return m_strings[item]; | |
470 | } | |
471 | ||
472 | void wxVListBoxComboPopup::SetString( int item, const wxString& str ) | |
473 | { | |
474 | m_strings[item] = str; | |
475 | } | |
476 | ||
477 | wxString wxVListBoxComboPopup::GetStringValue() const | |
478 | { | |
479 | if ( m_value >= 0 ) | |
480 | return m_strings[m_value]; | |
481 | return wxEmptyString; | |
482 | } | |
483 | ||
484 | void wxVListBoxComboPopup::SetSelection( int item ) | |
485 | { | |
85fed18c WS |
486 | wxCHECK_RET( item == wxNOT_FOUND || ((unsigned int)item < GetCount()), |
487 | wxT("invalid index in wxVListBoxComboPopup::SetSelection") ); | |
a340b80d VZ |
488 | |
489 | m_value = item; | |
490 | ||
491 | if ( IsCreated() ) | |
492 | wxVListBox::SetSelection(item); | |
493 | } | |
494 | ||
6d0ce565 VZ |
495 | int wxVListBoxComboPopup::GetSelection() const |
496 | { | |
497 | return m_value; | |
498 | } | |
499 | ||
a340b80d VZ |
500 | void wxVListBoxComboPopup::SetStringValue( const wxString& value ) |
501 | { | |
502 | int index = m_strings.Index(value); | |
503 | ||
504 | m_value = index; | |
505 | ||
506 | if ( index >= -1 && index < (int)wxVListBox::GetItemCount() ) | |
507 | wxVListBox::SetSelection(index); | |
508 | } | |
509 | ||
510 | wxSize wxVListBoxComboPopup::GetAdjustedSize( int minWidth, int prefHeight, int maxHeight ) | |
511 | { | |
512 | int height = 250; | |
513 | ||
514 | if ( m_strings.GetCount() ) | |
515 | { | |
516 | if ( prefHeight > 0 ) | |
517 | height = prefHeight; | |
518 | ||
519 | if ( height > maxHeight ) | |
520 | height = maxHeight; | |
521 | ||
522 | int totalHeight = GetTotalHeight(); // + 3; | |
523 | if ( height >= totalHeight ) | |
524 | { | |
525 | height = totalHeight; | |
526 | } | |
527 | else | |
528 | { | |
529 | // Adjust height to a multiple of the height of the first item | |
530 | // NB: Calculations that take variable height into account | |
531 | // are unnecessary. | |
532 | int fih = GetLineHeight(0); | |
533 | int shown = height/fih; | |
534 | height = shown * fih; | |
535 | } | |
536 | } | |
537 | else | |
538 | height = 50; | |
539 | ||
540 | // Take scrollbar into account in width calculations | |
541 | int widestWidth = m_widestWidth + wxSystemSettings::GetMetric(wxSYS_VSCROLL_X); | |
542 | return wxSize(minWidth > widestWidth ? minWidth : widestWidth, | |
543 | height+2); | |
544 | } | |
545 | ||
6d0ce565 VZ |
546 | //void wxVListBoxComboPopup::Populate( int n, const wxString choices[] ) |
547 | void wxVListBoxComboPopup::Populate( const wxArrayString& choices ) | |
a340b80d VZ |
548 | { |
549 | int i; | |
550 | ||
6d0ce565 VZ |
551 | int n = choices.GetCount(); |
552 | ||
a340b80d VZ |
553 | for ( i=0; i<n; i++ ) |
554 | { | |
6d0ce565 | 555 | const wxString& item = choices.Item(i); |
a340b80d VZ |
556 | m_strings.Add(item); |
557 | CheckWidth(i); | |
558 | } | |
559 | ||
560 | if ( IsCreated() ) | |
561 | wxVListBox::SetItemCount(n); | |
562 | ||
563 | // Sort the initial choices | |
564 | if ( m_combo->GetWindowStyle() & wxCB_SORT ) | |
565 | m_strings.Sort(); | |
566 | ||
567 | // Find initial selection | |
568 | wxString strValue = m_combo->GetValue(); | |
40b26d75 | 569 | if ( strValue.length() ) |
a340b80d VZ |
570 | m_value = m_strings.Index(strValue); |
571 | } | |
572 | ||
573 | // ---------------------------------------------------------------------------- | |
574 | // wxOwnerDrawnComboBox | |
575 | // ---------------------------------------------------------------------------- | |
576 | ||
577 | ||
a57d600f | 578 | BEGIN_EVENT_TABLE(wxOwnerDrawnComboBox, wxComboCtrl) |
a340b80d VZ |
579 | END_EVENT_TABLE() |
580 | ||
581 | ||
a57d600f | 582 | IMPLEMENT_DYNAMIC_CLASS2(wxOwnerDrawnComboBox, wxComboCtrl, wxControlWithItems) |
a340b80d VZ |
583 | |
584 | void wxOwnerDrawnComboBox::Init() | |
585 | { | |
6d0ce565 | 586 | m_popupInterface = NULL; |
a340b80d VZ |
587 | } |
588 | ||
589 | bool wxOwnerDrawnComboBox::Create(wxWindow *parent, | |
590 | wxWindowID id, | |
591 | const wxString& value, | |
592 | const wxPoint& pos, | |
593 | const wxSize& size, | |
594 | long style, | |
595 | const wxValidator& validator, | |
596 | const wxString& name) | |
597 | { | |
a57d600f | 598 | return wxComboCtrl::Create(parent,id,value,pos,size,style,validator,name); |
a340b80d VZ |
599 | } |
600 | ||
601 | wxOwnerDrawnComboBox::wxOwnerDrawnComboBox(wxWindow *parent, | |
602 | wxWindowID id, | |
603 | const wxString& value, | |
604 | const wxPoint& pos, | |
605 | const wxSize& size, | |
606 | const wxArrayString& choices, | |
607 | long style, | |
608 | const wxValidator& validator, | |
609 | const wxString& name) | |
a57d600f | 610 | : wxComboCtrl() |
a340b80d VZ |
611 | { |
612 | Init(); | |
613 | ||
614 | Create(parent,id,value,pos,size,choices,style, validator, name); | |
615 | } | |
616 | ||
617 | bool wxOwnerDrawnComboBox::Create(wxWindow *parent, | |
618 | wxWindowID id, | |
619 | const wxString& value, | |
620 | const wxPoint& pos, | |
621 | const wxSize& size, | |
622 | const wxArrayString& choices, | |
623 | long style, | |
624 | const wxValidator& validator, | |
625 | const wxString& name) | |
626 | { | |
6d0ce565 VZ |
627 | m_initChs = choices; |
628 | //wxCArrayString chs(choices); | |
a340b80d | 629 | |
6d0ce565 VZ |
630 | //return Create(parent, id, value, pos, size, chs.GetCount(), |
631 | // chs.GetStrings(), style, validator, name); | |
632 | return Create(parent, id, value, pos, size, 0, | |
633 | NULL, style, validator, name); | |
a340b80d VZ |
634 | } |
635 | ||
636 | bool wxOwnerDrawnComboBox::Create(wxWindow *parent, | |
637 | wxWindowID id, | |
638 | const wxString& value, | |
639 | const wxPoint& pos, | |
640 | const wxSize& size, | |
641 | int n, | |
642 | const wxString choices[], | |
643 | long style, | |
644 | const wxValidator& validator, | |
645 | const wxString& name) | |
646 | { | |
647 | ||
648 | if ( !Create(parent, id, value, pos, size, style, | |
649 | validator, name) ) | |
650 | { | |
651 | return false; | |
652 | } | |
653 | ||
6d0ce565 VZ |
654 | int i; |
655 | for ( i=0; i<n; i++ ) | |
656 | m_initChs.Add(choices[i]); | |
a340b80d VZ |
657 | |
658 | return true; | |
659 | } | |
660 | ||
661 | wxOwnerDrawnComboBox::~wxOwnerDrawnComboBox() | |
662 | { | |
663 | if ( m_popupInterface ) | |
664 | m_popupInterface->ClearClientDatas(); | |
665 | } | |
666 | ||
6d0ce565 VZ |
667 | void wxOwnerDrawnComboBox::SetPopupControl( wxComboPopup* popup ) |
668 | { | |
669 | if ( !popup ) | |
670 | { | |
671 | popup = new wxVListBoxComboPopup(); | |
672 | } | |
673 | ||
a57d600f | 674 | wxComboCtrl::SetPopupControl(popup); |
6d0ce565 VZ |
675 | |
676 | wxASSERT(popup); | |
677 | m_popupInterface = (wxVListBoxComboPopup*) popup; | |
678 | ||
679 | // Add initial choices to the wxVListBox | |
680 | if ( !m_popupInterface->GetCount() ) | |
681 | { | |
682 | //m_popupInterface->Populate(m_initChs.GetCount(),m_initChs.GetStrings()); | |
683 | m_popupInterface->Populate(m_initChs); | |
684 | m_initChs.Clear(); | |
685 | } | |
686 | } | |
687 | ||
a340b80d VZ |
688 | // ---------------------------------------------------------------------------- |
689 | // wxOwnerDrawnComboBox item manipulation methods | |
690 | // ---------------------------------------------------------------------------- | |
691 | ||
692 | void wxOwnerDrawnComboBox::Clear() | |
693 | { | |
6d0ce565 | 694 | EnsurePopupControl(); |
a340b80d VZ |
695 | |
696 | m_popupInterface->Clear(); | |
697 | ||
cdc99ddd | 698 | SetValue(wxEmptyString); |
a340b80d VZ |
699 | } |
700 | ||
701 | void wxOwnerDrawnComboBox::Delete(unsigned int n) | |
702 | { | |
85fed18c | 703 | wxCHECK_RET( IsValid(n), _T("invalid index in wxOwnerDrawnComboBox::Delete") ); |
a340b80d VZ |
704 | |
705 | if ( GetSelection() == (int) n ) | |
706 | SetValue(wxEmptyString); | |
707 | ||
708 | m_popupInterface->Delete(n); | |
709 | } | |
710 | ||
711 | unsigned int wxOwnerDrawnComboBox::GetCount() const | |
712 | { | |
6d0ce565 | 713 | wxASSERT_MSG( m_popupInterface, wxT("no popup interface") ); |
a340b80d VZ |
714 | return m_popupInterface->GetCount(); |
715 | } | |
716 | ||
717 | wxString wxOwnerDrawnComboBox::GetString(unsigned int n) const | |
718 | { | |
85fed18c | 719 | wxCHECK_MSG( IsValid(n), wxEmptyString, _T("invalid index in wxOwnerDrawnComboBox::GetString") ); |
a340b80d VZ |
720 | return m_popupInterface->GetString(n); |
721 | } | |
722 | ||
723 | void wxOwnerDrawnComboBox::SetString(unsigned int n, const wxString& s) | |
724 | { | |
85fed18c | 725 | wxCHECK_RET( IsValid(n), _T("invalid index in wxOwnerDrawnComboBox::SetString") ); |
a340b80d VZ |
726 | m_popupInterface->SetString(n,s); |
727 | } | |
728 | ||
9e6aca68 | 729 | int wxOwnerDrawnComboBox::FindString(const wxString& s, bool bCase) const |
a340b80d | 730 | { |
6d0ce565 | 731 | wxASSERT_MSG( m_popupInterface, wxT("no popup interface") ); |
9e6aca68 | 732 | return m_popupInterface->FindString(s, bCase); |
a340b80d VZ |
733 | } |
734 | ||
735 | void wxOwnerDrawnComboBox::Select(int n) | |
736 | { | |
6d0ce565 | 737 | EnsurePopupControl(); |
a340b80d | 738 | |
40b26d75 WS |
739 | wxCHECK_RET( (n == wxNOT_FOUND) || IsValid(n), _T("invalid index in wxOwnerDrawnComboBox::Select") ); |
740 | ||
a340b80d VZ |
741 | m_popupInterface->SetSelection(n); |
742 | ||
743 | wxString str; | |
744 | if ( n >= 0 ) | |
745 | str = m_popupInterface->GetString(n); | |
746 | ||
747 | // Refresh text portion in control | |
748 | if ( m_text ) | |
749 | m_text->SetValue( str ); | |
750 | else | |
751 | m_valueString = str; | |
752 | ||
753 | Refresh(); | |
754 | } | |
755 | ||
756 | int wxOwnerDrawnComboBox::GetSelection() const | |
757 | { | |
6d0ce565 | 758 | wxASSERT_MSG( m_popupInterface, wxT("no popup interface") ); |
a340b80d VZ |
759 | return m_popupInterface->GetSelection(); |
760 | } | |
761 | ||
762 | int wxOwnerDrawnComboBox::DoAppend(const wxString& item) | |
763 | { | |
6d0ce565 VZ |
764 | EnsurePopupControl(); |
765 | wxASSERT(m_popupInterface); | |
a340b80d VZ |
766 | return m_popupInterface->Append(item); |
767 | } | |
768 | ||
769 | int wxOwnerDrawnComboBox::DoInsert(const wxString& item, unsigned int pos) | |
770 | { | |
40b26d75 WS |
771 | EnsurePopupControl(); |
772 | ||
a340b80d | 773 | wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT), -1, wxT("can't insert into sorted list")); |
85fed18c | 774 | wxCHECK_MSG(IsValidInsert(pos), -1, wxT("invalid index")); |
a340b80d VZ |
775 | |
776 | m_popupInterface->Insert(item,pos); | |
777 | ||
778 | return pos; | |
779 | } | |
780 | ||
781 | void wxOwnerDrawnComboBox::DoSetItemClientData(unsigned int n, void* clientData) | |
782 | { | |
6d0ce565 | 783 | EnsurePopupControl(); |
a340b80d VZ |
784 | m_popupInterface->SetItemClientData(n,clientData,m_clientDataItemsType); |
785 | } | |
786 | ||
787 | void* wxOwnerDrawnComboBox::DoGetItemClientData(unsigned int n) const | |
788 | { | |
6d0ce565 | 789 | wxASSERT_MSG( m_popupInterface, wxT("no popup interface") ); |
a340b80d VZ |
790 | return m_popupInterface->GetItemClientData(n); |
791 | } | |
792 | ||
793 | void wxOwnerDrawnComboBox::DoSetItemClientObject(unsigned int n, wxClientData* clientData) | |
794 | { | |
795 | DoSetItemClientData(n, (void*) clientData); | |
796 | } | |
797 | ||
798 | wxClientData* wxOwnerDrawnComboBox::DoGetItemClientObject(unsigned int n) const | |
799 | { | |
800 | return (wxClientData*) DoGetItemClientData(n); | |
801 | } | |
802 | ||
40b26d75 WS |
803 | // ---------------------------------------------------------------------------- |
804 | // wxOwnerDrawnComboBox item drawing and measuring default implementations | |
805 | // ---------------------------------------------------------------------------- | |
806 | ||
807 | void wxOwnerDrawnComboBox::OnDrawItem( wxDC& dc, | |
808 | const wxRect& rect, | |
809 | int item, | |
810 | int flags ) const | |
811 | { | |
812 | if ( flags & wxODCB_PAINTING_CONTROL ) | |
813 | { | |
814 | dc.DrawText( GetValue(), | |
815 | rect.x + GetTextIndent(), | |
816 | (rect.height-dc.GetCharHeight())/2 + rect.y ); | |
817 | } | |
818 | else | |
819 | { | |
820 | dc.DrawText( m_popupInterface->GetString(item), rect.x + 2, rect.y ); | |
821 | } | |
822 | } | |
823 | ||
824 | wxCoord wxOwnerDrawnComboBox::OnMeasureItem( size_t WXUNUSED(item) ) const | |
825 | { | |
826 | return -1; | |
827 | } | |
828 | ||
829 | wxCoord wxOwnerDrawnComboBox::OnMeasureItemWidth( size_t WXUNUSED(item) ) const | |
830 | { | |
831 | return -1; | |
832 | } | |
833 | ||
834 | void wxOwnerDrawnComboBox::OnDrawBackground(wxDC& dc, const wxRect& rect, int item, int flags) const | |
835 | { | |
836 | // we need to render selected and current items differently | |
837 | if ( m_popupInterface->IsCurrent((size_t)item) ) | |
838 | { | |
839 | DrawFocusBackground(dc, | |
840 | rect, | |
841 | (flags&wxODCB_PAINTING_CONTROL?0:wxCONTROL_ISSUBMENU) | | |
842 | wxCONTROL_SELECTED); | |
843 | } | |
844 | //else: do nothing for the normal items | |
845 | } | |
846 | ||
a57d600f | 847 | #endif // wxUSE_ODCOMBOBOX |