remove duplicate m_popupInterface (patch 1509424)
[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 // Move selection to cursor if it is inside the popup
305 int itemHere = GetItemAtPosition(event.GetPosition());
306 if ( itemHere >= 0 )
307 wxVListBox::SetSelection(itemHere);
308
309 event.Skip();
310 }
311
312 void wxVListBoxComboPopup::OnLeftClick(wxMouseEvent& WXUNUSED(event))
313 {
314 DismissWithEvent();
315 }
316
317 void wxVListBoxComboPopup::OnKey(wxKeyEvent& event)
318 {
319 // Select item if ENTER is pressed
320 if ( event.GetKeyCode() == WXK_RETURN || event.GetKeyCode() == WXK_NUMPAD_ENTER )
321 {
322 DismissWithEvent();
323 }
324 // Hide popup if ESC is pressed
325 else if ( event.GetKeyCode() == WXK_ESCAPE )
326 Dismiss();
327 else
328 event.Skip();
329 }
330
331 void wxVListBoxComboPopup::Insert( const wxString& item, int pos )
332 {
333 // Need to change selection?
334 wxString strValue;
335 if ( !(m_combo->GetWindowStyle() & wxCB_READONLY) &&
336 m_combo->GetValue() == item )
337 {
338 m_value = pos;
339 }
340
341 m_strings.Insert(item,pos);
342 m_widths.Insert(-1,pos);
343 m_widthsDirty = true;
344
345 if ( IsCreated() )
346 wxVListBox::SetItemCount( wxVListBox::GetItemCount()+1 );
347 }
348
349 int wxVListBoxComboPopup::Append(const wxString& item)
350 {
351 int pos = (int)m_strings.GetCount();
352
353 if ( m_combo->GetWindowStyle() & wxCB_SORT )
354 {
355 // Find position
356 // TODO: Could be optimized with binary search
357 wxArrayString strings = m_strings;
358 unsigned int i;
359
360 for ( i=0; i<strings.GetCount(); i++ )
361 {
362 if ( item.Cmp(strings.Item(i)) < 0 )
363 {
364 pos = (int)i;
365 break;
366 }
367 }
368 }
369
370 Insert(item,pos);
371
372 return pos;
373 }
374
375 void wxVListBoxComboPopup::Clear()
376 {
377 wxASSERT(m_combo);
378
379 m_strings.Empty();
380 m_widths.Empty();
381
382 m_widestWidth = 0;
383 m_widestItem = -1;
384
385 ClearClientDatas();
386
387 m_value = wxNOT_FOUND;
388
389 if ( IsCreated() )
390 wxVListBox::SetItemCount(0);
391 }
392
393 void wxVListBoxComboPopup::ClearClientDatas()
394 {
395 if ( m_clientDataItemsType == wxClientData_Object )
396 {
397 size_t i;
398 for ( i=0; i<m_clientDatas.GetCount(); i++ )
399 delete (wxClientData*) m_clientDatas[i];
400 }
401
402 m_clientDatas.Empty();
403 }
404
405 void wxVListBoxComboPopup::SetItemClientData( unsigned int n,
406 void* clientData,
407 wxClientDataType clientDataItemsType )
408 {
409 // It should be sufficient to update this variable only here
410 m_clientDataItemsType = clientDataItemsType;
411
412 m_clientDatas.SetCount(n+1,NULL);
413 m_clientDatas[n] = clientData;
414
415 ItemWidthChanged(n);
416 }
417
418 void* wxVListBoxComboPopup::GetItemClientData(unsigned int n) const
419 {
420 if ( m_clientDatas.GetCount() > n )
421 return m_clientDatas[n];
422
423 return NULL;
424 }
425
426 void wxVListBoxComboPopup::Delete( unsigned int item )
427 {
428 // Remove client data, if set
429 if ( m_clientDatas.GetCount() )
430 {
431 if ( m_clientDataItemsType == wxClientData_Object )
432 delete (wxClientData*) m_clientDatas[item];
433
434 m_clientDatas.RemoveAt(item);
435 }
436
437 m_strings.RemoveAt(item);
438 m_widths.RemoveAt(item);
439
440 if ( (int)item == m_widestItem )
441 m_findWidest = true;
442
443 if ( IsCreated() )
444 wxVListBox::SetItemCount( wxVListBox::GetItemCount()-1 );
445 }
446
447 int wxVListBoxComboPopup::FindString(const wxString& s, bool bCase) const
448 {
449 return m_strings.Index(s, bCase);
450 }
451
452 unsigned int wxVListBoxComboPopup::GetCount() const
453 {
454 return m_strings.GetCount();
455 }
456
457 wxString wxVListBoxComboPopup::GetString( int item ) const
458 {
459 return m_strings[item];
460 }
461
462 void wxVListBoxComboPopup::SetString( int item, const wxString& str )
463 {
464 m_strings[item] = str;
465 ItemWidthChanged(item);
466 }
467
468 wxString wxVListBoxComboPopup::GetStringValue() const
469 {
470 if ( m_value >= 0 )
471 return m_strings[m_value];
472 return wxEmptyString;
473 }
474
475 void wxVListBoxComboPopup::SetSelection( int item )
476 {
477 wxCHECK_RET( item == wxNOT_FOUND || ((unsigned int)item < GetCount()),
478 wxT("invalid index in wxVListBoxComboPopup::SetSelection") );
479
480 m_value = item;
481
482 if ( IsCreated() )
483 wxVListBox::SetSelection(item);
484 }
485
486 int wxVListBoxComboPopup::GetSelection() const
487 {
488 return m_value;
489 }
490
491 void wxVListBoxComboPopup::SetStringValue( const wxString& value )
492 {
493 int index = m_strings.Index(value);
494
495 m_value = index;
496
497 if ( index >= -1 && index < (int)wxVListBox::GetItemCount() )
498 wxVListBox::SetSelection(index);
499 }
500
501 wxSize wxVListBoxComboPopup::GetAdjustedSize( int minWidth, int prefHeight, int maxHeight )
502 {
503 int height = 250;
504
505 if ( m_strings.GetCount() )
506 {
507 if ( prefHeight > 0 )
508 height = prefHeight;
509
510 if ( height > maxHeight )
511 height = maxHeight;
512
513 int totalHeight = GetTotalHeight(); // + 3;
514 if ( height >= totalHeight )
515 {
516 height = totalHeight;
517 }
518 else
519 {
520 // Adjust height to a multiple of the height of the first item
521 // NB: Calculations that take variable height into account
522 // are unnecessary.
523 int fih = GetLineHeight(0);
524 int shown = height/fih;
525 height = shown * fih;
526 }
527 }
528 else
529 height = 50;
530
531 bool doFindWidest = m_findWidest;
532
533 // Measure items with dirty width.
534 if ( m_widthsDirty )
535 {
536 unsigned int i;
537 unsigned int n = m_widths.GetCount();
538 int dirtyHandled = 0;
539 wxArrayInt& widths = m_widths;
540
541 // I think using wxDC::GetTextExtent is faster than
542 // wxWindow::GetTextExtent (assuming same dc is used
543 // for all calls, as we do here).
544 wxClientDC dc(m_combo);
545 dc.SetFont(m_useFont);
546
547 for ( i=0; i<n; i++ )
548 {
549 if ( widths[i] < 0 )
550 {
551 wxCoord x = OnMeasureItemWidth(i);
552
553 if ( x < 0 )
554 {
555 const wxString& text = m_strings[i];
556
557 // To make sure performance won't suck in extreme scenarios,
558 // we'll estimate length after some arbitrary number of items
559 // have been checked precily.
560 if ( dirtyHandled < 1024 )
561 {
562 wxCoord y;
563 dc.GetTextExtent(text, &x, &y, 0, 0);
564 x += 4;
565 }
566 else
567 {
568 x = text.length() * (dc.GetCharWidth()+1);
569 }
570 }
571
572 widths[i] = x;
573
574 if ( x >= m_widestWidth )
575 {
576 m_widestWidth = x;
577 m_widestItem = (int)i;
578 }
579 else if ( (int)i == m_widestItem )
580 {
581 // Width of previously widest item has been decreased, so
582 // we'll have to check all to find current widest item.
583 doFindWidest = true;
584 }
585
586 dirtyHandled++;
587 }
588 }
589
590 m_widthsDirty = false;
591 }
592
593 if ( doFindWidest )
594 {
595 unsigned int i;
596 unsigned int n = m_widths.GetCount();
597
598 int bestWidth = -1;
599 int bestIndex = -1;
600
601 for ( i=0; i<n; i++ )
602 {
603 int w = m_widths[i];
604 if ( w > bestWidth )
605 {
606 bestIndex = (int)i;
607 bestWidth = w;
608 }
609 }
610
611 m_widestWidth = bestWidth;
612 m_widestItem = bestIndex;
613
614 m_findWidest = false;
615 }
616
617 // Take scrollbar into account in width calculations
618 int widestWidth = m_widestWidth + wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
619 return wxSize(minWidth > widestWidth ? minWidth : widestWidth,
620 height+2);
621 }
622
623 //void wxVListBoxComboPopup::Populate( int n, const wxString choices[] )
624 void wxVListBoxComboPopup::Populate( const wxArrayString& choices )
625 {
626 int i;
627
628 int n = choices.GetCount();
629
630 for ( i=0; i<n; i++ )
631 {
632 const wxString& item = choices.Item(i);
633 m_strings.Add(item);
634 }
635
636 m_widths.SetCount(n,-1);
637 m_widthsDirty = true;
638
639 if ( IsCreated() )
640 wxVListBox::SetItemCount(n);
641
642 // Sort the initial choices
643 if ( m_combo->GetWindowStyle() & wxCB_SORT )
644 m_strings.Sort();
645
646 // Find initial selection
647 wxString strValue = m_combo->GetValue();
648 if ( strValue.length() )
649 m_value = m_strings.Index(strValue);
650 }
651
652 // ----------------------------------------------------------------------------
653 // wxOwnerDrawnComboBox
654 // ----------------------------------------------------------------------------
655
656
657 BEGIN_EVENT_TABLE(wxOwnerDrawnComboBox, wxComboCtrl)
658 END_EVENT_TABLE()
659
660
661 IMPLEMENT_DYNAMIC_CLASS2(wxOwnerDrawnComboBox, wxComboCtrl, wxControlWithItems)
662
663 void wxOwnerDrawnComboBox::Init()
664 {
665 }
666
667 bool wxOwnerDrawnComboBox::Create(wxWindow *parent,
668 wxWindowID id,
669 const wxString& value,
670 const wxPoint& pos,
671 const wxSize& size,
672 long style,
673 const wxValidator& validator,
674 const wxString& name)
675 {
676 return wxComboCtrl::Create(parent,id,value,pos,size,style,validator,name);
677 }
678
679 wxOwnerDrawnComboBox::wxOwnerDrawnComboBox(wxWindow *parent,
680 wxWindowID id,
681 const wxString& value,
682 const wxPoint& pos,
683 const wxSize& size,
684 const wxArrayString& choices,
685 long style,
686 const wxValidator& validator,
687 const wxString& name)
688 : wxComboCtrl()
689 {
690 Init();
691
692 Create(parent,id,value,pos,size,choices,style, validator, name);
693 }
694
695 bool wxOwnerDrawnComboBox::Create(wxWindow *parent,
696 wxWindowID id,
697 const wxString& value,
698 const wxPoint& pos,
699 const wxSize& size,
700 const wxArrayString& choices,
701 long style,
702 const wxValidator& validator,
703 const wxString& name)
704 {
705 m_initChs = choices;
706 //wxCArrayString chs(choices);
707
708 //return Create(parent, id, value, pos, size, chs.GetCount(),
709 // chs.GetStrings(), style, validator, name);
710 return Create(parent, id, value, pos, size, 0,
711 NULL, style, validator, name);
712 }
713
714 bool wxOwnerDrawnComboBox::Create(wxWindow *parent,
715 wxWindowID id,
716 const wxString& value,
717 const wxPoint& pos,
718 const wxSize& size,
719 int n,
720 const wxString choices[],
721 long style,
722 const wxValidator& validator,
723 const wxString& name)
724 {
725
726 if ( !Create(parent, id, value, pos, size, style,
727 validator, name) )
728 {
729 return false;
730 }
731
732 int i;
733 for ( i=0; i<n; i++ )
734 m_initChs.Add(choices[i]);
735
736 return true;
737 }
738
739 wxOwnerDrawnComboBox::~wxOwnerDrawnComboBox()
740 {
741 if ( m_popupInterface )
742 GetVListBoxComboPopup()->ClearClientDatas();
743 }
744
745 void wxOwnerDrawnComboBox::DoSetPopupControl(wxComboPopup* popup)
746 {
747 if ( !popup )
748 {
749 popup = new wxVListBoxComboPopup();
750 }
751
752 wxComboCtrl::DoSetPopupControl(popup);
753
754 wxASSERT(popup);
755
756 // Add initial choices to the wxVListBox
757 if ( !GetVListBoxComboPopup()->GetCount() )
758 {
759 GetVListBoxComboPopup()->Populate(m_initChs);
760 m_initChs.Clear();
761 }
762 }
763
764 // ----------------------------------------------------------------------------
765 // wxOwnerDrawnComboBox item manipulation methods
766 // ----------------------------------------------------------------------------
767
768 void wxOwnerDrawnComboBox::Clear()
769 {
770 EnsurePopupControl();
771
772 GetVListBoxComboPopup()->Clear();
773
774 SetValue(wxEmptyString);
775 }
776
777 void wxOwnerDrawnComboBox::Delete(unsigned int n)
778 {
779 wxCHECK_RET( IsValid(n), _T("invalid index in wxOwnerDrawnComboBox::Delete") );
780
781 if ( GetSelection() == (int) n )
782 SetValue(wxEmptyString);
783
784 GetVListBoxComboPopup()->Delete(n);
785 }
786
787 unsigned int wxOwnerDrawnComboBox::GetCount() const
788 {
789 if ( !m_popupInterface )
790 return m_initChs.GetCount();
791
792 return GetVListBoxComboPopup()->GetCount();
793 }
794
795 wxString wxOwnerDrawnComboBox::GetString(unsigned int n) const
796 {
797 wxCHECK_MSG( IsValid(n), wxEmptyString, _T("invalid index in wxOwnerDrawnComboBox::GetString") );
798
799 if ( !m_popupInterface )
800 return m_initChs.Item(n);
801
802 return GetVListBoxComboPopup()->GetString(n);
803 }
804
805 void wxOwnerDrawnComboBox::SetString(unsigned int n, const wxString& s)
806 {
807 EnsurePopupControl();
808
809 wxCHECK_RET( IsValid(n), _T("invalid index in wxOwnerDrawnComboBox::SetString") );
810
811 GetVListBoxComboPopup()->SetString(n,s);
812 }
813
814 int wxOwnerDrawnComboBox::FindString(const wxString& s, bool bCase) const
815 {
816 if ( !m_popupInterface )
817 return m_initChs.Index(s, bCase);
818
819 return GetVListBoxComboPopup()->FindString(s, bCase);
820 }
821
822 void wxOwnerDrawnComboBox::Select(int n)
823 {
824 EnsurePopupControl();
825
826 wxCHECK_RET( (n == wxNOT_FOUND) || IsValid(n), _T("invalid index in wxOwnerDrawnComboBox::Select") );
827
828 GetVListBoxComboPopup()->SetSelection(n);
829
830 wxString str;
831 if ( n >= 0 )
832 str = GetVListBoxComboPopup()->GetString(n);
833
834 // Refresh text portion in control
835 if ( m_text )
836 m_text->SetValue( str );
837 else
838 m_valueString = str;
839
840 Refresh();
841 }
842
843 int wxOwnerDrawnComboBox::GetSelection() const
844 {
845 if ( !m_popupInterface )
846 return m_initChs.Index(m_valueString);
847
848 return GetVListBoxComboPopup()->GetSelection();
849 }
850
851 int wxOwnerDrawnComboBox::DoAppend(const wxString& item)
852 {
853 EnsurePopupControl();
854 wxASSERT(m_popupInterface);
855
856 return GetVListBoxComboPopup()->Append(item);
857 }
858
859 int wxOwnerDrawnComboBox::DoInsert(const wxString& item, unsigned int pos)
860 {
861 EnsurePopupControl();
862
863 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT), -1, wxT("can't insert into sorted list"));
864 wxCHECK_MSG(IsValidInsert(pos), -1, wxT("invalid index"));
865
866 GetVListBoxComboPopup()->Insert(item,pos);
867
868 return pos;
869 }
870
871 void wxOwnerDrawnComboBox::DoSetItemClientData(unsigned int n, void* clientData)
872 {
873 EnsurePopupControl();
874
875 GetVListBoxComboPopup()->SetItemClientData(n,clientData,m_clientDataItemsType);
876 }
877
878 void* wxOwnerDrawnComboBox::DoGetItemClientData(unsigned int n) const
879 {
880 if ( !m_popupInterface )
881 return NULL;
882
883 return GetVListBoxComboPopup()->GetItemClientData(n);
884 }
885
886 void wxOwnerDrawnComboBox::DoSetItemClientObject(unsigned int n, wxClientData* clientData)
887 {
888 DoSetItemClientData(n, (void*) clientData);
889 }
890
891 wxClientData* wxOwnerDrawnComboBox::DoGetItemClientObject(unsigned int n) const
892 {
893 return (wxClientData*) DoGetItemClientData(n);
894 }
895
896 // ----------------------------------------------------------------------------
897 // wxOwnerDrawnComboBox item drawing and measuring default implementations
898 // ----------------------------------------------------------------------------
899
900 void wxOwnerDrawnComboBox::OnDrawItem( wxDC& dc,
901 const wxRect& rect,
902 int item,
903 int flags ) const
904 {
905 if ( flags & wxODCB_PAINTING_CONTROL )
906 {
907 dc.DrawText( GetValue(),
908 rect.x + GetTextIndent(),
909 (rect.height-dc.GetCharHeight())/2 + rect.y );
910 }
911 else
912 {
913 dc.DrawText( GetVListBoxComboPopup()->GetString(item), rect.x + 2, rect.y );
914 }
915 }
916
917 wxCoord wxOwnerDrawnComboBox::OnMeasureItem( size_t WXUNUSED(item) ) const
918 {
919 return -1;
920 }
921
922 wxCoord wxOwnerDrawnComboBox::OnMeasureItemWidth( size_t WXUNUSED(item) ) const
923 {
924 return -1;
925 }
926
927 void wxOwnerDrawnComboBox::OnDrawBackground(wxDC& dc, const wxRect& rect, int item, int flags) const
928 {
929 // we need to render selected and current items differently
930 if ( GetVListBoxComboPopup()->IsCurrent((size_t)item) )
931 {
932 DrawFocusBackground(dc,
933 rect,
934 (flags&wxODCB_PAINTING_CONTROL?0:wxCONTROL_ISSUBMENU) |
935 wxCONTROL_SELECTED);
936 }
937 //else: do nothing for the normal items
938 }
939
940 #endif // wxUSE_ODCOMBOBOX