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 SetMode( wxDataViewCellMode mode
)
62 virtual wxDataViewCellMode
GetMode() const
65 virtual bool Activate( wxRect
WXUNUSED(cell
),
66 wxDataViewModel
*WXUNUSED(model
),
67 const wxDataViewItem
& WXUNUSED(item
),
68 unsigned int WXUNUSED(col
) )
71 virtual bool LeftClick( wxPoint
WXUNUSED(cursor
),
72 wxRect
WXUNUSED(cell
),
73 wxDataViewModel
*WXUNUSED(model
),
74 const wxDataViewItem
& WXUNUSED(item
),
75 unsigned int WXUNUSED(col
) )
77 virtual bool RightClick( wxPoint
WXUNUSED(cursor
),
78 wxRect
WXUNUSED(cell
),
79 wxDataViewModel
*WXUNUSED(model
),
80 const wxDataViewItem
& WXUNUSED(item
),
81 unsigned int WXUNUSED(col
) )
83 virtual bool StartDrag( wxPoint
WXUNUSED(cursor
),
84 wxRect
WXUNUSED(cell
),
85 wxDataViewModel
*WXUNUSED(model
),
86 const wxDataViewItem
& WXUNUSED(item
),
87 unsigned int WXUNUSED(col
) )
90 // Create DC on request
91 virtual wxDC
*GetDC();
94 int CalculateAlignment() const;
97 // This is just a convenience for the derived classes overriding
98 // RenderWithAttr() to avoid repeating the same wxFAIL_MSG() in all of them
99 bool DummyRender(wxRect
WXUNUSED(cell
),
103 wxFAIL_MSG("shouldn't be called at all, use RenderWithAttr() instead");
111 wxDataViewCellMode m_mode
;
113 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewRenderer
)
116 // ---------------------------------------------------------
117 // wxDataViewCustomRenderer
118 // ---------------------------------------------------------
120 class WXDLLIMPEXP_ADV wxDataViewCustomRenderer
: public wxDataViewRenderer
123 wxDataViewCustomRenderer( const wxString
&varianttype
= wxT("string"),
124 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
,
125 int align
= wxDVR_DEFAULT_ALIGNMENT
);
127 // Draw the text using the provided attributes
128 void RenderText(wxDC
& dc
,
131 const wxString
& text
,
132 const wxDataViewItemAttr
*attr
, // may be NULL if none
136 // Overload using standard attributes
137 void RenderText( const wxString
&text
, int xoffset
, wxRect cell
, wxDC
*dc
, int state
);
140 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewCustomRenderer
)
144 // ---------------------------------------------------------
145 // wxDataViewTextRenderer
146 // ---------------------------------------------------------
148 class WXDLLIMPEXP_ADV wxDataViewTextRenderer
: public wxDataViewCustomRenderer
151 wxDataViewTextRenderer( const wxString
&varianttype
= wxT("string"),
152 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
,
153 int align
= wxDVR_DEFAULT_ALIGNMENT
);
155 bool SetValue( const wxVariant
&value
);
156 bool GetValue( wxVariant
&value
) const;
158 virtual bool RenderWithAttr(wxDC
& dc
,
161 const wxDataViewItemAttr
*attr
,
163 virtual bool Render(wxRect cell
, wxDC
*dc
, int state
)
165 return DummyRender(cell
, dc
, state
);
168 wxSize
GetSize() const;
171 virtual bool HasEditorCtrl() const;
172 virtual wxControl
* CreateEditorCtrl( wxWindow
*parent
, wxRect labelRect
,
173 const wxVariant
&value
);
174 virtual bool GetValueFromEditorCtrl( wxControl
* editor
, wxVariant
&value
);
180 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewTextRenderer
)
183 // ---------------------------------------------------------
184 // wxDataViewBitmapRenderer
185 // ---------------------------------------------------------
187 class WXDLLIMPEXP_ADV wxDataViewBitmapRenderer
: public wxDataViewCustomRenderer
190 wxDataViewBitmapRenderer( const wxString
&varianttype
= wxT("wxBitmap"),
191 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
,
192 int align
= wxDVR_DEFAULT_ALIGNMENT
);
194 bool SetValue( const wxVariant
&value
);
195 bool GetValue( wxVariant
&value
) const;
197 bool Render( wxRect cell
, wxDC
*dc
, int state
);
198 wxSize
GetSize() const;
205 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewBitmapRenderer
)
208 // ---------------------------------------------------------
209 // wxDataViewToggleRenderer
210 // ---------------------------------------------------------
212 class WXDLLIMPEXP_ADV wxDataViewToggleRenderer
: public wxDataViewCustomRenderer
215 wxDataViewToggleRenderer( const wxString
&varianttype
= wxT("bool"),
216 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
,
217 int align
= wxDVR_DEFAULT_ALIGNMENT
);
219 bool SetValue( const wxVariant
&value
);
220 bool GetValue( wxVariant
&value
) const;
222 bool Render( wxRect cell
, wxDC
*dc
, int state
);
223 bool Activate( wxRect cell
, wxDataViewModel
*model
, const wxDataViewItem
& item
,
225 wxSize
GetSize() const;
231 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewToggleRenderer
)
234 // ---------------------------------------------------------
235 // wxDataViewProgressRenderer
236 // ---------------------------------------------------------
238 class WXDLLIMPEXP_ADV wxDataViewProgressRenderer
: public wxDataViewCustomRenderer
241 wxDataViewProgressRenderer( const wxString
&label
= wxEmptyString
,
242 const wxString
&varianttype
= wxT("long"),
243 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
,
244 int align
= wxDVR_DEFAULT_ALIGNMENT
);
246 bool SetValue( const wxVariant
&value
);
247 bool GetValue( wxVariant
& value
) const;
249 virtual bool RenderWithAttr(wxDC
& dc
,
252 const wxDataViewItemAttr
*attr
,
254 virtual bool Render(wxRect cell
, wxDC
*dc
, int state
)
256 return DummyRender(cell
, dc
, state
);
258 virtual wxSize
GetSize() const;
265 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewProgressRenderer
)
268 // ---------------------------------------------------------
269 // wxDataViewIconTextRenderer
270 // ---------------------------------------------------------
272 class WXDLLIMPEXP_ADV wxDataViewIconTextRenderer
: public wxDataViewCustomRenderer
275 wxDataViewIconTextRenderer( const wxString
&varianttype
= wxT("wxDataViewIconText"),
276 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
,
277 int align
= wxDVR_DEFAULT_ALIGNMENT
);
278 virtual ~wxDataViewIconTextRenderer();
280 bool SetValue( const wxVariant
&value
);
281 bool GetValue( wxVariant
&value
) const;
283 virtual bool Render( wxRect cell
, wxDC
*dc
, int state
);
284 virtual wxSize
GetSize() const;
286 virtual bool HasEditorCtrl() const { return true; }
287 virtual wxControl
* CreateEditorCtrl( wxWindow
*parent
, wxRect labelRect
,
288 const wxVariant
&value
);
289 virtual bool GetValueFromEditorCtrl( wxControl
* editor
, wxVariant
&value
);
292 wxDataViewIconText m_value
;
295 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewIconTextRenderer
)
298 // ---------------------------------------------------------
299 // wxDataViewDateRenderer
300 // ---------------------------------------------------------
302 class WXDLLIMPEXP_ADV wxDataViewDateRenderer
: public wxDataViewCustomRenderer
305 wxDataViewDateRenderer( const wxString
&varianttype
= wxT("datetime"),
306 wxDataViewCellMode mode
= wxDATAVIEW_CELL_ACTIVATABLE
,
307 int align
= wxDVR_DEFAULT_ALIGNMENT
);
309 bool SetValue( const wxVariant
&value
);
310 bool GetValue( wxVariant
& value
) const;
312 virtual bool Render( wxRect cell
, wxDC
*dc
, int state
);
313 virtual wxSize
GetSize() const;
314 virtual bool Activate( wxRect cell
,
315 wxDataViewModel
*model
,
316 const wxDataViewItem
& item
,
323 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewDateRenderer
)
326 // ---------------------------------------------------------
328 // ---------------------------------------------------------
330 class WXDLLIMPEXP_ADV wxDataViewColumn
: public wxDataViewColumnBase
333 wxDataViewColumn(const wxString
& title
,
334 wxDataViewRenderer
*renderer
,
335 unsigned int model_column
,
336 int width
= wxDVC_DEFAULT_WIDTH
,
337 wxAlignment align
= wxALIGN_CENTER
,
338 int flags
= wxDATAVIEW_COL_RESIZABLE
)
339 : wxDataViewColumnBase(renderer
, model_column
),
342 Init(width
, align
, flags
);
345 wxDataViewColumn(const wxBitmap
& bitmap
,
346 wxDataViewRenderer
*renderer
,
347 unsigned int model_column
,
348 int width
= wxDVC_DEFAULT_WIDTH
,
349 wxAlignment align
= wxALIGN_CENTER
,
350 int flags
= wxDATAVIEW_COL_RESIZABLE
)
351 : wxDataViewColumnBase(bitmap
, renderer
, model_column
)
353 Init(width
, align
, flags
);
356 // implement wxHeaderColumnBase methods
357 virtual void SetTitle(const wxString
& title
) { m_title
= title
; }
358 virtual wxString
GetTitle() const { return m_title
; }
360 virtual void SetWidth(int width
) { m_width
= width
; }
361 virtual int GetWidth() const { return m_width
; }
363 virtual void SetMinWidth(int minWidth
) { m_minWidth
= minWidth
; }
364 virtual int GetMinWidth() const { return m_minWidth
; }
366 virtual void SetAlignment(wxAlignment align
) { m_align
= align
; }
367 virtual wxAlignment
GetAlignment() const { return m_align
; }
369 virtual void SetFlags(int flags
) { m_flags
= flags
; }
370 virtual int GetFlags() const { return m_flags
; }
372 virtual void SetAsSortKey(bool sort
= true) { m_sort
= sort
; }
373 virtual bool IsSortKey() const { return m_sort
; }
375 virtual void SetSortOrder(bool ascending
) { m_sortAscending
= ascending
; }
376 virtual bool IsSortOrderAscending() const { return m_sortAscending
; }
379 // common part of all ctors
380 void Init(int width
, wxAlignment align
, int flags
)
382 m_width
= width
== wxCOL_WIDTH_DEFAULT ? wxDVC_DEFAULT_WIDTH
: width
;
387 m_sortAscending
= true;
398 friend class wxDataViewHeaderWindowBase
;
399 friend class wxDataViewHeaderWindow
;
400 friend class wxDataViewHeaderWindowMSW
;
403 // ---------------------------------------------------------
405 // ---------------------------------------------------------
407 WX_DECLARE_LIST_WITH_DECL(wxDataViewColumn
, wxDataViewColumnList
,
408 class WXDLLIMPEXP_ADV
);
410 class WXDLLIMPEXP_ADV wxDataViewCtrl
: public wxDataViewCtrlBase
,
411 public wxScrollHelper
413 friend class wxDataViewMainWindow
;
414 friend class wxDataViewHeaderWindowBase
;
415 friend class wxDataViewHeaderWindow
;
416 friend class wxDataViewHeaderWindowMSW
;
417 friend class wxDataViewColumn
;
420 wxDataViewCtrl() : wxScrollHelper(this)
425 wxDataViewCtrl( wxWindow
*parent
, wxWindowID id
,
426 const wxPoint
& pos
= wxDefaultPosition
,
427 const wxSize
& size
= wxDefaultSize
, long style
= 0,
428 const wxValidator
& validator
= wxDefaultValidator
)
429 : wxScrollHelper(this)
431 Create(parent
, id
, pos
, size
, style
, validator
);
434 virtual ~wxDataViewCtrl();
438 bool Create(wxWindow
*parent
, wxWindowID id
,
439 const wxPoint
& pos
= wxDefaultPosition
,
440 const wxSize
& size
= wxDefaultSize
, long style
= 0,
441 const wxValidator
& validator
= wxDefaultValidator
);
443 virtual bool AssociateModel( wxDataViewModel
*model
);
445 virtual bool AppendColumn( wxDataViewColumn
*col
);
446 virtual bool PrependColumn( wxDataViewColumn
*col
);
447 virtual bool InsertColumn( unsigned int pos
, wxDataViewColumn
*col
);
449 virtual void DoSetExpanderColumn();
450 virtual void DoSetIndent();
452 virtual unsigned int GetColumnCount() const;
453 virtual wxDataViewColumn
* GetColumn( unsigned int pos
) const;
454 virtual bool DeleteColumn( wxDataViewColumn
*column
);
455 virtual bool ClearColumns();
456 virtual int GetColumnPosition( const wxDataViewColumn
*column
) const;
458 virtual wxDataViewColumn
*GetSortingColumn() const;
460 virtual wxDataViewItem
GetSelection() const;
461 virtual int GetSelections( wxDataViewItemArray
& sel
) const;
462 virtual void SetSelections( const wxDataViewItemArray
& sel
);
463 virtual void Select( const wxDataViewItem
& item
);
464 virtual void Unselect( const wxDataViewItem
& item
);
465 virtual bool IsSelected( const wxDataViewItem
& item
) const;
467 virtual void SelectAll();
468 virtual void UnselectAll();
470 virtual void EnsureVisible( const wxDataViewItem
& item
,
471 const wxDataViewColumn
*column
= NULL
);
472 virtual void HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
473 wxDataViewColumn
* &column
) const;
474 virtual wxRect
GetItemRect( const wxDataViewItem
& item
,
475 const wxDataViewColumn
*column
= NULL
) const;
477 virtual void Expand( const wxDataViewItem
& item
);
478 virtual void Collapse( const wxDataViewItem
& item
);
479 virtual bool IsExpanded( const wxDataViewItem
& item
) const;
481 virtual void SetFocus();
483 #if wxUSE_DRAG_AND_DROP
484 virtual bool EnableDragSource( const wxDataFormat
&format
);
485 virtual bool EnableDropTarget( const wxDataFormat
&format
);
486 #endif // wxUSE_DRAG_AND_DROP
488 virtual wxBorder
GetDefaultBorder() const;
490 void StartEditor( const wxDataViewItem
& item
, unsigned int column
);
493 virtual int GetSelections( wxArrayInt
& sel
) const;
494 virtual void SetSelections( const wxArrayInt
& sel
);
495 virtual void Select( int row
);
496 virtual void Unselect( int row
);
497 virtual bool IsSelected( int row
) const;
498 virtual void SelectRange( int from
, int to
);
499 virtual void UnselectRange( int from
, int to
);
501 virtual void EnsureVisible( int row
, int column
);
503 virtual wxDataViewItem
GetItemByRow( unsigned int row
) const;
504 virtual int GetRowByItem( const wxDataViewItem
& item
) const;
506 int GetSortingColumnIndex() const { return m_sortingColumnIdx
; }
507 void SetSortingColumnIndex(int idx
) { m_sortingColumnIdx
= idx
; }
509 public: // utility functions not part of the API
511 // returns the "best" width for the idx-th column
512 unsigned int GetBestColumnWidth(int WXUNUSED(idx
)) const
514 return GetClientSize().GetWidth() / GetColumnCount();
517 // called by header window after reorder
518 void ColumnMoved( wxDataViewColumn
* col
, unsigned int new_pos
);
520 // update the display after a change to an individual column
521 void OnColumnChange(unsigned int idx
);
523 // update after a change to the number of columns
524 void OnColumnsCountChanged();
526 wxWindow
*GetMainWindow() { return (wxWindow
*) m_clientArea
; }
528 // return the index of the given column in m_cols
529 int GetColumnIndex(const wxDataViewColumn
*column
) const;
531 // return the column displayed at the given position in the control
532 wxDataViewColumn
*GetColumnAt(unsigned int pos
) const;
535 wxDataViewColumnList m_cols
;
536 wxDataViewModelNotifier
*m_notifier
;
537 wxDataViewMainWindow
*m_clientArea
;
538 wxDataViewHeaderWindow
*m_headerArea
;
540 // the index of the column currently used for sorting or -1
541 int m_sortingColumnIdx
;
544 void OnSize( wxSizeEvent
&event
);
545 virtual wxSize
GetSizeAvailableForScrollTarget(const wxSize
& size
);
547 // we need to return a special WM_GETDLGCODE value to process just the
548 // arrows but let the other navigation characters through
550 virtual WXLRESULT
MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
);
553 WX_FORWARD_TO_SCROLL_HELPER()
556 DECLARE_DYNAMIC_CLASS(wxDataViewCtrl
)
557 wxDECLARE_NO_COPY_CLASS(wxDataViewCtrl
);
558 DECLARE_EVENT_TABLE()
562 #endif // __GENERICDATAVIEWCTRLH__