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( wxDynamicCast(combo
, 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( wxDynamicCast(combo
, 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( wxDynamicCast(combo
, 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( wxDynamicCast(combo
, 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
->SetValueByUser(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 const bool readOnly
= (comboStyle
& wxCB_READONLY
) != 0;
282 if ( keycode
== WXK_DOWN
|| keycode
== WXK_NUMPAD_DOWN
|| ( keycode
== WXK_RIGHT
&& readOnly
) )
285 StopPartialCompletion();
287 else if ( keycode
== WXK_UP
|| keycode
== WXK_NUMPAD_UP
|| ( keycode
== WXK_LEFT
&& readOnly
) )
290 StopPartialCompletion();
292 else if ( keycode
== WXK_PAGEDOWN
|| keycode
== WXK_NUMPAD_PAGEDOWN
)
295 StopPartialCompletion();
297 else if ( keycode
== WXK_PAGEUP
|| keycode
== WXK_NUMPAD_PAGEUP
)
300 StopPartialCompletion();
302 else if ( ( keycode
== WXK_HOME
|| keycode
== WXK_NUMPAD_HOME
) && readOnly
)
305 StopPartialCompletion();
307 else if ( ( keycode
== WXK_END
|| keycode
== WXK_NUMPAD_END
) && readOnly
)
310 StopPartialCompletion();
312 else if ( keychar
&& readOnly
)
314 // Try partial completion
316 // find the new partial completion string
318 if (m_partialCompletionTimer
.IsRunning())
319 m_partialCompletionString
+=wxString(keychar
);
321 #endif // wxUSE_TIMER
322 m_partialCompletionString
=wxString(keychar
);
324 // now search through the values to see if this is found
326 unsigned int length
=m_partialCompletionString
.length();
328 for (i
=0; i
<itemCount
; i
++)
330 wxString item
=GetString(i
);
331 if (( item
.length() >= length
) && (! m_partialCompletionString
.CmpNoCase(item
.Left(length
))))
340 StopPartialCompletion();
342 return true; // to stop the first value being set
348 m_partialCompletionTimer
.Start(wxODCB_PARTIAL_COMPLETION_TIME
, true);
349 #endif // wxUSE_TIMER
357 if ( value
>= itemCount
)
358 value
= itemCount
- 1;
359 else if ( value
< 0 )
364 if ( value
>= itemCount
)
366 else if ( value
< 0 )
370 if ( value
== m_value
)
371 // Even if value was same, don't skip the event
372 // (good for consistency)
376 m_combo
->ChangeValue(m_strings
[value
]);
378 // The m_combo->SetValue() call above sets m_value to the index of this
379 // string. But if there are more identical string, the index is of the
380 // first occurrence, which may be wrong, so set the index explicitly here,
381 // _after_ the SetValue() call.
384 SendComboBoxEvent(m_value
);
389 // stop partial completion
390 void wxVListBoxComboPopup::StopPartialCompletion()
392 m_partialCompletionString
= wxEmptyString
;
394 m_partialCompletionTimer
.Stop();
395 #endif // wxUSE_TIMER
398 void wxVListBoxComboPopup::OnComboDoubleClick()
400 // Cycle on dclick (disable saturation to allow true cycling).
401 if ( !::wxGetKeyState(WXK_SHIFT
) )
402 HandleKey(WXK_DOWN
,false);
404 HandleKey(WXK_UP
,false);
407 void wxVListBoxComboPopup::OnComboKeyEvent( wxKeyEvent
& event
)
409 // Saturated key movement on
410 if ( !HandleKey(event
.GetKeyCode(), true) )
414 void wxVListBoxComboPopup::OnComboCharEvent( wxKeyEvent
& event
)
416 // unlike in OnComboKeyEvent, wxEVT_CHAR contains meaningful
417 // printable character information, so pass it
419 const wxChar charcode
= event
.GetUnicodeKey();
421 const wxChar charcode
= (wxChar
)event
.GetKeyCode();
424 if ( !HandleKey(event
.GetKeyCode(), true, charcode
) )
428 void wxVListBoxComboPopup::OnPopup()
430 // *must* set value after size is set (this is because of a vlbox bug)
431 wxVListBox::SetSelection(m_value
);
434 void wxVListBoxComboPopup::OnMouseMove(wxMouseEvent
& event
)
438 // Move selection to cursor if it is inside the popup
440 int y
= event
.GetPosition().y
;
441 int fromBottom
= GetClientSize().y
- y
;
443 // Since in any case we need to find out if the last item is only
444 // partially visible, we might just as well replicate the HitTest
446 const size_t lineMax
= GetVisibleEnd();
447 for ( size_t line
= GetVisibleBegin(); line
< lineMax
; line
++ )
449 y
-= OnGetRowHeight(line
);
452 // Only change selection if item is fully visible
453 if ( (y
+ fromBottom
) >= 0 )
455 wxVListBox::SetSelection((int)line
);
462 void wxVListBoxComboPopup::OnLeftClick(wxMouseEvent
& WXUNUSED(event
))
467 void wxVListBoxComboPopup::OnKey(wxKeyEvent
& event
)
469 // Hide popup if certain key or key combination was pressed
470 if ( m_combo
->IsKeyPopupToggle(event
) )
472 StopPartialCompletion();
475 else if ( event
.AltDown() )
477 // On both wxGTK and wxMSW, pressing Alt down seems to
478 // completely freeze things in popup (ie. arrow keys and
479 // enter won't work).
482 // Select item if ENTER is pressed
483 else if ( event
.GetKeyCode() == WXK_RETURN
|| event
.GetKeyCode() == WXK_NUMPAD_ENTER
)
489 // completion is handled in OnChar() below
494 void wxVListBoxComboPopup::OnChar(wxKeyEvent
& event
)
496 if ( m_combo
->GetWindowStyle() & wxCB_READONLY
)
498 // Process partial completion key codes here, but not the arrow keys as
499 // the base class will do that for us
501 const wxChar charcode
= event
.GetUnicodeKey();
503 const wxChar charcode
= (wxChar
)event
.GetKeyCode();
505 if ( wxIsprint(charcode
) )
507 OnComboCharEvent(event
);
508 SetSelection(m_value
); // ensure the highlight bar moves
509 return; // don't skip the event
516 void wxVListBoxComboPopup::Insert( const wxString
& item
, int pos
)
518 // Need to change selection?
520 if ( !(m_combo
->GetWindowStyle() & wxCB_READONLY
) &&
521 m_combo
->GetValue() == item
)
526 m_strings
.Insert(item
,pos
);
527 if ( (int)m_clientDatas
.size() >= pos
)
528 m_clientDatas
.Insert(NULL
, pos
);
530 m_widths
.Insert(-1,pos
);
531 m_widthsDirty
= true;
534 wxVListBox::SetItemCount( wxVListBox::GetItemCount()+1 );
537 int wxVListBoxComboPopup::Append(const wxString
& item
)
539 int pos
= (int)m_strings
.GetCount();
541 if ( m_combo
->GetWindowStyle() & wxCB_SORT
)
544 // TODO: Could be optimized with binary search
545 wxArrayString strings
= m_strings
;
548 for ( i
=0; i
<strings
.GetCount(); i
++ )
550 if ( item
.CmpNoCase(strings
.Item(i
)) <= 0 )
563 void wxVListBoxComboPopup::Clear()
575 m_value
= wxNOT_FOUND
;
578 wxVListBox::SetItemCount(0);
581 void wxVListBoxComboPopup::ClearClientDatas()
583 if ( m_clientDataItemsType
== wxClientData_Object
)
586 for ( i
=0; i
<m_clientDatas
.GetCount(); i
++ )
587 delete (wxClientData
*) m_clientDatas
[i
];
590 m_clientDatas
.Empty();
593 void wxVListBoxComboPopup::SetItemClientData( unsigned int n
,
595 wxClientDataType clientDataItemsType
)
597 // It should be sufficient to update this variable only here
598 m_clientDataItemsType
= clientDataItemsType
;
600 m_clientDatas
[n
] = clientData
;
605 void* wxVListBoxComboPopup::GetItemClientData(unsigned int n
) const
607 if ( m_clientDatas
.GetCount() > n
)
608 return m_clientDatas
[n
];
613 void wxVListBoxComboPopup::Delete( unsigned int item
)
615 // Remove client data, if set
616 if ( m_clientDatas
.GetCount() )
618 if ( m_clientDataItemsType
== wxClientData_Object
)
619 delete (wxClientData
*) m_clientDatas
[item
];
621 m_clientDatas
.RemoveAt(item
);
624 m_strings
.RemoveAt(item
);
625 m_widths
.RemoveAt(item
);
627 if ( (int)item
== m_widestItem
)
630 int sel
= GetSelection();
633 wxVListBox::SetItemCount( wxVListBox::GetItemCount()-1 );
636 if ( (int)item
< sel
)
638 else if ( (int)item
== sel
)
639 SetSelection(wxNOT_FOUND
);
642 int wxVListBoxComboPopup::FindString(const wxString
& s
, bool bCase
) const
644 return m_strings
.Index(s
, bCase
);
647 bool wxVListBoxComboPopup::FindItem(const wxString
& item
, wxString
* trueItem
)
649 int idx
= m_strings
.Index(item
, false);
650 if ( idx
== wxNOT_FOUND
)
652 if ( trueItem
!= NULL
)
653 *trueItem
= m_strings
[idx
];
657 unsigned int wxVListBoxComboPopup::GetCount() const
659 return m_strings
.GetCount();
662 wxString
wxVListBoxComboPopup::GetString( int item
) const
664 return m_strings
[item
];
667 void wxVListBoxComboPopup::SetString( int item
, const wxString
& str
)
669 m_strings
[item
] = str
;
670 ItemWidthChanged(item
);
673 wxString
wxVListBoxComboPopup::GetStringValue() const
675 return m_stringValue
;
678 void wxVListBoxComboPopup::SetSelection( int item
)
680 wxCHECK_RET( item
== wxNOT_FOUND
|| ((unsigned int)item
< GetCount()),
681 wxT("invalid index in wxVListBoxComboPopup::SetSelection") );
686 m_stringValue
= m_strings
[item
];
688 m_stringValue
= wxEmptyString
;
691 wxVListBox::SetSelection(item
);
694 int wxVListBoxComboPopup::GetSelection() const
699 void wxVListBoxComboPopup::SetStringValue( const wxString
& value
)
701 int index
= m_strings
.Index(value
);
703 m_stringValue
= value
;
705 if ( index
>= 0 && index
< (int)wxVListBox::GetItemCount() )
707 wxVListBox::SetSelection(index
);
712 void wxVListBoxComboPopup::CalcWidths()
714 bool doFindWidest
= m_findWidest
;
716 // Measure items with dirty width.
720 unsigned int n
= m_widths
.GetCount();
721 int dirtyHandled
= 0;
722 wxArrayInt
& widths
= m_widths
;
724 // I think using wxDC::GetTextExtent is faster than
725 // wxWindow::GetTextExtent (assuming same dc is used
726 // for all calls, as we do here).
727 wxClientDC
dc(m_combo
);
728 if ( !m_useFont
.IsOk() )
729 m_useFont
= m_combo
->GetFont();
730 dc
.SetFont(m_useFont
);
732 for ( i
=0; i
<n
; i
++ )
736 wxCoord x
= OnMeasureItemWidth(i
);
740 const wxString
& text
= m_strings
[i
];
742 // To make sure performance won't suck in extreme scenarios,
743 // we'll estimate length after some arbitrary number of items
744 // have been checked precily.
745 if ( dirtyHandled
< 1024 )
748 dc
.GetTextExtent(text
, &x
, &y
, 0, 0);
753 x
= text
.length() * (dc
.GetCharWidth()+1);
759 if ( x
>= m_widestWidth
)
762 m_widestItem
= (int)i
;
764 else if ( (int)i
== m_widestItem
)
766 // Width of previously widest item has been decreased, so
767 // we'll have to check all to find current widest item.
775 m_widthsDirty
= false;
781 unsigned int n
= m_widths
.GetCount();
786 for ( i
=0; i
<n
; i
++ )
796 m_widestWidth
= bestWidth
;
797 m_widestItem
= bestIndex
;
799 m_findWidest
= false;
803 wxSize
wxVListBoxComboPopup::GetAdjustedSize( int minWidth
, int prefHeight
, int maxHeight
)
807 maxHeight
-= 2; // Must take borders into account
809 if ( m_strings
.GetCount() )
811 if ( prefHeight
> 0 )
814 if ( height
> maxHeight
)
817 int totalHeight
= GetTotalHeight(); // + 3;
819 // Take borders into account on Mac or scrollbars always appear
820 #if defined(__WXMAC__)
823 if ( height
>= totalHeight
)
825 height
= totalHeight
;
829 // Adjust height to a multiple of the height of the first item
830 // NB: Calculations that take variable height into account
832 int fih
= GetLineHeight(0);
833 height
-= height
% fih
;
841 // Take scrollbar into account in width calculations
842 int widestWidth
= m_widestWidth
+ wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
843 return wxSize(minWidth
> widestWidth
? minWidth
: widestWidth
,
847 //void wxVListBoxComboPopup::Populate( int n, const wxString choices[] )
848 void wxVListBoxComboPopup::Populate( const wxArrayString
& choices
)
852 int n
= choices
.GetCount();
854 for ( i
=0; i
<n
; i
++ )
856 const wxString
& item
= choices
.Item(i
);
860 m_widths
.SetCount(n
,-1);
861 m_widthsDirty
= true;
864 wxVListBox::SetItemCount(n
);
866 // Sort the initial choices
867 if ( m_combo
->GetWindowStyle() & wxCB_SORT
)
870 // Find initial selection
871 wxString strValue
= m_combo
->GetValue();
872 if ( !strValue
.empty() )
873 m_value
= m_strings
.Index(strValue
);
876 // ----------------------------------------------------------------------------
877 // wxOwnerDrawnComboBox
878 // ----------------------------------------------------------------------------
881 BEGIN_EVENT_TABLE(wxOwnerDrawnComboBox
, wxComboCtrl
)
884 void wxOwnerDrawnComboBox::Init()
888 bool wxOwnerDrawnComboBox::Create(wxWindow
*parent
,
890 const wxString
& value
,
894 const wxValidator
& validator
,
895 const wxString
& name
)
897 return wxComboCtrl::Create(parent
,id
,value
,pos
,size
,style
,validator
,name
);
900 wxOwnerDrawnComboBox::wxOwnerDrawnComboBox(wxWindow
*parent
,
902 const wxString
& value
,
905 const wxArrayString
& choices
,
907 const wxValidator
& validator
,
908 const wxString
& name
)
912 Create(parent
,id
,value
,pos
,size
,choices
,style
, validator
, name
);
915 bool wxOwnerDrawnComboBox::Create(wxWindow
*parent
,
917 const wxString
& value
,
920 const wxArrayString
& choices
,
922 const wxValidator
& validator
,
923 const wxString
& name
)
926 //wxCArrayString chs(choices);
928 //return Create(parent, id, value, pos, size, chs.GetCount(),
929 // chs.GetStrings(), style, validator, name);
930 return Create(parent
, id
, value
, pos
, size
, 0,
931 NULL
, style
, validator
, name
);
934 bool wxOwnerDrawnComboBox::Create(wxWindow
*parent
,
936 const wxString
& value
,
940 const wxString choices
[],
942 const wxValidator
& validator
,
943 const wxString
& name
)
946 if ( !Create(parent
, id
, value
, pos
, size
, style
,
953 for ( i
=0; i
<n
; i
++ )
954 m_initChs
.Add(choices
[i
]);
959 wxOwnerDrawnComboBox::~wxOwnerDrawnComboBox()
961 if ( m_popupInterface
)
962 GetVListBoxComboPopup()->ClearClientDatas();
965 void wxOwnerDrawnComboBox::DoSetPopupControl(wxComboPopup
* popup
)
969 popup
= new wxVListBoxComboPopup();
972 wxComboCtrl::DoSetPopupControl(popup
);
976 // Add initial choices to the wxVListBox
977 if ( !GetVListBoxComboPopup()->GetCount() )
979 GetVListBoxComboPopup()->Populate(m_initChs
);
984 // ----------------------------------------------------------------------------
985 // wxOwnerDrawnComboBox item manipulation methods
986 // ----------------------------------------------------------------------------
988 void wxOwnerDrawnComboBox::DoClear()
990 EnsurePopupControl();
992 GetVListBoxComboPopup()->Clear();
994 // NB: This really needs to be SetValue() instead of ChangeValue(),
995 // as wxTextEntry API expects an event to be sent.
996 SetValue(wxEmptyString
);
999 void wxOwnerDrawnComboBox::Clear()
1004 void wxOwnerDrawnComboBox::DoDeleteOneItem(unsigned int n
)
1006 wxCHECK_RET( IsValid(n
), wxT("invalid index in wxOwnerDrawnComboBox::Delete") );
1008 if ( GetSelection() == (int) n
)
1009 ChangeValue(wxEmptyString
);
1011 GetVListBoxComboPopup()->Delete(n
);
1014 unsigned int wxOwnerDrawnComboBox::GetCount() const
1016 if ( !m_popupInterface
)
1017 return m_initChs
.GetCount();
1019 return GetVListBoxComboPopup()->GetCount();
1022 wxString
wxOwnerDrawnComboBox::GetString(unsigned int n
) const
1024 wxCHECK_MSG( IsValid(n
), wxEmptyString
, wxT("invalid index in wxOwnerDrawnComboBox::GetString") );
1026 if ( !m_popupInterface
)
1027 return m_initChs
.Item(n
);
1029 return GetVListBoxComboPopup()->GetString(n
);
1032 void wxOwnerDrawnComboBox::SetString(unsigned int n
, const wxString
& s
)
1034 EnsurePopupControl();
1036 wxCHECK_RET( IsValid(n
), wxT("invalid index in wxOwnerDrawnComboBox::SetString") );
1038 GetVListBoxComboPopup()->SetString(n
,s
);
1041 int wxOwnerDrawnComboBox::FindString(const wxString
& s
, bool bCase
) const
1043 if ( !m_popupInterface
)
1044 return m_initChs
.Index(s
, bCase
);
1046 return GetVListBoxComboPopup()->FindString(s
, bCase
);
1049 void wxOwnerDrawnComboBox::Select(int n
)
1051 EnsurePopupControl();
1053 wxCHECK_RET( (n
== wxNOT_FOUND
) || IsValid(n
), wxT("invalid index in wxOwnerDrawnComboBox::Select") );
1055 GetVListBoxComboPopup()->SetSelection(n
);
1059 str
= GetVListBoxComboPopup()->GetString(n
);
1061 // Refresh text portion in control
1063 m_text
->ChangeValue( str
);
1065 m_valueString
= str
;
1070 int wxOwnerDrawnComboBox::GetSelection() const
1072 if ( !m_popupInterface
)
1073 return m_initChs
.Index(m_valueString
);
1075 return GetVListBoxComboPopup()->GetSelection();
1078 void wxOwnerDrawnComboBox::GetSelection(long *from
, long *to
) const
1080 wxComboCtrl::GetSelection(from
, to
);
1083 int wxOwnerDrawnComboBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
1086 wxClientDataType type
)
1088 EnsurePopupControl();
1090 const unsigned int count
= items
.GetCount();
1092 if ( HasFlag(wxCB_SORT
) )
1096 for ( unsigned int i
= 0; i
< count
; ++i
)
1098 n
= GetVListBoxComboPopup()->Append(items
[i
]);
1099 AssignNewItemClientData(n
, clientData
, i
, type
);
1106 for ( unsigned int i
= 0; i
< count
; ++i
, ++pos
)
1108 GetVListBoxComboPopup()->Insert(items
[i
], pos
);
1109 AssignNewItemClientData(pos
, clientData
, i
, type
);
1116 void wxOwnerDrawnComboBox::DoSetItemClientData(unsigned int n
, void* clientData
)
1118 EnsurePopupControl();
1120 GetVListBoxComboPopup()->SetItemClientData(n
, clientData
,
1121 GetClientDataType());
1124 void* wxOwnerDrawnComboBox::DoGetItemClientData(unsigned int n
) const
1126 if ( !m_popupInterface
)
1129 return GetVListBoxComboPopup()->GetItemClientData(n
);
1132 // ----------------------------------------------------------------------------
1133 // wxOwnerDrawnComboBox item drawing and measuring default implementations
1134 // ----------------------------------------------------------------------------
1136 void wxOwnerDrawnComboBox::OnDrawItem( wxDC
& dc
,
1141 if ( flags
& wxODCB_PAINTING_CONTROL
)
1145 if ( !ShouldUseHintText() )
1152 wxColour col
= wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
);
1153 dc
.SetTextForeground(col
);
1157 rect
.x
+ GetMargins().x
,
1158 (rect
.height
-dc
.GetCharHeight())/2 + rect
.y
);
1162 dc
.DrawText( GetVListBoxComboPopup()->GetString(item
), rect
.x
+ 2, rect
.y
);
1166 wxCoord
wxOwnerDrawnComboBox::OnMeasureItem( size_t WXUNUSED(item
) ) const
1171 wxCoord
wxOwnerDrawnComboBox::OnMeasureItemWidth( size_t WXUNUSED(item
) ) const
1176 wxSize
wxOwnerDrawnComboBox::DoGetBestSize() const
1178 wxSize
best( wxComboCtrlBase::DoGetBestSize() );
1180 if ( GetCount() > 0 )
1182 wxOwnerDrawnComboBox
* odc
= const_cast<wxOwnerDrawnComboBox
*>(this);
1183 best
.x
= odc
->GetWidestItemWidth();
1184 // TODO: this class may also have GetHightestItemHeight() and
1185 // GetHightestItem() methods, and so set the whole (edit part + arrow)
1186 // control's height according with this max height, not only max width.
1189 return GetSizeFromTextSize(best
.x
);
1192 void wxOwnerDrawnComboBox::OnDrawBackground(wxDC
& dc
,
1197 // We need only to explicitly draw background for items
1198 // that should have selected background. Also, call PrepareBackground
1199 // always when painting the control so that clipping is done properly.
1201 if ( (flags
& wxODCB_PAINTING_SELECTED
) ||
1202 ((flags
& wxODCB_PAINTING_CONTROL
) && HasFlag(wxCB_READONLY
)) )
1204 int bgFlags
= wxCONTROL_SELECTED
;
1206 if ( !(flags
& wxODCB_PAINTING_CONTROL
) )
1207 bgFlags
|= wxCONTROL_ISSUBMENU
;
1209 PrepareBackground(dc
, rect
, bgFlags
);
1213 #endif // wxUSE_ODCOMBOBOX