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_
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
40 // Callback flags (see wxOwnerDrawnComboBox::OnDrawItem)
44 // when set, we are painting the selected item in control,
46 wxODCB_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 wxComboCtrl. 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 WXDLLIMPEXP_ADV wxVListBoxComboPopup
: public wxVListBox
,
65 friend class wxOwnerDrawnComboBox
;
69 wxVListBoxComboPopup() : wxVListBox(), wxComboPopup() { }
70 virtual ~wxVListBoxComboPopup();
74 virtual bool Create(wxWindow
* parent
);
75 virtual wxWindow
*GetControl() { return this; }
76 virtual void SetStringValue( const wxString
& value
);
77 virtual wxString
GetStringValue() const;
80 virtual void OnPopup();
81 virtual wxSize
GetAdjustedSize( int minWidth
, int prefHeight
, int maxHeight
);
82 virtual void PaintComboControl( wxDC
& dc
, const wxRect
& rect
);
83 virtual void OnComboKeyEvent( wxKeyEvent
& event
);
84 virtual void OnComboDoubleClick();
85 virtual bool LazyCreate();
88 void SetSelection( int item
);
89 void Insert( const wxString
& item
, int pos
);
90 int Append(const wxString
& item
);
92 void Delete( unsigned int item
);
93 void SetItemClientData(unsigned int n
, void* clientData
, wxClientDataType clientDataItemsType
);
94 void *GetItemClientData(unsigned int n
) const;
95 void SetString( int item
, const wxString
& str
);
96 wxString
GetString( int item
) const;
97 unsigned int GetCount() const;
98 int FindString(const wxString
& s
, bool bCase
= false) const;
99 int GetSelection() const;
101 //void Populate( int n, const wxString choices[] );
102 void Populate( const wxArrayString
& choices
);
103 void ClearClientDatas();
106 int GetItemAtPosition( const wxPoint
& pos
) { return HitTest(pos
); }
107 wxCoord
GetTotalHeight() const { return EstimateTotalHeight(); }
108 wxCoord
GetLineHeight(int line
) const { return OnGetLineHeight(line
); }
112 // Called by OnComboDoubleClick and OnComboKeyEvent
113 bool HandleKey( int keycode
, bool saturate
);
115 // sends combobox select event from the parent combo control
116 void SendComboBoxEvent( int selection
);
118 // gets value, sends event and dismisses
119 void DismissWithEvent();
121 // OnMeasureItemWidth will be called on next GetAdjustedSize.
122 void ItemWidthChanged(unsigned int item
)
125 m_widthsDirty
= true;
128 // Callbacks for drawing and measuring items. Override in a derived class for
129 // owner-drawnness. Font, background and text colour have been prepared according
130 // to selection, focus and such.
132 // item: item index to be drawn, may be wxNOT_FOUND when painting combo control itself
133 // and there is no valid selection
134 // flags: wxODCB_PAINTING_CONTROL is set if painting to combo control instead of list
135 // NOTE: If wxVListBoxComboPopup is used with wxComboCtrl class not derived from
136 // wxOwnerDrawnComboBox, this method must be overridden.
137 virtual void OnDrawItem( wxDC
& dc
, const wxRect
& rect
, int item
, int flags
) const;
139 // This is same as in wxVListBox
140 virtual wxCoord
OnMeasureItem( size_t item
) const;
142 // Return item width, or -1 for calculating from text extent (default)
143 virtual wxCoord
OnMeasureItemWidth( size_t item
) const;
145 // Draw item and combo control background. Flags are same as with OnDrawItem.
146 // NB: Can't use name OnDrawBackground because of virtual function hiding warnings.
147 virtual void OnDrawBg(wxDC
& dc
, const wxRect
& rect
, int item
, int flags
) const;
149 // Additional wxVListBox implementation (no need to override in derived classes)
150 virtual void OnDrawItem(wxDC
& dc
, const wxRect
& rect
, size_t n
) const;
151 void OnDrawBackground(wxDC
& dc
, const wxRect
& rect
, size_t n
) const;
153 // filter mouse move events happening outside the list box
154 // move selection with cursor
155 void OnMouseMove(wxMouseEvent
& event
);
156 void OnMouseWheel(wxMouseEvent
& event
);
157 void OnKey(wxKeyEvent
& event
);
158 void OnLeftClick(wxMouseEvent
& event
);
160 // Return the widest item width (recalculating it if necessary)
161 int GetWidestItemWidth() { CalcWidths(); return m_widestWidth
; }
163 // Return the index of the widest item (recalculating it if necessary)
164 int GetWidestItem() { CalcWidths(); return m_widestItem
; }
166 wxArrayString m_strings
;
167 wxArrayPtrVoid m_clientDatas
;
171 //wxString m_stringValue; // displayed text (may be different than m_strings[m_value])
172 int m_value
; // selection
173 int m_itemHover
; // on which item the cursor is
174 int m_itemHeight
; // default item height (calculate from font size
175 // and used in the absence of callback)
176 wxClientDataType m_clientDataItemsType
;
180 // Cached item widths (in pixels).
183 // Width of currently widest item.
186 // Index of currently widest item.
189 // Measure some items in next GetAdjustedSize?
192 // Find widest item in next GetAdjustedSize?
195 // has the mouse been released on this control?
198 // Recalculate widths if they are dirty
201 DECLARE_EVENT_TABLE()
205 // ----------------------------------------------------------------------------
206 // wxOwnerDrawnComboBox: a generic wxComboBox that allows custom paint items
207 // in addition to many other types of customization already allowed by
209 // ----------------------------------------------------------------------------
211 class WXDLLIMPEXP_ADV wxOwnerDrawnComboBox
: public wxComboCtrl
,
212 public wxItemContainer
214 //friend class wxComboPopupWindow;
215 friend class wxVListBoxComboPopup
;
219 wxOwnerDrawnComboBox() : wxComboCtrl() { Init(); }
221 wxOwnerDrawnComboBox(wxWindow
*parent
,
223 const wxString
& value
,
227 const wxString choices
[],
229 const wxValidator
& validator
= wxDefaultValidator
,
230 const wxString
& name
= wxComboBoxNameStr
)
235 (void)Create(parent
, id
, value
, pos
, size
, n
,
236 choices
, style
, validator
, name
);
239 bool Create(wxWindow
*parent
,
241 const wxString
& value
= wxEmptyString
,
242 const wxPoint
& pos
= wxDefaultPosition
,
243 const wxSize
& size
= wxDefaultSize
,
245 const wxValidator
& validator
= wxDefaultValidator
,
246 const wxString
& name
= wxComboBoxNameStr
);
248 wxOwnerDrawnComboBox(wxWindow
*parent
,
250 const wxString
& value
,
253 const wxArrayString
& choices
,
255 const wxValidator
& validator
= wxDefaultValidator
,
256 const wxString
& name
= wxComboBoxNameStr
);
258 bool Create(wxWindow
*parent
,
260 const wxString
& value
,
264 const wxString choices
[],
266 const wxValidator
& validator
= wxDefaultValidator
,
267 const wxString
& name
= wxComboBoxNameStr
);
269 bool Create(wxWindow
*parent
,
271 const wxString
& value
,
274 const wxArrayString
& choices
,
276 const wxValidator
& validator
= wxDefaultValidator
,
277 const wxString
& name
= wxComboBoxNameStr
);
279 virtual ~wxOwnerDrawnComboBox();
281 // Prevent app from using wxComboPopup
282 void SetPopupControl(wxVListBoxComboPopup
* popup
)
284 DoSetPopupControl(popup
);
287 // wxControlWithItems methods
288 virtual void Clear();
289 virtual void Delete(unsigned int n
);
290 virtual unsigned int GetCount() const;
291 virtual wxString
GetString(unsigned int n
) const;
292 virtual void SetString(unsigned int n
, const wxString
& s
);
293 virtual int FindString(const wxString
& s
, bool bCase
= false) const;
294 virtual void Select(int n
);
295 virtual int GetSelection() const;
296 virtual void SetSelection(int n
) { Select(n
); }
299 // Prevent a method from being hidden
300 virtual void SetSelection(long from
, long to
)
302 wxComboCtrl::SetSelection(from
,to
);
305 // Return the widest item width (recalculating it if necessary)
306 virtual int GetWidestItemWidth() { EnsurePopupControl(); return GetVListBoxComboPopup()->GetWidestItemWidth(); }
308 // Return the index of the widest item (recalculating it if necessary)
309 virtual int GetWidestItem() { EnsurePopupControl(); return GetVListBoxComboPopup()->GetWidestItem(); }
311 wxCONTROL_ITEMCONTAINER_CLIENTDATAOBJECT_RECAST
315 // Callback for drawing. Font, background and text colour have been
316 // prepared according to selection, focus and such.
317 // item: item index to be drawn, may be wxNOT_FOUND when painting combo control itself
318 // and there is no valid selection
319 // flags: wxODCB_PAINTING_CONTROL is set if painting to combo control instead of list
320 virtual void OnDrawItem( wxDC
& dc
, const wxRect
& rect
, int item
, int flags
) const;
322 // Callback for item height, or -1 for default
323 virtual wxCoord
OnMeasureItem( size_t item
) const;
325 // Callback for item width, or -1 for default/undetermined
326 virtual wxCoord
OnMeasureItemWidth( size_t item
) const;
328 // Callback for background drawing. Flags are same as with
330 virtual void OnDrawBackground( wxDC
& dc
, const wxRect
& rect
, int item
, int flags
) const;
332 // NULL popup can be used to indicate default interface
333 virtual void DoSetPopupControl(wxComboPopup
* popup
);
335 // clears all allocated client datas
336 void ClearClientDatas();
338 wxVListBoxComboPopup
* GetVListBoxComboPopup() const
340 return (wxVListBoxComboPopup
*) m_popupInterface
;
343 virtual int DoAppend(const wxString
& item
);
344 virtual int DoInsert(const wxString
& item
, unsigned int pos
);
345 virtual void DoSetItemClientData(unsigned int n
, void* clientData
);
346 virtual void* DoGetItemClientData(unsigned int n
) const;
347 virtual void DoSetItemClientObject(unsigned int n
, wxClientData
* clientData
);
348 virtual wxClientData
* DoGetItemClientObject(unsigned int n
) const;
350 // temporary storage for the initial choices
351 //const wxString* m_baseChoices;
352 //int m_baseChoicesCount;
353 wxArrayString m_initChs
;
358 DECLARE_EVENT_TABLE()
360 DECLARE_DYNAMIC_CLASS(wxOwnerDrawnComboBox
)
364 #endif // wxUSE_ODCOMBOBOX