]> git.saurik.com Git - wxWidgets.git/blob - include/wx/dvrenderers.h
Add lambda-friendly wxDialog::ShowWindowModalThenDo().
[wxWidgets.git] / include / wx / dvrenderers.h
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
11 #ifndef _WX_DVRENDERERS_H_
12 #define _WX_DVRENDERERS_H_
13
14 /*
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.
20
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.
28 */
29
30 class WXDLLIMPEXP_FWD_ADV wxDataViewCustomRenderer;
31
32 // ----------------------------------------------------------------------------
33 // wxDataViewIconText: helper class used by wxDataViewIconTextRenderer
34 // ----------------------------------------------------------------------------
35
36 class WXDLLIMPEXP_ADV wxDataViewIconText : public wxObject
37 {
38 public:
39 wxDataViewIconText( const wxString &text = wxEmptyString,
40 const wxIcon& icon = wxNullIcon )
41 : m_text(text),
42 m_icon(icon)
43 { }
44
45 wxDataViewIconText( const wxDataViewIconText &other )
46 : wxObject(),
47 m_text(other.m_text),
48 m_icon(other.m_icon)
49 { }
50
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; }
55
56 bool IsSameAs(const wxDataViewIconText& other) const
57 {
58 return m_text == other.m_text && m_icon.IsSameAs(other.m_icon);
59 }
60
61 bool operator==(const wxDataViewIconText& other) const
62 {
63 return IsSameAs(other);
64 }
65
66 bool operator!=(const wxDataViewIconText& other) const
67 {
68 return !IsSameAs(other);
69 }
70
71 private:
72 wxString m_text;
73 wxIcon m_icon;
74
75 DECLARE_DYNAMIC_CLASS(wxDataViewIconText)
76 };
77
78 DECLARE_VARIANT_OBJECT_EXPORTED(wxDataViewIconText, WXDLLIMPEXP_ADV)
79
80 // ----------------------------------------------------------------------------
81 // wxDataViewRendererBase
82 // ----------------------------------------------------------------------------
83
84 enum wxDataViewCellMode
85 {
86 wxDATAVIEW_CELL_INERT,
87 wxDATAVIEW_CELL_ACTIVATABLE,
88 wxDATAVIEW_CELL_EDITABLE
89 };
90
91 enum wxDataViewCellRenderState
92 {
93 wxDATAVIEW_CELL_SELECTED = 1,
94 wxDATAVIEW_CELL_PRELIT = 2,
95 wxDATAVIEW_CELL_INSENSITIVE = 4,
96 wxDATAVIEW_CELL_FOCUSED = 8
97 };
98
99 class WXDLLIMPEXP_ADV wxDataViewRendererBase: public wxObject
100 {
101 public:
102 wxDataViewRendererBase( const wxString &varianttype,
103 wxDataViewCellMode mode = wxDATAVIEW_CELL_INERT,
104 int alignment = wxDVR_DEFAULT_ALIGNMENT );
105 virtual ~wxDataViewRendererBase();
106
107 virtual bool Validate( wxVariant& WXUNUSED(value) )
108 { return true; }
109
110 void SetOwner( wxDataViewColumn *owner ) { m_owner = owner; }
111 wxDataViewColumn* GetOwner() const { return m_owner; }
112
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;
117
118 virtual void SetAttr(const wxDataViewItemAttr& WXUNUSED(attr)) { }
119
120 virtual void SetEnabled(bool WXUNUSED(enabled)) { }
121
122 wxString GetVariantType() const { return m_variantType; }
123
124 // helper that calls SetValue and SetAttr:
125 void PrepareForItem(const wxDataViewModel *model,
126 const wxDataViewItem& item, unsigned column);
127
128 // renderer properties:
129 virtual void SetMode( wxDataViewCellMode mode ) = 0;
130 virtual wxDataViewCellMode GetMode() const = 0;
131
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;
137
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); }
143
144 virtual wxEllipsizeMode GetEllipsizeMode() const = 0;
145
146 // in-place editing
147 virtual bool HasEditorCtrl() const
148 { return false; }
149 virtual wxWindow* CreateEditorCtrl(wxWindow * WXUNUSED(parent),
150 wxRect WXUNUSED(labelRect),
151 const wxVariant& WXUNUSED(value))
152 { return NULL; }
153 virtual bool GetValueFromEditorCtrl(wxWindow * WXUNUSED(editor),
154 wxVariant& WXUNUSED(value))
155 { return false; }
156
157 virtual bool StartEditing( const wxDataViewItem &item, wxRect labelRect );
158 virtual void CancelEditing();
159 virtual bool FinishEditing();
160
161 wxWindow *GetEditorCtrl() { return m_editorCtrl; }
162
163 virtual bool IsCustomRenderer() const { return false; }
164
165
166 protected:
167 // Called from {Cancel,Finish}Editing() to cleanup m_editorCtrl
168 void DestroyEditControl();
169
170 wxString m_variantType;
171 wxDataViewColumn *m_owner;
172 wxWeakRef<wxWindow> m_editorCtrl;
173 wxDataViewItem m_item; // for m_editorCtrl
174
175 // internal utility, may be used anywhere the window associated with the
176 // renderer is required
177 wxDataViewCtrl* GetView() const;
178
179 protected:
180 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewRendererBase)
181 };
182
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
188 //
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
195 #else
196 #if defined(__WXGTK20__)
197 #include "wx/gtk/dvrenderer.h"
198 #elif defined(__WXMAC__)
199 #include "wx/osx/dvrenderer.h"
200 #else
201 #error "unknown native wxDataViewCtrl implementation"
202 #endif
203 #define wxDataViewCustomRendererRealBase wxDataViewRenderer
204 #endif
205
206 // ----------------------------------------------------------------------------
207 // wxDataViewCustomRendererBase
208 // ----------------------------------------------------------------------------
209
210 class WXDLLIMPEXP_ADV wxDataViewCustomRendererBase
211 : public wxDataViewCustomRendererRealBase
212 {
213 public:
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)
220 {
221 }
222
223
224 // Render the item using the current value (returned by GetValue()).
225 virtual bool Render(wxRect cell, wxDC *dc, int state) = 0;
226
227 // Return the size of the item appropriate to its current value.
228 virtual wxSize GetSize() const = 0;
229
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
233
234 virtual bool ActivateCell(const wxRect& cell,
235 wxDataViewModel *model,
236 const wxDataViewItem & item,
237 unsigned int col,
238 const wxMouseEvent* mouseEvent);
239
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)),
246 return false; )
247
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)),
255 return false; )
256
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) )
262 { return false; }
263
264
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
267 // renderers do.
268 virtual void RenderText(const wxString& text,
269 int xoffset,
270 wxRect cell,
271 wxDC *dc,
272 int state);
273
274
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; }
279
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; }
284
285
286 // Implementation only from now on
287
288 // Retrieve the DC to use for drawing. This is implemented in derived
289 // platform-specific classes.
290 virtual wxDC *GetDC() = 0;
291
292 // To draw background use the background colour in wxDataViewItemAttr
293 virtual void RenderBackground(wxDC* dc, const wxRect& rect);
294
295 // Prepare DC to use attributes and call Render().
296 void WXCallRender(wxRect rect, wxDC *dc, int state);
297
298 virtual bool IsCustomRenderer() const { return true; }
299
300 protected:
301 // helper for GetSize() implementations, respects attributes
302 wxSize GetTextExtent(const wxString& str) const;
303
304 private:
305 wxDataViewItemAttr m_attr;
306 bool m_enabled;
307
308 wxDECLARE_NO_COPY_CLASS(wxDataViewCustomRendererBase);
309 };
310
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"
324 #else
325 #error "unknown native wxDataViewCtrl implementation"
326 #endif
327
328 // ----------------------------------------------------------------------------
329 // wxDataViewSpinRenderer
330 // ----------------------------------------------------------------------------
331
332 class WXDLLIMPEXP_ADV wxDataViewSpinRenderer: public wxDataViewCustomRenderer
333 {
334 public:
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;
345
346 private:
347 long m_data;
348 long m_min,m_max;
349 };
350
351 #if defined(wxHAS_GENERIC_DATAVIEWCTRL) || defined(__WXOSX_CARBON__)
352
353 // ----------------------------------------------------------------------------
354 // wxDataViewChoiceRenderer
355 // ----------------------------------------------------------------------------
356
357 class WXDLLIMPEXP_ADV wxDataViewChoiceRenderer: public wxDataViewCustomRenderer
358 {
359 public:
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;
370
371 wxString GetChoice(size_t index) const { return m_choices[index]; }
372 const wxArrayString& GetChoices() const { return m_choices; }
373
374 private:
375 wxArrayString m_choices;
376 wxString m_data;
377 };
378
379 // ----------------------------------------------------------------------------
380 // wxDataViewChoiceByIndexRenderer
381 // ----------------------------------------------------------------------------
382
383 class WXDLLIMPEXP_ADV wxDataViewChoiceByIndexRenderer: public wxDataViewChoiceRenderer
384 {
385 public:
386 wxDataViewChoiceByIndexRenderer( const wxArrayString &choices,
387 wxDataViewCellMode mode = wxDATAVIEW_CELL_EDITABLE,
388 int alignment = wxDVR_DEFAULT_ALIGNMENT );
389
390 virtual wxWindow* CreateEditorCtrl( wxWindow *parent, wxRect labelRect, const wxVariant &value );
391 virtual bool GetValueFromEditorCtrl( wxWindow* editor, wxVariant &value );
392
393 virtual bool SetValue( const wxVariant &value );
394 virtual bool GetValue( wxVariant &value ) const;
395 };
396
397
398 #endif // generic or Carbon versions
399
400 #if defined(wxHAS_GENERIC_DATAVIEWCTRL) || defined(__WXGTK__)
401
402 // ----------------------------------------------------------------------------
403 // wxDataViewDateRenderer
404 // ----------------------------------------------------------------------------
405
406 #if wxUSE_DATEPICKCTRL
407 class WXDLLIMPEXP_ADV wxDataViewDateRenderer: public wxDataViewCustomRenderer
408 {
409 public:
410 wxDataViewDateRenderer(const wxString &varianttype = wxT("datetime"),
411 wxDataViewCellMode mode = wxDATAVIEW_CELL_EDITABLE,
412 int align = wxDVR_DEFAULT_ALIGNMENT);
413
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;
421
422 private:
423 wxDateTime m_date;
424 };
425 #else // !wxUSE_DATEPICKCTRL
426 typedef wxDataViewTextRenderer wxDataViewDateRenderer;
427 #endif
428
429 #endif // generic or GTK+ versions
430
431 // this class is obsolete, its functionality was merged in
432 // wxDataViewTextRenderer itself now, don't use it any more
433 #define wxDataViewTextRendererAttr wxDataViewTextRenderer
434
435 #endif // _WX_DVRENDERERS_H_
436