1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxOwnerDrawnComboBox and wxVListBoxPopup
4 // Author: Jaakko Salli
6 // Created: Apr-30-2006
7 // Copyright: (c) Jaakko Salli
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_ODCOMBO_H_
12 #define _WX_ODCOMBO_H_
19 #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)
42 enum wxOwnerDrawnComboBoxPaintingFlags
44 // when set, we are painting the selected item in control,
46 wxODCB_PAINTING_CONTROL
= 0x0001,
49 // when set, we are painting an item which should have
50 // focus rectangle painted in the background. Text colour
51 // and clipping region are then appropriately set in
52 // the default OnDrawBackground implementation.
53 wxODCB_PAINTING_SELECTED
= 0x0002
57 // ----------------------------------------------------------------------------
58 // wxVListBoxComboPopup is a wxVListBox customized to act as a popup control.
61 // wxOwnerDrawnComboBox uses this as its popup. However, it always derives
62 // from native wxComboCtrl. If you need to use this popup with
63 // wxGenericComboControl, then remember that vast majority of item manipulation
64 // functionality is implemented in the wxVListBoxComboPopup class itself.
66 // ----------------------------------------------------------------------------
69 class WXDLLIMPEXP_ADV wxVListBoxComboPopup
: public wxVListBox
,
72 friend class wxOwnerDrawnComboBox
;
76 wxVListBoxComboPopup() : wxVListBox(), wxComboPopup() { }
77 virtual ~wxVListBoxComboPopup();
81 virtual bool Create(wxWindow
* parent
);
82 virtual void SetFocus();
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();
95 virtual bool FindItem(const wxString
& item
, wxString
* trueItem
);
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
, bool bCase
= false) 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 OnGetRowHeight(line
); }
122 // Called by OnComboDoubleClick and OnCombo{Key,Char}Event
123 bool HandleKey( int keycode
, bool saturate
, wxChar keychar
= 0 );
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 // OnMeasureItemWidth will be called on next GetAdjustedSize.
132 void ItemWidthChanged(unsigned int item
)
135 m_widthsDirty
= true;
138 // Callbacks for drawing and measuring items. Override in a derived class for
139 // owner-drawnness. Font, background and text colour have been prepared according
140 // to selection, focus and such.
142 // item: item index to be drawn, may be wxNOT_FOUND when painting combo control itself
143 // and there is no valid selection
144 // flags: wxODCB_PAINTING_CONTROL is set if painting to combo control instead of list
146 // NOTE: If wxVListBoxComboPopup is used with a wxComboCtrl class not derived from
147 // wxOwnerDrawnComboBox, this method must be overridden.
148 virtual void OnDrawItem( wxDC
& dc
, const wxRect
& rect
, int item
, int flags
) const;
150 // This is same as in wxVListBox
151 virtual wxCoord
OnMeasureItem( size_t item
) const;
153 // Return item width, or -1 for calculating from text extent (default)
154 virtual wxCoord
OnMeasureItemWidth( size_t item
) const;
156 // Draw item and combo control background. Flags are same as with OnDrawItem.
157 // NB: Can't use name OnDrawBackground because of virtual function hiding warnings.
158 virtual void OnDrawBg(wxDC
& dc
, const wxRect
& rect
, int item
, int flags
) const;
160 // Additional wxVListBox implementation (no need to override in derived classes)
161 virtual void OnDrawItem(wxDC
& dc
, const wxRect
& rect
, size_t n
) const;
162 void OnDrawBackground(wxDC
& dc
, const wxRect
& rect
, size_t n
) const;
164 // filter mouse move events happening outside the list box
165 // move selection with cursor
166 void OnMouseMove(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
:
236 public wxWindowWithItems
<wxComboCtrl
, wxItemContainer
>
238 //friend class wxComboPopupWindow;
239 friend class wxVListBoxComboPopup
;
243 wxOwnerDrawnComboBox() { Init(); }
245 wxOwnerDrawnComboBox(wxWindow
*parent
,
247 const wxString
& value
,
251 const wxString choices
[],
253 const wxValidator
& validator
= wxDefaultValidator
,
254 const wxString
& name
= wxComboBoxNameStr
)
258 (void)Create(parent
, id
, value
, pos
, size
, n
,
259 choices
, style
, validator
, name
);
262 bool Create(wxWindow
*parent
,
264 const wxString
& value
= wxEmptyString
,
265 const wxPoint
& pos
= wxDefaultPosition
,
266 const wxSize
& size
= wxDefaultSize
,
268 const wxValidator
& validator
= wxDefaultValidator
,
269 const wxString
& name
= wxComboBoxNameStr
);
271 wxOwnerDrawnComboBox(wxWindow
*parent
,
273 const wxString
& value
= wxEmptyString
,
274 const wxPoint
& pos
= wxDefaultPosition
,
275 const wxSize
& size
= wxDefaultSize
,
276 const wxArrayString
& choices
= wxArrayString(),
278 const wxValidator
& validator
= wxDefaultValidator
,
279 const wxString
& name
= wxComboBoxNameStr
);
281 bool Create(wxWindow
*parent
,
283 const wxString
& value
,
287 const wxString choices
[],
289 const wxValidator
& validator
= wxDefaultValidator
,
290 const wxString
& name
= wxComboBoxNameStr
);
292 bool Create(wxWindow
*parent
,
294 const wxString
& value
,
297 const wxArrayString
& choices
,
299 const wxValidator
& validator
= wxDefaultValidator
,
300 const wxString
& name
= wxComboBoxNameStr
);
302 virtual ~wxOwnerDrawnComboBox();
304 // Prevent app from using wxComboPopup
305 void SetPopupControl(wxVListBoxComboPopup
* popup
)
307 DoSetPopupControl(popup
);
310 // wxControlWithItems methods
311 virtual unsigned int GetCount() const;
312 virtual wxString
GetString(unsigned int n
) const;
313 virtual void SetString(unsigned int n
, const wxString
& s
);
314 virtual int FindString(const wxString
& s
, bool bCase
= false) const;
315 virtual void Select(int n
);
316 virtual int GetSelection() const;
318 // Override these just to maintain consistency with virtual methods
320 virtual void Clear();
321 virtual void GetSelection(long *from
, long *to
) const;
323 virtual void SetSelection(int n
) { Select(n
); }
326 // Prevent a method from being hidden
327 virtual void SetSelection(long from
, long to
)
329 wxComboCtrl::SetSelection(from
,to
);
332 // Return the widest item width (recalculating it if necessary)
333 virtual int GetWidestItemWidth() { EnsurePopupControl(); return GetVListBoxComboPopup()->GetWidestItemWidth(); }
335 // Return the index of the widest item (recalculating it if necessary)
336 virtual int GetWidestItem() { EnsurePopupControl(); return GetVListBoxComboPopup()->GetWidestItem(); }
338 virtual bool IsSorted() const { return HasFlag(wxCB_SORT
); }
341 virtual void DoClear();
342 virtual void DoDeleteOneItem(unsigned int n
);
344 // Callback for drawing. Font, background and text colour have been
345 // prepared according to selection, focus and such.
346 // item: item index to be drawn, may be wxNOT_FOUND when painting combo control itself
347 // and there is no valid selection
348 // flags: wxODCB_PAINTING_CONTROL is set if painting to combo control instead of list
349 virtual void OnDrawItem( wxDC
& dc
, const wxRect
& rect
, int item
, int flags
) const;
351 // Callback for item height, or -1 for default
352 virtual wxCoord
OnMeasureItem( size_t item
) const;
354 // Callback for item width, or -1 for default/undetermined
355 virtual wxCoord
OnMeasureItemWidth( size_t item
) const;
357 // override base implementation so we can return the size for the
359 virtual wxSize
DoGetBestSize() const;
361 // Callback for background drawing. Flags are same as with
363 virtual void OnDrawBackground( wxDC
& dc
, const wxRect
& rect
, int item
, int flags
) const;
365 // NULL popup can be used to indicate default interface
366 virtual void DoSetPopupControl(wxComboPopup
* popup
);
368 // clears all allocated client datas
369 void ClearClientDatas();
371 wxVListBoxComboPopup
* GetVListBoxComboPopup() const
373 return (wxVListBoxComboPopup
*) m_popupInterface
;
376 virtual int DoInsertItems(const wxArrayStringsAdapter
& items
,
378 void **clientData
, wxClientDataType type
);
379 virtual void DoSetItemClientData(unsigned int n
, void* clientData
);
380 virtual void* DoGetItemClientData(unsigned int n
) const;
382 // temporary storage for the initial choices
383 //const wxString* m_baseChoices;
384 //int m_baseChoicesCount;
385 wxArrayString m_initChs
;
390 DECLARE_EVENT_TABLE()
392 DECLARE_DYNAMIC_CLASS(wxOwnerDrawnComboBox
)
396 #endif // wxUSE_ODCOMBOBOX