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()
667 bool wxOwnerDrawnComboBox::Create(wxWindow
*parent
,
669 const wxString
& value
,
673 const wxValidator
& validator
,
674 const wxString
& name
)
676 return wxComboCtrl::Create(parent
,id
,value
,pos
,size
,style
,validator
,name
);
679 wxOwnerDrawnComboBox::wxOwnerDrawnComboBox(wxWindow
*parent
,
681 const wxString
& value
,
684 const wxArrayString
& choices
,
686 const wxValidator
& validator
,
687 const wxString
& name
)
692 Create(parent
,id
,value
,pos
,size
,choices
,style
, validator
, name
);
695 bool wxOwnerDrawnComboBox::Create(wxWindow
*parent
,
697 const wxString
& value
,
700 const wxArrayString
& choices
,
702 const wxValidator
& validator
,
703 const wxString
& name
)
706 //wxCArrayString chs(choices);
708 //return Create(parent, id, value, pos, size, chs.GetCount(),
709 // chs.GetStrings(), style, validator, name);
710 return Create(parent
, id
, value
, pos
, size
, 0,
711 NULL
, style
, validator
, name
);
714 bool wxOwnerDrawnComboBox::Create(wxWindow
*parent
,
716 const wxString
& value
,
720 const wxString choices
[],
722 const wxValidator
& validator
,
723 const wxString
& name
)
726 if ( !Create(parent
, id
, value
, pos
, size
, style
,
733 for ( i
=0; i
<n
; i
++ )
734 m_initChs
.Add(choices
[i
]);
739 wxOwnerDrawnComboBox::~wxOwnerDrawnComboBox()
741 if ( m_popupInterface
)
742 GetVListBoxComboPopup()->ClearClientDatas();
745 void wxOwnerDrawnComboBox::DoSetPopupControl(wxComboPopup
* popup
)
749 popup
= new wxVListBoxComboPopup();
752 wxComboCtrl::DoSetPopupControl(popup
);
756 // Add initial choices to the wxVListBox
757 if ( !GetVListBoxComboPopup()->GetCount() )
759 GetVListBoxComboPopup()->Populate(m_initChs
);
764 // ----------------------------------------------------------------------------
765 // wxOwnerDrawnComboBox item manipulation methods
766 // ----------------------------------------------------------------------------
768 void wxOwnerDrawnComboBox::Clear()
770 EnsurePopupControl();
772 GetVListBoxComboPopup()->Clear();
774 SetValue(wxEmptyString
);
777 void wxOwnerDrawnComboBox::Delete(unsigned int n
)
779 wxCHECK_RET( IsValid(n
), _T("invalid index in wxOwnerDrawnComboBox::Delete") );
781 if ( GetSelection() == (int) n
)
782 SetValue(wxEmptyString
);
784 GetVListBoxComboPopup()->Delete(n
);
787 unsigned int wxOwnerDrawnComboBox::GetCount() const
789 if ( !m_popupInterface
)
790 return m_initChs
.GetCount();
792 return GetVListBoxComboPopup()->GetCount();
795 wxString
wxOwnerDrawnComboBox::GetString(unsigned int n
) const
797 wxCHECK_MSG( IsValid(n
), wxEmptyString
, _T("invalid index in wxOwnerDrawnComboBox::GetString") );
799 if ( !m_popupInterface
)
800 return m_initChs
.Item(n
);
802 return GetVListBoxComboPopup()->GetString(n
);
805 void wxOwnerDrawnComboBox::SetString(unsigned int n
, const wxString
& s
)
807 EnsurePopupControl();
809 wxCHECK_RET( IsValid(n
), _T("invalid index in wxOwnerDrawnComboBox::SetString") );
811 GetVListBoxComboPopup()->SetString(n
,s
);
814 int wxOwnerDrawnComboBox::FindString(const wxString
& s
, bool bCase
) const
816 if ( !m_popupInterface
)
817 return m_initChs
.Index(s
, bCase
);
819 return GetVListBoxComboPopup()->FindString(s
, bCase
);
822 void wxOwnerDrawnComboBox::Select(int n
)
824 EnsurePopupControl();
826 wxCHECK_RET( (n
== wxNOT_FOUND
) || IsValid(n
), _T("invalid index in wxOwnerDrawnComboBox::Select") );
828 GetVListBoxComboPopup()->SetSelection(n
);
832 str
= GetVListBoxComboPopup()->GetString(n
);
834 // Refresh text portion in control
836 m_text
->SetValue( str
);
843 int wxOwnerDrawnComboBox::GetSelection() const
845 if ( !m_popupInterface
)
846 return m_initChs
.Index(m_valueString
);
848 return GetVListBoxComboPopup()->GetSelection();
851 int wxOwnerDrawnComboBox::DoAppend(const wxString
& item
)
853 EnsurePopupControl();
854 wxASSERT(m_popupInterface
);
856 return GetVListBoxComboPopup()->Append(item
);
859 int wxOwnerDrawnComboBox::DoInsert(const wxString
& item
, unsigned int pos
)
861 EnsurePopupControl();
863 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT
), -1, wxT("can't insert into sorted list"));
864 wxCHECK_MSG(IsValidInsert(pos
), -1, wxT("invalid index"));
866 GetVListBoxComboPopup()->Insert(item
,pos
);
871 void wxOwnerDrawnComboBox::DoSetItemClientData(unsigned int n
, void* clientData
)
873 EnsurePopupControl();
875 GetVListBoxComboPopup()->SetItemClientData(n
,clientData
,m_clientDataItemsType
);
878 void* wxOwnerDrawnComboBox::DoGetItemClientData(unsigned int n
) const
880 if ( !m_popupInterface
)
883 return GetVListBoxComboPopup()->GetItemClientData(n
);
886 void wxOwnerDrawnComboBox::DoSetItemClientObject(unsigned int n
, wxClientData
* clientData
)
888 DoSetItemClientData(n
, (void*) clientData
);
891 wxClientData
* wxOwnerDrawnComboBox::DoGetItemClientObject(unsigned int n
) const
893 return (wxClientData
*) DoGetItemClientData(n
);
896 // ----------------------------------------------------------------------------
897 // wxOwnerDrawnComboBox item drawing and measuring default implementations
898 // ----------------------------------------------------------------------------
900 void wxOwnerDrawnComboBox::OnDrawItem( wxDC
& dc
,
905 if ( flags
& wxODCB_PAINTING_CONTROL
)
907 dc
.DrawText( GetValue(),
908 rect
.x
+ GetTextIndent(),
909 (rect
.height
-dc
.GetCharHeight())/2 + rect
.y
);
913 dc
.DrawText( GetVListBoxComboPopup()->GetString(item
), rect
.x
+ 2, rect
.y
);
917 wxCoord
wxOwnerDrawnComboBox::OnMeasureItem( size_t WXUNUSED(item
) ) const
922 wxCoord
wxOwnerDrawnComboBox::OnMeasureItemWidth( size_t WXUNUSED(item
) ) const
927 void wxOwnerDrawnComboBox::OnDrawBackground(wxDC
& dc
, const wxRect
& rect
, int item
, int flags
) const
929 // we need to render selected and current items differently
930 if ( GetVListBoxComboPopup()->IsCurrent((size_t)item
) )
932 DrawFocusBackground(dc
,
934 (flags
&wxODCB_PAINTING_CONTROL
?0:wxCONTROL_ISSUBMENU
) |
937 //else: do nothing for the normal items
940 #endif // wxUSE_ODCOMBOBOX