Implement rest of MSW's wxDataViewCtrl's native column reordering
[wxWidgets.git] / include / wx / generic / dataview.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/generic/dataview.h
3 // Purpose: wxDataViewCtrl generic implementation header
4 // Author: Robert Roebling
5 // Modified By: Bo Yang
6 // Id: $Id$
7 // Copyright: (c) 1998 Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #ifndef __GENERICDATAVIEWCTRLH__
12 #define __GENERICDATAVIEWCTRLH__
13
14 #include "wx/defs.h"
15 #include "wx/object.h"
16 #include "wx/list.h"
17 #include "wx/control.h"
18 #include "wx/scrolwin.h"
19 #include "wx/icon.h"
20
21 // ---------------------------------------------------------
22 // classes
23 // ---------------------------------------------------------
24
25 class WXDLLIMPEXP_FWD_ADV wxDataViewCtrl;
26 class WXDLLIMPEXP_FWD_ADV wxDataViewMainWindow;
27 class WXDLLIMPEXP_FWD_ADV wxDataViewHeaderWindow;
28
29 // ---------------------------------------------------------
30 // wxDataViewRenderer
31 // ---------------------------------------------------------
32
33 class WXDLLIMPEXP_ADV wxDataViewRenderer: public wxDataViewRendererBase
34 {
35 public:
36 wxDataViewRenderer( const wxString &varianttype,
37 wxDataViewCellMode mode = wxDATAVIEW_CELL_INERT,
38 int align = wxDVR_DEFAULT_ALIGNMENT );
39 virtual ~wxDataViewRenderer();
40
41 virtual bool Render( wxRect cell, wxDC *dc, int state ) = 0;
42 virtual wxSize GetSize() const = 0;
43
44 virtual void SetAlignment( int align )
45 { m_align=align; }
46 virtual int GetAlignment() const
47 { return m_align; }
48
49 virtual void SetMode( wxDataViewCellMode mode )
50 { m_mode=mode; }
51 virtual wxDataViewCellMode GetMode() const
52 { return m_mode; }
53
54 virtual bool Activate( wxRect WXUNUSED(cell),
55 wxDataViewModel *WXUNUSED(model),
56 const wxDataViewItem & WXUNUSED(item),
57 unsigned int WXUNUSED(col) )
58 { return false; }
59
60 virtual bool LeftClick( wxPoint WXUNUSED(cursor),
61 wxRect WXUNUSED(cell),
62 wxDataViewModel *WXUNUSED(model),
63 const wxDataViewItem & WXUNUSED(item),
64 unsigned int WXUNUSED(col) )
65 { return false; }
66 virtual bool RightClick( wxPoint WXUNUSED(cursor),
67 wxRect WXUNUSED(cell),
68 wxDataViewModel *WXUNUSED(model),
69 const wxDataViewItem & WXUNUSED(item),
70 unsigned int WXUNUSED(col) )
71 { return false; }
72 virtual bool StartDrag( wxPoint WXUNUSED(cursor),
73 wxRect WXUNUSED(cell),
74 wxDataViewModel *WXUNUSED(model),
75 const wxDataViewItem & WXUNUSED(item),
76 unsigned int WXUNUSED(col) )
77 { return false; }
78
79 // Create DC on request
80 virtual wxDC *GetDC();
81
82 void SetHasAttr( bool set ) { m_hasAttr = set; }
83 void SetAttr( const wxDataViewItemAttr &attr ) { m_attr = attr; }
84 bool GetWantsAttr() { return m_wantsAttr; }
85
86 private:
87 wxDC *m_dc;
88 int m_align;
89 wxDataViewCellMode m_mode;
90
91 protected:
92 bool m_wantsAttr;
93 bool m_hasAttr;
94 wxDataViewItemAttr m_attr;
95
96 protected:
97 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewRenderer)
98 };
99
100 // ---------------------------------------------------------
101 // wxDataViewCustomRenderer
102 // ---------------------------------------------------------
103
104 class WXDLLIMPEXP_ADV wxDataViewCustomRenderer: public wxDataViewRenderer
105 {
106 public:
107 wxDataViewCustomRenderer( const wxString &varianttype = wxT("string"),
108 wxDataViewCellMode mode = wxDATAVIEW_CELL_INERT,
109 int align = wxDVR_DEFAULT_ALIGNMENT );
110
111 void RenderText( const wxString &text, int xoffset, wxRect cell, wxDC *dc, int state );
112
113 protected:
114 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewCustomRenderer)
115 };
116
117
118 // ---------------------------------------------------------
119 // wxDataViewTextRenderer
120 // ---------------------------------------------------------
121
122 class WXDLLIMPEXP_ADV wxDataViewTextRenderer: public wxDataViewCustomRenderer
123 {
124 public:
125 wxDataViewTextRenderer( const wxString &varianttype = wxT("string"),
126 wxDataViewCellMode mode = wxDATAVIEW_CELL_INERT,
127 int align = wxDVR_DEFAULT_ALIGNMENT );
128
129 bool SetValue( const wxVariant &value );
130 bool GetValue( wxVariant &value ) const;
131
132 bool Render( wxRect cell, wxDC *dc, int state );
133 wxSize GetSize() const;
134
135 // in-place editing
136 virtual bool HasEditorCtrl();
137 virtual wxControl* CreateEditorCtrl( wxWindow *parent, wxRect labelRect, const wxVariant &value );
138 virtual bool GetValueFromEditorCtrl( wxControl* editor, wxVariant &value );
139
140 protected:
141 wxString m_text;
142
143 protected:
144 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewTextRenderer)
145 };
146
147 // ---------------------------------------------------------
148 // wxDataViewTextRendererAttr
149 // ---------------------------------------------------------
150
151 class WXDLLIMPEXP_ADV wxDataViewTextRendererAttr: public wxDataViewTextRenderer
152 {
153 public:
154 wxDataViewTextRendererAttr( const wxString &varianttype = wxT("string"),
155 wxDataViewCellMode mode = wxDATAVIEW_CELL_INERT,
156 int align = wxDVR_DEFAULT_ALIGNMENT );
157
158 bool Render( wxRect cell, wxDC *dc, int state );
159
160 protected:
161 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewTextRendererAttr)
162 };
163
164 // ---------------------------------------------------------
165 // wxDataViewBitmapRenderer
166 // ---------------------------------------------------------
167
168 class WXDLLIMPEXP_ADV wxDataViewBitmapRenderer: public wxDataViewCustomRenderer
169 {
170 public:
171 wxDataViewBitmapRenderer( const wxString &varianttype = wxT("wxBitmap"),
172 wxDataViewCellMode mode = wxDATAVIEW_CELL_INERT,
173 int align = wxDVR_DEFAULT_ALIGNMENT );
174
175 bool SetValue( const wxVariant &value );
176 bool GetValue( wxVariant &value ) const;
177
178 bool Render( wxRect cell, wxDC *dc, int state );
179 wxSize GetSize() const;
180
181 private:
182 wxIcon m_icon;
183 wxBitmap m_bitmap;
184
185 protected:
186 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewBitmapRenderer)
187 };
188
189 // ---------------------------------------------------------
190 // wxDataViewToggleRenderer
191 // ---------------------------------------------------------
192
193 class WXDLLIMPEXP_ADV wxDataViewToggleRenderer: public wxDataViewCustomRenderer
194 {
195 public:
196 wxDataViewToggleRenderer( const wxString &varianttype = wxT("bool"),
197 wxDataViewCellMode mode = wxDATAVIEW_CELL_INERT,
198 int align = wxDVR_DEFAULT_ALIGNMENT );
199
200 bool SetValue( const wxVariant &value );
201 bool GetValue( wxVariant &value ) const;
202
203 bool Render( wxRect cell, wxDC *dc, int state );
204 bool Activate( wxRect cell, wxDataViewModel *model, const wxDataViewItem & item,
205 unsigned int col );
206 wxSize GetSize() const;
207
208 private:
209 bool m_toggle;
210
211 protected:
212 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewToggleRenderer)
213 };
214
215 // ---------------------------------------------------------
216 // wxDataViewProgressRenderer
217 // ---------------------------------------------------------
218
219 class WXDLLIMPEXP_ADV wxDataViewProgressRenderer: public wxDataViewCustomRenderer
220 {
221 public:
222 wxDataViewProgressRenderer( const wxString &label = wxEmptyString,
223 const wxString &varianttype = wxT("long"),
224 wxDataViewCellMode mode = wxDATAVIEW_CELL_INERT,
225 int align = wxDVR_DEFAULT_ALIGNMENT );
226 virtual ~wxDataViewProgressRenderer();
227
228 bool SetValue( const wxVariant &value );
229 bool GetValue( wxVariant& value ) const;
230
231 virtual bool Render( wxRect cell, wxDC *dc, int state );
232 virtual wxSize GetSize() const;
233
234 private:
235 wxString m_label;
236 int m_value;
237
238 protected:
239 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewProgressRenderer)
240 };
241
242 // ---------------------------------------------------------
243 // wxDataViewIconTextRenderer
244 // ---------------------------------------------------------
245
246 class wxDataViewIconTextRenderer: public wxDataViewCustomRenderer
247 {
248 public:
249 wxDataViewIconTextRenderer( const wxString &varianttype = wxT("wxDataViewIconText"),
250 wxDataViewCellMode mode = wxDATAVIEW_CELL_INERT,
251 int align = wxDVR_DEFAULT_ALIGNMENT );
252 virtual ~wxDataViewIconTextRenderer();
253
254 bool SetValue( const wxVariant &value );
255 bool GetValue( wxVariant &value ) const;
256
257 virtual bool Render( wxRect cell, wxDC *dc, int state );
258 virtual wxSize GetSize() const;
259
260 virtual bool HasEditorCtrl() { return true; }
261 virtual wxControl* CreateEditorCtrl( wxWindow *parent, wxRect labelRect, const wxVariant &value );
262 virtual bool GetValueFromEditorCtrl( wxControl* editor, wxVariant &value );
263
264 private:
265 wxDataViewIconText m_value;
266
267 protected:
268 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewIconTextRenderer)
269 };
270
271 // ---------------------------------------------------------
272 // wxDataViewDateRenderer
273 // ---------------------------------------------------------
274
275 class WXDLLIMPEXP_ADV wxDataViewDateRenderer: public wxDataViewCustomRenderer
276 {
277 public:
278 wxDataViewDateRenderer( const wxString &varianttype = wxT("datetime"),
279 wxDataViewCellMode mode = wxDATAVIEW_CELL_ACTIVATABLE,
280 int align = wxDVR_DEFAULT_ALIGNMENT );
281
282 bool SetValue( const wxVariant &value );
283 bool GetValue( wxVariant& value ) const;
284
285 virtual bool Render( wxRect cell, wxDC *dc, int state );
286 virtual wxSize GetSize() const;
287 virtual bool Activate( wxRect cell,
288 wxDataViewModel *model, const wxDataViewItem & item, unsigned int col );
289
290 private:
291 wxDateTime m_date;
292
293 protected:
294 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewDateRenderer)
295 };
296
297 // ---------------------------------------------------------
298 // wxDataViewColumn
299 // ---------------------------------------------------------
300
301 class WXDLLIMPEXP_ADV wxDataViewColumn: public wxDataViewColumnBase
302 {
303 friend class wxDataViewHeaderWindowBase;
304 friend class wxDataViewHeaderWindow;
305 friend class wxDataViewHeaderWindowMSW;
306
307 public:
308 wxDataViewColumn( const wxString &title, wxDataViewRenderer *renderer,
309 unsigned int model_column, int width = wxDVC_DEFAULT_WIDTH,
310 wxAlignment align = wxALIGN_CENTER,
311 int flags = wxDATAVIEW_COL_RESIZABLE );
312 wxDataViewColumn( const wxBitmap &bitmap, wxDataViewRenderer *renderer,
313 unsigned int model_column, int width = wxDVC_DEFAULT_WIDTH,
314 wxAlignment align = wxALIGN_CENTER,
315 int flags = wxDATAVIEW_COL_RESIZABLE );
316 virtual ~wxDataViewColumn();
317
318 // setters:
319
320 virtual void SetTitle( const wxString &title )
321 { m_title=title; }
322 virtual void SetAlignment( wxAlignment align )
323 { m_align=align; }
324 virtual void SetMinWidth( int minWidth )
325 { m_minWidth=minWidth; }
326 virtual void SetWidth( int width );
327 virtual void SetSortable( bool sortable );
328 virtual void SetResizeable( bool resizeable );
329 virtual void SetHidden( bool hidden );
330 virtual void SetSortOrder( bool ascending );
331 virtual void SetReorderable( bool reorderable );
332
333 // getters:
334
335 virtual wxString GetTitle() const
336 { return m_title; }
337 virtual wxAlignment GetAlignment() const
338 { return m_align; }
339 virtual int GetWidth() const
340 { return m_width; }
341 virtual int GetMinWidth() const
342 { return m_minWidth; }
343 virtual bool IsSortable() const
344 { return (m_flags & wxDATAVIEW_COL_SORTABLE) != 0; }
345 virtual bool IsResizeable() const
346 { return (m_flags & wxDATAVIEW_COL_RESIZABLE) != 0; }
347 virtual bool IsHidden() const
348 { return (m_flags & wxDATAVIEW_COL_HIDDEN) != 0; }
349 virtual bool IsSortOrderAscending() const;
350 virtual bool IsReorderable() const
351 { return (m_flags & wxDATAVIEW_COL_REORDERABLE) != 0; }
352
353 private:
354 int m_width;
355 int m_minWidth;
356 int m_flags;
357 wxAlignment m_align;
358 wxString m_title;
359 bool m_ascending;
360
361 void Init(int width);
362
363 // like SetWidth() but does not ask the header window of the
364 // wxDataViewCtrl to reflect the width-change.
365 void SetInternalWidth(int width);
366
367 protected:
368 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewColumn)
369 };
370
371 // ---------------------------------------------------------
372 // wxDataViewCtrl
373 // ---------------------------------------------------------
374
375 WX_DECLARE_LIST_WITH_DECL(wxDataViewColumn, wxDataViewColumnList,
376 class WXDLLIMPEXP_ADV);
377
378 class WXDLLIMPEXP_ADV wxDataViewCtrl: public wxDataViewCtrlBase,
379 public wxScrollHelperNative
380 {
381 friend class wxDataViewMainWindow;
382 friend class wxDataViewHeaderWindowBase;
383 friend class wxDataViewHeaderWindow;
384 friend class wxDataViewHeaderWindowMSW;
385 friend class wxDataViewColumn;
386
387 public:
388 wxDataViewCtrl() : wxScrollHelperNative(this)
389 {
390 //No sorting column at start, I think
391 m_sortingColumn = NULL;
392 Init();
393 }
394
395 wxDataViewCtrl( wxWindow *parent, wxWindowID id,
396 const wxPoint& pos = wxDefaultPosition,
397 const wxSize& size = wxDefaultSize, long style = 0,
398 const wxValidator& validator = wxDefaultValidator )
399 : wxScrollHelperNative(this)
400 {
401 m_sortingColumn = NULL;
402 Create(parent, id, pos, size, style, validator );
403 }
404
405 virtual ~wxDataViewCtrl();
406
407 void Init();
408
409 bool Create(wxWindow *parent, wxWindowID id,
410 const wxPoint& pos = wxDefaultPosition,
411 const wxSize& size = wxDefaultSize, long style = 0,
412 const wxValidator& validator = wxDefaultValidator );
413
414 virtual bool AssociateModel( wxDataViewModel *model );
415 virtual bool AppendColumn( wxDataViewColumn *col );
416 virtual bool PrependColumn( wxDataViewColumn *col );
417
418 virtual void DoSetExpanderColumn();
419 virtual void DoSetIndent();
420
421 virtual unsigned int GetColumnCount() const;
422 virtual wxDataViewColumn* GetColumn( unsigned int pos ) const;
423 virtual bool DeleteColumn( wxDataViewColumn *column );
424 virtual bool ClearColumns();
425 virtual int GetColumnPosition( const wxDataViewColumn *column ) const;
426
427 virtual wxDataViewColumn *GetSortingColumn() const;
428
429 virtual wxDataViewItem GetSelection() const;
430 virtual int GetSelections( wxDataViewItemArray & sel ) const;
431 virtual void SetSelections( const wxDataViewItemArray & sel );
432 virtual void Select( const wxDataViewItem & item );
433 virtual void Unselect( const wxDataViewItem & item );
434 virtual bool IsSelected( const wxDataViewItem & item ) const;
435
436 virtual void SelectAll();
437 virtual void UnselectAll();
438
439 virtual void EnsureVisible( const wxDataViewItem & item,
440 const wxDataViewColumn *column = NULL );
441 virtual void HitTest( const wxPoint & point, wxDataViewItem & item, wxDataViewColumn* &column ) const;
442 virtual wxRect GetItemRect( const wxDataViewItem & item, const wxDataViewColumn *column = NULL ) const;
443
444 virtual void Expand( const wxDataViewItem & item );
445 virtual void Collapse( const wxDataViewItem & item );
446
447 protected:
448 virtual int GetSelections( wxArrayInt & sel ) const;
449 virtual void SetSelections( const wxArrayInt & sel );
450 virtual void Select( int row );
451 virtual void Unselect( int row );
452 virtual bool IsSelected( int row ) const;
453 virtual void SelectRange( int from, int to );
454 virtual void UnselectRange( int from, int to );
455
456 virtual void EnsureVisible( int row, int column );
457
458 virtual wxDataViewItem GetItemByRow( unsigned int row ) const;
459 virtual int GetRowByItem( const wxDataViewItem & item ) const;
460
461 wxDataViewColumn* GetSortingColumn() { return m_sortingColumn; }
462 void SetSortingColumn( wxDataViewColumn* column ) { m_sortingColumn = column; }
463
464 public: // utility functions not part of the API
465
466 // returns the "best" width for the idx-th column
467 unsigned int GetBestColumnWidth(int WXUNUSED(idx)) const
468 {
469 return GetClientSize().GetWidth() / GetColumnCount();
470 }
471
472 // called by header window after reorder
473 void ColumnMoved( wxDataViewColumn* col, unsigned int new_pos );
474
475 // updates the header window after a change in a column setting
476 void OnColumnChange();
477
478 wxWindow *GetMainWindow() { return (wxWindow*) m_clientArea; }
479
480 private:
481 wxDataViewColumnList m_cols;
482 wxDataViewModelNotifier *m_notifier;
483 wxDataViewMainWindow *m_clientArea;
484 wxDataViewHeaderWindow *m_headerArea;
485 wxDataViewColumn* m_sortingColumn;
486
487 private:
488 void OnSize( wxSizeEvent &event );
489
490 // we need to return a special WM_GETDLGCODE value to process just the
491 // arrows but let the other navigation characters through
492 #ifdef __WXMSW__
493 virtual WXLRESULT MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam);
494 #endif // __WXMSW__
495
496 WX_FORWARD_TO_SCROLL_HELPER()
497
498 private:
499 DECLARE_DYNAMIC_CLASS(wxDataViewCtrl)
500 DECLARE_NO_COPY_CLASS(wxDataViewCtrl)
501 DECLARE_EVENT_TABLE()
502 };
503
504
505 #endif // __GENERICDATAVIEWCTRLH__