1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/generic/dataview.h
3 // Purpose: wxDataViewCtrl generic implementation header
4 // Author: Robert Roebling
5 // Modified By: Bo Yang
7 // Copyright: (c) 1998 Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #ifndef __GENERICDATAVIEWCTRLH__
12 #define __GENERICDATAVIEWCTRLH__
15 #include "wx/object.h"
17 #include "wx/control.h"
18 #include "wx/scrolwin.h"
21 // ---------------------------------------------------------
23 // ---------------------------------------------------------
25 class WXDLLIMPEXP_FWD_ADV wxDataViewCtrl
;
26 class WXDLLIMPEXP_FWD_ADV wxDataViewMainWindow
;
27 class WXDLLIMPEXP_FWD_ADV wxDataViewHeaderWindow
;
29 // ---------------------------------------------------------
31 // ---------------------------------------------------------
33 class WXDLLIMPEXP_ADV wxDataViewRenderer
: public wxDataViewRendererBase
36 wxDataViewRenderer( const wxString
&varianttype
,
37 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
,
38 int align
= wxDVR_DEFAULT_ALIGNMENT
);
39 virtual ~wxDataViewRenderer();
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;
47 // NB: RenderWithAttr() also has more standard parameter order and types
49 RenderWithAttr(wxDC
& dc
,
51 int align
, // combination of horizontal and vertical
52 const wxDataViewItemAttr
*attr
, // may be NULL if none
55 virtual wxSize
GetSize() const = 0;
57 virtual void SetAlignment( int align
);
58 virtual int GetAlignment() const;
60 virtual void EnableEllipsize(wxEllipsizeMode mode
= wxELLIPSIZE_MIDDLE
)
61 { m_ellipsizeMode
= mode
; }
62 virtual wxEllipsizeMode
GetEllipsizeMode() const
63 { return m_ellipsizeMode
; }
65 virtual void SetMode( wxDataViewCellMode mode
)
67 virtual wxDataViewCellMode
GetMode() const
70 virtual bool Activate( wxRect
WXUNUSED(cell
),
71 wxDataViewModel
*WXUNUSED(model
),
72 const wxDataViewItem
& WXUNUSED(item
),
73 unsigned int WXUNUSED(col
) )
76 virtual bool LeftClick( wxPoint
WXUNUSED(cursor
),
77 wxRect
WXUNUSED(cell
),
78 wxDataViewModel
*WXUNUSED(model
),
79 const wxDataViewItem
& WXUNUSED(item
),
80 unsigned int WXUNUSED(col
) )
82 virtual bool RightClick( wxPoint
WXUNUSED(cursor
),
83 wxRect
WXUNUSED(cell
),
84 wxDataViewModel
*WXUNUSED(model
),
85 const wxDataViewItem
& WXUNUSED(item
),
86 unsigned int WXUNUSED(col
) )
88 virtual bool StartDrag( wxPoint
WXUNUSED(cursor
),
89 wxRect
WXUNUSED(cell
),
90 wxDataViewModel
*WXUNUSED(model
),
91 const wxDataViewItem
& WXUNUSED(item
),
92 unsigned int WXUNUSED(col
) )
95 // Create DC on request
96 virtual wxDC
*GetDC();
99 int CalculateAlignment() const;
102 // This is just a convenience for the derived classes overriding
103 // RenderWithAttr() to avoid repeating the same wxFAIL_MSG() in all of them
104 bool DummyRender(wxRect
WXUNUSED(cell
),
108 wxFAIL_MSG("shouldn't be called at all, use RenderWithAttr() instead");
116 wxDataViewCellMode m_mode
;
118 wxEllipsizeMode m_ellipsizeMode
;
120 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewRenderer
)
123 // ---------------------------------------------------------
124 // wxDataViewCustomRenderer
125 // ---------------------------------------------------------
127 class WXDLLIMPEXP_ADV wxDataViewCustomRenderer
: public wxDataViewRenderer
130 wxDataViewCustomRenderer( const wxString
&varianttype
= wxT("string"),
131 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
,
132 int align
= wxDVR_DEFAULT_ALIGNMENT
);
134 // Draw the text using the provided attributes
135 void RenderText(wxDC
& dc
,
138 const wxString
& text
,
139 const wxDataViewItemAttr
*attr
, // may be NULL if none
143 // Overload using standard attributes
144 void RenderText(const wxString
& text
,
150 RenderText(*dc
, cell
, wxALIGN_NOT
, text
, NULL
, state
, xoffset
);
154 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewCustomRenderer
)
158 // ---------------------------------------------------------
159 // wxDataViewTextRenderer
160 // ---------------------------------------------------------
162 class WXDLLIMPEXP_ADV wxDataViewTextRenderer
: public wxDataViewCustomRenderer
165 wxDataViewTextRenderer( const wxString
&varianttype
= wxT("string"),
166 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
,
167 int align
= wxDVR_DEFAULT_ALIGNMENT
);
169 bool SetValue( const wxVariant
&value
);
170 bool GetValue( wxVariant
&value
) const;
172 virtual bool RenderWithAttr(wxDC
& dc
,
175 const wxDataViewItemAttr
*attr
,
177 virtual bool Render(wxRect cell
, wxDC
*dc
, int state
)
179 return DummyRender(cell
, dc
, state
);
182 wxSize
GetSize() const;
185 virtual bool HasEditorCtrl() const;
186 virtual wxControl
* CreateEditorCtrl( wxWindow
*parent
, wxRect labelRect
,
187 const wxVariant
&value
);
188 virtual bool GetValueFromEditorCtrl( wxControl
* editor
, wxVariant
&value
);
194 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewTextRenderer
)
197 // ---------------------------------------------------------
198 // wxDataViewBitmapRenderer
199 // ---------------------------------------------------------
201 class WXDLLIMPEXP_ADV wxDataViewBitmapRenderer
: public wxDataViewCustomRenderer
204 wxDataViewBitmapRenderer( const wxString
&varianttype
= wxT("wxBitmap"),
205 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
,
206 int align
= wxDVR_DEFAULT_ALIGNMENT
);
208 bool SetValue( const wxVariant
&value
);
209 bool GetValue( wxVariant
&value
) const;
211 bool Render( wxRect cell
, wxDC
*dc
, int state
);
212 wxSize
GetSize() const;
219 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewBitmapRenderer
)
222 // ---------------------------------------------------------
223 // wxDataViewToggleRenderer
224 // ---------------------------------------------------------
226 class WXDLLIMPEXP_ADV wxDataViewToggleRenderer
: public wxDataViewCustomRenderer
229 wxDataViewToggleRenderer( const wxString
&varianttype
= wxT("bool"),
230 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
,
231 int align
= wxDVR_DEFAULT_ALIGNMENT
);
233 bool SetValue( const wxVariant
&value
);
234 bool GetValue( wxVariant
&value
) const;
236 bool Render( wxRect cell
, wxDC
*dc
, int state
);
237 bool Activate( wxRect cell
, wxDataViewModel
*model
, const wxDataViewItem
& item
,
239 wxSize
GetSize() const;
245 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewToggleRenderer
)
248 // ---------------------------------------------------------
249 // wxDataViewProgressRenderer
250 // ---------------------------------------------------------
252 class WXDLLIMPEXP_ADV wxDataViewProgressRenderer
: public wxDataViewCustomRenderer
255 wxDataViewProgressRenderer( const wxString
&label
= wxEmptyString
,
256 const wxString
&varianttype
= wxT("long"),
257 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
,
258 int align
= wxDVR_DEFAULT_ALIGNMENT
);
260 bool SetValue( const wxVariant
&value
);
261 bool GetValue( wxVariant
& value
) const;
263 virtual bool RenderWithAttr(wxDC
& dc
,
266 const wxDataViewItemAttr
*attr
,
268 virtual bool Render(wxRect cell
, wxDC
*dc
, int state
)
270 return DummyRender(cell
, dc
, state
);
272 virtual wxSize
GetSize() const;
279 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewProgressRenderer
)
282 // ---------------------------------------------------------
283 // wxDataViewIconTextRenderer
284 // ---------------------------------------------------------
286 class WXDLLIMPEXP_ADV wxDataViewIconTextRenderer
: public wxDataViewCustomRenderer
289 wxDataViewIconTextRenderer( const wxString
&varianttype
= wxT("wxDataViewIconText"),
290 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
,
291 int align
= wxDVR_DEFAULT_ALIGNMENT
);
293 bool SetValue( const wxVariant
&value
);
294 bool GetValue( wxVariant
&value
) const;
296 virtual bool RenderWithAttr(wxDC
& dc
,
299 const wxDataViewItemAttr
*attr
,
301 virtual bool Render(wxRect cell
, wxDC
*dc
, int state
)
303 return DummyRender(cell
, dc
, state
);
305 virtual wxSize
GetSize() const;
307 virtual bool HasEditorCtrl() const { return true; }
308 virtual wxControl
* CreateEditorCtrl( wxWindow
*parent
, wxRect labelRect
,
309 const wxVariant
&value
);
310 virtual bool GetValueFromEditorCtrl( wxControl
* editor
, wxVariant
&value
);
313 wxDataViewIconText m_value
;
316 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewIconTextRenderer
)
319 // ---------------------------------------------------------
320 // wxDataViewDateRenderer
321 // ---------------------------------------------------------
323 class WXDLLIMPEXP_ADV wxDataViewDateRenderer
: public wxDataViewCustomRenderer
326 wxDataViewDateRenderer( const wxString
&varianttype
= wxT("datetime"),
327 wxDataViewCellMode mode
= wxDATAVIEW_CELL_ACTIVATABLE
,
328 int align
= wxDVR_DEFAULT_ALIGNMENT
);
330 bool SetValue( const wxVariant
&value
);
331 bool GetValue( wxVariant
& value
) const;
333 virtual bool Render( wxRect cell
, wxDC
*dc
, int state
);
334 virtual wxSize
GetSize() const;
335 virtual bool Activate( wxRect cell
,
336 wxDataViewModel
*model
,
337 const wxDataViewItem
& item
,
344 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewDateRenderer
)
347 // ---------------------------------------------------------
349 // ---------------------------------------------------------
351 class WXDLLIMPEXP_ADV wxDataViewColumn
: public wxDataViewColumnBase
354 wxDataViewColumn(const wxString
& title
,
355 wxDataViewRenderer
*renderer
,
356 unsigned int model_column
,
357 int width
= wxDVC_DEFAULT_WIDTH
,
358 wxAlignment align
= wxALIGN_CENTER
,
359 int flags
= wxDATAVIEW_COL_RESIZABLE
)
360 : wxDataViewColumnBase(renderer
, model_column
),
363 Init(width
, align
, flags
);
366 wxDataViewColumn(const wxBitmap
& bitmap
,
367 wxDataViewRenderer
*renderer
,
368 unsigned int model_column
,
369 int width
= wxDVC_DEFAULT_WIDTH
,
370 wxAlignment align
= wxALIGN_CENTER
,
371 int flags
= wxDATAVIEW_COL_RESIZABLE
)
372 : wxDataViewColumnBase(bitmap
, renderer
, model_column
)
374 Init(width
, align
, flags
);
377 // implement wxHeaderColumnBase methods
378 virtual void SetTitle(const wxString
& title
) { m_title
= title
; }
379 virtual wxString
GetTitle() const { return m_title
; }
381 virtual void SetWidth(int width
) { m_width
= width
; }
382 virtual int GetWidth() const { return m_width
; }
384 virtual void SetMinWidth(int minWidth
) { m_minWidth
= minWidth
; }
385 virtual int GetMinWidth() const { return m_minWidth
; }
387 virtual void SetAlignment(wxAlignment align
) { m_align
= align
; }
388 virtual wxAlignment
GetAlignment() const { return m_align
; }
390 virtual void SetFlags(int flags
) { m_flags
= flags
; }
391 virtual int GetFlags() const { return m_flags
; }
393 virtual void SetAsSortKey(bool sort
= true) { m_sort
= sort
; }
394 virtual bool IsSortKey() const { return m_sort
; }
396 virtual void SetSortOrder(bool ascending
) { m_sortAscending
= ascending
; }
397 virtual bool IsSortOrderAscending() const { return m_sortAscending
; }
400 // common part of all ctors
401 void Init(int width
, wxAlignment align
, int flags
)
403 m_width
= width
== wxCOL_WIDTH_DEFAULT
? wxDVC_DEFAULT_WIDTH
: width
;
408 m_sortAscending
= true;
419 friend class wxDataViewHeaderWindowBase
;
420 friend class wxDataViewHeaderWindow
;
421 friend class wxDataViewHeaderWindowMSW
;
424 // ---------------------------------------------------------
426 // ---------------------------------------------------------
428 WX_DECLARE_LIST_WITH_DECL(wxDataViewColumn
, wxDataViewColumnList
,
429 class WXDLLIMPEXP_ADV
);
431 class WXDLLIMPEXP_ADV wxDataViewCtrl
: public wxDataViewCtrlBase
,
432 public wxScrollHelper
434 friend class wxDataViewMainWindow
;
435 friend class wxDataViewHeaderWindowBase
;
436 friend class wxDataViewHeaderWindow
;
437 friend class wxDataViewHeaderWindowMSW
;
438 friend class wxDataViewColumn
;
441 wxDataViewCtrl() : wxScrollHelper(this)
446 wxDataViewCtrl( wxWindow
*parent
, wxWindowID id
,
447 const wxPoint
& pos
= wxDefaultPosition
,
448 const wxSize
& size
= wxDefaultSize
, long style
= 0,
449 const wxValidator
& validator
= wxDefaultValidator
)
450 : wxScrollHelper(this)
452 Create(parent
, id
, pos
, size
, style
, validator
);
455 virtual ~wxDataViewCtrl();
459 bool Create(wxWindow
*parent
, wxWindowID id
,
460 const wxPoint
& pos
= wxDefaultPosition
,
461 const wxSize
& size
= wxDefaultSize
, long style
= 0,
462 const wxValidator
& validator
= wxDefaultValidator
);
464 virtual bool AssociateModel( wxDataViewModel
*model
);
466 virtual bool AppendColumn( wxDataViewColumn
*col
);
467 virtual bool PrependColumn( wxDataViewColumn
*col
);
468 virtual bool InsertColumn( unsigned int pos
, wxDataViewColumn
*col
);
470 virtual void DoSetExpanderColumn();
471 virtual void DoSetIndent();
473 virtual unsigned int GetColumnCount() const;
474 virtual wxDataViewColumn
* GetColumn( unsigned int pos
) const;
475 virtual bool DeleteColumn( wxDataViewColumn
*column
);
476 virtual bool ClearColumns();
477 virtual int GetColumnPosition( const wxDataViewColumn
*column
) const;
479 virtual wxDataViewColumn
*GetSortingColumn() const;
481 virtual wxDataViewItem
GetSelection() const;
482 virtual int GetSelections( wxDataViewItemArray
& sel
) const;
483 virtual void SetSelections( const wxDataViewItemArray
& sel
);
484 virtual void Select( const wxDataViewItem
& item
);
485 virtual void Unselect( const wxDataViewItem
& item
);
486 virtual bool IsSelected( const wxDataViewItem
& item
) const;
488 virtual void SelectAll();
489 virtual void UnselectAll();
491 virtual void EnsureVisible( const wxDataViewItem
& item
,
492 const wxDataViewColumn
*column
= NULL
);
493 virtual void HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
494 wxDataViewColumn
* &column
) const;
495 virtual wxRect
GetItemRect( const wxDataViewItem
& item
,
496 const wxDataViewColumn
*column
= NULL
) const;
498 virtual void Expand( const wxDataViewItem
& item
);
499 virtual void Collapse( const wxDataViewItem
& item
);
500 virtual bool IsExpanded( const wxDataViewItem
& item
) const;
502 virtual void SetFocus();
504 #if wxUSE_DRAG_AND_DROP
505 virtual bool EnableDragSource( const wxDataFormat
&format
);
506 virtual bool EnableDropTarget( const wxDataFormat
&format
);
507 #endif // wxUSE_DRAG_AND_DROP
509 virtual wxBorder
GetDefaultBorder() const;
511 void StartEditor( const wxDataViewItem
& item
, unsigned int column
);
514 virtual int GetSelections( wxArrayInt
& sel
) const;
515 virtual void SetSelections( const wxArrayInt
& sel
);
516 virtual void Select( int row
);
517 virtual void Unselect( int row
);
518 virtual bool IsSelected( int row
) const;
519 virtual void SelectRange( int from
, int to
);
520 virtual void UnselectRange( int from
, int to
);
522 virtual void EnsureVisible( int row
, int column
);
524 virtual wxDataViewItem
GetItemByRow( unsigned int row
) const;
525 virtual int GetRowByItem( const wxDataViewItem
& item
) const;
527 int GetSortingColumnIndex() const { return m_sortingColumnIdx
; }
528 void SetSortingColumnIndex(int idx
) { m_sortingColumnIdx
= idx
; }
530 public: // utility functions not part of the API
532 // returns the "best" width for the idx-th column
533 unsigned int GetBestColumnWidth(int WXUNUSED(idx
)) const
535 return GetClientSize().GetWidth() / GetColumnCount();
538 // called by header window after reorder
539 void ColumnMoved( wxDataViewColumn
* col
, unsigned int new_pos
);
541 // update the display after a change to an individual column
542 void OnColumnChange(unsigned int idx
);
544 // update after a change to the number of columns
545 void OnColumnsCountChanged();
547 wxWindow
*GetMainWindow() { return (wxWindow
*) m_clientArea
; }
549 // return the index of the given column in m_cols
550 int GetColumnIndex(const wxDataViewColumn
*column
) const;
552 // return the column displayed at the given position in the control
553 wxDataViewColumn
*GetColumnAt(unsigned int pos
) const;
556 wxDataViewColumnList m_cols
;
557 wxDataViewModelNotifier
*m_notifier
;
558 wxDataViewMainWindow
*m_clientArea
;
559 wxDataViewHeaderWindow
*m_headerArea
;
561 // the index of the column currently used for sorting or -1
562 int m_sortingColumnIdx
;
565 void OnSize( wxSizeEvent
&event
);
566 virtual wxSize
GetSizeAvailableForScrollTarget(const wxSize
& size
);
568 // we need to return a special WM_GETDLGCODE value to process just the
569 // arrows but let the other navigation characters through
571 virtual WXLRESULT
MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
);
574 WX_FORWARD_TO_SCROLL_HELPER()
577 DECLARE_DYNAMIC_CLASS(wxDataViewCtrl
)
578 wxDECLARE_NO_COPY_CLASS(wxDataViewCtrl
);
579 DECLARE_EVENT_TABLE()
583 #endif // __GENERICDATAVIEWCTRLH__