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