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 void wxVListBoxComboPopup::SetFocus()
101 // Suppress SetFocus() warning by simply not calling it. This combo popup
102 // has already been designed with the assumption that SetFocus() may not
103 // do anything useful, so it really doesn't need to be called.
107 wxVListBox::SetFocus();
111 bool wxVListBoxComboPopup::LazyCreate()
113 // NB: There is a bug with wxVListBox that can be avoided by creating
114 // it later (bug causes empty space to be shown if initial selection
115 // is at the end of a list longer than the control can show at once).
119 // paint the control itself
120 void wxVListBoxComboPopup::PaintComboControl( wxDC
& dc
, const wxRect
& rect
)
122 if ( !(m_combo
->GetWindowStyle() & wxODCB_STD_CONTROL_PAINT
) )
124 int flags
= wxODCB_PAINTING_CONTROL
;
126 if ( m_combo
->ShouldDrawFocus() )
127 flags
|= wxODCB_PAINTING_SELECTED
;
129 OnDrawBg(dc
, rect
, m_value
, flags
);
133 OnDrawItem(dc
,rect
,m_value
,flags
);
138 wxComboPopup::PaintComboControl(dc
,rect
);
141 void wxVListBoxComboPopup::OnDrawItem(wxDC
& dc
, const wxRect
& rect
, size_t n
) const
143 // TODO: Maybe this code could be moved to wxVListBox::OnPaint?
144 dc
.SetFont(m_useFont
);
148 // Set correct text colour for selected items
149 if ( wxVListBox::GetSelection() == (int) n
)
151 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) );
152 flags
|= wxODCB_PAINTING_SELECTED
;
156 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
) );
159 OnDrawItem(dc
,rect
,(int)n
,flags
);
162 wxCoord
wxVListBoxComboPopup::OnMeasureItem(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 wxCoord h
= combo
->OnMeasureItem(n
);
175 wxCoord
wxVListBoxComboPopup::OnMeasureItemWidth(size_t n
) const
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 return combo
->OnMeasureItemWidth(n
);
185 void wxVListBoxComboPopup::OnDrawBg( wxDC
& dc
,
190 wxOwnerDrawnComboBox
* combo
= (wxOwnerDrawnComboBox
*) m_combo
;
192 wxASSERT_MSG( combo
->IsKindOf(CLASSINFO(wxOwnerDrawnComboBox
)),
193 wxT("you must subclass wxVListBoxComboPopup for drawing and measuring methods") );
195 if ( IsCurrent((size_t)item
) && !(flags
& wxODCB_PAINTING_CONTROL
) )
196 flags
|= wxODCB_PAINTING_SELECTED
;
198 combo
->OnDrawBackground(dc
,rect
,item
,flags
);
201 void wxVListBoxComboPopup::OnDrawBackground(wxDC
& dc
, const wxRect
& rect
, size_t n
) const
203 OnDrawBg(dc
,rect
,(int)n
,0);
206 // This is called from wxVListBoxComboPopup::OnDrawItem, with text colour and font prepared
207 void wxVListBoxComboPopup::OnDrawItem( wxDC
& dc
, const wxRect
& rect
, int item
, int flags
) const
209 wxOwnerDrawnComboBox
* combo
= (wxOwnerDrawnComboBox
*) m_combo
;
211 wxASSERT_MSG( combo
->IsKindOf(CLASSINFO(wxOwnerDrawnComboBox
)),
212 wxT("you must subclass wxVListBoxComboPopup for drawing and measuring methods") );
214 combo
->OnDrawItem(dc
,rect
,item
,flags
);
217 void wxVListBoxComboPopup::DismissWithEvent()
219 StopPartialCompletion();
221 int selection
= wxVListBox::GetSelection();
225 if ( selection
!= wxNOT_FOUND
)
226 m_stringValue
= m_strings
[selection
];
228 m_stringValue
= wxEmptyString
;
230 if ( m_stringValue
!= m_combo
->GetValue() )
231 m_combo
->SetValueWithEvent(m_stringValue
);
235 SendComboBoxEvent(selection
);
238 void wxVListBoxComboPopup::SendComboBoxEvent( int selection
)
240 wxCommandEvent
evt(wxEVT_COMMAND_COMBOBOX_SELECTED
,m_combo
->GetId());
242 evt
.SetEventObject(m_combo
);
244 evt
.SetInt(selection
);
246 // Set client data, if any
247 if ( selection
>= 0 && (int)m_clientDatas
.GetCount() > selection
)
249 void* clientData
= m_clientDatas
[selection
];
250 if ( m_clientDataItemsType
== wxClientData_Object
)
251 evt
.SetClientObject((wxClientData
*)clientData
);
253 evt
.SetClientData(clientData
);
256 m_combo
->GetEventHandler()->AddPendingEvent(evt
);
259 // returns true if key was consumed
260 bool wxVListBoxComboPopup::HandleKey( int keycode
, bool saturate
, wxChar keychar
)
262 const int itemCount
= GetCount();
264 // keys do nothing in the empty control and returning immediately avoids
265 // using invalid indices below
270 int comboStyle
= m_combo
->GetWindowStyle();
274 // we have character equivalent of the keycode; filter out these that
275 // are not printable characters
276 if ( !wxIsprint(keychar
) )
280 if ( keycode
== WXK_DOWN
|| keycode
== WXK_NUMPAD_DOWN
|| keycode
== WXK_RIGHT
)
283 StopPartialCompletion();
285 else if ( keycode
== WXK_UP
|| keycode
== WXK_NUMPAD_UP
|| keycode
== WXK_LEFT
)
288 StopPartialCompletion();
290 else if ( keycode
== WXK_PAGEDOWN
|| keycode
== WXK_NUMPAD_PAGEDOWN
)
293 StopPartialCompletion();
295 else if ( keycode
== WXK_PAGEUP
|| keycode
== WXK_NUMPAD_PAGEUP
)
298 StopPartialCompletion();
300 else if ( keycode
== WXK_HOME
|| keycode
== WXK_NUMPAD_HOME
)
303 StopPartialCompletion();
305 else if ( keycode
== WXK_END
|| keycode
== WXK_NUMPAD_END
)
308 StopPartialCompletion();
310 else if ( keychar
&& (comboStyle
& wxCB_READONLY
) )
312 // Try partial completion
314 // find the new partial completion string
316 if (m_partialCompletionTimer
.IsRunning())
317 m_partialCompletionString
+=wxString(keychar
);
319 #endif // wxUSE_TIMER
320 m_partialCompletionString
=wxString(keychar
);
322 // now search through the values to see if this is found
324 unsigned int length
=m_partialCompletionString
.length();
326 for (i
=0; i
<itemCount
; i
++)
328 wxString item
=GetString(i
);
329 if (( item
.length() >= length
) && (! m_partialCompletionString
.CmpNoCase(item
.Left(length
))))
338 StopPartialCompletion();
340 return true; // to stop the first value being set
346 m_partialCompletionTimer
.Start(wxODCB_PARTIAL_COMPLETION_TIME
, true);
347 #endif // wxUSE_TIMER
355 if ( value
>= itemCount
)
356 value
= itemCount
- 1;
357 else if ( value
< 0 )
362 if ( value
>= itemCount
)
364 else if ( value
< 0 )
368 if ( value
== m_value
)
369 // Even if value was same, don't skip the event
370 // (good for consistency)
376 m_combo
->SetValue(m_strings
[value
]);
378 SendComboBoxEvent(m_value
);
383 // stop partial completion
384 void wxVListBoxComboPopup::StopPartialCompletion()
386 m_partialCompletionString
= wxEmptyString
;
388 m_partialCompletionTimer
.Stop();
389 #endif // wxUSE_TIMER
392 void wxVListBoxComboPopup::OnComboDoubleClick()
394 // Cycle on dclick (disable saturation to allow true cycling).
395 if ( !::wxGetKeyState(WXK_SHIFT
) )
396 HandleKey(WXK_DOWN
,false);
398 HandleKey(WXK_UP
,false);
401 void wxVListBoxComboPopup::OnComboKeyEvent( wxKeyEvent
& event
)
403 // Saturated key movement on
404 if ( !HandleKey(event
.GetKeyCode(), true) )
408 void wxVListBoxComboPopup::OnComboCharEvent( wxKeyEvent
& event
)
410 // unlike in OnComboKeyEvent, wxEVT_CHAR contains meaningful
411 // printable character information, so pass it
413 const wxChar charcode
= event
.GetUnicodeKey();
415 const wxChar charcode
= (wxChar
)event
.GetKeyCode();
418 if ( !HandleKey(event
.GetKeyCode(), true, charcode
) )
422 void wxVListBoxComboPopup::OnPopup()
424 // *must* set value after size is set (this is because of a vlbox bug)
425 wxVListBox::SetSelection(m_value
);
428 void wxVListBoxComboPopup::OnMouseMove(wxMouseEvent
& event
)
432 // Move selection to cursor if it is inside the popup
434 int y
= event
.GetPosition().y
;
435 int fromBottom
= GetClientSize().y
- y
;
437 // Since in any case we need to find out if the last item is only
438 // partially visible, we might just as well replicate the HitTest
440 const size_t lineMax
= GetVisibleEnd();
441 for ( size_t line
= GetVisibleBegin(); line
< lineMax
; line
++ )
443 y
-= OnGetRowHeight(line
);
446 // Only change selection if item is fully visible
447 if ( (y
+ fromBottom
) >= 0 )
449 wxVListBox::SetSelection((int)line
);
456 void wxVListBoxComboPopup::OnLeftClick(wxMouseEvent
& WXUNUSED(event
))
461 void wxVListBoxComboPopup::OnKey(wxKeyEvent
& event
)
463 // Hide popup if certain key or key combination was pressed
464 if ( m_combo
->IsKeyPopupToggle(event
) )
466 StopPartialCompletion();
469 else if ( event
.AltDown() )
471 // On both wxGTK and wxMSW, pressing Alt down seems to
472 // completely freeze things in popup (ie. arrow keys and
473 // enter won't work).
476 // Select item if ENTER is pressed
477 else if ( event
.GetKeyCode() == WXK_RETURN
|| event
.GetKeyCode() == WXK_NUMPAD_ENTER
)
483 // completion is handled in OnChar() below
488 void wxVListBoxComboPopup::OnChar(wxKeyEvent
& event
)
490 if ( m_combo
->GetWindowStyle() & wxCB_READONLY
)
492 // Process partial completion key codes here, but not the arrow keys as
493 // the base class will do that for us
495 const wxChar charcode
= event
.GetUnicodeKey();
497 const wxChar charcode
= (wxChar
)event
.GetKeyCode();
499 if ( wxIsprint(charcode
) )
501 OnComboCharEvent(event
);
502 SetSelection(m_value
); // ensure the highlight bar moves
503 return; // don't skip the event
510 void wxVListBoxComboPopup::Insert( const wxString
& item
, int pos
)
512 // Need to change selection?
514 if ( !(m_combo
->GetWindowStyle() & wxCB_READONLY
) &&
515 m_combo
->GetValue() == item
)
520 m_strings
.Insert(item
,pos
);
521 if ( (int)m_clientDatas
.size() >= pos
)
522 m_clientDatas
.Insert(NULL
, pos
);
524 m_widths
.Insert(-1,pos
);
525 m_widthsDirty
= true;
528 wxVListBox::SetItemCount( wxVListBox::GetItemCount()+1 );
531 int wxVListBoxComboPopup::Append(const wxString
& item
)
533 int pos
= (int)m_strings
.GetCount();
535 if ( m_combo
->GetWindowStyle() & wxCB_SORT
)
538 // TODO: Could be optimized with binary search
539 wxArrayString strings
= m_strings
;
542 for ( i
=0; i
<strings
.GetCount(); i
++ )
544 if ( item
.CmpNoCase(strings
.Item(i
)) < 0 )
557 void wxVListBoxComboPopup::Clear()
569 m_value
= wxNOT_FOUND
;
572 wxVListBox::SetItemCount(0);
575 void wxVListBoxComboPopup::ClearClientDatas()
577 if ( m_clientDataItemsType
== wxClientData_Object
)
580 for ( i
=0; i
<m_clientDatas
.GetCount(); i
++ )
581 delete (wxClientData
*) m_clientDatas
[i
];
584 m_clientDatas
.Empty();
587 void wxVListBoxComboPopup::SetItemClientData( unsigned int n
,
589 wxClientDataType clientDataItemsType
)
591 // It should be sufficient to update this variable only here
592 m_clientDataItemsType
= clientDataItemsType
;
594 m_clientDatas
[n
] = clientData
;
599 void* wxVListBoxComboPopup::GetItemClientData(unsigned int n
) const
601 if ( m_clientDatas
.GetCount() > n
)
602 return m_clientDatas
[n
];
607 void wxVListBoxComboPopup::Delete( unsigned int item
)
609 // Remove client data, if set
610 if ( m_clientDatas
.GetCount() )
612 if ( m_clientDataItemsType
== wxClientData_Object
)
613 delete (wxClientData
*) m_clientDatas
[item
];
615 m_clientDatas
.RemoveAt(item
);
618 m_strings
.RemoveAt(item
);
619 m_widths
.RemoveAt(item
);
621 if ( (int)item
== m_widestItem
)
624 int sel
= GetSelection();
627 wxVListBox::SetItemCount( wxVListBox::GetItemCount()-1 );
630 if ( (int)item
< sel
)
632 else if ( (int)item
== sel
)
633 SetSelection(wxNOT_FOUND
);
636 int wxVListBoxComboPopup::FindString(const wxString
& s
, bool bCase
) const
638 return m_strings
.Index(s
, bCase
);
641 unsigned int wxVListBoxComboPopup::GetCount() const
643 return m_strings
.GetCount();
646 wxString
wxVListBoxComboPopup::GetString( int item
) const
648 return m_strings
[item
];
651 void wxVListBoxComboPopup::SetString( int item
, const wxString
& str
)
653 m_strings
[item
] = str
;
654 ItemWidthChanged(item
);
657 wxString
wxVListBoxComboPopup::GetStringValue() const
659 return m_stringValue
;
662 void wxVListBoxComboPopup::SetSelection( int item
)
664 wxCHECK_RET( item
== wxNOT_FOUND
|| ((unsigned int)item
< GetCount()),
665 wxT("invalid index in wxVListBoxComboPopup::SetSelection") );
670 m_stringValue
= m_strings
[item
];
672 m_stringValue
= wxEmptyString
;
675 wxVListBox::SetSelection(item
);
678 int wxVListBoxComboPopup::GetSelection() const
683 void wxVListBoxComboPopup::SetStringValue( const wxString
& value
)
685 int index
= m_strings
.Index(value
);
687 m_stringValue
= value
;
689 if ( index
>= 0 && index
< (int)wxVListBox::GetItemCount() )
691 wxVListBox::SetSelection(index
);
696 void wxVListBoxComboPopup::CalcWidths()
698 bool doFindWidest
= m_findWidest
;
700 // Measure items with dirty width.
704 unsigned int n
= m_widths
.GetCount();
705 int dirtyHandled
= 0;
706 wxArrayInt
& widths
= m_widths
;
708 // I think using wxDC::GetTextExtent is faster than
709 // wxWindow::GetTextExtent (assuming same dc is used
710 // for all calls, as we do here).
711 wxClientDC
dc(m_combo
);
712 dc
.SetFont(m_useFont
);
714 for ( i
=0; i
<n
; i
++ )
718 wxCoord x
= OnMeasureItemWidth(i
);
722 const wxString
& text
= m_strings
[i
];
724 // To make sure performance won't suck in extreme scenarios,
725 // we'll estimate length after some arbitrary number of items
726 // have been checked precily.
727 if ( dirtyHandled
< 1024 )
730 dc
.GetTextExtent(text
, &x
, &y
, 0, 0);
735 x
= text
.length() * (dc
.GetCharWidth()+1);
741 if ( x
>= m_widestWidth
)
744 m_widestItem
= (int)i
;
746 else if ( (int)i
== m_widestItem
)
748 // Width of previously widest item has been decreased, so
749 // we'll have to check all to find current widest item.
757 m_widthsDirty
= false;
763 unsigned int n
= m_widths
.GetCount();
768 for ( i
=0; i
<n
; i
++ )
778 m_widestWidth
= bestWidth
;
779 m_widestItem
= bestIndex
;
781 m_findWidest
= false;
785 wxSize
wxVListBoxComboPopup::GetAdjustedSize( int minWidth
, int prefHeight
, int maxHeight
)
789 maxHeight
-= 2; // Must take borders into account
791 if ( m_strings
.GetCount() )
793 if ( prefHeight
> 0 )
796 if ( height
> maxHeight
)
799 int totalHeight
= GetTotalHeight(); // + 3;
801 // Take borders into account on Mac or scrollbars always appear
802 #if defined(__WXMAC__)
805 if ( height
>= totalHeight
)
807 height
= totalHeight
;
811 // Adjust height to a multiple of the height of the first item
812 // NB: Calculations that take variable height into account
814 int fih
= GetLineHeight(0);
815 height
-= height
% fih
;
823 // Take scrollbar into account in width calculations
824 int widestWidth
= m_widestWidth
+ wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
825 return wxSize(minWidth
> widestWidth
? minWidth
: widestWidth
,
829 //void wxVListBoxComboPopup::Populate( int n, const wxString choices[] )
830 void wxVListBoxComboPopup::Populate( const wxArrayString
& choices
)
834 int n
= choices
.GetCount();
836 for ( i
=0; i
<n
; i
++ )
838 const wxString
& item
= choices
.Item(i
);
842 m_widths
.SetCount(n
,-1);
843 m_widthsDirty
= true;
846 wxVListBox::SetItemCount(n
);
848 // Sort the initial choices
849 if ( m_combo
->GetWindowStyle() & wxCB_SORT
)
852 // Find initial selection
853 wxString strValue
= m_combo
->GetValue();
854 if ( strValue
.length() )
855 m_value
= m_strings
.Index(strValue
);
858 // ----------------------------------------------------------------------------
859 // wxOwnerDrawnComboBox
860 // ----------------------------------------------------------------------------
863 BEGIN_EVENT_TABLE(wxOwnerDrawnComboBox
, wxComboCtrl
)
867 #if wxUSE_EXTENDED_RTTI
868 IMPLEMENT_DYNAMIC_CLASS2_XTI(wxOwnerDrawnComboBox
, wxComboCtrl
, wxControlWithItems
, "wx/odcombo.h")
870 wxBEGIN_PROPERTIES_TABLE(wxOwnerDrawnComboBox
)
871 wxEND_PROPERTIES_TABLE()
873 wxBEGIN_HANDLERS_TABLE(wxOwnerDrawnComboBox
)
874 wxEND_HANDLERS_TABLE()
876 wxCONSTRUCTOR_5( wxOwnerDrawnComboBox
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, Value
, wxPoint
, Position
, wxSize
, Size
)
878 IMPLEMENT_DYNAMIC_CLASS2(wxOwnerDrawnComboBox
, wxComboCtrl
, wxControlWithItems
)
881 void wxOwnerDrawnComboBox::Init()
885 bool wxOwnerDrawnComboBox::Create(wxWindow
*parent
,
887 const wxString
& value
,
891 const wxValidator
& validator
,
892 const wxString
& name
)
894 return wxComboCtrl::Create(parent
,id
,value
,pos
,size
,style
,validator
,name
);
897 wxOwnerDrawnComboBox::wxOwnerDrawnComboBox(wxWindow
*parent
,
899 const wxString
& value
,
902 const wxArrayString
& choices
,
904 const wxValidator
& validator
,
905 const wxString
& name
)
910 Create(parent
,id
,value
,pos
,size
,choices
,style
, validator
, name
);
913 bool wxOwnerDrawnComboBox::Create(wxWindow
*parent
,
915 const wxString
& value
,
918 const wxArrayString
& choices
,
920 const wxValidator
& validator
,
921 const wxString
& name
)
924 //wxCArrayString chs(choices);
926 //return Create(parent, id, value, pos, size, chs.GetCount(),
927 // chs.GetStrings(), style, validator, name);
928 return Create(parent
, id
, value
, pos
, size
, 0,
929 NULL
, style
, validator
, name
);
932 bool wxOwnerDrawnComboBox::Create(wxWindow
*parent
,
934 const wxString
& value
,
938 const wxString choices
[],
940 const wxValidator
& validator
,
941 const wxString
& name
)
944 if ( !Create(parent
, id
, value
, pos
, size
, style
,
951 for ( i
=0; i
<n
; i
++ )
952 m_initChs
.Add(choices
[i
]);
957 wxOwnerDrawnComboBox::~wxOwnerDrawnComboBox()
959 if ( m_popupInterface
)
960 GetVListBoxComboPopup()->ClearClientDatas();
963 void wxOwnerDrawnComboBox::DoSetPopupControl(wxComboPopup
* popup
)
967 popup
= new wxVListBoxComboPopup();
970 wxComboCtrl::DoSetPopupControl(popup
);
974 // Add initial choices to the wxVListBox
975 if ( !GetVListBoxComboPopup()->GetCount() )
977 GetVListBoxComboPopup()->Populate(m_initChs
);
982 // ----------------------------------------------------------------------------
983 // wxOwnerDrawnComboBox item manipulation methods
984 // ----------------------------------------------------------------------------
986 void wxOwnerDrawnComboBox::DoClear()
988 EnsurePopupControl();
990 GetVListBoxComboPopup()->Clear();
992 SetValue(wxEmptyString
);
995 void wxOwnerDrawnComboBox::DoDeleteOneItem(unsigned int n
)
997 wxCHECK_RET( IsValid(n
), wxT("invalid index in wxOwnerDrawnComboBox::Delete") );
999 if ( GetSelection() == (int) n
)
1000 SetValue(wxEmptyString
);
1002 GetVListBoxComboPopup()->Delete(n
);
1005 unsigned int wxOwnerDrawnComboBox::GetCount() const
1007 if ( !m_popupInterface
)
1008 return m_initChs
.GetCount();
1010 return GetVListBoxComboPopup()->GetCount();
1013 wxString
wxOwnerDrawnComboBox::GetString(unsigned int n
) const
1015 wxCHECK_MSG( IsValid(n
), wxEmptyString
, wxT("invalid index in wxOwnerDrawnComboBox::GetString") );
1017 if ( !m_popupInterface
)
1018 return m_initChs
.Item(n
);
1020 return GetVListBoxComboPopup()->GetString(n
);
1023 void wxOwnerDrawnComboBox::SetString(unsigned int n
, const wxString
& s
)
1025 EnsurePopupControl();
1027 wxCHECK_RET( IsValid(n
), wxT("invalid index in wxOwnerDrawnComboBox::SetString") );
1029 GetVListBoxComboPopup()->SetString(n
,s
);
1032 int wxOwnerDrawnComboBox::FindString(const wxString
& s
, bool bCase
) const
1034 if ( !m_popupInterface
)
1035 return m_initChs
.Index(s
, bCase
);
1037 return GetVListBoxComboPopup()->FindString(s
, bCase
);
1040 void wxOwnerDrawnComboBox::Select(int n
)
1042 EnsurePopupControl();
1044 wxCHECK_RET( (n
== wxNOT_FOUND
) || IsValid(n
), wxT("invalid index in wxOwnerDrawnComboBox::Select") );
1046 GetVListBoxComboPopup()->SetSelection(n
);
1050 str
= GetVListBoxComboPopup()->GetString(n
);
1052 // Refresh text portion in control
1054 m_text
->SetValue( str
);
1056 m_valueString
= str
;
1061 int wxOwnerDrawnComboBox::GetSelection() const
1063 if ( !m_popupInterface
)
1064 return m_initChs
.Index(m_valueString
);
1066 return GetVListBoxComboPopup()->GetSelection();
1069 int wxOwnerDrawnComboBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
1072 wxClientDataType type
)
1074 EnsurePopupControl();
1076 const unsigned int count
= items
.GetCount();
1078 if ( HasFlag(wxCB_SORT
) )
1082 for ( unsigned int i
= 0; i
< count
; ++i
)
1084 n
= GetVListBoxComboPopup()->Append(items
[i
]);
1085 AssignNewItemClientData(n
, clientData
, i
, type
);
1092 for ( unsigned int i
= 0; i
< count
; ++i
, ++pos
)
1094 GetVListBoxComboPopup()->Insert(items
[i
], pos
);
1095 AssignNewItemClientData(pos
, clientData
, i
, type
);
1102 void wxOwnerDrawnComboBox::DoSetItemClientData(unsigned int n
, void* clientData
)
1104 EnsurePopupControl();
1106 GetVListBoxComboPopup()->SetItemClientData(n
, clientData
,
1107 GetClientDataType());
1110 void* wxOwnerDrawnComboBox::DoGetItemClientData(unsigned int n
) const
1112 if ( !m_popupInterface
)
1115 return GetVListBoxComboPopup()->GetItemClientData(n
);
1118 // ----------------------------------------------------------------------------
1119 // wxOwnerDrawnComboBox item drawing and measuring default implementations
1120 // ----------------------------------------------------------------------------
1122 void wxOwnerDrawnComboBox::OnDrawItem( wxDC
& dc
,
1127 if ( flags
& wxODCB_PAINTING_CONTROL
)
1131 if ( !ShouldUseHintText() )
1138 wxColour col
= wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
);
1139 dc
.SetTextForeground(col
);
1143 rect
.x
+ GetMargins().x
,
1144 (rect
.height
-dc
.GetCharHeight())/2 + rect
.y
);
1148 dc
.DrawText( GetVListBoxComboPopup()->GetString(item
), rect
.x
+ 2, rect
.y
);
1152 wxCoord
wxOwnerDrawnComboBox::OnMeasureItem( size_t WXUNUSED(item
) ) const
1157 wxCoord
wxOwnerDrawnComboBox::OnMeasureItemWidth( size_t WXUNUSED(item
) ) const
1162 void wxOwnerDrawnComboBox::OnDrawBackground(wxDC
& dc
,
1167 // We need only to explicitly draw background for items
1168 // that should have selected background. Also, call PrepareBackground
1169 // always when painting the control so that clipping is done properly.
1171 if ( (flags
& wxODCB_PAINTING_SELECTED
) ||
1172 ((flags
& wxODCB_PAINTING_CONTROL
) && HasFlag(wxCB_READONLY
)) )
1174 int bgFlags
= wxCONTROL_SELECTED
;
1176 if ( !(flags
& wxODCB_PAINTING_CONTROL
) )
1177 bgFlags
|= wxCONTROL_ISSUBMENU
;
1179 PrepareBackground(dc
, rect
, bgFlags
);
1183 #endif // wxUSE_ODCOMBOBOX