Big wxDataViewCtrl renderer classes refactoring.
[wxWidgets.git] / include / wx / dvrenderers.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/dvrenderers.h
3 // Purpose: Declare all wxDataViewCtrl classes
4 // Author: Robert Roebling, Vadim Zeitlin
5 // Created: 2009-11-08 (extracted from wx/dataview.h)
6 // RCS-ID: $Id: wxhead.h,v 1.11 2009-06-29 10:23:04 zeitlin Exp $
7 // Copyright: (c) 2006 Robert Roebling
8 // (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_DVRENDERERS_H_
13 #define _WX_DVRENDERERS_H_
14
15 /*
16 Note about the structure of various headers: they're organized in a more
17 complicated way than usual because of the various dependencies which are
18 different for different ports. In any case the only public header, i.e. the
19 one which can be included directly is wx/dataview.h. It, in turn, includes
20 this one to define all the renderer classes.
21
22 We define the base wxDataViewRendererBase class first and then include a
23 port-dependent wx/xxx/dvrenderer.h which defines wxDataViewRenderer itself.
24 After this we can define wxDataViewRendererCustomBase (and maybe in the
25 future base classes for other renderers if the need arises, i.e. if there
26 is any non-trivial code or API which it makes sense to keep in common code)
27 and include wx/xxx/dvrenderers.h (notice the plural) which defines all the
28 rest of the renderer classes.
29 */
30
31 class WXDLLIMPEXP_FWD_ADV wxDataViewCustomRenderer;
32
33 // ----------------------------------------------------------------------------
34 // wxDataViewIconText: helper class used by wxDataViewIconTextRenderer
35 // ----------------------------------------------------------------------------
36
37 class WXDLLIMPEXP_ADV wxDataViewIconText : public wxObject
38 {
39 public:
40 wxDataViewIconText( const wxString &text = wxEmptyString,
41 const wxIcon& icon = wxNullIcon )
42 : m_text(text),
43 m_icon(icon)
44 { }
45
46 wxDataViewIconText( const wxDataViewIconText &other )
47 : wxObject(),
48 m_text(other.m_text),
49 m_icon(other.m_icon)
50 { }
51
52 void SetText( const wxString &text ) { m_text = text; }
53 wxString GetText() const { return m_text; }
54 void SetIcon( const wxIcon &icon ) { m_icon = icon; }
55 const wxIcon &GetIcon() const { return m_icon; }
56
57 private:
58 wxString m_text;
59 wxIcon m_icon;
60
61 DECLARE_DYNAMIC_CLASS(wxDataViewIconText)
62 };
63
64 inline
65 bool operator==(const wxDataViewIconText& left, const wxDataViewIconText& right)
66 {
67 return left.GetText() == right.GetText() &&
68 left.GetIcon().IsSameAs(right.GetIcon());
69 }
70
71 inline
72 bool operator!=(const wxDataViewIconText& left, const wxDataViewIconText& right)
73 {
74 return !(left == right);
75 }
76
77 DECLARE_VARIANT_OBJECT_EXPORTED(wxDataViewIconText, WXDLLIMPEXP_ADV)
78
79 // ----------------------------------------------------------------------------
80 // wxDataViewRendererBase
81 // ----------------------------------------------------------------------------
82
83 enum wxDataViewCellMode
84 {
85 wxDATAVIEW_CELL_INERT,
86 wxDATAVIEW_CELL_ACTIVATABLE,
87 wxDATAVIEW_CELL_EDITABLE
88 };
89
90 enum wxDataViewCellRenderState
91 {
92 wxDATAVIEW_CELL_SELECTED = 1,
93 wxDATAVIEW_CELL_PRELIT = 2,
94 wxDATAVIEW_CELL_INSENSITIVE = 4,
95 wxDATAVIEW_CELL_FOCUSED = 8
96 };
97
98 class WXDLLIMPEXP_ADV wxDataViewRendererBase: public wxObject
99 {
100 public:
101 wxDataViewRendererBase( const wxString &varianttype,
102 wxDataViewCellMode mode = wxDATAVIEW_CELL_INERT,
103 int alignment = wxDVR_DEFAULT_ALIGNMENT );
104 virtual ~wxDataViewRendererBase();
105
106 virtual bool Validate( wxVariant& WXUNUSED(value) )
107 { return true; }
108
109 void SetOwner( wxDataViewColumn *owner ) { m_owner = owner; }
110 wxDataViewColumn* GetOwner() const { return m_owner; }
111
112 // renderer properties:
113
114 virtual bool SetValue( const wxVariant& WXUNUSED(value) ) = 0;
115 virtual bool GetValue( wxVariant& WXUNUSED(value) ) const = 0;
116
117 wxString GetVariantType() const { return m_variantType; }
118
119 virtual void SetMode( wxDataViewCellMode mode ) = 0;
120 virtual wxDataViewCellMode GetMode() const = 0;
121
122 // NOTE: Set/GetAlignment do not take/return a wxAlignment enum but
123 // rather an "int"; that's because for rendering cells it's allowed
124 // to combine alignment flags (e.g. wxALIGN_LEFT|wxALIGN_BOTTOM)
125 virtual void SetAlignment( int align ) = 0;
126 virtual int GetAlignment() const = 0;
127
128 // enable or disable (if called with wxELLIPSIZE_NONE) replacing parts of
129 // the item text (hence this only makes sense for renderers showing
130 // text...) with ellipsis in order to make it fit the column width
131 virtual void EnableEllipsize(wxEllipsizeMode mode = wxELLIPSIZE_MIDDLE) = 0;
132 void DisableEllipsize() { EnableEllipsize(wxELLIPSIZE_NONE); }
133
134 virtual wxEllipsizeMode GetEllipsizeMode() const = 0;
135
136 // in-place editing
137 virtual bool HasEditorCtrl() const
138 { return false; }
139 virtual wxControl* CreateEditorCtrl(wxWindow * WXUNUSED(parent),
140 wxRect WXUNUSED(labelRect),
141 const wxVariant& WXUNUSED(value))
142 { return NULL; }
143 virtual bool GetValueFromEditorCtrl(wxControl * WXUNUSED(editor),
144 wxVariant& WXUNUSED(value))
145 { return false; }
146
147 virtual bool StartEditing( const wxDataViewItem &item, wxRect labelRect );
148 virtual void CancelEditing();
149 virtual bool FinishEditing();
150
151 wxControl *GetEditorCtrl() { return m_editorCtrl; }
152
153 protected:
154 wxString m_variantType;
155 wxDataViewColumn *m_owner;
156 wxWeakRef<wxControl> m_editorCtrl;
157 wxDataViewItem m_item; // for m_editorCtrl
158
159 // internal utility:
160 const wxDataViewCtrl* GetView() const;
161
162 protected:
163 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewRendererBase)
164 };
165
166 // include the real wxDataViewRenderer declaration for the native ports
167 #ifdef wxHAS_GENERIC_DATAVIEWCTRL
168 // in the generic implementation there is no real wxDataViewRenderer, all
169 // renderers are custom so it's the same as wxDataViewCustomRenderer and
170 // wxDataViewCustomRendererBase derives from wxDataViewRendererBase directly
171 //
172 // this is a rather ugly hack but unfortunately it just doesn't seem to be
173 // possible to have the same class hierarchy in all ports and avoid
174 // duplicating the entire wxDataViewCustomRendererBase in the generic
175 // wxDataViewRenderer class (well, we could use a mix-in but this would
176 // make classes hierarchy non linear and arguably even more complex)
177 #define wxDataViewCustomRendererRealBase wxDataViewRendererBase
178 #else
179 #if defined(__WXGTK20__)
180 #include "wx/gtk/dvrenderer.h"
181 #elif defined(__WXMAC__)
182 #include "wx/osx/dvrenderer.h"
183 #else
184 #error "unknown native wxDataViewCtrl implementation"
185 #endif
186 #define wxDataViewCustomRendererRealBase wxDataViewRenderer
187 #endif
188
189 // ----------------------------------------------------------------------------
190 // wxDataViewCustomRendererBase
191 // ----------------------------------------------------------------------------
192
193 class WXDLLIMPEXP_ADV wxDataViewCustomRendererBase
194 : public wxDataViewCustomRendererRealBase
195 {
196 public:
197 // Constructor must specify the usual renderer parameters which we simply
198 // pass to the base class
199 wxDataViewCustomRendererBase(const wxString& varianttype = "string",
200 wxDataViewCellMode mode = wxDATAVIEW_CELL_INERT,
201 int align = wxDVR_DEFAULT_ALIGNMENT)
202 : wxDataViewCustomRendererRealBase(varianttype, mode, align)
203 {
204 }
205
206
207 // Define virtual function which are called when the item is activated
208 // (double-clicked or Enter is pressed on it), clicked or the user starts
209 // to drag it: by default they all simply return false indicating that the
210 // events are not handled
211
212 virtual bool Activate(wxRect WXUNUSED(cell),
213 wxDataViewModel *WXUNUSED(model),
214 const wxDataViewItem & WXUNUSED(item),
215 unsigned int WXUNUSED(col))
216 { return false; }
217
218 virtual bool LeftClick(wxPoint WXUNUSED(cursor),
219 wxRect WXUNUSED(cell),
220 wxDataViewModel *WXUNUSED(model),
221 const wxDataViewItem & WXUNUSED(item),
222 unsigned int WXUNUSED(col) )
223 { return false; }
224
225 virtual bool StartDrag(wxPoint WXUNUSED(cursor),
226 wxRect WXUNUSED(cell),
227 wxDataViewModel *WXUNUSED(model),
228 const wxDataViewItem & WXUNUSED(item),
229 unsigned int WXUNUSED(col) )
230 { return false; }
231
232
233 private:
234 wxDECLARE_NO_COPY_CLASS(wxDataViewCustomRendererBase);
235 };
236
237 // include the declaration of all the other renderers to get the real
238 // wxDataViewCustomRenderer from which we need to inherit below
239 #ifdef wxHAS_GENERIC_DATAVIEWCTRL
240 // because of the different renderer classes hierarchy in the generic
241 // version, as explained above, we can include the header defining
242 // wxDataViewRenderer only here and not before wxDataViewCustomRendererBase
243 // declaration as for the native ports
244 #include "wx/generic/dvrenderer.h"
245 #include "wx/generic/dvrenderers.h"
246 #elif defined(__WXGTK20__)
247 #include "wx/gtk/dvrenderers.h"
248 #elif defined(__WXMAC__)
249 #include "wx/osx/dvrenderers.h"
250 #else
251 #error "unknown native wxDataViewCtrl implementation"
252 #endif
253
254 // ----------------------------------------------------------------------------
255 // wxDataViewSpinRenderer
256 // ----------------------------------------------------------------------------
257
258 class WXDLLIMPEXP_ADV wxDataViewSpinRenderer: public wxDataViewCustomRenderer
259 {
260 public:
261 wxDataViewSpinRenderer( int min, int max,
262 wxDataViewCellMode mode = wxDATAVIEW_CELL_EDITABLE,
263 int alignment = wxDVR_DEFAULT_ALIGNMENT );
264 virtual bool HasEditorCtrl() const { return true; }
265 virtual wxControl* CreateEditorCtrl( wxWindow *parent, wxRect labelRect, const wxVariant &value );
266 virtual bool GetValueFromEditorCtrl( wxControl* editor, wxVariant &value );
267 virtual bool Render( wxRect rect, wxDC *dc, int state );
268 virtual wxSize GetSize() const;
269 virtual bool SetValue( const wxVariant &value );
270 virtual bool GetValue( wxVariant &value ) const;
271
272 private:
273 long m_data;
274 long m_min,m_max;
275 };
276
277 #if defined(wxHAS_GENERIC_DATAVIEWCTRL) || defined(__WXOSX_CARBON__)
278
279 // ----------------------------------------------------------------------------
280 // wxDataViewChoiceRenderer
281 // ----------------------------------------------------------------------------
282
283 class WXDLLIMPEXP_ADV wxDataViewChoiceRenderer: public wxDataViewCustomRenderer
284 {
285 public:
286 wxDataViewChoiceRenderer( const wxArrayString &choices,
287 wxDataViewCellMode mode = wxDATAVIEW_CELL_EDITABLE,
288 int alignment = wxDVR_DEFAULT_ALIGNMENT );
289 virtual bool HasEditorCtrl() const { return true; }
290 virtual wxControl* CreateEditorCtrl( wxWindow *parent, wxRect labelRect, const wxVariant &value );
291 virtual bool GetValueFromEditorCtrl( wxControl* editor, wxVariant &value );
292 virtual bool Render( wxRect rect, wxDC *dc, int state );
293 virtual wxSize GetSize() const;
294 virtual bool SetValue( const wxVariant &value );
295 virtual bool GetValue( wxVariant &value ) const;
296
297 private:
298 wxArrayString m_choices;
299 wxString m_data;
300 };
301
302 #endif // generic or Carbon versions
303
304 // this class is obsolete, its functionality was merged in
305 // wxDataViewTextRenderer itself now, don't use it any more
306 #define wxDataViewTextRendererAttr wxDataViewTextRenderer
307
308 #endif // _WX_DVRENDERERS_H_
309