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
; }
57 bool IsSameAs(const wxDataViewIconText
& other
) const
59 return m_text
== other
.m_text
&& m_icon
.IsSameAs(other
.m_icon
);
62 bool operator==(const wxDataViewIconText
& other
) const
64 return IsSameAs(other
);
67 bool operator!=(const wxDataViewIconText
& other
) const
69 return !IsSameAs(other
);
76 DECLARE_DYNAMIC_CLASS(wxDataViewIconText
)
79 DECLARE_VARIANT_OBJECT_EXPORTED(wxDataViewIconText
, WXDLLIMPEXP_ADV
)
81 // ----------------------------------------------------------------------------
82 // wxDataViewRendererBase
83 // ----------------------------------------------------------------------------
85 enum wxDataViewCellMode
87 wxDATAVIEW_CELL_INERT
,
88 wxDATAVIEW_CELL_ACTIVATABLE
,
89 wxDATAVIEW_CELL_EDITABLE
92 enum wxDataViewCellRenderState
94 wxDATAVIEW_CELL_SELECTED
= 1,
95 wxDATAVIEW_CELL_PRELIT
= 2,
96 wxDATAVIEW_CELL_INSENSITIVE
= 4,
97 wxDATAVIEW_CELL_FOCUSED
= 8
100 class WXDLLIMPEXP_ADV wxDataViewRendererBase
: public wxObject
103 wxDataViewRendererBase( const wxString
&varianttype
,
104 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
,
105 int alignment
= wxDVR_DEFAULT_ALIGNMENT
);
106 virtual ~wxDataViewRendererBase();
108 virtual bool Validate( wxVariant
& WXUNUSED(value
) )
111 void SetOwner( wxDataViewColumn
*owner
) { m_owner
= owner
; }
112 wxDataViewColumn
* GetOwner() const { return m_owner
; }
114 // renderer value and attributes: SetValue() and SetAttr() are called
115 // before a cell is rendered using this renderer
116 virtual bool SetValue(const wxVariant
& value
) = 0;
117 virtual bool GetValue(wxVariant
& value
) const = 0;
119 virtual void SetAttr(const wxDataViewItemAttr
& WXUNUSED(attr
)) { }
121 virtual void SetEnabled(bool WXUNUSED(enabled
)) { }
123 wxString
GetVariantType() const { return m_variantType
; }
125 // helper that calls SetValue and SetAttr:
126 void PrepareForItem(const wxDataViewModel
*model
,
127 const wxDataViewItem
& item
, unsigned column
);
129 // renderer properties:
130 virtual void SetMode( wxDataViewCellMode mode
) = 0;
131 virtual wxDataViewCellMode
GetMode() const = 0;
133 // NOTE: Set/GetAlignment do not take/return a wxAlignment enum but
134 // rather an "int"; that's because for rendering cells it's allowed
135 // to combine alignment flags (e.g. wxALIGN_LEFT|wxALIGN_BOTTOM)
136 virtual void SetAlignment( int align
) = 0;
137 virtual int GetAlignment() const = 0;
139 // enable or disable (if called with wxELLIPSIZE_NONE) replacing parts of
140 // the item text (hence this only makes sense for renderers showing
141 // text...) with ellipsis in order to make it fit the column width
142 virtual void EnableEllipsize(wxEllipsizeMode mode
= wxELLIPSIZE_MIDDLE
) = 0;
143 void DisableEllipsize() { EnableEllipsize(wxELLIPSIZE_NONE
); }
145 virtual wxEllipsizeMode
GetEllipsizeMode() const = 0;
148 virtual bool HasEditorCtrl() const
150 virtual wxWindow
* CreateEditorCtrl(wxWindow
* WXUNUSED(parent
),
151 wxRect
WXUNUSED(labelRect
),
152 const wxVariant
& WXUNUSED(value
))
154 virtual bool GetValueFromEditorCtrl(wxWindow
* WXUNUSED(editor
),
155 wxVariant
& WXUNUSED(value
))
158 virtual bool StartEditing( const wxDataViewItem
&item
, wxRect labelRect
);
159 virtual void CancelEditing();
160 virtual bool FinishEditing();
162 wxWindow
*GetEditorCtrl() { return m_editorCtrl
; }
164 virtual bool IsCustomRenderer() const { return false; }
168 // Called from {Cancel,Finish}Editing() to cleanup m_editorCtrl
169 void DestroyEditControl();
171 wxString m_variantType
;
172 wxDataViewColumn
*m_owner
;
173 wxWeakRef
<wxWindow
> m_editorCtrl
;
174 wxDataViewItem m_item
; // for m_editorCtrl
177 const wxDataViewCtrl
* GetView() const;
180 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewRendererBase
)
183 // include the real wxDataViewRenderer declaration for the native ports
184 #ifdef wxHAS_GENERIC_DATAVIEWCTRL
185 // in the generic implementation there is no real wxDataViewRenderer, all
186 // renderers are custom so it's the same as wxDataViewCustomRenderer and
187 // wxDataViewCustomRendererBase derives from wxDataViewRendererBase directly
189 // this is a rather ugly hack but unfortunately it just doesn't seem to be
190 // possible to have the same class hierarchy in all ports and avoid
191 // duplicating the entire wxDataViewCustomRendererBase in the generic
192 // wxDataViewRenderer class (well, we could use a mix-in but this would
193 // make classes hierarchy non linear and arguably even more complex)
194 #define wxDataViewCustomRendererRealBase wxDataViewRendererBase
196 #if defined(__WXGTK20__)
197 #include "wx/gtk/dvrenderer.h"
198 #elif defined(__WXMAC__)
199 #include "wx/osx/dvrenderer.h"
201 #error "unknown native wxDataViewCtrl implementation"
203 #define wxDataViewCustomRendererRealBase wxDataViewRenderer
206 // ----------------------------------------------------------------------------
207 // wxDataViewCustomRendererBase
208 // ----------------------------------------------------------------------------
210 class WXDLLIMPEXP_ADV wxDataViewCustomRendererBase
211 : public wxDataViewCustomRendererRealBase
214 // Constructor must specify the usual renderer parameters which we simply
215 // pass to the base class
216 wxDataViewCustomRendererBase(const wxString
& varianttype
= "string",
217 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
,
218 int align
= wxDVR_DEFAULT_ALIGNMENT
)
219 : wxDataViewCustomRendererRealBase(varianttype
, mode
, align
)
224 // Render the item using the current value (returned by GetValue()).
225 virtual bool Render(wxRect cell
, wxDC
*dc
, int state
) = 0;
227 // Return the size of the item appropriate to its current value.
228 virtual wxSize
GetSize() const = 0;
230 // Define virtual function which are called when the item is activated
231 // (double-clicked or Enter is pressed on it), clicked or the user starts
232 // to drag it: by default they all simply return false indicating that the
233 // events are not handled
235 virtual bool Activate(const wxRect
& WXUNUSED(cell
),
236 wxDataViewModel
*WXUNUSED(model
),
237 const wxDataViewItem
& WXUNUSED(item
),
238 unsigned int WXUNUSED(col
))
241 virtual bool LeftClick(const wxPoint
& WXUNUSED(cursor
),
242 const wxRect
& WXUNUSED(cell
),
243 wxDataViewModel
*WXUNUSED(model
),
244 const wxDataViewItem
& WXUNUSED(item
),
245 unsigned int WXUNUSED(col
) )
248 virtual bool StartDrag(const wxPoint
& WXUNUSED(cursor
),
249 const wxRect
& WXUNUSED(cell
),
250 wxDataViewModel
*WXUNUSED(model
),
251 const wxDataViewItem
& WXUNUSED(item
),
252 unsigned int WXUNUSED(col
) )
256 // Helper which can be used by Render() implementation in the derived
257 // classes: it will draw the text in the same manner as the standard
259 virtual void RenderText(const wxString
& text
,
266 // Override the base class virtual method to simply store the attribute so
267 // that it can be accessed using GetAttr() from Render() if needed.
268 virtual void SetAttr(const wxDataViewItemAttr
& attr
) { m_attr
= attr
; }
269 const wxDataViewItemAttr
& GetAttr() const { return m_attr
; }
271 // Store the enabled state of the item so that it can be accessed from
272 // Render() via GetEnabled() if needed.
273 virtual void SetEnabled(bool enabled
) { m_enabled
= enabled
; }
274 bool GetEnabled() const { return m_enabled
; }
277 // Implementation only from now on
279 // Retrieve the DC to use for drawing. This is implemented in derived
280 // platform-specific classes.
281 virtual wxDC
*GetDC() = 0;
283 // Prepare DC to use attributes and call Render().
284 void WXCallRender(wxRect rect
, wxDC
*dc
, int state
);
286 virtual bool IsCustomRenderer() const { return true; }
289 // helper for GetSize() implementations, respects attributes
290 wxSize
GetTextExtent(const wxString
& str
) const;
293 wxDataViewItemAttr m_attr
;
296 wxDECLARE_NO_COPY_CLASS(wxDataViewCustomRendererBase
);
299 // include the declaration of all the other renderers to get the real
300 // wxDataViewCustomRenderer from which we need to inherit below
301 #ifdef wxHAS_GENERIC_DATAVIEWCTRL
302 // because of the different renderer classes hierarchy in the generic
303 // version, as explained above, we can include the header defining
304 // wxDataViewRenderer only here and not before wxDataViewCustomRendererBase
305 // declaration as for the native ports
306 #include "wx/generic/dvrenderer.h"
307 #include "wx/generic/dvrenderers.h"
308 #elif defined(__WXGTK20__)
309 #include "wx/gtk/dvrenderers.h"
310 #elif defined(__WXMAC__)
311 #include "wx/osx/dvrenderers.h"
313 #error "unknown native wxDataViewCtrl implementation"
316 // ----------------------------------------------------------------------------
317 // wxDataViewSpinRenderer
318 // ----------------------------------------------------------------------------
320 class WXDLLIMPEXP_ADV wxDataViewSpinRenderer
: public wxDataViewCustomRenderer
323 wxDataViewSpinRenderer( int min
, int max
,
324 wxDataViewCellMode mode
= wxDATAVIEW_CELL_EDITABLE
,
325 int alignment
= wxDVR_DEFAULT_ALIGNMENT
);
326 virtual bool HasEditorCtrl() const { return true; }
327 virtual wxWindow
* CreateEditorCtrl( wxWindow
*parent
, wxRect labelRect
, const wxVariant
&value
);
328 virtual bool GetValueFromEditorCtrl( wxWindow
* editor
, wxVariant
&value
);
329 virtual bool Render( wxRect rect
, wxDC
*dc
, int state
);
330 virtual wxSize
GetSize() const;
331 virtual bool SetValue( const wxVariant
&value
);
332 virtual bool GetValue( wxVariant
&value
) const;
339 #if defined(wxHAS_GENERIC_DATAVIEWCTRL) || defined(__WXOSX_CARBON__)
341 // ----------------------------------------------------------------------------
342 // wxDataViewChoiceRenderer
343 // ----------------------------------------------------------------------------
345 class WXDLLIMPEXP_ADV wxDataViewChoiceRenderer
: public wxDataViewCustomRenderer
348 wxDataViewChoiceRenderer( const wxArrayString
&choices
,
349 wxDataViewCellMode mode
= wxDATAVIEW_CELL_EDITABLE
,
350 int alignment
= wxDVR_DEFAULT_ALIGNMENT
);
351 virtual bool HasEditorCtrl() const { return true; }
352 virtual wxWindow
* CreateEditorCtrl( wxWindow
*parent
, wxRect labelRect
, const wxVariant
&value
);
353 virtual bool GetValueFromEditorCtrl( wxWindow
* editor
, wxVariant
&value
);
354 virtual bool Render( wxRect rect
, wxDC
*dc
, int state
);
355 virtual wxSize
GetSize() const;
356 virtual bool SetValue( const wxVariant
&value
);
357 virtual bool GetValue( wxVariant
&value
) const;
359 wxString
GetChoice(size_t index
) const { return m_choices
[index
]; }
360 const wxArrayString
& GetChoices() const { return m_choices
; }
363 wxArrayString m_choices
;
367 // ----------------------------------------------------------------------------
368 // wxDataViewChoiceByIndexRenderer
369 // ----------------------------------------------------------------------------
371 class WXDLLIMPEXP_ADV wxDataViewChoiceByIndexRenderer
: public wxDataViewChoiceRenderer
374 wxDataViewChoiceByIndexRenderer( const wxArrayString
&choices
,
375 wxDataViewCellMode mode
= wxDATAVIEW_CELL_EDITABLE
,
376 int alignment
= wxDVR_DEFAULT_ALIGNMENT
);
378 virtual wxWindow
* CreateEditorCtrl( wxWindow
*parent
, wxRect labelRect
, const wxVariant
&value
);
379 virtual bool GetValueFromEditorCtrl( wxWindow
* editor
, wxVariant
&value
);
381 virtual bool SetValue( const wxVariant
&value
);
382 virtual bool GetValue( wxVariant
&value
) const;
386 #endif // generic or Carbon versions
388 // this class is obsolete, its functionality was merged in
389 // wxDataViewTextRenderer itself now, don't use it any more
390 #define wxDataViewTextRendererAttr wxDataViewTextRenderer
392 #endif // _WX_DVRENDERERS_H_