]> git.saurik.com Git - wxWidgets.git/blob - include/wx/dataview.h
Most of FM's wxDataViewCtrl patch.
[wxWidgets.git] / include / wx / dataview.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/dataview.h
3 // Purpose: wxDataViewCtrl base classes
4 // Author: Robert Roebling
5 // Modified by:
6 // Created: 08.01.06
7 // RCS-ID: $Id$
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_DATAVIEW_H_BASE_
13 #define _WX_DATAVIEW_H_BASE_
14
15 #include "wx/defs.h"
16
17 #if wxUSE_DATAVIEWCTRL
18
19 #include "wx/control.h"
20 #include "wx/textctrl.h"
21 #include "wx/bitmap.h"
22 #include "wx/variant.h"
23
24
25 #if defined(__WXGTK20__)
26 // for testing
27 // #define wxUSE_GENERICDATAVIEWCTRL 1
28 #elif defined(__WXMAC__)
29 #define wxUSE_GENERICDATAVIEWCTRL 1
30 #else
31 #define wxUSE_GENERICDATAVIEWCTRL 1
32 #endif
33
34 // ----------------------------------------------------------------------------
35 // wxDataViewCtrl flags
36 // ----------------------------------------------------------------------------
37
38 // ----------------------------------------------------------------------------
39 // wxDataViewCtrl globals
40 // ----------------------------------------------------------------------------
41
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;
47
48 extern WXDLLIMPEXP_DATA_ADV(const wxChar) wxDataViewCtrlNameStr[];
49
50 // the default width of new (text) columns:
51 #define wxDVC_DEFAULT_WIDTH 80
52
53 // the default width of new toggle columns:
54 #define wxDVC_TOGGLE_DEFAULT_WIDTH 30
55
56
57 // ---------------------------------------------------------
58 // wxDataViewModel
59 // ---------------------------------------------------------
60
61 class WXDLLIMPEXP_ADV wxDataViewModel: public wxObjectRefData
62 {
63 public:
64 wxDataViewModel() { }
65
66 protected:
67 // the user should not delete this class directly: he should use DecRef() instead!
68 virtual ~wxDataViewModel() { }
69 };
70
71 // ---------------------------------------------------------
72 // wxDataViewListModelNotifier
73 // ---------------------------------------------------------
74
75 class WXDLLIMPEXP_ADV wxDataViewListModelNotifier: public wxObject
76 {
77 public:
78 wxDataViewListModelNotifier() { }
79 virtual ~wxDataViewListModelNotifier() { }
80
81 virtual bool RowAppended() = 0;
82 virtual bool RowPrepended() = 0;
83 virtual bool RowInserted( unsigned int before ) = 0;
84 virtual bool RowDeleted( unsigned int row ) = 0;
85 virtual bool RowChanged( unsigned int row ) = 0;
86 virtual bool ValueChanged( unsigned int col, unsigned int row ) = 0;
87 virtual bool RowsReordered( unsigned int *new_order ) = 0;
88 virtual bool Cleared() = 0;
89 virtual bool Freed()
90 { m_owner = NULL; return true; }
91
92 void SetOwner( wxDataViewListModel *owner ) { m_owner = owner; }
93 wxDataViewListModel *GetOwner() { return m_owner; }
94
95 private:
96 wxDataViewListModel *m_owner;
97 };
98
99 // ---------------------------------------------------------
100 // wxDataViewListModel
101 // ---------------------------------------------------------
102
103 class WXDLLIMPEXP_ADV wxDataViewViewingColumn: public wxObject
104 {
105 public:
106 wxDataViewViewingColumn( wxDataViewColumn *view_column, unsigned int model_column )
107 {
108 m_viewColumn = view_column;
109 m_modelColumn = model_column;
110 }
111
112 wxDataViewColumn *m_viewColumn;
113 unsigned int m_modelColumn;
114 };
115
116 class WXDLLIMPEXP_ADV wxDataViewListModel: public wxDataViewModel
117 {
118 friend class WXDLLIMPEXP_ADV wxDataViewCtrl;
119 friend class WXDLLIMPEXP_ADV wxDataViewCtrlBase;
120 friend class WXDLLIMPEXP_ADV wxDataViewSortedListModel;
121 friend class WXDLLIMPEXP_ADV wxDataViewColumnBase;
122 friend class WXDLLIMPEXP_ADV wxGtkDataViewListModelNotifier;
123
124 public:
125 wxDataViewListModel();
126
127 virtual unsigned int GetNumberOfRows() = 0;
128 virtual unsigned int GetNumberOfCols() = 0;
129
130 // return type as reported by wxVariant
131 virtual wxString GetColType( unsigned int col ) = 0;
132
133 // get value into a wxVariant
134 virtual void GetValue( wxVariant &variant, unsigned int col, unsigned int row ) = 0;
135
136 // set value, call ValueChanged() afterwards!
137 virtual bool SetValue( wxVariant &variant, unsigned int col, unsigned int row ) = 0;
138
139 // delegated notifiers
140 virtual bool RowAppended();
141 virtual bool RowPrepended();
142 virtual bool RowInserted( unsigned int before );
143 virtual bool RowDeleted( unsigned int row );
144 virtual bool RowChanged( unsigned int row );
145 virtual bool ValueChanged( unsigned int col, unsigned int row );
146 virtual bool RowsReordered( unsigned int *new_order );
147 virtual bool Cleared();
148
149 protected:
150 // the user should not delete this class directly: he should use DecRef() instead!
151 virtual ~wxDataViewListModel();
152
153 // Used internally
154 void AddViewingColumn( wxDataViewColumn *view_column, unsigned int model_column );
155 void RemoveViewingColumn( wxDataViewColumn *column );
156
157 void AddNotifier( wxDataViewListModelNotifier *notifier );
158 void RemoveNotifier( wxDataViewListModelNotifier *notifier );
159
160 wxList m_notifiers;
161 wxList m_viewingColumns;
162 };
163
164
165
166 // ---------------------------------------------------------
167 // wxDataViewSortedListModel
168 // ---------------------------------------------------------
169
170 typedef int (wxCALLBACK *wxDataViewListModelCompare)
171 (unsigned int row1, unsigned int row2, unsigned int col, wxDataViewListModel* model );
172
173 WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSortedIndexArray, WXDLLIMPEXP_ADV);
174
175 class WXDLLIMPEXP_ADV wxDataViewSortedListModel: public wxDataViewListModel
176 {
177 friend class wxDataViewSortedListModelNotifier;
178
179 public:
180 wxDataViewSortedListModel( wxDataViewListModel *child );
181 virtual ~wxDataViewSortedListModel();
182
183 void SetAscending( bool ascending ) { m_ascending = ascending; }
184 bool GetAscending() { return m_ascending; }
185
186 virtual unsigned int GetNumberOfRows();
187 virtual unsigned int GetNumberOfCols();
188 // return type as reported by wxVariant
189 virtual wxString GetColType( unsigned int col );
190 // get value into a wxVariant
191 virtual void GetValue( wxVariant &variant, unsigned int col, unsigned int row );
192 // set value, call ValueChanged() afterwards!
193 virtual bool SetValue( wxVariant &variant, unsigned int col, unsigned int row );
194
195 // called from user
196 virtual bool RowAppended();
197 virtual bool RowPrepended();
198 virtual bool RowInserted( unsigned int before );
199 virtual bool RowDeleted( unsigned int row );
200 virtual bool RowChanged( unsigned int row );
201 virtual bool ValueChanged( unsigned int col, unsigned int row );
202 virtual bool RowsReordered( unsigned int *new_order );
203 virtual bool Cleared();
204
205 // called if child's notifiers are called
206 bool ChildRowAppended();
207 bool ChildRowPrepended();
208 bool ChildRowInserted( unsigned int before );
209 bool ChildRowDeleted( unsigned int row );
210 bool ChildRowChanged( unsigned int row );
211 bool ChildValueChanged( unsigned int col, unsigned int row );
212 bool ChildRowsReordered( unsigned int *new_order );
213 bool ChildCleared();
214
215 virtual void Resort();
216
217 private:
218 bool m_ascending;
219 wxDataViewListModel *m_child;
220 wxDataViewSortedIndexArray m_array;
221 wxDataViewListModelNotifier *m_notifierOnChild;
222
223 void InitStatics(); // BAD
224 };
225
226 // ---------------------------------------------------------
227 // wxDataViewRendererBase
228 // ---------------------------------------------------------
229
230 enum wxDataViewCellMode
231 {
232 wxDATAVIEW_CELL_INERT,
233 wxDATAVIEW_CELL_ACTIVATABLE,
234 wxDATAVIEW_CELL_EDITABLE
235 };
236
237 enum wxDataViewCellRenderState
238 {
239 wxDATAVIEW_CELL_SELECTED = 1,
240 wxDATAVIEW_CELL_PRELIT = 2,
241 wxDATAVIEW_CELL_INSENSITIVE = 4,
242 wxDATAVIEW_CELL_FOCUSED = 8
243 };
244
245 class WXDLLIMPEXP_ADV wxDataViewRendererBase: public wxObject
246 {
247 public:
248 wxDataViewRendererBase( const wxString &varianttype, wxDataViewCellMode mode = wxDATAVIEW_CELL_INERT );
249
250 virtual bool SetValue( const wxVariant& WXUNUSED(value) ) { return true; }
251 virtual bool GetValue( wxVariant& WXUNUSED(value) ) { return true; }
252 virtual bool Validate( wxVariant& WXUNUSED(value) ) { return true; }
253
254 wxString GetVariantType() { return m_variantType; }
255 wxDataViewCellMode GetMode() { return m_mode; }
256
257 void SetOwner( wxDataViewColumn *owner ) { m_owner = owner; }
258 wxDataViewColumn* GetOwner() { return m_owner; }
259
260 protected:
261 wxDataViewCellMode m_mode;
262 wxString m_variantType;
263 wxDataViewColumn *m_owner;
264
265 protected:
266 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewRendererBase)
267 };
268
269 // ---------------------------------------------------------
270 // wxDataViewColumnBase
271 // ---------------------------------------------------------
272
273 enum wxDataViewColumnFlags
274 {
275 wxDATAVIEW_COL_RESIZABLE = 1,
276 wxDATAVIEW_COL_SORTABLE = 2,
277 wxDATAVIEW_COL_HIDDEN = 4
278 };
279
280 class WXDLLIMPEXP_ADV wxDataViewColumnBase: public wxObject
281 {
282 public:
283 wxDataViewColumnBase( const wxString &title, wxDataViewRenderer *renderer,
284 unsigned int model_column, int width = wxDVC_DEFAULT_WIDTH,
285 wxAlignment align = wxALIGN_CENTER,
286 int flags = wxDATAVIEW_COL_RESIZABLE );
287 wxDataViewColumnBase( const wxBitmap &bitmap, wxDataViewRenderer *renderer,
288 unsigned int model_column, int width = wxDVC_DEFAULT_WIDTH,
289 wxAlignment align = wxALIGN_CENTER,
290 int flags = wxDATAVIEW_COL_RESIZABLE );
291 virtual ~wxDataViewColumnBase();
292
293 virtual void SetTitle( const wxString &title );
294 virtual wxString GetTitle();
295
296 virtual void SetBitmap( const wxBitmap &bitmap );
297 virtual const wxBitmap &GetBitmap();
298
299 virtual void SetAlignment( wxAlignment align ) = 0;
300
301 virtual void SetSortable( bool sortable ) = 0;
302 virtual bool IsSortable() const
303 { return (m_flags & wxDATAVIEW_COL_SORTABLE) != 0; }
304 virtual bool IsResizeable() const
305 { return (m_flags & wxDATAVIEW_COL_RESIZABLE) != 0; }
306 virtual bool IsHidden() const
307 { return (m_flags & wxDATAVIEW_COL_HIDDEN) != 0; }
308
309 virtual void SetSortOrder( bool ascending ) = 0;
310 virtual bool IsSortOrderAscending() const = 0;
311
312 wxDataViewRenderer* GetRenderer() { return m_renderer; }
313
314 unsigned int GetModelColumn() { return m_model_column; }
315
316 virtual void SetOwner( wxDataViewCtrl *owner ) { m_owner = owner; }
317 wxDataViewCtrl *GetOwner() { return m_owner; }
318
319 virtual int GetWidth() const = 0;
320
321 private:
322 wxDataViewCtrl *m_ctrl;
323 wxDataViewRenderer *m_renderer;
324 int m_model_column;
325 int m_flags;
326 wxString m_title;
327 wxBitmap m_bitmap;
328 wxDataViewCtrl *m_owner;
329
330 protected:
331 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewColumnBase)
332 };
333
334 // ---------------------------------------------------------
335 // wxDataViewCtrlBase
336 // ---------------------------------------------------------
337
338 #define wxDV_SINGLE 0x0000 // for convenience
339 #define wxDV_MULTIPLE 0x0020 // can select multiple items
340
341 class WXDLLIMPEXP_ADV wxDataViewCtrlBase: public wxControl
342 {
343 public:
344 wxDataViewCtrlBase();
345 virtual ~wxDataViewCtrlBase();
346
347 virtual bool AssociateModel( wxDataViewListModel *model );
348 wxDataViewListModel* GetModel();
349
350 // short cuts
351 bool AppendTextColumn( const wxString &label, unsigned int model_column,
352 wxDataViewCellMode mode = wxDATAVIEW_CELL_INERT, int width = -1,
353 wxAlignment align = wxALIGN_CENTER,
354 int flags = wxDATAVIEW_COL_RESIZABLE );
355 bool AppendToggleColumn( const wxString &label, unsigned int model_column,
356 wxDataViewCellMode mode = wxDATAVIEW_CELL_INERT, int width = wxDVC_TOGGLE_DEFAULT_WIDTH,
357 wxAlignment align = wxALIGN_CENTER,
358 int flags = wxDATAVIEW_COL_RESIZABLE );
359 bool AppendProgressColumn( const wxString &label, unsigned int model_column,
360 wxDataViewCellMode mode = wxDATAVIEW_CELL_INERT, int width = wxDVC_DEFAULT_WIDTH,
361 wxAlignment align = wxALIGN_CENTER,
362 int flags = wxDATAVIEW_COL_RESIZABLE );
363 bool AppendDateColumn( const wxString &label, unsigned int model_column,
364 wxDataViewCellMode mode = wxDATAVIEW_CELL_ACTIVATABLE, int width = -1,
365 wxAlignment align = wxALIGN_CENTER,
366 int flags = wxDATAVIEW_COL_RESIZABLE );
367 bool AppendBitmapColumn( const wxString &label, unsigned int model_column,
368 wxDataViewCellMode mode = wxDATAVIEW_CELL_INERT, int width = -1,
369 wxAlignment align = wxALIGN_CENTER,
370 int flags = wxDATAVIEW_COL_RESIZABLE );
371 bool AppendTextColumn( const wxBitmap &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 AppendToggleColumn( const wxBitmap &label, unsigned int model_column,
376 wxDataViewCellMode mode = wxDATAVIEW_CELL_INERT, int width = wxDVC_TOGGLE_DEFAULT_WIDTH,
377 wxAlignment align = wxALIGN_CENTER,
378 int flags = wxDATAVIEW_COL_RESIZABLE );
379 bool AppendProgressColumn( const wxBitmap &label, unsigned int model_column,
380 wxDataViewCellMode mode = wxDATAVIEW_CELL_INERT, int width = wxDVC_DEFAULT_WIDTH,
381 wxAlignment align = wxALIGN_CENTER,
382 int flags = wxDATAVIEW_COL_RESIZABLE );
383 bool AppendDateColumn( const wxBitmap &label, unsigned int model_column,
384 wxDataViewCellMode mode = wxDATAVIEW_CELL_ACTIVATABLE, int width = -1,
385 wxAlignment align = wxALIGN_CENTER,
386 int flags = wxDATAVIEW_COL_RESIZABLE );
387 bool AppendBitmapColumn( const wxBitmap &label, unsigned int model_column,
388 wxDataViewCellMode mode = wxDATAVIEW_CELL_INERT, int width = -1,
389 wxAlignment align = wxALIGN_CENTER,
390 int flags = wxDATAVIEW_COL_RESIZABLE );
391
392 virtual bool AppendColumn( wxDataViewColumn *col );
393 virtual unsigned int GetNumberOfColumns();
394 virtual bool DeleteColumn( unsigned int pos );
395 virtual bool ClearColumns();
396 virtual wxDataViewColumn* GetColumn( unsigned int pos );
397
398 virtual void SetSelection( int row ) = 0; // -1 for unselect
399 inline void ClearSelection() { SetSelection( -1 ); }
400 virtual void Unselect( unsigned int row ) = 0;
401 virtual void SetSelectionRange( unsigned int from, unsigned int to ) = 0;
402 virtual void SetSelections( const wxArrayInt& aSelections) = 0;
403
404 virtual bool IsSelected( unsigned int row ) const = 0;
405 virtual int GetSelection() const = 0;
406 virtual int GetSelections(wxArrayInt& aSelections) const = 0;
407
408 private:
409 wxDataViewListModel *m_model;
410 wxList m_cols;
411
412 protected:
413 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewCtrlBase)
414 };
415
416
417 // ----------------------------------------------------------------------------
418 // wxDataViewEvent - the event class for the wxDataViewCtrl notifications
419 // ----------------------------------------------------------------------------
420
421 class WXDLLIMPEXP_ADV wxDataViewEvent : public wxNotifyEvent
422 {
423 public:
424 wxDataViewEvent(wxEventType commandType = wxEVT_NULL, int winid = 0)
425 : wxNotifyEvent(commandType, winid),
426 m_col(-1),
427 m_row(-1),
428 m_model(NULL),
429 m_value(wxNullVariant),
430 m_editCancelled(false),
431 m_column(NULL)
432 { }
433
434 wxDataViewEvent(const wxDataViewEvent& event)
435 : wxNotifyEvent(event),
436 m_col(event.m_col),
437 m_row(event.m_col),
438 m_model(event.m_model),
439 m_value(event.m_value),
440 m_editCancelled(event.m_editCancelled),
441 m_column(event.m_column)
442 { }
443
444 int GetColumn() const { return m_col; }
445 void SetColumn( int col ) { m_col = col; }
446 int GetRow() const { return m_row; }
447 void SetRow( int row ) { m_row = row; }
448 wxDataViewModel* GetModel() const { return m_model; }
449 void SetModel( wxDataViewModel *model ) { m_model = model; }
450 const wxVariant &GetValue() const { return m_value; }
451 void SetValue( const wxVariant &value ) { m_value = value; }
452
453 // for wxEVT_DATAVIEW_COLUMN_HEADER_CLICKED only
454 void SetDataViewColumn( wxDataViewColumn *col ) { m_column = col; }
455 wxDataViewColumn *GetDataViewColumn() { return m_column; }
456
457 // was label editing canceled? (for wxEVT_COMMAND_DATVIEW_END_LABEL_EDIT only)
458 bool IsEditCancelled() const { return m_editCancelled; }
459 void SetEditCanceled(bool editCancelled) { m_editCancelled = editCancelled; }
460
461 virtual wxEvent *Clone() const { return new wxDataViewEvent(*this); }
462
463 protected:
464 int m_col;
465 int m_row;
466 wxDataViewModel *m_model;
467 wxVariant m_value;
468 bool m_editCancelled;
469 wxDataViewColumn *m_column;
470
471 private:
472 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxDataViewEvent)
473 };
474
475 BEGIN_DECLARE_EVENT_TYPES()
476 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_ADV, wxEVT_COMMAND_DATAVIEW_ROW_SELECTED, -1)
477 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_ADV, wxEVT_COMMAND_DATAVIEW_ROW_ACTIVATED, -1)
478 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_ADV, wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, -1)
479 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_ADV, wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, -1)
480 END_DECLARE_EVENT_TYPES()
481
482 typedef void (wxEvtHandler::*wxDataViewEventFunction)(wxDataViewEvent&);
483
484 #define wxDataViewEventHandler(func) \
485 (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxDataViewEventFunction, &func)
486
487 #define wx__DECLARE_DATAVIEWEVT(evt, id, fn) \
488 wx__DECLARE_EVT1(wxEVT_COMMAND_DATAVIEW_ ## evt, id, wxDataViewEventHandler(fn))
489
490 #define EVT_DATAVIEW_ROW_SELECTED(id, fn) wx__DECLARE_DATAVIEWEVT(ROW_SELECTED, id, fn)
491 #define EVT_DATAVIEW_ROW_ACTIVATED(id, fn) wx__DECLARE_DATAVIEWEVT(ROW_ACTIVATED, id, fn)
492 #define EVT_DATAVIEW_COLUMN_HEADER_CLICK(id, fn) wx__DECLARE_DATAVIEWEVT(COLUMN_HEADER_CLICK, id, fn)
493 #define EVT_DATAVIEW_COLUMN_HEADER_RIGHT_CLICKED(id, fn) wx__DECLARE_DATAVIEWEVT(COLUMN_HEADER_RIGHT_CLICK, id, fn)
494
495
496 #if defined(wxUSE_GENERICDATAVIEWCTRL)
497 #include "wx/generic/dataview.h"
498 #elif defined(__WXGTK20__)
499 #include "wx/gtk/dataview.h"
500 #elif defined(__WXMAC__)
501 // TODO
502 // #include "wx/mac/dataview.h"
503 #else
504 #include "wx/generic/dataview.h"
505 #endif
506
507 #endif // wxUSE_DATAVIEWCTRL
508
509 #endif
510 // _WX_DATAVIEW_H_BASE_