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 StartDrag( wxPoint
WXUNUSED(cursor
),
83 wxRect
WXUNUSED(cell
),
84 wxDataViewModel
*WXUNUSED(model
),
85 const wxDataViewItem
& WXUNUSED(item
),
86 unsigned int WXUNUSED(col
) )
89 // Create DC on request
90 virtual wxDC
*GetDC();
93 int CalculateAlignment() const;
96 // This is just a convenience for the derived classes overriding
97 // RenderWithAttr() to avoid repeating the same wxFAIL_MSG() in all of them
98 bool DummyRender(wxRect
WXUNUSED(cell
),
102 wxFAIL_MSG("shouldn't be called at all, use RenderWithAttr() instead");
110 wxDataViewCellMode m_mode
;
112 wxEllipsizeMode m_ellipsizeMode
;
114 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewRenderer
)
117 // ---------------------------------------------------------
118 // wxDataViewCustomRenderer
119 // ---------------------------------------------------------
121 class WXDLLIMPEXP_ADV wxDataViewCustomRenderer
: public wxDataViewRenderer
124 wxDataViewCustomRenderer( const wxString
&varianttype
= wxT("string"),
125 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
,
126 int align
= wxDVR_DEFAULT_ALIGNMENT
);
128 // Draw the text using the provided attributes
129 void RenderText(wxDC
& dc
,
132 const wxString
& text
,
133 const wxDataViewItemAttr
*attr
, // may be NULL if none
137 // Overload using standard attributes
138 void RenderText(const wxString
& text
,
144 RenderText(*dc
, cell
, wxALIGN_NOT
, text
, NULL
, state
, xoffset
);
148 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewCustomRenderer
)
152 // ---------------------------------------------------------
153 // wxDataViewTextRenderer
154 // ---------------------------------------------------------
156 class WXDLLIMPEXP_ADV wxDataViewTextRenderer
: public wxDataViewCustomRenderer
159 wxDataViewTextRenderer( const wxString
&varianttype
= wxT("string"),
160 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
,
161 int align
= wxDVR_DEFAULT_ALIGNMENT
);
163 bool SetValue( const wxVariant
&value
);
164 bool GetValue( wxVariant
&value
) const;
166 virtual bool RenderWithAttr(wxDC
& dc
,
169 const wxDataViewItemAttr
*attr
,
171 virtual bool Render(wxRect cell
, wxDC
*dc
, int state
)
173 return DummyRender(cell
, dc
, state
);
176 wxSize
GetSize() const;
179 virtual bool HasEditorCtrl() const;
180 virtual wxControl
* CreateEditorCtrl( wxWindow
*parent
, wxRect labelRect
,
181 const wxVariant
&value
);
182 virtual bool GetValueFromEditorCtrl( wxControl
* editor
, wxVariant
&value
);
188 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewTextRenderer
)
191 // ---------------------------------------------------------
192 // wxDataViewBitmapRenderer
193 // ---------------------------------------------------------
195 class WXDLLIMPEXP_ADV wxDataViewBitmapRenderer
: public wxDataViewCustomRenderer
198 wxDataViewBitmapRenderer( const wxString
&varianttype
= wxT("wxBitmap"),
199 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
,
200 int align
= wxDVR_DEFAULT_ALIGNMENT
);
202 bool SetValue( const wxVariant
&value
);
203 bool GetValue( wxVariant
&value
) const;
205 bool Render( wxRect cell
, wxDC
*dc
, int state
);
206 wxSize
GetSize() const;
213 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewBitmapRenderer
)
216 // ---------------------------------------------------------
217 // wxDataViewToggleRenderer
218 // ---------------------------------------------------------
220 class WXDLLIMPEXP_ADV wxDataViewToggleRenderer
: public wxDataViewCustomRenderer
223 wxDataViewToggleRenderer( const wxString
&varianttype
= wxT("bool"),
224 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
,
225 int align
= wxDVR_DEFAULT_ALIGNMENT
);
227 bool SetValue( const wxVariant
&value
);
228 bool GetValue( wxVariant
&value
) const;
230 bool Render( wxRect cell
, wxDC
*dc
, int state
);
231 bool Activate( wxRect cell
, wxDataViewModel
*model
, const wxDataViewItem
& item
,
233 wxSize
GetSize() const;
239 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewToggleRenderer
)
242 // ---------------------------------------------------------
243 // wxDataViewProgressRenderer
244 // ---------------------------------------------------------
246 class WXDLLIMPEXP_ADV wxDataViewProgressRenderer
: public wxDataViewCustomRenderer
249 wxDataViewProgressRenderer( const wxString
&label
= wxEmptyString
,
250 const wxString
&varianttype
= wxT("long"),
251 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
,
252 int align
= wxDVR_DEFAULT_ALIGNMENT
);
254 bool SetValue( const wxVariant
&value
);
255 bool GetValue( wxVariant
& value
) const;
257 virtual bool RenderWithAttr(wxDC
& dc
,
260 const wxDataViewItemAttr
*attr
,
262 virtual bool Render(wxRect cell
, wxDC
*dc
, int state
)
264 return DummyRender(cell
, dc
, state
);
266 virtual wxSize
GetSize() const;
273 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewProgressRenderer
)
276 // ---------------------------------------------------------
277 // wxDataViewIconTextRenderer
278 // ---------------------------------------------------------
280 class WXDLLIMPEXP_ADV wxDataViewIconTextRenderer
: public wxDataViewCustomRenderer
283 wxDataViewIconTextRenderer( const wxString
&varianttype
= wxT("wxDataViewIconText"),
284 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
,
285 int align
= wxDVR_DEFAULT_ALIGNMENT
);
287 bool SetValue( const wxVariant
&value
);
288 bool GetValue( wxVariant
&value
) const;
290 virtual bool RenderWithAttr(wxDC
& dc
,
293 const wxDataViewItemAttr
*attr
,
295 virtual bool Render(wxRect cell
, wxDC
*dc
, int state
)
297 return DummyRender(cell
, dc
, state
);
299 virtual wxSize
GetSize() const;
301 virtual bool HasEditorCtrl() const { return true; }
302 virtual wxControl
* CreateEditorCtrl( wxWindow
*parent
, wxRect labelRect
,
303 const wxVariant
&value
);
304 virtual bool GetValueFromEditorCtrl( wxControl
* editor
, wxVariant
&value
);
307 wxDataViewIconText m_value
;
310 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewIconTextRenderer
)
313 // ---------------------------------------------------------
314 // wxDataViewDateRenderer
315 // ---------------------------------------------------------
317 class WXDLLIMPEXP_ADV wxDataViewDateRenderer
: public wxDataViewCustomRenderer
320 wxDataViewDateRenderer( const wxString
&varianttype
= wxT("datetime"),
321 wxDataViewCellMode mode
= wxDATAVIEW_CELL_ACTIVATABLE
,
322 int align
= wxDVR_DEFAULT_ALIGNMENT
);
324 bool SetValue( const wxVariant
&value
);
325 bool GetValue( wxVariant
& value
) const;
327 virtual bool Render( wxRect cell
, wxDC
*dc
, int state
);
328 virtual wxSize
GetSize() const;
329 virtual bool Activate( wxRect cell
,
330 wxDataViewModel
*model
,
331 const wxDataViewItem
& item
,
338 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewDateRenderer
)
341 // ---------------------------------------------------------
343 // ---------------------------------------------------------
345 class WXDLLIMPEXP_ADV wxDataViewColumn
: public wxDataViewColumnBase
348 wxDataViewColumn(const wxString
& title
,
349 wxDataViewRenderer
*renderer
,
350 unsigned int model_column
,
351 int width
= wxDVC_DEFAULT_WIDTH
,
352 wxAlignment align
= wxALIGN_CENTER
,
353 int flags
= wxDATAVIEW_COL_RESIZABLE
)
354 : wxDataViewColumnBase(renderer
, model_column
),
357 Init(width
, align
, flags
);
360 wxDataViewColumn(const wxBitmap
& bitmap
,
361 wxDataViewRenderer
*renderer
,
362 unsigned int model_column
,
363 int width
= wxDVC_DEFAULT_WIDTH
,
364 wxAlignment align
= wxALIGN_CENTER
,
365 int flags
= wxDATAVIEW_COL_RESIZABLE
)
366 : wxDataViewColumnBase(bitmap
, renderer
, model_column
)
368 Init(width
, align
, flags
);
371 // implement wxHeaderColumnBase methods
372 virtual void SetTitle(const wxString
& title
) { m_title
= title
; }
373 virtual wxString
GetTitle() const { return m_title
; }
375 virtual void SetWidth(int width
) { m_width
= width
; }
376 virtual int GetWidth() const { return m_width
; }
378 virtual void SetMinWidth(int minWidth
) { m_minWidth
= minWidth
; }
379 virtual int GetMinWidth() const { return m_minWidth
; }
381 virtual void SetAlignment(wxAlignment align
) { m_align
= align
; }
382 virtual wxAlignment
GetAlignment() const { return m_align
; }
384 virtual void SetFlags(int flags
) { m_flags
= flags
; }
385 virtual int GetFlags() const { return m_flags
; }
387 virtual void SetAsSortKey(bool sort
= true) { m_sort
= sort
; }
388 virtual bool IsSortKey() const { return m_sort
; }
390 virtual void SetSortOrder(bool ascending
) { m_sortAscending
= ascending
; }
391 virtual bool IsSortOrderAscending() const { return m_sortAscending
; }
394 // common part of all ctors
395 void Init(int width
, wxAlignment align
, int flags
)
397 m_width
= width
== wxCOL_WIDTH_DEFAULT
? wxDVC_DEFAULT_WIDTH
: width
;
402 m_sortAscending
= true;
413 friend class wxDataViewHeaderWindowBase
;
414 friend class wxDataViewHeaderWindow
;
415 friend class wxDataViewHeaderWindowMSW
;
418 // ---------------------------------------------------------
420 // ---------------------------------------------------------
422 WX_DECLARE_LIST_WITH_DECL(wxDataViewColumn
, wxDataViewColumnList
,
423 class WXDLLIMPEXP_ADV
);
425 class WXDLLIMPEXP_ADV wxDataViewCtrl
: public wxDataViewCtrlBase
,
426 public wxScrollHelper
428 friend class wxDataViewMainWindow
;
429 friend class wxDataViewHeaderWindowBase
;
430 friend class wxDataViewHeaderWindow
;
431 friend class wxDataViewHeaderWindowMSW
;
432 friend class wxDataViewColumn
;
435 wxDataViewCtrl() : wxScrollHelper(this)
440 wxDataViewCtrl( wxWindow
*parent
, wxWindowID id
,
441 const wxPoint
& pos
= wxDefaultPosition
,
442 const wxSize
& size
= wxDefaultSize
, long style
= 0,
443 const wxValidator
& validator
= wxDefaultValidator
)
444 : wxScrollHelper(this)
446 Create(parent
, id
, pos
, size
, style
, validator
);
449 virtual ~wxDataViewCtrl();
453 bool Create(wxWindow
*parent
, wxWindowID id
,
454 const wxPoint
& pos
= wxDefaultPosition
,
455 const wxSize
& size
= wxDefaultSize
, long style
= 0,
456 const wxValidator
& validator
= wxDefaultValidator
);
458 virtual bool AssociateModel( wxDataViewModel
*model
);
460 virtual bool AppendColumn( wxDataViewColumn
*col
);
461 virtual bool PrependColumn( wxDataViewColumn
*col
);
462 virtual bool InsertColumn( unsigned int pos
, wxDataViewColumn
*col
);
464 virtual void DoSetExpanderColumn();
465 virtual void DoSetIndent();
467 virtual unsigned int GetColumnCount() const;
468 virtual wxDataViewColumn
* GetColumn( unsigned int pos
) const;
469 virtual bool DeleteColumn( wxDataViewColumn
*column
);
470 virtual bool ClearColumns();
471 virtual int GetColumnPosition( const wxDataViewColumn
*column
) const;
473 virtual wxDataViewColumn
*GetSortingColumn() const;
475 virtual wxDataViewItem
GetSelection() const;
476 virtual int GetSelections( wxDataViewItemArray
& sel
) const;
477 virtual void SetSelections( const wxDataViewItemArray
& sel
);
478 virtual void Select( const wxDataViewItem
& item
);
479 virtual void Unselect( const wxDataViewItem
& item
);
480 virtual bool IsSelected( const wxDataViewItem
& item
) const;
482 virtual void SelectAll();
483 virtual void UnselectAll();
485 virtual void EnsureVisible( const wxDataViewItem
& item
,
486 const wxDataViewColumn
*column
= NULL
);
487 virtual void HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
488 wxDataViewColumn
* &column
) const;
489 virtual wxRect
GetItemRect( const wxDataViewItem
& item
,
490 const wxDataViewColumn
*column
= NULL
) const;
492 virtual void Expand( const wxDataViewItem
& item
);
493 virtual void Collapse( const wxDataViewItem
& item
);
494 virtual bool IsExpanded( const wxDataViewItem
& item
) const;
496 virtual void SetFocus();
498 #if wxUSE_DRAG_AND_DROP
499 virtual bool EnableDragSource( const wxDataFormat
&format
);
500 virtual bool EnableDropTarget( const wxDataFormat
&format
);
501 #endif // wxUSE_DRAG_AND_DROP
503 virtual wxBorder
GetDefaultBorder() const;
505 void StartEditor( const wxDataViewItem
& item
, unsigned int column
);
508 virtual int GetSelections( wxArrayInt
& sel
) const;
509 virtual void SetSelections( const wxArrayInt
& sel
);
510 virtual void Select( int row
);
511 virtual void Unselect( int row
);
512 virtual bool IsSelected( int row
) const;
513 virtual void SelectRange( int from
, int to
);
514 virtual void UnselectRange( int from
, int to
);
516 virtual void EnsureVisible( int row
, int column
);
518 virtual wxDataViewItem
GetItemByRow( unsigned int row
) const;
519 virtual int GetRowByItem( const wxDataViewItem
& item
) const;
521 int GetSortingColumnIndex() const { return m_sortingColumnIdx
; }
522 void SetSortingColumnIndex(int idx
) { m_sortingColumnIdx
= idx
; }
524 public: // utility functions not part of the API
526 // returns the "best" width for the idx-th column
527 unsigned int GetBestColumnWidth(int WXUNUSED(idx
)) const
529 return GetClientSize().GetWidth() / GetColumnCount();
532 // called by header window after reorder
533 void ColumnMoved( wxDataViewColumn
* col
, unsigned int new_pos
);
535 // update the display after a change to an individual column
536 void OnColumnChange(unsigned int idx
);
538 // update after a change to the number of columns
539 void OnColumnsCountChanged();
541 wxWindow
*GetMainWindow() { return (wxWindow
*) m_clientArea
; }
543 // return the index of the given column in m_cols
544 int GetColumnIndex(const wxDataViewColumn
*column
) const;
546 // return the column displayed at the given position in the control
547 wxDataViewColumn
*GetColumnAt(unsigned int pos
) const;
550 wxDataViewColumnList m_cols
;
551 wxDataViewModelNotifier
*m_notifier
;
552 wxDataViewMainWindow
*m_clientArea
;
553 wxDataViewHeaderWindow
*m_headerArea
;
555 // the index of the column currently used for sorting or -1
556 int m_sortingColumnIdx
;
559 void OnSize( wxSizeEvent
&event
);
560 virtual wxSize
GetSizeAvailableForScrollTarget(const wxSize
& size
);
562 // we need to return a special WM_GETDLGCODE value to process just the
563 // arrows but let the other navigation characters through
565 virtual WXLRESULT
MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
);
568 WX_FORWARD_TO_SCROLL_HELPER()
571 DECLARE_DYNAMIC_CLASS(wxDataViewCtrl
)
572 wxDECLARE_NO_COPY_CLASS(wxDataViewCtrl
);
573 DECLARE_EVENT_TABLE()
577 #endif // __GENERICDATAVIEWCTRLH__