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 // Return the alignment of this renderer if it's specified (i.e. has value
171 // different from the default wxDVR_DEFAULT_ALIGNMENT) or the alignment of
172 // the column it is used for otherwise.
174 // Unlike GetAlignment(), this always returns a valid combination of
175 // wxALIGN_XXX flags (although possibly wxALIGN_NOT) and never returns
176 // wxDVR_DEFAULT_ALIGNMENT.
177 int GetEffectiveAlignment() const;
179 wxString m_variantType
;
180 wxDataViewColumn
*m_owner
;
181 wxWeakRef
<wxWindow
> m_editorCtrl
;
182 wxDataViewItem m_item
; // for m_editorCtrl
184 // internal utility, may be used anywhere the window associated with the
185 // renderer is required
186 wxDataViewCtrl
* GetView() const;
189 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewRendererBase
)
192 // include the real wxDataViewRenderer declaration for the native ports
193 #ifdef wxHAS_GENERIC_DATAVIEWCTRL
194 // in the generic implementation there is no real wxDataViewRenderer, all
195 // renderers are custom so it's the same as wxDataViewCustomRenderer and
196 // wxDataViewCustomRendererBase derives from wxDataViewRendererBase directly
198 // this is a rather ugly hack but unfortunately it just doesn't seem to be
199 // possible to have the same class hierarchy in all ports and avoid
200 // duplicating the entire wxDataViewCustomRendererBase in the generic
201 // wxDataViewRenderer class (well, we could use a mix-in but this would
202 // make classes hierarchy non linear and arguably even more complex)
203 #define wxDataViewCustomRendererRealBase wxDataViewRendererBase
205 #if defined(__WXGTK20__)
206 #include "wx/gtk/dvrenderer.h"
207 #elif defined(__WXMAC__)
208 #include "wx/osx/dvrenderer.h"
210 #error "unknown native wxDataViewCtrl implementation"
212 #define wxDataViewCustomRendererRealBase wxDataViewRenderer
215 // ----------------------------------------------------------------------------
216 // wxDataViewCustomRendererBase
217 // ----------------------------------------------------------------------------
219 class WXDLLIMPEXP_ADV wxDataViewCustomRendererBase
220 : public wxDataViewCustomRendererRealBase
223 // Constructor must specify the usual renderer parameters which we simply
224 // pass to the base class
225 wxDataViewCustomRendererBase(const wxString
& varianttype
= "string",
226 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
,
227 int align
= wxDVR_DEFAULT_ALIGNMENT
)
228 : wxDataViewCustomRendererRealBase(varianttype
, mode
, align
)
233 // Render the item using the current value (returned by GetValue()).
234 virtual bool Render(wxRect cell
, wxDC
*dc
, int state
) = 0;
236 // Return the size of the item appropriate to its current value.
237 virtual wxSize
GetSize() const = 0;
239 // Define virtual function which are called when a key is pressed on the
240 // item, clicked or the user starts to drag it: by default they all simply
241 // return false indicating that the events are not handled
243 virtual bool ActivateCell(const wxRect
& cell
,
244 wxDataViewModel
*model
,
245 const wxDataViewItem
& item
,
247 const wxMouseEvent
* mouseEvent
);
249 // Deprecated, use (and override) ActivateCell() instead
250 wxDEPRECATED_BUT_USED_INTERNALLY_INLINE(
251 virtual bool Activate(wxRect
WXUNUSED(cell
),
252 wxDataViewModel
*WXUNUSED(model
),
253 const wxDataViewItem
& WXUNUSED(item
),
254 unsigned int WXUNUSED(col
)),
257 // Deprecated, use (and override) ActivateCell() instead
258 wxDEPRECATED_BUT_USED_INTERNALLY_INLINE(
259 virtual bool LeftClick(wxPoint
WXUNUSED(cursor
),
260 wxRect
WXUNUSED(cell
),
261 wxDataViewModel
*WXUNUSED(model
),
262 const wxDataViewItem
& WXUNUSED(item
),
263 unsigned int WXUNUSED(col
)),
266 virtual bool StartDrag(const wxPoint
& WXUNUSED(cursor
),
267 const wxRect
& WXUNUSED(cell
),
268 wxDataViewModel
*WXUNUSED(model
),
269 const wxDataViewItem
& WXUNUSED(item
),
270 unsigned int WXUNUSED(col
) )
274 // Helper which can be used by Render() implementation in the derived
275 // classes: it will draw the text in the same manner as the standard
277 virtual void RenderText(const wxString
& text
,
284 // Override the base class virtual method to simply store the attribute so
285 // that it can be accessed using GetAttr() from Render() if needed.
286 virtual void SetAttr(const wxDataViewItemAttr
& attr
) { m_attr
= attr
; }
287 const wxDataViewItemAttr
& GetAttr() const { return m_attr
; }
289 // Store the enabled state of the item so that it can be accessed from
290 // Render() via GetEnabled() if needed.
291 virtual void SetEnabled(bool enabled
) { m_enabled
= enabled
; }
292 bool GetEnabled() const { return m_enabled
; }
295 // Implementation only from now on
297 // Retrieve the DC to use for drawing. This is implemented in derived
298 // platform-specific classes.
299 virtual wxDC
*GetDC() = 0;
301 // To draw background use the background colour in wxDataViewItemAttr
302 virtual void RenderBackground(wxDC
* dc
, const wxRect
& rect
);
304 // Prepare DC to use attributes and call Render().
305 void WXCallRender(wxRect rect
, wxDC
*dc
, int state
);
307 virtual bool IsCustomRenderer() const { return true; }
310 // helper for GetSize() implementations, respects attributes
311 wxSize
GetTextExtent(const wxString
& str
) const;
314 wxDataViewItemAttr m_attr
;
317 wxDECLARE_NO_COPY_CLASS(wxDataViewCustomRendererBase
);
320 // include the declaration of all the other renderers to get the real
321 // wxDataViewCustomRenderer from which we need to inherit below
322 #ifdef wxHAS_GENERIC_DATAVIEWCTRL
323 // because of the different renderer classes hierarchy in the generic
324 // version, as explained above, we can include the header defining
325 // wxDataViewRenderer only here and not before wxDataViewCustomRendererBase
326 // declaration as for the native ports
327 #include "wx/generic/dvrenderer.h"
328 #include "wx/generic/dvrenderers.h"
329 #elif defined(__WXGTK20__)
330 #include "wx/gtk/dvrenderers.h"
331 #elif defined(__WXMAC__)
332 #include "wx/osx/dvrenderers.h"
334 #error "unknown native wxDataViewCtrl implementation"
337 // ----------------------------------------------------------------------------
338 // wxDataViewSpinRenderer
339 // ----------------------------------------------------------------------------
341 class WXDLLIMPEXP_ADV wxDataViewSpinRenderer
: public wxDataViewCustomRenderer
344 wxDataViewSpinRenderer( int min
, int max
,
345 wxDataViewCellMode mode
= wxDATAVIEW_CELL_EDITABLE
,
346 int alignment
= wxDVR_DEFAULT_ALIGNMENT
);
347 virtual bool HasEditorCtrl() const { return true; }
348 virtual wxWindow
* CreateEditorCtrl( wxWindow
*parent
, wxRect labelRect
, const wxVariant
&value
);
349 virtual bool GetValueFromEditorCtrl( wxWindow
* editor
, wxVariant
&value
);
350 virtual bool Render( wxRect rect
, wxDC
*dc
, int state
);
351 virtual wxSize
GetSize() const;
352 virtual bool SetValue( const wxVariant
&value
);
353 virtual bool GetValue( wxVariant
&value
) const;
360 #if defined(wxHAS_GENERIC_DATAVIEWCTRL) || defined(__WXOSX_CARBON__)
362 // ----------------------------------------------------------------------------
363 // wxDataViewChoiceRenderer
364 // ----------------------------------------------------------------------------
366 class WXDLLIMPEXP_ADV wxDataViewChoiceRenderer
: public wxDataViewCustomRenderer
369 wxDataViewChoiceRenderer( const wxArrayString
&choices
,
370 wxDataViewCellMode mode
= wxDATAVIEW_CELL_EDITABLE
,
371 int alignment
= wxDVR_DEFAULT_ALIGNMENT
);
372 virtual bool HasEditorCtrl() const { return true; }
373 virtual wxWindow
* CreateEditorCtrl( wxWindow
*parent
, wxRect labelRect
, const wxVariant
&value
);
374 virtual bool GetValueFromEditorCtrl( wxWindow
* editor
, wxVariant
&value
);
375 virtual bool Render( wxRect rect
, wxDC
*dc
, int state
);
376 virtual wxSize
GetSize() const;
377 virtual bool SetValue( const wxVariant
&value
);
378 virtual bool GetValue( wxVariant
&value
) const;
380 wxString
GetChoice(size_t index
) const { return m_choices
[index
]; }
381 const wxArrayString
& GetChoices() const { return m_choices
; }
384 wxArrayString m_choices
;
388 // ----------------------------------------------------------------------------
389 // wxDataViewChoiceByIndexRenderer
390 // ----------------------------------------------------------------------------
392 class WXDLLIMPEXP_ADV wxDataViewChoiceByIndexRenderer
: public wxDataViewChoiceRenderer
395 wxDataViewChoiceByIndexRenderer( const wxArrayString
&choices
,
396 wxDataViewCellMode mode
= wxDATAVIEW_CELL_EDITABLE
,
397 int alignment
= wxDVR_DEFAULT_ALIGNMENT
);
399 virtual wxWindow
* CreateEditorCtrl( wxWindow
*parent
, wxRect labelRect
, const wxVariant
&value
);
400 virtual bool GetValueFromEditorCtrl( wxWindow
* editor
, wxVariant
&value
);
402 virtual bool SetValue( const wxVariant
&value
);
403 virtual bool GetValue( wxVariant
&value
) const;
407 #endif // generic or Carbon versions
409 #if defined(wxHAS_GENERIC_DATAVIEWCTRL) || defined(__WXGTK__)
411 // ----------------------------------------------------------------------------
412 // wxDataViewDateRenderer
413 // ----------------------------------------------------------------------------
415 #if wxUSE_DATEPICKCTRL
416 class WXDLLIMPEXP_ADV wxDataViewDateRenderer
: public wxDataViewCustomRenderer
419 wxDataViewDateRenderer(const wxString
&varianttype
= wxT("datetime"),
420 wxDataViewCellMode mode
= wxDATAVIEW_CELL_EDITABLE
,
421 int align
= wxDVR_DEFAULT_ALIGNMENT
);
423 virtual bool HasEditorCtrl() const { return true; }
424 virtual wxWindow
*CreateEditorCtrl(wxWindow
*parent
, wxRect labelRect
, const wxVariant
&value
);
425 virtual bool GetValueFromEditorCtrl(wxWindow
* editor
, wxVariant
&value
);
426 virtual bool SetValue(const wxVariant
&value
);
427 virtual bool GetValue(wxVariant
& value
) const;
428 virtual bool Render( wxRect cell
, wxDC
*dc
, int state
);
429 virtual wxSize
GetSize() const;
434 #else // !wxUSE_DATEPICKCTRL
435 typedef wxDataViewTextRenderer wxDataViewDateRenderer
;
438 #endif // generic or GTK+ versions
440 // this class is obsolete, its functionality was merged in
441 // wxDataViewTextRenderer itself now, don't use it any more
442 #define wxDataViewTextRendererAttr wxDataViewTextRenderer
444 #endif // _WX_DVRENDERERS_H_