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 wxDataViewItem
GetFirstChild( const wxDataViewItem
&parent
) const = 0;
116 virtual wxDataViewItem
GetNextSibling( const wxDataViewItem
&item
) const = 0;
118 // delegated notifiers
119 virtual bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
120 virtual bool ItemDeleted( const wxDataViewItem
&item
);
121 virtual bool ItemChanged( const wxDataViewItem
&item
);
122 virtual bool ValueChanged( const wxDataViewItem
&item
, unsigned int col
);
123 virtual bool Cleared();
125 void AddNotifier( wxDataViewModelNotifier
*notifier
);
126 void RemoveNotifier( wxDataViewModelNotifier
*notifier
);
128 void SetCompareFunction( wxDataViewModelCompare func
) { m_cmpFunc
= func
; }
129 wxDataViewModelCompare
GetCompareFunction() { return m_cmpFunc
; }
132 // the user should not delete this class directly: he should use DecRef() instead!
133 virtual ~wxDataViewModel() { }
136 wxDataViewModelCompare m_cmpFunc
;
139 // ---------------------------------------------------------
140 // wxDataViewModelNotifier
141 // ---------------------------------------------------------
143 class WXDLLIMPEXP_ADV wxDataViewModelNotifier
: public wxObject
146 wxDataViewModelNotifier() { }
147 virtual ~wxDataViewModelNotifier() { m_owner
= NULL
; }
149 virtual bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
) = 0;
150 virtual bool ItemDeleted( const wxDataViewItem
&item
) = 0;
151 virtual bool ItemChanged( const wxDataViewItem
&item
) = 0;
152 virtual bool ValueChanged( const wxDataViewItem
&item
, unsigned int col
) = 0;
153 virtual bool Cleared() = 0;
155 void SetOwner( wxDataViewModel
*owner
) { m_owner
= owner
; }
156 wxDataViewModel
*GetOwner() { return m_owner
; }
159 wxDataViewModel
*m_owner
;
163 //-----------------------------------------------------------------------------
164 // wxDataViewEditorCtrlEvtHandler
165 //-----------------------------------------------------------------------------
167 class wxDataViewEditorCtrlEvtHandler
: public wxEvtHandler
170 wxDataViewEditorCtrlEvtHandler( wxControl
*editor
, wxDataViewRenderer
*owner
);
172 void AcceptChangesAndFinish();
173 void SetFocusOnIdle( bool focus
= true ) { m_focusOnIdle
= focus
; }
176 void OnChar( wxKeyEvent
&event
);
177 void OnKillFocus( wxFocusEvent
&event
);
178 void OnIdle( wxIdleEvent
&event
);
181 wxDataViewRenderer
*m_owner
;
182 wxControl
*m_editorCtrl
;
187 DECLARE_EVENT_TABLE()
190 // ---------------------------------------------------------
191 // wxDataViewRendererBase
192 // ---------------------------------------------------------
194 enum wxDataViewCellMode
196 wxDATAVIEW_CELL_INERT
,
197 wxDATAVIEW_CELL_ACTIVATABLE
,
198 wxDATAVIEW_CELL_EDITABLE
201 enum wxDataViewCellRenderState
203 wxDATAVIEW_CELL_SELECTED
= 1,
204 wxDATAVIEW_CELL_PRELIT
= 2,
205 wxDATAVIEW_CELL_INSENSITIVE
= 4,
206 wxDATAVIEW_CELL_FOCUSED
= 8
209 class WXDLLIMPEXP_ADV wxDataViewRendererBase
: public wxObject
212 wxDataViewRendererBase( const wxString
&varianttype
,
213 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
,
214 int alignment
= wxDVR_DEFAULT_ALIGNMENT
);
216 virtual bool Validate( wxVariant
& WXUNUSED(value
) )
219 void SetOwner( wxDataViewColumn
*owner
) { m_owner
= owner
; }
220 wxDataViewColumn
* GetOwner() { return m_owner
; }
222 // renderer properties:
224 virtual bool SetValue( const wxVariant
& WXUNUSED(value
) ) = 0;
225 virtual bool GetValue( wxVariant
& WXUNUSED(value
) ) const = 0;
227 wxString
GetVariantType() const { return m_variantType
; }
229 virtual void SetMode( wxDataViewCellMode mode
) = 0;
230 virtual wxDataViewCellMode
GetMode() const = 0;
232 // NOTE: Set/GetAlignment do not take/return a wxAlignment enum but
233 // rather an "int"; that's because for rendering cells it's allowed
234 // to combine alignment flags (e.g. wxALIGN_LEFT|wxALIGN_BOTTOM)
235 virtual void SetAlignment( int align
) = 0;
236 virtual int GetAlignment() const = 0;
239 virtual bool HasEditorCtrl()
241 virtual wxControl
* CreateEditorCtrl(wxWindow
* WXUNUSED(parent
),
242 wxRect
WXUNUSED(labelRect
),
243 const wxVariant
& WXUNUSED(value
))
245 virtual bool GetValueFromEditorCtrl(wxControl
* WXUNUSED(editor
),
246 wxVariant
& WXUNUSED(value
))
249 virtual bool StartEditing( const wxDataViewItem
&item
, wxRect labelRect
);
250 virtual void CancelEditing();
251 virtual bool FinishEditing();
253 wxControl
*GetEditorCtrl() { return m_editorCtrl
; }
256 wxString m_variantType
;
257 wxDataViewColumn
*m_owner
;
258 wxControl
*m_editorCtrl
;
259 wxDataViewItem m_item
; // for m_editorCtrl
262 const wxDataViewCtrl
* GetView() const;
265 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewRendererBase
)
268 // ---------------------------------------------------------
269 // wxDataViewColumnBase
270 // ---------------------------------------------------------
272 enum wxDataViewColumnFlags
274 wxDATAVIEW_COL_RESIZABLE
= 1,
275 wxDATAVIEW_COL_SORTABLE
= 2,
276 wxDATAVIEW_COL_HIDDEN
= 4
279 class WXDLLIMPEXP_ADV wxDataViewColumnBase
: public wxObject
282 wxDataViewColumnBase( const wxString
&title
, wxDataViewRenderer
*renderer
,
283 unsigned int model_column
, int width
= wxDVC_DEFAULT_WIDTH
,
284 wxAlignment align
= wxALIGN_CENTER
,
285 int flags
= wxDATAVIEW_COL_RESIZABLE
);
286 wxDataViewColumnBase( const wxBitmap
&bitmap
, wxDataViewRenderer
*renderer
,
287 unsigned int model_column
, int width
= wxDVC_DEFAULT_WIDTH
,
288 wxAlignment align
= wxALIGN_CENTER
,
289 int flags
= wxDATAVIEW_COL_RESIZABLE
);
290 virtual ~wxDataViewColumnBase();
294 virtual void SetTitle( const wxString
&title
) = 0;
295 virtual void SetAlignment( wxAlignment align
) = 0;
296 virtual void SetSortable( bool sortable
) = 0;
297 virtual void SetResizeable( bool resizeable
) = 0;
298 virtual void SetHidden( bool hidden
) = 0;
299 virtual void SetSortOrder( bool ascending
) = 0;
300 virtual void SetFlags( int flags
);
301 virtual void SetOwner( wxDataViewCtrl
*owner
)
303 virtual void SetBitmap( const wxBitmap
&bitmap
)
306 virtual void SetMinWidth( int minWidth
) = 0;
307 virtual void SetWidth( int width
) = 0;
312 virtual wxString
GetTitle() const = 0;
313 virtual wxAlignment
GetAlignment() const = 0;
314 virtual int GetWidth() const = 0;
315 virtual int GetMinWidth() const = 0;
317 virtual int GetFlags() const;
319 virtual bool IsSortable() const = 0;
320 virtual bool IsResizeable() const = 0;
321 virtual bool IsHidden() const = 0;
322 virtual bool IsSortOrderAscending() const = 0;
324 const wxBitmap
&GetBitmap() const { return m_bitmap
; }
325 unsigned int GetModelColumn() const { return m_model_column
; }
327 wxDataViewCtrl
*GetOwner() { return m_owner
; }
328 wxDataViewRenderer
* GetRenderer() { return m_renderer
; }
331 wxDataViewRenderer
*m_renderer
;
334 wxDataViewCtrl
*m_owner
;
337 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewColumnBase
)
340 // ---------------------------------------------------------
341 // wxDataViewCtrlBase
342 // ---------------------------------------------------------
344 #define wxDV_SINGLE 0x0000 // for convenience
345 #define wxDV_MULTIPLE 0x0001 // can select multiple items
347 #define wxDV_NO_HEADER 0x0002 // column titles not visible
348 #define wxDV_HORIZ_RULES 0x0004 // light horizontal rules between rows
349 #define wxDV_VERT_RULES 0x0008 // light vertical rules between columns
351 class WXDLLIMPEXP_ADV wxDataViewCtrlBase
: public wxControl
354 wxDataViewCtrlBase();
355 virtual ~wxDataViewCtrlBase();
357 virtual bool AssociateModel( wxDataViewModel
*model
);
358 wxDataViewModel
* GetModel();
361 bool AppendTextColumn( const wxString
&label
, unsigned int model_column
,
362 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
, int width
= -1,
363 wxAlignment align
= wxALIGN_CENTER
,
364 int flags
= wxDATAVIEW_COL_RESIZABLE
);
365 bool AppendToggleColumn( const wxString
&label
, unsigned int model_column
,
366 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
, int width
= wxDVC_TOGGLE_DEFAULT_WIDTH
,
367 wxAlignment align
= wxALIGN_CENTER
,
368 int flags
= wxDATAVIEW_COL_RESIZABLE
);
369 bool AppendProgressColumn( const wxString
&label
, unsigned int model_column
,
370 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
, int width
= wxDVC_DEFAULT_WIDTH
,
371 wxAlignment align
= wxALIGN_CENTER
,
372 int flags
= wxDATAVIEW_COL_RESIZABLE
);
373 bool AppendDateColumn( const wxString
&label
, unsigned int model_column
,
374 wxDataViewCellMode mode
= wxDATAVIEW_CELL_ACTIVATABLE
, int width
= -1,
375 wxAlignment align
= wxALIGN_CENTER
,
376 int flags
= wxDATAVIEW_COL_RESIZABLE
);
377 bool AppendBitmapColumn( const wxString
&label
, unsigned int model_column
,
378 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
, int width
= -1,
379 wxAlignment align
= wxALIGN_CENTER
,
380 int flags
= wxDATAVIEW_COL_RESIZABLE
);
381 bool AppendTextColumn( const wxBitmap
&label
, unsigned int model_column
,
382 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
, int width
= -1,
383 wxAlignment align
= wxALIGN_CENTER
,
384 int flags
= wxDATAVIEW_COL_RESIZABLE
);
385 bool AppendToggleColumn( const wxBitmap
&label
, unsigned int model_column
,
386 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
, int width
= wxDVC_TOGGLE_DEFAULT_WIDTH
,
387 wxAlignment align
= wxALIGN_CENTER
,
388 int flags
= wxDATAVIEW_COL_RESIZABLE
);
389 bool AppendProgressColumn( const wxBitmap
&label
, unsigned int model_column
,
390 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
, int width
= wxDVC_DEFAULT_WIDTH
,
391 wxAlignment align
= wxALIGN_CENTER
,
392 int flags
= wxDATAVIEW_COL_RESIZABLE
);
393 bool AppendDateColumn( const wxBitmap
&label
, unsigned int model_column
,
394 wxDataViewCellMode mode
= wxDATAVIEW_CELL_ACTIVATABLE
, int width
= -1,
395 wxAlignment align
= wxALIGN_CENTER
,
396 int flags
= wxDATAVIEW_COL_RESIZABLE
);
397 bool AppendBitmapColumn( const wxBitmap
&label
, unsigned int model_column
,
398 wxDataViewCellMode mode
= wxDATAVIEW_CELL_INERT
, int width
= -1,
399 wxAlignment align
= wxALIGN_CENTER
,
400 int flags
= wxDATAVIEW_COL_RESIZABLE
);
402 virtual bool AppendColumn( wxDataViewColumn
*col
);
404 virtual unsigned int GetColumnCount() const;
406 virtual bool DeleteColumn( unsigned int pos
);
407 virtual bool ClearColumns();
408 virtual wxDataViewColumn
* GetColumn( unsigned int pos
);
410 void SetExpanderColumn( unsigned int col
)
411 { m_expander_column
= col
; DoSetExpanderColumn(); }
412 unsigned int GetExpanderColumn() const
413 { return m_expander_column
; }
415 void SetIndent( int indent
)
416 { m_indent
= indent
; DoSetIndent(); }
417 int GetIndent() const
420 // TODO selection code
423 virtual void DoSetExpanderColumn() = 0 ;
424 virtual void DoSetIndent() = 0;
427 wxDataViewModel
*m_model
;
429 wxDataViewEventModelNotifier
*m_eventNotifier
;
430 unsigned int m_expander_column
;
434 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewCtrlBase
)
437 // ----------------------------------------------------------------------------
438 // wxDataViewEvent - the event class for the wxDataViewCtrl notifications
439 // ----------------------------------------------------------------------------
441 class WXDLLIMPEXP_ADV wxDataViewEvent
: public wxNotifyEvent
444 wxDataViewEvent(wxEventType commandType
= wxEVT_NULL
, int winid
= 0)
445 : wxNotifyEvent(commandType
, winid
),
449 m_value(wxNullVariant
),
453 wxDataViewEvent(const wxDataViewEvent
& event
)
454 : wxNotifyEvent(event
),
455 m_item(event
.m_item
),
457 m_model(event
.m_model
),
458 m_value(event
.m_value
),
459 m_column(event
.m_column
)
462 wxDataViewItem
GetItem() const { return m_item
; }
463 void SetItem( const wxDataViewItem
&item
) { m_item
= item
; }
465 int GetColumn() const { return m_col
; }
466 void SetColumn( int col
) { m_col
= col
; }
468 wxDataViewModel
* GetModel() const { return m_model
; }
469 void SetModel( wxDataViewModel
*model
) { m_model
= model
; }
471 const wxVariant
&GetValue() const { return m_value
; }
472 void SetValue( const wxVariant
&value
) { m_value
= value
; }
474 // for wxEVT_DATAVIEW_COLUMN_HEADER_CLICKED only
475 void SetDataViewColumn( wxDataViewColumn
*col
) { m_column
= col
; }
476 wxDataViewColumn
*GetDataViewColumn() const { return m_column
; }
478 virtual wxEvent
*Clone() const { return new wxDataViewEvent(*this); }
481 wxDataViewItem m_item
;
483 wxDataViewModel
*m_model
;
485 wxDataViewColumn
*m_column
;
488 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxDataViewEvent
)
491 BEGIN_DECLARE_EVENT_TYPES()
492 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_ADV
, wxEVT_COMMAND_DATAVIEW_ITEM_SELECTED
, -1)
493 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_ADV
, wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, -1)
494 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_ADV
, wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
, -1)
495 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_ADV
, wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
, -1)
496 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_ADV
, wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
, -1)
497 // notifications from the model to the control
498 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_ADV
, wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_ADDED
, -1)
499 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_ADV
, wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_DELETED
, -1)
500 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_ADV
, wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_CHANGED
, -1)
501 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_ADV
, wxEVT_COMMAND_DATAVIEW_MODEL_VALUE_CHANGED
, -1)
502 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_ADV
, wxEVT_COMMAND_DATAVIEW_MODEL_CLEARED
, -1)
503 END_DECLARE_EVENT_TYPES()
505 typedef void (wxEvtHandler::*wxDataViewEventFunction
)(wxDataViewEvent
&);
507 #define wxDataViewEventHandler(func) \
508 (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxDataViewEventFunction, &func)
510 #define wx__DECLARE_DATAVIEWEVT(evt, id, fn) \
511 wx__DECLARE_EVT1(wxEVT_COMMAND_DATAVIEW_ ## evt, id, wxDataViewEventHandler(fn))
513 #define EVT_DATAVIEW_ITEM_SELECTED(id, fn) wx__DECLARE_DATAVIEWEVT(ITEM_SELECTED, id, fn)
514 #define EVT_DATAVIEW_ITEM_ACTIVATED(id, fn) wx__DECLARE_DATAVIEWEVT(ITEM_ACTIVATED, id, fn)
515 #define EVT_DATAVIEW_COLUMN_HEADER_CLICK(id, fn) wx__DECLARE_DATAVIEWEVT(COLUMN_HEADER_CLICK, id, fn)
516 #define EVT_DATAVIEW_COLUMN_HEADER_RIGHT_CLICKED(id, fn) wx__DECLARE_DATAVIEWEVT(COLUMN_HEADER_RIGHT_CLICK, id, fn)
517 #define EVT_DATAVIEW_COLUMN_SORTED(id, fn) wx__DECLARE_DATAVIEWEVT(COLUMN_SORTED, id, fn)
519 #define EVT_DATAVIEW_MODEL_ITEM_ADDED(id, fn) wx__DECLARE_DATAVIEWEVT(MODEL_ITEM_APPENDED, id, fn)
520 #define EVT_DATAVIEW_MODEL_ITEM_DELETED(id, fn) wx__DECLARE_DATAVIEWEVT(MODEL_ITEM_DELETED, id, fn)
521 #define EVT_DATAVIEW_MODEL_ITEM_CHANGED(id, fn) wx__DECLARE_DATAVIEWEVT(MODEL_ITEM_CHANGED, id, fn)
522 #define EVT_DATAVIEW_MODEL_VALUE_CHANGED(id, fn) wx__DECLARE_DATAVIEWEVT(MODEL_VALUE_CHANGED, id, fn)
523 #define EVT_DATAVIEW_MODEL_CLEARED(id, fn) wx__DECLARE_DATAVIEWEVT(MODEL_CLEARED, id, fn)
526 #if defined(wxUSE_GENERICDATAVIEWCTRL)
527 #include "wx/generic/dataview.h"
528 #elif defined(__WXGTK20__)
529 #include "wx/gtk/dataview.h"
530 #elif defined(__WXMAC__)
531 #include "wx/mac/dataview.h"
533 #include "wx/generic/dataview.h"
536 #endif // wxUSE_DATAVIEWCTRL
539 // _WX_DATAVIEW_H_BASE_