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 wxWindow
*GetControl() { return this; }
84 virtual void SetStringValue( const wxString
& value
);
85 virtual wxString
GetStringValue() const;
88 virtual void OnPopup();
89 virtual wxSize
GetAdjustedSize( int minWidth
, int prefHeight
, int maxHeight
);
90 virtual void PaintComboControl( wxDC
& dc
, const wxRect
& rect
);
91 virtual void OnComboKeyEvent( wxKeyEvent
& event
);
92 virtual void OnComboCharEvent( wxKeyEvent
& event
);
93 virtual void OnComboDoubleClick();
94 virtual bool LazyCreate();
97 void SetSelection( int item
);
98 void Insert( const wxString
& item
, int pos
);
99 int Append(const wxString
& item
);
101 void Delete( unsigned int item
);
102 void SetItemClientData(unsigned int n
, void* clientData
, wxClientDataType clientDataItemsType
);
103 void *GetItemClientData(unsigned int n
) const;
104 void SetString( int item
, const wxString
& str
);
105 wxString
GetString( int item
) const;
106 unsigned int GetCount() const;
107 int FindString(const wxString
& s
, bool bCase
= false) const;
108 int GetSelection() const;
110 //void Populate( int n, const wxString choices[] );
111 void Populate( const wxArrayString
& choices
);
112 void ClearClientDatas();
115 int GetItemAtPosition( const wxPoint
& pos
) { return HitTest(pos
); }
116 wxCoord
GetTotalHeight() const { return EstimateTotalHeight(); }
117 wxCoord
GetLineHeight(int line
) const { return OnGetRowHeight(line
); }
121 // Called by OnComboDoubleClick and OnCombo{Key,Char}Event
122 bool HandleKey( int keycode
, bool saturate
, wxChar keychar
= 0 );
124 // sends combobox select event from the parent combo control
125 void SendComboBoxEvent( int selection
);
127 // gets value, sends event and dismisses
128 void DismissWithEvent();
130 // OnMeasureItemWidth will be called on next GetAdjustedSize.
131 void ItemWidthChanged(unsigned int item
)
134 m_widthsDirty
= true;
137 // Callbacks for drawing and measuring items. Override in a derived class for
138 // owner-drawnness. Font, background and text colour have been prepared according
139 // to selection, focus and such.
141 // item: item index to be drawn, may be wxNOT_FOUND when painting combo control itself
142 // and there is no valid selection
143 // flags: wxODCB_PAINTING_CONTROL is set if painting to combo control instead of list
145 // NOTE: If wxVListBoxComboPopup is used with a wxComboCtrl class not derived from
146 // wxOwnerDrawnComboBox, this method must be overridden.
147 virtual void OnDrawItem( wxDC
& dc
, const wxRect
& rect
, int item
, int flags
) const;
149 // This is same as in wxVListBox
150 virtual wxCoord
OnMeasureItem( size_t item
) const;
152 // Return item width, or -1 for calculating from text extent (default)
153 virtual wxCoord
OnMeasureItemWidth( size_t item
) const;
155 // Draw item and combo control background. Flags are same as with OnDrawItem.
156 // NB: Can't use name OnDrawBackground because of virtual function hiding warnings.
157 virtual void OnDrawBg(wxDC
& dc
, const wxRect
& rect
, int item
, int flags
) const;
159 // Additional wxVListBox implementation (no need to override in derived classes)
160 virtual void OnDrawItem(wxDC
& dc
, const wxRect
& rect
, size_t n
) const;
161 void OnDrawBackground(wxDC
& dc
, const wxRect
& rect
, size_t n
) const;
163 // filter mouse move events happening outside the list box
164 // move selection with cursor
165 void OnMouseMove(wxMouseEvent
& event
);
166 void OnMouseWheel(wxMouseEvent
& event
);
167 void OnKey(wxKeyEvent
& event
);
168 void OnChar(wxKeyEvent
& event
);
169 void OnLeftClick(wxMouseEvent
& event
);
171 // Return the widest item width (recalculating it if necessary)
172 int GetWidestItemWidth() { CalcWidths(); return m_widestWidth
; }
174 // Return the index of the widest item (recalculating it if necessary)
175 int GetWidestItem() { CalcWidths(); return m_widestItem
; }
177 // Stop partial completion (when some other event occurs)
178 void StopPartialCompletion();
180 wxArrayString m_strings
;
181 wxArrayPtrVoid m_clientDatas
;
185 //wxString m_stringValue; // displayed text (may be different than m_strings[m_value])
186 int m_value
; // selection
187 int m_itemHover
; // on which item the cursor is
188 int m_itemHeight
; // default item height (calculate from font size
189 // and used in the absence of callback)
190 wxClientDataType m_clientDataItemsType
;
194 // Cached item widths (in pixels).
197 // Width of currently widest item.
200 // Index of currently widest item.
203 // Measure some items in next GetAdjustedSize?
206 // Find widest item in next GetAdjustedSize?
209 // has the mouse been released on this control?
212 // Recalculate widths if they are dirty
215 // Partial completion string
216 wxString m_partialCompletionString
;
218 wxString m_stringValue
;
221 // Partial completion timer
222 wxTimer m_partialCompletionTimer
;
223 #endif // wxUSE_TIMER
225 DECLARE_EVENT_TABLE()
229 // ----------------------------------------------------------------------------
230 // wxOwnerDrawnComboBox: a generic wxComboBox that allows custom paint items
231 // in addition to many other types of customization already allowed by
233 // ----------------------------------------------------------------------------
235 class WXDLLIMPEXP_ADV wxOwnerDrawnComboBox
: public wxComboCtrl
,
236 public wxItemContainer
238 //friend class wxComboPopupWindow;
239 friend class wxVListBoxComboPopup
;
243 wxOwnerDrawnComboBox() : wxComboCtrl() { Init(); }
245 wxOwnerDrawnComboBox(wxWindow
*parent
,
247 const wxString
& value
,
251 const wxString choices
[],
253 const wxValidator
& validator
= wxDefaultValidator
,
254 const wxString
& name
= wxComboBoxNameStr
)
259 (void)Create(parent
, id
, value
, pos
, size
, n
,
260 choices
, style
, validator
, name
);
263 bool Create(wxWindow
*parent
,
265 const wxString
& value
= wxEmptyString
,
266 const wxPoint
& pos
= wxDefaultPosition
,
267 const wxSize
& size
= wxDefaultSize
,
269 const wxValidator
& validator
= wxDefaultValidator
,
270 const wxString
& name
= wxComboBoxNameStr
);
272 wxOwnerDrawnComboBox(wxWindow
*parent
,
274 const wxString
& value
,
277 const wxArrayString
& choices
,
279 const wxValidator
& validator
= wxDefaultValidator
,
280 const wxString
& name
= wxComboBoxNameStr
);
282 bool Create(wxWindow
*parent
,
284 const wxString
& value
,
288 const wxString choices
[],
290 const wxValidator
& validator
= wxDefaultValidator
,
291 const wxString
& name
= wxComboBoxNameStr
);
293 bool Create(wxWindow
*parent
,
295 const wxString
& value
,
298 const wxArrayString
& choices
,
300 const wxValidator
& validator
= wxDefaultValidator
,
301 const wxString
& name
= wxComboBoxNameStr
);
303 virtual ~wxOwnerDrawnComboBox();
305 // Prevent app from using wxComboPopup
306 void SetPopupControl(wxVListBoxComboPopup
* popup
)
308 DoSetPopupControl(popup
);
311 // wxControlWithItems methods
312 virtual unsigned int GetCount() const;
313 virtual wxString
GetString(unsigned int n
) const;
314 virtual void SetString(unsigned int n
, const wxString
& s
);
315 virtual int FindString(const wxString
& s
, bool bCase
= false) const;
316 virtual void Select(int n
);
317 virtual int GetSelection() const;
318 virtual void SetSelection(int n
) { Select(n
); }
321 // Prevent a method from being hidden
322 virtual void SetSelection(long from
, long to
)
324 wxComboCtrl::SetSelection(from
,to
);
327 // Return the widest item width (recalculating it if necessary)
328 virtual int GetWidestItemWidth() { EnsurePopupControl(); return GetVListBoxComboPopup()->GetWidestItemWidth(); }
330 // Return the index of the widest item (recalculating it if necessary)
331 virtual int GetWidestItem() { EnsurePopupControl(); return GetVListBoxComboPopup()->GetWidestItem(); }
333 virtual bool IsSorted() const { return HasFlag(wxCB_SORT
); }
335 wxCONTROL_ITEMCONTAINER_CLIENTDATAOBJECT_RECAST
338 virtual void DoClear();
339 virtual void DoDeleteOneItem(unsigned int n
);
341 // Callback for drawing. Font, background and text colour have been
342 // prepared according to selection, focus and such.
343 // item: item index to be drawn, may be wxNOT_FOUND when painting combo control itself
344 // and there is no valid selection
345 // flags: wxODCB_PAINTING_CONTROL is set if painting to combo control instead of list
346 virtual void OnDrawItem( wxDC
& dc
, const wxRect
& rect
, int item
, int flags
) const;
348 // Callback for item height, or -1 for default
349 virtual wxCoord
OnMeasureItem( size_t item
) const;
351 // Callback for item width, or -1 for default/undetermined
352 virtual wxCoord
OnMeasureItemWidth( size_t item
) const;
354 // Callback for background drawing. Flags are same as with
356 virtual void OnDrawBackground( wxDC
& dc
, const wxRect
& rect
, int item
, int flags
) const;
358 // NULL popup can be used to indicate default interface
359 virtual void DoSetPopupControl(wxComboPopup
* popup
);
361 // clears all allocated client datas
362 void ClearClientDatas();
364 wxVListBoxComboPopup
* GetVListBoxComboPopup() const
366 return (wxVListBoxComboPopup
*) m_popupInterface
;
369 virtual int DoInsertItems(const wxArrayStringsAdapter
& items
,
371 void **clientData
, wxClientDataType type
);
372 virtual void DoSetItemClientData(unsigned int n
, void* clientData
);
373 virtual void* DoGetItemClientData(unsigned int n
) const;
375 // temporary storage for the initial choices
376 //const wxString* m_baseChoices;
377 //int m_baseChoicesCount;
378 wxArrayString m_initChs
;
383 DECLARE_EVENT_TABLE()
385 DECLARE_DYNAMIC_CLASS(wxOwnerDrawnComboBox
)
389 #endif // wxUSE_ODCOMBOBOX