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