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_CHAR(wxVListBoxComboPopup::OnChar
)
58 EVT_LEFT_UP(wxVListBoxComboPopup::OnLeftClick
)
62 void wxVListBoxComboPopup::Init()
66 m_widthsDirty
= false;
71 m_clientDataItemsType
= wxClientData_None
;
72 m_partialCompletionString
= wxEmptyString
;
75 bool wxVListBoxComboPopup::Create(wxWindow
* parent
)
77 if ( !wxVListBox::Create(parent
,
81 wxBORDER_SIMPLE
| wxLB_INT_HEIGHT
| wxWANTS_CHARS
) )
84 m_useFont
= m_combo
->GetFont();
86 wxVListBox::SetItemCount(m_strings
.GetCount());
88 // TODO: Move this to SetFont
89 m_itemHeight
= GetCharHeight() + 0;
94 wxVListBoxComboPopup::~wxVListBoxComboPopup()
99 bool wxVListBoxComboPopup::LazyCreate()
101 // NB: There is a bug with wxVListBox that can be avoided by creating
102 // it later (bug causes empty space to be shown if initial selection
103 // is at the end of a list longer than the control can show at once).
107 // paint the control itself
108 void wxVListBoxComboPopup::PaintComboControl( wxDC
& dc
, const wxRect
& rect
)
110 if ( !(m_combo
->GetWindowStyle() & wxODCB_STD_CONTROL_PAINT
) )
112 int flags
= wxODCB_PAINTING_CONTROL
;
114 if ( m_combo
->ShouldDrawFocus() )
115 flags
|= wxODCB_PAINTING_SELECTED
;
117 OnDrawBg(dc
, rect
, m_value
, flags
);
121 OnDrawItem(dc
,rect
,m_value
,flags
);
126 wxComboPopup::PaintComboControl(dc
,rect
);
129 void wxVListBoxComboPopup::OnDrawItem(wxDC
& dc
, const wxRect
& rect
, size_t n
) const
131 // TODO: Maybe this code could be moved to wxVListBox::OnPaint?
132 dc
.SetFont(m_useFont
);
136 // Set correct text colour for selected items
137 if ( wxVListBox::GetSelection() == (int) n
)
139 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) );
140 flags
|= wxODCB_PAINTING_SELECTED
;
144 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
) );
147 OnDrawItem(dc
,rect
,(int)n
,flags
);
150 wxCoord
wxVListBoxComboPopup::OnMeasureItem(size_t n
) const
152 wxOwnerDrawnComboBox
* combo
= (wxOwnerDrawnComboBox
*) m_combo
;
154 wxASSERT_MSG( combo
->IsKindOf(CLASSINFO(wxOwnerDrawnComboBox
)),
155 wxT("you must subclass wxVListBoxComboPopup for drawing and measuring methods") );
157 wxCoord h
= combo
->OnMeasureItem(n
);
163 wxCoord
wxVListBoxComboPopup::OnMeasureItemWidth(size_t n
) const
165 wxOwnerDrawnComboBox
* combo
= (wxOwnerDrawnComboBox
*) m_combo
;
167 wxASSERT_MSG( combo
->IsKindOf(CLASSINFO(wxOwnerDrawnComboBox
)),
168 wxT("you must subclass wxVListBoxComboPopup for drawing and measuring methods") );
170 return combo
->OnMeasureItemWidth(n
);
173 void wxVListBoxComboPopup::OnDrawBg( wxDC
& dc
,
178 wxOwnerDrawnComboBox
* combo
= (wxOwnerDrawnComboBox
*) m_combo
;
180 wxASSERT_MSG( combo
->IsKindOf(CLASSINFO(wxOwnerDrawnComboBox
)),
181 wxT("you must subclass wxVListBoxComboPopup for drawing and measuring methods") );
183 if ( IsCurrent((size_t)item
) && !(flags
& wxODCB_PAINTING_CONTROL
) )
184 flags
|= wxODCB_PAINTING_SELECTED
;
186 combo
->OnDrawBackground(dc
,rect
,item
,flags
);
189 void wxVListBoxComboPopup::OnDrawBackground(wxDC
& dc
, const wxRect
& rect
, size_t n
) const
191 OnDrawBg(dc
,rect
,(int)n
,0);
194 // This is called from wxVListBoxComboPopup::OnDrawItem, with text colour and font prepared
195 void wxVListBoxComboPopup::OnDrawItem( wxDC
& dc
, const wxRect
& rect
, int item
, int flags
) const
197 wxOwnerDrawnComboBox
* combo
= (wxOwnerDrawnComboBox
*) m_combo
;
199 wxASSERT_MSG( combo
->IsKindOf(CLASSINFO(wxOwnerDrawnComboBox
)),
200 wxT("you must subclass wxVListBoxComboPopup for drawing and measuring methods") );
202 combo
->OnDrawItem(dc
,rect
,item
,flags
);
205 void wxVListBoxComboPopup::DismissWithEvent()
207 StopPartialCompletion();
209 int selection
= wxVListBox::GetSelection();
213 if ( selection
!= wxNOT_FOUND
)
214 m_stringValue
= m_strings
[selection
];
216 m_stringValue
= wxEmptyString
;
218 if ( m_stringValue
!= m_combo
->GetValue() )
219 m_combo
->SetValueWithEvent(m_stringValue
);
223 SendComboBoxEvent(selection
);
226 void wxVListBoxComboPopup::SendComboBoxEvent( int selection
)
228 wxCommandEvent
evt(wxEVT_COMMAND_COMBOBOX_SELECTED
,m_combo
->GetId());
230 evt
.SetEventObject(m_combo
);
232 evt
.SetInt(selection
);
234 // Set client data, if any
235 if ( selection
>= 0 && (int)m_clientDatas
.GetCount() > selection
)
237 void* clientData
= m_clientDatas
[selection
];
238 if ( m_clientDataItemsType
== wxClientData_Object
)
239 evt
.SetClientObject((wxClientData
*)clientData
);
241 evt
.SetClientData(clientData
);
244 m_combo
->GetEventHandler()->AddPendingEvent(evt
);
247 // returns true if key was consumed
248 bool wxVListBoxComboPopup::HandleKey( int keycode
, bool saturate
, wxChar keychar
)
250 const int itemCount
= GetCount();
252 // keys do nothing in the empty control and returning immediately avoids
253 // using invalid indices below
258 int comboStyle
= m_combo
->GetWindowStyle();
262 // we have character equivalent of the keycode; filter out these that
263 // are not printable characters
264 if ( !wxIsprint(keychar
) )
268 if ( keycode
== WXK_DOWN
|| keycode
== WXK_NUMPAD_DOWN
|| keycode
== WXK_RIGHT
)
271 StopPartialCompletion();
273 else if ( keycode
== WXK_UP
|| keycode
== WXK_NUMPAD_UP
|| keycode
== WXK_LEFT
)
276 StopPartialCompletion();
278 else if ( keycode
== WXK_PAGEDOWN
|| keycode
== WXK_NUMPAD_PAGEDOWN
)
281 StopPartialCompletion();
283 else if ( keycode
== WXK_PAGEUP
|| keycode
== WXK_NUMPAD_PAGEUP
)
286 StopPartialCompletion();
288 else if ( keycode
== WXK_HOME
|| keycode
== WXK_NUMPAD_HOME
)
291 StopPartialCompletion();
293 else if ( keycode
== WXK_END
|| keycode
== WXK_NUMPAD_END
)
296 StopPartialCompletion();
298 else if ( keychar
&& (comboStyle
& wxCB_READONLY
) )
300 // Try partial completion
302 // find the new partial completion string
304 if (m_partialCompletionTimer
.IsRunning())
305 m_partialCompletionString
+=wxString(keychar
);
307 #endif // wxUSE_TIMER
308 m_partialCompletionString
=wxString(keychar
);
310 // now search through the values to see if this is found
312 unsigned int length
=m_partialCompletionString
.length();
314 for (i
=0; i
<itemCount
; i
++)
316 wxString item
=GetString(i
);
317 if (( item
.length() >= length
) && (! m_partialCompletionString
.CmpNoCase(item
.Left(length
))))
326 StopPartialCompletion();
328 return true; // to stop the first value being set
334 m_partialCompletionTimer
.Start(wxODCB_PARTIAL_COMPLETION_TIME
, true);
335 #endif // wxUSE_TIMER
343 if ( value
>= itemCount
)
344 value
= itemCount
- 1;
345 else if ( value
< 0 )
350 if ( value
>= itemCount
)
352 else if ( value
< 0 )
356 if ( value
== m_value
)
357 // Even if value was same, don't skip the event
358 // (good for consistency)
364 m_combo
->SetValue(m_strings
[value
]);
366 SendComboBoxEvent(m_value
);
371 // stop partial completion
372 void wxVListBoxComboPopup::StopPartialCompletion()
374 m_partialCompletionString
= wxEmptyString
;
376 m_partialCompletionTimer
.Stop();
377 #endif // wxUSE_TIMER
380 void wxVListBoxComboPopup::OnComboDoubleClick()
382 // Cycle on dclick (disable saturation to allow true cycling).
383 if ( !::wxGetKeyState(WXK_SHIFT
) )
384 HandleKey(WXK_DOWN
,false);
386 HandleKey(WXK_UP
,false);
389 void wxVListBoxComboPopup::OnComboKeyEvent( wxKeyEvent
& event
)
391 // Saturated key movement on
392 if ( !HandleKey(event
.GetKeyCode(), true) )
396 void wxVListBoxComboPopup::OnComboCharEvent( wxKeyEvent
& event
)
398 // unlike in OnComboKeyEvent, wxEVT_CHAR contains meaningful
399 // printable character information, so pass it
401 const wxChar charcode
= event
.GetUnicodeKey();
403 const wxChar charcode
= (wxChar
)event
.GetKeyCode();
406 if ( !HandleKey(event
.GetKeyCode(), true, charcode
) )
410 void wxVListBoxComboPopup::OnPopup()
412 // *must* set value after size is set (this is because of a vlbox bug)
413 wxVListBox::SetSelection(m_value
);
416 void wxVListBoxComboPopup::OnMouseMove(wxMouseEvent
& event
)
420 // Move selection to cursor if it is inside the popup
422 int y
= event
.GetPosition().y
;
423 int fromBottom
= GetClientSize().y
- y
;
425 // Since in any case we need to find out if the last item is only
426 // partially visible, we might just as well replicate the HitTest
428 const size_t lineMax
= GetVisibleEnd();
429 for ( size_t line
= GetVisibleBegin(); line
< lineMax
; line
++ )
431 y
-= OnGetRowHeight(line
);
434 // Only change selection if item is fully visible
435 if ( (y
+ fromBottom
) >= 0 )
437 wxVListBox::SetSelection((int)line
);
444 void wxVListBoxComboPopup::OnLeftClick(wxMouseEvent
& WXUNUSED(event
))
449 void wxVListBoxComboPopup::OnKey(wxKeyEvent
& event
)
451 // Hide popup if certain key or key combination was pressed
452 if ( m_combo
->IsKeyPopupToggle(event
) )
454 StopPartialCompletion();
457 else if ( event
.AltDown() )
459 // On both wxGTK and wxMSW, pressing Alt down seems to
460 // completely freeze things in popup (ie. arrow keys and
461 // enter won't work).
464 // Select item if ENTER is pressed
465 else if ( event
.GetKeyCode() == WXK_RETURN
|| event
.GetKeyCode() == WXK_NUMPAD_ENTER
)
471 // completion is handled in OnChar() below
476 void wxVListBoxComboPopup::OnChar(wxKeyEvent
& event
)
478 if ( m_combo
->GetWindowStyle() & wxCB_READONLY
)
480 // Process partial completion key codes here, but not the arrow keys as
481 // the base class will do that for us
483 const wxChar charcode
= event
.GetUnicodeKey();
485 const wxChar charcode
= (wxChar
)event
.GetKeyCode();
487 if ( wxIsprint(charcode
) )
489 OnComboCharEvent(event
);
490 SetSelection(m_value
); // ensure the highlight bar moves
491 return; // don't skip the event
498 void wxVListBoxComboPopup::Insert( const wxString
& item
, int pos
)
500 // Need to change selection?
502 if ( !(m_combo
->GetWindowStyle() & wxCB_READONLY
) &&
503 m_combo
->GetValue() == item
)
508 m_strings
.Insert(item
,pos
);
509 if ( (int)m_clientDatas
.size() >= pos
)
510 m_clientDatas
.Insert(NULL
, pos
);
512 m_widths
.Insert(-1,pos
);
513 m_widthsDirty
= true;
516 wxVListBox::SetItemCount( wxVListBox::GetItemCount()+1 );
519 int wxVListBoxComboPopup::Append(const wxString
& item
)
521 int pos
= (int)m_strings
.GetCount();
523 if ( m_combo
->GetWindowStyle() & wxCB_SORT
)
526 // TODO: Could be optimized with binary search
527 wxArrayString strings
= m_strings
;
530 for ( i
=0; i
<strings
.GetCount(); i
++ )
532 if ( item
.CmpNoCase(strings
.Item(i
)) < 0 )
545 void wxVListBoxComboPopup::Clear()
557 m_value
= wxNOT_FOUND
;
560 wxVListBox::SetItemCount(0);
563 void wxVListBoxComboPopup::ClearClientDatas()
565 if ( m_clientDataItemsType
== wxClientData_Object
)
568 for ( i
=0; i
<m_clientDatas
.GetCount(); i
++ )
569 delete (wxClientData
*) m_clientDatas
[i
];
572 m_clientDatas
.Empty();
575 void wxVListBoxComboPopup::SetItemClientData( unsigned int n
,
577 wxClientDataType clientDataItemsType
)
579 // It should be sufficient to update this variable only here
580 m_clientDataItemsType
= clientDataItemsType
;
582 m_clientDatas
[n
] = clientData
;
587 void* wxVListBoxComboPopup::GetItemClientData(unsigned int n
) const
589 if ( m_clientDatas
.GetCount() > n
)
590 return m_clientDatas
[n
];
595 void wxVListBoxComboPopup::Delete( unsigned int item
)
597 // Remove client data, if set
598 if ( m_clientDatas
.GetCount() )
600 if ( m_clientDataItemsType
== wxClientData_Object
)
601 delete (wxClientData
*) m_clientDatas
[item
];
603 m_clientDatas
.RemoveAt(item
);
606 m_strings
.RemoveAt(item
);
607 m_widths
.RemoveAt(item
);
609 if ( (int)item
== m_widestItem
)
612 int sel
= GetSelection();
615 wxVListBox::SetItemCount( wxVListBox::GetItemCount()-1 );
618 if ( (int)item
< sel
)
620 else if ( (int)item
== sel
)
621 SetSelection(wxNOT_FOUND
);
624 int wxVListBoxComboPopup::FindString(const wxString
& s
, bool bCase
) const
626 return m_strings
.Index(s
, bCase
);
629 unsigned int wxVListBoxComboPopup::GetCount() const
631 return m_strings
.GetCount();
634 wxString
wxVListBoxComboPopup::GetString( int item
) const
636 return m_strings
[item
];
639 void wxVListBoxComboPopup::SetString( int item
, const wxString
& str
)
641 m_strings
[item
] = str
;
642 ItemWidthChanged(item
);
645 wxString
wxVListBoxComboPopup::GetStringValue() const
647 return m_stringValue
;
650 void wxVListBoxComboPopup::SetSelection( int item
)
652 wxCHECK_RET( item
== wxNOT_FOUND
|| ((unsigned int)item
< GetCount()),
653 wxT("invalid index in wxVListBoxComboPopup::SetSelection") );
658 m_stringValue
= m_strings
[item
];
660 m_stringValue
= wxEmptyString
;
663 wxVListBox::SetSelection(item
);
666 int wxVListBoxComboPopup::GetSelection() const
671 void wxVListBoxComboPopup::SetStringValue( const wxString
& value
)
673 int index
= m_strings
.Index(value
);
675 m_stringValue
= value
;
677 if ( index
>= 0 && index
< (int)wxVListBox::GetItemCount() )
679 wxVListBox::SetSelection(index
);
684 void wxVListBoxComboPopup::CalcWidths()
686 bool doFindWidest
= m_findWidest
;
688 // Measure items with dirty width.
692 unsigned int n
= m_widths
.GetCount();
693 int dirtyHandled
= 0;
694 wxArrayInt
& widths
= m_widths
;
696 // I think using wxDC::GetTextExtent is faster than
697 // wxWindow::GetTextExtent (assuming same dc is used
698 // for all calls, as we do here).
699 wxClientDC
dc(m_combo
);
700 dc
.SetFont(m_useFont
);
702 for ( i
=0; i
<n
; i
++ )
706 wxCoord x
= OnMeasureItemWidth(i
);
710 const wxString
& text
= m_strings
[i
];
712 // To make sure performance won't suck in extreme scenarios,
713 // we'll estimate length after some arbitrary number of items
714 // have been checked precily.
715 if ( dirtyHandled
< 1024 )
718 dc
.GetTextExtent(text
, &x
, &y
, 0, 0);
723 x
= text
.length() * (dc
.GetCharWidth()+1);
729 if ( x
>= m_widestWidth
)
732 m_widestItem
= (int)i
;
734 else if ( (int)i
== m_widestItem
)
736 // Width of previously widest item has been decreased, so
737 // we'll have to check all to find current widest item.
745 m_widthsDirty
= false;
751 unsigned int n
= m_widths
.GetCount();
756 for ( i
=0; i
<n
; i
++ )
766 m_widestWidth
= bestWidth
;
767 m_widestItem
= bestIndex
;
769 m_findWidest
= false;
773 wxSize
wxVListBoxComboPopup::GetAdjustedSize( int minWidth
, int prefHeight
, int maxHeight
)
777 maxHeight
-= 2; // Must take borders into account
779 if ( m_strings
.GetCount() )
781 if ( prefHeight
> 0 )
784 if ( height
> maxHeight
)
787 int totalHeight
= GetTotalHeight(); // + 3;
789 // Take borders into account on Mac or scrollbars always appear
790 #if defined(__WXMAC__)
793 if ( height
>= totalHeight
)
795 height
= totalHeight
;
799 // Adjust height to a multiple of the height of the first item
800 // NB: Calculations that take variable height into account
802 int fih
= GetLineHeight(0);
803 height
-= height
% fih
;
811 // Take scrollbar into account in width calculations
812 int widestWidth
= m_widestWidth
+ wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
813 return wxSize(minWidth
> widestWidth
? minWidth
: widestWidth
,
817 //void wxVListBoxComboPopup::Populate( int n, const wxString choices[] )
818 void wxVListBoxComboPopup::Populate( const wxArrayString
& choices
)
822 int n
= choices
.GetCount();
824 for ( i
=0; i
<n
; i
++ )
826 const wxString
& item
= choices
.Item(i
);
830 m_widths
.SetCount(n
,-1);
831 m_widthsDirty
= true;
834 wxVListBox::SetItemCount(n
);
836 // Sort the initial choices
837 if ( m_combo
->GetWindowStyle() & wxCB_SORT
)
840 // Find initial selection
841 wxString strValue
= m_combo
->GetValue();
842 if ( strValue
.length() )
843 m_value
= m_strings
.Index(strValue
);
846 // ----------------------------------------------------------------------------
847 // wxOwnerDrawnComboBox
848 // ----------------------------------------------------------------------------
851 BEGIN_EVENT_TABLE(wxOwnerDrawnComboBox
, wxComboCtrl
)
855 #if wxUSE_EXTENDED_RTTI
856 IMPLEMENT_DYNAMIC_CLASS2_XTI(wxOwnerDrawnComboBox
, wxComboCtrl
, wxControlWithItems
, "wx/odcombo.h")
858 wxBEGIN_PROPERTIES_TABLE(wxOwnerDrawnComboBox
)
859 wxEND_PROPERTIES_TABLE()
861 wxBEGIN_HANDLERS_TABLE(wxOwnerDrawnComboBox
)
862 wxEND_HANDLERS_TABLE()
864 wxCONSTRUCTOR_5( wxOwnerDrawnComboBox
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, Value
, wxPoint
, Position
, wxSize
, Size
)
866 IMPLEMENT_DYNAMIC_CLASS2(wxOwnerDrawnComboBox
, wxComboCtrl
, wxControlWithItems
)
869 void wxOwnerDrawnComboBox::Init()
873 bool wxOwnerDrawnComboBox::Create(wxWindow
*parent
,
875 const wxString
& value
,
879 const wxValidator
& validator
,
880 const wxString
& name
)
882 return wxComboCtrl::Create(parent
,id
,value
,pos
,size
,style
,validator
,name
);
885 wxOwnerDrawnComboBox::wxOwnerDrawnComboBox(wxWindow
*parent
,
887 const wxString
& value
,
890 const wxArrayString
& choices
,
892 const wxValidator
& validator
,
893 const wxString
& name
)
898 Create(parent
,id
,value
,pos
,size
,choices
,style
, validator
, name
);
901 bool wxOwnerDrawnComboBox::Create(wxWindow
*parent
,
903 const wxString
& value
,
906 const wxArrayString
& choices
,
908 const wxValidator
& validator
,
909 const wxString
& name
)
912 //wxCArrayString chs(choices);
914 //return Create(parent, id, value, pos, size, chs.GetCount(),
915 // chs.GetStrings(), style, validator, name);
916 return Create(parent
, id
, value
, pos
, size
, 0,
917 NULL
, style
, validator
, name
);
920 bool wxOwnerDrawnComboBox::Create(wxWindow
*parent
,
922 const wxString
& value
,
926 const wxString choices
[],
928 const wxValidator
& validator
,
929 const wxString
& name
)
932 if ( !Create(parent
, id
, value
, pos
, size
, style
,
939 for ( i
=0; i
<n
; i
++ )
940 m_initChs
.Add(choices
[i
]);
945 wxOwnerDrawnComboBox::~wxOwnerDrawnComboBox()
947 if ( m_popupInterface
)
948 GetVListBoxComboPopup()->ClearClientDatas();
951 void wxOwnerDrawnComboBox::DoSetPopupControl(wxComboPopup
* popup
)
955 popup
= new wxVListBoxComboPopup();
958 wxComboCtrl::DoSetPopupControl(popup
);
962 // Add initial choices to the wxVListBox
963 if ( !GetVListBoxComboPopup()->GetCount() )
965 GetVListBoxComboPopup()->Populate(m_initChs
);
970 // ----------------------------------------------------------------------------
971 // wxOwnerDrawnComboBox item manipulation methods
972 // ----------------------------------------------------------------------------
974 void wxOwnerDrawnComboBox::DoClear()
976 EnsurePopupControl();
978 GetVListBoxComboPopup()->Clear();
980 SetValue(wxEmptyString
);
983 void wxOwnerDrawnComboBox::DoDeleteOneItem(unsigned int n
)
985 wxCHECK_RET( IsValid(n
), wxT("invalid index in wxOwnerDrawnComboBox::Delete") );
987 if ( GetSelection() == (int) n
)
988 SetValue(wxEmptyString
);
990 GetVListBoxComboPopup()->Delete(n
);
993 unsigned int wxOwnerDrawnComboBox::GetCount() const
995 if ( !m_popupInterface
)
996 return m_initChs
.GetCount();
998 return GetVListBoxComboPopup()->GetCount();
1001 wxString
wxOwnerDrawnComboBox::GetString(unsigned int n
) const
1003 wxCHECK_MSG( IsValid(n
), wxEmptyString
, wxT("invalid index in wxOwnerDrawnComboBox::GetString") );
1005 if ( !m_popupInterface
)
1006 return m_initChs
.Item(n
);
1008 return GetVListBoxComboPopup()->GetString(n
);
1011 void wxOwnerDrawnComboBox::SetString(unsigned int n
, const wxString
& s
)
1013 EnsurePopupControl();
1015 wxCHECK_RET( IsValid(n
), wxT("invalid index in wxOwnerDrawnComboBox::SetString") );
1017 GetVListBoxComboPopup()->SetString(n
,s
);
1020 int wxOwnerDrawnComboBox::FindString(const wxString
& s
, bool bCase
) const
1022 if ( !m_popupInterface
)
1023 return m_initChs
.Index(s
, bCase
);
1025 return GetVListBoxComboPopup()->FindString(s
, bCase
);
1028 void wxOwnerDrawnComboBox::Select(int n
)
1030 EnsurePopupControl();
1032 wxCHECK_RET( (n
== wxNOT_FOUND
) || IsValid(n
), wxT("invalid index in wxOwnerDrawnComboBox::Select") );
1034 GetVListBoxComboPopup()->SetSelection(n
);
1038 str
= GetVListBoxComboPopup()->GetString(n
);
1040 // Refresh text portion in control
1042 m_text
->SetValue( str
);
1044 m_valueString
= str
;
1049 int wxOwnerDrawnComboBox::GetSelection() const
1051 if ( !m_popupInterface
)
1052 return m_initChs
.Index(m_valueString
);
1054 return GetVListBoxComboPopup()->GetSelection();
1057 int wxOwnerDrawnComboBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
1060 wxClientDataType type
)
1062 EnsurePopupControl();
1064 const unsigned int count
= items
.GetCount();
1066 if ( HasFlag(wxCB_SORT
) )
1070 for ( unsigned int i
= 0; i
< count
; ++i
)
1072 n
= GetVListBoxComboPopup()->Append(items
[i
]);
1073 AssignNewItemClientData(n
, clientData
, i
, type
);
1080 for ( unsigned int i
= 0; i
< count
; ++i
, ++pos
)
1082 GetVListBoxComboPopup()->Insert(items
[i
], pos
);
1083 AssignNewItemClientData(pos
, clientData
, i
, type
);
1090 void wxOwnerDrawnComboBox::DoSetItemClientData(unsigned int n
, void* clientData
)
1092 EnsurePopupControl();
1094 GetVListBoxComboPopup()->SetItemClientData(n
, clientData
,
1095 GetClientDataType());
1098 void* wxOwnerDrawnComboBox::DoGetItemClientData(unsigned int n
) const
1100 if ( !m_popupInterface
)
1103 return GetVListBoxComboPopup()->GetItemClientData(n
);
1106 // ----------------------------------------------------------------------------
1107 // wxOwnerDrawnComboBox item drawing and measuring default implementations
1108 // ----------------------------------------------------------------------------
1110 void wxOwnerDrawnComboBox::OnDrawItem( wxDC
& dc
,
1115 if ( flags
& wxODCB_PAINTING_CONTROL
)
1119 if ( !ShouldUseHintText() )
1126 wxColour col
= wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
);
1127 dc
.SetTextForeground(col
);
1131 rect
.x
+ GetMargins().x
,
1132 (rect
.height
-dc
.GetCharHeight())/2 + rect
.y
);
1136 dc
.DrawText( GetVListBoxComboPopup()->GetString(item
), rect
.x
+ 2, rect
.y
);
1140 wxCoord
wxOwnerDrawnComboBox::OnMeasureItem( size_t WXUNUSED(item
) ) const
1145 wxCoord
wxOwnerDrawnComboBox::OnMeasureItemWidth( size_t WXUNUSED(item
) ) const
1150 void wxOwnerDrawnComboBox::OnDrawBackground(wxDC
& dc
,
1155 // We need only to explicitly draw background for items
1156 // that should have selected background. Also, call PrepareBackground
1157 // always when painting the control so that clipping is done properly.
1159 if ( (flags
& wxODCB_PAINTING_SELECTED
) ||
1160 ((flags
& wxODCB_PAINTING_CONTROL
) && HasFlag(wxCB_READONLY
)) )
1162 int bgFlags
= wxCONTROL_SELECTED
;
1164 if ( !(flags
& wxODCB_PAINTING_CONTROL
) )
1165 bgFlags
|= wxCONTROL_ISSUBMENU
;
1167 PrepareBackground(dc
, rect
, bgFlags
);
1171 #endif // wxUSE_ODCOMBOBOX