]> git.saurik.com Git - wxWidgets.git/blob - include/wx/odcombo.h
Remove duplication in wxImageList defines and always default to using the native...
[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
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
51 // ----------------------------------------------------------------------------
52 // wxVListBoxComboPopup is a wxVListBox customized to act as a popup control.
53 //
54 // Notes:
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.
59 //
60 // ----------------------------------------------------------------------------
61
62
63 class WXDLLIMPEXP_ADV wxVListBoxComboPopup : public wxVListBox,
64 public wxComboPopup
65 {
66 friend class wxOwnerDrawnComboBox;
67 public:
68
69 // init and dtor
70 wxVListBoxComboPopup() : wxVListBox(), wxComboPopup() { }
71 virtual ~wxVListBoxComboPopup();
72
73 // required virtuals
74 virtual void Init();
75 virtual bool Create(wxWindow* parent);
76 virtual wxWindow *GetControl() { return this; }
77 virtual void SetStringValue( const wxString& value );
78 virtual wxString GetStringValue() const;
79
80 // more customization
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();
87
88 // Item management
89 void SetSelection( int item );
90 void Insert( const wxString& item, int pos );
91 int Append(const wxString& item);
92 void Clear();
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;
101
102 //void Populate( int n, const wxString choices[] );
103 void Populate( const wxArrayString& choices );
104 void ClearClientDatas();
105
106 // helpers
107 int GetItemAtPosition( const wxPoint& pos ) { return HitTest(pos); }
108 wxCoord GetTotalHeight() const { return EstimateTotalHeight(); }
109 wxCoord GetLineHeight(int line) const { return OnGetLineHeight(line); }
110
111 protected:
112
113 // Called by OnComboDoubleClick and OnComboKeyEvent
114 bool HandleKey( int keycode, bool saturate, wxChar unicode = 0 );
115
116 // sends combobox select event from the parent combo control
117 void SendComboBoxEvent( int selection );
118
119 // gets value, sends event and dismisses
120 void DismissWithEvent();
121
122 // OnMeasureItemWidth will be called on next GetAdjustedSize.
123 void ItemWidthChanged(unsigned int item)
124 {
125 m_widths[item] = -1;
126 m_widthsDirty = true;
127 }
128
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.
132 //
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;
139
140 // This is same as in wxVListBox
141 virtual wxCoord OnMeasureItem( size_t item ) const;
142
143 // Return item width, or -1 for calculating from text extent (default)
144 virtual wxCoord OnMeasureItemWidth( size_t item ) const;
145
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;
149
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;
153
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);
160
161 // Return the widest item width (recalculating it if necessary)
162 int GetWidestItemWidth() { CalcWidths(); return m_widestWidth; }
163
164 // Return the index of the widest item (recalculating it if necessary)
165 int GetWidestItem() { CalcWidths(); return m_widestItem; }
166
167 // Stop partial completion (when some other event occurs)
168 void StopPartialCompletion();
169
170 wxArrayString m_strings;
171 wxArrayPtrVoid m_clientDatas;
172
173 wxFont m_useFont;
174
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;
181
182 private:
183
184 // Cached item widths (in pixels).
185 wxArrayInt m_widths;
186
187 // Width of currently widest item.
188 int m_widestWidth;
189
190 // Index of currently widest item.
191 int m_widestItem;
192
193 // Measure some items in next GetAdjustedSize?
194 bool m_widthsDirty;
195
196 // Find widest item in next GetAdjustedSize?
197 bool m_findWidest;
198
199 // has the mouse been released on this control?
200 bool m_clicked;
201
202 // Recalculate widths if they are dirty
203 void CalcWidths();
204
205 // Partial completion string
206 wxString m_partialCompletionString;
207
208 #if wxUSE_TIMER
209 // Partial completion timer
210 wxTimer m_partialCompletionTimer;
211 #endif // wxUSE_TIMER
212
213 DECLARE_EVENT_TABLE()
214 };
215
216
217 // ----------------------------------------------------------------------------
218 // wxOwnerDrawnComboBox: a generic wxComboBox that allows custom paint items
219 // in addition to many other types of customization already allowed by
220 // the wxComboCtrl.
221 // ----------------------------------------------------------------------------
222
223 class WXDLLIMPEXP_ADV wxOwnerDrawnComboBox : public wxComboCtrl,
224 public wxItemContainer
225 {
226 //friend class wxComboPopupWindow;
227 friend class wxVListBoxComboPopup;
228 public:
229
230 // ctors and such
231 wxOwnerDrawnComboBox() : wxComboCtrl() { Init(); }
232
233 wxOwnerDrawnComboBox(wxWindow *parent,
234 wxWindowID id,
235 const wxString& value,
236 const wxPoint& pos,
237 const wxSize& size,
238 int n,
239 const wxString choices[],
240 long style = 0,
241 const wxValidator& validator = wxDefaultValidator,
242 const wxString& name = wxComboBoxNameStr)
243 : wxComboCtrl()
244 {
245 Init();
246
247 (void)Create(parent, id, value, pos, size, n,
248 choices, style, validator, name);
249 }
250
251 bool Create(wxWindow *parent,
252 wxWindowID id,
253 const wxString& value = wxEmptyString,
254 const wxPoint& pos = wxDefaultPosition,
255 const wxSize& size = wxDefaultSize,
256 long style = 0,
257 const wxValidator& validator = wxDefaultValidator,
258 const wxString& name = wxComboBoxNameStr);
259
260 wxOwnerDrawnComboBox(wxWindow *parent,
261 wxWindowID id,
262 const wxString& value,
263 const wxPoint& pos,
264 const wxSize& size,
265 const wxArrayString& choices,
266 long style,
267 const wxValidator& validator = wxDefaultValidator,
268 const wxString& name = wxComboBoxNameStr);
269
270 bool Create(wxWindow *parent,
271 wxWindowID id,
272 const wxString& value,
273 const wxPoint& pos,
274 const wxSize& size,
275 int n,
276 const wxString choices[],
277 long style = 0,
278 const wxValidator& validator = wxDefaultValidator,
279 const wxString& name = wxComboBoxNameStr);
280
281 bool Create(wxWindow *parent,
282 wxWindowID id,
283 const wxString& value,
284 const wxPoint& pos,
285 const wxSize& size,
286 const wxArrayString& choices,
287 long style = 0,
288 const wxValidator& validator = wxDefaultValidator,
289 const wxString& name = wxComboBoxNameStr);
290
291 virtual ~wxOwnerDrawnComboBox();
292
293 // Prevent app from using wxComboPopup
294 void SetPopupControl(wxVListBoxComboPopup* popup)
295 {
296 DoSetPopupControl(popup);
297 }
298
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); }
309
310
311 // Prevent a method from being hidden
312 virtual void SetSelection(long from, long to)
313 {
314 wxComboCtrl::SetSelection(from,to);
315 }
316
317 // Return the widest item width (recalculating it if necessary)
318 virtual int GetWidestItemWidth() { EnsurePopupControl(); return GetVListBoxComboPopup()->GetWidestItemWidth(); }
319
320 // Return the index of the widest item (recalculating it if necessary)
321 virtual int GetWidestItem() { EnsurePopupControl(); return GetVListBoxComboPopup()->GetWidestItem(); }
322
323 wxCONTROL_ITEMCONTAINER_CLIENTDATAOBJECT_RECAST
324
325 protected:
326
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;
333
334 // Callback for item height, or -1 for default
335 virtual wxCoord OnMeasureItem( size_t item ) const;
336
337 // Callback for item width, or -1 for default/undetermined
338 virtual wxCoord OnMeasureItemWidth( size_t item ) const;
339
340 // Callback for background drawing. Flags are same as with
341 // OnDrawItem.
342 virtual void OnDrawBackground( wxDC& dc, const wxRect& rect, int item, int flags ) const;
343
344 // NULL popup can be used to indicate default interface
345 virtual void DoSetPopupControl(wxComboPopup* popup);
346
347 // clears all allocated client datas
348 void ClearClientDatas();
349
350 wxVListBoxComboPopup* GetVListBoxComboPopup() const
351 {
352 return (wxVListBoxComboPopup*) m_popupInterface;
353 }
354
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;
361
362 // temporary storage for the initial choices
363 //const wxString* m_baseChoices;
364 //int m_baseChoicesCount;
365 wxArrayString m_initChs;
366
367 private:
368 void Init();
369
370 DECLARE_EVENT_TABLE()
371
372 DECLARE_DYNAMIC_CLASS(wxOwnerDrawnComboBox)
373 };
374
375
376 #endif // wxUSE_ODCOMBOBOX
377
378 #endif
379 // _WX_ODCOMBO_H_