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)
45 // when set, we are painting the selected item in control,
47 wxODCB_PAINTING_CONTROL
= 0x0001
51 // ----------------------------------------------------------------------------
52 // wxVListBoxComboPopup is a wxVListBox customized to act as a popup control.
55 // wxOwnerDrawnComboBox uses this as its popup. However, it always derives
56 // from native wxComboCtrl. If you need to use this popup with
57 // wxGenericComboControl, then remember that vast majority of item manipulation
58 // functionality is implemented in the wxVListBoxComboPopup class itself.
60 // ----------------------------------------------------------------------------
63 class WXDLLIMPEXP_ADV wxVListBoxComboPopup
: public wxVListBox
,
66 friend class wxOwnerDrawnComboBox
;
70 wxVListBoxComboPopup() : wxVListBox(), wxComboPopup() { }
71 virtual ~wxVListBoxComboPopup();
75 virtual bool Create(wxWindow
* parent
);
76 virtual wxWindow
*GetControl() { return this; }
77 virtual void SetStringValue( const wxString
& value
);
78 virtual wxString
GetStringValue() const;
81 virtual void OnPopup();
82 virtual wxSize
GetAdjustedSize( int minWidth
, int prefHeight
, int maxHeight
);
83 virtual void PaintComboControl( wxDC
& dc
, const wxRect
& rect
);
84 virtual void OnComboKeyEvent( wxKeyEvent
& event
);
85 virtual void OnComboDoubleClick();
86 virtual bool LazyCreate();
89 void SetSelection( int item
);
90 void Insert( const wxString
& item
, int pos
);
91 int Append(const wxString
& item
);
93 void Delete( unsigned int item
);
94 void SetItemClientData(unsigned int n
, void* clientData
, wxClientDataType clientDataItemsType
);
95 void *GetItemClientData(unsigned int n
) const;
96 void SetString( int item
, const wxString
& str
);
97 wxString
GetString( int item
) const;
98 unsigned int GetCount() const;
99 int FindString(const wxString
& s
, bool bCase
= false) const;
100 int GetSelection() const;
102 //void Populate( int n, const wxString choices[] );
103 void Populate( const wxArrayString
& choices
);
104 void ClearClientDatas();
107 int GetItemAtPosition( const wxPoint
& pos
) { return HitTest(pos
); }
108 wxCoord
GetTotalHeight() const { return EstimateTotalHeight(); }
109 wxCoord
GetLineHeight(int line
) const { return OnGetLineHeight(line
); }
113 // Called by OnComboDoubleClick and OnComboKeyEvent
114 bool HandleKey( int keycode
, bool saturate
, wxChar unicode
= 0 );
116 // sends combobox select event from the parent combo control
117 void SendComboBoxEvent( int selection
);
119 // gets value, sends event and dismisses
120 void DismissWithEvent();
122 // OnMeasureItemWidth will be called on next GetAdjustedSize.
123 void ItemWidthChanged(unsigned int item
)
126 m_widthsDirty
= true;
129 // Callbacks for drawing and measuring items. Override in a derived class for
130 // owner-drawnness. Font, background and text colour have been prepared according
131 // to selection, focus and such.
133 // item: item index to be drawn, may be wxNOT_FOUND when painting combo control itself
134 // and there is no valid selection
135 // flags: wxODCB_PAINTING_CONTROL is set if painting to combo control instead of list
136 // NOTE: If wxVListBoxComboPopup is used with wxComboCtrl class not derived from
137 // wxOwnerDrawnComboBox, this method must be overridden.
138 virtual void OnDrawItem( wxDC
& dc
, const wxRect
& rect
, int item
, int flags
) const;
140 // This is same as in wxVListBox
141 virtual wxCoord
OnMeasureItem( size_t item
) const;
143 // Return item width, or -1 for calculating from text extent (default)
144 virtual wxCoord
OnMeasureItemWidth( size_t item
) const;
146 // Draw item and combo control background. Flags are same as with OnDrawItem.
147 // NB: Can't use name OnDrawBackground because of virtual function hiding warnings.
148 virtual void OnDrawBg(wxDC
& dc
, const wxRect
& rect
, int item
, int flags
) const;
150 // Additional wxVListBox implementation (no need to override in derived classes)
151 virtual void OnDrawItem(wxDC
& dc
, const wxRect
& rect
, size_t n
) const;
152 void OnDrawBackground(wxDC
& dc
, const wxRect
& rect
, size_t n
) const;
154 // filter mouse move events happening outside the list box
155 // move selection with cursor
156 void OnMouseMove(wxMouseEvent
& event
);
157 void OnMouseWheel(wxMouseEvent
& event
);
158 void OnKey(wxKeyEvent
& event
);
159 void OnLeftClick(wxMouseEvent
& event
);
161 // Return the widest item width (recalculating it if necessary)
162 int GetWidestItemWidth() { CalcWidths(); return m_widestWidth
; }
164 // Return the index of the widest item (recalculating it if necessary)
165 int GetWidestItem() { CalcWidths(); return m_widestItem
; }
167 // Stop partial completion (when some other event occurs)
168 void StopPartialCompletion();
170 wxArrayString m_strings
;
171 wxArrayPtrVoid m_clientDatas
;
175 //wxString m_stringValue; // displayed text (may be different than m_strings[m_value])
176 int m_value
; // selection
177 int m_itemHover
; // on which item the cursor is
178 int m_itemHeight
; // default item height (calculate from font size
179 // and used in the absence of callback)
180 wxClientDataType m_clientDataItemsType
;
184 // Cached item widths (in pixels).
187 // Width of currently widest item.
190 // Index of currently widest item.
193 // Measure some items in next GetAdjustedSize?
196 // Find widest item in next GetAdjustedSize?
199 // has the mouse been released on this control?
202 // Recalculate widths if they are dirty
205 // Partial completion string
206 wxString m_partialCompletionString
;
209 // Partial completion timer
210 wxTimer m_partialCompletionTimer
;
211 #endif // wxUSE_TIMER
213 DECLARE_EVENT_TABLE()
217 // ----------------------------------------------------------------------------
218 // wxOwnerDrawnComboBox: a generic wxComboBox that allows custom paint items
219 // in addition to many other types of customization already allowed by
221 // ----------------------------------------------------------------------------
223 class WXDLLIMPEXP_ADV wxOwnerDrawnComboBox
: public wxComboCtrl
,
224 public wxItemContainer
226 //friend class wxComboPopupWindow;
227 friend class wxVListBoxComboPopup
;
231 wxOwnerDrawnComboBox() : wxComboCtrl() { Init(); }
233 wxOwnerDrawnComboBox(wxWindow
*parent
,
235 const wxString
& value
,
239 const wxString choices
[],
241 const wxValidator
& validator
= wxDefaultValidator
,
242 const wxString
& name
= wxComboBoxNameStr
)
247 (void)Create(parent
, id
, value
, pos
, size
, n
,
248 choices
, style
, validator
, name
);
251 bool Create(wxWindow
*parent
,
253 const wxString
& value
= wxEmptyString
,
254 const wxPoint
& pos
= wxDefaultPosition
,
255 const wxSize
& size
= wxDefaultSize
,
257 const wxValidator
& validator
= wxDefaultValidator
,
258 const wxString
& name
= wxComboBoxNameStr
);
260 wxOwnerDrawnComboBox(wxWindow
*parent
,
262 const wxString
& value
,
265 const wxArrayString
& choices
,
267 const wxValidator
& validator
= wxDefaultValidator
,
268 const wxString
& name
= wxComboBoxNameStr
);
270 bool Create(wxWindow
*parent
,
272 const wxString
& value
,
276 const wxString choices
[],
278 const wxValidator
& validator
= wxDefaultValidator
,
279 const wxString
& name
= wxComboBoxNameStr
);
281 bool Create(wxWindow
*parent
,
283 const wxString
& value
,
286 const wxArrayString
& choices
,
288 const wxValidator
& validator
= wxDefaultValidator
,
289 const wxString
& name
= wxComboBoxNameStr
);
291 virtual ~wxOwnerDrawnComboBox();
293 // Prevent app from using wxComboPopup
294 void SetPopupControl(wxVListBoxComboPopup
* popup
)
296 DoSetPopupControl(popup
);
299 // wxControlWithItems methods
300 virtual void Clear();
301 virtual void Delete(unsigned int n
);
302 virtual unsigned int GetCount() const;
303 virtual wxString
GetString(unsigned int n
) const;
304 virtual void SetString(unsigned int n
, const wxString
& s
);
305 virtual int FindString(const wxString
& s
, bool bCase
= false) const;
306 virtual void Select(int n
);
307 virtual int GetSelection() const;
308 virtual void SetSelection(int n
) { Select(n
); }
311 // Prevent a method from being hidden
312 virtual void SetSelection(long from
, long to
)
314 wxComboCtrl::SetSelection(from
,to
);
317 // Return the widest item width (recalculating it if necessary)
318 virtual int GetWidestItemWidth() { EnsurePopupControl(); return GetVListBoxComboPopup()->GetWidestItemWidth(); }
320 // Return the index of the widest item (recalculating it if necessary)
321 virtual int GetWidestItem() { EnsurePopupControl(); return GetVListBoxComboPopup()->GetWidestItem(); }
323 wxCONTROL_ITEMCONTAINER_CLIENTDATAOBJECT_RECAST
327 // Callback for drawing. Font, background and text colour have been
328 // prepared according to selection, focus and such.
329 // item: item index to be drawn, may be wxNOT_FOUND when painting combo control itself
330 // and there is no valid selection
331 // flags: wxODCB_PAINTING_CONTROL is set if painting to combo control instead of list
332 virtual void OnDrawItem( wxDC
& dc
, const wxRect
& rect
, int item
, int flags
) const;
334 // Callback for item height, or -1 for default
335 virtual wxCoord
OnMeasureItem( size_t item
) const;
337 // Callback for item width, or -1 for default/undetermined
338 virtual wxCoord
OnMeasureItemWidth( size_t item
) const;
340 // Callback for background drawing. Flags are same as with
342 virtual void OnDrawBackground( wxDC
& dc
, const wxRect
& rect
, int item
, int flags
) const;
344 // NULL popup can be used to indicate default interface
345 virtual void DoSetPopupControl(wxComboPopup
* popup
);
347 // clears all allocated client datas
348 void ClearClientDatas();
350 wxVListBoxComboPopup
* GetVListBoxComboPopup() const
352 return (wxVListBoxComboPopup
*) m_popupInterface
;
355 virtual int DoAppend(const wxString
& item
);
356 virtual int DoInsert(const wxString
& item
, unsigned int pos
);
357 virtual void DoSetItemClientData(unsigned int n
, void* clientData
);
358 virtual void* DoGetItemClientData(unsigned int n
) const;
359 virtual void DoSetItemClientObject(unsigned int n
, wxClientData
* clientData
);
360 virtual wxClientData
* DoGetItemClientObject(unsigned int n
) const;
362 // temporary storage for the initial choices
363 //const wxString* m_baseChoices;
364 //int m_baseChoicesCount;
365 wxArrayString m_initChs
;
370 DECLARE_EVENT_TABLE()
372 DECLARE_DYNAMIC_CLASS(wxOwnerDrawnComboBox
)
376 #endif // wxUSE_ODCOMBOBOX