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 // Copyright: (c) 2006 Robert Roebling
7 // (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_DVRENDERERS_H_
12 #define _WX_DVRENDERERS_H_
15 Note about the structure of various headers: they're organized in a more
16 complicated way than usual because of the various dependencies which are
17 different for different ports. In any case the only public header, i.e. the
18 one which can be included directly is wx/dataview.h. It, in turn, includes
19 this one to define all the renderer classes.
21 We define the base wxDataViewRendererBase class first and then include a
22 port-dependent wx/xxx/dvrenderer.h which defines wxDataViewRenderer itself.
23 After this we can define wxDataViewRendererCustomBase (and maybe in the
24 future base classes for other renderers if the need arises, i.e. if there
25 is any non-trivial code or API which it makes sense to keep in common code)
26 and include wx/xxx/dvrenderers.h (notice the plural) which defines all the
27 rest of the renderer classes.
30 class WXDLLIMPEXP_FWD_ADV wxDataViewCustomRenderer
;
32 // ----------------------------------------------------------------------------
33 // wxDataViewIconText: helper class used by wxDataViewIconTextRenderer
34 // ----------------------------------------------------------------------------
36 class WXDLLIMPEXP_ADV wxDataViewIconText
: public wxObject
39 wxDataViewIconText( const wxString
&text
= wxEmptyString
,
40 const wxIcon
& icon
= wxNullIcon
)
45 wxDataViewIconText( const wxDataViewIconText
&other
)
51 void SetText( const wxString
&text
) { m_text
= text
; }
52 wxString
GetText() const { return m_text
; }
53 void SetIcon( const wxIcon
&icon
) { m_icon
= icon
; }
54 const wxIcon
&GetIcon() const { return m_icon
; }
56 bool IsSameAs(const wxDataViewIconText
& other
) const
58 return m_text
== other
.m_text
&& m_icon
.IsSameAs(other
.m_icon
);
61 bool operator==(const wxDataViewIconText
& other
) const
63 return IsSameAs(other
);
66 bool operator!=(const wxDataViewIconText
& other
) const
68 return !IsSameAs(other
);
75 DECLARE_DYNAMIC_CLASS(wxDataViewIconText
)
78 DECLARE_VARIANT_OBJECT_EXPORTED(wxDataViewIconText
, WXDLLIMPEXP_ADV
)
80 // ----------------------------------------------------------------------------
81 // wxDataViewRendererBase
82 // ----------------------------------------------------------------------------
84 enum wxDataViewCellMode
86 wxDATAVIEW_CELL_INERT
,
87 wxDATAVIEW_CELL_ACTIVATABLE
,
88 wxDATAVIEW_CELL_EDITABLE
91 enum wxDataViewCellRenderState
93 wxDATAVIEW_CELL_SELECTED
= 1,
94 wxDATAVIEW_CELL_PRELIT
= 2,
95 wxDATAVIEW_CELL_INSENSITIVE
= 4,
96 wxDATAVIEW_CELL_FOCUSED
= 8
99 class WXDLLIMPEXP_ADV wxDataViewRendererBase
: public wxObject
102 wxDataViewRendererBase( const wxString
&varianttype
,
103 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
,
104 int alignment
= wxDVR_DEFAULT_ALIGNMENT
);
105 virtual ~wxDataViewRendererBase();
107 virtual bool Validate( wxVariant
& WXUNUSED(value
) )
110 void SetOwner( wxDataViewColumn
*owner
) { m_owner
= owner
; }
111 wxDataViewColumn
* GetOwner() const { return m_owner
; }
113 // renderer value and attributes: SetValue() and SetAttr() are called
114 // before a cell is rendered using this renderer
115 virtual bool SetValue(const wxVariant
& value
) = 0;
116 virtual bool GetValue(wxVariant
& value
) const = 0;
118 virtual void SetAttr(const wxDataViewItemAttr
& WXUNUSED(attr
)) { }
120 virtual void SetEnabled(bool WXUNUSED(enabled
)) { }
122 wxString
GetVariantType() const { return m_variantType
; }
124 // helper that calls SetValue and SetAttr:
125 void PrepareForItem(const wxDataViewModel
*model
,
126 const wxDataViewItem
& item
, unsigned column
);
128 // renderer properties:
129 virtual void SetMode( wxDataViewCellMode mode
) = 0;
130 virtual wxDataViewCellMode
GetMode() const = 0;
132 // NOTE: Set/GetAlignment do not take/return a wxAlignment enum but
133 // rather an "int"; that's because for rendering cells it's allowed
134 // to combine alignment flags (e.g. wxALIGN_LEFT|wxALIGN_BOTTOM)
135 virtual void SetAlignment( int align
) = 0;
136 virtual int GetAlignment() const = 0;
138 // enable or disable (if called with wxELLIPSIZE_NONE) replacing parts of
139 // the item text (hence this only makes sense for renderers showing
140 // text...) with ellipsis in order to make it fit the column width
141 virtual void EnableEllipsize(wxEllipsizeMode mode
= wxELLIPSIZE_MIDDLE
) = 0;
142 void DisableEllipsize() { EnableEllipsize(wxELLIPSIZE_NONE
); }
144 virtual wxEllipsizeMode
GetEllipsizeMode() const = 0;
147 virtual bool HasEditorCtrl() const
149 virtual wxWindow
* CreateEditorCtrl(wxWindow
* WXUNUSED(parent
),
150 wxRect
WXUNUSED(labelRect
),
151 const wxVariant
& WXUNUSED(value
))
153 virtual bool GetValueFromEditorCtrl(wxWindow
* WXUNUSED(editor
),
154 wxVariant
& WXUNUSED(value
))
157 virtual bool StartEditing( const wxDataViewItem
&item
, wxRect labelRect
);
158 virtual void CancelEditing();
159 virtual bool FinishEditing();
161 wxWindow
*GetEditorCtrl() { return m_editorCtrl
; }
163 virtual bool IsCustomRenderer() const { return false; }
167 // Called from {Cancel,Finish}Editing() to cleanup m_editorCtrl
168 void DestroyEditControl();
170 wxString m_variantType
;
171 wxDataViewColumn
*m_owner
;
172 wxWeakRef
<wxWindow
> m_editorCtrl
;
173 wxDataViewItem m_item
; // for m_editorCtrl
175 // internal utility, may be used anywhere the window associated with the
176 // renderer is required
177 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 a key is pressed on the
231 // item, clicked or the user starts to drag it: by default they all simply
232 // return false indicating that the events are not handled
234 virtual bool ActivateCell(const wxRect
& cell
,
235 wxDataViewModel
*model
,
236 const wxDataViewItem
& item
,
238 const wxMouseEvent
* mouseEvent
);
240 // Deprecated, use (and override) ActivateCell() instead
241 wxDEPRECATED_BUT_USED_INTERNALLY_INLINE(
242 virtual bool Activate(wxRect
WXUNUSED(cell
),
243 wxDataViewModel
*WXUNUSED(model
),
244 const wxDataViewItem
& WXUNUSED(item
),
245 unsigned int WXUNUSED(col
)),
248 // Deprecated, use (and override) ActivateCell() instead
249 wxDEPRECATED_BUT_USED_INTERNALLY_INLINE(
250 virtual bool LeftClick(wxPoint
WXUNUSED(cursor
),
251 wxRect
WXUNUSED(cell
),
252 wxDataViewModel
*WXUNUSED(model
),
253 const wxDataViewItem
& WXUNUSED(item
),
254 unsigned int WXUNUSED(col
)),
257 virtual bool StartDrag(const wxPoint
& WXUNUSED(cursor
),
258 const wxRect
& WXUNUSED(cell
),
259 wxDataViewModel
*WXUNUSED(model
),
260 const wxDataViewItem
& WXUNUSED(item
),
261 unsigned int WXUNUSED(col
) )
265 // Helper which can be used by Render() implementation in the derived
266 // classes: it will draw the text in the same manner as the standard
268 virtual void RenderText(const wxString
& text
,
275 // Override the base class virtual method to simply store the attribute so
276 // that it can be accessed using GetAttr() from Render() if needed.
277 virtual void SetAttr(const wxDataViewItemAttr
& attr
) { m_attr
= attr
; }
278 const wxDataViewItemAttr
& GetAttr() const { return m_attr
; }
280 // Store the enabled state of the item so that it can be accessed from
281 // Render() via GetEnabled() if needed.
282 virtual void SetEnabled(bool enabled
) { m_enabled
= enabled
; }
283 bool GetEnabled() const { return m_enabled
; }
286 // Implementation only from now on
288 // Retrieve the DC to use for drawing. This is implemented in derived
289 // platform-specific classes.
290 virtual wxDC
*GetDC() = 0;
292 // To draw background use the background colour in wxDataViewItemAttr
293 virtual void RenderBackground(wxDC
* dc
, const wxRect
& rect
);
295 // Prepare DC to use attributes and call Render().
296 void WXCallRender(wxRect rect
, wxDC
*dc
, int state
);
298 virtual bool IsCustomRenderer() const { return true; }
301 // helper for GetSize() implementations, respects attributes
302 wxSize
GetTextExtent(const wxString
& str
) const;
305 wxDataViewItemAttr m_attr
;
308 wxDECLARE_NO_COPY_CLASS(wxDataViewCustomRendererBase
);
311 // include the declaration of all the other renderers to get the real
312 // wxDataViewCustomRenderer from which we need to inherit below
313 #ifdef wxHAS_GENERIC_DATAVIEWCTRL
314 // because of the different renderer classes hierarchy in the generic
315 // version, as explained above, we can include the header defining
316 // wxDataViewRenderer only here and not before wxDataViewCustomRendererBase
317 // declaration as for the native ports
318 #include "wx/generic/dvrenderer.h"
319 #include "wx/generic/dvrenderers.h"
320 #elif defined(__WXGTK20__)
321 #include "wx/gtk/dvrenderers.h"
322 #elif defined(__WXMAC__)
323 #include "wx/osx/dvrenderers.h"
325 #error "unknown native wxDataViewCtrl implementation"
328 // ----------------------------------------------------------------------------
329 // wxDataViewSpinRenderer
330 // ----------------------------------------------------------------------------
332 class WXDLLIMPEXP_ADV wxDataViewSpinRenderer
: public wxDataViewCustomRenderer
335 wxDataViewSpinRenderer( int min
, int max
,
336 wxDataViewCellMode mode
= wxDATAVIEW_CELL_EDITABLE
,
337 int alignment
= wxDVR_DEFAULT_ALIGNMENT
);
338 virtual bool HasEditorCtrl() const { return true; }
339 virtual wxWindow
* CreateEditorCtrl( wxWindow
*parent
, wxRect labelRect
, const wxVariant
&value
);
340 virtual bool GetValueFromEditorCtrl( wxWindow
* editor
, wxVariant
&value
);
341 virtual bool Render( wxRect rect
, wxDC
*dc
, int state
);
342 virtual wxSize
GetSize() const;
343 virtual bool SetValue( const wxVariant
&value
);
344 virtual bool GetValue( wxVariant
&value
) const;
351 #if defined(wxHAS_GENERIC_DATAVIEWCTRL) || defined(__WXOSX_CARBON__)
353 // ----------------------------------------------------------------------------
354 // wxDataViewChoiceRenderer
355 // ----------------------------------------------------------------------------
357 class WXDLLIMPEXP_ADV wxDataViewChoiceRenderer
: public wxDataViewCustomRenderer
360 wxDataViewChoiceRenderer( const wxArrayString
&choices
,
361 wxDataViewCellMode mode
= wxDATAVIEW_CELL_EDITABLE
,
362 int alignment
= wxDVR_DEFAULT_ALIGNMENT
);
363 virtual bool HasEditorCtrl() const { return true; }
364 virtual wxWindow
* CreateEditorCtrl( wxWindow
*parent
, wxRect labelRect
, const wxVariant
&value
);
365 virtual bool GetValueFromEditorCtrl( wxWindow
* editor
, wxVariant
&value
);
366 virtual bool Render( wxRect rect
, wxDC
*dc
, int state
);
367 virtual wxSize
GetSize() const;
368 virtual bool SetValue( const wxVariant
&value
);
369 virtual bool GetValue( wxVariant
&value
) const;
371 wxString
GetChoice(size_t index
) const { return m_choices
[index
]; }
372 const wxArrayString
& GetChoices() const { return m_choices
; }
375 wxArrayString m_choices
;
379 // ----------------------------------------------------------------------------
380 // wxDataViewChoiceByIndexRenderer
381 // ----------------------------------------------------------------------------
383 class WXDLLIMPEXP_ADV wxDataViewChoiceByIndexRenderer
: public wxDataViewChoiceRenderer
386 wxDataViewChoiceByIndexRenderer( const wxArrayString
&choices
,
387 wxDataViewCellMode mode
= wxDATAVIEW_CELL_EDITABLE
,
388 int alignment
= wxDVR_DEFAULT_ALIGNMENT
);
390 virtual wxWindow
* CreateEditorCtrl( wxWindow
*parent
, wxRect labelRect
, const wxVariant
&value
);
391 virtual bool GetValueFromEditorCtrl( wxWindow
* editor
, wxVariant
&value
);
393 virtual bool SetValue( const wxVariant
&value
);
394 virtual bool GetValue( wxVariant
&value
) const;
398 #endif // generic or Carbon versions
400 #if defined(wxHAS_GENERIC_DATAVIEWCTRL) || defined(__WXGTK__)
402 // ----------------------------------------------------------------------------
403 // wxDataViewDateRenderer
404 // ----------------------------------------------------------------------------
406 #if wxUSE_DATEPICKCTRL
407 class WXDLLIMPEXP_ADV wxDataViewDateRenderer
: public wxDataViewCustomRenderer
410 wxDataViewDateRenderer(const wxString
&varianttype
= wxT("datetime"),
411 wxDataViewCellMode mode
= wxDATAVIEW_CELL_EDITABLE
,
412 int align
= wxDVR_DEFAULT_ALIGNMENT
);
414 virtual bool HasEditorCtrl() const { return true; }
415 virtual wxWindow
*CreateEditorCtrl(wxWindow
*parent
, wxRect labelRect
, const wxVariant
&value
);
416 virtual bool GetValueFromEditorCtrl(wxWindow
* editor
, wxVariant
&value
);
417 virtual bool SetValue(const wxVariant
&value
);
418 virtual bool GetValue(wxVariant
& value
) const;
419 virtual bool Render( wxRect cell
, wxDC
*dc
, int state
);
420 virtual wxSize
GetSize() const;
425 #else // !wxUSE_DATEPICKCTRL
426 typedef wxDataViewTextRenderer wxDataViewDateRenderer
;
429 #endif // generic or GTK+ versions
431 // this class is obsolete, its functionality was merged in
432 // wxDataViewTextRenderer itself now, don't use it any more
433 #define wxDataViewTextRendererAttr wxDataViewTextRenderer
435 #endif // _WX_DVRENDERERS_H_