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_ADV wxDataViewItem
;
42 class WXDLLIMPEXP_ADV wxDataViewModel
;
43 class WXDLLIMPEXP_ADV wxDataViewCtrl
;
44 class WXDLLIMPEXP_ADV wxDataViewColumn
;
45 class WXDLLIMPEXP_ADV wxDataViewRenderer
;
46 class WXDLLIMPEXP_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 // ---------------------------------------------------------
88 // ---------------------------------------------------------
90 class WXDLLIMPEXP_ADV wxDataViewModel
: public wxObjectRefData
95 virtual unsigned int GetColumnCount() const = 0;
97 // return type as reported by wxVariant
98 virtual wxString
GetColumnType( unsigned int col
) const = 0;
100 // get value into a wxVariant
101 virtual void GetValue( wxVariant
&variant
,
102 const wxDataViewItem
&item
, unsigned int col
) const = 0;
104 // set value, call ValueChanged() afterwards!
105 virtual bool SetValue( const wxVariant
&variant
,
106 const wxDataViewItem
&item
, unsigned int col
) = 0;
109 virtual bool HasChildren( const wxDataViewItem
&item
) const = 0;
110 virtual int GetChildCount( const wxDataViewItem
&item
) const = 0;
111 virtual wxDataViewItem
GetParent( const wxDataViewItem
&child
) const = 0;
112 virtual wxDataViewItem
GetFirstChild( const wxDataViewItem
&parent
) const = 0;
113 virtual wxDataViewItem
GetNextSibling( const wxDataViewItem
&item
) const = 0;
114 virtual wxDataViewItem
GetNthChild( const wxDataViewItem
&parent
, unsigned int n
) const = 0;
116 // delegated notifiers
117 virtual bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
118 virtual bool ItemDeleted( const wxDataViewItem
&item
);
119 virtual bool ItemChanged( const wxDataViewItem
&item
);
120 virtual bool ValueChanged( const wxDataViewItem
&item
, unsigned int col
);
121 virtual bool Cleared();
123 void AddNotifier( wxDataViewModelNotifier
*notifier
);
124 void RemoveNotifier( wxDataViewModelNotifier
*notifier
);
127 // the user should not delete this class directly: he should use DecRef() instead!
128 virtual ~wxDataViewModel() { }
133 // ---------------------------------------------------------
134 // wxDataViewModelNotifier
135 // ---------------------------------------------------------
137 class WXDLLIMPEXP_ADV wxDataViewModelNotifier
: public wxObject
140 wxDataViewModelNotifier() { }
141 virtual ~wxDataViewModelNotifier() { m_owner
= NULL
; }
143 virtual bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
) = 0;
144 virtual bool ItemDeleted( const wxDataViewItem
&item
) = 0;
145 virtual bool ItemChanged( const wxDataViewItem
&item
) = 0;
146 virtual bool ValueChanged( const wxDataViewItem
&item
, unsigned int col
) = 0;
147 virtual bool Cleared() = 0;
149 void SetOwner( wxDataViewModel
*owner
) { m_owner
= owner
; }
150 wxDataViewModel
*GetOwner() { return m_owner
; }
153 wxDataViewModel
*m_owner
;
157 //-----------------------------------------------------------------------------
158 // wxDataViewEditorCtrlEvtHandler
159 //-----------------------------------------------------------------------------
161 class wxDataViewEditorCtrlEvtHandler
: public wxEvtHandler
164 wxDataViewEditorCtrlEvtHandler( wxControl
*editor
, wxDataViewRenderer
*owner
);
166 void AcceptChangesAndFinish();
167 void SetFocusOnIdle( bool focus
= true ) { m_focusOnIdle
= focus
; }
170 void OnChar( wxKeyEvent
&event
);
171 void OnKillFocus( wxFocusEvent
&event
);
172 void OnIdle( wxIdleEvent
&event
);
175 wxDataViewRenderer
*m_owner
;
176 wxControl
*m_editorCtrl
;
181 DECLARE_EVENT_TABLE()
184 // ---------------------------------------------------------
185 // wxDataViewRendererBase
186 // ---------------------------------------------------------
188 enum wxDataViewCellMode
190 wxDATAVIEW_CELL_INERT
,
191 wxDATAVIEW_CELL_ACTIVATABLE
,
192 wxDATAVIEW_CELL_EDITABLE
195 enum wxDataViewCellRenderState
197 wxDATAVIEW_CELL_SELECTED
= 1,
198 wxDATAVIEW_CELL_PRELIT
= 2,
199 wxDATAVIEW_CELL_INSENSITIVE
= 4,
200 wxDATAVIEW_CELL_FOCUSED
= 8
203 class WXDLLIMPEXP_ADV wxDataViewRendererBase
: public wxObject
206 wxDataViewRendererBase( const wxString
&varianttype
,
207 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
,
208 int alignment
= wxDVR_DEFAULT_ALIGNMENT
);
210 virtual bool Validate( wxVariant
& WXUNUSED(value
) )
213 void SetOwner( wxDataViewColumn
*owner
) { m_owner
= owner
; }
214 wxDataViewColumn
* GetOwner() { return m_owner
; }
216 // renderer properties:
218 virtual bool SetValue( const wxVariant
& WXUNUSED(value
) ) = 0;
219 virtual bool GetValue( wxVariant
& WXUNUSED(value
) ) const = 0;
221 wxString
GetVariantType() const { return m_variantType
; }
223 virtual void SetMode( wxDataViewCellMode mode
) = 0;
224 virtual wxDataViewCellMode
GetMode() const = 0;
226 // NOTE: Set/GetAlignment do not take/return a wxAlignment enum but
227 // rather an "int"; that's because for rendering cells it's allowed
228 // to combine alignment flags (e.g. wxALIGN_LEFT|wxALIGN_BOTTOM)
229 virtual void SetAlignment( int align
) = 0;
230 virtual int GetAlignment() const = 0;
233 virtual bool HasEditorCtrl()
235 virtual wxControl
* CreateEditorCtrl(wxWindow
* WXUNUSED(parent
),
236 wxRect
WXUNUSED(labelRect
),
237 const wxVariant
& WXUNUSED(value
))
239 virtual bool GetValueFromEditorCtrl(wxControl
* WXUNUSED(editor
),
240 wxVariant
& WXUNUSED(value
))
243 virtual bool StartEditing( const wxDataViewItem
&item
, wxRect labelRect
);
244 virtual void CancelEditing();
245 virtual bool FinishEditing();
247 wxControl
*GetEditorCtrl() { return m_editorCtrl
; }
250 wxString m_variantType
;
251 wxDataViewColumn
*m_owner
;
252 wxControl
*m_editorCtrl
;
253 wxDataViewItem m_item
; // for m_editorCtrl
256 const wxDataViewCtrl
* GetView() const;
259 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewRendererBase
)
262 // ---------------------------------------------------------
263 // wxDataViewColumnBase
264 // ---------------------------------------------------------
266 enum wxDataViewColumnFlags
268 wxDATAVIEW_COL_RESIZABLE
= 1,
269 wxDATAVIEW_COL_SORTABLE
= 2,
270 wxDATAVIEW_COL_HIDDEN
= 4
273 class WXDLLIMPEXP_ADV wxDataViewColumnBase
: public wxObject
276 wxDataViewColumnBase( const wxString
&title
, wxDataViewRenderer
*renderer
,
277 unsigned int model_column
, int width
= wxDVC_DEFAULT_WIDTH
,
278 wxAlignment align
= wxALIGN_CENTER
,
279 int flags
= wxDATAVIEW_COL_RESIZABLE
);
280 wxDataViewColumnBase( const wxBitmap
&bitmap
, wxDataViewRenderer
*renderer
,
281 unsigned int model_column
, int width
= wxDVC_DEFAULT_WIDTH
,
282 wxAlignment align
= wxALIGN_CENTER
,
283 int flags
= wxDATAVIEW_COL_RESIZABLE
);
284 virtual ~wxDataViewColumnBase();
288 virtual void SetTitle( const wxString
&title
) = 0;
289 virtual void SetAlignment( wxAlignment align
) = 0;
290 virtual void SetSortable( bool sortable
) = 0;
291 virtual void SetResizeable( bool resizeable
) = 0;
292 virtual void SetHidden( bool hidden
) = 0;
293 virtual void SetSortOrder( bool ascending
) = 0;
294 virtual void SetFlags( int flags
);
295 virtual void SetOwner( wxDataViewCtrl
*owner
)
297 virtual void SetBitmap( const wxBitmap
&bitmap
)
300 virtual void SetMinWidth( int minWidth
) = 0;
301 virtual void SetWidth( int width
) = 0;
306 virtual wxString
GetTitle() const = 0;
307 virtual wxAlignment
GetAlignment() const = 0;
308 virtual int GetWidth() const = 0;
309 virtual int GetMinWidth() const = 0;
311 virtual int GetFlags() const;
313 virtual bool IsSortable() const = 0;
314 virtual bool IsResizeable() const = 0;
315 virtual bool IsHidden() const = 0;
316 virtual bool IsSortOrderAscending() const = 0;
318 const wxBitmap
&GetBitmap() const { return m_bitmap
; }
319 unsigned int GetModelColumn() const { return m_model_column
; }
321 wxDataViewCtrl
*GetOwner() { return m_owner
; }
322 wxDataViewRenderer
* GetRenderer() { return m_renderer
; }
325 wxDataViewRenderer
*m_renderer
;
328 wxDataViewCtrl
*m_owner
;
331 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewColumnBase
)
334 // ---------------------------------------------------------
335 // wxDataViewCtrlBase
336 // ---------------------------------------------------------
338 #define wxDV_SINGLE 0x0000 // for convenience
339 #define wxDV_MULTIPLE 0x0001 // can select multiple items
341 #define wxDV_NO_HEADER 0x0002 // column titles not visible
342 #define wxDV_HORIZ_RULES 0x0004 // light horizontal rules between rows
343 #define wxDV_VERT_RULES 0x0008 // light vertical rules between columns
345 class WXDLLIMPEXP_ADV wxDataViewCtrlBase
: public wxControl
348 wxDataViewCtrlBase();
349 virtual ~wxDataViewCtrlBase();
351 virtual bool AssociateModel( wxDataViewModel
*model
);
352 wxDataViewModel
* GetModel();
355 bool AppendTextColumn( const wxString
&label
, unsigned int model_column
,
356 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
, int width
= -1,
357 wxAlignment align
= wxALIGN_CENTER
,
358 int flags
= wxDATAVIEW_COL_RESIZABLE
);
359 bool AppendToggleColumn( const wxString
&label
, unsigned int model_column
,
360 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
, int width
= wxDVC_TOGGLE_DEFAULT_WIDTH
,
361 wxAlignment align
= wxALIGN_CENTER
,
362 int flags
= wxDATAVIEW_COL_RESIZABLE
);
363 bool AppendProgressColumn( const wxString
&label
, unsigned int model_column
,
364 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
, int width
= wxDVC_DEFAULT_WIDTH
,
365 wxAlignment align
= wxALIGN_CENTER
,
366 int flags
= wxDATAVIEW_COL_RESIZABLE
);
367 bool AppendDateColumn( const wxString
&label
, unsigned int model_column
,
368 wxDataViewCellMode mode
= wxDATAVIEW_CELL_ACTIVATABLE
, int width
= -1,
369 wxAlignment align
= wxALIGN_CENTER
,
370 int flags
= wxDATAVIEW_COL_RESIZABLE
);
371 bool AppendBitmapColumn( const wxString
&label
, unsigned int model_column
,
372 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
, int width
= -1,
373 wxAlignment align
= wxALIGN_CENTER
,
374 int flags
= wxDATAVIEW_COL_RESIZABLE
);
375 bool AppendTextColumn( const wxBitmap
&label
, unsigned int model_column
,
376 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
, int width
= -1,
377 wxAlignment align
= wxALIGN_CENTER
,
378 int flags
= wxDATAVIEW_COL_RESIZABLE
);
379 bool AppendToggleColumn( const wxBitmap
&label
, unsigned int model_column
,
380 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
, int width
= wxDVC_TOGGLE_DEFAULT_WIDTH
,
381 wxAlignment align
= wxALIGN_CENTER
,
382 int flags
= wxDATAVIEW_COL_RESIZABLE
);
383 bool AppendProgressColumn( const wxBitmap
&label
, unsigned int model_column
,
384 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
, int width
= wxDVC_DEFAULT_WIDTH
,
385 wxAlignment align
= wxALIGN_CENTER
,
386 int flags
= wxDATAVIEW_COL_RESIZABLE
);
387 bool AppendDateColumn( const wxBitmap
&label
, unsigned int model_column
,
388 wxDataViewCellMode mode
= wxDATAVIEW_CELL_ACTIVATABLE
, int width
= -1,
389 wxAlignment align
= wxALIGN_CENTER
,
390 int flags
= wxDATAVIEW_COL_RESIZABLE
);
391 bool AppendBitmapColumn( const wxBitmap
&label
, unsigned int model_column
,
392 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
, int width
= -1,
393 wxAlignment align
= wxALIGN_CENTER
,
394 int flags
= wxDATAVIEW_COL_RESIZABLE
);
396 virtual bool AppendColumn( wxDataViewColumn
*col
);
398 virtual unsigned int GetColumnCount() const;
400 virtual bool DeleteColumn( unsigned int pos
);
401 virtual bool ClearColumns();
402 virtual wxDataViewColumn
* GetColumn( unsigned int pos
);
404 // TODO selection code
407 wxDataViewModel
*m_model
;
409 wxDataViewEventModelNotifier
*m_eventNotifier
;
412 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewCtrlBase
)
415 // ----------------------------------------------------------------------------
416 // wxDataViewEvent - the event class for the wxDataViewCtrl notifications
417 // ----------------------------------------------------------------------------
419 class WXDLLIMPEXP_ADV wxDataViewEvent
: public wxNotifyEvent
422 wxDataViewEvent(wxEventType commandType
= wxEVT_NULL
, int winid
= 0)
423 : wxNotifyEvent(commandType
, winid
),
427 m_value(wxNullVariant
),
431 wxDataViewEvent(const wxDataViewEvent
& event
)
432 : wxNotifyEvent(event
),
433 m_item(event
.m_item
),
435 m_model(event
.m_model
),
436 m_value(event
.m_value
),
437 m_column(event
.m_column
)
440 wxDataViewItem
GetItem() const { return m_item
; }
441 void SetItem( const wxDataViewItem
&item
) { m_item
= item
; }
443 int GetColumn() const { return m_col
; }
444 void SetColumn( int col
) { m_col
= col
; }
446 wxDataViewModel
* GetModel() const { return m_model
; }
447 void SetModel( wxDataViewModel
*model
) { m_model
= model
; }
449 const wxVariant
&GetValue() const { return m_value
; }
450 void SetValue( const wxVariant
&value
) { m_value
= value
; }
452 // for wxEVT_DATAVIEW_COLUMN_HEADER_CLICKED only
453 void SetDataViewColumn( wxDataViewColumn
*col
) { m_column
= col
; }
454 wxDataViewColumn
*GetDataViewColumn() const { return m_column
; }
456 virtual wxEvent
*Clone() const { return new wxDataViewEvent(*this); }
459 wxDataViewItem m_item
;
461 wxDataViewModel
*m_model
;
463 wxDataViewColumn
*m_column
;
466 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxDataViewEvent
)
469 BEGIN_DECLARE_EVENT_TYPES()
470 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_ADV
, wxEVT_COMMAND_DATAVIEW_ITEM_SELECTED
, -1)
471 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_ADV
, wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, -1)
472 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_ADV
, wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
, -1)
473 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_ADV
, wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
, -1)
474 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_ADV
, wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
, -1)
475 // notifications from the model to the control
476 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_ADV
, wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_ADDED
, -1)
477 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_ADV
, wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_DELETED
, -1)
478 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_ADV
, wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_CHANGED
, -1)
479 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_ADV
, wxEVT_COMMAND_DATAVIEW_MODEL_VALUE_CHANGED
, -1)
480 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_ADV
, wxEVT_COMMAND_DATAVIEW_MODEL_CLEARED
, -1)
481 END_DECLARE_EVENT_TYPES()
483 typedef void (wxEvtHandler::*wxDataViewEventFunction
)(wxDataViewEvent
&);
485 #define wxDataViewEventHandler(func) \
486 (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxDataViewEventFunction, &func)
488 #define wx__DECLARE_DATAVIEWEVT(evt, id, fn) \
489 wx__DECLARE_EVT1(wxEVT_COMMAND_DATAVIEW_ ## evt, id, wxDataViewEventHandler(fn))
491 #define EVT_DATAVIEW_ITEM_SELECTED(id, fn) wx__DECLARE_DATAVIEWEVT(ITEM_SELECTED, id, fn)
492 #define EVT_DATAVIEW_ITEM_ACTIVATED(id, fn) wx__DECLARE_DATAVIEWEVT(ITEM_ACTIVATED, id, fn)
493 #define EVT_DATAVIEW_COLUMN_HEADER_CLICK(id, fn) wx__DECLARE_DATAVIEWEVT(COLUMN_HEADER_CLICK, id, fn)
494 #define EVT_DATAVIEW_COLUMN_HEADER_RIGHT_CLICKED(id, fn) wx__DECLARE_DATAVIEWEVT(COLUMN_HEADER_RIGHT_CLICK, id, fn)
495 #define EVT_DATAVIEW_COLUMN_SORTED(id, fn) wx__DECLARE_DATAVIEWEVT(COLUMN_SORTED, id, fn)
497 #define EVT_DATAVIEW_MODEL_ITEM_ADDED(id, fn) wx__DECLARE_DATAVIEWEVT(MODEL_ITEM_APPENDED, id, fn)
498 #define EVT_DATAVIEW_MODEL_ITEM_DELETED(id, fn) wx__DECLARE_DATAVIEWEVT(MODEL_ITEM_DELETED, id, fn)
499 #define EVT_DATAVIEW_MODEL_ITEM_CHANGED(id, fn) wx__DECLARE_DATAVIEWEVT(MODEL_ITEM_CHANGED, id, fn)
500 #define EVT_DATAVIEW_MODEL_VALUE_CHANGED(id, fn) wx__DECLARE_DATAVIEWEVT(MODEL_VALUE_CHANGED, id, fn)
501 #define EVT_DATAVIEW_MODEL_CLEARED(id, fn) wx__DECLARE_DATAVIEWEVT(MODEL_CLEARED, id, fn)
504 #if defined(wxUSE_GENERICDATAVIEWCTRL)
505 #include "wx/generic/dataview.h"
506 #elif defined(__WXGTK20__)
507 #include "wx/gtk/dataview.h"
508 #elif defined(__WXMAC__)
509 #include "wx/mac/dataview.h"
511 #include "wx/generic/dataview.h"
514 #endif // wxUSE_DATAVIEWCTRL
517 // _WX_DATAVIEW_H_BASE_