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