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_START
)
265 if ( wxIsprint(unicode
) )
268 else if ( wxIsprint(keycode
) )
270 keychar
= (wxChar
) keycode
;
274 if ( keycode
== WXK_DOWN
|| keycode
== WXK_NUMPAD_DOWN
|| keycode
== WXK_RIGHT
)
277 StopPartialCompletion();
279 else if ( keycode
== WXK_UP
|| keycode
== WXK_NUMPAD_UP
|| keycode
== WXK_LEFT
)
282 StopPartialCompletion();
284 else if ( keycode
== WXK_PAGEDOWN
|| keycode
== WXK_NUMPAD_PAGEDOWN
)
287 StopPartialCompletion();
289 else if ( keycode
== WXK_PAGEUP
|| keycode
== WXK_NUMPAD_PAGEUP
)
292 StopPartialCompletion();
294 else if ( keycode
== WXK_HOME
|| keycode
== WXK_NUMPAD_HOME
)
297 StopPartialCompletion();
299 else if ( keycode
== WXK_END
|| keycode
== WXK_NUMPAD_END
)
302 StopPartialCompletion();
304 else if ( keychar
&& (comboStyle
& wxCB_READONLY
) )
306 // Try partial completion
308 // find the new partial completion string
310 if (m_partialCompletionTimer
.IsRunning())
311 m_partialCompletionString
+=wxString(keychar
);
313 #endif // wxUSE_TIMER
314 m_partialCompletionString
=wxString(keychar
);
316 // now search through the values to see if this is found
318 unsigned int length
=m_partialCompletionString
.length();
320 for (i
=0; i
<itemCount
; i
++)
322 wxString item
=GetString(i
);
323 if (( item
.length() >= length
) && (! m_partialCompletionString
.CmpNoCase(item
.Left(length
))))
332 StopPartialCompletion();
334 return true; // to stop the first value being set
340 m_partialCompletionTimer
.Start(wxODCB_PARTIAL_COMPLETION_TIME
, true);
341 #endif // wxUSE_TIMER
349 if ( value
>= itemCount
)
350 value
= itemCount
- 1;
351 else if ( value
< 0 )
356 if ( value
>= itemCount
)
358 else if ( value
< 0 )
362 if ( value
== m_value
)
363 // Even if value was same, don't skip the event
364 // (good for consistency)
370 m_combo
->SetValue(m_strings
[value
]);
372 SendComboBoxEvent(m_value
);
377 // stop partial completion
378 void wxVListBoxComboPopup::StopPartialCompletion()
380 m_partialCompletionString
= wxEmptyString
;
382 m_partialCompletionTimer
.Stop();
383 #endif // wxUSE_TIMER
386 void wxVListBoxComboPopup::OnComboDoubleClick()
388 // Cycle on dclick (disable saturation to allow true cycling).
389 if ( !::wxGetKeyState(WXK_SHIFT
) )
390 HandleKey(WXK_DOWN
,false);
392 HandleKey(WXK_UP
,false);
395 void wxVListBoxComboPopup::OnComboKeyEvent( wxKeyEvent
& event
)
397 // Saturated key movement on
398 if ( !HandleKey(event
.GetKeyCode(),true,
400 event
.GetUnicodeKey()
408 void wxVListBoxComboPopup::OnPopup()
410 // *must* set value after size is set (this is because of a vlbox bug)
411 wxVListBox::SetSelection(m_value
);
414 void wxVListBoxComboPopup::OnMouseMove(wxMouseEvent
& event
)
418 // Move selection to cursor if it is inside the popup
420 int y
= event
.GetPosition().y
;
421 int fromBottom
= GetClientSize().y
- y
;
423 // Since in any case we need to find out if the last item is only
424 // partially visible, we might just as well replicate the HitTest
426 const size_t lineMax
= GetVisibleEnd();
427 for ( size_t line
= GetVisibleBegin(); line
< lineMax
; line
++ )
429 y
-= OnGetRowHeight(line
);
432 // Only change selection if item is fully visible
433 if ( (y
+ fromBottom
) >= 0 )
435 wxVListBox::SetSelection((int)line
);
442 void wxVListBoxComboPopup::OnLeftClick(wxMouseEvent
& WXUNUSED(event
))
447 void wxVListBoxComboPopup::OnKey(wxKeyEvent
& event
)
449 // Hide popup if certain key or key combination was pressed
450 if ( m_combo
->IsKeyPopupToggle(event
) )
452 StopPartialCompletion();
455 else if ( event
.AltDown() )
457 // On both wxGTK and wxMSW, pressing Alt down seems to
458 // completely freeze things in popup (ie. arrow keys and
459 // enter won't work).
462 // Select item if ENTER is pressed
463 else if ( event
.GetKeyCode() == WXK_RETURN
|| event
.GetKeyCode() == WXK_NUMPAD_ENTER
)
469 int comboStyle
= m_combo
->GetWindowStyle();
470 int keycode
= event
.GetKeyCode();
471 // Process partial completion key codes here, but not the arrow keys as the base class will do that for us
472 if ((comboStyle
& wxCB_READONLY
) &&
473 (keycode
>= WXK_SPACE
) && (keycode
<=255) && (keycode
!= WXK_DELETE
) && wxIsprint(keycode
))
475 OnComboKeyEvent(event
);
476 SetSelection(m_value
); // ensure the highlight bar moves
483 void wxVListBoxComboPopup::Insert( const wxString
& item
, int pos
)
485 // Need to change selection?
487 if ( !(m_combo
->GetWindowStyle() & wxCB_READONLY
) &&
488 m_combo
->GetValue() == item
)
493 m_strings
.Insert(item
,pos
);
494 if ( (int)m_clientDatas
.size() >= pos
)
495 m_clientDatas
.Insert(NULL
, pos
);
497 m_widths
.Insert(-1,pos
);
498 m_widthsDirty
= true;
501 wxVListBox::SetItemCount( wxVListBox::GetItemCount()+1 );
504 int wxVListBoxComboPopup::Append(const wxString
& item
)
506 int pos
= (int)m_strings
.GetCount();
508 if ( m_combo
->GetWindowStyle() & wxCB_SORT
)
511 // TODO: Could be optimized with binary search
512 wxArrayString strings
= m_strings
;
515 for ( i
=0; i
<strings
.GetCount(); i
++ )
517 if ( item
.CmpNoCase(strings
.Item(i
)) < 0 )
530 void wxVListBoxComboPopup::Clear()
542 m_value
= wxNOT_FOUND
;
545 wxVListBox::SetItemCount(0);
548 void wxVListBoxComboPopup::ClearClientDatas()
550 if ( m_clientDataItemsType
== wxClientData_Object
)
553 for ( i
=0; i
<m_clientDatas
.GetCount(); i
++ )
554 delete (wxClientData
*) m_clientDatas
[i
];
557 m_clientDatas
.Empty();
560 void wxVListBoxComboPopup::SetItemClientData( unsigned int n
,
562 wxClientDataType clientDataItemsType
)
564 // It should be sufficient to update this variable only here
565 m_clientDataItemsType
= clientDataItemsType
;
567 m_clientDatas
[n
] = clientData
;
572 void* wxVListBoxComboPopup::GetItemClientData(unsigned int n
) const
574 if ( m_clientDatas
.GetCount() > n
)
575 return m_clientDatas
[n
];
580 void wxVListBoxComboPopup::Delete( unsigned int item
)
582 // Remove client data, if set
583 if ( m_clientDatas
.GetCount() )
585 if ( m_clientDataItemsType
== wxClientData_Object
)
586 delete (wxClientData
*) m_clientDatas
[item
];
588 m_clientDatas
.RemoveAt(item
);
591 m_strings
.RemoveAt(item
);
592 m_widths
.RemoveAt(item
);
594 if ( (int)item
== m_widestItem
)
597 int sel
= GetSelection();
600 wxVListBox::SetItemCount( wxVListBox::GetItemCount()-1 );
603 if ( (int)item
< sel
)
605 else if ( (int)item
== sel
)
606 SetSelection(wxNOT_FOUND
);
609 int wxVListBoxComboPopup::FindString(const wxString
& s
, bool bCase
) const
611 return m_strings
.Index(s
, bCase
);
614 unsigned int wxVListBoxComboPopup::GetCount() const
616 return m_strings
.GetCount();
619 wxString
wxVListBoxComboPopup::GetString( int item
) const
621 return m_strings
[item
];
624 void wxVListBoxComboPopup::SetString( int item
, const wxString
& str
)
626 m_strings
[item
] = str
;
627 ItemWidthChanged(item
);
630 wxString
wxVListBoxComboPopup::GetStringValue() const
632 return m_stringValue
;
635 void wxVListBoxComboPopup::SetSelection( int item
)
637 wxCHECK_RET( item
== wxNOT_FOUND
|| ((unsigned int)item
< GetCount()),
638 wxT("invalid index in wxVListBoxComboPopup::SetSelection") );
643 m_stringValue
= m_strings
[item
];
645 m_stringValue
= wxEmptyString
;
648 wxVListBox::SetSelection(item
);
651 int wxVListBoxComboPopup::GetSelection() const
656 void wxVListBoxComboPopup::SetStringValue( const wxString
& value
)
658 int index
= m_strings
.Index(value
);
660 m_stringValue
= value
;
662 if ( index
>= 0 && index
< (int)wxVListBox::GetItemCount() )
664 wxVListBox::SetSelection(index
);
669 void wxVListBoxComboPopup::CalcWidths()
671 bool doFindWidest
= m_findWidest
;
673 // Measure items with dirty width.
677 unsigned int n
= m_widths
.GetCount();
678 int dirtyHandled
= 0;
679 wxArrayInt
& widths
= m_widths
;
681 // I think using wxDC::GetTextExtent is faster than
682 // wxWindow::GetTextExtent (assuming same dc is used
683 // for all calls, as we do here).
684 wxClientDC
dc(m_combo
);
685 dc
.SetFont(m_useFont
);
687 for ( i
=0; i
<n
; i
++ )
691 wxCoord x
= OnMeasureItemWidth(i
);
695 const wxString
& text
= m_strings
[i
];
697 // To make sure performance won't suck in extreme scenarios,
698 // we'll estimate length after some arbitrary number of items
699 // have been checked precily.
700 if ( dirtyHandled
< 1024 )
703 dc
.GetTextExtent(text
, &x
, &y
, 0, 0);
708 x
= text
.length() * (dc
.GetCharWidth()+1);
714 if ( x
>= m_widestWidth
)
717 m_widestItem
= (int)i
;
719 else if ( (int)i
== m_widestItem
)
721 // Width of previously widest item has been decreased, so
722 // we'll have to check all to find current widest item.
730 m_widthsDirty
= false;
736 unsigned int n
= m_widths
.GetCount();
741 for ( i
=0; i
<n
; i
++ )
751 m_widestWidth
= bestWidth
;
752 m_widestItem
= bestIndex
;
754 m_findWidest
= false;
758 wxSize
wxVListBoxComboPopup::GetAdjustedSize( int minWidth
, int prefHeight
, int maxHeight
)
762 maxHeight
-= 2; // Must take borders into account
764 if ( m_strings
.GetCount() )
766 if ( prefHeight
> 0 )
769 if ( height
> maxHeight
)
772 int totalHeight
= GetTotalHeight(); // + 3;
774 // Take borders into account on Mac or scrollbars always appear
775 #if defined(__WXMAC__)
778 if ( height
>= totalHeight
)
780 height
= totalHeight
;
784 // Adjust height to a multiple of the height of the first item
785 // NB: Calculations that take variable height into account
787 int fih
= GetLineHeight(0);
788 height
-= height
% fih
;
796 // Take scrollbar into account in width calculations
797 int widestWidth
= m_widestWidth
+ wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
798 return wxSize(minWidth
> widestWidth
? minWidth
: widestWidth
,
802 //void wxVListBoxComboPopup::Populate( int n, const wxString choices[] )
803 void wxVListBoxComboPopup::Populate( const wxArrayString
& choices
)
807 int n
= choices
.GetCount();
809 for ( i
=0; i
<n
; i
++ )
811 const wxString
& item
= choices
.Item(i
);
815 m_widths
.SetCount(n
,-1);
816 m_widthsDirty
= true;
819 wxVListBox::SetItemCount(n
);
821 // Sort the initial choices
822 if ( m_combo
->GetWindowStyle() & wxCB_SORT
)
825 // Find initial selection
826 wxString strValue
= m_combo
->GetValue();
827 if ( strValue
.length() )
828 m_value
= m_strings
.Index(strValue
);
831 // ----------------------------------------------------------------------------
832 // wxOwnerDrawnComboBox
833 // ----------------------------------------------------------------------------
836 BEGIN_EVENT_TABLE(wxOwnerDrawnComboBox
, wxComboCtrl
)
840 #if wxUSE_EXTENDED_RTTI
841 IMPLEMENT_DYNAMIC_CLASS2_XTI(wxOwnerDrawnComboBox
, wxComboCtrl
, wxControlWithItems
, "wx/odcombo.h")
843 wxBEGIN_PROPERTIES_TABLE(wxOwnerDrawnComboBox
)
844 wxEND_PROPERTIES_TABLE()
846 wxBEGIN_HANDLERS_TABLE(wxOwnerDrawnComboBox
)
847 wxEND_HANDLERS_TABLE()
849 wxCONSTRUCTOR_5( wxOwnerDrawnComboBox
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, Value
, wxPoint
, Position
, wxSize
, Size
)
851 IMPLEMENT_DYNAMIC_CLASS2(wxOwnerDrawnComboBox
, wxComboCtrl
, wxControlWithItems
)
854 void wxOwnerDrawnComboBox::Init()
858 bool wxOwnerDrawnComboBox::Create(wxWindow
*parent
,
860 const wxString
& value
,
864 const wxValidator
& validator
,
865 const wxString
& name
)
867 return wxComboCtrl::Create(parent
,id
,value
,pos
,size
,style
,validator
,name
);
870 wxOwnerDrawnComboBox::wxOwnerDrawnComboBox(wxWindow
*parent
,
872 const wxString
& value
,
875 const wxArrayString
& choices
,
877 const wxValidator
& validator
,
878 const wxString
& name
)
883 Create(parent
,id
,value
,pos
,size
,choices
,style
, validator
, name
);
886 bool wxOwnerDrawnComboBox::Create(wxWindow
*parent
,
888 const wxString
& value
,
891 const wxArrayString
& choices
,
893 const wxValidator
& validator
,
894 const wxString
& name
)
897 //wxCArrayString chs(choices);
899 //return Create(parent, id, value, pos, size, chs.GetCount(),
900 // chs.GetStrings(), style, validator, name);
901 return Create(parent
, id
, value
, pos
, size
, 0,
902 NULL
, style
, validator
, name
);
905 bool wxOwnerDrawnComboBox::Create(wxWindow
*parent
,
907 const wxString
& value
,
911 const wxString choices
[],
913 const wxValidator
& validator
,
914 const wxString
& name
)
917 if ( !Create(parent
, id
, value
, pos
, size
, style
,
924 for ( i
=0; i
<n
; i
++ )
925 m_initChs
.Add(choices
[i
]);
930 wxOwnerDrawnComboBox::~wxOwnerDrawnComboBox()
932 if ( m_popupInterface
)
933 GetVListBoxComboPopup()->ClearClientDatas();
936 void wxOwnerDrawnComboBox::DoSetPopupControl(wxComboPopup
* popup
)
940 popup
= new wxVListBoxComboPopup();
943 wxComboCtrl::DoSetPopupControl(popup
);
947 // Add initial choices to the wxVListBox
948 if ( !GetVListBoxComboPopup()->GetCount() )
950 GetVListBoxComboPopup()->Populate(m_initChs
);
955 // ----------------------------------------------------------------------------
956 // wxOwnerDrawnComboBox item manipulation methods
957 // ----------------------------------------------------------------------------
959 void wxOwnerDrawnComboBox::DoClear()
961 EnsurePopupControl();
963 GetVListBoxComboPopup()->Clear();
965 SetValue(wxEmptyString
);
968 void wxOwnerDrawnComboBox::DoDeleteOneItem(unsigned int n
)
970 wxCHECK_RET( IsValid(n
), wxT("invalid index in wxOwnerDrawnComboBox::Delete") );
972 if ( GetSelection() == (int) n
)
973 SetValue(wxEmptyString
);
975 GetVListBoxComboPopup()->Delete(n
);
978 unsigned int wxOwnerDrawnComboBox::GetCount() const
980 if ( !m_popupInterface
)
981 return m_initChs
.GetCount();
983 return GetVListBoxComboPopup()->GetCount();
986 wxString
wxOwnerDrawnComboBox::GetString(unsigned int n
) const
988 wxCHECK_MSG( IsValid(n
), wxEmptyString
, wxT("invalid index in wxOwnerDrawnComboBox::GetString") );
990 if ( !m_popupInterface
)
991 return m_initChs
.Item(n
);
993 return GetVListBoxComboPopup()->GetString(n
);
996 void wxOwnerDrawnComboBox::SetString(unsigned int n
, const wxString
& s
)
998 EnsurePopupControl();
1000 wxCHECK_RET( IsValid(n
), wxT("invalid index in wxOwnerDrawnComboBox::SetString") );
1002 GetVListBoxComboPopup()->SetString(n
,s
);
1005 int wxOwnerDrawnComboBox::FindString(const wxString
& s
, bool bCase
) const
1007 if ( !m_popupInterface
)
1008 return m_initChs
.Index(s
, bCase
);
1010 return GetVListBoxComboPopup()->FindString(s
, bCase
);
1013 void wxOwnerDrawnComboBox::Select(int n
)
1015 EnsurePopupControl();
1017 wxCHECK_RET( (n
== wxNOT_FOUND
) || IsValid(n
), wxT("invalid index in wxOwnerDrawnComboBox::Select") );
1019 GetVListBoxComboPopup()->SetSelection(n
);
1023 str
= GetVListBoxComboPopup()->GetString(n
);
1025 // Refresh text portion in control
1027 m_text
->SetValue( str
);
1029 m_valueString
= str
;
1034 int wxOwnerDrawnComboBox::GetSelection() const
1036 if ( !m_popupInterface
)
1037 return m_initChs
.Index(m_valueString
);
1039 return GetVListBoxComboPopup()->GetSelection();
1042 int wxOwnerDrawnComboBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
1045 wxClientDataType type
)
1047 EnsurePopupControl();
1049 const unsigned int count
= items
.GetCount();
1051 if ( HasFlag(wxCB_SORT
) )
1055 for ( unsigned int i
= 0; i
< count
; ++i
)
1057 n
= GetVListBoxComboPopup()->Append(items
[i
]);
1058 AssignNewItemClientData(n
, clientData
, i
, type
);
1065 for ( unsigned int i
= 0; i
< count
; ++i
, ++pos
)
1067 GetVListBoxComboPopup()->Insert(items
[i
], pos
);
1068 AssignNewItemClientData(pos
, clientData
, i
, type
);
1075 void wxOwnerDrawnComboBox::DoSetItemClientData(unsigned int n
, void* clientData
)
1077 EnsurePopupControl();
1079 GetVListBoxComboPopup()->SetItemClientData(n
, clientData
,
1080 GetClientDataType());
1083 void* wxOwnerDrawnComboBox::DoGetItemClientData(unsigned int n
) const
1085 if ( !m_popupInterface
)
1088 return GetVListBoxComboPopup()->GetItemClientData(n
);
1091 // ----------------------------------------------------------------------------
1092 // wxOwnerDrawnComboBox item drawing and measuring default implementations
1093 // ----------------------------------------------------------------------------
1095 void wxOwnerDrawnComboBox::OnDrawItem( wxDC
& dc
,
1100 if ( flags
& wxODCB_PAINTING_CONTROL
)
1104 if ( !ShouldUseHintText() )
1111 wxColour col
= wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
);
1112 dc
.SetTextForeground(col
);
1116 rect
.x
+ GetMargins().x
,
1117 (rect
.height
-dc
.GetCharHeight())/2 + rect
.y
);
1121 dc
.DrawText( GetVListBoxComboPopup()->GetString(item
), rect
.x
+ 2, rect
.y
);
1125 wxCoord
wxOwnerDrawnComboBox::OnMeasureItem( size_t WXUNUSED(item
) ) const
1130 wxCoord
wxOwnerDrawnComboBox::OnMeasureItemWidth( size_t WXUNUSED(item
) ) const
1135 void wxOwnerDrawnComboBox::OnDrawBackground(wxDC
& dc
,
1140 // We need only to explicitly draw background for items
1141 // that should have selected background. Also, call PrepareBackground
1142 // always when painting the control so that clipping is done properly.
1144 if ( (flags
& wxODCB_PAINTING_SELECTED
) ||
1145 ((flags
& wxODCB_PAINTING_CONTROL
) && HasFlag(wxCB_READONLY
)) )
1147 int bgFlags
= wxCONTROL_SELECTED
;
1149 if ( !(flags
& wxODCB_PAINTING_CONTROL
) )
1150 bgFlags
|= wxCONTROL_ISSUBMENU
;
1152 PrepareBackground(dc
, rect
, bgFlags
);
1156 #endif // wxUSE_ODCOMBOBOX