]> git.saurik.com Git - wxWidgets.git/blame - include/wx/odcombo.h
using Run of base class
[wxWidgets.git] / include / wx / odcombo.h
CommitLineData
a340b80d
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: wx/odcombo.h
3// Purpose: wxOwnerDrawnComboBox and wxVListBoxPopup
4// Author: Jaakko Salli
5// Modified by:
6// Created: Apr-30-2006
a340b80d
VZ
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
a57d600f 16#if wxUSE_ODCOMBOBOX
a340b80d
VZ
17
18#include "wx/combo.h"
19#include "wx/ctrlsub.h"
20#include "wx/vlbox.h"
f0fa8b47 21#include "wx/timer.h"
a340b80d
VZ
22
23
24//
25// New window styles for wxOwnerDrawnComboBox
26//
27enum
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//
40b26d75 40// Callback flags (see wxOwnerDrawnComboBox::OnDrawItem)
a340b80d 41//
3fedffdc 42enum wxOwnerDrawnComboBoxPaintingFlags
a340b80d
VZ
43{
44 // when set, we are painting the selected item in control,
45 // not in the popup
ce22ac45
RR
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
a340b80d
VZ
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
a57d600f 62// from native wxComboCtrl. If you need to use this popup with
a340b80d
VZ
63// wxGenericComboControl, then remember that vast majority of item manipulation
64// functionality is implemented in the wxVListBoxComboPopup class itself.
65//
66// ----------------------------------------------------------------------------
67
68
c98e80d9
VZ
69class WXDLLIMPEXP_ADV wxVListBoxComboPopup : public wxVListBox,
70 public wxComboPopup
a340b80d
VZ
71{
72 friend class wxOwnerDrawnComboBox;
73public:
74
6d0ce565
VZ
75 // init and dtor
76 wxVListBoxComboPopup() : wxVListBox(), wxComboPopup() { }
a340b80d
VZ
77 virtual ~wxVListBoxComboPopup();
78
79 // required virtuals
6d0ce565 80 virtual void Init();
a340b80d 81 virtual bool Create(wxWindow* parent);
c0c05e79 82 virtual void SetFocus();
a340b80d
VZ
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 );
c765e6fb 92 virtual void OnComboCharEvent( wxKeyEvent& event );
a340b80d
VZ
93 virtual void OnComboDoubleClick();
94 virtual bool LazyCreate();
238b33ab 95 virtual bool FindItem(const wxString& item, wxString* trueItem);
a340b80d
VZ
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;
9e6aca68 108 int FindString(const wxString& s, bool bCase = false) const;
6d0ce565 109 int GetSelection() const;
a340b80d 110
6d0ce565
VZ
111 //void Populate( int n, const wxString choices[] );
112 void Populate( const wxArrayString& choices );
a340b80d
VZ
113 void ClearClientDatas();
114
115 // helpers
116 int GetItemAtPosition( const wxPoint& pos ) { return HitTest(pos); }
117 wxCoord GetTotalHeight() const { return EstimateTotalHeight(); }
e02c72fa 118 wxCoord GetLineHeight(int line) const { return OnGetRowHeight(line); }
a340b80d
VZ
119
120protected:
121
c765e6fb
VS
122 // Called by OnComboDoubleClick and OnCombo{Key,Char}Event
123 bool HandleKey( int keycode, bool saturate, wxChar keychar = 0 );
a340b80d
VZ
124
125 // sends combobox select event from the parent combo control
6d0ce565 126 void SendComboBoxEvent( int selection );
7ca4ac63 127
6d0ce565
VZ
128 // gets value, sends event and dismisses
129 void DismissWithEvent();
a340b80d 130
e5d63342
WS
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 }
a340b80d 137
40b26d75
WS
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
3fedffdc
FM
145 //
146 // NOTE: If wxVListBoxComboPopup is used with a wxComboCtrl class not derived from
40b26d75 147 // wxOwnerDrawnComboBox, this method must be overridden.
3fedffdc 148 virtual void OnDrawItem( wxDC& dc, const wxRect& rect, int item, int flags) const;
a340b80d 149
40b26d75 150 // This is same as in wxVListBox
dcc8cf5a
PC
151 virtual wxCoord OnMeasureItem( size_t item ) const;
152
40b26d75
WS
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
a340b80d
VZ
164 // filter mouse move events happening outside the list box
165 // move selection with cursor
166 void OnMouseMove(wxMouseEvent& event);
a340b80d 167 void OnKey(wxKeyEvent& event);
c765e6fb 168 void OnChar(wxKeyEvent& event);
a340b80d
VZ
169 void OnLeftClick(wxMouseEvent& event);
170
d9e0958c
AB
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
dc8a1aa5
AB
177 // Stop partial completion (when some other event occurs)
178 void StopPartialCompletion();
179
a340b80d
VZ
180 wxArrayString m_strings;
181 wxArrayPtrVoid m_clientDatas;
a340b80d 182
6d0ce565 183 wxFont m_useFont;
a340b80d 184
6d0ce565 185 //wxString m_stringValue; // displayed text (may be different than m_strings[m_value])
a340b80d
VZ
186 int m_value; // selection
187 int m_itemHover; // on which item the cursor is
a340b80d
VZ
188 int m_itemHeight; // default item height (calculate from font size
189 // and used in the absence of callback)
190 wxClientDataType m_clientDataItemsType;
191
192private:
193
e5d63342
WS
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
a340b80d 209 // has the mouse been released on this control?
e5d63342 210 bool m_clicked;
a340b80d 211
d9e0958c
AB
212 // Recalculate widths if they are dirty
213 void CalcWidths();
214
dc8a1aa5
AB
215 // Partial completion string
216 wxString m_partialCompletionString;
217
fdb47e62
VZ
218 wxString m_stringValue;
219
f0fa8b47 220#if wxUSE_TIMER
dc8a1aa5
AB
221 // Partial completion timer
222 wxTimer m_partialCompletionTimer;
f0fa8b47 223#endif // wxUSE_TIMER
dc8a1aa5 224
a340b80d
VZ
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
a57d600f 232// the wxComboCtrl.
a340b80d
VZ
233// ----------------------------------------------------------------------------
234
bf8f1022
VZ
235class WXDLLIMPEXP_ADV wxOwnerDrawnComboBox :
236 public wxWindowWithItems<wxComboCtrl, wxItemContainer>
a340b80d 237{
40b26d75
WS
238 //friend class wxComboPopupWindow;
239 friend class wxVListBoxComboPopup;
a340b80d
VZ
240public:
241
242 // ctors and such
bf8f1022 243 wxOwnerDrawnComboBox() { Init(); }
a340b80d
VZ
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)
a340b80d
VZ
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,
1c4743aa 264 const wxString& value = wxEmptyString,
a340b80d
VZ
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,
94daedc8
JS
273 const wxString& value = wxEmptyString,
274 const wxPoint& pos = wxDefaultPosition,
275 const wxSize& size = wxDefaultSize,
276 const wxArrayString& choices = wxArrayString(),
277 long style = 0,
a340b80d
VZ
278 const wxValidator& validator = wxDefaultValidator,
279 const wxString& name = wxComboBoxNameStr);
280
281 bool Create(wxWindow *parent,
282 wxWindowID id,
1c4743aa
WS
283 const wxString& value,
284 const wxPoint& pos,
285 const wxSize& size,
286 int n,
287 const wxString choices[],
a340b80d
VZ
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
db53c6ea
WS
304 // Prevent app from using wxComboPopup
305 void SetPopupControl(wxVListBoxComboPopup* popup)
306 {
307 DoSetPopupControl(popup);
308 }
6d0ce565 309
a340b80d 310 // wxControlWithItems methods
a340b80d
VZ
311 virtual unsigned int GetCount() const;
312 virtual wxString GetString(unsigned int n) const;
313 virtual void SetString(unsigned int n, const wxString& s);
9e6aca68 314 virtual int FindString(const wxString& s, bool bCase = false) const;
a340b80d
VZ
315 virtual void Select(int n);
316 virtual int GetSelection() const;
9ff4b6cc 317
eae20c90 318 // Override these just to maintain consistency with virtual methods
9ff4b6cc 319 // between classes.
eae20c90 320 virtual void Clear();
9ff4b6cc
JS
321 virtual void GetSelection(long *from, long *to) const;
322
c0f48280 323 virtual void SetSelection(int n) { Select(n); }
a340b80d 324
7ca4ac63
WS
325
326 // Prevent a method from being hidden
327 virtual void SetSelection(long from, long to)
328 {
329 wxComboCtrl::SetSelection(from,to);
330 }
331
d9e0958c
AB
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
a236aa20
VZ
338 virtual bool IsSorted() const { return HasFlag(wxCB_SORT); }
339
a340b80d 340protected:
470f357f
PC
341 virtual void DoClear();
342 virtual void DoDeleteOneItem(unsigned int n);
a340b80d 343
40b26d75
WS
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
e2ac737f
VZ
357 // override base implementation so we can return the size for the
358 // largest item
359 virtual wxSize DoGetBestSize() const;
360
40b26d75
WS
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
db53c6ea
WS
365 // NULL popup can be used to indicate default interface
366 virtual void DoSetPopupControl(wxComboPopup* popup);
367
a340b80d
VZ
368 // clears all allocated client datas
369 void ClearClientDatas();
370
57693473
VZ
371 wxVListBoxComboPopup* GetVListBoxComboPopup() const
372 {
373 return (wxVListBoxComboPopup*) m_popupInterface;
374 }
375
a236aa20
VZ
376 virtual int DoInsertItems(const wxArrayStringsAdapter& items,
377 unsigned int pos,
378 void **clientData, wxClientDataType type);
a340b80d
VZ
379 virtual void DoSetItemClientData(unsigned int n, void* clientData);
380 virtual void* DoGetItemClientData(unsigned int n) const;
a340b80d 381
6d0ce565
VZ
382 // temporary storage for the initial choices
383 //const wxString* m_baseChoices;
384 //int m_baseChoicesCount;
385 wxArrayString m_initChs;
386
a340b80d
VZ
387private:
388 void Init();
389
390 DECLARE_EVENT_TABLE()
391
392 DECLARE_DYNAMIC_CLASS(wxOwnerDrawnComboBox)
393};
394
395
a57d600f 396#endif // wxUSE_ODCOMBOBOX
7ca4ac63 397
a340b80d
VZ
398#endif
399 // _WX_ODCOMBO_H_