]> git.saurik.com Git - wxWidgets.git/blob - include/wx/odcombo.h
add more flexible and safer template Connect() overloads (#10000)
[wxWidgets.git] / include / wx / odcombo.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/odcombo.h
3 // Purpose: wxOwnerDrawnComboBox and wxVListBoxPopup
4 // Author: Jaakko Salli
5 // Modified by:
6 // Created: Apr-30-2006
7 // RCS-ID: $Id$
8 // Copyright: (c) Jaakko Salli
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_ODCOMBO_H_
13 #define _WX_ODCOMBO_H_
14
15 #include "wx/defs.h"
16
17 #if wxUSE_ODCOMBOBOX
18
19 #include "wx/combo.h"
20 #include "wx/ctrlsub.h"
21 #include "wx/vlbox.h"
22 #include "wx/timer.h"
23
24
25 //
26 // New window styles for wxOwnerDrawnComboBox
27 //
28 enum
29 {
30 // Double-clicking cycles item if wxCB_READONLY is also used.
31 wxODCB_DCLICK_CYCLES = wxCC_SPECIAL_DCLICK,
32
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
37 };
38
39
40 //
41 // Callback flags (see wxOwnerDrawnComboBox::OnDrawItem)
42 //
43 enum wxOwnerDrawnComboBoxPaintingFlags
44 {
45 // when set, we are painting the selected item in control,
46 // not in the popup
47 wxODCB_PAINTING_CONTROL = 0x0001,
48
49
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
55 };
56
57
58 // ----------------------------------------------------------------------------
59 // wxVListBoxComboPopup is a wxVListBox customized to act as a popup control.
60 //
61 // Notes:
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.
66 //
67 // ----------------------------------------------------------------------------
68
69
70 class WXDLLIMPEXP_ADV wxVListBoxComboPopup : public wxVListBox,
71 public wxComboPopup
72 {
73 friend class wxOwnerDrawnComboBox;
74 public:
75
76 // init and dtor
77 wxVListBoxComboPopup() : wxVListBox(), wxComboPopup() { }
78 virtual ~wxVListBoxComboPopup();
79
80 // required virtuals
81 virtual void Init();
82 virtual bool Create(wxWindow* parent);
83 virtual wxWindow *GetControl() { return this; }
84 virtual void SetStringValue( const wxString& value );
85 virtual wxString GetStringValue() const;
86
87 // more customization
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 OnComboDoubleClick();
93 virtual bool LazyCreate();
94
95 // Item management
96 void SetSelection( int item );
97 void Insert( const wxString& item, int pos );
98 int Append(const wxString& item);
99 void Clear();
100 void Delete( unsigned int item );
101 void SetItemClientData(unsigned int n, void* clientData, wxClientDataType clientDataItemsType);
102 void *GetItemClientData(unsigned int n) const;
103 void SetString( int item, const wxString& str );
104 wxString GetString( int item ) const;
105 unsigned int GetCount() const;
106 int FindString(const wxString& s, bool bCase = false) const;
107 int GetSelection() const;
108
109 //void Populate( int n, const wxString choices[] );
110 void Populate( const wxArrayString& choices );
111 void ClearClientDatas();
112
113 // helpers
114 int GetItemAtPosition( const wxPoint& pos ) { return HitTest(pos); }
115 wxCoord GetTotalHeight() const { return EstimateTotalHeight(); }
116 wxCoord GetLineHeight(int line) const { return OnGetRowHeight(line); }
117
118 protected:
119
120 // Called by OnComboDoubleClick and OnComboKeyEvent
121 bool HandleKey( int keycode, bool saturate, wxChar unicode = 0 );
122
123 // sends combobox select event from the parent combo control
124 void SendComboBoxEvent( int selection );
125
126 // gets value, sends event and dismisses
127 void DismissWithEvent();
128
129 // OnMeasureItemWidth will be called on next GetAdjustedSize.
130 void ItemWidthChanged(unsigned int item)
131 {
132 m_widths[item] = -1;
133 m_widthsDirty = true;
134 }
135
136 // Callbacks for drawing and measuring items. Override in a derived class for
137 // owner-drawnness. Font, background and text colour have been prepared according
138 // to selection, focus and such.
139 //
140 // item: item index to be drawn, may be wxNOT_FOUND when painting combo control itself
141 // and there is no valid selection
142 // flags: wxODCB_PAINTING_CONTROL is set if painting to combo control instead of list
143 //
144 // NOTE: If wxVListBoxComboPopup is used with a wxComboCtrl class not derived from
145 // wxOwnerDrawnComboBox, this method must be overridden.
146 virtual void OnDrawItem( wxDC& dc, const wxRect& rect, int item, int flags) const;
147
148 // This is same as in wxVListBox
149 virtual wxCoord OnMeasureItem( size_t item ) const;
150
151 // Return item width, or -1 for calculating from text extent (default)
152 virtual wxCoord OnMeasureItemWidth( size_t item ) const;
153
154 // Draw item and combo control background. Flags are same as with OnDrawItem.
155 // NB: Can't use name OnDrawBackground because of virtual function hiding warnings.
156 virtual void OnDrawBg(wxDC& dc, const wxRect& rect, int item, int flags) const;
157
158 // Additional wxVListBox implementation (no need to override in derived classes)
159 virtual void OnDrawItem(wxDC& dc, const wxRect& rect, size_t n) const;
160 void OnDrawBackground(wxDC& dc, const wxRect& rect, size_t n) const;
161
162 // filter mouse move events happening outside the list box
163 // move selection with cursor
164 void OnMouseMove(wxMouseEvent& event);
165 void OnMouseWheel(wxMouseEvent& event);
166 void OnKey(wxKeyEvent& event);
167 void OnLeftClick(wxMouseEvent& event);
168
169 // Return the widest item width (recalculating it if necessary)
170 int GetWidestItemWidth() { CalcWidths(); return m_widestWidth; }
171
172 // Return the index of the widest item (recalculating it if necessary)
173 int GetWidestItem() { CalcWidths(); return m_widestItem; }
174
175 // Stop partial completion (when some other event occurs)
176 void StopPartialCompletion();
177
178 wxArrayString m_strings;
179 wxArrayPtrVoid m_clientDatas;
180
181 wxFont m_useFont;
182
183 //wxString m_stringValue; // displayed text (may be different than m_strings[m_value])
184 int m_value; // selection
185 int m_itemHover; // on which item the cursor is
186 int m_itemHeight; // default item height (calculate from font size
187 // and used in the absence of callback)
188 wxClientDataType m_clientDataItemsType;
189
190 private:
191
192 // Cached item widths (in pixels).
193 wxArrayInt m_widths;
194
195 // Width of currently widest item.
196 int m_widestWidth;
197
198 // Index of currently widest item.
199 int m_widestItem;
200
201 // Measure some items in next GetAdjustedSize?
202 bool m_widthsDirty;
203
204 // Find widest item in next GetAdjustedSize?
205 bool m_findWidest;
206
207 // has the mouse been released on this control?
208 bool m_clicked;
209
210 // Recalculate widths if they are dirty
211 void CalcWidths();
212
213 // Partial completion string
214 wxString m_partialCompletionString;
215
216 wxString m_stringValue;
217
218 #if wxUSE_TIMER
219 // Partial completion timer
220 wxTimer m_partialCompletionTimer;
221 #endif // wxUSE_TIMER
222
223 DECLARE_EVENT_TABLE()
224 };
225
226
227 // ----------------------------------------------------------------------------
228 // wxOwnerDrawnComboBox: a generic wxComboBox that allows custom paint items
229 // in addition to many other types of customization already allowed by
230 // the wxComboCtrl.
231 // ----------------------------------------------------------------------------
232
233 class WXDLLIMPEXP_ADV wxOwnerDrawnComboBox : public wxComboCtrl,
234 public wxItemContainer
235 {
236 //friend class wxComboPopupWindow;
237 friend class wxVListBoxComboPopup;
238 public:
239
240 // ctors and such
241 wxOwnerDrawnComboBox() : wxComboCtrl() { Init(); }
242
243 wxOwnerDrawnComboBox(wxWindow *parent,
244 wxWindowID id,
245 const wxString& value,
246 const wxPoint& pos,
247 const wxSize& size,
248 int n,
249 const wxString choices[],
250 long style = 0,
251 const wxValidator& validator = wxDefaultValidator,
252 const wxString& name = wxComboBoxNameStr)
253 : wxComboCtrl()
254 {
255 Init();
256
257 (void)Create(parent, id, value, pos, size, n,
258 choices, style, validator, name);
259 }
260
261 bool Create(wxWindow *parent,
262 wxWindowID id,
263 const wxString& value = wxEmptyString,
264 const wxPoint& pos = wxDefaultPosition,
265 const wxSize& size = wxDefaultSize,
266 long style = 0,
267 const wxValidator& validator = wxDefaultValidator,
268 const wxString& name = wxComboBoxNameStr);
269
270 wxOwnerDrawnComboBox(wxWindow *parent,
271 wxWindowID id,
272 const wxString& value,
273 const wxPoint& pos,
274 const wxSize& size,
275 const wxArrayString& choices,
276 long style,
277 const wxValidator& validator = wxDefaultValidator,
278 const wxString& name = wxComboBoxNameStr);
279
280 bool Create(wxWindow *parent,
281 wxWindowID id,
282 const wxString& value,
283 const wxPoint& pos,
284 const wxSize& size,
285 int n,
286 const wxString choices[],
287 long style = 0,
288 const wxValidator& validator = wxDefaultValidator,
289 const wxString& name = wxComboBoxNameStr);
290
291 bool Create(wxWindow *parent,
292 wxWindowID id,
293 const wxString& value,
294 const wxPoint& pos,
295 const wxSize& size,
296 const wxArrayString& choices,
297 long style = 0,
298 const wxValidator& validator = wxDefaultValidator,
299 const wxString& name = wxComboBoxNameStr);
300
301 virtual ~wxOwnerDrawnComboBox();
302
303 // Prevent app from using wxComboPopup
304 void SetPopupControl(wxVListBoxComboPopup* popup)
305 {
306 DoSetPopupControl(popup);
307 }
308
309 // wxControlWithItems methods
310 virtual unsigned int GetCount() const;
311 virtual wxString GetString(unsigned int n) const;
312 virtual void SetString(unsigned int n, const wxString& s);
313 virtual int FindString(const wxString& s, bool bCase = false) const;
314 virtual void Select(int n);
315 virtual int GetSelection() const;
316 virtual void SetSelection(int n) { Select(n); }
317
318
319 // Prevent a method from being hidden
320 virtual void SetSelection(long from, long to)
321 {
322 wxComboCtrl::SetSelection(from,to);
323 }
324
325 // Return the widest item width (recalculating it if necessary)
326 virtual int GetWidestItemWidth() { EnsurePopupControl(); return GetVListBoxComboPopup()->GetWidestItemWidth(); }
327
328 // Return the index of the widest item (recalculating it if necessary)
329 virtual int GetWidestItem() { EnsurePopupControl(); return GetVListBoxComboPopup()->GetWidestItem(); }
330
331 virtual bool IsSorted() const { return HasFlag(wxCB_SORT); }
332
333 wxCONTROL_ITEMCONTAINER_CLIENTDATAOBJECT_RECAST
334
335 protected:
336 virtual void DoClear();
337 virtual void DoDeleteOneItem(unsigned int n);
338
339 // Callback for drawing. Font, background and text colour have been
340 // prepared according to selection, focus and such.
341 // item: item index to be drawn, may be wxNOT_FOUND when painting combo control itself
342 // and there is no valid selection
343 // flags: wxODCB_PAINTING_CONTROL is set if painting to combo control instead of list
344 virtual void OnDrawItem( wxDC& dc, const wxRect& rect, int item, int flags ) const;
345
346 // Callback for item height, or -1 for default
347 virtual wxCoord OnMeasureItem( size_t item ) const;
348
349 // Callback for item width, or -1 for default/undetermined
350 virtual wxCoord OnMeasureItemWidth( size_t item ) const;
351
352 // Callback for background drawing. Flags are same as with
353 // OnDrawItem.
354 virtual void OnDrawBackground( wxDC& dc, const wxRect& rect, int item, int flags ) const;
355
356 // NULL popup can be used to indicate default interface
357 virtual void DoSetPopupControl(wxComboPopup* popup);
358
359 // clears all allocated client datas
360 void ClearClientDatas();
361
362 wxVListBoxComboPopup* GetVListBoxComboPopup() const
363 {
364 return (wxVListBoxComboPopup*) m_popupInterface;
365 }
366
367 virtual int DoInsertItems(const wxArrayStringsAdapter& items,
368 unsigned int pos,
369 void **clientData, wxClientDataType type);
370 virtual void DoSetItemClientData(unsigned int n, void* clientData);
371 virtual void* DoGetItemClientData(unsigned int n) const;
372
373 // temporary storage for the initial choices
374 //const wxString* m_baseChoices;
375 //int m_baseChoicesCount;
376 wxArrayString m_initChs;
377
378 private:
379 void Init();
380
381 DECLARE_EVENT_TABLE()
382
383 DECLARE_DYNAMIC_CLASS(wxOwnerDrawnComboBox)
384 };
385
386
387 #endif // wxUSE_ODCOMBOBOX
388
389 #endif
390 // _WX_ODCOMBO_H_