]>
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 | ||
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 | |
6d0ce565 | 46 | wxCP_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 | |
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 | ||
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 | ||
6d0ce565 VZ |
87 | // Callbacks for drawing and measuring items. Override in a derived class for |
88 | // owner-drawnness. | |
89 | // item: item index to be drawn, may be wxNOT_FOUND when painting combo control itself | |
90 | // and there is no valid selection | |
91 | // flags: wxCP_PAINTING_CONTROL is set if painting to combo control instead of list | |
92 | virtual void OnDrawItem( wxDC& dc, const wxRect& rect, int item, int flags ) const; | |
93 | ||
6d0ce565 VZ |
94 | // Return item width, or -1 for calculating from text extent (default) |
95 | virtual wxCoord OnMeasureItemWidth( size_t item ) const; | |
96 | ||
97 | ||
a340b80d VZ |
98 | // Item management |
99 | void SetSelection( int item ); | |
100 | void Insert( const wxString& item, int pos ); | |
101 | int Append(const wxString& item); | |
102 | void Clear(); | |
103 | void Delete( unsigned int item ); | |
104 | void SetItemClientData(unsigned int n, void* clientData, wxClientDataType clientDataItemsType); | |
105 | void *GetItemClientData(unsigned int n) const; | |
106 | void SetString( int item, const wxString& str ); | |
107 | wxString GetString( int item ) const; | |
108 | unsigned int GetCount() const; | |
109 | int FindString(const wxString& s) const; | |
6d0ce565 | 110 | int GetSelection() const; |
a340b80d | 111 | |
6d0ce565 VZ |
112 | //void Populate( int n, const wxString choices[] ); |
113 | void Populate( const wxArrayString& choices ); | |
a340b80d VZ |
114 | void ClearClientDatas(); |
115 | ||
116 | // helpers | |
117 | int GetItemAtPosition( const wxPoint& pos ) { return HitTest(pos); } | |
118 | wxCoord GetTotalHeight() const { return EstimateTotalHeight(); } | |
119 | wxCoord GetLineHeight(int line) const { return OnGetLineHeight(line); } | |
120 | ||
121 | protected: | |
122 | ||
123 | // Called by OnComboDoubleClick and OnComboKeyEvent | |
124 | bool HandleKey( int keycode, bool saturate ); | |
125 | ||
126 | // sends combobox select event from the parent combo control | |
6d0ce565 VZ |
127 | void SendComboBoxEvent( int selection ); |
128 | ||
129 | // gets value, sends event and dismisses | |
130 | void DismissWithEvent(); | |
a340b80d VZ |
131 | |
132 | // Re-calculates width for given item | |
133 | void CheckWidth( int pos ); | |
134 | ||
135 | // wxVListBox implementation | |
136 | virtual void OnDrawItem(wxDC& dc, const wxRect& rect, size_t n) const; | |
6d0ce565 | 137 | //virtual wxCoord OnMeasureItem(size_t n) const; |
a340b80d VZ |
138 | void OnDrawBackground(wxDC& dc, const wxRect& rect, size_t n) const; |
139 | ||
dcc8cf5a PC |
140 | // Return item height |
141 | virtual wxCoord OnMeasureItem( size_t item ) const; | |
142 | ||
a340b80d VZ |
143 | // filter mouse move events happening outside the list box |
144 | // move selection with cursor | |
145 | void OnMouseMove(wxMouseEvent& event); | |
146 | void OnMouseWheel(wxMouseEvent& event); | |
147 | void OnKey(wxKeyEvent& event); | |
148 | void OnLeftClick(wxMouseEvent& event); | |
149 | ||
150 | wxArrayString m_strings; | |
151 | wxArrayPtrVoid m_clientDatas; | |
152 | wxArrayInt m_widths; // cached line widths | |
153 | ||
6d0ce565 | 154 | wxFont m_useFont; |
a340b80d | 155 | |
6d0ce565 | 156 | //wxString m_stringValue; // displayed text (may be different than m_strings[m_value]) |
a340b80d VZ |
157 | int m_value; // selection |
158 | int m_itemHover; // on which item the cursor is | |
159 | int m_widestWidth; // width of widest item thus far | |
160 | int m_avgCharWidth; | |
161 | int m_baseImageWidth; // how much per item drawn in addition to text | |
162 | int m_itemHeight; // default item height (calculate from font size | |
163 | // and used in the absence of callback) | |
164 | wxClientDataType m_clientDataItemsType; | |
165 | ||
166 | private: | |
167 | ||
168 | // has the mouse been released on this control? | |
169 | bool m_clicked; | |
170 | ||
171 | DECLARE_EVENT_TABLE() | |
172 | }; | |
173 | ||
174 | ||
175 | // ---------------------------------------------------------------------------- | |
176 | // wxOwnerDrawnComboBox: a generic wxComboBox that allows custom paint items | |
177 | // in addition to many other types of customization already allowed by | |
178 | // the wxComboControl. | |
179 | // ---------------------------------------------------------------------------- | |
180 | ||
c98e80d9 VZ |
181 | class WXDLLIMPEXP_ADV wxOwnerDrawnComboBox : public wxComboControl, |
182 | public wxItemContainer | |
a340b80d VZ |
183 | { |
184 | friend class wxComboPopupWindow; | |
185 | friend class wxComboControlBase; | |
186 | public: | |
187 | ||
188 | // ctors and such | |
189 | wxOwnerDrawnComboBox() : wxComboControl() { Init(); } | |
190 | ||
191 | wxOwnerDrawnComboBox(wxWindow *parent, | |
192 | wxWindowID id, | |
193 | const wxString& value, | |
194 | const wxPoint& pos, | |
195 | const wxSize& size, | |
196 | int n, | |
197 | const wxString choices[], | |
198 | long style = 0, | |
199 | const wxValidator& validator = wxDefaultValidator, | |
200 | const wxString& name = wxComboBoxNameStr) | |
201 | : wxComboControl() | |
202 | { | |
203 | Init(); | |
204 | ||
205 | (void)Create(parent, id, value, pos, size, n, | |
206 | choices, style, validator, name); | |
207 | } | |
208 | ||
209 | bool Create(wxWindow *parent, | |
210 | wxWindowID id, | |
c0f48280 | 211 | const wxString& value, |
a340b80d VZ |
212 | const wxPoint& pos = wxDefaultPosition, |
213 | const wxSize& size = wxDefaultSize, | |
214 | long style = 0, | |
215 | const wxValidator& validator = wxDefaultValidator, | |
216 | const wxString& name = wxComboBoxNameStr); | |
217 | ||
218 | wxOwnerDrawnComboBox(wxWindow *parent, | |
219 | wxWindowID id, | |
220 | const wxString& value, | |
221 | const wxPoint& pos, | |
222 | const wxSize& size, | |
223 | const wxArrayString& choices, | |
224 | long style = 0, | |
225 | const wxValidator& validator = wxDefaultValidator, | |
226 | const wxString& name = wxComboBoxNameStr); | |
227 | ||
228 | bool Create(wxWindow *parent, | |
229 | wxWindowID id, | |
230 | const wxString& value = wxEmptyString, | |
231 | const wxPoint& pos = wxDefaultPosition, | |
232 | const wxSize& size = wxDefaultSize, | |
233 | int n = 0, | |
234 | const wxString choices[] = (const wxString *) NULL, | |
235 | long style = 0, | |
236 | const wxValidator& validator = wxDefaultValidator, | |
237 | const wxString& name = wxComboBoxNameStr); | |
238 | ||
239 | bool Create(wxWindow *parent, | |
240 | wxWindowID id, | |
241 | const wxString& value, | |
242 | const wxPoint& pos, | |
243 | const wxSize& size, | |
244 | const wxArrayString& choices, | |
245 | long style = 0, | |
246 | const wxValidator& validator = wxDefaultValidator, | |
247 | const wxString& name = wxComboBoxNameStr); | |
248 | ||
249 | virtual ~wxOwnerDrawnComboBox(); | |
250 | ||
6d0ce565 VZ |
251 | // NULL popup can be used to indicate default interface |
252 | virtual void SetPopupControl( wxComboPopup* popup ); | |
253 | ||
a340b80d VZ |
254 | // wxControlWithItems methods |
255 | virtual void Clear(); | |
256 | virtual void Delete(unsigned int n); | |
257 | virtual unsigned int GetCount() const; | |
258 | virtual wxString GetString(unsigned int n) const; | |
259 | virtual void SetString(unsigned int n, const wxString& s); | |
260 | virtual int FindString(const wxString& s) const; | |
261 | virtual void Select(int n); | |
262 | virtual int GetSelection() const; | |
c0f48280 | 263 | virtual void SetSelection(int n) { Select(n); } |
a340b80d VZ |
264 | |
265 | wxCONTROL_ITEMCONTAINER_CLIENTDATAOBJECT_RECAST | |
266 | ||
267 | protected: | |
268 | ||
269 | // clears all allocated client datas | |
270 | void ClearClientDatas(); | |
271 | ||
272 | virtual int DoAppend(const wxString& item); | |
273 | virtual int DoInsert(const wxString& item, unsigned int pos); | |
274 | virtual void DoSetItemClientData(unsigned int n, void* clientData); | |
275 | virtual void* DoGetItemClientData(unsigned int n) const; | |
276 | virtual void DoSetItemClientObject(unsigned int n, wxClientData* clientData); | |
277 | virtual wxClientData* DoGetItemClientObject(unsigned int n) const; | |
278 | ||
279 | // overload m_popupInterface member so we can access specific popup interface easier | |
280 | wxVListBoxComboPopup* m_popupInterface; | |
281 | ||
6d0ce565 VZ |
282 | // temporary storage for the initial choices |
283 | //const wxString* m_baseChoices; | |
284 | //int m_baseChoicesCount; | |
285 | wxArrayString m_initChs; | |
286 | ||
a340b80d VZ |
287 | private: |
288 | void Init(); | |
289 | ||
290 | DECLARE_EVENT_TABLE() | |
291 | ||
292 | DECLARE_DYNAMIC_CLASS(wxOwnerDrawnComboBox) | |
293 | }; | |
294 | ||
295 | ||
296 | #endif // wxUSE_OWNERDRAWNCOMBOBOX | |
297 | #endif | |
298 | // _WX_ODCOMBO_H_ |