]> git.saurik.com Git - wxWidgets.git/blob - include/wx/odcombo.h
d5baadcc368bc403deaff8dba0a10f372fb2f9c8
[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 // 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 OnGetLineHeight(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 // NOTE: If wxVListBoxComboPopup is used with wxComboCtrl class not derived from
144 // wxOwnerDrawnComboBox, this method must be overridden.
145 virtual void OnDrawItem( wxDC& dc, const wxRect& rect, int item, int flags ) const;
146
147 // This is same as in wxVListBox
148 virtual wxCoord OnMeasureItem( size_t item ) const;
149
150 // Return item width, or -1 for calculating from text extent (default)
151 virtual wxCoord OnMeasureItemWidth( size_t item ) const;
152
153 // Draw item and combo control background. Flags are same as with OnDrawItem.
154 // NB: Can't use name OnDrawBackground because of virtual function hiding warnings.
155 virtual void OnDrawBg(wxDC& dc, const wxRect& rect, int item, int flags) const;
156
157 // Additional wxVListBox implementation (no need to override in derived classes)
158 virtual void OnDrawItem(wxDC& dc, const wxRect& rect, size_t n) const;
159 void OnDrawBackground(wxDC& dc, const wxRect& rect, size_t n) const;
160
161 // filter mouse move events happening outside the list box
162 // move selection with cursor
163 void OnMouseMove(wxMouseEvent& event);
164 void OnMouseWheel(wxMouseEvent& event);
165 void OnKey(wxKeyEvent& event);
166 void OnLeftClick(wxMouseEvent& event);
167
168 // Return the widest item width (recalculating it if necessary)
169 int GetWidestItemWidth() { CalcWidths(); return m_widestWidth; }
170
171 // Return the index of the widest item (recalculating it if necessary)
172 int GetWidestItem() { CalcWidths(); return m_widestItem; }
173
174 // Stop partial completion (when some other event occurs)
175 void StopPartialCompletion();
176
177 wxArrayString m_strings;
178 wxArrayPtrVoid m_clientDatas;
179
180 wxFont m_useFont;
181
182 //wxString m_stringValue; // displayed text (may be different than m_strings[m_value])
183 int m_value; // selection
184 int m_itemHover; // on which item the cursor is
185 int m_itemHeight; // default item height (calculate from font size
186 // and used in the absence of callback)
187 wxClientDataType m_clientDataItemsType;
188
189 private:
190
191 // Cached item widths (in pixels).
192 wxArrayInt m_widths;
193
194 // Width of currently widest item.
195 int m_widestWidth;
196
197 // Index of currently widest item.
198 int m_widestItem;
199
200 // Measure some items in next GetAdjustedSize?
201 bool m_widthsDirty;
202
203 // Find widest item in next GetAdjustedSize?
204 bool m_findWidest;
205
206 // has the mouse been released on this control?
207 bool m_clicked;
208
209 // Recalculate widths if they are dirty
210 void CalcWidths();
211
212 // Partial completion string
213 wxString m_partialCompletionString;
214
215 #if wxUSE_TIMER
216 // Partial completion timer
217 wxTimer m_partialCompletionTimer;
218 #endif // wxUSE_TIMER
219
220 DECLARE_EVENT_TABLE()
221 };
222
223
224 // ----------------------------------------------------------------------------
225 // wxOwnerDrawnComboBox: a generic wxComboBox that allows custom paint items
226 // in addition to many other types of customization already allowed by
227 // the wxComboCtrl.
228 // ----------------------------------------------------------------------------
229
230 class WXDLLIMPEXP_ADV wxOwnerDrawnComboBox : public wxComboCtrl,
231 public wxItemContainer
232 {
233 //friend class wxComboPopupWindow;
234 friend class wxVListBoxComboPopup;
235 public:
236
237 // ctors and such
238 wxOwnerDrawnComboBox() : wxComboCtrl() { Init(); }
239
240 wxOwnerDrawnComboBox(wxWindow *parent,
241 wxWindowID id,
242 const wxString& value,
243 const wxPoint& pos,
244 const wxSize& size,
245 int n,
246 const wxString choices[],
247 long style = 0,
248 const wxValidator& validator = wxDefaultValidator,
249 const wxString& name = wxComboBoxNameStr)
250 : wxComboCtrl()
251 {
252 Init();
253
254 (void)Create(parent, id, value, pos, size, n,
255 choices, style, validator, name);
256 }
257
258 bool Create(wxWindow *parent,
259 wxWindowID id,
260 const wxString& value = wxEmptyString,
261 const wxPoint& pos = wxDefaultPosition,
262 const wxSize& size = wxDefaultSize,
263 long style = 0,
264 const wxValidator& validator = wxDefaultValidator,
265 const wxString& name = wxComboBoxNameStr);
266
267 wxOwnerDrawnComboBox(wxWindow *parent,
268 wxWindowID id,
269 const wxString& value,
270 const wxPoint& pos,
271 const wxSize& size,
272 const wxArrayString& choices,
273 long style,
274 const wxValidator& validator = wxDefaultValidator,
275 const wxString& name = wxComboBoxNameStr);
276
277 bool Create(wxWindow *parent,
278 wxWindowID id,
279 const wxString& value,
280 const wxPoint& pos,
281 const wxSize& size,
282 int n,
283 const wxString choices[],
284 long style = 0,
285 const wxValidator& validator = wxDefaultValidator,
286 const wxString& name = wxComboBoxNameStr);
287
288 bool Create(wxWindow *parent,
289 wxWindowID id,
290 const wxString& value,
291 const wxPoint& pos,
292 const wxSize& size,
293 const wxArrayString& choices,
294 long style = 0,
295 const wxValidator& validator = wxDefaultValidator,
296 const wxString& name = wxComboBoxNameStr);
297
298 virtual ~wxOwnerDrawnComboBox();
299
300 // Prevent app from using wxComboPopup
301 void SetPopupControl(wxVListBoxComboPopup* popup)
302 {
303 DoSetPopupControl(popup);
304 }
305
306 // wxControlWithItems methods
307 virtual void Clear();
308 virtual void Delete(unsigned int n);
309 virtual unsigned int GetCount() const;
310 virtual wxString GetString(unsigned int n) const;
311 virtual void SetString(unsigned int n, const wxString& s);
312 virtual int FindString(const wxString& s, bool bCase = false) const;
313 virtual void Select(int n);
314 virtual int GetSelection() const;
315 virtual void SetSelection(int n) { Select(n); }
316
317
318 // Prevent a method from being hidden
319 virtual void SetSelection(long from, long to)
320 {
321 wxComboCtrl::SetSelection(from,to);
322 }
323
324 // Return the widest item width (recalculating it if necessary)
325 virtual int GetWidestItemWidth() { EnsurePopupControl(); return GetVListBoxComboPopup()->GetWidestItemWidth(); }
326
327 // Return the index of the widest item (recalculating it if necessary)
328 virtual int GetWidestItem() { EnsurePopupControl(); return GetVListBoxComboPopup()->GetWidestItem(); }
329
330 wxCONTROL_ITEMCONTAINER_CLIENTDATAOBJECT_RECAST
331
332 protected:
333
334 // Callback for drawing. Font, background and text colour have been
335 // prepared according to selection, focus and such.
336 // item: item index to be drawn, may be wxNOT_FOUND when painting combo control itself
337 // and there is no valid selection
338 // flags: wxODCB_PAINTING_CONTROL is set if painting to combo control instead of list
339 virtual void OnDrawItem( wxDC& dc, const wxRect& rect, int item, int flags ) const;
340
341 // Callback for item height, or -1 for default
342 virtual wxCoord OnMeasureItem( size_t item ) const;
343
344 // Callback for item width, or -1 for default/undetermined
345 virtual wxCoord OnMeasureItemWidth( size_t item ) const;
346
347 // Callback for background drawing. Flags are same as with
348 // OnDrawItem.
349 virtual void OnDrawBackground( wxDC& dc, const wxRect& rect, int item, int flags ) const;
350
351 // NULL popup can be used to indicate default interface
352 virtual void DoSetPopupControl(wxComboPopup* popup);
353
354 // clears all allocated client datas
355 void ClearClientDatas();
356
357 wxVListBoxComboPopup* GetVListBoxComboPopup() const
358 {
359 return (wxVListBoxComboPopup*) m_popupInterface;
360 }
361
362 virtual int DoAppend(const wxString& item);
363 virtual int DoInsert(const wxString& item, unsigned int pos);
364 virtual void DoSetItemClientData(unsigned int n, void* clientData);
365 virtual void* DoGetItemClientData(unsigned int n) const;
366 virtual void DoSetItemClientObject(unsigned int n, wxClientData* clientData);
367 virtual wxClientData* DoGetItemClientObject(unsigned int n) const;
368
369 // temporary storage for the initial choices
370 //const wxString* m_baseChoices;
371 //int m_baseChoicesCount;
372 wxArrayString m_initChs;
373
374 private:
375 void Init();
376
377 DECLARE_EVENT_TABLE()
378
379 DECLARE_DYNAMIC_CLASS(wxOwnerDrawnComboBox)
380 };
381
382
383 #endif // wxUSE_ODCOMBOBOX
384
385 #endif
386 // _WX_ODCOMBO_H_