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"
26 // New window styles for wxOwnerDrawnComboBox
30 // Double-clicking cycles item if wxCB_READONLY is also used.
31 wxODCB_DCLICK_CYCLES
= wxCC_SPECIAL_DCLICK
,
33 // If used, control itself is not custom paint using callback.
34 // Even if this is not used, writable combo is never custom paint
35 // until SetCustomPaintWidth is called
36 wxODCB_STD_CONTROL_PAINT
= 0x1000
41 // Callback flags (see wxOwnerDrawnComboBox::OnDrawItem)
43 enum wxOwnerDrawnComboBoxPaintingFlags
45 // when set, we are painting the selected item in control,
47 wxODCB_PAINTING_CONTROL
= 0x0001,
50 // when set, we are painting an item which should have
51 // focus rectangle painted in the background. Text colour
52 // and clipping region are then appropriately set in
53 // the default OnDrawBackground implementation.
54 wxODCB_PAINTING_SELECTED
= 0x0002
58 // ----------------------------------------------------------------------------
59 // wxVListBoxComboPopup is a wxVListBox customized to act as a popup control.
62 // wxOwnerDrawnComboBox uses this as its popup. However, it always derives
63 // from native wxComboCtrl. If you need to use this popup with
64 // wxGenericComboControl, then remember that vast majority of item manipulation
65 // functionality is implemented in the wxVListBoxComboPopup class itself.
67 // ----------------------------------------------------------------------------
70 class WXDLLIMPEXP_ADV wxVListBoxComboPopup
: public wxVListBox
,
73 friend class wxOwnerDrawnComboBox
;
77 wxVListBoxComboPopup() : wxVListBox(), wxComboPopup() { }
78 virtual ~wxVListBoxComboPopup();
82 virtual bool Create(wxWindow
* parent
);
83 virtual void SetFocus();
84 virtual wxWindow
*GetControl() { return this; }
85 virtual void SetStringValue( const wxString
& value
);
86 virtual wxString
GetStringValue() const;
89 virtual void OnPopup();
90 virtual wxSize
GetAdjustedSize( int minWidth
, int prefHeight
, int maxHeight
);
91 virtual void PaintComboControl( wxDC
& dc
, const wxRect
& rect
);
92 virtual void OnComboKeyEvent( wxKeyEvent
& event
);
93 virtual void OnComboCharEvent( wxKeyEvent
& event
);
94 virtual void OnComboDoubleClick();
95 virtual bool LazyCreate();
96 virtual bool FindItem(const wxString
& item
, wxString
* trueItem
);
99 void SetSelection( int item
);
100 void Insert( const wxString
& item
, int pos
);
101 int Append(const wxString
& item
);
103 void Delete( unsigned int item
);
104 void SetItemClientData(unsigned int n
, void* clientData
, wxClientDataType clientDataItemsType
);
105 void *GetItemClientData(unsigned int n
) const;
106 void SetString( int item
, const wxString
& str
);
107 wxString
GetString( int item
) const;
108 unsigned int GetCount() const;
109 int FindString(const wxString
& s
, bool bCase
= false) const;
110 int GetSelection() const;
112 //void Populate( int n, const wxString choices[] );
113 void Populate( const wxArrayString
& choices
);
114 void ClearClientDatas();
117 int GetItemAtPosition( const wxPoint
& pos
) { return HitTest(pos
); }
118 wxCoord
GetTotalHeight() const { return EstimateTotalHeight(); }
119 wxCoord
GetLineHeight(int line
) const { return OnGetRowHeight(line
); }
123 // Called by OnComboDoubleClick and OnCombo{Key,Char}Event
124 bool HandleKey( int keycode
, bool saturate
, wxChar keychar
= 0 );
126 // sends combobox select event from the parent combo control
127 void SendComboBoxEvent( int selection
);
129 // gets value, sends event and dismisses
130 void DismissWithEvent();
132 // OnMeasureItemWidth will be called on next GetAdjustedSize.
133 void ItemWidthChanged(unsigned int item
)
136 m_widthsDirty
= true;
139 // Callbacks for drawing and measuring items. Override in a derived class for
140 // owner-drawnness. Font, background and text colour have been prepared according
141 // to selection, focus and such.
143 // item: item index to be drawn, may be wxNOT_FOUND when painting combo control itself
144 // and there is no valid selection
145 // flags: wxODCB_PAINTING_CONTROL is set if painting to combo control instead of list
147 // NOTE: If wxVListBoxComboPopup is used with a wxComboCtrl class not derived from
148 // wxOwnerDrawnComboBox, this method must be overridden.
149 virtual void OnDrawItem( wxDC
& dc
, const wxRect
& rect
, int item
, int flags
) const;
151 // This is same as in wxVListBox
152 virtual wxCoord
OnMeasureItem( size_t item
) const;
154 // Return item width, or -1 for calculating from text extent (default)
155 virtual wxCoord
OnMeasureItemWidth( size_t item
) const;
157 // Draw item and combo control background. Flags are same as with OnDrawItem.
158 // NB: Can't use name OnDrawBackground because of virtual function hiding warnings.
159 virtual void OnDrawBg(wxDC
& dc
, const wxRect
& rect
, int item
, int flags
) const;
161 // Additional wxVListBox implementation (no need to override in derived classes)
162 virtual void OnDrawItem(wxDC
& dc
, const wxRect
& rect
, size_t n
) const;
163 void OnDrawBackground(wxDC
& dc
, const wxRect
& rect
, size_t n
) const;
165 // filter mouse move events happening outside the list box
166 // move selection with cursor
167 void OnMouseMove(wxMouseEvent
& event
);
168 void OnMouseWheel(wxMouseEvent
& event
);
169 void OnKey(wxKeyEvent
& event
);
170 void OnChar(wxKeyEvent
& event
);
171 void OnLeftClick(wxMouseEvent
& event
);
173 // Return the widest item width (recalculating it if necessary)
174 int GetWidestItemWidth() { CalcWidths(); return m_widestWidth
; }
176 // Return the index of the widest item (recalculating it if necessary)
177 int GetWidestItem() { CalcWidths(); return m_widestItem
; }
179 // Stop partial completion (when some other event occurs)
180 void StopPartialCompletion();
182 wxArrayString m_strings
;
183 wxArrayPtrVoid m_clientDatas
;
187 //wxString m_stringValue; // displayed text (may be different than m_strings[m_value])
188 int m_value
; // selection
189 int m_itemHover
; // on which item the cursor is
190 int m_itemHeight
; // default item height (calculate from font size
191 // and used in the absence of callback)
192 wxClientDataType m_clientDataItemsType
;
196 // Cached item widths (in pixels).
199 // Width of currently widest item.
202 // Index of currently widest item.
205 // Measure some items in next GetAdjustedSize?
208 // Find widest item in next GetAdjustedSize?
211 // has the mouse been released on this control?
214 // Recalculate widths if they are dirty
217 // Partial completion string
218 wxString m_partialCompletionString
;
220 wxString m_stringValue
;
223 // Partial completion timer
224 wxTimer m_partialCompletionTimer
;
225 #endif // wxUSE_TIMER
227 DECLARE_EVENT_TABLE()
231 // ----------------------------------------------------------------------------
232 // wxOwnerDrawnComboBox: a generic wxComboBox that allows custom paint items
233 // in addition to many other types of customization already allowed by
235 // ----------------------------------------------------------------------------
237 class WXDLLIMPEXP_ADV wxOwnerDrawnComboBox
: public wxComboCtrl
,
238 public wxItemContainer
240 //friend class wxComboPopupWindow;
241 friend class wxVListBoxComboPopup
;
245 wxOwnerDrawnComboBox() : wxComboCtrl() { Init(); }
247 wxOwnerDrawnComboBox(wxWindow
*parent
,
249 const wxString
& value
,
253 const wxString choices
[],
255 const wxValidator
& validator
= wxDefaultValidator
,
256 const wxString
& name
= wxComboBoxNameStr
)
261 (void)Create(parent
, id
, value
, pos
, size
, n
,
262 choices
, style
, validator
, name
);
265 bool Create(wxWindow
*parent
,
267 const wxString
& value
= wxEmptyString
,
268 const wxPoint
& pos
= wxDefaultPosition
,
269 const wxSize
& size
= wxDefaultSize
,
271 const wxValidator
& validator
= wxDefaultValidator
,
272 const wxString
& name
= wxComboBoxNameStr
);
274 wxOwnerDrawnComboBox(wxWindow
*parent
,
276 const wxString
& value
= wxEmptyString
,
277 const wxPoint
& pos
= wxDefaultPosition
,
278 const wxSize
& size
= wxDefaultSize
,
279 const wxArrayString
& choices
= wxArrayString(),
281 const wxValidator
& validator
= wxDefaultValidator
,
282 const wxString
& name
= wxComboBoxNameStr
);
284 bool Create(wxWindow
*parent
,
286 const wxString
& value
,
290 const wxString choices
[],
292 const wxValidator
& validator
= wxDefaultValidator
,
293 const wxString
& name
= wxComboBoxNameStr
);
295 bool Create(wxWindow
*parent
,
297 const wxString
& value
,
300 const wxArrayString
& choices
,
302 const wxValidator
& validator
= wxDefaultValidator
,
303 const wxString
& name
= wxComboBoxNameStr
);
305 virtual ~wxOwnerDrawnComboBox();
307 // Prevent app from using wxComboPopup
308 void SetPopupControl(wxVListBoxComboPopup
* popup
)
310 DoSetPopupControl(popup
);
313 // wxControlWithItems methods
314 virtual unsigned int GetCount() const;
315 virtual wxString
GetString(unsigned int n
) const;
316 virtual void SetString(unsigned int n
, const wxString
& s
);
317 virtual int FindString(const wxString
& s
, bool bCase
= false) const;
318 virtual void Select(int n
);
319 virtual int GetSelection() const;
321 // Override these just to maintain consistency with virtual methods
323 virtual void Clear();
324 virtual void GetSelection(long *from
, long *to
) const;
326 virtual void SetSelection(int n
) { Select(n
); }
329 // Prevent a method from being hidden
330 virtual void SetSelection(long from
, long to
)
332 wxComboCtrl::SetSelection(from
,to
);
335 // Return the widest item width (recalculating it if necessary)
336 virtual int GetWidestItemWidth() { EnsurePopupControl(); return GetVListBoxComboPopup()->GetWidestItemWidth(); }
338 // Return the index of the widest item (recalculating it if necessary)
339 virtual int GetWidestItem() { EnsurePopupControl(); return GetVListBoxComboPopup()->GetWidestItem(); }
341 virtual bool IsSorted() const { return HasFlag(wxCB_SORT
); }
343 wxCONTROL_ITEMCONTAINER_CLIENTDATAOBJECT_RECAST
346 virtual void DoClear();
347 virtual void DoDeleteOneItem(unsigned int n
);
349 // Callback for drawing. Font, background and text colour have been
350 // prepared according to selection, focus and such.
351 // item: item index to be drawn, may be wxNOT_FOUND when painting combo control itself
352 // and there is no valid selection
353 // flags: wxODCB_PAINTING_CONTROL is set if painting to combo control instead of list
354 virtual void OnDrawItem( wxDC
& dc
, const wxRect
& rect
, int item
, int flags
) const;
356 // Callback for item height, or -1 for default
357 virtual wxCoord
OnMeasureItem( size_t item
) const;
359 // Callback for item width, or -1 for default/undetermined
360 virtual wxCoord
OnMeasureItemWidth( size_t item
) const;
362 // Callback for background drawing. Flags are same as with
364 virtual void OnDrawBackground( wxDC
& dc
, const wxRect
& rect
, int item
, int flags
) const;
366 // NULL popup can be used to indicate default interface
367 virtual void DoSetPopupControl(wxComboPopup
* popup
);
369 // clears all allocated client datas
370 void ClearClientDatas();
372 wxVListBoxComboPopup
* GetVListBoxComboPopup() const
374 return (wxVListBoxComboPopup
*) m_popupInterface
;
377 virtual int DoInsertItems(const wxArrayStringsAdapter
& items
,
379 void **clientData
, wxClientDataType type
);
380 virtual void DoSetItemClientData(unsigned int n
, void* clientData
);
381 virtual void* DoGetItemClientData(unsigned int n
) const;
383 // temporary storage for the initial choices
384 //const wxString* m_baseChoices;
385 //int m_baseChoicesCount;
386 wxArrayString m_initChs
;
391 DECLARE_EVENT_TABLE()
393 DECLARE_DYNAMIC_CLASS(wxOwnerDrawnComboBox
)
397 #endif // wxUSE_ODCOMBOBOX