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;
99 wxDataViewCellMode m_mode
;
102 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewRenderer
)
105 // ---------------------------------------------------------
106 // wxDataViewCustomRenderer
107 // ---------------------------------------------------------
109 class WXDLLIMPEXP_ADV wxDataViewCustomRenderer
: public wxDataViewRenderer
112 wxDataViewCustomRenderer( const wxString
&varianttype
= wxT("string"),
113 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
,
114 int align
= wxDVR_DEFAULT_ALIGNMENT
);
116 // Draw the text using the provided attributes
117 void RenderText(wxDC
& dc
,
120 const wxString
& text
,
121 const wxDataViewItemAttr
*attr
, // may be NULL if none
125 // Overload using standard attributes
126 void RenderText( const wxString
&text
, int xoffset
, wxRect cell
, wxDC
*dc
, int state
);
129 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewCustomRenderer
)
133 // ---------------------------------------------------------
134 // wxDataViewTextRenderer
135 // ---------------------------------------------------------
137 class WXDLLIMPEXP_ADV wxDataViewTextRenderer
: public wxDataViewCustomRenderer
140 wxDataViewTextRenderer( const wxString
&varianttype
= wxT("string"),
141 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
,
142 int align
= wxDVR_DEFAULT_ALIGNMENT
);
144 bool SetValue( const wxVariant
&value
);
145 bool GetValue( wxVariant
&value
) const;
147 virtual bool RenderWithAttr(wxDC
& dc
,
150 const wxDataViewItemAttr
*attr
,
152 virtual bool Render(wxRect
WXUNUSED(cell
),
156 wxFAIL_MSG("only RenderWithAttr() should be called");
160 wxSize
GetSize() const;
163 virtual bool HasEditorCtrl() const;
164 virtual wxControl
* CreateEditorCtrl( wxWindow
*parent
, wxRect labelRect
,
165 const wxVariant
&value
);
166 virtual bool GetValueFromEditorCtrl( wxControl
* editor
, wxVariant
&value
);
172 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewTextRenderer
)
175 // ---------------------------------------------------------
176 // wxDataViewBitmapRenderer
177 // ---------------------------------------------------------
179 class WXDLLIMPEXP_ADV wxDataViewBitmapRenderer
: public wxDataViewCustomRenderer
182 wxDataViewBitmapRenderer( const wxString
&varianttype
= wxT("wxBitmap"),
183 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
,
184 int align
= wxDVR_DEFAULT_ALIGNMENT
);
186 bool SetValue( const wxVariant
&value
);
187 bool GetValue( wxVariant
&value
) const;
189 bool Render( wxRect cell
, wxDC
*dc
, int state
);
190 wxSize
GetSize() const;
197 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewBitmapRenderer
)
200 // ---------------------------------------------------------
201 // wxDataViewToggleRenderer
202 // ---------------------------------------------------------
204 class WXDLLIMPEXP_ADV wxDataViewToggleRenderer
: public wxDataViewCustomRenderer
207 wxDataViewToggleRenderer( const wxString
&varianttype
= wxT("bool"),
208 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
,
209 int align
= wxDVR_DEFAULT_ALIGNMENT
);
211 bool SetValue( const wxVariant
&value
);
212 bool GetValue( wxVariant
&value
) const;
214 bool Render( wxRect cell
, wxDC
*dc
, int state
);
215 bool Activate( wxRect cell
, wxDataViewModel
*model
, const wxDataViewItem
& item
,
217 wxSize
GetSize() const;
223 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewToggleRenderer
)
226 // ---------------------------------------------------------
227 // wxDataViewProgressRenderer
228 // ---------------------------------------------------------
230 class WXDLLIMPEXP_ADV wxDataViewProgressRenderer
: public wxDataViewCustomRenderer
233 wxDataViewProgressRenderer( const wxString
&label
= wxEmptyString
,
234 const wxString
&varianttype
= wxT("long"),
235 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
,
236 int align
= wxDVR_DEFAULT_ALIGNMENT
);
237 virtual ~wxDataViewProgressRenderer();
239 bool SetValue( const wxVariant
&value
);
240 bool GetValue( wxVariant
& value
) const;
242 virtual bool Render( wxRect cell
, wxDC
*dc
, int state
);
243 virtual wxSize
GetSize() const;
250 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewProgressRenderer
)
253 // ---------------------------------------------------------
254 // wxDataViewIconTextRenderer
255 // ---------------------------------------------------------
257 class WXDLLIMPEXP_ADV wxDataViewIconTextRenderer
: public wxDataViewCustomRenderer
260 wxDataViewIconTextRenderer( const wxString
&varianttype
= wxT("wxDataViewIconText"),
261 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
,
262 int align
= wxDVR_DEFAULT_ALIGNMENT
);
263 virtual ~wxDataViewIconTextRenderer();
265 bool SetValue( const wxVariant
&value
);
266 bool GetValue( wxVariant
&value
) const;
268 virtual bool Render( wxRect cell
, wxDC
*dc
, int state
);
269 virtual wxSize
GetSize() const;
271 virtual bool HasEditorCtrl() const { return true; }
272 virtual wxControl
* CreateEditorCtrl( wxWindow
*parent
, wxRect labelRect
,
273 const wxVariant
&value
);
274 virtual bool GetValueFromEditorCtrl( wxControl
* editor
, wxVariant
&value
);
277 wxDataViewIconText m_value
;
280 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewIconTextRenderer
)
283 // ---------------------------------------------------------
284 // wxDataViewDateRenderer
285 // ---------------------------------------------------------
287 class WXDLLIMPEXP_ADV wxDataViewDateRenderer
: public wxDataViewCustomRenderer
290 wxDataViewDateRenderer( const wxString
&varianttype
= wxT("datetime"),
291 wxDataViewCellMode mode
= wxDATAVIEW_CELL_ACTIVATABLE
,
292 int align
= wxDVR_DEFAULT_ALIGNMENT
);
294 bool SetValue( const wxVariant
&value
);
295 bool GetValue( wxVariant
& value
) const;
297 virtual bool Render( wxRect cell
, wxDC
*dc
, int state
);
298 virtual wxSize
GetSize() const;
299 virtual bool Activate( wxRect cell
,
300 wxDataViewModel
*model
,
301 const wxDataViewItem
& item
,
308 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewDateRenderer
)
311 // ---------------------------------------------------------
313 // ---------------------------------------------------------
315 class WXDLLIMPEXP_ADV wxDataViewColumn
: public wxDataViewColumnBase
318 wxDataViewColumn(const wxString
& title
,
319 wxDataViewRenderer
*renderer
,
320 unsigned int model_column
,
321 int width
= wxDVC_DEFAULT_WIDTH
,
322 wxAlignment align
= wxALIGN_CENTER
,
323 int flags
= wxDATAVIEW_COL_RESIZABLE
)
324 : wxDataViewColumnBase(renderer
, model_column
),
327 Init(width
, align
, flags
);
330 wxDataViewColumn(const wxBitmap
& bitmap
,
331 wxDataViewRenderer
*renderer
,
332 unsigned int model_column
,
333 int width
= wxDVC_DEFAULT_WIDTH
,
334 wxAlignment align
= wxALIGN_CENTER
,
335 int flags
= wxDATAVIEW_COL_RESIZABLE
)
336 : wxDataViewColumnBase(bitmap
, renderer
, model_column
)
338 Init(width
, align
, flags
);
341 // implement wxHeaderColumnBase methods
342 virtual void SetTitle(const wxString
& title
) { m_title
= title
; }
343 virtual wxString
GetTitle() const { return m_title
; }
345 virtual void SetWidth(int width
) { m_width
= width
; }
346 virtual int GetWidth() const { return m_width
; }
348 virtual void SetMinWidth(int minWidth
) { m_minWidth
= minWidth
; }
349 virtual int GetMinWidth() const { return m_minWidth
; }
351 virtual void SetAlignment(wxAlignment align
) { m_align
= align
; }
352 virtual wxAlignment
GetAlignment() const { return m_align
; }
354 virtual void SetFlags(int flags
) { m_flags
= flags
; }
355 virtual int GetFlags() const { return m_flags
; }
357 virtual void SetAsSortKey(bool sort
= true) { m_sort
= sort
; }
358 virtual bool IsSortKey() const { return m_sort
; }
360 virtual void SetSortOrder(bool ascending
) { m_sortAscending
= ascending
; }
361 virtual bool IsSortOrderAscending() const { return m_sortAscending
; }
364 // common part of all ctors
365 void Init(int width
, wxAlignment align
, int flags
)
367 m_width
= width
== wxCOL_WIDTH_DEFAULT
? wxDVC_DEFAULT_WIDTH
: width
;
372 m_sortAscending
= true;
383 friend class wxDataViewHeaderWindowBase
;
384 friend class wxDataViewHeaderWindow
;
385 friend class wxDataViewHeaderWindowMSW
;
388 // ---------------------------------------------------------
390 // ---------------------------------------------------------
392 WX_DECLARE_LIST_WITH_DECL(wxDataViewColumn
, wxDataViewColumnList
,
393 class WXDLLIMPEXP_ADV
);
395 class WXDLLIMPEXP_ADV wxDataViewCtrl
: public wxDataViewCtrlBase
,
396 public wxScrollHelper
398 friend class wxDataViewMainWindow
;
399 friend class wxDataViewHeaderWindowBase
;
400 friend class wxDataViewHeaderWindow
;
401 friend class wxDataViewHeaderWindowMSW
;
402 friend class wxDataViewColumn
;
405 wxDataViewCtrl() : wxScrollHelper(this)
410 wxDataViewCtrl( wxWindow
*parent
, wxWindowID id
,
411 const wxPoint
& pos
= wxDefaultPosition
,
412 const wxSize
& size
= wxDefaultSize
, long style
= 0,
413 const wxValidator
& validator
= wxDefaultValidator
)
414 : wxScrollHelper(this)
416 Create(parent
, id
, pos
, size
, style
, validator
);
419 virtual ~wxDataViewCtrl();
423 bool Create(wxWindow
*parent
, wxWindowID id
,
424 const wxPoint
& pos
= wxDefaultPosition
,
425 const wxSize
& size
= wxDefaultSize
, long style
= 0,
426 const wxValidator
& validator
= wxDefaultValidator
);
428 virtual bool AssociateModel( wxDataViewModel
*model
);
430 virtual bool AppendColumn( wxDataViewColumn
*col
);
431 virtual bool PrependColumn( wxDataViewColumn
*col
);
432 virtual bool InsertColumn( unsigned int pos
, wxDataViewColumn
*col
);
434 virtual void DoSetExpanderColumn();
435 virtual void DoSetIndent();
437 virtual unsigned int GetColumnCount() const;
438 virtual wxDataViewColumn
* GetColumn( unsigned int pos
) const;
439 virtual bool DeleteColumn( wxDataViewColumn
*column
);
440 virtual bool ClearColumns();
441 virtual int GetColumnPosition( const wxDataViewColumn
*column
) const;
443 virtual wxDataViewColumn
*GetSortingColumn() const;
445 virtual wxDataViewItem
GetSelection() const;
446 virtual int GetSelections( wxDataViewItemArray
& sel
) const;
447 virtual void SetSelections( const wxDataViewItemArray
& sel
);
448 virtual void Select( const wxDataViewItem
& item
);
449 virtual void Unselect( const wxDataViewItem
& item
);
450 virtual bool IsSelected( const wxDataViewItem
& item
) const;
452 virtual void SelectAll();
453 virtual void UnselectAll();
455 virtual void EnsureVisible( const wxDataViewItem
& item
,
456 const wxDataViewColumn
*column
= NULL
);
457 virtual void HitTest( const wxPoint
& point
, wxDataViewItem
& item
,
458 wxDataViewColumn
* &column
) const;
459 virtual wxRect
GetItemRect( const wxDataViewItem
& item
,
460 const wxDataViewColumn
*column
= NULL
) const;
462 virtual void Expand( const wxDataViewItem
& item
);
463 virtual void Collapse( const wxDataViewItem
& item
);
464 virtual bool IsExpanded( const wxDataViewItem
& item
) const;
466 virtual void SetFocus();
468 #if wxUSE_DRAG_AND_DROP
469 virtual bool EnableDragSource( const wxDataFormat
&format
);
470 virtual bool EnableDropTarget( const wxDataFormat
&format
);
471 #endif // wxUSE_DRAG_AND_DROP
473 virtual wxBorder
GetDefaultBorder() const;
475 void StartEditor( const wxDataViewItem
& item
, unsigned int column
);
478 virtual int GetSelections( wxArrayInt
& sel
) const;
479 virtual void SetSelections( const wxArrayInt
& sel
);
480 virtual void Select( int row
);
481 virtual void Unselect( int row
);
482 virtual bool IsSelected( int row
) const;
483 virtual void SelectRange( int from
, int to
);
484 virtual void UnselectRange( int from
, int to
);
486 virtual void EnsureVisible( int row
, int column
);
488 virtual wxDataViewItem
GetItemByRow( unsigned int row
) const;
489 virtual int GetRowByItem( const wxDataViewItem
& item
) const;
491 int GetSortingColumnIndex() const { return m_sortingColumnIdx
; }
492 void SetSortingColumnIndex(int idx
) { m_sortingColumnIdx
= idx
; }
494 public: // utility functions not part of the API
496 // returns the "best" width for the idx-th column
497 unsigned int GetBestColumnWidth(int WXUNUSED(idx
)) const
499 return GetClientSize().GetWidth() / GetColumnCount();
502 // called by header window after reorder
503 void ColumnMoved( wxDataViewColumn
* col
, unsigned int new_pos
);
505 // update the display after a change to an individual column
506 void OnColumnChange(unsigned int idx
);
508 // update after a change to the number of columns
509 void OnColumnsCountChanged();
511 wxWindow
*GetMainWindow() { return (wxWindow
*) m_clientArea
; }
513 // return the index of the given column in m_cols
514 int GetColumnIndex(const wxDataViewColumn
*column
) const;
516 // return the column displayed at the given position in the control
517 wxDataViewColumn
*GetColumnAt(unsigned int pos
) const;
520 wxDataViewColumnList m_cols
;
521 wxDataViewModelNotifier
*m_notifier
;
522 wxDataViewMainWindow
*m_clientArea
;
523 wxDataViewHeaderWindow
*m_headerArea
;
525 // the index of the column currently used for sorting or -1
526 int m_sortingColumnIdx
;
529 void OnSize( wxSizeEvent
&event
);
530 virtual wxSize
GetSizeAvailableForScrollTarget(const wxSize
& size
);
532 // we need to return a special WM_GETDLGCODE value to process just the
533 // arrows but let the other navigation characters through
535 virtual WXLRESULT
MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
);
538 WX_FORWARD_TO_SCROLL_HELPER()
541 DECLARE_DYNAMIC_CLASS(wxDataViewCtrl
)
542 wxDECLARE_NO_COPY_CLASS(wxDataViewCtrl
);
543 DECLARE_EVENT_TABLE()
547 #endif // __GENERICDATAVIEWCTRLH__