]>
Commit | Line | Data |
---|---|---|
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" | |
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 | // | |
40b26d75 | 40 | // Callback flags (see wxOwnerDrawnComboBox::OnDrawItem) |
a340b80d VZ |
41 | // |
42 | enum | |
43 | { | |
44 | // when set, we are painting the selected item in control, | |
45 | // not in the popup | |
40b26d75 | 46 | wxODCB_PAINTING_CONTROL = 0x0001 |
a340b80d VZ |
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 | |
a57d600f | 55 | // from native wxComboCtrl. If you need to use this popup with |
a340b80d VZ |
56 | // wxGenericComboControl, then remember that vast majority of item manipulation |
57 | // functionality is implemented in the wxVListBoxComboPopup class itself. | |
58 | // | |
59 | // ---------------------------------------------------------------------------- | |
60 | ||
61 | ||
c98e80d9 VZ |
62 | class WXDLLIMPEXP_ADV wxVListBoxComboPopup : public wxVListBox, |
63 | public wxComboPopup | |
a340b80d VZ |
64 | { |
65 | friend class wxOwnerDrawnComboBox; | |
66 | public: | |
67 | ||
6d0ce565 VZ |
68 | // init and dtor |
69 | wxVListBoxComboPopup() : wxVListBox(), wxComboPopup() { } | |
a340b80d VZ |
70 | virtual ~wxVListBoxComboPopup(); |
71 | ||
72 | // required virtuals | |
6d0ce565 | 73 | virtual void Init(); |
a340b80d VZ |
74 | virtual bool Create(wxWindow* parent); |
75 | virtual wxWindow *GetControl() { return this; } | |
76 | virtual void SetStringValue( const wxString& value ); | |
77 | virtual wxString GetStringValue() const; | |
78 | ||
79 | // more customization | |
80 | virtual void OnPopup(); | |
81 | virtual wxSize GetAdjustedSize( int minWidth, int prefHeight, int maxHeight ); | |
82 | virtual void PaintComboControl( wxDC& dc, const wxRect& rect ); | |
83 | virtual void OnComboKeyEvent( wxKeyEvent& event ); | |
84 | virtual void OnComboDoubleClick(); | |
85 | virtual bool LazyCreate(); | |
86 | ||
87 | // Item management | |
88 | void SetSelection( int item ); | |
89 | void Insert( const wxString& item, int pos ); | |
90 | int Append(const wxString& item); | |
91 | void Clear(); | |
92 | void Delete( unsigned int item ); | |
93 | void SetItemClientData(unsigned int n, void* clientData, wxClientDataType clientDataItemsType); | |
94 | void *GetItemClientData(unsigned int n) const; | |
95 | void SetString( int item, const wxString& str ); | |
96 | wxString GetString( int item ) const; | |
97 | unsigned int GetCount() const; | |
9e6aca68 | 98 | int FindString(const wxString& s, bool bCase = false) const; |
6d0ce565 | 99 | int GetSelection() const; |
a340b80d | 100 | |
6d0ce565 VZ |
101 | //void Populate( int n, const wxString choices[] ); |
102 | void Populate( const wxArrayString& choices ); | |
a340b80d VZ |
103 | void ClearClientDatas(); |
104 | ||
105 | // helpers | |
106 | int GetItemAtPosition( const wxPoint& pos ) { return HitTest(pos); } | |
107 | wxCoord GetTotalHeight() const { return EstimateTotalHeight(); } | |
108 | wxCoord GetLineHeight(int line) const { return OnGetLineHeight(line); } | |
109 | ||
110 | protected: | |
111 | ||
112 | // Called by OnComboDoubleClick and OnComboKeyEvent | |
113 | bool HandleKey( int keycode, bool saturate ); | |
114 | ||
115 | // sends combobox select event from the parent combo control | |
6d0ce565 | 116 | void SendComboBoxEvent( int selection ); |
7ca4ac63 | 117 | |
6d0ce565 VZ |
118 | // gets value, sends event and dismisses |
119 | void DismissWithEvent(); | |
a340b80d VZ |
120 | |
121 | // Re-calculates width for given item | |
122 | void CheckWidth( int pos ); | |
123 | ||
40b26d75 WS |
124 | // Callbacks for drawing and measuring items. Override in a derived class for |
125 | // owner-drawnness. Font, background and text colour have been prepared according | |
126 | // to selection, focus and such. | |
127 | // | |
128 | // item: item index to be drawn, may be wxNOT_FOUND when painting combo control itself | |
129 | // and there is no valid selection | |
130 | // flags: wxODCB_PAINTING_CONTROL is set if painting to combo control instead of list | |
131 | // NOTE: If wxVListBoxComboPopup is used with wxComboCtrl class not derived from | |
132 | // wxOwnerDrawnComboBox, this method must be overridden. | |
133 | virtual void OnDrawItem( wxDC& dc, const wxRect& rect, int item, int flags ) const; | |
a340b80d | 134 | |
40b26d75 | 135 | // This is same as in wxVListBox |
dcc8cf5a PC |
136 | virtual wxCoord OnMeasureItem( size_t item ) const; |
137 | ||
40b26d75 WS |
138 | // Return item width, or -1 for calculating from text extent (default) |
139 | virtual wxCoord OnMeasureItemWidth( size_t item ) const; | |
140 | ||
141 | // Draw item and combo control background. Flags are same as with OnDrawItem. | |
142 | // NB: Can't use name OnDrawBackground because of virtual function hiding warnings. | |
143 | virtual void OnDrawBg(wxDC& dc, const wxRect& rect, int item, int flags) const; | |
144 | ||
145 | // Additional wxVListBox implementation (no need to override in derived classes) | |
146 | virtual void OnDrawItem(wxDC& dc, const wxRect& rect, size_t n) const; | |
147 | void OnDrawBackground(wxDC& dc, const wxRect& rect, size_t n) const; | |
148 | ||
a340b80d VZ |
149 | // filter mouse move events happening outside the list box |
150 | // move selection with cursor | |
151 | void OnMouseMove(wxMouseEvent& event); | |
152 | void OnMouseWheel(wxMouseEvent& event); | |
153 | void OnKey(wxKeyEvent& event); | |
154 | void OnLeftClick(wxMouseEvent& event); | |
155 | ||
156 | wxArrayString m_strings; | |
157 | wxArrayPtrVoid m_clientDatas; | |
158 | wxArrayInt m_widths; // cached line widths | |
159 | ||
6d0ce565 | 160 | wxFont m_useFont; |
a340b80d | 161 | |
6d0ce565 | 162 | //wxString m_stringValue; // displayed text (may be different than m_strings[m_value]) |
a340b80d VZ |
163 | int m_value; // selection |
164 | int m_itemHover; // on which item the cursor is | |
165 | int m_widestWidth; // width of widest item thus far | |
166 | int m_avgCharWidth; | |
167 | int m_baseImageWidth; // how much per item drawn in addition to text | |
168 | int m_itemHeight; // default item height (calculate from font size | |
169 | // and used in the absence of callback) | |
170 | wxClientDataType m_clientDataItemsType; | |
171 | ||
172 | private: | |
173 | ||
174 | // has the mouse been released on this control? | |
175 | bool m_clicked; | |
176 | ||
177 | DECLARE_EVENT_TABLE() | |
178 | }; | |
179 | ||
180 | ||
181 | // ---------------------------------------------------------------------------- | |
182 | // wxOwnerDrawnComboBox: a generic wxComboBox that allows custom paint items | |
183 | // in addition to many other types of customization already allowed by | |
a57d600f | 184 | // the wxComboCtrl. |
a340b80d VZ |
185 | // ---------------------------------------------------------------------------- |
186 | ||
a57d600f | 187 | class WXDLLIMPEXP_ADV wxOwnerDrawnComboBox : public wxComboCtrl, |
c98e80d9 | 188 | public wxItemContainer |
a340b80d | 189 | { |
40b26d75 WS |
190 | //friend class wxComboPopupWindow; |
191 | friend class wxVListBoxComboPopup; | |
a340b80d VZ |
192 | public: |
193 | ||
194 | // ctors and such | |
a57d600f | 195 | wxOwnerDrawnComboBox() : wxComboCtrl() { Init(); } |
a340b80d VZ |
196 | |
197 | wxOwnerDrawnComboBox(wxWindow *parent, | |
198 | wxWindowID id, | |
199 | const wxString& value, | |
200 | const wxPoint& pos, | |
201 | const wxSize& size, | |
202 | int n, | |
203 | const wxString choices[], | |
204 | long style = 0, | |
205 | const wxValidator& validator = wxDefaultValidator, | |
206 | const wxString& name = wxComboBoxNameStr) | |
a57d600f | 207 | : wxComboCtrl() |
a340b80d VZ |
208 | { |
209 | Init(); | |
210 | ||
211 | (void)Create(parent, id, value, pos, size, n, | |
212 | choices, style, validator, name); | |
213 | } | |
214 | ||
215 | bool Create(wxWindow *parent, | |
216 | wxWindowID id, | |
1c4743aa | 217 | const wxString& value = wxEmptyString, |
a340b80d VZ |
218 | const wxPoint& pos = wxDefaultPosition, |
219 | const wxSize& size = wxDefaultSize, | |
220 | long style = 0, | |
221 | const wxValidator& validator = wxDefaultValidator, | |
222 | const wxString& name = wxComboBoxNameStr); | |
223 | ||
224 | wxOwnerDrawnComboBox(wxWindow *parent, | |
225 | wxWindowID id, | |
226 | const wxString& value, | |
227 | const wxPoint& pos, | |
228 | const wxSize& size, | |
229 | const wxArrayString& choices, | |
7ca4ac63 | 230 | long style, |
a340b80d VZ |
231 | const wxValidator& validator = wxDefaultValidator, |
232 | const wxString& name = wxComboBoxNameStr); | |
233 | ||
234 | bool Create(wxWindow *parent, | |
235 | wxWindowID id, | |
1c4743aa WS |
236 | const wxString& value, |
237 | const wxPoint& pos, | |
238 | const wxSize& size, | |
239 | int n, | |
240 | const wxString choices[], | |
a340b80d VZ |
241 | long style = 0, |
242 | const wxValidator& validator = wxDefaultValidator, | |
243 | const wxString& name = wxComboBoxNameStr); | |
244 | ||
245 | bool Create(wxWindow *parent, | |
246 | wxWindowID id, | |
247 | const wxString& value, | |
248 | const wxPoint& pos, | |
249 | const wxSize& size, | |
250 | const wxArrayString& choices, | |
251 | long style = 0, | |
252 | const wxValidator& validator = wxDefaultValidator, | |
253 | const wxString& name = wxComboBoxNameStr); | |
254 | ||
255 | virtual ~wxOwnerDrawnComboBox(); | |
256 | ||
6d0ce565 VZ |
257 | // NULL popup can be used to indicate default interface |
258 | virtual void SetPopupControl( wxComboPopup* popup ); | |
259 | ||
a340b80d VZ |
260 | // wxControlWithItems methods |
261 | virtual void Clear(); | |
262 | virtual void Delete(unsigned int n); | |
263 | virtual unsigned int GetCount() const; | |
264 | virtual wxString GetString(unsigned int n) const; | |
265 | virtual void SetString(unsigned int n, const wxString& s); | |
9e6aca68 | 266 | virtual int FindString(const wxString& s, bool bCase = false) const; |
a340b80d VZ |
267 | virtual void Select(int n); |
268 | virtual int GetSelection() const; | |
c0f48280 | 269 | virtual void SetSelection(int n) { Select(n); } |
a340b80d | 270 | |
7ca4ac63 WS |
271 | |
272 | // Prevent a method from being hidden | |
273 | virtual void SetSelection(long from, long to) | |
274 | { | |
275 | wxComboCtrl::SetSelection(from,to); | |
276 | } | |
277 | ||
a340b80d VZ |
278 | wxCONTROL_ITEMCONTAINER_CLIENTDATAOBJECT_RECAST |
279 | ||
280 | protected: | |
281 | ||
40b26d75 WS |
282 | // Callback for drawing. Font, background and text colour have been |
283 | // prepared according to selection, focus and such. | |
284 | // item: item index to be drawn, may be wxNOT_FOUND when painting combo control itself | |
285 | // and there is no valid selection | |
286 | // flags: wxODCB_PAINTING_CONTROL is set if painting to combo control instead of list | |
287 | virtual void OnDrawItem( wxDC& dc, const wxRect& rect, int item, int flags ) const; | |
288 | ||
289 | // Callback for item height, or -1 for default | |
290 | virtual wxCoord OnMeasureItem( size_t item ) const; | |
291 | ||
292 | // Callback for item width, or -1 for default/undetermined | |
293 | virtual wxCoord OnMeasureItemWidth( size_t item ) const; | |
294 | ||
295 | // Callback for background drawing. Flags are same as with | |
296 | // OnDrawItem. | |
297 | virtual void OnDrawBackground( wxDC& dc, const wxRect& rect, int item, int flags ) const; | |
298 | ||
a340b80d VZ |
299 | // clears all allocated client datas |
300 | void ClearClientDatas(); | |
301 | ||
302 | virtual int DoAppend(const wxString& item); | |
303 | virtual int DoInsert(const wxString& item, unsigned int pos); | |
304 | virtual void DoSetItemClientData(unsigned int n, void* clientData); | |
305 | virtual void* DoGetItemClientData(unsigned int n) const; | |
306 | virtual void DoSetItemClientObject(unsigned int n, wxClientData* clientData); | |
307 | virtual wxClientData* DoGetItemClientObject(unsigned int n) const; | |
308 | ||
309 | // overload m_popupInterface member so we can access specific popup interface easier | |
310 | wxVListBoxComboPopup* m_popupInterface; | |
311 | ||
6d0ce565 VZ |
312 | // temporary storage for the initial choices |
313 | //const wxString* m_baseChoices; | |
314 | //int m_baseChoicesCount; | |
315 | wxArrayString m_initChs; | |
316 | ||
a340b80d VZ |
317 | private: |
318 | void Init(); | |
319 | ||
320 | DECLARE_EVENT_TABLE() | |
321 | ||
322 | DECLARE_DYNAMIC_CLASS(wxOwnerDrawnComboBox) | |
323 | }; | |
324 | ||
325 | ||
a57d600f | 326 | #endif // wxUSE_ODCOMBOBOX |
7ca4ac63 | 327 | |
a340b80d VZ |
328 | #endif |
329 | // _WX_ODCOMBO_H_ |