Remove wxDataViewTextRendererAttr by merging it with wxDataViewTextRenderer.
[wxWidgets.git] / include / wx / generic / dataview.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/generic/dataview.h
3 // Purpose: wxDataViewCtrl generic implementation header
4 // Author: Robert Roebling
5 // Modified By: Bo Yang
6 // Id: $Id$
7 // Copyright: (c) 1998 Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #ifndef __GENERICDATAVIEWCTRLH__
12 #define __GENERICDATAVIEWCTRLH__
13
14 #include "wx/defs.h"
15 #include "wx/object.h"
16 #include "wx/list.h"
17 #include "wx/control.h"
18 #include "wx/scrolwin.h"
19 #include "wx/icon.h"
20
21 // ---------------------------------------------------------
22 // classes
23 // ---------------------------------------------------------
24
25 class WXDLLIMPEXP_FWD_ADV wxDataViewCtrl;
26 class WXDLLIMPEXP_FWD_ADV wxDataViewMainWindow;
27 class WXDLLIMPEXP_FWD_ADV wxDataViewHeaderWindow;
28
29 // ---------------------------------------------------------
30 // wxDataViewRenderer
31 // ---------------------------------------------------------
32
33 class WXDLLIMPEXP_ADV wxDataViewRenderer: public wxDataViewRendererBase
34 {
35 public:
36 wxDataViewRenderer( const wxString &varianttype,
37 wxDataViewCellMode mode = wxDATAVIEW_CELL_INERT,
38 int align = wxDVR_DEFAULT_ALIGNMENT );
39 virtual ~wxDataViewRenderer();
40
41 // these methods are used to draw the cell contents, Render() doesn't care
42 // about the attributes while RenderWithAttr() does -- override it if you
43 // want to take the attributes defined for this cell into account, otherwise
44 // overriding Render() is enough
45 virtual bool Render( wxRect cell, wxDC *dc, int state ) = 0;
46
47 // NB: RenderWithAttr() also has more standard parameter order and types
48 virtual bool
49 RenderWithAttr(wxDC& dc,
50 const wxRect& rect,
51 const wxDataViewItemAttr * WXUNUSED(attr), // NULL if none
52 int state)
53 {
54 return Render(rect, &dc, state);
55 }
56
57 virtual wxSize GetSize() const = 0;
58
59 virtual void SetAlignment( int align );
60 virtual int GetAlignment() const;
61
62 virtual void SetMode( wxDataViewCellMode mode )
63 { m_mode=mode; }
64 virtual wxDataViewCellMode GetMode() const
65 { return m_mode; }
66
67 virtual bool Activate( wxRect WXUNUSED(cell),
68 wxDataViewModel *WXUNUSED(model),
69 const wxDataViewItem & WXUNUSED(item),
70 unsigned int WXUNUSED(col) )
71 { return false; }
72
73 virtual bool LeftClick( wxPoint WXUNUSED(cursor),
74 wxRect WXUNUSED(cell),
75 wxDataViewModel *WXUNUSED(model),
76 const wxDataViewItem & WXUNUSED(item),
77 unsigned int WXUNUSED(col) )
78 { return false; }
79 virtual bool RightClick( wxPoint WXUNUSED(cursor),
80 wxRect WXUNUSED(cell),
81 wxDataViewModel *WXUNUSED(model),
82 const wxDataViewItem & WXUNUSED(item),
83 unsigned int WXUNUSED(col) )
84 { return false; }
85 virtual bool StartDrag( wxPoint WXUNUSED(cursor),
86 wxRect WXUNUSED(cell),
87 wxDataViewModel *WXUNUSED(model),
88 const wxDataViewItem & WXUNUSED(item),
89 unsigned int WXUNUSED(col) )
90 { return false; }
91
92 // Create DC on request
93 virtual wxDC *GetDC();
94
95 // implementation
96 int CalculateAlignment() const;
97
98 private:
99 wxDC *m_dc;
100 int m_align;
101 wxDataViewCellMode m_mode;
102
103 protected:
104 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewRenderer)
105 };
106
107 // ---------------------------------------------------------
108 // wxDataViewCustomRenderer
109 // ---------------------------------------------------------
110
111 class WXDLLIMPEXP_ADV wxDataViewCustomRenderer: public wxDataViewRenderer
112 {
113 public:
114 wxDataViewCustomRenderer( const wxString &varianttype = wxT("string"),
115 wxDataViewCellMode mode = wxDATAVIEW_CELL_INERT,
116 int align = wxDVR_DEFAULT_ALIGNMENT );
117
118 // Draw the text using the provided attributes
119 void RenderText(wxDC& dc,
120 const wxRect& rect,
121 const wxString& text,
122 const wxDataViewItemAttr *attr, // may be NULL if none
123 int state,
124 int xoffset = 0);
125
126 // Overload using standard attributes
127 void RenderText( const wxString &text, int xoffset, wxRect cell, wxDC *dc, int state );
128
129 protected:
130 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewCustomRenderer)
131 };
132
133
134 // ---------------------------------------------------------
135 // wxDataViewTextRenderer
136 // ---------------------------------------------------------
137
138 class WXDLLIMPEXP_ADV wxDataViewTextRenderer: public wxDataViewCustomRenderer
139 {
140 public:
141 wxDataViewTextRenderer( const wxString &varianttype = wxT("string"),
142 wxDataViewCellMode mode = wxDATAVIEW_CELL_INERT,
143 int align = wxDVR_DEFAULT_ALIGNMENT );
144
145 bool SetValue( const wxVariant &value );
146 bool GetValue( wxVariant &value ) const;
147
148 virtual bool RenderWithAttr(wxDC& dc,
149 const wxRect& rect,
150 const wxDataViewItemAttr *attr,
151 int state);
152 virtual bool Render( wxRect cell, wxDC *dc, int state )
153 {
154 return RenderWithAttr(*dc, cell, NULL, state);
155 }
156
157 wxSize GetSize() const;
158
159 // in-place editing
160 virtual bool HasEditorCtrl() const;
161 virtual wxControl* CreateEditorCtrl( wxWindow *parent, wxRect labelRect,
162 const wxVariant &value );
163 virtual bool GetValueFromEditorCtrl( wxControl* editor, wxVariant &value );
164
165 protected:
166 wxString m_text;
167
168 protected:
169 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewTextRenderer)
170 };
171
172 // ---------------------------------------------------------
173 // wxDataViewBitmapRenderer
174 // ---------------------------------------------------------
175
176 class WXDLLIMPEXP_ADV wxDataViewBitmapRenderer: public wxDataViewCustomRenderer
177 {
178 public:
179 wxDataViewBitmapRenderer( const wxString &varianttype = wxT("wxBitmap"),
180 wxDataViewCellMode mode = wxDATAVIEW_CELL_INERT,
181 int align = wxDVR_DEFAULT_ALIGNMENT );
182
183 bool SetValue( const wxVariant &value );
184 bool GetValue( wxVariant &value ) const;
185
186 bool Render( wxRect cell, wxDC *dc, int state );
187 wxSize GetSize() const;
188
189 private:
190 wxIcon m_icon;
191 wxBitmap m_bitmap;
192
193 protected:
194 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewBitmapRenderer)
195 };
196
197 // ---------------------------------------------------------
198 // wxDataViewToggleRenderer
199 // ---------------------------------------------------------
200
201 class WXDLLIMPEXP_ADV wxDataViewToggleRenderer: public wxDataViewCustomRenderer
202 {
203 public:
204 wxDataViewToggleRenderer( const wxString &varianttype = wxT("bool"),
205 wxDataViewCellMode mode = wxDATAVIEW_CELL_INERT,
206 int align = wxDVR_DEFAULT_ALIGNMENT );
207
208 bool SetValue( const wxVariant &value );
209 bool GetValue( wxVariant &value ) const;
210
211 bool Render( wxRect cell, wxDC *dc, int state );
212 bool Activate( wxRect cell, wxDataViewModel *model, const wxDataViewItem & item,
213 unsigned int col );
214 wxSize GetSize() const;
215
216 private:
217 bool m_toggle;
218
219 protected:
220 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewToggleRenderer)
221 };
222
223 // ---------------------------------------------------------
224 // wxDataViewProgressRenderer
225 // ---------------------------------------------------------
226
227 class WXDLLIMPEXP_ADV wxDataViewProgressRenderer: public wxDataViewCustomRenderer
228 {
229 public:
230 wxDataViewProgressRenderer( const wxString &label = wxEmptyString,
231 const wxString &varianttype = wxT("long"),
232 wxDataViewCellMode mode = wxDATAVIEW_CELL_INERT,
233 int align = wxDVR_DEFAULT_ALIGNMENT );
234 virtual ~wxDataViewProgressRenderer();
235
236 bool SetValue( const wxVariant &value );
237 bool GetValue( wxVariant& value ) const;
238
239 virtual bool Render( wxRect cell, wxDC *dc, int state );
240 virtual wxSize GetSize() const;
241
242 private:
243 wxString m_label;
244 int m_value;
245
246 protected:
247 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewProgressRenderer)
248 };
249
250 // ---------------------------------------------------------
251 // wxDataViewIconTextRenderer
252 // ---------------------------------------------------------
253
254 class WXDLLIMPEXP_ADV wxDataViewIconTextRenderer: public wxDataViewCustomRenderer
255 {
256 public:
257 wxDataViewIconTextRenderer( const wxString &varianttype = wxT("wxDataViewIconText"),
258 wxDataViewCellMode mode = wxDATAVIEW_CELL_INERT,
259 int align = wxDVR_DEFAULT_ALIGNMENT );
260 virtual ~wxDataViewIconTextRenderer();
261
262 bool SetValue( const wxVariant &value );
263 bool GetValue( wxVariant &value ) const;
264
265 virtual bool Render( wxRect cell, wxDC *dc, int state );
266 virtual wxSize GetSize() const;
267
268 virtual bool HasEditorCtrl() const { return true; }
269 virtual wxControl* CreateEditorCtrl( wxWindow *parent, wxRect labelRect,
270 const wxVariant &value );
271 virtual bool GetValueFromEditorCtrl( wxControl* editor, wxVariant &value );
272
273 private:
274 wxDataViewIconText m_value;
275
276 protected:
277 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewIconTextRenderer)
278 };
279
280 // ---------------------------------------------------------
281 // wxDataViewDateRenderer
282 // ---------------------------------------------------------
283
284 class WXDLLIMPEXP_ADV wxDataViewDateRenderer: public wxDataViewCustomRenderer
285 {
286 public:
287 wxDataViewDateRenderer( const wxString &varianttype = wxT("datetime"),
288 wxDataViewCellMode mode = wxDATAVIEW_CELL_ACTIVATABLE,
289 int align = wxDVR_DEFAULT_ALIGNMENT );
290
291 bool SetValue( const wxVariant &value );
292 bool GetValue( wxVariant& value ) const;
293
294 virtual bool Render( wxRect cell, wxDC *dc, int state );
295 virtual wxSize GetSize() const;
296 virtual bool Activate( wxRect cell,
297 wxDataViewModel *model,
298 const wxDataViewItem& item,
299 unsigned int col );
300
301 private:
302 wxDateTime m_date;
303
304 protected:
305 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewDateRenderer)
306 };
307
308 // ---------------------------------------------------------
309 // wxDataViewColumn
310 // ---------------------------------------------------------
311
312 class WXDLLIMPEXP_ADV wxDataViewColumn : public wxDataViewColumnBase
313 {
314 public:
315 wxDataViewColumn(const wxString& title,
316 wxDataViewRenderer *renderer,
317 unsigned int model_column,
318 int width = wxDVC_DEFAULT_WIDTH,
319 wxAlignment align = wxALIGN_CENTER,
320 int flags = wxDATAVIEW_COL_RESIZABLE)
321 : wxDataViewColumnBase(renderer, model_column),
322 m_title(title)
323 {
324 Init(width, align, flags);
325 }
326
327 wxDataViewColumn(const wxBitmap& bitmap,
328 wxDataViewRenderer *renderer,
329 unsigned int model_column,
330 int width = wxDVC_DEFAULT_WIDTH,
331 wxAlignment align = wxALIGN_CENTER,
332 int flags = wxDATAVIEW_COL_RESIZABLE)
333 : wxDataViewColumnBase(bitmap, renderer, model_column)
334 {
335 Init(width, align, flags);
336 }
337
338 // implement wxHeaderColumnBase methods
339 virtual void SetTitle(const wxString& title) { m_title = title; }
340 virtual wxString GetTitle() const { return m_title; }
341
342 virtual void SetWidth(int width) { m_width = width; }
343 virtual int GetWidth() const { return m_width; }
344
345 virtual void SetMinWidth(int minWidth) { m_minWidth = minWidth; }
346 virtual int GetMinWidth() const { return m_minWidth; }
347
348 virtual void SetAlignment(wxAlignment align) { m_align = align; }
349 virtual wxAlignment GetAlignment() const { return m_align; }
350
351 virtual void SetFlags(int flags) { m_flags = flags; }
352 virtual int GetFlags() const { return m_flags; }
353
354 virtual void SetAsSortKey(bool sort = true) { m_sort = sort; }
355 virtual bool IsSortKey() const { return m_sort; }
356
357 virtual void SetSortOrder(bool ascending) { m_sortAscending = ascending; }
358 virtual bool IsSortOrderAscending() const { return m_sortAscending; }
359
360 private:
361 // common part of all ctors
362 void Init(int width, wxAlignment align, int flags)
363 {
364 m_width = width == wxCOL_WIDTH_DEFAULT ? wxDVC_DEFAULT_WIDTH : width;
365 m_minWidth = 0;
366 m_align = align;
367 m_flags = flags;
368 m_sort = false;
369 m_sortAscending = true;
370 }
371
372 wxString m_title;
373 int m_width,
374 m_minWidth;
375 wxAlignment m_align;
376 int m_flags;
377 bool m_sort,
378 m_sortAscending;
379
380 friend class wxDataViewHeaderWindowBase;
381 friend class wxDataViewHeaderWindow;
382 friend class wxDataViewHeaderWindowMSW;
383 };
384
385 // ---------------------------------------------------------
386 // wxDataViewCtrl
387 // ---------------------------------------------------------
388
389 WX_DECLARE_LIST_WITH_DECL(wxDataViewColumn, wxDataViewColumnList,
390 class WXDLLIMPEXP_ADV);
391
392 class WXDLLIMPEXP_ADV wxDataViewCtrl : public wxDataViewCtrlBase,
393 public wxScrollHelper
394 {
395 friend class wxDataViewMainWindow;
396 friend class wxDataViewHeaderWindowBase;
397 friend class wxDataViewHeaderWindow;
398 friend class wxDataViewHeaderWindowMSW;
399 friend class wxDataViewColumn;
400
401 public:
402 wxDataViewCtrl() : wxScrollHelper(this)
403 {
404 Init();
405 }
406
407 wxDataViewCtrl( wxWindow *parent, wxWindowID id,
408 const wxPoint& pos = wxDefaultPosition,
409 const wxSize& size = wxDefaultSize, long style = 0,
410 const wxValidator& validator = wxDefaultValidator )
411 : wxScrollHelper(this)
412 {
413 Create(parent, id, pos, size, style, validator );
414 }
415
416 virtual ~wxDataViewCtrl();
417
418 void Init();
419
420 bool Create(wxWindow *parent, wxWindowID id,
421 const wxPoint& pos = wxDefaultPosition,
422 const wxSize& size = wxDefaultSize, long style = 0,
423 const wxValidator& validator = wxDefaultValidator );
424
425 virtual bool AssociateModel( wxDataViewModel *model );
426
427 virtual bool AppendColumn( wxDataViewColumn *col );
428 virtual bool PrependColumn( wxDataViewColumn *col );
429 virtual bool InsertColumn( unsigned int pos, wxDataViewColumn *col );
430
431 virtual void DoSetExpanderColumn();
432 virtual void DoSetIndent();
433
434 virtual unsigned int GetColumnCount() const;
435 virtual wxDataViewColumn* GetColumn( unsigned int pos ) const;
436 virtual bool DeleteColumn( wxDataViewColumn *column );
437 virtual bool ClearColumns();
438 virtual int GetColumnPosition( const wxDataViewColumn *column ) const;
439
440 virtual wxDataViewColumn *GetSortingColumn() const;
441
442 virtual wxDataViewItem GetSelection() const;
443 virtual int GetSelections( wxDataViewItemArray & sel ) const;
444 virtual void SetSelections( const wxDataViewItemArray & sel );
445 virtual void Select( const wxDataViewItem & item );
446 virtual void Unselect( const wxDataViewItem & item );
447 virtual bool IsSelected( const wxDataViewItem & item ) const;
448
449 virtual void SelectAll();
450 virtual void UnselectAll();
451
452 virtual void EnsureVisible( const wxDataViewItem & item,
453 const wxDataViewColumn *column = NULL );
454 virtual void HitTest( const wxPoint & point, wxDataViewItem & item,
455 wxDataViewColumn* &column ) const;
456 virtual wxRect GetItemRect( const wxDataViewItem & item,
457 const wxDataViewColumn *column = NULL ) const;
458
459 virtual void Expand( const wxDataViewItem & item );
460 virtual void Collapse( const wxDataViewItem & item );
461 virtual bool IsExpanded( const wxDataViewItem & item ) const;
462
463 virtual void SetFocus();
464
465 #if wxUSE_DRAG_AND_DROP
466 virtual bool EnableDragSource( const wxDataFormat &format );
467 virtual bool EnableDropTarget( const wxDataFormat &format );
468 #endif // wxUSE_DRAG_AND_DROP
469
470 virtual wxBorder GetDefaultBorder() const;
471
472 void StartEditor( const wxDataViewItem & item, unsigned int column );
473
474 protected:
475 virtual int GetSelections( wxArrayInt & sel ) const;
476 virtual void SetSelections( const wxArrayInt & sel );
477 virtual void Select( int row );
478 virtual void Unselect( int row );
479 virtual bool IsSelected( int row ) const;
480 virtual void SelectRange( int from, int to );
481 virtual void UnselectRange( int from, int to );
482
483 virtual void EnsureVisible( int row, int column );
484
485 virtual wxDataViewItem GetItemByRow( unsigned int row ) const;
486 virtual int GetRowByItem( const wxDataViewItem & item ) const;
487
488 int GetSortingColumnIndex() const { return m_sortingColumnIdx; }
489 void SetSortingColumnIndex(int idx) { m_sortingColumnIdx = idx; }
490
491 public: // utility functions not part of the API
492
493 // returns the "best" width for the idx-th column
494 unsigned int GetBestColumnWidth(int WXUNUSED(idx)) const
495 {
496 return GetClientSize().GetWidth() / GetColumnCount();
497 }
498
499 // called by header window after reorder
500 void ColumnMoved( wxDataViewColumn* col, unsigned int new_pos );
501
502 // update the display after a change to an individual column
503 void OnColumnChange(unsigned int idx);
504
505 // update after a change to the number of columns
506 void OnColumnsCountChanged();
507
508 wxWindow *GetMainWindow() { return (wxWindow*) m_clientArea; }
509
510 // return the index of the given column in m_cols
511 int GetColumnIndex(const wxDataViewColumn *column) const;
512
513 // return the column displayed at the given position in the control
514 wxDataViewColumn *GetColumnAt(unsigned int pos) const;
515
516 private:
517 wxDataViewColumnList m_cols;
518 wxDataViewModelNotifier *m_notifier;
519 wxDataViewMainWindow *m_clientArea;
520 wxDataViewHeaderWindow *m_headerArea;
521
522 // the index of the column currently used for sorting or -1
523 int m_sortingColumnIdx;
524
525 private:
526 void OnSize( wxSizeEvent &event );
527 virtual wxSize GetSizeAvailableForScrollTarget(const wxSize& size);
528
529 // we need to return a special WM_GETDLGCODE value to process just the
530 // arrows but let the other navigation characters through
531 #ifdef __WXMSW__
532 virtual WXLRESULT MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam);
533 #endif // __WXMSW__
534
535 WX_FORWARD_TO_SCROLL_HELPER()
536
537 private:
538 DECLARE_DYNAMIC_CLASS(wxDataViewCtrl)
539 wxDECLARE_NO_COPY_CLASS(wxDataViewCtrl);
540 DECLARE_EVENT_TABLE()
541 };
542
543
544 #endif // __GENERICDATAVIEWCTRLH__