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_
17 #if wxUSE_OWNERDRAWNCOMBOBOX
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
44 // when set, we are painting the selected item in control,
46 wxCP_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 wxComboControl. 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();
87 // Callbacks for drawing and measuring items. Override in a derived class for
89 // item: item index to be drawn, may be wxNOT_FOUND when painting combo control itself
90 // and there is no valid selection
91 // flags: wxCP_PAINTING_CONTROL is set if painting to combo control instead of list
92 virtual void OnDrawItem( wxDC
& dc
, const wxRect
& rect
, int item
, int flags
) const;
94 // Return item width, or -1 for calculating from text extent (default)
95 virtual wxCoord
OnMeasureItemWidth( size_t item
) const;
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
) 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 OnGetLineHeight(line
); }
123 // Called by OnComboDoubleClick and OnComboKeyEvent
124 bool HandleKey( int keycode
, bool saturate
);
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 // Re-calculates width for given item
133 void CheckWidth( int pos
);
135 // wxVListBox implementation
136 virtual void OnDrawItem(wxDC
& dc
, const wxRect
& rect
, size_t n
) const;
137 //virtual wxCoord OnMeasureItem(size_t n) const;
138 void OnDrawBackground(wxDC
& dc
, const wxRect
& rect
, size_t n
) const;
140 // Return item height
141 virtual wxCoord
OnMeasureItem( size_t item
) const;
143 // filter mouse move events happening outside the list box
144 // move selection with cursor
145 void OnMouseMove(wxMouseEvent
& event
);
146 void OnMouseWheel(wxMouseEvent
& event
);
147 void OnKey(wxKeyEvent
& event
);
148 void OnLeftClick(wxMouseEvent
& event
);
150 wxArrayString m_strings
;
151 wxArrayPtrVoid m_clientDatas
;
152 wxArrayInt m_widths
; // cached line widths
156 //wxString m_stringValue; // displayed text (may be different than m_strings[m_value])
157 int m_value
; // selection
158 int m_itemHover
; // on which item the cursor is
159 int m_widestWidth
; // width of widest item thus far
161 int m_baseImageWidth
; // how much per item drawn in addition to text
162 int m_itemHeight
; // default item height (calculate from font size
163 // and used in the absence of callback)
164 wxClientDataType m_clientDataItemsType
;
168 // has the mouse been released on this control?
171 DECLARE_EVENT_TABLE()
175 // ----------------------------------------------------------------------------
176 // wxOwnerDrawnComboBox: a generic wxComboBox that allows custom paint items
177 // in addition to many other types of customization already allowed by
178 // the wxComboControl.
179 // ----------------------------------------------------------------------------
181 class WXDLLIMPEXP_ADV wxOwnerDrawnComboBox
: public wxComboControl
,
182 public wxItemContainer
184 friend class wxComboPopupWindow
;
185 friend class wxComboControlBase
;
189 wxOwnerDrawnComboBox() : wxComboControl() { Init(); }
191 wxOwnerDrawnComboBox(wxWindow
*parent
,
193 const wxString
& value
,
197 const wxString choices
[],
199 const wxValidator
& validator
= wxDefaultValidator
,
200 const wxString
& name
= wxComboBoxNameStr
)
205 (void)Create(parent
, id
, value
, pos
, size
, n
,
206 choices
, style
, validator
, name
);
209 bool Create(wxWindow
*parent
,
211 const wxString
& value
,
212 const wxPoint
& pos
= wxDefaultPosition
,
213 const wxSize
& size
= wxDefaultSize
,
215 const wxValidator
& validator
= wxDefaultValidator
,
216 const wxString
& name
= wxComboBoxNameStr
);
218 wxOwnerDrawnComboBox(wxWindow
*parent
,
220 const wxString
& value
,
223 const wxArrayString
& choices
,
225 const wxValidator
& validator
= wxDefaultValidator
,
226 const wxString
& name
= wxComboBoxNameStr
);
228 bool Create(wxWindow
*parent
,
230 const wxString
& value
= wxEmptyString
,
231 const wxPoint
& pos
= wxDefaultPosition
,
232 const wxSize
& size
= wxDefaultSize
,
234 const wxString choices
[] = (const wxString
*) NULL
,
236 const wxValidator
& validator
= wxDefaultValidator
,
237 const wxString
& name
= wxComboBoxNameStr
);
239 bool Create(wxWindow
*parent
,
241 const wxString
& value
,
244 const wxArrayString
& choices
,
246 const wxValidator
& validator
= wxDefaultValidator
,
247 const wxString
& name
= wxComboBoxNameStr
);
249 virtual ~wxOwnerDrawnComboBox();
251 // NULL popup can be used to indicate default interface
252 virtual void SetPopupControl( wxComboPopup
* popup
);
254 // wxControlWithItems methods
255 virtual void Clear();
256 virtual void Delete(unsigned int n
);
257 virtual unsigned int GetCount() const;
258 virtual wxString
GetString(unsigned int n
) const;
259 virtual void SetString(unsigned int n
, const wxString
& s
);
260 virtual int FindString(const wxString
& s
) const;
261 virtual void Select(int n
);
262 virtual int GetSelection() const;
263 virtual void SetSelection(int n
) { Select(n
); }
265 wxCONTROL_ITEMCONTAINER_CLIENTDATAOBJECT_RECAST
269 // clears all allocated client datas
270 void ClearClientDatas();
272 virtual int DoAppend(const wxString
& item
);
273 virtual int DoInsert(const wxString
& item
, unsigned int pos
);
274 virtual void DoSetItemClientData(unsigned int n
, void* clientData
);
275 virtual void* DoGetItemClientData(unsigned int n
) const;
276 virtual void DoSetItemClientObject(unsigned int n
, wxClientData
* clientData
);
277 virtual wxClientData
* DoGetItemClientObject(unsigned int n
) const;
279 // overload m_popupInterface member so we can access specific popup interface easier
280 wxVListBoxComboPopup
* m_popupInterface
;
282 // temporary storage for the initial choices
283 //const wxString* m_baseChoices;
284 //int m_baseChoicesCount;
285 wxArrayString m_initChs
;
290 DECLARE_EVENT_TABLE()
292 DECLARE_DYNAMIC_CLASS(wxOwnerDrawnComboBox
)
296 #endif // wxUSE_OWNERDRAWNCOMBOBOX