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"
40 // ============================================================================
42 // ============================================================================
45 // ----------------------------------------------------------------------------
46 // wxVListBoxComboPopup is a wxVListBox customized to act as a popup control
48 // ----------------------------------------------------------------------------
51 BEGIN_EVENT_TABLE(wxVListBoxComboPopup
, wxVListBox
)
52 EVT_MOTION(wxVListBoxComboPopup::OnMouseMove
)
53 EVT_KEY_DOWN(wxVListBoxComboPopup::OnKey
)
54 EVT_LEFT_UP(wxVListBoxComboPopup::OnLeftClick
)
58 void wxVListBoxComboPopup::Init()
62 m_widthsDirty
= false;
67 m_clientDataItemsType
= wxClientData_None
;
70 bool wxVListBoxComboPopup::Create(wxWindow
* parent
)
72 if ( !wxVListBox::Create(parent
,
76 wxBORDER_SIMPLE
| wxLB_INT_HEIGHT
| wxWANTS_CHARS
) )
79 m_useFont
= m_combo
->GetFont();
81 wxVListBox::SetItemCount(m_strings
.GetCount());
83 // TODO: Move this to SetFont
84 m_itemHeight
= GetCharHeight() + 0;
89 wxVListBoxComboPopup::~wxVListBoxComboPopup()
94 bool wxVListBoxComboPopup::LazyCreate()
96 // NB: There is a bug with wxVListBox that can be avoided by creating
97 // it later (bug causes empty space to be shown if initial selection
98 // is at the end of a list longer than the control can show at once).
102 // paint the control itself
103 void wxVListBoxComboPopup::PaintComboControl( wxDC
& dc
, const wxRect
& rect
)
105 if ( !(m_combo
->GetWindowStyle() & wxODCB_STD_CONTROL_PAINT
) )
107 OnDrawBg(dc
,rect
,m_value
,wxODCB_PAINTING_CONTROL
);
110 OnDrawItem(dc
,rect
,m_value
,wxODCB_PAINTING_CONTROL
);
115 wxComboPopup::PaintComboControl(dc
,rect
);
118 void wxVListBoxComboPopup::OnDrawItem(wxDC
& dc
, const wxRect
& rect
, size_t n
) const
120 // TODO: Maybe this code could be moved to wxVListBox::OnPaint?
121 dc
.SetFont(m_useFont
);
123 // Set correct text colour for selected items
124 if ( wxVListBox::GetSelection() == (int) n
)
125 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) );
127 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
) );
129 OnDrawItem(dc
,rect
,(int)n
,0);
132 wxCoord
wxVListBoxComboPopup::OnMeasureItem(size_t n
) const
134 wxOwnerDrawnComboBox
* combo
= (wxOwnerDrawnComboBox
*) m_combo
;
136 wxASSERT_MSG( combo
->IsKindOf(CLASSINFO(wxOwnerDrawnComboBox
)),
137 wxT("you must subclass wxVListBoxComboPopup for drawing and measuring methods") );
139 wxCoord h
= combo
->OnMeasureItem(n
);
145 wxCoord
wxVListBoxComboPopup::OnMeasureItemWidth(size_t n
) const
147 wxOwnerDrawnComboBox
* combo
= (wxOwnerDrawnComboBox
*) m_combo
;
149 wxASSERT_MSG( combo
->IsKindOf(CLASSINFO(wxOwnerDrawnComboBox
)),
150 wxT("you must subclass wxVListBoxComboPopup for drawing and measuring methods") );
152 return combo
->OnMeasureItemWidth(n
);
155 void wxVListBoxComboPopup::OnDrawBg( wxDC
& dc
,
160 wxOwnerDrawnComboBox
* combo
= (wxOwnerDrawnComboBox
*) m_combo
;
162 wxASSERT_MSG( combo
->IsKindOf(CLASSINFO(wxOwnerDrawnComboBox
)),
163 wxT("you must subclass wxVListBoxComboPopup for drawing and measuring methods") );
165 combo
->OnDrawBackground(dc
,rect
,item
,flags
);
168 void wxVListBoxComboPopup::OnDrawBackground(wxDC
& dc
, const wxRect
& rect
, size_t n
) const
170 OnDrawBg(dc
,rect
,(int)n
,0);
173 // This is called from wxVListBoxComboPopup::OnDrawItem, with text colour and font prepared
174 void wxVListBoxComboPopup::OnDrawItem( wxDC
& dc
, const wxRect
& rect
, int item
, int flags
) const
176 wxOwnerDrawnComboBox
* combo
= (wxOwnerDrawnComboBox
*) m_combo
;
178 wxASSERT_MSG( combo
->IsKindOf(CLASSINFO(wxOwnerDrawnComboBox
)),
179 wxT("you must subclass wxVListBoxComboPopup for drawing and measuring methods") );
181 combo
->OnDrawItem(dc
,rect
,item
,flags
);
184 void wxVListBoxComboPopup::DismissWithEvent()
186 int selection
= wxVListBox::GetSelection();
191 if ( selection
!= wxNOT_FOUND
)
192 valStr
= m_strings
[selection
];
194 valStr
= wxEmptyString
;
198 if ( valStr
!= m_combo
->GetValue() )
199 m_combo
->SetValue(valStr
);
201 SendComboBoxEvent(selection
);
204 void wxVListBoxComboPopup::SendComboBoxEvent( int selection
)
206 wxCommandEvent
evt(wxEVT_COMMAND_COMBOBOX_SELECTED
,m_combo
->GetId());
208 evt
.SetEventObject(m_combo
);
210 evt
.SetInt(selection
);
212 // Set client data, if any
213 if ( selection
>= 0 && (int)m_clientDatas
.GetCount() > selection
)
215 void* clientData
= m_clientDatas
[selection
];
216 if ( m_clientDataItemsType
== wxClientData_Object
)
217 evt
.SetClientObject((wxClientData
*)clientData
);
219 evt
.SetClientData(clientData
);
222 m_combo
->GetEventHandler()->AddPendingEvent(evt
);
225 // returns true if key was consumed
226 bool wxVListBoxComboPopup::HandleKey( int keycode
, bool saturate
)
229 int itemCount
= GetCount();
231 if ( keycode
== WXK_DOWN
|| keycode
== WXK_RIGHT
)
235 else if ( keycode
== WXK_UP
|| keycode
== WXK_LEFT
)
239 else if ( keycode
== WXK_PAGEDOWN
)
243 else if ( keycode
== WXK_PAGEUP
)
252 if ( value
>= itemCount
)
253 value
= itemCount
- 1;
254 else if ( value
< 0 )
259 if ( value
>= itemCount
)
261 else if ( value
< 0 )
265 if ( value
== m_value
)
266 // Even if value was same, don't skip the event
267 // (good for consistency)
273 m_combo
->SetValue(m_strings
[value
]);
275 SendComboBoxEvent(m_value
);
280 void wxVListBoxComboPopup::OnComboDoubleClick()
282 // Cycle on dclick (disable saturation to allow true cycling).
283 if ( !::wxGetKeyState(WXK_SHIFT
) )
284 HandleKey(WXK_DOWN
,false);
286 HandleKey(WXK_UP
,false);
289 void wxVListBoxComboPopup::OnComboKeyEvent( wxKeyEvent
& event
)
291 // Saturated key movement on
292 if ( !HandleKey(event
.GetKeyCode(),true) )
296 void wxVListBoxComboPopup::OnPopup()
298 // *must* set value after size is set (this is because of a vlbox bug)
299 wxVListBox::SetSelection(m_value
);
302 void wxVListBoxComboPopup::OnMouseMove(wxMouseEvent
& event
)
304 // Move selection to cursor if it is inside the popup
305 int itemHere
= GetItemAtPosition(event
.GetPosition());
307 wxVListBox::SetSelection(itemHere
);
312 void wxVListBoxComboPopup::OnLeftClick(wxMouseEvent
& WXUNUSED(event
))
317 void wxVListBoxComboPopup::OnKey(wxKeyEvent
& event
)
319 // Select item if ENTER is pressed
320 if ( event
.GetKeyCode() == WXK_RETURN
|| event
.GetKeyCode() == WXK_NUMPAD_ENTER
)
324 // Hide popup if ESC is pressed
325 else if ( event
.GetKeyCode() == WXK_ESCAPE
)
331 void wxVListBoxComboPopup::Insert( const wxString
& item
, int pos
)
333 // Need to change selection?
335 if ( !(m_combo
->GetWindowStyle() & wxCB_READONLY
) &&
336 m_combo
->GetValue() == item
)
341 m_strings
.Insert(item
,pos
);
342 m_widths
.Insert(-1,pos
);
343 m_widthsDirty
= true;
346 wxVListBox::SetItemCount( wxVListBox::GetItemCount()+1 );
349 int wxVListBoxComboPopup::Append(const wxString
& item
)
351 int pos
= (int)m_strings
.GetCount();
353 if ( m_combo
->GetWindowStyle() & wxCB_SORT
)
356 // TODO: Could be optimized with binary search
357 wxArrayString strings
= m_strings
;
360 for ( i
=0; i
<strings
.GetCount(); i
++ )
362 if ( item
.Cmp(strings
.Item(i
)) < 0 )
375 void wxVListBoxComboPopup::Clear()
387 m_value
= wxNOT_FOUND
;
390 wxVListBox::SetItemCount(0);
393 void wxVListBoxComboPopup::ClearClientDatas()
395 if ( m_clientDataItemsType
== wxClientData_Object
)
398 for ( i
=0; i
<m_clientDatas
.GetCount(); i
++ )
399 delete (wxClientData
*) m_clientDatas
[i
];
402 m_clientDatas
.Empty();
405 void wxVListBoxComboPopup::SetItemClientData( unsigned int n
,
407 wxClientDataType clientDataItemsType
)
409 // It should be sufficient to update this variable only here
410 m_clientDataItemsType
= clientDataItemsType
;
412 m_clientDatas
.SetCount(n
+1,NULL
);
413 m_clientDatas
[n
] = clientData
;
418 void* wxVListBoxComboPopup::GetItemClientData(unsigned int n
) const
420 if ( m_clientDatas
.GetCount() > n
)
421 return m_clientDatas
[n
];
426 void wxVListBoxComboPopup::Delete( unsigned int item
)
428 // Remove client data, if set
429 if ( m_clientDatas
.GetCount() )
431 if ( m_clientDataItemsType
== wxClientData_Object
)
432 delete (wxClientData
*) m_clientDatas
[item
];
434 m_clientDatas
.RemoveAt(item
);
437 m_strings
.RemoveAt(item
);
438 m_widths
.RemoveAt(item
);
440 if ( (int)item
== m_widestItem
)
444 wxVListBox::SetItemCount( wxVListBox::GetItemCount()-1 );
447 int wxVListBoxComboPopup::FindString(const wxString
& s
, bool bCase
) const
449 return m_strings
.Index(s
, bCase
);
452 unsigned int wxVListBoxComboPopup::GetCount() const
454 return m_strings
.GetCount();
457 wxString
wxVListBoxComboPopup::GetString( int item
) const
459 return m_strings
[item
];
462 void wxVListBoxComboPopup::SetString( int item
, const wxString
& str
)
464 m_strings
[item
] = str
;
465 ItemWidthChanged(item
);
468 wxString
wxVListBoxComboPopup::GetStringValue() const
471 return m_strings
[m_value
];
472 return wxEmptyString
;
475 void wxVListBoxComboPopup::SetSelection( int item
)
477 wxCHECK_RET( item
== wxNOT_FOUND
|| ((unsigned int)item
< GetCount()),
478 wxT("invalid index in wxVListBoxComboPopup::SetSelection") );
483 wxVListBox::SetSelection(item
);
486 int wxVListBoxComboPopup::GetSelection() const
491 void wxVListBoxComboPopup::SetStringValue( const wxString
& value
)
493 int index
= m_strings
.Index(value
);
497 if ( index
>= -1 && index
< (int)wxVListBox::GetItemCount() )
498 wxVListBox::SetSelection(index
);
501 wxSize
wxVListBoxComboPopup::GetAdjustedSize( int minWidth
, int prefHeight
, int maxHeight
)
505 if ( m_strings
.GetCount() )
507 if ( prefHeight
> 0 )
510 if ( height
> maxHeight
)
513 int totalHeight
= GetTotalHeight(); // + 3;
514 if ( height
>= totalHeight
)
516 height
= totalHeight
;
520 // Adjust height to a multiple of the height of the first item
521 // NB: Calculations that take variable height into account
523 int fih
= GetLineHeight(0);
524 int shown
= height
/fih
;
525 height
= shown
* fih
;
531 bool doFindWidest
= m_findWidest
;
533 // Measure items with dirty width.
537 unsigned int n
= m_widths
.GetCount();
538 int dirtyHandled
= 0;
539 wxArrayInt
& widths
= m_widths
;
541 // I think using wxDC::GetTextExtent is faster than
542 // wxWindow::GetTextExtent (assuming same dc is used
543 // for all calls, as we do here).
544 wxClientDC
dc(m_combo
);
545 dc
.SetFont(m_useFont
);
547 for ( i
=0; i
<n
; i
++ )
551 wxCoord x
= OnMeasureItemWidth(i
);
555 const wxString
& text
= m_strings
[i
];
557 // To make sure performance won't suck in extreme scenarios,
558 // we'll estimate length after some arbitrary number of items
559 // have been checked precily.
560 if ( dirtyHandled
< 1024 )
563 dc
.GetTextExtent(text
, &x
, &y
, 0, 0);
568 x
= text
.length() * (dc
.GetCharWidth()+1);
574 if ( x
>= m_widestWidth
)
577 m_widestItem
= (int)i
;
579 else if ( (int)i
== m_widestItem
)
581 // Width of previously widest item has been decreased, so
582 // we'll have to check all to find current widest item.
590 m_widthsDirty
= false;
596 unsigned int n
= m_widths
.GetCount();
601 for ( i
=0; i
<n
; i
++ )
611 m_widestWidth
= bestWidth
;
612 m_widestItem
= bestIndex
;
614 m_findWidest
= false;
617 // Take scrollbar into account in width calculations
618 int widestWidth
= m_widestWidth
+ wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
619 return wxSize(minWidth
> widestWidth
? minWidth
: widestWidth
,
623 //void wxVListBoxComboPopup::Populate( int n, const wxString choices[] )
624 void wxVListBoxComboPopup::Populate( const wxArrayString
& choices
)
628 int n
= choices
.GetCount();
630 for ( i
=0; i
<n
; i
++ )
632 const wxString
& item
= choices
.Item(i
);
636 m_widths
.SetCount(n
,-1);
637 m_widthsDirty
= true;
640 wxVListBox::SetItemCount(n
);
642 // Sort the initial choices
643 if ( m_combo
->GetWindowStyle() & wxCB_SORT
)
646 // Find initial selection
647 wxString strValue
= m_combo
->GetValue();
648 if ( strValue
.length() )
649 m_value
= m_strings
.Index(strValue
);
652 // ----------------------------------------------------------------------------
653 // wxOwnerDrawnComboBox
654 // ----------------------------------------------------------------------------
657 BEGIN_EVENT_TABLE(wxOwnerDrawnComboBox
, wxComboCtrl
)
661 IMPLEMENT_DYNAMIC_CLASS2(wxOwnerDrawnComboBox
, wxComboCtrl
, wxControlWithItems
)
663 void wxOwnerDrawnComboBox::Init()
665 m_popupInterface
= NULL
;
668 bool wxOwnerDrawnComboBox::Create(wxWindow
*parent
,
670 const wxString
& value
,
674 const wxValidator
& validator
,
675 const wxString
& name
)
677 return wxComboCtrl::Create(parent
,id
,value
,pos
,size
,style
,validator
,name
);
680 wxOwnerDrawnComboBox::wxOwnerDrawnComboBox(wxWindow
*parent
,
682 const wxString
& value
,
685 const wxArrayString
& choices
,
687 const wxValidator
& validator
,
688 const wxString
& name
)
693 Create(parent
,id
,value
,pos
,size
,choices
,style
, validator
, name
);
696 bool wxOwnerDrawnComboBox::Create(wxWindow
*parent
,
698 const wxString
& value
,
701 const wxArrayString
& choices
,
703 const wxValidator
& validator
,
704 const wxString
& name
)
707 //wxCArrayString chs(choices);
709 //return Create(parent, id, value, pos, size, chs.GetCount(),
710 // chs.GetStrings(), style, validator, name);
711 return Create(parent
, id
, value
, pos
, size
, 0,
712 NULL
, style
, validator
, name
);
715 bool wxOwnerDrawnComboBox::Create(wxWindow
*parent
,
717 const wxString
& value
,
721 const wxString choices
[],
723 const wxValidator
& validator
,
724 const wxString
& name
)
727 if ( !Create(parent
, id
, value
, pos
, size
, style
,
734 for ( i
=0; i
<n
; i
++ )
735 m_initChs
.Add(choices
[i
]);
740 wxOwnerDrawnComboBox::~wxOwnerDrawnComboBox()
742 if ( m_popupInterface
)
743 m_popupInterface
->ClearClientDatas();
746 void wxOwnerDrawnComboBox::DoSetPopupControl(wxComboPopup
* popup
)
750 popup
= new wxVListBoxComboPopup();
753 wxComboCtrl::DoSetPopupControl(popup
);
756 m_popupInterface
= (wxVListBoxComboPopup
*) popup
;
758 // Add initial choices to the wxVListBox
759 if ( !m_popupInterface
->GetCount() )
761 //m_popupInterface->Populate(m_initChs.GetCount(),m_initChs.GetStrings());
762 m_popupInterface
->Populate(m_initChs
);
767 // ----------------------------------------------------------------------------
768 // wxOwnerDrawnComboBox item manipulation methods
769 // ----------------------------------------------------------------------------
771 void wxOwnerDrawnComboBox::Clear()
773 EnsurePopupControl();
775 m_popupInterface
->Clear();
777 SetValue(wxEmptyString
);
780 void wxOwnerDrawnComboBox::Delete(unsigned int n
)
782 wxCHECK_RET( IsValid(n
), _T("invalid index in wxOwnerDrawnComboBox::Delete") );
784 if ( GetSelection() == (int) n
)
785 SetValue(wxEmptyString
);
787 m_popupInterface
->Delete(n
);
790 unsigned int wxOwnerDrawnComboBox::GetCount() const
792 if ( !m_popupInterface
)
793 return m_initChs
.GetCount();
795 return m_popupInterface
->GetCount();
798 wxString
wxOwnerDrawnComboBox::GetString(unsigned int n
) const
800 wxCHECK_MSG( IsValid(n
), wxEmptyString
, _T("invalid index in wxOwnerDrawnComboBox::GetString") );
802 if ( !m_popupInterface
)
803 return m_initChs
.Item(n
);
805 return m_popupInterface
->GetString(n
);
808 void wxOwnerDrawnComboBox::SetString(unsigned int n
, const wxString
& s
)
810 EnsurePopupControl();
812 wxCHECK_RET( IsValid(n
), _T("invalid index in wxOwnerDrawnComboBox::SetString") );
814 m_popupInterface
->SetString(n
,s
);
817 int wxOwnerDrawnComboBox::FindString(const wxString
& s
, bool bCase
) const
819 if ( !m_popupInterface
)
820 return m_initChs
.Index(s
, bCase
);
822 return m_popupInterface
->FindString(s
, bCase
);
825 void wxOwnerDrawnComboBox::Select(int n
)
827 EnsurePopupControl();
829 wxCHECK_RET( (n
== wxNOT_FOUND
) || IsValid(n
), _T("invalid index in wxOwnerDrawnComboBox::Select") );
831 m_popupInterface
->SetSelection(n
);
835 str
= m_popupInterface
->GetString(n
);
837 // Refresh text portion in control
839 m_text
->SetValue( str
);
846 int wxOwnerDrawnComboBox::GetSelection() const
848 if ( !m_popupInterface
)
849 return m_initChs
.Index(m_valueString
);
851 return m_popupInterface
->GetSelection();
854 int wxOwnerDrawnComboBox::DoAppend(const wxString
& item
)
856 EnsurePopupControl();
857 wxASSERT(m_popupInterface
);
858 return m_popupInterface
->Append(item
);
861 int wxOwnerDrawnComboBox::DoInsert(const wxString
& item
, unsigned int pos
)
863 EnsurePopupControl();
865 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT
), -1, wxT("can't insert into sorted list"));
866 wxCHECK_MSG(IsValidInsert(pos
), -1, wxT("invalid index"));
868 m_popupInterface
->Insert(item
,pos
);
873 void wxOwnerDrawnComboBox::DoSetItemClientData(unsigned int n
, void* clientData
)
875 EnsurePopupControl();
876 m_popupInterface
->SetItemClientData(n
,clientData
,m_clientDataItemsType
);
879 void* wxOwnerDrawnComboBox::DoGetItemClientData(unsigned int n
) const
881 if ( !m_popupInterface
)
884 return m_popupInterface
->GetItemClientData(n
);
887 void wxOwnerDrawnComboBox::DoSetItemClientObject(unsigned int n
, wxClientData
* clientData
)
889 DoSetItemClientData(n
, (void*) clientData
);
892 wxClientData
* wxOwnerDrawnComboBox::DoGetItemClientObject(unsigned int n
) const
894 return (wxClientData
*) DoGetItemClientData(n
);
897 // ----------------------------------------------------------------------------
898 // wxOwnerDrawnComboBox item drawing and measuring default implementations
899 // ----------------------------------------------------------------------------
901 void wxOwnerDrawnComboBox::OnDrawItem( wxDC
& dc
,
906 if ( flags
& wxODCB_PAINTING_CONTROL
)
908 dc
.DrawText( GetValue(),
909 rect
.x
+ GetTextIndent(),
910 (rect
.height
-dc
.GetCharHeight())/2 + rect
.y
);
914 dc
.DrawText( m_popupInterface
->GetString(item
), rect
.x
+ 2, rect
.y
);
918 wxCoord
wxOwnerDrawnComboBox::OnMeasureItem( size_t WXUNUSED(item
) ) const
923 wxCoord
wxOwnerDrawnComboBox::OnMeasureItemWidth( size_t WXUNUSED(item
) ) const
928 void wxOwnerDrawnComboBox::OnDrawBackground(wxDC
& dc
, const wxRect
& rect
, int item
, int flags
) const
930 // we need to render selected and current items differently
931 if ( m_popupInterface
->IsCurrent((size_t)item
) )
933 DrawFocusBackground(dc
,
935 (flags
&wxODCB_PAINTING_CONTROL
?0:wxCONTROL_ISSUBMENU
) |
938 //else: do nothing for the normal items
941 #endif // wxUSE_ODCOMBOBOX