Added GetWidestItem() and GetWidestItemWidth() to wxVListBoxComboPopup
[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 void wxVListBoxComboPopup::CalcWidths()
520 {
521 bool doFindWidest = m_findWidest;
522
523 // Measure items with dirty width.
524 if ( m_widthsDirty )
525 {
526 unsigned int i;
527 unsigned int n = m_widths.GetCount();
528 int dirtyHandled = 0;
529 wxArrayInt& widths = m_widths;
530
531 // I think using wxDC::GetTextExtent is faster than
532 // wxWindow::GetTextExtent (assuming same dc is used
533 // for all calls, as we do here).
534 wxClientDC dc(m_combo);
535 dc.SetFont(m_useFont);
536
537 for ( i=0; i<n; i++ )
538 {
539 if ( widths[i] < 0 )
540 {
541 wxCoord x = OnMeasureItemWidth(i);
542
543 if ( x < 0 )
544 {
545 const wxString& text = m_strings[i];
546
547 // To make sure performance won't suck in extreme scenarios,
548 // we'll estimate length after some arbitrary number of items
549 // have been checked precily.
550 if ( dirtyHandled < 1024 )
551 {
552 wxCoord y;
553 dc.GetTextExtent(text, &x, &y, 0, 0);
554 x += 4;
555 }
556 else
557 {
558 x = text.length() * (dc.GetCharWidth()+1);
559 }
560 }
561
562 widths[i] = x;
563
564 if ( x >= m_widestWidth )
565 {
566 m_widestWidth = x;
567 m_widestItem = (int)i;
568 }
569 else if ( (int)i == m_widestItem )
570 {
571 // Width of previously widest item has been decreased, so
572 // we'll have to check all to find current widest item.
573 doFindWidest = true;
574 }
575
576 dirtyHandled++;
577 }
578 }
579
580 m_widthsDirty = false;
581 }
582
583 if ( doFindWidest )
584 {
585 unsigned int i;
586 unsigned int n = m_widths.GetCount();
587
588 int bestWidth = -1;
589 int bestIndex = -1;
590
591 for ( i=0; i<n; i++ )
592 {
593 int w = m_widths[i];
594 if ( w > bestWidth )
595 {
596 bestIndex = (int)i;
597 bestWidth = w;
598 }
599 }
600
601 m_widestWidth = bestWidth;
602 m_widestItem = bestIndex;
603
604 m_findWidest = false;
605 }
606 }
607
608 wxSize wxVListBoxComboPopup::GetAdjustedSize( int minWidth, int prefHeight, int maxHeight )
609 {
610 int height = 250;
611
612 if ( m_strings.GetCount() )
613 {
614 if ( prefHeight > 0 )
615 height = prefHeight;
616
617 if ( height > maxHeight )
618 height = maxHeight;
619
620 int totalHeight = GetTotalHeight(); // + 3;
621 if ( height >= totalHeight )
622 {
623 height = totalHeight;
624 }
625 else
626 {
627 // Adjust height to a multiple of the height of the first item
628 // NB: Calculations that take variable height into account
629 // are unnecessary.
630 int fih = GetLineHeight(0);
631 int shown = height/fih;
632 height = shown * fih;
633 }
634 }
635 else
636 height = 50;
637
638 CalcWidths();
639
640 // Take scrollbar into account in width calculations
641 int widestWidth = m_widestWidth + wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
642 return wxSize(minWidth > widestWidth ? minWidth : widestWidth,
643 height+2);
644 }
645
646 //void wxVListBoxComboPopup::Populate( int n, const wxString choices[] )
647 void wxVListBoxComboPopup::Populate( const wxArrayString& choices )
648 {
649 int i;
650
651 int n = choices.GetCount();
652
653 for ( i=0; i<n; i++ )
654 {
655 const wxString& item = choices.Item(i);
656 m_strings.Add(item);
657 }
658
659 m_widths.SetCount(n,-1);
660 m_widthsDirty = true;
661
662 if ( IsCreated() )
663 wxVListBox::SetItemCount(n);
664
665 // Sort the initial choices
666 if ( m_combo->GetWindowStyle() & wxCB_SORT )
667 m_strings.Sort();
668
669 // Find initial selection
670 wxString strValue = m_combo->GetValue();
671 if ( strValue.length() )
672 m_value = m_strings.Index(strValue);
673 }
674
675 // ----------------------------------------------------------------------------
676 // wxOwnerDrawnComboBox
677 // ----------------------------------------------------------------------------
678
679
680 BEGIN_EVENT_TABLE(wxOwnerDrawnComboBox, wxComboCtrl)
681 END_EVENT_TABLE()
682
683
684 IMPLEMENT_DYNAMIC_CLASS2(wxOwnerDrawnComboBox, wxComboCtrl, wxControlWithItems)
685
686 void wxOwnerDrawnComboBox::Init()
687 {
688 }
689
690 bool wxOwnerDrawnComboBox::Create(wxWindow *parent,
691 wxWindowID id,
692 const wxString& value,
693 const wxPoint& pos,
694 const wxSize& size,
695 long style,
696 const wxValidator& validator,
697 const wxString& name)
698 {
699 return wxComboCtrl::Create(parent,id,value,pos,size,style,validator,name);
700 }
701
702 wxOwnerDrawnComboBox::wxOwnerDrawnComboBox(wxWindow *parent,
703 wxWindowID id,
704 const wxString& value,
705 const wxPoint& pos,
706 const wxSize& size,
707 const wxArrayString& choices,
708 long style,
709 const wxValidator& validator,
710 const wxString& name)
711 : wxComboCtrl()
712 {
713 Init();
714
715 Create(parent,id,value,pos,size,choices,style, validator, name);
716 }
717
718 bool wxOwnerDrawnComboBox::Create(wxWindow *parent,
719 wxWindowID id,
720 const wxString& value,
721 const wxPoint& pos,
722 const wxSize& size,
723 const wxArrayString& choices,
724 long style,
725 const wxValidator& validator,
726 const wxString& name)
727 {
728 m_initChs = choices;
729 //wxCArrayString chs(choices);
730
731 //return Create(parent, id, value, pos, size, chs.GetCount(),
732 // chs.GetStrings(), style, validator, name);
733 return Create(parent, id, value, pos, size, 0,
734 NULL, style, validator, name);
735 }
736
737 bool wxOwnerDrawnComboBox::Create(wxWindow *parent,
738 wxWindowID id,
739 const wxString& value,
740 const wxPoint& pos,
741 const wxSize& size,
742 int n,
743 const wxString choices[],
744 long style,
745 const wxValidator& validator,
746 const wxString& name)
747 {
748
749 if ( !Create(parent, id, value, pos, size, style,
750 validator, name) )
751 {
752 return false;
753 }
754
755 int i;
756 for ( i=0; i<n; i++ )
757 m_initChs.Add(choices[i]);
758
759 return true;
760 }
761
762 wxOwnerDrawnComboBox::~wxOwnerDrawnComboBox()
763 {
764 if ( m_popupInterface )
765 GetVListBoxComboPopup()->ClearClientDatas();
766 }
767
768 void wxOwnerDrawnComboBox::DoSetPopupControl(wxComboPopup* popup)
769 {
770 if ( !popup )
771 {
772 popup = new wxVListBoxComboPopup();
773 }
774
775 wxComboCtrl::DoSetPopupControl(popup);
776
777 wxASSERT(popup);
778
779 // Add initial choices to the wxVListBox
780 if ( !GetVListBoxComboPopup()->GetCount() )
781 {
782 GetVListBoxComboPopup()->Populate(m_initChs);
783 m_initChs.Clear();
784 }
785 }
786
787 // ----------------------------------------------------------------------------
788 // wxOwnerDrawnComboBox item manipulation methods
789 // ----------------------------------------------------------------------------
790
791 void wxOwnerDrawnComboBox::Clear()
792 {
793 EnsurePopupControl();
794
795 GetVListBoxComboPopup()->Clear();
796
797 SetValue(wxEmptyString);
798 }
799
800 void wxOwnerDrawnComboBox::Delete(unsigned int n)
801 {
802 wxCHECK_RET( IsValid(n), _T("invalid index in wxOwnerDrawnComboBox::Delete") );
803
804 if ( GetSelection() == (int) n )
805 SetValue(wxEmptyString);
806
807 GetVListBoxComboPopup()->Delete(n);
808 }
809
810 unsigned int wxOwnerDrawnComboBox::GetCount() const
811 {
812 if ( !m_popupInterface )
813 return m_initChs.GetCount();
814
815 return GetVListBoxComboPopup()->GetCount();
816 }
817
818 wxString wxOwnerDrawnComboBox::GetString(unsigned int n) const
819 {
820 wxCHECK_MSG( IsValid(n), wxEmptyString, _T("invalid index in wxOwnerDrawnComboBox::GetString") );
821
822 if ( !m_popupInterface )
823 return m_initChs.Item(n);
824
825 return GetVListBoxComboPopup()->GetString(n);
826 }
827
828 void wxOwnerDrawnComboBox::SetString(unsigned int n, const wxString& s)
829 {
830 EnsurePopupControl();
831
832 wxCHECK_RET( IsValid(n), _T("invalid index in wxOwnerDrawnComboBox::SetString") );
833
834 GetVListBoxComboPopup()->SetString(n,s);
835 }
836
837 int wxOwnerDrawnComboBox::FindString(const wxString& s, bool bCase) const
838 {
839 if ( !m_popupInterface )
840 return m_initChs.Index(s, bCase);
841
842 return GetVListBoxComboPopup()->FindString(s, bCase);
843 }
844
845 void wxOwnerDrawnComboBox::Select(int n)
846 {
847 EnsurePopupControl();
848
849 wxCHECK_RET( (n == wxNOT_FOUND) || IsValid(n), _T("invalid index in wxOwnerDrawnComboBox::Select") );
850
851 GetVListBoxComboPopup()->SetSelection(n);
852
853 wxString str;
854 if ( n >= 0 )
855 str = GetVListBoxComboPopup()->GetString(n);
856
857 // Refresh text portion in control
858 if ( m_text )
859 m_text->SetValue( str );
860 else
861 m_valueString = str;
862
863 Refresh();
864 }
865
866 int wxOwnerDrawnComboBox::GetSelection() const
867 {
868 if ( !m_popupInterface )
869 return m_initChs.Index(m_valueString);
870
871 return GetVListBoxComboPopup()->GetSelection();
872 }
873
874 int wxOwnerDrawnComboBox::DoAppend(const wxString& item)
875 {
876 EnsurePopupControl();
877 wxASSERT(m_popupInterface);
878
879 return GetVListBoxComboPopup()->Append(item);
880 }
881
882 int wxOwnerDrawnComboBox::DoInsert(const wxString& item, unsigned int pos)
883 {
884 EnsurePopupControl();
885
886 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT), -1, wxT("can't insert into sorted list"));
887 wxCHECK_MSG(IsValidInsert(pos), -1, wxT("invalid index"));
888
889 GetVListBoxComboPopup()->Insert(item,pos);
890
891 return pos;
892 }
893
894 void wxOwnerDrawnComboBox::DoSetItemClientData(unsigned int n, void* clientData)
895 {
896 EnsurePopupControl();
897
898 GetVListBoxComboPopup()->SetItemClientData(n,clientData,m_clientDataItemsType);
899 }
900
901 void* wxOwnerDrawnComboBox::DoGetItemClientData(unsigned int n) const
902 {
903 if ( !m_popupInterface )
904 return NULL;
905
906 return GetVListBoxComboPopup()->GetItemClientData(n);
907 }
908
909 void wxOwnerDrawnComboBox::DoSetItemClientObject(unsigned int n, wxClientData* clientData)
910 {
911 DoSetItemClientData(n, (void*) clientData);
912 }
913
914 wxClientData* wxOwnerDrawnComboBox::DoGetItemClientObject(unsigned int n) const
915 {
916 return (wxClientData*) DoGetItemClientData(n);
917 }
918
919 // ----------------------------------------------------------------------------
920 // wxOwnerDrawnComboBox item drawing and measuring default implementations
921 // ----------------------------------------------------------------------------
922
923 void wxOwnerDrawnComboBox::OnDrawItem( wxDC& dc,
924 const wxRect& rect,
925 int item,
926 int flags ) const
927 {
928 if ( flags & wxODCB_PAINTING_CONTROL )
929 {
930 dc.DrawText( GetValue(),
931 rect.x + GetTextIndent(),
932 (rect.height-dc.GetCharHeight())/2 + rect.y );
933 }
934 else
935 {
936 dc.DrawText( GetVListBoxComboPopup()->GetString(item), rect.x + 2, rect.y );
937 }
938 }
939
940 wxCoord wxOwnerDrawnComboBox::OnMeasureItem( size_t WXUNUSED(item) ) const
941 {
942 return -1;
943 }
944
945 wxCoord wxOwnerDrawnComboBox::OnMeasureItemWidth( size_t WXUNUSED(item) ) const
946 {
947 return -1;
948 }
949
950 void wxOwnerDrawnComboBox::OnDrawBackground(wxDC& dc, const wxRect& rect, int item, int flags) const
951 {
952 // we need to render selected and current items differently
953 if ( GetVListBoxComboPopup()->IsCurrent((size_t)item) )
954 {
955 DrawFocusBackground(dc,
956 rect,
957 (flags&wxODCB_PAINTING_CONTROL?0:wxCONTROL_ISSUBMENU) |
958 wxCONTROL_SELECTED);
959 }
960 //else: do nothing for the normal items
961 }
962
963 #endif // wxUSE_ODCOMBOBOX