1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxOwnerDrawnComboBox and wxVListBoxPopup
4 // Author: Jaakko Salli
6 // Created: Apr-30-2006
8 // Copyright: (c) Jaakko Salli
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_ODCOMBO_H_
13 #define _WX_ODCOMBO_H_
17 #if wxUSE_OWNERDRAWNCOMBOBOX
20 #include "wx/ctrlsub.h"
25 // New window styles for wxOwnerDrawnComboBox
29 // Double-clicking cycles item if wxCB_READONLY is also used.
30 wxODCB_DCLICK_CYCLES
= wxCC_SPECIAL_DCLICK
,
32 // If used, control itself is not custom paint using callback.
33 // Even if this is not used, writable combo is never custom paint
34 // until SetCustomPaintWidth is called
35 wxODCB_STD_CONTROL_PAINT
= 0x1000
44 // when set, we are painting the selected item in control,
46 wxCP_PAINTING_CONTROL
= 0x0001
50 // ----------------------------------------------------------------------------
51 // wxVListBoxComboPopup is a wxVListBox customized to act as a popup control.
54 // wxOwnerDrawnComboBox uses this as its popup. However, it always derives
55 // from native wxComboControl. If you need to use this popup with
56 // wxGenericComboControl, then remember that vast majority of item manipulation
57 // functionality is implemented in the wxVListBoxComboPopup class itself.
59 // ----------------------------------------------------------------------------
62 class WXDLLEXPORT wxVListBoxComboPopup
: public wxVListBox
, public wxComboPopup
64 friend class wxOwnerDrawnComboBox
;
68 wxVListBoxComboPopup() : wxVListBox(), wxComboPopup() { }
69 virtual ~wxVListBoxComboPopup();
73 virtual bool Create(wxWindow
* parent
);
74 virtual wxWindow
*GetControl() { return this; }
75 virtual void SetStringValue( const wxString
& value
);
76 virtual wxString
GetStringValue() const;
79 virtual void OnPopup();
80 virtual wxSize
GetAdjustedSize( int minWidth
, int prefHeight
, int maxHeight
);
81 virtual void PaintComboControl( wxDC
& dc
, const wxRect
& rect
);
82 virtual void OnComboKeyEvent( wxKeyEvent
& event
);
83 virtual void OnComboDoubleClick();
84 virtual bool LazyCreate();
86 // Callbacks for drawing and measuring items. Override in a derived class for
88 // item: item index to be drawn, may be wxNOT_FOUND when painting combo control itself
89 // and there is no valid selection
90 // flags: wxCP_PAINTING_CONTROL is set if painting to combo control instead of list
91 virtual void OnDrawItem( wxDC
& dc
, const wxRect
& rect
, int item
, int flags
) const;
93 // Return item width, or -1 for calculating from text extent (default)
94 virtual wxCoord
OnMeasureItemWidth( size_t item
) const;
98 void SetSelection( int item
);
99 void Insert( const wxString
& item
, int pos
);
100 int Append(const wxString
& item
);
102 void Delete( unsigned int item
);
103 void SetItemClientData(unsigned int n
, void* clientData
, wxClientDataType clientDataItemsType
);
104 void *GetItemClientData(unsigned int n
) const;
105 void SetString( int item
, const wxString
& str
);
106 wxString
GetString( int item
) const;
107 unsigned int GetCount() const;
108 int FindString(const wxString
& s
) const;
109 int GetSelection() const;
111 //void Populate( int n, const wxString choices[] );
112 void Populate( const wxArrayString
& choices
);
113 void ClearClientDatas();
116 int GetItemAtPosition( const wxPoint
& pos
) { return HitTest(pos
); }
117 wxCoord
GetTotalHeight() const { return EstimateTotalHeight(); }
118 wxCoord
GetLineHeight(int line
) const { return OnGetLineHeight(line
); }
122 // Called by OnComboDoubleClick and OnComboKeyEvent
123 bool HandleKey( int keycode
, bool saturate
);
125 // sends combobox select event from the parent combo control
126 void SendComboBoxEvent( int selection
);
128 // gets value, sends event and dismisses
129 void DismissWithEvent();
131 // Re-calculates width for given item
132 void CheckWidth( int pos
);
134 // wxVListBox implementation
135 virtual void OnDrawItem(wxDC
& dc
, const wxRect
& rect
, size_t n
) const;
136 //virtual wxCoord OnMeasureItem(size_t n) const;
137 void OnDrawBackground(wxDC
& dc
, const wxRect
& rect
, size_t n
) const;
139 // Return item height
140 virtual wxCoord
OnMeasureItem( size_t item
) const;
142 // filter mouse move events happening outside the list box
143 // move selection with cursor
144 void OnMouseMove(wxMouseEvent
& event
);
145 void OnMouseWheel(wxMouseEvent
& event
);
146 void OnKey(wxKeyEvent
& event
);
147 void OnLeftClick(wxMouseEvent
& event
);
149 wxArrayString m_strings
;
150 wxArrayPtrVoid m_clientDatas
;
151 wxArrayInt m_widths
; // cached line widths
155 //wxString m_stringValue; // displayed text (may be different than m_strings[m_value])
156 int m_value
; // selection
157 int m_itemHover
; // on which item the cursor is
158 int m_widestWidth
; // width of widest item thus far
160 int m_baseImageWidth
; // how much per item drawn in addition to text
161 int m_itemHeight
; // default item height (calculate from font size
162 // and used in the absence of callback)
163 wxClientDataType m_clientDataItemsType
;
167 // has the mouse been released on this control?
170 DECLARE_EVENT_TABLE()
174 // ----------------------------------------------------------------------------
175 // wxOwnerDrawnComboBox: a generic wxComboBox that allows custom paint items
176 // in addition to many other types of customization already allowed by
177 // the wxComboControl.
178 // ----------------------------------------------------------------------------
180 class WXDLLEXPORT wxOwnerDrawnComboBox
: public wxComboControl
, public wxItemContainer
182 friend class wxComboPopupWindow
;
183 friend class wxComboControlBase
;
187 wxOwnerDrawnComboBox() : wxComboControl() { Init(); }
189 wxOwnerDrawnComboBox(wxWindow
*parent
,
191 const wxString
& value
,
195 const wxString choices
[],
197 const wxValidator
& validator
= wxDefaultValidator
,
198 const wxString
& name
= wxComboBoxNameStr
)
203 (void)Create(parent
, id
, value
, pos
, size
, n
,
204 choices
, style
, validator
, name
);
207 bool Create(wxWindow
*parent
,
209 const wxString
& value
= wxEmptyString
,
210 const wxPoint
& pos
= wxDefaultPosition
,
211 const wxSize
& size
= wxDefaultSize
,
213 const wxValidator
& validator
= wxDefaultValidator
,
214 const wxString
& name
= wxComboBoxNameStr
);
216 wxOwnerDrawnComboBox(wxWindow
*parent
,
218 const wxString
& value
,
221 const wxArrayString
& choices
,
223 const wxValidator
& validator
= wxDefaultValidator
,
224 const wxString
& name
= wxComboBoxNameStr
);
226 bool Create(wxWindow
*parent
,
228 const wxString
& value
= wxEmptyString
,
229 const wxPoint
& pos
= wxDefaultPosition
,
230 const wxSize
& size
= wxDefaultSize
,
232 const wxString choices
[] = (const wxString
*) NULL
,
234 const wxValidator
& validator
= wxDefaultValidator
,
235 const wxString
& name
= wxComboBoxNameStr
);
237 bool Create(wxWindow
*parent
,
239 const wxString
& value
,
242 const wxArrayString
& choices
,
244 const wxValidator
& validator
= wxDefaultValidator
,
245 const wxString
& name
= wxComboBoxNameStr
);
247 virtual ~wxOwnerDrawnComboBox();
249 // NULL popup can be used to indicate default interface
250 virtual void SetPopupControl( wxComboPopup
* popup
);
252 // wxControlWithItems methods
253 virtual void Clear();
254 virtual void Delete(unsigned int n
);
255 virtual unsigned int GetCount() const;
256 virtual wxString
GetString(unsigned int n
) const;
257 virtual void SetString(unsigned int n
, const wxString
& s
);
258 virtual int FindString(const wxString
& s
) const;
259 virtual void Select(int n
);
260 virtual int GetSelection() const;
261 void SetSelection(int n
) { Select(n
); }
263 wxCONTROL_ITEMCONTAINER_CLIENTDATAOBJECT_RECAST
267 // clears all allocated client datas
268 void ClearClientDatas();
270 virtual int DoAppend(const wxString
& item
);
271 virtual int DoInsert(const wxString
& item
, unsigned int pos
);
272 virtual void DoSetItemClientData(unsigned int n
, void* clientData
);
273 virtual void* DoGetItemClientData(unsigned int n
) const;
274 virtual void DoSetItemClientObject(unsigned int n
, wxClientData
* clientData
);
275 virtual wxClientData
* DoGetItemClientObject(unsigned int n
) const;
277 // overload m_popupInterface member so we can access specific popup interface easier
278 wxVListBoxComboPopup
* m_popupInterface
;
280 // temporary storage for the initial choices
281 //const wxString* m_baseChoices;
282 //int m_baseChoicesCount;
283 wxArrayString m_initChs
;
288 DECLARE_EVENT_TABLE()
290 DECLARE_DYNAMIC_CLASS(wxOwnerDrawnComboBox
)
294 #endif // wxUSE_OWNERDRAWNCOMBOBOX