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 if ( (int)m_clientDatas
.size() >= pos
)
481 m_clientDatas
.Insert(NULL
, pos
);
483 m_widths
.Insert(-1,pos
);
484 m_widthsDirty
= true;
487 wxVListBox::SetItemCount( wxVListBox::GetItemCount()+1 );
490 int wxVListBoxComboPopup::Append(const wxString
& item
)
492 int pos
= (int)m_strings
.GetCount();
494 if ( m_combo
->GetWindowStyle() & wxCB_SORT
)
497 // TODO: Could be optimized with binary search
498 wxArrayString strings
= m_strings
;
501 for ( i
=0; i
<strings
.GetCount(); i
++ )
503 if ( item
.Cmp(strings
.Item(i
)) < 0 )
516 void wxVListBoxComboPopup::Clear()
528 m_value
= wxNOT_FOUND
;
531 wxVListBox::SetItemCount(0);
534 void wxVListBoxComboPopup::ClearClientDatas()
536 if ( m_clientDataItemsType
== wxClientData_Object
)
539 for ( i
=0; i
<m_clientDatas
.GetCount(); i
++ )
540 delete (wxClientData
*) m_clientDatas
[i
];
543 m_clientDatas
.Empty();
546 void wxVListBoxComboPopup::SetItemClientData( unsigned int n
,
548 wxClientDataType clientDataItemsType
)
550 // It should be sufficient to update this variable only here
551 m_clientDataItemsType
= clientDataItemsType
;
553 m_clientDatas
[n
] = clientData
;
558 void* wxVListBoxComboPopup::GetItemClientData(unsigned int n
) const
560 if ( m_clientDatas
.GetCount() > n
)
561 return m_clientDatas
[n
];
566 void wxVListBoxComboPopup::Delete( unsigned int item
)
568 // Remove client data, if set
569 if ( m_clientDatas
.GetCount() )
571 if ( m_clientDataItemsType
== wxClientData_Object
)
572 delete (wxClientData
*) m_clientDatas
[item
];
574 m_clientDatas
.RemoveAt(item
);
577 m_strings
.RemoveAt(item
);
578 m_widths
.RemoveAt(item
);
580 if ( (int)item
== m_widestItem
)
583 int sel
= GetSelection();
586 wxVListBox::SetItemCount( wxVListBox::GetItemCount()-1 );
589 if ( (int)item
< sel
)
591 else if ( (int)item
== sel
)
592 SetSelection(wxNOT_FOUND
);
595 int wxVListBoxComboPopup::FindString(const wxString
& s
, bool bCase
) const
597 return m_strings
.Index(s
, bCase
);
600 unsigned int wxVListBoxComboPopup::GetCount() const
602 return m_strings
.GetCount();
605 wxString
wxVListBoxComboPopup::GetString( int item
) const
607 return m_strings
[item
];
610 void wxVListBoxComboPopup::SetString( int item
, const wxString
& str
)
612 m_strings
[item
] = str
;
613 ItemWidthChanged(item
);
616 wxString
wxVListBoxComboPopup::GetStringValue() const
618 return m_stringValue
;
621 void wxVListBoxComboPopup::SetSelection( int item
)
623 wxCHECK_RET( item
== wxNOT_FOUND
|| ((unsigned int)item
< GetCount()),
624 wxT("invalid index in wxVListBoxComboPopup::SetSelection") );
629 m_stringValue
= m_strings
[item
];
631 m_stringValue
= wxEmptyString
;
634 wxVListBox::SetSelection(item
);
637 int wxVListBoxComboPopup::GetSelection() const
642 void wxVListBoxComboPopup::SetStringValue( const wxString
& value
)
644 int index
= m_strings
.Index(value
);
646 m_stringValue
= value
;
648 if ( index
>= 0 && index
< (int)wxVListBox::GetItemCount() )
650 wxVListBox::SetSelection(index
);
655 void wxVListBoxComboPopup::CalcWidths()
657 bool doFindWidest
= m_findWidest
;
659 // Measure items with dirty width.
663 unsigned int n
= m_widths
.GetCount();
664 int dirtyHandled
= 0;
665 wxArrayInt
& widths
= m_widths
;
667 // I think using wxDC::GetTextExtent is faster than
668 // wxWindow::GetTextExtent (assuming same dc is used
669 // for all calls, as we do here).
670 wxClientDC
dc(m_combo
);
671 dc
.SetFont(m_useFont
);
673 for ( i
=0; i
<n
; i
++ )
677 wxCoord x
= OnMeasureItemWidth(i
);
681 const wxString
& text
= m_strings
[i
];
683 // To make sure performance won't suck in extreme scenarios,
684 // we'll estimate length after some arbitrary number of items
685 // have been checked precily.
686 if ( dirtyHandled
< 1024 )
689 dc
.GetTextExtent(text
, &x
, &y
, 0, 0);
694 x
= text
.length() * (dc
.GetCharWidth()+1);
700 if ( x
>= m_widestWidth
)
703 m_widestItem
= (int)i
;
705 else if ( (int)i
== m_widestItem
)
707 // Width of previously widest item has been decreased, so
708 // we'll have to check all to find current widest item.
716 m_widthsDirty
= false;
722 unsigned int n
= m_widths
.GetCount();
727 for ( i
=0; i
<n
; i
++ )
737 m_widestWidth
= bestWidth
;
738 m_widestItem
= bestIndex
;
740 m_findWidest
= false;
744 wxSize
wxVListBoxComboPopup::GetAdjustedSize( int minWidth
, int prefHeight
, int maxHeight
)
748 maxHeight
-= 2; // Must take borders into account
750 if ( m_strings
.GetCount() )
752 if ( prefHeight
> 0 )
755 if ( height
> maxHeight
)
758 int totalHeight
= GetTotalHeight(); // + 3;
759 if ( height
>= totalHeight
)
761 height
= totalHeight
;
765 // Adjust height to a multiple of the height of the first item
766 // NB: Calculations that take variable height into account
768 int fih
= GetLineHeight(0);
769 height
-= height
% fih
;
777 // Take scrollbar into account in width calculations
778 int widestWidth
= m_widestWidth
+ wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
779 return wxSize(minWidth
> widestWidth
? minWidth
: widestWidth
,
783 //void wxVListBoxComboPopup::Populate( int n, const wxString choices[] )
784 void wxVListBoxComboPopup::Populate( const wxArrayString
& choices
)
788 int n
= choices
.GetCount();
790 for ( i
=0; i
<n
; i
++ )
792 const wxString
& item
= choices
.Item(i
);
796 m_widths
.SetCount(n
,-1);
797 m_widthsDirty
= true;
800 wxVListBox::SetItemCount(n
);
802 // Sort the initial choices
803 if ( m_combo
->GetWindowStyle() & wxCB_SORT
)
806 // Find initial selection
807 wxString strValue
= m_combo
->GetValue();
808 if ( strValue
.length() )
809 m_value
= m_strings
.Index(strValue
);
812 // ----------------------------------------------------------------------------
813 // wxOwnerDrawnComboBox
814 // ----------------------------------------------------------------------------
817 BEGIN_EVENT_TABLE(wxOwnerDrawnComboBox
, wxComboCtrl
)
821 #if wxUSE_EXTENDED_RTTI
822 IMPLEMENT_DYNAMIC_CLASS2_XTI(wxOwnerDrawnComboBox
, wxComboCtrl
, wxControlWithItems
, "wx/odcombo.h")
824 wxBEGIN_PROPERTIES_TABLE(wxOwnerDrawnComboBox
)
825 wxEND_PROPERTIES_TABLE()
827 wxBEGIN_HANDLERS_TABLE(wxOwnerDrawnComboBox
)
828 wxEND_HANDLERS_TABLE()
830 wxCONSTRUCTOR_5( wxOwnerDrawnComboBox
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, Value
, wxPoint
, Position
, wxSize
, Size
)
832 IMPLEMENT_DYNAMIC_CLASS2(wxOwnerDrawnComboBox
, wxComboCtrl
, wxControlWithItems
)
835 void wxOwnerDrawnComboBox::Init()
839 bool wxOwnerDrawnComboBox::Create(wxWindow
*parent
,
841 const wxString
& value
,
845 const wxValidator
& validator
,
846 const wxString
& name
)
848 return wxComboCtrl::Create(parent
,id
,value
,pos
,size
,style
,validator
,name
);
851 wxOwnerDrawnComboBox::wxOwnerDrawnComboBox(wxWindow
*parent
,
853 const wxString
& value
,
856 const wxArrayString
& choices
,
858 const wxValidator
& validator
,
859 const wxString
& name
)
864 Create(parent
,id
,value
,pos
,size
,choices
,style
, validator
, name
);
867 bool wxOwnerDrawnComboBox::Create(wxWindow
*parent
,
869 const wxString
& value
,
872 const wxArrayString
& choices
,
874 const wxValidator
& validator
,
875 const wxString
& name
)
878 //wxCArrayString chs(choices);
880 //return Create(parent, id, value, pos, size, chs.GetCount(),
881 // chs.GetStrings(), style, validator, name);
882 return Create(parent
, id
, value
, pos
, size
, 0,
883 NULL
, style
, validator
, name
);
886 bool wxOwnerDrawnComboBox::Create(wxWindow
*parent
,
888 const wxString
& value
,
892 const wxString choices
[],
894 const wxValidator
& validator
,
895 const wxString
& name
)
898 if ( !Create(parent
, id
, value
, pos
, size
, style
,
905 for ( i
=0; i
<n
; i
++ )
906 m_initChs
.Add(choices
[i
]);
911 wxOwnerDrawnComboBox::~wxOwnerDrawnComboBox()
913 if ( m_popupInterface
)
914 GetVListBoxComboPopup()->ClearClientDatas();
917 void wxOwnerDrawnComboBox::DoSetPopupControl(wxComboPopup
* popup
)
921 popup
= new wxVListBoxComboPopup();
924 wxComboCtrl::DoSetPopupControl(popup
);
928 // Add initial choices to the wxVListBox
929 if ( !GetVListBoxComboPopup()->GetCount() )
931 GetVListBoxComboPopup()->Populate(m_initChs
);
936 // ----------------------------------------------------------------------------
937 // wxOwnerDrawnComboBox item manipulation methods
938 // ----------------------------------------------------------------------------
940 void wxOwnerDrawnComboBox::DoClear()
942 EnsurePopupControl();
944 GetVListBoxComboPopup()->Clear();
946 SetValue(wxEmptyString
);
949 void wxOwnerDrawnComboBox::DoDeleteOneItem(unsigned int n
)
951 wxCHECK_RET( IsValid(n
), _T("invalid index in wxOwnerDrawnComboBox::Delete") );
953 if ( GetSelection() == (int) n
)
954 SetValue(wxEmptyString
);
956 GetVListBoxComboPopup()->Delete(n
);
959 unsigned int wxOwnerDrawnComboBox::GetCount() const
961 if ( !m_popupInterface
)
962 return m_initChs
.GetCount();
964 return GetVListBoxComboPopup()->GetCount();
967 wxString
wxOwnerDrawnComboBox::GetString(unsigned int n
) const
969 wxCHECK_MSG( IsValid(n
), wxEmptyString
, _T("invalid index in wxOwnerDrawnComboBox::GetString") );
971 if ( !m_popupInterface
)
972 return m_initChs
.Item(n
);
974 return GetVListBoxComboPopup()->GetString(n
);
977 void wxOwnerDrawnComboBox::SetString(unsigned int n
, const wxString
& s
)
979 EnsurePopupControl();
981 wxCHECK_RET( IsValid(n
), _T("invalid index in wxOwnerDrawnComboBox::SetString") );
983 GetVListBoxComboPopup()->SetString(n
,s
);
986 int wxOwnerDrawnComboBox::FindString(const wxString
& s
, bool bCase
) const
988 if ( !m_popupInterface
)
989 return m_initChs
.Index(s
, bCase
);
991 return GetVListBoxComboPopup()->FindString(s
, bCase
);
994 void wxOwnerDrawnComboBox::Select(int n
)
996 EnsurePopupControl();
998 wxCHECK_RET( (n
== wxNOT_FOUND
) || IsValid(n
), _T("invalid index in wxOwnerDrawnComboBox::Select") );
1000 GetVListBoxComboPopup()->SetSelection(n
);
1004 str
= GetVListBoxComboPopup()->GetString(n
);
1006 // Refresh text portion in control
1008 m_text
->SetValue( str
);
1010 m_valueString
= str
;
1015 int wxOwnerDrawnComboBox::GetSelection() const
1017 if ( !m_popupInterface
)
1018 return m_initChs
.Index(m_valueString
);
1020 return GetVListBoxComboPopup()->GetSelection();
1023 int wxOwnerDrawnComboBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
1026 wxClientDataType type
)
1028 EnsurePopupControl();
1030 const unsigned int count
= items
.GetCount();
1032 if ( HasFlag(wxCB_SORT
) )
1036 for ( unsigned int i
= 0; i
< count
; ++i
)
1038 int n
= GetVListBoxComboPopup()->Append(items
[i
]);
1039 AssignNewItemClientData(n
, clientData
, i
, type
);
1046 for ( unsigned int i
= 0; i
< count
; ++i
, ++pos
)
1048 GetVListBoxComboPopup()->Insert(items
[i
], pos
);
1049 AssignNewItemClientData(pos
, clientData
, i
, type
);
1056 void wxOwnerDrawnComboBox::DoSetItemClientData(unsigned int n
, void* clientData
)
1058 EnsurePopupControl();
1060 GetVListBoxComboPopup()->SetItemClientData(n
, clientData
,
1061 GetClientDataType());
1064 void* wxOwnerDrawnComboBox::DoGetItemClientData(unsigned int n
) const
1066 if ( !m_popupInterface
)
1069 return GetVListBoxComboPopup()->GetItemClientData(n
);
1072 // ----------------------------------------------------------------------------
1073 // wxOwnerDrawnComboBox item drawing and measuring default implementations
1074 // ----------------------------------------------------------------------------
1076 void wxOwnerDrawnComboBox::OnDrawItem( wxDC
& dc
,
1081 if ( flags
& wxODCB_PAINTING_CONTROL
)
1083 dc
.DrawText( GetValue(),
1084 rect
.x
+ GetTextIndent(),
1085 (rect
.height
-dc
.GetCharHeight())/2 + rect
.y
);
1089 dc
.DrawText( GetVListBoxComboPopup()->GetString(item
), rect
.x
+ 2, rect
.y
);
1093 wxCoord
wxOwnerDrawnComboBox::OnMeasureItem( size_t WXUNUSED(item
) ) const
1098 wxCoord
wxOwnerDrawnComboBox::OnMeasureItemWidth( size_t WXUNUSED(item
) ) const
1103 void wxOwnerDrawnComboBox::OnDrawBackground(wxDC
& dc
,
1108 // We need only to explicitly draw background for items
1109 // that should have selected background. Also, call PrepareBackground
1110 // always when painting the control so that clipping is done properly.
1112 if ( (flags
& wxODCB_PAINTING_SELECTED
) ||
1113 ((flags
& wxODCB_PAINTING_CONTROL
) && HasFlag(wxCB_READONLY
)) )
1115 int bgFlags
= wxCONTROL_SELECTED
;
1117 if ( !(flags
& wxODCB_PAINTING_CONTROL
) )
1118 bgFlags
|= wxCONTROL_ISSUBMENU
;
1120 PrepareBackground(dc
, rect
, bgFlags
);
1124 #endif // wxUSE_ODCOMBOBOX