]>
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" | |
f0fa8b47 | 22 | #include "wx/timer.h" |
a340b80d VZ |
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 | // | |
40b26d75 | 41 | // Callback flags (see wxOwnerDrawnComboBox::OnDrawItem) |
a340b80d | 42 | // |
3fedffdc | 43 | enum wxOwnerDrawnComboBoxPaintingFlags |
a340b80d VZ |
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 |
70 | class WXDLLIMPEXP_ADV wxVListBoxComboPopup : public wxVListBox, |
71 | public wxComboPopup | |
a340b80d VZ |
72 | { |
73 | friend class wxOwnerDrawnComboBox; | |
74 | public: | |
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 ); | |
c765e6fb | 92 | virtual void OnComboCharEvent( wxKeyEvent& event ); |
a340b80d VZ |
93 | virtual void OnComboDoubleClick(); |
94 | virtual bool LazyCreate(); | |
95 | ||
96 | // Item management | |
97 | void SetSelection( int item ); | |
98 | void Insert( const wxString& item, int pos ); | |
99 | int Append(const wxString& item); | |
100 | void Clear(); | |
101 | void Delete( unsigned int item ); | |
102 | void SetItemClientData(unsigned int n, void* clientData, wxClientDataType clientDataItemsType); | |
103 | void *GetItemClientData(unsigned int n) const; | |
104 | void SetString( int item, const wxString& str ); | |
105 | wxString GetString( int item ) const; | |
106 | unsigned int GetCount() const; | |
9e6aca68 | 107 | int FindString(const wxString& s, bool bCase = false) const; |
6d0ce565 | 108 | int GetSelection() const; |
a340b80d | 109 | |
6d0ce565 VZ |
110 | //void Populate( int n, const wxString choices[] ); |
111 | void Populate( const wxArrayString& choices ); | |
a340b80d VZ |
112 | void ClearClientDatas(); |
113 | ||
114 | // helpers | |
115 | int GetItemAtPosition( const wxPoint& pos ) { return HitTest(pos); } | |
116 | wxCoord GetTotalHeight() const { return EstimateTotalHeight(); } | |
e02c72fa | 117 | wxCoord GetLineHeight(int line) const { return OnGetRowHeight(line); } |
a340b80d VZ |
118 | |
119 | protected: | |
120 | ||
c765e6fb VS |
121 | // Called by OnComboDoubleClick and OnCombo{Key,Char}Event |
122 | bool HandleKey( int keycode, bool saturate, wxChar keychar = 0 ); | |
a340b80d VZ |
123 | |
124 | // sends combobox select event from the parent combo control | |
6d0ce565 | 125 | void SendComboBoxEvent( int selection ); |
7ca4ac63 | 126 | |
6d0ce565 VZ |
127 | // gets value, sends event and dismisses |
128 | void DismissWithEvent(); | |
a340b80d | 129 | |
e5d63342 WS |
130 | // OnMeasureItemWidth will be called on next GetAdjustedSize. |
131 | void ItemWidthChanged(unsigned int item) | |
132 | { | |
133 | m_widths[item] = -1; | |
134 | m_widthsDirty = true; | |
135 | } | |
a340b80d | 136 | |
40b26d75 WS |
137 | // Callbacks for drawing and measuring items. Override in a derived class for |
138 | // owner-drawnness. Font, background and text colour have been prepared according | |
139 | // to selection, focus and such. | |
140 | // | |
141 | // item: item index to be drawn, may be wxNOT_FOUND when painting combo control itself | |
142 | // and there is no valid selection | |
143 | // flags: wxODCB_PAINTING_CONTROL is set if painting to combo control instead of list | |
3fedffdc FM |
144 | // |
145 | // NOTE: If wxVListBoxComboPopup is used with a wxComboCtrl class not derived from | |
40b26d75 | 146 | // wxOwnerDrawnComboBox, this method must be overridden. |
3fedffdc | 147 | virtual void OnDrawItem( wxDC& dc, const wxRect& rect, int item, int flags) const; |
a340b80d | 148 | |
40b26d75 | 149 | // This is same as in wxVListBox |
dcc8cf5a PC |
150 | virtual wxCoord OnMeasureItem( size_t item ) const; |
151 | ||
40b26d75 WS |
152 | // Return item width, or -1 for calculating from text extent (default) |
153 | virtual wxCoord OnMeasureItemWidth( size_t item ) const; | |
154 | ||
155 | // Draw item and combo control background. Flags are same as with OnDrawItem. | |
156 | // NB: Can't use name OnDrawBackground because of virtual function hiding warnings. | |
157 | virtual void OnDrawBg(wxDC& dc, const wxRect& rect, int item, int flags) const; | |
158 | ||
159 | // Additional wxVListBox implementation (no need to override in derived classes) | |
160 | virtual void OnDrawItem(wxDC& dc, const wxRect& rect, size_t n) const; | |
161 | void OnDrawBackground(wxDC& dc, const wxRect& rect, size_t n) const; | |
162 | ||
a340b80d VZ |
163 | // filter mouse move events happening outside the list box |
164 | // move selection with cursor | |
165 | void OnMouseMove(wxMouseEvent& event); | |
166 | void OnMouseWheel(wxMouseEvent& event); | |
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 | ||
192 | private: | |
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 | ||
a57d600f | 235 | class WXDLLIMPEXP_ADV wxOwnerDrawnComboBox : public wxComboCtrl, |
c98e80d9 | 236 | public wxItemContainer |
a340b80d | 237 | { |
40b26d75 WS |
238 | //friend class wxComboPopupWindow; |
239 | friend class wxVListBoxComboPopup; | |
a340b80d VZ |
240 | public: |
241 | ||
242 | // ctors and such | |
a57d600f | 243 | wxOwnerDrawnComboBox() : wxComboCtrl() { 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) | |
a57d600f | 255 | : wxComboCtrl() |
a340b80d VZ |
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, | |
1c4743aa | 265 | const wxString& value = wxEmptyString, |
a340b80d VZ |
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, | |
275 | const wxPoint& pos, | |
276 | const wxSize& size, | |
277 | const wxArrayString& choices, | |
7ca4ac63 | 278 | long style, |
a340b80d VZ |
279 | const wxValidator& validator = wxDefaultValidator, |
280 | const wxString& name = wxComboBoxNameStr); | |
281 | ||
282 | bool Create(wxWindow *parent, | |
283 | wxWindowID id, | |
1c4743aa WS |
284 | const wxString& value, |
285 | const wxPoint& pos, | |
286 | const wxSize& size, | |
287 | int n, | |
288 | const wxString choices[], | |
a340b80d VZ |
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 | ||
db53c6ea WS |
305 | // Prevent app from using wxComboPopup |
306 | void SetPopupControl(wxVListBoxComboPopup* popup) | |
307 | { | |
308 | DoSetPopupControl(popup); | |
309 | } | |
6d0ce565 | 310 | |
a340b80d | 311 | // wxControlWithItems methods |
a340b80d VZ |
312 | virtual unsigned int GetCount() const; |
313 | virtual wxString GetString(unsigned int n) const; | |
314 | virtual void SetString(unsigned int n, const wxString& s); | |
9e6aca68 | 315 | virtual int FindString(const wxString& s, bool bCase = false) const; |
a340b80d VZ |
316 | virtual void Select(int n); |
317 | virtual int GetSelection() const; | |
c0f48280 | 318 | virtual void SetSelection(int n) { Select(n); } |
a340b80d | 319 | |
7ca4ac63 WS |
320 | |
321 | // Prevent a method from being hidden | |
322 | virtual void SetSelection(long from, long to) | |
323 | { | |
324 | wxComboCtrl::SetSelection(from,to); | |
325 | } | |
326 | ||
d9e0958c AB |
327 | // Return the widest item width (recalculating it if necessary) |
328 | virtual int GetWidestItemWidth() { EnsurePopupControl(); return GetVListBoxComboPopup()->GetWidestItemWidth(); } | |
329 | ||
330 | // Return the index of the widest item (recalculating it if necessary) | |
331 | virtual int GetWidestItem() { EnsurePopupControl(); return GetVListBoxComboPopup()->GetWidestItem(); } | |
332 | ||
a236aa20 VZ |
333 | virtual bool IsSorted() const { return HasFlag(wxCB_SORT); } |
334 | ||
a340b80d VZ |
335 | wxCONTROL_ITEMCONTAINER_CLIENTDATAOBJECT_RECAST |
336 | ||
337 | protected: | |
470f357f PC |
338 | virtual void DoClear(); |
339 | virtual void DoDeleteOneItem(unsigned int n); | |
a340b80d | 340 | |
40b26d75 WS |
341 | // Callback for drawing. Font, background and text colour have been |
342 | // prepared according to selection, focus and such. | |
343 | // item: item index to be drawn, may be wxNOT_FOUND when painting combo control itself | |
344 | // and there is no valid selection | |
345 | // flags: wxODCB_PAINTING_CONTROL is set if painting to combo control instead of list | |
346 | virtual void OnDrawItem( wxDC& dc, const wxRect& rect, int item, int flags ) const; | |
347 | ||
348 | // Callback for item height, or -1 for default | |
349 | virtual wxCoord OnMeasureItem( size_t item ) const; | |
350 | ||
351 | // Callback for item width, or -1 for default/undetermined | |
352 | virtual wxCoord OnMeasureItemWidth( size_t item ) const; | |
353 | ||
354 | // Callback for background drawing. Flags are same as with | |
355 | // OnDrawItem. | |
356 | virtual void OnDrawBackground( wxDC& dc, const wxRect& rect, int item, int flags ) const; | |
357 | ||
db53c6ea WS |
358 | // NULL popup can be used to indicate default interface |
359 | virtual void DoSetPopupControl(wxComboPopup* popup); | |
360 | ||
a340b80d VZ |
361 | // clears all allocated client datas |
362 | void ClearClientDatas(); | |
363 | ||
57693473 VZ |
364 | wxVListBoxComboPopup* GetVListBoxComboPopup() const |
365 | { | |
366 | return (wxVListBoxComboPopup*) m_popupInterface; | |
367 | } | |
368 | ||
a236aa20 VZ |
369 | virtual int DoInsertItems(const wxArrayStringsAdapter& items, |
370 | unsigned int pos, | |
371 | void **clientData, wxClientDataType type); | |
a340b80d VZ |
372 | virtual void DoSetItemClientData(unsigned int n, void* clientData); |
373 | virtual void* DoGetItemClientData(unsigned int n) const; | |
a340b80d | 374 | |
6d0ce565 VZ |
375 | // temporary storage for the initial choices |
376 | //const wxString* m_baseChoices; | |
377 | //int m_baseChoicesCount; | |
378 | wxArrayString m_initChs; | |
379 | ||
a340b80d VZ |
380 | private: |
381 | void Init(); | |
382 | ||
383 | DECLARE_EVENT_TABLE() | |
384 | ||
385 | DECLARE_DYNAMIC_CLASS(wxOwnerDrawnComboBox) | |
386 | }; | |
387 | ||
388 | ||
a57d600f | 389 | #endif // wxUSE_ODCOMBOBOX |
7ca4ac63 | 390 | |
a340b80d VZ |
391 | #endif |
392 | // _WX_ODCOMBO_H_ |