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)
7 // Copyright: (c) 2006 Robert Roebling
8 // (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_DVRENDERERS_H_
13 #define _WX_DVRENDERERS_H_
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.
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.
31 class WXDLLIMPEXP_FWD_ADV wxDataViewCustomRenderer
;
33 // ----------------------------------------------------------------------------
34 // wxDataViewIconText: helper class used by wxDataViewIconTextRenderer
35 // ----------------------------------------------------------------------------
37 class WXDLLIMPEXP_ADV wxDataViewIconText
: public wxObject
40 wxDataViewIconText( const wxString
&text
= wxEmptyString
,
41 const wxIcon
& icon
= wxNullIcon
)
46 wxDataViewIconText( const wxDataViewIconText
&other
)
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
; }
61 DECLARE_DYNAMIC_CLASS(wxDataViewIconText
)
65 bool operator==(const wxDataViewIconText
& left
, const wxDataViewIconText
& right
)
67 return left
.GetText() == right
.GetText() &&
68 left
.GetIcon().IsSameAs(right
.GetIcon());
72 bool operator!=(const wxDataViewIconText
& left
, const wxDataViewIconText
& right
)
74 return !(left
== right
);
77 DECLARE_VARIANT_OBJECT_EXPORTED(wxDataViewIconText
, WXDLLIMPEXP_ADV
)
79 // ----------------------------------------------------------------------------
80 // wxDataViewRendererBase
81 // ----------------------------------------------------------------------------
83 enum wxDataViewCellMode
85 wxDATAVIEW_CELL_INERT
,
86 wxDATAVIEW_CELL_ACTIVATABLE
,
87 wxDATAVIEW_CELL_EDITABLE
90 enum wxDataViewCellRenderState
92 wxDATAVIEW_CELL_SELECTED
= 1,
93 wxDATAVIEW_CELL_PRELIT
= 2,
94 wxDATAVIEW_CELL_INSENSITIVE
= 4,
95 wxDATAVIEW_CELL_FOCUSED
= 8
98 class WXDLLIMPEXP_ADV wxDataViewRendererBase
: public wxObject
101 wxDataViewRendererBase( const wxString
&varianttype
,
102 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
,
103 int alignment
= wxDVR_DEFAULT_ALIGNMENT
);
104 virtual ~wxDataViewRendererBase();
106 virtual bool Validate( wxVariant
& WXUNUSED(value
) )
109 void SetOwner( wxDataViewColumn
*owner
) { m_owner
= owner
; }
110 wxDataViewColumn
* GetOwner() const { return m_owner
; }
112 // renderer value and attributes: SetValue() and SetAttr() are called
113 // before a cell is rendered using this renderer
114 virtual bool SetValue(const wxVariant
& value
) = 0;
115 virtual bool GetValue(wxVariant
& value
) const = 0;
117 virtual void SetAttr(const wxDataViewItemAttr
& WXUNUSED(attr
)) { }
119 virtual void SetEnabled(bool WXUNUSED(enabled
)) { }
121 wxString
GetVariantType() const { return m_variantType
; }
123 // helper that calls SetValue and SetAttr:
124 void PrepareForItem(const wxDataViewModel
*model
,
125 const wxDataViewItem
& item
, unsigned column
);
127 // renderer properties:
128 virtual void SetMode( wxDataViewCellMode mode
) = 0;
129 virtual wxDataViewCellMode
GetMode() const = 0;
131 // NOTE: Set/GetAlignment do not take/return a wxAlignment enum but
132 // rather an "int"; that's because for rendering cells it's allowed
133 // to combine alignment flags (e.g. wxALIGN_LEFT|wxALIGN_BOTTOM)
134 virtual void SetAlignment( int align
) = 0;
135 virtual int GetAlignment() const = 0;
137 // enable or disable (if called with wxELLIPSIZE_NONE) replacing parts of
138 // the item text (hence this only makes sense for renderers showing
139 // text...) with ellipsis in order to make it fit the column width
140 virtual void EnableEllipsize(wxEllipsizeMode mode
= wxELLIPSIZE_MIDDLE
) = 0;
141 void DisableEllipsize() { EnableEllipsize(wxELLIPSIZE_NONE
); }
143 virtual wxEllipsizeMode
GetEllipsizeMode() const = 0;
146 virtual bool HasEditorCtrl() const
148 virtual wxControl
* CreateEditorCtrl(wxWindow
* WXUNUSED(parent
),
149 wxRect
WXUNUSED(labelRect
),
150 const wxVariant
& WXUNUSED(value
))
152 virtual bool GetValueFromEditorCtrl(wxControl
* WXUNUSED(editor
),
153 wxVariant
& WXUNUSED(value
))
156 virtual bool StartEditing( const wxDataViewItem
&item
, wxRect labelRect
);
157 virtual void CancelEditing();
158 virtual bool FinishEditing();
160 wxControl
*GetEditorCtrl() { return m_editorCtrl
; }
163 // Called from {Cancel,Finish}Editing() to cleanup m_editorCtrl
164 void DestroyEditControl();
166 wxString m_variantType
;
167 wxDataViewColumn
*m_owner
;
168 wxWeakRef
<wxControl
> m_editorCtrl
;
169 wxDataViewItem m_item
; // for m_editorCtrl
172 const wxDataViewCtrl
* GetView() const;
175 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewRendererBase
)
178 // include the real wxDataViewRenderer declaration for the native ports
179 #ifdef wxHAS_GENERIC_DATAVIEWCTRL
180 // in the generic implementation there is no real wxDataViewRenderer, all
181 // renderers are custom so it's the same as wxDataViewCustomRenderer and
182 // wxDataViewCustomRendererBase derives from wxDataViewRendererBase directly
184 // this is a rather ugly hack but unfortunately it just doesn't seem to be
185 // possible to have the same class hierarchy in all ports and avoid
186 // duplicating the entire wxDataViewCustomRendererBase in the generic
187 // wxDataViewRenderer class (well, we could use a mix-in but this would
188 // make classes hierarchy non linear and arguably even more complex)
189 #define wxDataViewCustomRendererRealBase wxDataViewRendererBase
191 #if defined(__WXGTK20__)
192 #include "wx/gtk/dvrenderer.h"
193 #elif defined(__WXMAC__)
194 #include "wx/osx/dvrenderer.h"
196 #error "unknown native wxDataViewCtrl implementation"
198 #define wxDataViewCustomRendererRealBase wxDataViewRenderer
201 // ----------------------------------------------------------------------------
202 // wxDataViewCustomRendererBase
203 // ----------------------------------------------------------------------------
205 class WXDLLIMPEXP_ADV wxDataViewCustomRendererBase
206 : public wxDataViewCustomRendererRealBase
209 // Constructor must specify the usual renderer parameters which we simply
210 // pass to the base class
211 wxDataViewCustomRendererBase(const wxString
& varianttype
= "string",
212 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
,
213 int align
= wxDVR_DEFAULT_ALIGNMENT
)
214 : wxDataViewCustomRendererRealBase(varianttype
, mode
, align
)
219 // Render the item using the current value (returned by GetValue()).
220 virtual bool Render(wxRect cell
, wxDC
*dc
, int state
) = 0;
222 // Return the size of the item appropriate to its current value.
223 virtual wxSize
GetSize() const = 0;
225 // Define virtual function which are called when the item is activated
226 // (double-clicked or Enter is pressed on it), clicked or the user starts
227 // to drag it: by default they all simply return false indicating that the
228 // events are not handled
230 virtual bool Activate(wxRect
WXUNUSED(cell
),
231 wxDataViewModel
*WXUNUSED(model
),
232 const wxDataViewItem
& WXUNUSED(item
),
233 unsigned int WXUNUSED(col
))
236 virtual bool LeftClick(wxPoint
WXUNUSED(cursor
),
237 wxRect
WXUNUSED(cell
),
238 wxDataViewModel
*WXUNUSED(model
),
239 const wxDataViewItem
& WXUNUSED(item
),
240 unsigned int WXUNUSED(col
) )
243 virtual bool StartDrag(wxPoint
WXUNUSED(cursor
),
244 wxRect
WXUNUSED(cell
),
245 wxDataViewModel
*WXUNUSED(model
),
246 const wxDataViewItem
& WXUNUSED(item
),
247 unsigned int WXUNUSED(col
) )
251 // Helper which can be used by Render() implementation in the derived
252 // classes: it will draw the text in the same manner as the standard
254 virtual void RenderText(const wxString
& text
,
261 // Override the base class virtual method to simply store the attribute so
262 // that it can be accessed using GetAttr() from Render() if needed.
263 virtual void SetAttr(const wxDataViewItemAttr
& attr
) { m_attr
= attr
; }
264 const wxDataViewItemAttr
& GetAttr() const { return m_attr
; }
266 // Store the enabled state of the item so that it can be accessed from
267 // Render() via GetEnabled() if needed.
268 virtual void SetEnabled(bool enabled
) { m_enabled
= enabled
; }
269 bool GetEnabled() const { return m_enabled
; }
272 // Implementation only from now on
274 // Retrieve the DC to use for drawing. This is implemented in derived
275 // platform-specific classes.
276 virtual wxDC
*GetDC() = 0;
278 // Prepare DC to use attributes and call Render().
279 void WXCallRender(wxRect rect
, wxDC
*dc
, int state
);
282 // helper for GetSize() implementations, respects attributes
283 wxSize
GetTextExtent(const wxString
& str
) const;
286 wxDataViewItemAttr m_attr
;
289 wxDECLARE_NO_COPY_CLASS(wxDataViewCustomRendererBase
);
292 // include the declaration of all the other renderers to get the real
293 // wxDataViewCustomRenderer from which we need to inherit below
294 #ifdef wxHAS_GENERIC_DATAVIEWCTRL
295 // because of the different renderer classes hierarchy in the generic
296 // version, as explained above, we can include the header defining
297 // wxDataViewRenderer only here and not before wxDataViewCustomRendererBase
298 // declaration as for the native ports
299 #include "wx/generic/dvrenderer.h"
300 #include "wx/generic/dvrenderers.h"
301 #elif defined(__WXGTK20__)
302 #include "wx/gtk/dvrenderers.h"
303 #elif defined(__WXMAC__)
304 #include "wx/osx/dvrenderers.h"
306 #error "unknown native wxDataViewCtrl implementation"
309 // ----------------------------------------------------------------------------
310 // wxDataViewSpinRenderer
311 // ----------------------------------------------------------------------------
313 class WXDLLIMPEXP_ADV wxDataViewSpinRenderer
: public wxDataViewCustomRenderer
316 wxDataViewSpinRenderer( int min
, int max
,
317 wxDataViewCellMode mode
= wxDATAVIEW_CELL_EDITABLE
,
318 int alignment
= wxDVR_DEFAULT_ALIGNMENT
);
319 virtual bool HasEditorCtrl() const { return true; }
320 virtual wxControl
* CreateEditorCtrl( wxWindow
*parent
, wxRect labelRect
, const wxVariant
&value
);
321 virtual bool GetValueFromEditorCtrl( wxControl
* editor
, wxVariant
&value
);
322 virtual bool Render( wxRect rect
, wxDC
*dc
, int state
);
323 virtual wxSize
GetSize() const;
324 virtual bool SetValue( const wxVariant
&value
);
325 virtual bool GetValue( wxVariant
&value
) const;
332 #if defined(wxHAS_GENERIC_DATAVIEWCTRL) || defined(__WXOSX_CARBON__)
334 // ----------------------------------------------------------------------------
335 // wxDataViewChoiceRenderer
336 // ----------------------------------------------------------------------------
338 class WXDLLIMPEXP_ADV wxDataViewChoiceRenderer
: public wxDataViewCustomRenderer
341 wxDataViewChoiceRenderer( const wxArrayString
&choices
,
342 wxDataViewCellMode mode
= wxDATAVIEW_CELL_EDITABLE
,
343 int alignment
= wxDVR_DEFAULT_ALIGNMENT
);
344 virtual bool HasEditorCtrl() const { return true; }
345 virtual wxControl
* CreateEditorCtrl( wxWindow
*parent
, wxRect labelRect
, const wxVariant
&value
);
346 virtual bool GetValueFromEditorCtrl( wxControl
* editor
, wxVariant
&value
);
347 virtual bool Render( wxRect rect
, wxDC
*dc
, int state
);
348 virtual wxSize
GetSize() const;
349 virtual bool SetValue( const wxVariant
&value
);
350 virtual bool GetValue( wxVariant
&value
) const;
352 wxString
GetChoice(size_t index
) const { return m_choices
[index
]; }
353 const wxArrayString
& GetChoices() const { return m_choices
; }
356 wxArrayString m_choices
;
360 // ----------------------------------------------------------------------------
361 // wxDataViewChoiceByIndexRenderer
362 // ----------------------------------------------------------------------------
364 class WXDLLIMPEXP_ADV wxDataViewChoiceByIndexRenderer
: public wxDataViewChoiceRenderer
367 wxDataViewChoiceByIndexRenderer( const wxArrayString
&choices
,
368 wxDataViewCellMode mode
= wxDATAVIEW_CELL_EDITABLE
,
369 int alignment
= wxDVR_DEFAULT_ALIGNMENT
);
371 virtual wxControl
* CreateEditorCtrl( wxWindow
*parent
, wxRect labelRect
, const wxVariant
&value
);
372 virtual bool GetValueFromEditorCtrl( wxControl
* editor
, wxVariant
&value
);
374 virtual bool SetValue( const wxVariant
&value
);
375 virtual bool GetValue( wxVariant
&value
) const;
379 #endif // generic or Carbon versions
381 // this class is obsolete, its functionality was merged in
382 // wxDataViewTextRenderer itself now, don't use it any more
383 #define wxDataViewTextRendererAttr wxDataViewTextRenderer
385 #endif // _WX_DVRENDERERS_H_