1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDataViewCtrl base classes
4 // Author: Robert Roebling
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_DATAVIEW_H_BASE_
13 #define _WX_DATAVIEW_H_BASE_
17 #if wxUSE_DATAVIEWCTRL
19 #include "wx/control.h"
20 #include "wx/textctrl.h"
21 #include "wx/bitmap.h"
22 #include "wx/variant.h"
25 #if defined(__WXGTK20__)
27 // #define wxUSE_GENERICDATAVIEWCTRL 1
28 #elif defined(__WXMAC__)
29 #define wxUSE_GENERICDATAVIEWCTRL 1
31 #define wxUSE_GENERICDATAVIEWCTRL 1
34 // ----------------------------------------------------------------------------
35 // wxDataViewCtrl flags
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
39 // wxDataViewCtrl globals
40 // ----------------------------------------------------------------------------
42 class WXDLLIMPEXP_ADV wxDataViewModel
;
43 class WXDLLIMPEXP_ADV wxDataViewListModel
;
44 class WXDLLIMPEXP_ADV wxDataViewCtrl
;
45 class WXDLLIMPEXP_ADV wxDataViewColumn
;
46 class WXDLLIMPEXP_ADV wxDataViewRenderer
;
48 extern WXDLLIMPEXP_DATA_ADV(const wxChar
) wxDataViewCtrlNameStr
[];
50 // the default width of new (text) columns:
51 #define wxDVC_DEFAULT_WIDTH 80
53 // the default width of new toggle columns:
54 #define wxDVC_TOGGLE_DEFAULT_WIDTH 30
56 // the default minimal width of the columns:
57 #define wxDVC_DEFAULT_MINWIDTH 30
59 // the default alignment of wxDataViewRenderers:
60 #define wxDVR_DEFAULT_ALIGNMENT (wxALIGN_LEFT|wxALIGN_TOP)
63 // ---------------------------------------------------------
65 // ---------------------------------------------------------
67 class WXDLLIMPEXP_ADV wxDataViewModel
: public wxObjectRefData
73 // the user should not delete this class directly: he should use DecRef() instead!
74 virtual ~wxDataViewModel() { }
77 // ---------------------------------------------------------
78 // wxDataViewListModelNotifier
79 // ---------------------------------------------------------
81 class WXDLLIMPEXP_ADV wxDataViewListModelNotifier
: public wxObject
84 wxDataViewListModelNotifier() { }
85 virtual ~wxDataViewListModelNotifier() { m_owner
= NULL
; }
87 virtual bool RowAppended() = 0;
88 virtual bool RowPrepended() = 0;
89 virtual bool RowInserted( unsigned int before
) = 0;
90 virtual bool RowDeleted( unsigned int row
) = 0;
91 virtual bool RowChanged( unsigned int row
) = 0;
92 virtual bool ValueChanged( unsigned int col
, unsigned int row
) = 0;
93 virtual bool RowsReordered( unsigned int *new_order
) = 0;
94 virtual bool Cleared() = 0;
96 void SetOwner( wxDataViewListModel
*owner
) { m_owner
= owner
; }
97 wxDataViewListModel
*GetOwner() { return m_owner
; }
100 wxDataViewListModel
*m_owner
;
103 // ---------------------------------------------------------
104 // wxDataViewListModel
105 // ---------------------------------------------------------
107 class WXDLLIMPEXP_ADV wxDataViewViewingColumn
: public wxObject
110 wxDataViewViewingColumn( wxDataViewColumn
*view_column
, unsigned int model_column
)
112 m_viewColumn
= view_column
;
113 m_modelColumn
= model_column
;
116 wxDataViewColumn
*m_viewColumn
;
117 unsigned int m_modelColumn
;
120 class WXDLLIMPEXP_ADV wxDataViewListModel
: public wxDataViewModel
122 friend class WXDLLIMPEXP_ADV wxDataViewCtrl
;
123 friend class WXDLLIMPEXP_ADV wxDataViewCtrlBase
;
124 friend class WXDLLIMPEXP_ADV wxDataViewSortedListModel
;
125 friend class WXDLLIMPEXP_ADV wxDataViewColumnBase
;
126 friend class WXDLLIMPEXP_ADV wxGtkDataViewListModelNotifier
;
129 wxDataViewListModel();
131 virtual unsigned int GetRowCount() const = 0;
132 virtual unsigned int GetColumnCount() const = 0;
134 // return type as reported by wxVariant
135 virtual wxString
GetColumnType( unsigned int col
) const = 0;
137 // get value into a wxVariant
138 virtual void GetValue( wxVariant
&variant
, unsigned int col
, unsigned int row
) const = 0;
140 // set value, call ValueChanged() afterwards!
141 virtual bool SetValue( const wxVariant
&variant
, unsigned int col
, unsigned int row
) = 0;
143 // delegated notifiers
144 virtual bool RowAppended();
145 virtual bool RowPrepended();
146 virtual bool RowInserted( unsigned int before
);
147 virtual bool RowDeleted( unsigned int row
);
148 virtual bool RowChanged( unsigned int row
);
149 virtual bool ValueChanged( unsigned int col
, unsigned int row
);
150 virtual bool RowsReordered( unsigned int *new_order
);
151 virtual bool Cleared();
154 // the user should not delete this class directly: he should use DecRef() instead!
155 virtual ~wxDataViewListModel();
158 void AddViewingColumn( wxDataViewColumn
*view_column
, unsigned int model_column
);
159 void RemoveViewingColumn( wxDataViewColumn
*column
);
161 void AddNotifier( wxDataViewListModelNotifier
*notifier
);
162 void RemoveNotifier( wxDataViewListModelNotifier
*notifier
);
165 wxList m_viewingColumns
;
170 // ---------------------------------------------------------
171 // wxDataViewSortedListModel
172 // ---------------------------------------------------------
174 typedef int (wxCALLBACK
*wxDataViewListModelCompare
)
175 (unsigned int row1
, unsigned int row2
, unsigned int col
, wxDataViewListModel
* model
);
177 WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_SIZE_T(unsigned int,
178 wxDataViewSortedIndexArray
, WXDLLIMPEXP_ADV
);
180 class WXDLLIMPEXP_ADV wxDataViewSortedListModel
: public wxDataViewListModel
182 friend class wxDataViewSortedListModelNotifier
;
185 wxDataViewSortedListModel( wxDataViewListModel
*child
);
186 virtual ~wxDataViewSortedListModel();
188 void SetAscending( bool ascending
) { m_ascending
= ascending
; }
189 bool IsAscending() const { return m_ascending
; }
191 virtual unsigned int GetRowCount() const;
192 virtual unsigned int GetColumnCount() const;
194 // return type as reported by wxVariant
195 virtual wxString
GetColumnType( unsigned int col
) const;
197 // get value into a wxVariant
198 virtual void GetValue( wxVariant
&variant
, unsigned int col
, unsigned int row
) const;
200 // set value, call ValueChanged() afterwards!
201 virtual bool SetValue( const wxVariant
&variant
, unsigned int col
, unsigned int row
);
204 virtual bool RowAppended();
205 virtual bool RowPrepended();
206 virtual bool RowInserted( unsigned int before
);
207 virtual bool RowDeleted( unsigned int row
);
208 virtual bool RowChanged( unsigned int row
);
209 virtual bool ValueChanged( unsigned int col
, unsigned int row
);
210 virtual bool RowsReordered( unsigned int *new_order
);
211 virtual bool Cleared();
213 // called if child's notifiers are called
214 bool ChildRowAppended();
215 bool ChildRowPrepended();
216 bool ChildRowInserted( unsigned int before
);
217 bool ChildRowDeleted( unsigned int row
);
218 bool ChildRowChanged( unsigned int row
);
219 bool ChildValueChanged( unsigned int col
, unsigned int row
);
220 bool ChildRowsReordered( unsigned int *new_order
);
223 virtual void Resort();
229 wxDataViewListModel
*m_child
;
230 wxDataViewSortedIndexArray m_array
;
231 wxDataViewListModelNotifier
*m_notifierOnChild
;
233 void InitStatics(); // BAD
236 //-----------------------------------------------------------------------------
237 // wxDataViewEditorCtrlEvtHandler
238 //-----------------------------------------------------------------------------
240 class wxDataViewEditorCtrlEvtHandler
: public wxEvtHandler
243 wxDataViewEditorCtrlEvtHandler( wxControl
*editor
, wxDataViewRenderer
*owner
);
245 void AcceptChangesAndFinish();
246 void SetFocusOnIdle( bool focus
= true ) { m_focusOnIdle
= focus
; }
249 void OnChar( wxKeyEvent
&event
);
250 void OnKillFocus( wxFocusEvent
&event
);
251 void OnIdle( wxIdleEvent
&event
);
254 wxDataViewRenderer
*m_owner
;
255 wxControl
*m_editorCtrl
;
260 DECLARE_EVENT_TABLE()
263 // ---------------------------------------------------------
264 // wxDataViewRendererBase
265 // ---------------------------------------------------------
267 enum wxDataViewCellMode
269 wxDATAVIEW_CELL_INERT
,
270 wxDATAVIEW_CELL_ACTIVATABLE
,
271 wxDATAVIEW_CELL_EDITABLE
274 enum wxDataViewCellRenderState
276 wxDATAVIEW_CELL_SELECTED
= 1,
277 wxDATAVIEW_CELL_PRELIT
= 2,
278 wxDATAVIEW_CELL_INSENSITIVE
= 4,
279 wxDATAVIEW_CELL_FOCUSED
= 8
282 class WXDLLIMPEXP_ADV wxDataViewRendererBase
: public wxObject
285 wxDataViewRendererBase( const wxString
&varianttype
,
286 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
,
287 int alignment
= wxDVR_DEFAULT_ALIGNMENT
);
289 virtual bool Validate( wxVariant
& WXUNUSED(value
) )
292 void SetOwner( wxDataViewColumn
*owner
) { m_owner
= owner
; }
293 wxDataViewColumn
* GetOwner() { return m_owner
; }
295 // renderer properties:
297 virtual bool SetValue( const wxVariant
& WXUNUSED(value
) ) = 0;
298 virtual bool GetValue( wxVariant
& WXUNUSED(value
) ) const = 0;
300 wxString
GetVariantType() const { return m_variantType
; }
302 virtual void SetMode( wxDataViewCellMode mode
) = 0;
303 virtual wxDataViewCellMode
GetMode() const = 0;
305 // NOTE: Set/GetAlignment do not take/return a wxAlignment enum but
306 // rather an "int"; that's because for rendering cells it's allowed
307 // to combine alignment flags (e.g. wxALIGN_LEFT|wxALIGN_BOTTOM)
308 virtual void SetAlignment( int align
) = 0;
309 virtual int GetAlignment() const = 0;
312 virtual bool HasEditorCtrl()
314 virtual wxControl
* CreateEditorCtrl(wxWindow
* WXUNUSED(parent
),
315 wxRect
WXUNUSED(labelRect
),
316 const wxVariant
& WXUNUSED(value
))
318 virtual bool GetValueFromEditorCtrl(wxControl
* WXUNUSED(editor
),
319 wxVariant
& WXUNUSED(value
))
322 virtual bool StartEditing( unsigned int row
, wxRect labelRect
);
323 virtual void CancelEditing();
324 virtual bool FinishEditing();
326 wxControl
*GetEditorCtrl() { return m_editorCtrl
; }
329 wxString m_variantType
;
330 wxDataViewColumn
*m_owner
;
331 wxControl
*m_editorCtrl
;
332 unsigned int m_row
; // for m_editorCtrl
335 const wxDataViewCtrl
* GetView() const;
338 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewRendererBase
)
341 // ---------------------------------------------------------
342 // wxDataViewColumnBase
343 // ---------------------------------------------------------
345 enum wxDataViewColumnFlags
347 wxDATAVIEW_COL_RESIZABLE
= 1,
348 wxDATAVIEW_COL_SORTABLE
= 2,
349 wxDATAVIEW_COL_HIDDEN
= 4
352 class WXDLLIMPEXP_ADV wxDataViewColumnBase
: public wxObject
355 wxDataViewColumnBase( const wxString
&title
, wxDataViewRenderer
*renderer
,
356 unsigned int model_column
, int width
= wxDVC_DEFAULT_WIDTH
,
357 wxAlignment align
= wxALIGN_CENTER
,
358 int flags
= wxDATAVIEW_COL_RESIZABLE
);
359 wxDataViewColumnBase( const wxBitmap
&bitmap
, wxDataViewRenderer
*renderer
,
360 unsigned int model_column
, int width
= wxDVC_DEFAULT_WIDTH
,
361 wxAlignment align
= wxALIGN_CENTER
,
362 int flags
= wxDATAVIEW_COL_RESIZABLE
);
363 virtual ~wxDataViewColumnBase();
367 virtual void SetTitle( const wxString
&title
) = 0;
368 virtual void SetAlignment( wxAlignment align
) = 0;
369 virtual void SetSortable( bool sortable
) = 0;
370 virtual void SetResizeable( bool resizeable
) = 0;
371 virtual void SetHidden( bool hidden
) = 0;
372 virtual void SetSortOrder( bool ascending
) = 0;
373 virtual void SetFlags( int flags
);
374 virtual void SetOwner( wxDataViewCtrl
*owner
)
376 virtual void SetBitmap( const wxBitmap
&bitmap
)
379 virtual void SetMinWidth( int minWidth
) = 0;
380 virtual void SetWidth( int width
) = 0;
385 virtual wxString
GetTitle() const = 0;
386 virtual wxAlignment
GetAlignment() const = 0;
387 virtual int GetWidth() const = 0;
388 virtual int GetMinWidth() const = 0;
390 virtual int GetFlags() const;
392 virtual bool IsSortable() const = 0;
393 virtual bool IsResizeable() const = 0;
394 virtual bool IsHidden() const = 0;
395 virtual bool IsSortOrderAscending() const = 0;
397 const wxBitmap
&GetBitmap() const { return m_bitmap
; }
398 unsigned int GetModelColumn() const { return m_model_column
; }
400 wxDataViewCtrl
*GetOwner() { return m_owner
; }
401 wxDataViewRenderer
* GetRenderer() { return m_renderer
; }
404 wxDataViewRenderer
*m_renderer
;
407 wxDataViewCtrl
*m_owner
;
410 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewColumnBase
)
413 // ---------------------------------------------------------
414 // wxDataViewCtrlBase
415 // ---------------------------------------------------------
417 #define wxDV_SINGLE 0x0000 // for convenience
418 #define wxDV_MULTIPLE 0x0001 // can select multiple items
420 #define wxDV_NO_HEADER 0x0002 // column titles not visible
421 #define wxDV_HORIZ_RULES 0x0004 // light horizontal rules between rows
422 #define wxDV_VERT_RULES 0x0008 // light vertical rules between columns
424 class WXDLLIMPEXP_ADV wxDataViewCtrlBase
: public wxControl
427 wxDataViewCtrlBase();
428 virtual ~wxDataViewCtrlBase();
430 virtual bool AssociateModel( wxDataViewListModel
*model
);
431 wxDataViewListModel
* GetModel();
434 bool AppendTextColumn( const wxString
&label
, unsigned int model_column
,
435 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
, int width
= -1,
436 wxAlignment align
= wxALIGN_CENTER
,
437 int flags
= wxDATAVIEW_COL_RESIZABLE
);
438 bool AppendToggleColumn( const wxString
&label
, unsigned int model_column
,
439 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
, int width
= wxDVC_TOGGLE_DEFAULT_WIDTH
,
440 wxAlignment align
= wxALIGN_CENTER
,
441 int flags
= wxDATAVIEW_COL_RESIZABLE
);
442 bool AppendProgressColumn( const wxString
&label
, unsigned int model_column
,
443 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
, int width
= wxDVC_DEFAULT_WIDTH
,
444 wxAlignment align
= wxALIGN_CENTER
,
445 int flags
= wxDATAVIEW_COL_RESIZABLE
);
446 bool AppendDateColumn( const wxString
&label
, unsigned int model_column
,
447 wxDataViewCellMode mode
= wxDATAVIEW_CELL_ACTIVATABLE
, int width
= -1,
448 wxAlignment align
= wxALIGN_CENTER
,
449 int flags
= wxDATAVIEW_COL_RESIZABLE
);
450 bool AppendBitmapColumn( const wxString
&label
, unsigned int model_column
,
451 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
, int width
= -1,
452 wxAlignment align
= wxALIGN_CENTER
,
453 int flags
= wxDATAVIEW_COL_RESIZABLE
);
454 bool AppendTextColumn( const wxBitmap
&label
, unsigned int model_column
,
455 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
, int width
= -1,
456 wxAlignment align
= wxALIGN_CENTER
,
457 int flags
= wxDATAVIEW_COL_RESIZABLE
);
458 bool AppendToggleColumn( const wxBitmap
&label
, unsigned int model_column
,
459 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
, int width
= wxDVC_TOGGLE_DEFAULT_WIDTH
,
460 wxAlignment align
= wxALIGN_CENTER
,
461 int flags
= wxDATAVIEW_COL_RESIZABLE
);
462 bool AppendProgressColumn( const wxBitmap
&label
, unsigned int model_column
,
463 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
, int width
= wxDVC_DEFAULT_WIDTH
,
464 wxAlignment align
= wxALIGN_CENTER
,
465 int flags
= wxDATAVIEW_COL_RESIZABLE
);
466 bool AppendDateColumn( const wxBitmap
&label
, unsigned int model_column
,
467 wxDataViewCellMode mode
= wxDATAVIEW_CELL_ACTIVATABLE
, int width
= -1,
468 wxAlignment align
= wxALIGN_CENTER
,
469 int flags
= wxDATAVIEW_COL_RESIZABLE
);
470 bool AppendBitmapColumn( const wxBitmap
&label
, unsigned int model_column
,
471 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
, int width
= -1,
472 wxAlignment align
= wxALIGN_CENTER
,
473 int flags
= wxDATAVIEW_COL_RESIZABLE
);
475 virtual bool AppendColumn( wxDataViewColumn
*col
);
477 virtual unsigned int GetColumnCount() const;
479 virtual bool DeleteColumn( unsigned int pos
);
480 virtual bool ClearColumns();
481 virtual wxDataViewColumn
* GetColumn( unsigned int pos
);
483 virtual void SetSelection( int row
) = 0; // -1 for unselect
484 inline void ClearSelection() { SetSelection( -1 ); }
485 virtual void Unselect( unsigned int row
) = 0;
486 virtual void SetSelectionRange( unsigned int from
, unsigned int to
) = 0;
487 virtual void SetSelections( const wxArrayInt
& aSelections
) = 0;
489 virtual bool IsSelected( unsigned int row
) const = 0;
490 virtual int GetSelection() const = 0;
491 virtual int GetSelections(wxArrayInt
& aSelections
) const = 0;
494 wxDataViewListModel
*m_model
;
498 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewCtrlBase
)
502 // ----------------------------------------------------------------------------
503 // wxDataViewEvent - the event class for the wxDataViewCtrl notifications
504 // ----------------------------------------------------------------------------
506 class WXDLLIMPEXP_ADV wxDataViewEvent
: public wxNotifyEvent
509 wxDataViewEvent(wxEventType commandType
= wxEVT_NULL
, int winid
= 0)
510 : wxNotifyEvent(commandType
, winid
),
514 m_value(wxNullVariant
),
515 m_editCancelled(false),
519 wxDataViewEvent(const wxDataViewEvent
& event
)
520 : wxNotifyEvent(event
),
523 m_model(event
.m_model
),
524 m_value(event
.m_value
),
525 m_editCancelled(event
.m_editCancelled
),
526 m_column(event
.m_column
)
529 int GetColumn() const { return m_col
; }
530 void SetColumn( int col
) { m_col
= col
; }
532 int GetRow() const { return m_row
; }
533 void SetRow( int row
) { m_row
= row
; }
535 wxDataViewModel
* GetModel() const { return m_model
; }
536 void SetModel( wxDataViewModel
*model
) { m_model
= model
; }
538 const wxVariant
&GetValue() const { return m_value
; }
539 void SetValue( const wxVariant
&value
) { m_value
= value
; }
541 // for wxEVT_DATAVIEW_COLUMN_HEADER_CLICKED only
542 void SetDataViewColumn( wxDataViewColumn
*col
) { m_column
= col
; }
543 wxDataViewColumn
*GetDataViewColumn() const { return m_column
; }
545 // was label editing canceled? (for wxEVT_COMMAND_DATVIEW_END_LABEL_EDIT only)
546 bool IsEditCancelled() const { return m_editCancelled
; }
547 void SetEditCanceled(bool editCancelled
) { m_editCancelled
= editCancelled
; }
549 virtual wxEvent
*Clone() const { return new wxDataViewEvent(*this); }
554 wxDataViewModel
*m_model
;
556 bool m_editCancelled
;
557 wxDataViewColumn
*m_column
;
560 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxDataViewEvent
)
563 BEGIN_DECLARE_EVENT_TYPES()
564 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_ADV
, wxEVT_COMMAND_DATAVIEW_ROW_SELECTED
, -1)
565 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_ADV
, wxEVT_COMMAND_DATAVIEW_ROW_ACTIVATED
, -1)
566 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_ADV
, wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
, -1)
567 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_ADV
, wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
, -1)
568 END_DECLARE_EVENT_TYPES()
570 typedef void (wxEvtHandler::*wxDataViewEventFunction
)(wxDataViewEvent
&);
572 #define wxDataViewEventHandler(func) \
573 (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxDataViewEventFunction, &func)
575 #define wx__DECLARE_DATAVIEWEVT(evt, id, fn) \
576 wx__DECLARE_EVT1(wxEVT_COMMAND_DATAVIEW_ ## evt, id, wxDataViewEventHandler(fn))
578 #define EVT_DATAVIEW_ROW_SELECTED(id, fn) wx__DECLARE_DATAVIEWEVT(ROW_SELECTED, id, fn)
579 #define EVT_DATAVIEW_ROW_ACTIVATED(id, fn) wx__DECLARE_DATAVIEWEVT(ROW_ACTIVATED, id, fn)
580 #define EVT_DATAVIEW_COLUMN_HEADER_CLICK(id, fn) wx__DECLARE_DATAVIEWEVT(COLUMN_HEADER_CLICK, id, fn)
581 #define EVT_DATAVIEW_COLUMN_HEADER_RIGHT_CLICKED(id, fn) wx__DECLARE_DATAVIEWEVT(COLUMN_HEADER_RIGHT_CLICK, id, fn)
584 #if defined(wxUSE_GENERICDATAVIEWCTRL)
585 #include "wx/generic/dataview.h"
586 #elif defined(__WXGTK20__)
587 #include "wx/gtk/dataview.h"
588 #elif defined(__WXMAC__)
590 // #include "wx/mac/dataview.h"
592 #include "wx/generic/dataview.h"
595 #endif // wxUSE_DATAVIEWCTRL
598 // _WX_DATAVIEW_H_BASE_