]> git.saurik.com Git - wxWidgets.git/blob - include/wx/odcombo.h
wxComboControl and wxOwnerDrawnComboBox (patch 1479938)
[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_OWNERDRAWNCOMBOBOX
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
41 //
42 enum
43 {
44 // when set, we are painting the selected item in control,
45 // not in the popup
46 wxCC_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 wxComboControl. 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 WXDLLEXPORT wxVListBoxComboPopup : public wxVListBox, public wxComboPopup
63 {
64 friend class wxOwnerDrawnComboBox;
65 public:
66
67 // ctor and dtor
68 wxVListBoxComboPopup(wxComboControlBase* combo);
69 virtual ~wxVListBoxComboPopup();
70
71 // required virtuals
72 virtual bool Create(wxWindow* parent);
73 virtual wxWindow *GetControl() { return this; }
74 virtual void SetStringValue( const wxString& value );
75 virtual wxString GetStringValue() const;
76
77 // more customization
78 virtual void OnPopup();
79 virtual wxSize GetAdjustedSize( int minWidth, int prefHeight, int maxHeight );
80 virtual void PaintComboControl( wxDC& dc, const wxRect& rect );
81 virtual void OnComboKeyEvent( wxKeyEvent& event );
82 virtual void OnComboDoubleClick();
83 virtual bool LazyCreate();
84
85 // Item management
86 void SetSelection( int item );
87 void Insert( const wxString& item, int pos );
88 int Append(const wxString& item);
89 void Clear();
90 void Delete( unsigned int item );
91 void SetItemClientData(unsigned int n, void* clientData, wxClientDataType clientDataItemsType);
92 void *GetItemClientData(unsigned int n) const;
93 void SetString( int item, const wxString& str );
94 wxString GetString( int item ) const;
95 unsigned int GetCount() const;
96 int FindString(const wxString& s) const;
97
98 void Populate( int n, const wxString choices[] );
99 void ClearClientDatas();
100
101 // helpers
102 int GetItemAtPosition( const wxPoint& pos ) { return HitTest(pos); }
103 wxCoord GetTotalHeight() const { return EstimateTotalHeight(); }
104 wxCoord GetLineHeight(int line) const { return OnGetLineHeight(line); }
105
106 protected:
107
108 // Called by OnComboDoubleClick and OnComboKeyEvent
109 bool HandleKey( int keycode, bool saturate );
110
111 // sends combobox select event from the parent combo control
112 void SendComboBoxEvent();
113
114 // Re-calculates width for given item
115 void CheckWidth( int pos );
116
117 // wxVListBox implementation
118 virtual void OnDrawItem(wxDC& dc, const wxRect& rect, size_t n) const;
119 virtual wxCoord OnMeasureItem(size_t n) const;
120 void OnDrawBackground(wxDC& dc, const wxRect& rect, size_t n) const;
121
122 // filter mouse move events happening outside the list box
123 // move selection with cursor
124 void OnMouseMove(wxMouseEvent& event);
125 void OnMouseWheel(wxMouseEvent& event);
126 void OnKey(wxKeyEvent& event);
127 void OnLeftClick(wxMouseEvent& event);
128
129 wxArrayString m_strings;
130 wxArrayPtrVoid m_clientDatas;
131 wxArrayInt m_widths; // cached line widths
132
133 wxFont m_font;
134
135 int m_value; // selection
136 int m_itemHover; // on which item the cursor is
137 int m_widestWidth; // width of widest item thus far
138 int m_avgCharWidth;
139 int m_baseImageWidth; // how much per item drawn in addition to text
140 int m_itemHeight; // default item height (calculate from font size
141 // and used in the absence of callback)
142 wxClientDataType m_clientDataItemsType;
143
144 private:
145
146 // has the mouse been released on this control?
147 bool m_clicked;
148
149 DECLARE_EVENT_TABLE()
150 };
151
152
153 // ----------------------------------------------------------------------------
154 // wxOwnerDrawnComboBox: a generic wxComboBox that allows custom paint items
155 // in addition to many other types of customization already allowed by
156 // the wxComboControl.
157 // ----------------------------------------------------------------------------
158
159 class WXDLLEXPORT wxOwnerDrawnComboBox : public wxComboControl, public wxItemContainer
160 {
161 friend class wxComboPopupWindow;
162 friend class wxComboControlBase;
163 public:
164
165 // ctors and such
166 wxOwnerDrawnComboBox() : wxComboControl() { Init(); }
167
168 wxOwnerDrawnComboBox(wxWindow *parent,
169 wxWindowID id,
170 const wxString& value,
171 const wxPoint& pos,
172 const wxSize& size,
173 int n,
174 const wxString choices[],
175 long style = 0,
176 const wxValidator& validator = wxDefaultValidator,
177 const wxString& name = wxComboBoxNameStr)
178 : wxComboControl()
179 {
180 Init();
181
182 (void)Create(parent, id, value, pos, size, n,
183 choices, style, validator, name);
184 }
185
186 bool Create(wxWindow *parent,
187 wxWindowID id,
188 const wxString& value = wxEmptyString,
189 const wxPoint& pos = wxDefaultPosition,
190 const wxSize& size = wxDefaultSize,
191 long style = 0,
192 const wxValidator& validator = wxDefaultValidator,
193 const wxString& name = wxComboBoxNameStr);
194
195 wxOwnerDrawnComboBox(wxWindow *parent,
196 wxWindowID id,
197 const wxString& value,
198 const wxPoint& pos,
199 const wxSize& size,
200 const wxArrayString& choices,
201 long style = 0,
202 const wxValidator& validator = wxDefaultValidator,
203 const wxString& name = wxComboBoxNameStr);
204
205 bool Create(wxWindow *parent,
206 wxWindowID id,
207 const wxString& value = wxEmptyString,
208 const wxPoint& pos = wxDefaultPosition,
209 const wxSize& size = wxDefaultSize,
210 int n = 0,
211 const wxString choices[] = (const wxString *) NULL,
212 long style = 0,
213 const wxValidator& validator = wxDefaultValidator,
214 const wxString& name = wxComboBoxNameStr);
215
216 bool Create(wxWindow *parent,
217 wxWindowID id,
218 const wxString& value,
219 const wxPoint& pos,
220 const wxSize& size,
221 const wxArrayString& choices,
222 long style = 0,
223 const wxValidator& validator = wxDefaultValidator,
224 const wxString& name = wxComboBoxNameStr);
225
226 virtual ~wxOwnerDrawnComboBox();
227
228 // wxControlWithItems methods
229 virtual void Clear();
230 virtual void Delete(unsigned int n);
231 virtual unsigned int GetCount() const;
232 virtual wxString GetString(unsigned int n) const;
233 virtual void SetString(unsigned int n, const wxString& s);
234 virtual int FindString(const wxString& s) const;
235 virtual void Select(int n);
236 virtual int GetSelection() const;
237 void SetSelection(int n) { Select(n); }
238
239 wxCONTROL_ITEMCONTAINER_CLIENTDATAOBJECT_RECAST
240
241 protected:
242
243 // clears all allocated client datas
244 void ClearClientDatas();
245
246 virtual int DoAppend(const wxString& item);
247 virtual int DoInsert(const wxString& item, unsigned int pos);
248 virtual void DoSetItemClientData(unsigned int n, void* clientData);
249 virtual void* DoGetItemClientData(unsigned int n) const;
250 virtual void DoSetItemClientObject(unsigned int n, wxClientData* clientData);
251 virtual wxClientData* DoGetItemClientObject(unsigned int n) const;
252
253 // overload m_popupInterface member so we can access specific popup interface easier
254 wxVListBoxComboPopup* m_popupInterface;
255
256 private:
257 void Init();
258
259 DECLARE_EVENT_TABLE()
260
261 DECLARE_DYNAMIC_CLASS(wxOwnerDrawnComboBox)
262 };
263
264
265 #endif // wxUSE_OWNERDRAWNCOMBOBOX
266 #endif
267 // _WX_ODCOMBO_H_