dvrenderers.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. ///////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_DVRENDERERS_H_
  11. #define _WX_DVRENDERERS_H_
  12. /*
  13. Note about the structure of various headers: they're organized in a more
  14. complicated way than usual because of the various dependencies which are
  15. different for different ports. In any case the only public header, i.e. the
  16. one which can be included directly is wx/dataview.h. It, in turn, includes
  17. this one to define all the renderer classes.
  18. We define the base wxDataViewRendererBase class first and then include a
  19. port-dependent wx/xxx/dvrenderer.h which defines wxDataViewRenderer itself.
  20. After this we can define wxDataViewRendererCustomBase (and maybe in the
  21. future base classes for other renderers if the need arises, i.e. if there
  22. is any non-trivial code or API which it makes sense to keep in common code)
  23. and include wx/xxx/dvrenderers.h (notice the plural) which defines all the
  24. rest of the renderer classes.
  25. */
  26. class WXDLLIMPEXP_FWD_ADV wxDataViewCustomRenderer;
  27. // ----------------------------------------------------------------------------
  28. // wxDataViewIconText: helper class used by wxDataViewIconTextRenderer
  29. // ----------------------------------------------------------------------------
  30. class WXDLLIMPEXP_ADV wxDataViewIconText : public wxObject
  31. {
  32. public:
  33. wxDataViewIconText( const wxString &text = wxEmptyString,
  34. const wxIcon& icon = wxNullIcon )
  35. : m_text(text),
  36. m_icon(icon)
  37. { }
  38. wxDataViewIconText( const wxDataViewIconText &other )
  39. : wxObject(),
  40. m_text(other.m_text),
  41. m_icon(other.m_icon)
  42. { }
  43. void SetText( const wxString &text ) { m_text = text; }
  44. wxString GetText() const { return m_text; }
  45. void SetIcon( const wxIcon &icon ) { m_icon = icon; }
  46. const wxIcon &GetIcon() const { return m_icon; }
  47. bool IsSameAs(const wxDataViewIconText& other) const
  48. {
  49. return m_text == other.m_text && m_icon.IsSameAs(other.m_icon);
  50. }
  51. bool operator==(const wxDataViewIconText& other) const
  52. {
  53. return IsSameAs(other);
  54. }
  55. bool operator!=(const wxDataViewIconText& other) const
  56. {
  57. return !IsSameAs(other);
  58. }
  59. private:
  60. wxString m_text;
  61. wxIcon m_icon;
  62. DECLARE_DYNAMIC_CLASS(wxDataViewIconText)
  63. };
  64. DECLARE_VARIANT_OBJECT_EXPORTED(wxDataViewIconText, WXDLLIMPEXP_ADV)
  65. // ----------------------------------------------------------------------------
  66. // wxDataViewRendererBase
  67. // ----------------------------------------------------------------------------
  68. enum wxDataViewCellMode
  69. {
  70. wxDATAVIEW_CELL_INERT,
  71. wxDATAVIEW_CELL_ACTIVATABLE,
  72. wxDATAVIEW_CELL_EDITABLE
  73. };
  74. enum wxDataViewCellRenderState
  75. {
  76. wxDATAVIEW_CELL_SELECTED = 1,
  77. wxDATAVIEW_CELL_PRELIT = 2,
  78. wxDATAVIEW_CELL_INSENSITIVE = 4,
  79. wxDATAVIEW_CELL_FOCUSED = 8
  80. };
  81. class WXDLLIMPEXP_ADV wxDataViewRendererBase: public wxObject
  82. {
  83. public:
  84. wxDataViewRendererBase( const wxString &varianttype,
  85. wxDataViewCellMode mode = wxDATAVIEW_CELL_INERT,
  86. int alignment = wxDVR_DEFAULT_ALIGNMENT );
  87. virtual ~wxDataViewRendererBase();
  88. virtual bool Validate( wxVariant& WXUNUSED(value) )
  89. { return true; }
  90. void SetOwner( wxDataViewColumn *owner ) { m_owner = owner; }
  91. wxDataViewColumn* GetOwner() const { return m_owner; }
  92. // renderer value and attributes: SetValue() and SetAttr() are called
  93. // before a cell is rendered using this renderer
  94. virtual bool SetValue(const wxVariant& value) = 0;
  95. virtual bool GetValue(wxVariant& value) const = 0;
  96. virtual void SetAttr(const wxDataViewItemAttr& WXUNUSED(attr)) { }
  97. virtual void SetEnabled(bool WXUNUSED(enabled)) { }
  98. wxString GetVariantType() const { return m_variantType; }
  99. // helper that calls SetValue and SetAttr:
  100. void PrepareForItem(const wxDataViewModel *model,
  101. const wxDataViewItem& item, unsigned column);
  102. // renderer properties:
  103. virtual void SetMode( wxDataViewCellMode mode ) = 0;
  104. virtual wxDataViewCellMode GetMode() const = 0;
  105. // NOTE: Set/GetAlignment do not take/return a wxAlignment enum but
  106. // rather an "int"; that's because for rendering cells it's allowed
  107. // to combine alignment flags (e.g. wxALIGN_LEFT|wxALIGN_BOTTOM)
  108. virtual void SetAlignment( int align ) = 0;
  109. virtual int GetAlignment() const = 0;
  110. // enable or disable (if called with wxELLIPSIZE_NONE) replacing parts of
  111. // the item text (hence this only makes sense for renderers showing
  112. // text...) with ellipsis in order to make it fit the column width
  113. virtual void EnableEllipsize(wxEllipsizeMode mode = wxELLIPSIZE_MIDDLE) = 0;
  114. void DisableEllipsize() { EnableEllipsize(wxELLIPSIZE_NONE); }
  115. virtual wxEllipsizeMode GetEllipsizeMode() const = 0;
  116. // in-place editing
  117. virtual bool HasEditorCtrl() const
  118. { return false; }
  119. virtual wxWindow* CreateEditorCtrl(wxWindow * WXUNUSED(parent),
  120. wxRect WXUNUSED(labelRect),
  121. const wxVariant& WXUNUSED(value))
  122. { return NULL; }
  123. virtual bool GetValueFromEditorCtrl(wxWindow * WXUNUSED(editor),
  124. wxVariant& WXUNUSED(value))
  125. { return false; }
  126. virtual bool StartEditing( const wxDataViewItem &item, wxRect labelRect );
  127. virtual void CancelEditing();
  128. virtual bool FinishEditing();
  129. wxWindow *GetEditorCtrl() { return m_editorCtrl; }
  130. virtual bool IsCustomRenderer() const { return false; }
  131. protected:
  132. // Called from {Cancel,Finish}Editing() to cleanup m_editorCtrl
  133. void DestroyEditControl();
  134. // Return the alignment of this renderer if it's specified (i.e. has value
  135. // different from the default wxDVR_DEFAULT_ALIGNMENT) or the alignment of
  136. // the column it is used for otherwise.
  137. //
  138. // Unlike GetAlignment(), this always returns a valid combination of
  139. // wxALIGN_XXX flags (although possibly wxALIGN_NOT) and never returns
  140. // wxDVR_DEFAULT_ALIGNMENT.
  141. int GetEffectiveAlignment() const;
  142. wxString m_variantType;
  143. wxDataViewColumn *m_owner;
  144. wxWeakRef<wxWindow> m_editorCtrl;
  145. wxDataViewItem m_item; // for m_editorCtrl
  146. // internal utility, may be used anywhere the window associated with the
  147. // renderer is required
  148. wxDataViewCtrl* GetView() const;
  149. protected:
  150. DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewRendererBase)
  151. };
  152. // include the real wxDataViewRenderer declaration for the native ports
  153. #ifdef wxHAS_GENERIC_DATAVIEWCTRL
  154. // in the generic implementation there is no real wxDataViewRenderer, all
  155. // renderers are custom so it's the same as wxDataViewCustomRenderer and
  156. // wxDataViewCustomRendererBase derives from wxDataViewRendererBase directly
  157. //
  158. // this is a rather ugly hack but unfortunately it just doesn't seem to be
  159. // possible to have the same class hierarchy in all ports and avoid
  160. // duplicating the entire wxDataViewCustomRendererBase in the generic
  161. // wxDataViewRenderer class (well, we could use a mix-in but this would
  162. // make classes hierarchy non linear and arguably even more complex)
  163. #define wxDataViewCustomRendererRealBase wxDataViewRendererBase
  164. #else
  165. #if defined(__WXGTK20__)
  166. #include "wx/gtk/dvrenderer.h"
  167. #elif defined(__WXMAC__)
  168. #include "wx/osx/dvrenderer.h"
  169. #else
  170. #error "unknown native wxDataViewCtrl implementation"
  171. #endif
  172. #define wxDataViewCustomRendererRealBase wxDataViewRenderer
  173. #endif
  174. // ----------------------------------------------------------------------------
  175. // wxDataViewCustomRendererBase
  176. // ----------------------------------------------------------------------------
  177. class WXDLLIMPEXP_ADV wxDataViewCustomRendererBase
  178. : public wxDataViewCustomRendererRealBase
  179. {
  180. public:
  181. // Constructor must specify the usual renderer parameters which we simply
  182. // pass to the base class
  183. wxDataViewCustomRendererBase(const wxString& varianttype = "string",
  184. wxDataViewCellMode mode = wxDATAVIEW_CELL_INERT,
  185. int align = wxDVR_DEFAULT_ALIGNMENT)
  186. : wxDataViewCustomRendererRealBase(varianttype, mode, align)
  187. {
  188. }
  189. // Render the item using the current value (returned by GetValue()).
  190. virtual bool Render(wxRect cell, wxDC *dc, int state) = 0;
  191. // Return the size of the item appropriate to its current value.
  192. virtual wxSize GetSize() const = 0;
  193. // Define virtual function which are called when a key is pressed on the
  194. // item, clicked or the user starts to drag it: by default they all simply
  195. // return false indicating that the events are not handled
  196. virtual bool ActivateCell(const wxRect& cell,
  197. wxDataViewModel *model,
  198. const wxDataViewItem & item,
  199. unsigned int col,
  200. const wxMouseEvent* mouseEvent);
  201. // Deprecated, use (and override) ActivateCell() instead
  202. wxDEPRECATED_BUT_USED_INTERNALLY_INLINE(
  203. virtual bool Activate(wxRect WXUNUSED(cell),
  204. wxDataViewModel *WXUNUSED(model),
  205. const wxDataViewItem & WXUNUSED(item),
  206. unsigned int WXUNUSED(col)),
  207. return false; )
  208. // Deprecated, use (and override) ActivateCell() instead
  209. wxDEPRECATED_BUT_USED_INTERNALLY_INLINE(
  210. virtual bool LeftClick(wxPoint WXUNUSED(cursor),
  211. wxRect WXUNUSED(cell),
  212. wxDataViewModel *WXUNUSED(model),
  213. const wxDataViewItem & WXUNUSED(item),
  214. unsigned int WXUNUSED(col)),
  215. return false; )
  216. virtual bool StartDrag(const wxPoint& WXUNUSED(cursor),
  217. const wxRect& WXUNUSED(cell),
  218. wxDataViewModel *WXUNUSED(model),
  219. const wxDataViewItem & WXUNUSED(item),
  220. unsigned int WXUNUSED(col) )
  221. { return false; }
  222. // Helper which can be used by Render() implementation in the derived
  223. // classes: it will draw the text in the same manner as the standard
  224. // renderers do.
  225. virtual void RenderText(const wxString& text,
  226. int xoffset,
  227. wxRect cell,
  228. wxDC *dc,
  229. int state);
  230. // Override the base class virtual method to simply store the attribute so
  231. // that it can be accessed using GetAttr() from Render() if needed.
  232. virtual void SetAttr(const wxDataViewItemAttr& attr) { m_attr = attr; }
  233. const wxDataViewItemAttr& GetAttr() const { return m_attr; }
  234. // Store the enabled state of the item so that it can be accessed from
  235. // Render() via GetEnabled() if needed.
  236. virtual void SetEnabled(bool enabled) { m_enabled = enabled; }
  237. bool GetEnabled() const { return m_enabled; }
  238. // Implementation only from now on
  239. // Retrieve the DC to use for drawing. This is implemented in derived
  240. // platform-specific classes.
  241. virtual wxDC *GetDC() = 0;
  242. // To draw background use the background colour in wxDataViewItemAttr
  243. virtual void RenderBackground(wxDC* dc, const wxRect& rect);
  244. // Prepare DC to use attributes and call Render().
  245. void WXCallRender(wxRect rect, wxDC *dc, int state);
  246. virtual bool IsCustomRenderer() const { return true; }
  247. protected:
  248. // helper for GetSize() implementations, respects attributes
  249. wxSize GetTextExtent(const wxString& str) const;
  250. private:
  251. wxDataViewItemAttr m_attr;
  252. bool m_enabled;
  253. wxDECLARE_NO_COPY_CLASS(wxDataViewCustomRendererBase);
  254. };
  255. // include the declaration of all the other renderers to get the real
  256. // wxDataViewCustomRenderer from which we need to inherit below
  257. #ifdef wxHAS_GENERIC_DATAVIEWCTRL
  258. // because of the different renderer classes hierarchy in the generic
  259. // version, as explained above, we can include the header defining
  260. // wxDataViewRenderer only here and not before wxDataViewCustomRendererBase
  261. // declaration as for the native ports
  262. #include "wx/generic/dvrenderer.h"
  263. #include "wx/generic/dvrenderers.h"
  264. #elif defined(__WXGTK20__)
  265. #include "wx/gtk/dvrenderers.h"
  266. #elif defined(__WXMAC__)
  267. #include "wx/osx/dvrenderers.h"
  268. #else
  269. #error "unknown native wxDataViewCtrl implementation"
  270. #endif
  271. // ----------------------------------------------------------------------------
  272. // wxDataViewSpinRenderer
  273. // ----------------------------------------------------------------------------
  274. class WXDLLIMPEXP_ADV wxDataViewSpinRenderer: public wxDataViewCustomRenderer
  275. {
  276. public:
  277. wxDataViewSpinRenderer( int min, int max,
  278. wxDataViewCellMode mode = wxDATAVIEW_CELL_EDITABLE,
  279. int alignment = wxDVR_DEFAULT_ALIGNMENT );
  280. virtual bool HasEditorCtrl() const { return true; }
  281. virtual wxWindow* CreateEditorCtrl( wxWindow *parent, wxRect labelRect, const wxVariant &value );
  282. virtual bool GetValueFromEditorCtrl( wxWindow* editor, wxVariant &value );
  283. virtual bool Render( wxRect rect, wxDC *dc, int state );
  284. virtual wxSize GetSize() const;
  285. virtual bool SetValue( const wxVariant &value );
  286. virtual bool GetValue( wxVariant &value ) const;
  287. private:
  288. long m_data;
  289. long m_min,m_max;
  290. };
  291. #if defined(wxHAS_GENERIC_DATAVIEWCTRL) || defined(__WXOSX_CARBON__)
  292. // ----------------------------------------------------------------------------
  293. // wxDataViewChoiceRenderer
  294. // ----------------------------------------------------------------------------
  295. class WXDLLIMPEXP_ADV wxDataViewChoiceRenderer: public wxDataViewCustomRenderer
  296. {
  297. public:
  298. wxDataViewChoiceRenderer( const wxArrayString &choices,
  299. wxDataViewCellMode mode = wxDATAVIEW_CELL_EDITABLE,
  300. int alignment = wxDVR_DEFAULT_ALIGNMENT );
  301. virtual bool HasEditorCtrl() const { return true; }
  302. virtual wxWindow* CreateEditorCtrl( wxWindow *parent, wxRect labelRect, const wxVariant &value );
  303. virtual bool GetValueFromEditorCtrl( wxWindow* editor, wxVariant &value );
  304. virtual bool Render( wxRect rect, wxDC *dc, int state );
  305. virtual wxSize GetSize() const;
  306. virtual bool SetValue( const wxVariant &value );
  307. virtual bool GetValue( wxVariant &value ) const;
  308. wxString GetChoice(size_t index) const { return m_choices[index]; }
  309. const wxArrayString& GetChoices() const { return m_choices; }
  310. private:
  311. wxArrayString m_choices;
  312. wxString m_data;
  313. };
  314. // ----------------------------------------------------------------------------
  315. // wxDataViewChoiceByIndexRenderer
  316. // ----------------------------------------------------------------------------
  317. class WXDLLIMPEXP_ADV wxDataViewChoiceByIndexRenderer: public wxDataViewChoiceRenderer
  318. {
  319. public:
  320. wxDataViewChoiceByIndexRenderer( const wxArrayString &choices,
  321. wxDataViewCellMode mode = wxDATAVIEW_CELL_EDITABLE,
  322. int alignment = wxDVR_DEFAULT_ALIGNMENT );
  323. virtual wxWindow* CreateEditorCtrl( wxWindow *parent, wxRect labelRect, const wxVariant &value );
  324. virtual bool GetValueFromEditorCtrl( wxWindow* editor, wxVariant &value );
  325. virtual bool SetValue( const wxVariant &value );
  326. virtual bool GetValue( wxVariant &value ) const;
  327. };
  328. #endif // generic or Carbon versions
  329. #if defined(wxHAS_GENERIC_DATAVIEWCTRL) || defined(__WXGTK__)
  330. // ----------------------------------------------------------------------------
  331. // wxDataViewDateRenderer
  332. // ----------------------------------------------------------------------------
  333. #if wxUSE_DATEPICKCTRL
  334. class WXDLLIMPEXP_ADV wxDataViewDateRenderer: public wxDataViewCustomRenderer
  335. {
  336. public:
  337. wxDataViewDateRenderer(const wxString &varianttype = wxT("datetime"),
  338. wxDataViewCellMode mode = wxDATAVIEW_CELL_EDITABLE,
  339. int align = wxDVR_DEFAULT_ALIGNMENT);
  340. virtual bool HasEditorCtrl() const { return true; }
  341. virtual wxWindow *CreateEditorCtrl(wxWindow *parent, wxRect labelRect, const wxVariant &value);
  342. virtual bool GetValueFromEditorCtrl(wxWindow* editor, wxVariant &value);
  343. virtual bool SetValue(const wxVariant &value);
  344. virtual bool GetValue(wxVariant& value) const;
  345. virtual bool Render( wxRect cell, wxDC *dc, int state );
  346. virtual wxSize GetSize() const;
  347. private:
  348. wxDateTime m_date;
  349. };
  350. #else // !wxUSE_DATEPICKCTRL
  351. typedef wxDataViewTextRenderer wxDataViewDateRenderer;
  352. #endif
  353. #endif // generic or GTK+ versions
  354. // this class is obsolete, its functionality was merged in
  355. // wxDataViewTextRenderer itself now, don't use it any more
  356. #define wxDataViewTextRendererAttr wxDataViewTextRenderer
  357. #endif // _WX_DVRENDERERS_H_