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"
23 #include "wx/listctrl.h"
25 #if defined(__WXGTK20__)
27 // #define wxUSE_GENERICDATAVIEWCTRL 1
28 #elif defined(__WXMAC__)
30 #define wxUSE_GENERICDATAVIEWCTRL 1
33 // ----------------------------------------------------------------------------
34 // wxDataViewCtrl flags
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
38 // wxDataViewCtrl globals
39 // ----------------------------------------------------------------------------
41 class WXDLLIMPEXP_FWD_ADV wxDataViewItem
;
42 class WXDLLIMPEXP_FWD_ADV wxDataViewModel
;
43 class WXDLLIMPEXP_FWD_ADV wxDataViewCtrl
;
44 class WXDLLIMPEXP_FWD_ADV wxDataViewColumn
;
45 class WXDLLIMPEXP_FWD_ADV wxDataViewRenderer
;
46 class WXDLLIMPEXP_FWD_ADV wxDataViewModelNotifier
;
47 class wxDataViewEventModelNotifier
;
49 extern WXDLLIMPEXP_DATA_ADV(const wxChar
) wxDataViewCtrlNameStr
[];
51 // the default width of new (text) columns:
52 #define wxDVC_DEFAULT_WIDTH 80
54 // the default width of new toggle columns:
55 #define wxDVC_TOGGLE_DEFAULT_WIDTH 30
57 // the default minimal width of the columns:
58 #define wxDVC_DEFAULT_MINWIDTH 30
60 // the default alignment of wxDataViewRenderers:
61 #define wxDVR_DEFAULT_ALIGNMENT (wxALIGN_LEFT|wxALIGN_TOP)
64 // ---------------------------------------------------------
66 // ---------------------------------------------------------
68 class WXDLLIMPEXP_ADV wxDataViewItem
71 wxDataViewItem( wxUint32 id
= 0 )
72 { m_id
= id
; m_reserved1
= 0; m_reserved2
= NULL
; }
73 wxDataViewItem( const wxDataViewItem
&item
)
74 { m_id
= item
.m_id
; m_reserved1
= item
.m_reserved1
; m_reserved2
= item
.m_reserved2
; }
75 bool IsOk() const { return m_id
!= 0; }
76 wxUint32
GetID() const { return m_id
; }
86 bool operator == ( const wxDataViewItem
& left
, const wxDataViewItem
& right
);
88 // ---------------------------------------------------------
90 // ---------------------------------------------------------
92 typedef int (wxCALLBACK
*wxDataViewModelCompare
)
93 (const wxDataViewItem
& item1
, const wxDataViewItem
& item2
, unsigned int col
, unsigned int option
);
95 class WXDLLIMPEXP_ADV wxDataViewModel
: public wxObjectRefData
100 virtual unsigned int GetColumnCount() const = 0;
102 // return type as reported by wxVariant
103 virtual wxString
GetColumnType( unsigned int col
) const = 0;
105 // get value into a wxVariant
106 virtual void GetValue( wxVariant
&variant
,
107 const wxDataViewItem
&item
, unsigned int col
) const = 0;
109 // set value, call ValueChanged() afterwards!
110 virtual bool SetValue( const wxVariant
&variant
,
111 const wxDataViewItem
&item
, unsigned int col
) = 0;
114 virtual bool HasChildren( const wxDataViewItem
&item
) const = 0;
115 virtual int GetChildCount( const wxDataViewItem
&item
) const = 0;
116 virtual wxDataViewItem
GetParent( const wxDataViewItem
&child
) const = 0;
117 virtual wxDataViewItem
GetFirstChild( const wxDataViewItem
&parent
) const = 0;
118 virtual wxDataViewItem
GetNextSibling( const wxDataViewItem
&item
) const = 0;
119 virtual wxDataViewItem
GetNthChild( const wxDataViewItem
&parent
, unsigned int n
) const = 0;
121 // delegated notifiers
122 virtual bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
123 virtual bool ItemDeleted( const wxDataViewItem
&item
);
124 virtual bool ItemChanged( const wxDataViewItem
&item
);
125 virtual bool ValueChanged( const wxDataViewItem
&item
, unsigned int col
);
126 virtual bool Cleared();
128 void AddNotifier( wxDataViewModelNotifier
*notifier
);
129 void RemoveNotifier( wxDataViewModelNotifier
*notifier
);
131 void SetCompareFunction( wxDataViewModelCompare func
) { m_cmpFunc
= func
; }
132 wxDataViewModelCompare
GetCompareFunction() { return m_cmpFunc
; }
135 // the user should not delete this class directly: he should use DecRef() instead!
136 virtual ~wxDataViewModel() { }
139 wxDataViewModelCompare m_cmpFunc
;
142 // ---------------------------------------------------------
143 // wxDataViewModelNotifier
144 // ---------------------------------------------------------
146 class WXDLLIMPEXP_ADV wxDataViewModelNotifier
: public wxObject
149 wxDataViewModelNotifier() { }
150 virtual ~wxDataViewModelNotifier() { m_owner
= NULL
; }
152 virtual bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
) = 0;
153 virtual bool ItemDeleted( const wxDataViewItem
&item
) = 0;
154 virtual bool ItemChanged( const wxDataViewItem
&item
) = 0;
155 virtual bool ValueChanged( const wxDataViewItem
&item
, unsigned int col
) = 0;
156 virtual bool Cleared() = 0;
158 void SetOwner( wxDataViewModel
*owner
) { m_owner
= owner
; }
159 wxDataViewModel
*GetOwner() { return m_owner
; }
162 wxDataViewModel
*m_owner
;
166 //-----------------------------------------------------------------------------
167 // wxDataViewEditorCtrlEvtHandler
168 //-----------------------------------------------------------------------------
170 class wxDataViewEditorCtrlEvtHandler
: public wxEvtHandler
173 wxDataViewEditorCtrlEvtHandler( wxControl
*editor
, wxDataViewRenderer
*owner
);
175 void AcceptChangesAndFinish();
176 void SetFocusOnIdle( bool focus
= true ) { m_focusOnIdle
= focus
; }
179 void OnChar( wxKeyEvent
&event
);
180 void OnKillFocus( wxFocusEvent
&event
);
181 void OnIdle( wxIdleEvent
&event
);
184 wxDataViewRenderer
*m_owner
;
185 wxControl
*m_editorCtrl
;
190 DECLARE_EVENT_TABLE()
193 // ---------------------------------------------------------
194 // wxDataViewRendererBase
195 // ---------------------------------------------------------
197 enum wxDataViewCellMode
199 wxDATAVIEW_CELL_INERT
,
200 wxDATAVIEW_CELL_ACTIVATABLE
,
201 wxDATAVIEW_CELL_EDITABLE
204 enum wxDataViewCellRenderState
206 wxDATAVIEW_CELL_SELECTED
= 1,
207 wxDATAVIEW_CELL_PRELIT
= 2,
208 wxDATAVIEW_CELL_INSENSITIVE
= 4,
209 wxDATAVIEW_CELL_FOCUSED
= 8
212 class WXDLLIMPEXP_ADV wxDataViewRendererBase
: public wxObject
215 wxDataViewRendererBase( const wxString
&varianttype
,
216 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
,
217 int alignment
= wxDVR_DEFAULT_ALIGNMENT
);
219 virtual bool Validate( wxVariant
& WXUNUSED(value
) )
222 void SetOwner( wxDataViewColumn
*owner
) { m_owner
= owner
; }
223 wxDataViewColumn
* GetOwner() { return m_owner
; }
225 // renderer properties:
227 virtual bool SetValue( const wxVariant
& WXUNUSED(value
) ) = 0;
228 virtual bool GetValue( wxVariant
& WXUNUSED(value
) ) const = 0;
230 wxString
GetVariantType() const { return m_variantType
; }
232 virtual void SetMode( wxDataViewCellMode mode
) = 0;
233 virtual wxDataViewCellMode
GetMode() const = 0;
235 // NOTE: Set/GetAlignment do not take/return a wxAlignment enum but
236 // rather an "int"; that's because for rendering cells it's allowed
237 // to combine alignment flags (e.g. wxALIGN_LEFT|wxALIGN_BOTTOM)
238 virtual void SetAlignment( int align
) = 0;
239 virtual int GetAlignment() const = 0;
242 virtual bool HasEditorCtrl()
244 virtual wxControl
* CreateEditorCtrl(wxWindow
* WXUNUSED(parent
),
245 wxRect
WXUNUSED(labelRect
),
246 const wxVariant
& WXUNUSED(value
))
248 virtual bool GetValueFromEditorCtrl(wxControl
* WXUNUSED(editor
),
249 wxVariant
& WXUNUSED(value
))
252 virtual bool StartEditing( const wxDataViewItem
&item
, wxRect labelRect
);
253 virtual void CancelEditing();
254 virtual bool FinishEditing();
256 wxControl
*GetEditorCtrl() { return m_editorCtrl
; }
259 wxString m_variantType
;
260 wxDataViewColumn
*m_owner
;
261 wxControl
*m_editorCtrl
;
262 wxDataViewItem m_item
; // for m_editorCtrl
265 const wxDataViewCtrl
* GetView() const;
268 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewRendererBase
)
271 // ---------------------------------------------------------
272 // wxDataViewColumnBase
273 // ---------------------------------------------------------
275 enum wxDataViewColumnFlags
277 wxDATAVIEW_COL_RESIZABLE
= 1,
278 wxDATAVIEW_COL_SORTABLE
= 2,
279 wxDATAVIEW_COL_HIDDEN
= 4
282 class WXDLLIMPEXP_ADV wxDataViewColumnBase
: public wxObject
285 wxDataViewColumnBase( const wxString
&title
, wxDataViewRenderer
*renderer
,
286 unsigned int model_column
, int width
= wxDVC_DEFAULT_WIDTH
,
287 wxAlignment align
= wxALIGN_CENTER
,
288 int flags
= wxDATAVIEW_COL_RESIZABLE
);
289 wxDataViewColumnBase( const wxBitmap
&bitmap
, wxDataViewRenderer
*renderer
,
290 unsigned int model_column
, int width
= wxDVC_DEFAULT_WIDTH
,
291 wxAlignment align
= wxALIGN_CENTER
,
292 int flags
= wxDATAVIEW_COL_RESIZABLE
);
293 virtual ~wxDataViewColumnBase();
297 virtual void SetTitle( const wxString
&title
) = 0;
298 virtual void SetAlignment( wxAlignment align
) = 0;
299 virtual void SetSortable( bool sortable
) = 0;
300 virtual void SetResizeable( bool resizeable
) = 0;
301 virtual void SetHidden( bool hidden
) = 0;
302 virtual void SetSortOrder( bool ascending
) = 0;
303 virtual void SetFlags( int flags
);
304 virtual void SetOwner( wxDataViewCtrl
*owner
)
306 virtual void SetBitmap( const wxBitmap
&bitmap
)
309 virtual void SetMinWidth( int minWidth
) = 0;
310 virtual void SetWidth( int width
) = 0;
315 virtual wxString
GetTitle() const = 0;
316 virtual wxAlignment
GetAlignment() const = 0;
317 virtual int GetWidth() const = 0;
318 virtual int GetMinWidth() const = 0;
320 virtual int GetFlags() const;
322 virtual bool IsSortable() const = 0;
323 virtual bool IsResizeable() const = 0;
324 virtual bool IsHidden() const = 0;
325 virtual bool IsSortOrderAscending() const = 0;
327 const wxBitmap
&GetBitmap() const { return m_bitmap
; }
328 unsigned int GetModelColumn() const { return m_model_column
; }
330 wxDataViewCtrl
*GetOwner() { return m_owner
; }
331 wxDataViewRenderer
* GetRenderer() { return m_renderer
; }
334 wxDataViewRenderer
*m_renderer
;
337 wxDataViewCtrl
*m_owner
;
340 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewColumnBase
)
343 // ---------------------------------------------------------
344 // wxDataViewCtrlBase
345 // ---------------------------------------------------------
347 #define wxDV_SINGLE 0x0000 // for convenience
348 #define wxDV_MULTIPLE 0x0001 // can select multiple items
350 #define wxDV_NO_HEADER 0x0002 // column titles not visible
351 #define wxDV_HORIZ_RULES 0x0004 // light horizontal rules between rows
352 #define wxDV_VERT_RULES 0x0008 // light vertical rules between columns
354 class WXDLLIMPEXP_ADV wxDataViewCtrlBase
: public wxControl
357 wxDataViewCtrlBase();
358 virtual ~wxDataViewCtrlBase();
360 virtual bool AssociateModel( wxDataViewModel
*model
);
361 wxDataViewModel
* GetModel();
364 bool AppendTextColumn( const wxString
&label
, unsigned int model_column
,
365 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
, int width
= -1,
366 wxAlignment align
= wxALIGN_CENTER
,
367 int flags
= wxDATAVIEW_COL_RESIZABLE
);
368 bool AppendToggleColumn( const wxString
&label
, unsigned int model_column
,
369 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
, int width
= wxDVC_TOGGLE_DEFAULT_WIDTH
,
370 wxAlignment align
= wxALIGN_CENTER
,
371 int flags
= wxDATAVIEW_COL_RESIZABLE
);
372 bool AppendProgressColumn( const wxString
&label
, unsigned int model_column
,
373 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
, int width
= wxDVC_DEFAULT_WIDTH
,
374 wxAlignment align
= wxALIGN_CENTER
,
375 int flags
= wxDATAVIEW_COL_RESIZABLE
);
376 bool AppendDateColumn( const wxString
&label
, unsigned int model_column
,
377 wxDataViewCellMode mode
= wxDATAVIEW_CELL_ACTIVATABLE
, int width
= -1,
378 wxAlignment align
= wxALIGN_CENTER
,
379 int flags
= wxDATAVIEW_COL_RESIZABLE
);
380 bool AppendBitmapColumn( const wxString
&label
, unsigned int model_column
,
381 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
, int width
= -1,
382 wxAlignment align
= wxALIGN_CENTER
,
383 int flags
= wxDATAVIEW_COL_RESIZABLE
);
384 bool AppendTextColumn( const wxBitmap
&label
, unsigned int model_column
,
385 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
, int width
= -1,
386 wxAlignment align
= wxALIGN_CENTER
,
387 int flags
= wxDATAVIEW_COL_RESIZABLE
);
388 bool AppendToggleColumn( const wxBitmap
&label
, unsigned int model_column
,
389 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
, int width
= wxDVC_TOGGLE_DEFAULT_WIDTH
,
390 wxAlignment align
= wxALIGN_CENTER
,
391 int flags
= wxDATAVIEW_COL_RESIZABLE
);
392 bool AppendProgressColumn( const wxBitmap
&label
, unsigned int model_column
,
393 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
, int width
= wxDVC_DEFAULT_WIDTH
,
394 wxAlignment align
= wxALIGN_CENTER
,
395 int flags
= wxDATAVIEW_COL_RESIZABLE
);
396 bool AppendDateColumn( const wxBitmap
&label
, unsigned int model_column
,
397 wxDataViewCellMode mode
= wxDATAVIEW_CELL_ACTIVATABLE
, int width
= -1,
398 wxAlignment align
= wxALIGN_CENTER
,
399 int flags
= wxDATAVIEW_COL_RESIZABLE
);
400 bool AppendBitmapColumn( const wxBitmap
&label
, unsigned int model_column
,
401 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
, int width
= -1,
402 wxAlignment align
= wxALIGN_CENTER
,
403 int flags
= wxDATAVIEW_COL_RESIZABLE
);
405 virtual bool AppendColumn( wxDataViewColumn
*col
);
407 virtual unsigned int GetColumnCount() const;
409 virtual bool DeleteColumn( unsigned int pos
);
410 virtual bool ClearColumns();
411 virtual wxDataViewColumn
* GetColumn( unsigned int pos
);
413 // TODO selection code
416 wxDataViewModel
*m_model
;
418 wxDataViewEventModelNotifier
*m_eventNotifier
;
421 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewCtrlBase
)
424 // ----------------------------------------------------------------------------
425 // wxDataViewEvent - the event class for the wxDataViewCtrl notifications
426 // ----------------------------------------------------------------------------
428 class WXDLLIMPEXP_ADV wxDataViewEvent
: public wxNotifyEvent
431 wxDataViewEvent(wxEventType commandType
= wxEVT_NULL
, int winid
= 0)
432 : wxNotifyEvent(commandType
, winid
),
436 m_value(wxNullVariant
),
440 wxDataViewEvent(const wxDataViewEvent
& event
)
441 : wxNotifyEvent(event
),
442 m_item(event
.m_item
),
444 m_model(event
.m_model
),
445 m_value(event
.m_value
),
446 m_column(event
.m_column
)
449 wxDataViewItem
GetItem() const { return m_item
; }
450 void SetItem( const wxDataViewItem
&item
) { m_item
= item
; }
452 int GetColumn() const { return m_col
; }
453 void SetColumn( int col
) { m_col
= col
; }
455 wxDataViewModel
* GetModel() const { return m_model
; }
456 void SetModel( wxDataViewModel
*model
) { m_model
= model
; }
458 const wxVariant
&GetValue() const { return m_value
; }
459 void SetValue( const wxVariant
&value
) { m_value
= value
; }
461 // for wxEVT_DATAVIEW_COLUMN_HEADER_CLICKED only
462 void SetDataViewColumn( wxDataViewColumn
*col
) { m_column
= col
; }
463 wxDataViewColumn
*GetDataViewColumn() const { return m_column
; }
465 virtual wxEvent
*Clone() const { return new wxDataViewEvent(*this); }
468 wxDataViewItem m_item
;
470 wxDataViewModel
*m_model
;
472 wxDataViewColumn
*m_column
;
475 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxDataViewEvent
)
478 BEGIN_DECLARE_EVENT_TYPES()
479 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_ADV
, wxEVT_COMMAND_DATAVIEW_ITEM_SELECTED
, -1)
480 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_ADV
, wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, -1)
481 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_ADV
, wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
, -1)
482 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_ADV
, wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
, -1)
483 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_ADV
, wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
, -1)
484 // notifications from the model to the control
485 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_ADV
, wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_ADDED
, -1)
486 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_ADV
, wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_DELETED
, -1)
487 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_ADV
, wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_CHANGED
, -1)
488 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_ADV
, wxEVT_COMMAND_DATAVIEW_MODEL_VALUE_CHANGED
, -1)
489 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_ADV
, wxEVT_COMMAND_DATAVIEW_MODEL_CLEARED
, -1)
490 END_DECLARE_EVENT_TYPES()
492 typedef void (wxEvtHandler::*wxDataViewEventFunction
)(wxDataViewEvent
&);
494 #define wxDataViewEventHandler(func) \
495 (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxDataViewEventFunction, &func)
497 #define wx__DECLARE_DATAVIEWEVT(evt, id, fn) \
498 wx__DECLARE_EVT1(wxEVT_COMMAND_DATAVIEW_ ## evt, id, wxDataViewEventHandler(fn))
500 #define EVT_DATAVIEW_ITEM_SELECTED(id, fn) wx__DECLARE_DATAVIEWEVT(ITEM_SELECTED, id, fn)
501 #define EVT_DATAVIEW_ITEM_ACTIVATED(id, fn) wx__DECLARE_DATAVIEWEVT(ITEM_ACTIVATED, id, fn)
502 #define EVT_DATAVIEW_COLUMN_HEADER_CLICK(id, fn) wx__DECLARE_DATAVIEWEVT(COLUMN_HEADER_CLICK, id, fn)
503 #define EVT_DATAVIEW_COLUMN_HEADER_RIGHT_CLICKED(id, fn) wx__DECLARE_DATAVIEWEVT(COLUMN_HEADER_RIGHT_CLICK, id, fn)
504 #define EVT_DATAVIEW_COLUMN_SORTED(id, fn) wx__DECLARE_DATAVIEWEVT(COLUMN_SORTED, id, fn)
506 #define EVT_DATAVIEW_MODEL_ITEM_ADDED(id, fn) wx__DECLARE_DATAVIEWEVT(MODEL_ITEM_APPENDED, id, fn)
507 #define EVT_DATAVIEW_MODEL_ITEM_DELETED(id, fn) wx__DECLARE_DATAVIEWEVT(MODEL_ITEM_DELETED, id, fn)
508 #define EVT_DATAVIEW_MODEL_ITEM_CHANGED(id, fn) wx__DECLARE_DATAVIEWEVT(MODEL_ITEM_CHANGED, id, fn)
509 #define EVT_DATAVIEW_MODEL_VALUE_CHANGED(id, fn) wx__DECLARE_DATAVIEWEVT(MODEL_VALUE_CHANGED, id, fn)
510 #define EVT_DATAVIEW_MODEL_CLEARED(id, fn) wx__DECLARE_DATAVIEWEVT(MODEL_CLEARED, id, fn)
513 #if defined(wxUSE_GENERICDATAVIEWCTRL)
514 #include "wx/generic/dataview.h"
515 #elif defined(__WXGTK20__)
516 #include "wx/gtk/dataview.h"
517 #elif defined(__WXMAC__)
518 #include "wx/mac/dataview.h"
520 #include "wx/generic/dataview.h"
523 #endif // wxUSE_DATAVIEWCTRL
526 // _WX_DATAVIEW_H_BASE_