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