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