]> git.saurik.com Git - wxWidgets.git/blame - include/wx/odcombo.h
don't pass unnecessary arguments to FindOrCreateBrush/Pen()
[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
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
a57d600f 17#if wxUSE_ODCOMBOBOX
a340b80d
VZ
18
19#include "wx/combo.h"
20#include "wx/ctrlsub.h"
21#include "wx/vlbox.h"
f0fa8b47 22#include "wx/timer.h"
a340b80d
VZ
23
24
25//
26// New window styles for wxOwnerDrawnComboBox
27//
28enum
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//
40b26d75 41// Callback flags (see wxOwnerDrawnComboBox::OnDrawItem)
a340b80d
VZ
42//
43enum
44{
45 // when set, we are painting the selected item in control,
46 // not in the popup
ce22ac45
RR
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
a340b80d
VZ
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
a57d600f 63// from native wxComboCtrl. If you need to use this popup with
a340b80d
VZ
64// wxGenericComboControl, then remember that vast majority of item manipulation
65// functionality is implemented in the wxVListBoxComboPopup class itself.
66//
67// ----------------------------------------------------------------------------
68
69
c98e80d9
VZ
70class WXDLLIMPEXP_ADV wxVListBoxComboPopup : public wxVListBox,
71 public wxComboPopup
a340b80d
VZ
72{
73 friend class wxOwnerDrawnComboBox;
74public:
75
6d0ce565
VZ
76 // init and dtor
77 wxVListBoxComboPopup() : wxVListBox(), wxComboPopup() { }
a340b80d
VZ
78 virtual ~wxVListBoxComboPopup();
79
80 // required virtuals
6d0ce565 81 virtual void Init();
a340b80d
VZ
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;
9e6aca68 106 int FindString(const wxString& s, bool bCase = false) const;
6d0ce565 107 int GetSelection() const;
a340b80d 108
6d0ce565
VZ
109 //void Populate( int n, const wxString choices[] );
110 void Populate( const wxArrayString& choices );
a340b80d
VZ
111 void ClearClientDatas();
112
113 // helpers
114 int GetItemAtPosition( const wxPoint& pos ) { return HitTest(pos); }
115 wxCoord GetTotalHeight() const { return EstimateTotalHeight(); }
e02c72fa 116 wxCoord GetLineHeight(int line) const { return OnGetRowHeight(line); }
a340b80d
VZ
117
118protected:
119
120 // Called by OnComboDoubleClick and OnComboKeyEvent
dc8a1aa5 121 bool HandleKey( int keycode, bool saturate, wxChar unicode = 0 );
a340b80d
VZ
122
123 // sends combobox select event from the parent combo control
6d0ce565 124 void SendComboBoxEvent( int selection );
7ca4ac63 125
6d0ce565
VZ
126 // gets value, sends event and dismisses
127 void DismissWithEvent();
a340b80d 128
e5d63342
WS
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 }
a340b80d 135
40b26d75
WS
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;
a340b80d 146
40b26d75 147 // This is same as in wxVListBox
dcc8cf5a
PC
148 virtual wxCoord OnMeasureItem( size_t item ) const;
149
40b26d75
WS
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
a340b80d
VZ
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
d9e0958c
AB
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
dc8a1aa5
AB
174 // Stop partial completion (when some other event occurs)
175 void StopPartialCompletion();
176
a340b80d
VZ
177 wxArrayString m_strings;
178 wxArrayPtrVoid m_clientDatas;
a340b80d 179
6d0ce565 180 wxFont m_useFont;
a340b80d 181
6d0ce565 182 //wxString m_stringValue; // displayed text (may be different than m_strings[m_value])
a340b80d
VZ
183 int m_value; // selection
184 int m_itemHover; // on which item the cursor is
a340b80d
VZ
185 int m_itemHeight; // default item height (calculate from font size
186 // and used in the absence of callback)
187 wxClientDataType m_clientDataItemsType;
188
189private:
190
e5d63342
WS
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
a340b80d 206 // has the mouse been released on this control?
e5d63342 207 bool m_clicked;
a340b80d 208
d9e0958c
AB
209 // Recalculate widths if they are dirty
210 void CalcWidths();
211
dc8a1aa5
AB
212 // Partial completion string
213 wxString m_partialCompletionString;
214
fdb47e62
VZ
215 wxString m_stringValue;
216
f0fa8b47 217#if wxUSE_TIMER
dc8a1aa5
AB
218 // Partial completion timer
219 wxTimer m_partialCompletionTimer;
f0fa8b47 220#endif // wxUSE_TIMER
dc8a1aa5 221
a340b80d
VZ
222 DECLARE_EVENT_TABLE()
223};
224
225
226// ----------------------------------------------------------------------------
227// wxOwnerDrawnComboBox: a generic wxComboBox that allows custom paint items
228// in addition to many other types of customization already allowed by
a57d600f 229// the wxComboCtrl.
a340b80d
VZ
230// ----------------------------------------------------------------------------
231
a57d600f 232class WXDLLIMPEXP_ADV wxOwnerDrawnComboBox : public wxComboCtrl,
c98e80d9 233 public wxItemContainer
a340b80d 234{
40b26d75
WS
235 //friend class wxComboPopupWindow;
236 friend class wxVListBoxComboPopup;
a340b80d
VZ
237public:
238
239 // ctors and such
a57d600f 240 wxOwnerDrawnComboBox() : wxComboCtrl() { Init(); }
a340b80d
VZ
241
242 wxOwnerDrawnComboBox(wxWindow *parent,
243 wxWindowID id,
244 const wxString& value,
245 const wxPoint& pos,
246 const wxSize& size,
247 int n,
248 const wxString choices[],
249 long style = 0,
250 const wxValidator& validator = wxDefaultValidator,
251 const wxString& name = wxComboBoxNameStr)
a57d600f 252 : wxComboCtrl()
a340b80d
VZ
253 {
254 Init();
255
256 (void)Create(parent, id, value, pos, size, n,
257 choices, style, validator, name);
258 }
259
260 bool Create(wxWindow *parent,
261 wxWindowID id,
1c4743aa 262 const wxString& value = wxEmptyString,
a340b80d
VZ
263 const wxPoint& pos = wxDefaultPosition,
264 const wxSize& size = wxDefaultSize,
265 long style = 0,
266 const wxValidator& validator = wxDefaultValidator,
267 const wxString& name = wxComboBoxNameStr);
268
269 wxOwnerDrawnComboBox(wxWindow *parent,
270 wxWindowID id,
271 const wxString& value,
272 const wxPoint& pos,
273 const wxSize& size,
274 const wxArrayString& choices,
7ca4ac63 275 long style,
a340b80d
VZ
276 const wxValidator& validator = wxDefaultValidator,
277 const wxString& name = wxComboBoxNameStr);
278
279 bool Create(wxWindow *parent,
280 wxWindowID id,
1c4743aa
WS
281 const wxString& value,
282 const wxPoint& pos,
283 const wxSize& size,
284 int n,
285 const wxString choices[],
a340b80d
VZ
286 long style = 0,
287 const wxValidator& validator = wxDefaultValidator,
288 const wxString& name = wxComboBoxNameStr);
289
290 bool Create(wxWindow *parent,
291 wxWindowID id,
292 const wxString& value,
293 const wxPoint& pos,
294 const wxSize& size,
295 const wxArrayString& choices,
296 long style = 0,
297 const wxValidator& validator = wxDefaultValidator,
298 const wxString& name = wxComboBoxNameStr);
299
300 virtual ~wxOwnerDrawnComboBox();
301
db53c6ea
WS
302 // Prevent app from using wxComboPopup
303 void SetPopupControl(wxVListBoxComboPopup* popup)
304 {
305 DoSetPopupControl(popup);
306 }
6d0ce565 307
a340b80d 308 // wxControlWithItems methods
a236aa20
VZ
309 virtual void DoClear();
310 virtual void DoDeleteOneItem(unsigned int n);
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;
c0f48280 317 virtual void SetSelection(int n) { Select(n); }
a340b80d 318
7ca4ac63
WS
319
320 // Prevent a method from being hidden
321 virtual void SetSelection(long from, long to)
322 {
323 wxComboCtrl::SetSelection(from,to);
324 }
325
d9e0958c
AB
326 // Return the widest item width (recalculating it if necessary)
327 virtual int GetWidestItemWidth() { EnsurePopupControl(); return GetVListBoxComboPopup()->GetWidestItemWidth(); }
328
329 // Return the index of the widest item (recalculating it if necessary)
330 virtual int GetWidestItem() { EnsurePopupControl(); return GetVListBoxComboPopup()->GetWidestItem(); }
331
a236aa20
VZ
332 virtual bool IsSorted() const { return HasFlag(wxCB_SORT); }
333
a340b80d
VZ
334 wxCONTROL_ITEMCONTAINER_CLIENTDATAOBJECT_RECAST
335
336protected:
337
40b26d75
WS
338 // Callback for drawing. Font, background and text colour have been
339 // prepared according to selection, focus and such.
340 // item: item index to be drawn, may be wxNOT_FOUND when painting combo control itself
341 // and there is no valid selection
342 // flags: wxODCB_PAINTING_CONTROL is set if painting to combo control instead of list
343 virtual void OnDrawItem( wxDC& dc, const wxRect& rect, int item, int flags ) const;
344
345 // Callback for item height, or -1 for default
346 virtual wxCoord OnMeasureItem( size_t item ) const;
347
348 // Callback for item width, or -1 for default/undetermined
349 virtual wxCoord OnMeasureItemWidth( size_t item ) const;
350
351 // Callback for background drawing. Flags are same as with
352 // OnDrawItem.
353 virtual void OnDrawBackground( wxDC& dc, const wxRect& rect, int item, int flags ) const;
354
db53c6ea
WS
355 // NULL popup can be used to indicate default interface
356 virtual void DoSetPopupControl(wxComboPopup* popup);
357
a340b80d
VZ
358 // clears all allocated client datas
359 void ClearClientDatas();
360
57693473
VZ
361 wxVListBoxComboPopup* GetVListBoxComboPopup() const
362 {
363 return (wxVListBoxComboPopup*) m_popupInterface;
364 }
365
a236aa20
VZ
366 virtual int DoInsertItems(const wxArrayStringsAdapter& items,
367 unsigned int pos,
368 void **clientData, wxClientDataType type);
a340b80d
VZ
369 virtual void DoSetItemClientData(unsigned int n, void* clientData);
370 virtual void* DoGetItemClientData(unsigned int n) const;
a340b80d 371
6d0ce565
VZ
372 // temporary storage for the initial choices
373 //const wxString* m_baseChoices;
374 //int m_baseChoicesCount;
375 wxArrayString m_initChs;
376
a340b80d
VZ
377private:
378 void Init();
379
380 DECLARE_EVENT_TABLE()
381
382 DECLARE_DYNAMIC_CLASS(wxOwnerDrawnComboBox)
383};
384
385
a57d600f 386#endif // wxUSE_ODCOMBOBOX
7ca4ac63 387
a340b80d
VZ
388#endif
389 // _WX_ODCOMBO_H_