1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/odcombo.cpp
3 // Purpose: wxOwnerDrawnComboBox, wxVListBoxComboPopup
4 // Author: Jaakko Salli
6 // Created: Apr-30-2006
8 // Copyright: (c) 2005 Jaakko Salli
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #include "wx/wxprec.h"
28 #include "wx/odcombo.h"
32 #include "wx/combobox.h"
33 #include "wx/dcclient.h"
34 #include "wx/settings.h"
35 #include "wx/dialog.h"
36 #include "wx/textctrl.h"
41 // ============================================================================
43 // ============================================================================
45 // time in milliseconds before partial completion buffer drops
46 #define wxODCB_PARTIAL_COMPLETION_TIME 1000
48 // ----------------------------------------------------------------------------
49 // wxVListBoxComboPopup is a wxVListBox customized to act as a popup control
51 // ----------------------------------------------------------------------------
54 BEGIN_EVENT_TABLE(wxVListBoxComboPopup
, wxVListBox
)
55 EVT_MOTION(wxVListBoxComboPopup::OnMouseMove
)
56 EVT_KEY_DOWN(wxVListBoxComboPopup::OnKey
)
57 EVT_LEFT_UP(wxVListBoxComboPopup::OnLeftClick
)
61 void wxVListBoxComboPopup::Init()
65 m_widthsDirty
= false;
70 m_clientDataItemsType
= wxClientData_None
;
71 m_partialCompletionString
= wxEmptyString
;
74 bool wxVListBoxComboPopup::Create(wxWindow
* parent
)
76 if ( !wxVListBox::Create(parent
,
80 wxBORDER_SIMPLE
| wxLB_INT_HEIGHT
| wxWANTS_CHARS
) )
83 m_useFont
= m_combo
->GetFont();
85 wxVListBox::SetItemCount(m_strings
.GetCount());
87 // TODO: Move this to SetFont
88 m_itemHeight
= GetCharHeight() + 0;
93 wxVListBoxComboPopup::~wxVListBoxComboPopup()
98 bool wxVListBoxComboPopup::LazyCreate()
100 // NB: There is a bug with wxVListBox that can be avoided by creating
101 // it later (bug causes empty space to be shown if initial selection
102 // is at the end of a list longer than the control can show at once).
106 // paint the control itself
107 void wxVListBoxComboPopup::PaintComboControl( wxDC
& dc
, const wxRect
& rect
)
109 if ( !(m_combo
->GetWindowStyle() & wxODCB_STD_CONTROL_PAINT
) )
111 int flags
= wxODCB_PAINTING_CONTROL
;
113 if ( m_combo
->ShouldDrawFocus() )
114 flags
|= wxODCB_PAINTING_SELECTED
;
116 OnDrawBg(dc
, rect
, m_value
, flags
);
120 OnDrawItem(dc
,rect
,m_value
,flags
);
125 wxComboPopup::PaintComboControl(dc
,rect
);
128 void wxVListBoxComboPopup::OnDrawItem(wxDC
& dc
, const wxRect
& rect
, size_t n
) const
130 // TODO: Maybe this code could be moved to wxVListBox::OnPaint?
131 dc
.SetFont(m_useFont
);
135 // Set correct text colour for selected items
136 if ( wxVListBox::GetSelection() == (int) n
)
138 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) );
139 flags
|= wxODCB_PAINTING_SELECTED
;
143 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
) );
146 OnDrawItem(dc
,rect
,(int)n
,flags
);
149 wxCoord
wxVListBoxComboPopup::OnMeasureItem(size_t n
) const
151 wxOwnerDrawnComboBox
* combo
= (wxOwnerDrawnComboBox
*) m_combo
;
153 wxASSERT_MSG( combo
->IsKindOf(CLASSINFO(wxOwnerDrawnComboBox
)),
154 wxT("you must subclass wxVListBoxComboPopup for drawing and measuring methods") );
156 wxCoord h
= combo
->OnMeasureItem(n
);
162 wxCoord
wxVListBoxComboPopup::OnMeasureItemWidth(size_t n
) const
164 wxOwnerDrawnComboBox
* combo
= (wxOwnerDrawnComboBox
*) m_combo
;
166 wxASSERT_MSG( combo
->IsKindOf(CLASSINFO(wxOwnerDrawnComboBox
)),
167 wxT("you must subclass wxVListBoxComboPopup for drawing and measuring methods") );
169 return combo
->OnMeasureItemWidth(n
);
172 void wxVListBoxComboPopup::OnDrawBg( wxDC
& dc
,
177 wxOwnerDrawnComboBox
* combo
= (wxOwnerDrawnComboBox
*) m_combo
;
179 wxASSERT_MSG( combo
->IsKindOf(CLASSINFO(wxOwnerDrawnComboBox
)),
180 wxT("you must subclass wxVListBoxComboPopup for drawing and measuring methods") );
182 if ( IsCurrent((size_t)item
) && !(flags
& wxODCB_PAINTING_CONTROL
) )
183 flags
|= wxODCB_PAINTING_SELECTED
;
185 combo
->OnDrawBackground(dc
,rect
,item
,flags
);
188 void wxVListBoxComboPopup::OnDrawBackground(wxDC
& dc
, const wxRect
& rect
, size_t n
) const
190 OnDrawBg(dc
,rect
,(int)n
,0);
193 // This is called from wxVListBoxComboPopup::OnDrawItem, with text colour and font prepared
194 void wxVListBoxComboPopup::OnDrawItem( wxDC
& dc
, const wxRect
& rect
, int item
, int flags
) const
196 wxOwnerDrawnComboBox
* combo
= (wxOwnerDrawnComboBox
*) m_combo
;
198 wxASSERT_MSG( combo
->IsKindOf(CLASSINFO(wxOwnerDrawnComboBox
)),
199 wxT("you must subclass wxVListBoxComboPopup for drawing and measuring methods") );
201 combo
->OnDrawItem(dc
,rect
,item
,flags
);
204 void wxVListBoxComboPopup::DismissWithEvent()
206 StopPartialCompletion();
208 int selection
= wxVListBox::GetSelection();
212 if ( selection
!= wxNOT_FOUND
)
213 m_stringValue
= m_strings
[selection
];
215 m_stringValue
= wxEmptyString
;
217 if ( m_stringValue
!= m_combo
->GetValue() )
218 m_combo
->SetValueWithEvent(m_stringValue
);
222 SendComboBoxEvent(selection
);
225 void wxVListBoxComboPopup::SendComboBoxEvent( int selection
)
227 wxCommandEvent
evt(wxEVT_COMMAND_COMBOBOX_SELECTED
,m_combo
->GetId());
229 evt
.SetEventObject(m_combo
);
231 evt
.SetInt(selection
);
233 // Set client data, if any
234 if ( selection
>= 0 && (int)m_clientDatas
.GetCount() > selection
)
236 void* clientData
= m_clientDatas
[selection
];
237 if ( m_clientDataItemsType
== wxClientData_Object
)
238 evt
.SetClientObject((wxClientData
*)clientData
);
240 evt
.SetClientData(clientData
);
243 m_combo
->GetEventHandler()->AddPendingEvent(evt
);
246 // returns true if key was consumed
247 bool wxVListBoxComboPopup::HandleKey( int keycode
, bool saturate
, wxChar unicode
)
249 const int itemCount
= GetCount();
251 // keys do nothing in the empty control and returning immediately avoids
252 // using invalid indices below
257 int comboStyle
= m_combo
->GetWindowStyle();
259 // this is the character equivalent of the code
261 if ((keycode
>= WXK_SPACE
) && (keycode
<=255) && (keycode
!= WXK_DELETE
) && wxIsprint(keycode
))
263 keychar
= (wxChar
)keycode
;
270 if ( keycode
== WXK_DOWN
|| keycode
== WXK_RIGHT
)
273 StopPartialCompletion();
275 else if ( keycode
== WXK_UP
|| keycode
== WXK_LEFT
)
278 StopPartialCompletion();
280 else if ( keycode
== WXK_PAGEDOWN
)
283 StopPartialCompletion();
285 else if ( keycode
== WXK_PAGEUP
)
288 StopPartialCompletion();
290 else if ( comboStyle
& wxCB_READONLY
)
292 // Try partial completion
294 // find the new partial completion string
296 if (m_partialCompletionTimer
.IsRunning())
297 m_partialCompletionString
+=wxString(keychar
);
299 #endif // wxUSE_TIMER
300 m_partialCompletionString
=wxString(keychar
);
302 // now search through the values to see if this is found
304 unsigned int length
=m_partialCompletionString
.length();
306 for (i
=0; i
<itemCount
; i
++)
308 wxString item
=GetString(i
);
309 if (( item
.length() >= length
) && (! m_partialCompletionString
.CmpNoCase(item
.Left(length
))))
318 StopPartialCompletion();
320 return true; // to stop the first value being set
326 m_partialCompletionTimer
.Start(wxODCB_PARTIAL_COMPLETION_TIME
, true);
327 #endif // wxUSE_TIMER
335 if ( value
>= itemCount
)
336 value
= itemCount
- 1;
337 else if ( value
< 0 )
342 if ( value
>= itemCount
)
344 else if ( value
< 0 )
348 if ( value
== m_value
)
349 // Even if value was same, don't skip the event
350 // (good for consistency)
356 m_combo
->SetValue(m_strings
[value
]);
358 SendComboBoxEvent(m_value
);
363 // stop partial completion
364 void wxVListBoxComboPopup::StopPartialCompletion()
366 m_partialCompletionString
= wxEmptyString
;
368 m_partialCompletionTimer
.Stop();
369 #endif // wxUSE_TIMER
372 void wxVListBoxComboPopup::OnComboDoubleClick()
374 // Cycle on dclick (disable saturation to allow true cycling).
375 if ( !::wxGetKeyState(WXK_SHIFT
) )
376 HandleKey(WXK_DOWN
,false);
378 HandleKey(WXK_UP
,false);
381 void wxVListBoxComboPopup::OnComboKeyEvent( wxKeyEvent
& event
)
383 // Saturated key movement on
384 if ( !HandleKey(event
.GetKeyCode(),true,
386 event
.GetUnicodeKey()
394 void wxVListBoxComboPopup::OnPopup()
396 // *must* set value after size is set (this is because of a vlbox bug)
397 wxVListBox::SetSelection(m_value
);
400 void wxVListBoxComboPopup::OnMouseMove(wxMouseEvent
& event
)
404 // Move selection to cursor if it is inside the popup
406 int y
= event
.GetPosition().y
;
407 int fromBottom
= GetClientSize().y
- y
;
409 // Since in any case we need to find out if the last item is only
410 // partially visible, we might just as well replicate the HitTest
412 const size_t lineMax
= GetVisibleEnd();
413 for ( size_t line
= GetVisibleBegin(); line
< lineMax
; line
++ )
415 y
-= OnGetRowHeight(line
);
418 // Only change selection if item is fully visible
419 if ( (y
+ fromBottom
) >= 0 )
421 wxVListBox::SetSelection((int)line
);
428 void wxVListBoxComboPopup::OnLeftClick(wxMouseEvent
& WXUNUSED(event
))
433 void wxVListBoxComboPopup::OnKey(wxKeyEvent
& event
)
435 // Hide popup if certain key or key combination was pressed
436 if ( m_combo
->IsKeyPopupToggle(event
) )
438 StopPartialCompletion();
441 else if ( event
.AltDown() )
443 // On both wxGTK and wxMSW, pressing Alt down seems to
444 // completely freeze things in popup (ie. arrow keys and
445 // enter won't work).
448 // Select item if ENTER is pressed
449 else if ( event
.GetKeyCode() == WXK_RETURN
|| event
.GetKeyCode() == WXK_NUMPAD_ENTER
)
455 int comboStyle
= m_combo
->GetWindowStyle();
456 int keycode
= event
.GetKeyCode();
457 // Process partial completion key codes here, but not the arrow keys as the base class will do that for us
458 if ((comboStyle
& wxCB_READONLY
) &&
459 (keycode
>= WXK_SPACE
) && (keycode
<=255) && (keycode
!= WXK_DELETE
) && wxIsprint(keycode
))
461 OnComboKeyEvent(event
);
462 SetSelection(m_value
); // ensure the highlight bar moves
469 void wxVListBoxComboPopup::Insert( const wxString
& item
, int pos
)
471 // Need to change selection?
473 if ( !(m_combo
->GetWindowStyle() & wxCB_READONLY
) &&
474 m_combo
->GetValue() == item
)
479 m_strings
.Insert(item
,pos
);
480 m_clientDatas
.Insert(NULL
, pos
);
482 m_widths
.Insert(-1,pos
);
483 m_widthsDirty
= true;
486 wxVListBox::SetItemCount( wxVListBox::GetItemCount()+1 );
489 int wxVListBoxComboPopup::Append(const wxString
& item
)
491 int pos
= (int)m_strings
.GetCount();
493 if ( m_combo
->GetWindowStyle() & wxCB_SORT
)
496 // TODO: Could be optimized with binary search
497 wxArrayString strings
= m_strings
;
500 for ( i
=0; i
<strings
.GetCount(); i
++ )
502 if ( item
.Cmp(strings
.Item(i
)) < 0 )
515 void wxVListBoxComboPopup::Clear()
527 m_value
= wxNOT_FOUND
;
530 wxVListBox::SetItemCount(0);
533 void wxVListBoxComboPopup::ClearClientDatas()
535 if ( m_clientDataItemsType
== wxClientData_Object
)
538 for ( i
=0; i
<m_clientDatas
.GetCount(); i
++ )
539 delete (wxClientData
*) m_clientDatas
[i
];
542 m_clientDatas
.Empty();
545 void wxVListBoxComboPopup::SetItemClientData( unsigned int n
,
547 wxClientDataType clientDataItemsType
)
549 // It should be sufficient to update this variable only here
550 m_clientDataItemsType
= clientDataItemsType
;
552 m_clientDatas
[n
] = clientData
;
557 void* wxVListBoxComboPopup::GetItemClientData(unsigned int n
) const
559 if ( m_clientDatas
.GetCount() > n
)
560 return m_clientDatas
[n
];
565 void wxVListBoxComboPopup::Delete( unsigned int item
)
567 // Remove client data, if set
568 if ( m_clientDatas
.GetCount() )
570 if ( m_clientDataItemsType
== wxClientData_Object
)
571 delete (wxClientData
*) m_clientDatas
[item
];
573 m_clientDatas
.RemoveAt(item
);
576 m_strings
.RemoveAt(item
);
577 m_widths
.RemoveAt(item
);
579 if ( (int)item
== m_widestItem
)
582 int sel
= GetSelection();
585 wxVListBox::SetItemCount( wxVListBox::GetItemCount()-1 );
588 if ( (int)item
< sel
)
590 else if ( (int)item
== sel
)
591 SetSelection(wxNOT_FOUND
);
594 int wxVListBoxComboPopup::FindString(const wxString
& s
, bool bCase
) const
596 return m_strings
.Index(s
, bCase
);
599 unsigned int wxVListBoxComboPopup::GetCount() const
601 return m_strings
.GetCount();
604 wxString
wxVListBoxComboPopup::GetString( int item
) const
606 return m_strings
[item
];
609 void wxVListBoxComboPopup::SetString( int item
, const wxString
& str
)
611 m_strings
[item
] = str
;
612 ItemWidthChanged(item
);
615 wxString
wxVListBoxComboPopup::GetStringValue() const
617 return m_stringValue
;
620 void wxVListBoxComboPopup::SetSelection( int item
)
622 wxCHECK_RET( item
== wxNOT_FOUND
|| ((unsigned int)item
< GetCount()),
623 wxT("invalid index in wxVListBoxComboPopup::SetSelection") );
628 m_stringValue
= m_strings
[item
];
630 m_stringValue
= wxEmptyString
;
633 wxVListBox::SetSelection(item
);
636 int wxVListBoxComboPopup::GetSelection() const
641 void wxVListBoxComboPopup::SetStringValue( const wxString
& value
)
643 int index
= m_strings
.Index(value
);
645 m_stringValue
= value
;
647 if ( index
>= 0 && index
< (int)wxVListBox::GetItemCount() )
649 wxVListBox::SetSelection(index
);
654 void wxVListBoxComboPopup::CalcWidths()
656 bool doFindWidest
= m_findWidest
;
658 // Measure items with dirty width.
662 unsigned int n
= m_widths
.GetCount();
663 int dirtyHandled
= 0;
664 wxArrayInt
& widths
= m_widths
;
666 // I think using wxDC::GetTextExtent is faster than
667 // wxWindow::GetTextExtent (assuming same dc is used
668 // for all calls, as we do here).
669 wxClientDC
dc(m_combo
);
670 dc
.SetFont(m_useFont
);
672 for ( i
=0; i
<n
; i
++ )
676 wxCoord x
= OnMeasureItemWidth(i
);
680 const wxString
& text
= m_strings
[i
];
682 // To make sure performance won't suck in extreme scenarios,
683 // we'll estimate length after some arbitrary number of items
684 // have been checked precily.
685 if ( dirtyHandled
< 1024 )
688 dc
.GetTextExtent(text
, &x
, &y
, 0, 0);
693 x
= text
.length() * (dc
.GetCharWidth()+1);
699 if ( x
>= m_widestWidth
)
702 m_widestItem
= (int)i
;
704 else if ( (int)i
== m_widestItem
)
706 // Width of previously widest item has been decreased, so
707 // we'll have to check all to find current widest item.
715 m_widthsDirty
= false;
721 unsigned int n
= m_widths
.GetCount();
726 for ( i
=0; i
<n
; i
++ )
736 m_widestWidth
= bestWidth
;
737 m_widestItem
= bestIndex
;
739 m_findWidest
= false;
743 wxSize
wxVListBoxComboPopup::GetAdjustedSize( int minWidth
, int prefHeight
, int maxHeight
)
747 maxHeight
-= 2; // Must take borders into account
749 if ( m_strings
.GetCount() )
751 if ( prefHeight
> 0 )
754 if ( height
> maxHeight
)
757 int totalHeight
= GetTotalHeight(); // + 3;
758 if ( height
>= totalHeight
)
760 height
= totalHeight
;
764 // Adjust height to a multiple of the height of the first item
765 // NB: Calculations that take variable height into account
767 int fih
= GetLineHeight(0);
768 height
-= height
% fih
;
776 // Take scrollbar into account in width calculations
777 int widestWidth
= m_widestWidth
+ wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
778 return wxSize(minWidth
> widestWidth
? minWidth
: widestWidth
,
782 //void wxVListBoxComboPopup::Populate( int n, const wxString choices[] )
783 void wxVListBoxComboPopup::Populate( const wxArrayString
& choices
)
787 int n
= choices
.GetCount();
789 for ( i
=0; i
<n
; i
++ )
791 const wxString
& item
= choices
.Item(i
);
795 m_widths
.SetCount(n
,-1);
796 m_widthsDirty
= true;
799 wxVListBox::SetItemCount(n
);
801 // Sort the initial choices
802 if ( m_combo
->GetWindowStyle() & wxCB_SORT
)
805 // Find initial selection
806 wxString strValue
= m_combo
->GetValue();
807 if ( strValue
.length() )
808 m_value
= m_strings
.Index(strValue
);
811 // ----------------------------------------------------------------------------
812 // wxOwnerDrawnComboBox
813 // ----------------------------------------------------------------------------
816 BEGIN_EVENT_TABLE(wxOwnerDrawnComboBox
, wxComboCtrl
)
820 #if wxUSE_EXTENDED_RTTI
821 IMPLEMENT_DYNAMIC_CLASS2_XTI(wxOwnerDrawnComboBox
, wxComboCtrl
, wxControlWithItems
, "wx/odcombo.h")
823 wxBEGIN_PROPERTIES_TABLE(wxOwnerDrawnComboBox
)
824 wxEND_PROPERTIES_TABLE()
826 wxBEGIN_HANDLERS_TABLE(wxOwnerDrawnComboBox
)
827 wxEND_HANDLERS_TABLE()
829 wxCONSTRUCTOR_5( wxOwnerDrawnComboBox
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, Value
, wxPoint
, Position
, wxSize
, Size
)
831 IMPLEMENT_DYNAMIC_CLASS2(wxOwnerDrawnComboBox
, wxComboCtrl
, wxControlWithItems
)
834 void wxOwnerDrawnComboBox::Init()
838 bool wxOwnerDrawnComboBox::Create(wxWindow
*parent
,
840 const wxString
& value
,
844 const wxValidator
& validator
,
845 const wxString
& name
)
847 return wxComboCtrl::Create(parent
,id
,value
,pos
,size
,style
,validator
,name
);
850 wxOwnerDrawnComboBox::wxOwnerDrawnComboBox(wxWindow
*parent
,
852 const wxString
& value
,
855 const wxArrayString
& choices
,
857 const wxValidator
& validator
,
858 const wxString
& name
)
863 Create(parent
,id
,value
,pos
,size
,choices
,style
, validator
, name
);
866 bool wxOwnerDrawnComboBox::Create(wxWindow
*parent
,
868 const wxString
& value
,
871 const wxArrayString
& choices
,
873 const wxValidator
& validator
,
874 const wxString
& name
)
877 //wxCArrayString chs(choices);
879 //return Create(parent, id, value, pos, size, chs.GetCount(),
880 // chs.GetStrings(), style, validator, name);
881 return Create(parent
, id
, value
, pos
, size
, 0,
882 NULL
, style
, validator
, name
);
885 bool wxOwnerDrawnComboBox::Create(wxWindow
*parent
,
887 const wxString
& value
,
891 const wxString choices
[],
893 const wxValidator
& validator
,
894 const wxString
& name
)
897 if ( !Create(parent
, id
, value
, pos
, size
, style
,
904 for ( i
=0; i
<n
; i
++ )
905 m_initChs
.Add(choices
[i
]);
910 wxOwnerDrawnComboBox::~wxOwnerDrawnComboBox()
912 if ( m_popupInterface
)
913 GetVListBoxComboPopup()->ClearClientDatas();
916 void wxOwnerDrawnComboBox::DoSetPopupControl(wxComboPopup
* popup
)
920 popup
= new wxVListBoxComboPopup();
923 wxComboCtrl::DoSetPopupControl(popup
);
927 // Add initial choices to the wxVListBox
928 if ( !GetVListBoxComboPopup()->GetCount() )
930 GetVListBoxComboPopup()->Populate(m_initChs
);
935 // ----------------------------------------------------------------------------
936 // wxOwnerDrawnComboBox item manipulation methods
937 // ----------------------------------------------------------------------------
939 void wxOwnerDrawnComboBox::DoClear()
941 EnsurePopupControl();
943 GetVListBoxComboPopup()->Clear();
945 SetValue(wxEmptyString
);
948 void wxOwnerDrawnComboBox::DoDeleteOneItem(unsigned int n
)
950 wxCHECK_RET( IsValid(n
), _T("invalid index in wxOwnerDrawnComboBox::Delete") );
952 if ( GetSelection() == (int) n
)
953 SetValue(wxEmptyString
);
955 GetVListBoxComboPopup()->Delete(n
);
958 unsigned int wxOwnerDrawnComboBox::GetCount() const
960 if ( !m_popupInterface
)
961 return m_initChs
.GetCount();
963 return GetVListBoxComboPopup()->GetCount();
966 wxString
wxOwnerDrawnComboBox::GetString(unsigned int n
) const
968 wxCHECK_MSG( IsValid(n
), wxEmptyString
, _T("invalid index in wxOwnerDrawnComboBox::GetString") );
970 if ( !m_popupInterface
)
971 return m_initChs
.Item(n
);
973 return GetVListBoxComboPopup()->GetString(n
);
976 void wxOwnerDrawnComboBox::SetString(unsigned int n
, const wxString
& s
)
978 EnsurePopupControl();
980 wxCHECK_RET( IsValid(n
), _T("invalid index in wxOwnerDrawnComboBox::SetString") );
982 GetVListBoxComboPopup()->SetString(n
,s
);
985 int wxOwnerDrawnComboBox::FindString(const wxString
& s
, bool bCase
) const
987 if ( !m_popupInterface
)
988 return m_initChs
.Index(s
, bCase
);
990 return GetVListBoxComboPopup()->FindString(s
, bCase
);
993 void wxOwnerDrawnComboBox::Select(int n
)
995 EnsurePopupControl();
997 wxCHECK_RET( (n
== wxNOT_FOUND
) || IsValid(n
), _T("invalid index in wxOwnerDrawnComboBox::Select") );
999 GetVListBoxComboPopup()->SetSelection(n
);
1003 str
= GetVListBoxComboPopup()->GetString(n
);
1005 // Refresh text portion in control
1007 m_text
->SetValue( str
);
1009 m_valueString
= str
;
1014 int wxOwnerDrawnComboBox::GetSelection() const
1016 if ( !m_popupInterface
)
1017 return m_initChs
.Index(m_valueString
);
1019 return GetVListBoxComboPopup()->GetSelection();
1022 int wxOwnerDrawnComboBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
1025 wxClientDataType type
)
1027 EnsurePopupControl();
1029 const unsigned int count
= items
.GetCount();
1031 if ( HasFlag(wxCB_SORT
) )
1035 for ( unsigned int i
= 0; i
< count
; ++i
)
1037 int n
= GetVListBoxComboPopup()->Append(items
[i
]);
1038 AssignNewItemClientData(n
, clientData
, i
, type
);
1045 for ( unsigned int i
= 0; i
< count
; ++i
, ++pos
)
1047 GetVListBoxComboPopup()->Insert(items
[i
], pos
);
1048 AssignNewItemClientData(pos
, clientData
, i
, type
);
1055 void wxOwnerDrawnComboBox::DoSetItemClientData(unsigned int n
, void* clientData
)
1057 EnsurePopupControl();
1059 GetVListBoxComboPopup()->SetItemClientData(n
, clientData
,
1060 GetClientDataType());
1063 void* wxOwnerDrawnComboBox::DoGetItemClientData(unsigned int n
) const
1065 if ( !m_popupInterface
)
1068 return GetVListBoxComboPopup()->GetItemClientData(n
);
1071 // ----------------------------------------------------------------------------
1072 // wxOwnerDrawnComboBox item drawing and measuring default implementations
1073 // ----------------------------------------------------------------------------
1075 void wxOwnerDrawnComboBox::OnDrawItem( wxDC
& dc
,
1080 if ( flags
& wxODCB_PAINTING_CONTROL
)
1082 dc
.DrawText( GetValue(),
1083 rect
.x
+ GetTextIndent(),
1084 (rect
.height
-dc
.GetCharHeight())/2 + rect
.y
);
1088 dc
.DrawText( GetVListBoxComboPopup()->GetString(item
), rect
.x
+ 2, rect
.y
);
1092 wxCoord
wxOwnerDrawnComboBox::OnMeasureItem( size_t WXUNUSED(item
) ) const
1097 wxCoord
wxOwnerDrawnComboBox::OnMeasureItemWidth( size_t WXUNUSED(item
) ) const
1102 void wxOwnerDrawnComboBox::OnDrawBackground(wxDC
& dc
,
1107 // We need only to explicitly draw background for items
1108 // that should have selected background. Also, call PrepareBackground
1109 // always when painting the control so that clipping is done properly.
1111 if ( (flags
& wxODCB_PAINTING_SELECTED
) ||
1112 ((flags
& wxODCB_PAINTING_CONTROL
) && HasFlag(wxCB_READONLY
)) )
1114 int bgFlags
= wxCONTROL_SELECTED
;
1116 if ( !(flags
& wxODCB_PAINTING_CONTROL
) )
1117 bgFlags
|= wxCONTROL_ISSUBMENU
;
1119 PrepareBackground(dc
, rect
, bgFlags
);
1123 #endif // wxUSE_ODCOMBOBOX