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 const wxDataViewItemAttr
* WXUNUSED(attr
), // NULL if none
54 return Render(rect
, &dc
, state
);
57 virtual wxSize
GetSize() const = 0;
59 virtual void SetAlignment( int align
);
60 virtual int GetAlignment() const;
62 virtual void SetMode( wxDataViewCellMode mode
)
64 virtual wxDataViewCellMode
GetMode() const
67 virtual bool Activate( wxRect
WXUNUSED(cell
),
68 wxDataViewModel
*WXUNUSED(model
),
69 const wxDataViewItem
& WXUNUSED(item
),
70 unsigned int WXUNUSED(col
) )
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
) )
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
) )
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
) )
92 // Create DC on request
93 virtual wxDC
*GetDC();
96 int CalculateAlignment() const;
101 wxDataViewCellMode m_mode
;
104 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewRenderer
)
107 // ---------------------------------------------------------
108 // wxDataViewCustomRenderer
109 // ---------------------------------------------------------
111 class WXDLLIMPEXP_ADV wxDataViewCustomRenderer
: public wxDataViewRenderer
114 wxDataViewCustomRenderer( const wxString
&varianttype
= wxT("string"),
115 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
,
116 int align
= wxDVR_DEFAULT_ALIGNMENT
);
118 // Draw the text using the provided attributes
119 void RenderText(wxDC
& dc
,
121 const wxString
& text
,
122 const wxDataViewItemAttr
*attr
, // may be NULL if none
126 // Overload using standard attributes
127 void RenderText( const wxString
&text
, int xoffset
, wxRect cell
, wxDC
*dc
, int state
);
130 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewCustomRenderer
)
134 // ---------------------------------------------------------
135 // wxDataViewTextRenderer
136 // ---------------------------------------------------------
138 class WXDLLIMPEXP_ADV wxDataViewTextRenderer
: public wxDataViewCustomRenderer
141 wxDataViewTextRenderer( const wxString
&varianttype
= wxT("string"),
142 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
,
143 int align
= wxDVR_DEFAULT_ALIGNMENT
);
145 bool SetValue( const wxVariant
&value
);
146 bool GetValue( wxVariant
&value
) const;
148 virtual bool RenderWithAttr(wxDC
& dc
,
150 const wxDataViewItemAttr
*attr
,
152 virtual bool Render( wxRect cell
, wxDC
*dc
, int state
)
154 return RenderWithAttr(*dc
, cell
, NULL
, state
);
157 wxSize
GetSize() const;
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
);
169 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewTextRenderer
)
172 // ---------------------------------------------------------
173 // wxDataViewBitmapRenderer
174 // ---------------------------------------------------------
176 class WXDLLIMPEXP_ADV wxDataViewBitmapRenderer
: public wxDataViewCustomRenderer
179 wxDataViewBitmapRenderer( const wxString
&varianttype
= wxT("wxBitmap"),
180 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
,
181 int align
= wxDVR_DEFAULT_ALIGNMENT
);
183 bool SetValue( const wxVariant
&value
);
184 bool GetValue( wxVariant
&value
) const;
186 bool Render( wxRect cell
, wxDC
*dc
, int state
);
187 wxSize
GetSize() const;
194 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewBitmapRenderer
)
197 // ---------------------------------------------------------
198 // wxDataViewToggleRenderer
199 // ---------------------------------------------------------
201 class WXDLLIMPEXP_ADV wxDataViewToggleRenderer
: public wxDataViewCustomRenderer
204 wxDataViewToggleRenderer( const wxString
&varianttype
= wxT("bool"),
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 bool Activate( wxRect cell
, wxDataViewModel
*model
, const wxDataViewItem
& item
,
214 wxSize
GetSize() const;
220 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewToggleRenderer
)
223 // ---------------------------------------------------------
224 // wxDataViewProgressRenderer
225 // ---------------------------------------------------------
227 class WXDLLIMPEXP_ADV wxDataViewProgressRenderer
: public wxDataViewCustomRenderer
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();
236 bool SetValue( const wxVariant
&value
);
237 bool GetValue( wxVariant
& value
) const;
239 virtual bool Render( wxRect cell
, wxDC
*dc
, int state
);
240 virtual wxSize
GetSize() const;
247 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewProgressRenderer
)
250 // ---------------------------------------------------------
251 // wxDataViewIconTextRenderer
252 // ---------------------------------------------------------
254 class WXDLLIMPEXP_ADV wxDataViewIconTextRenderer
: public wxDataViewCustomRenderer
257 wxDataViewIconTextRenderer( const wxString
&varianttype
= wxT("wxDataViewIconText"),
258 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
,
259 int align
= wxDVR_DEFAULT_ALIGNMENT
);
260 virtual ~wxDataViewIconTextRenderer();
262 bool SetValue( const wxVariant
&value
);
263 bool GetValue( wxVariant
&value
) const;
265 virtual bool Render( wxRect cell
, wxDC
*dc
, int state
);
266 virtual wxSize
GetSize() const;
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
);
274 wxDataViewIconText m_value
;
277 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewIconTextRenderer
)
280 // ---------------------------------------------------------
281 // wxDataViewDateRenderer
282 // ---------------------------------------------------------
284 class WXDLLIMPEXP_ADV wxDataViewDateRenderer
: public wxDataViewCustomRenderer
287 wxDataViewDateRenderer( const wxString
&varianttype
= wxT("datetime"),
288 wxDataViewCellMode mode
= wxDATAVIEW_CELL_ACTIVATABLE
,
289 int align
= wxDVR_DEFAULT_ALIGNMENT
);
291 bool SetValue( const wxVariant
&value
);
292 bool GetValue( wxVariant
& value
) const;
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
,
305 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewDateRenderer
)
308 // ---------------------------------------------------------
310 // ---------------------------------------------------------
312 class WXDLLIMPEXP_ADV wxDataViewColumn
: public wxDataViewColumnBase
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
),
324 Init(width
, align
, flags
);
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
)
335 Init(width
, align
, flags
);
338 // implement wxHeaderColumnBase methods
339 virtual void SetTitle(const wxString
& title
) { m_title
= title
; }
340 virtual wxString
GetTitle() const { return m_title
; }
342 virtual void SetWidth(int width
) { m_width
= width
; }
343 virtual int GetWidth() const { return m_width
; }
345 virtual void SetMinWidth(int minWidth
) { m_minWidth
= minWidth
; }
346 virtual int GetMinWidth() const { return m_minWidth
; }
348 virtual void SetAlignment(wxAlignment align
) { m_align
= align
; }
349 virtual wxAlignment
GetAlignment() const { return m_align
; }
351 virtual void SetFlags(int flags
) { m_flags
= flags
; }
352 virtual int GetFlags() const { return m_flags
; }
354 virtual void SetAsSortKey(bool sort
= true) { m_sort
= sort
; }
355 virtual bool IsSortKey() const { return m_sort
; }
357 virtual void SetSortOrder(bool ascending
) { m_sortAscending
= ascending
; }
358 virtual bool IsSortOrderAscending() const { return m_sortAscending
; }
361 // common part of all ctors
362 void Init(int width
, wxAlignment align
, int flags
)
364 m_width
= width
== wxCOL_WIDTH_DEFAULT
? wxDVC_DEFAULT_WIDTH
: width
;
369 m_sortAscending
= true;
380 friend class wxDataViewHeaderWindowBase
;
381 friend class wxDataViewHeaderWindow
;
382 friend class wxDataViewHeaderWindowMSW
;
385 // ---------------------------------------------------------
387 // ---------------------------------------------------------
389 WX_DECLARE_LIST_WITH_DECL(wxDataViewColumn
, wxDataViewColumnList
,
390 class WXDLLIMPEXP_ADV
);
392 class WXDLLIMPEXP_ADV wxDataViewCtrl
: public wxDataViewCtrlBase
,
393 public wxScrollHelper
395 friend class wxDataViewMainWindow
;
396 friend class wxDataViewHeaderWindowBase
;
397 friend class wxDataViewHeaderWindow
;
398 friend class wxDataViewHeaderWindowMSW
;
399 friend class wxDataViewColumn
;
402 wxDataViewCtrl() : wxScrollHelper(this)
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)
413 Create(parent
, id
, pos
, size
, style
, validator
);
416 virtual ~wxDataViewCtrl();
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
);
425 virtual bool AssociateModel( wxDataViewModel
*model
);
427 virtual bool AppendColumn( wxDataViewColumn
*col
);
428 virtual bool PrependColumn( wxDataViewColumn
*col
);
429 virtual bool InsertColumn( unsigned int pos
, wxDataViewColumn
*col
);
431 virtual void DoSetExpanderColumn();
432 virtual void DoSetIndent();
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;
440 virtual wxDataViewColumn
*GetSortingColumn() const;
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;
449 virtual void SelectAll();
450 virtual void UnselectAll();
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;
459 virtual void Expand( const wxDataViewItem
& item
);
460 virtual void Collapse( const wxDataViewItem
& item
);
461 virtual bool IsExpanded( const wxDataViewItem
& item
) const;
463 virtual void SetFocus();
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
470 virtual wxBorder
GetDefaultBorder() const;
472 void StartEditor( const wxDataViewItem
& item
, unsigned int column
);
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
);
483 virtual void EnsureVisible( int row
, int column
);
485 virtual wxDataViewItem
GetItemByRow( unsigned int row
) const;
486 virtual int GetRowByItem( const wxDataViewItem
& item
) const;
488 int GetSortingColumnIndex() const { return m_sortingColumnIdx
; }
489 void SetSortingColumnIndex(int idx
) { m_sortingColumnIdx
= idx
; }
491 public: // utility functions not part of the API
493 // returns the "best" width for the idx-th column
494 unsigned int GetBestColumnWidth(int WXUNUSED(idx
)) const
496 return GetClientSize().GetWidth() / GetColumnCount();
499 // called by header window after reorder
500 void ColumnMoved( wxDataViewColumn
* col
, unsigned int new_pos
);
502 // update the display after a change to an individual column
503 void OnColumnChange(unsigned int idx
);
505 // update after a change to the number of columns
506 void OnColumnsCountChanged();
508 wxWindow
*GetMainWindow() { return (wxWindow
*) m_clientArea
; }
510 // return the index of the given column in m_cols
511 int GetColumnIndex(const wxDataViewColumn
*column
) const;
513 // return the column displayed at the given position in the control
514 wxDataViewColumn
*GetColumnAt(unsigned int pos
) const;
517 wxDataViewColumnList m_cols
;
518 wxDataViewModelNotifier
*m_notifier
;
519 wxDataViewMainWindow
*m_clientArea
;
520 wxDataViewHeaderWindow
*m_headerArea
;
522 // the index of the column currently used for sorting or -1
523 int m_sortingColumnIdx
;
526 void OnSize( wxSizeEvent
&event
);
527 virtual wxSize
GetSizeAvailableForScrollTarget(const wxSize
& size
);
529 // we need to return a special WM_GETDLGCODE value to process just the
530 // arrows but let the other navigation characters through
532 virtual WXLRESULT
MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
);
535 WX_FORWARD_TO_SCROLL_HELPER()
538 DECLARE_DYNAMIC_CLASS(wxDataViewCtrl
)
539 wxDECLARE_NO_COPY_CLASS(wxDataViewCtrl
);
540 DECLARE_EVENT_TABLE()
544 #endif // __GENERICDATAVIEWCTRLH__