Make all instances of HasEditorCtrl() const, not just a few
[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 virtual int GetAlignment() const;
46
47 virtual void SetMode( wxDataViewCellMode mode )
48 { m_mode=mode; }
49 virtual wxDataViewCellMode GetMode() const
50 { return m_mode; }
51
52 virtual bool Activate( wxRect WXUNUSED(cell),
53 wxDataViewModel *WXUNUSED(model),
54 const wxDataViewItem & WXUNUSED(item),
55 unsigned int WXUNUSED(col) )
56 { return false; }
57
58 virtual bool LeftClick( wxPoint WXUNUSED(cursor),
59 wxRect WXUNUSED(cell),
60 wxDataViewModel *WXUNUSED(model),
61 const wxDataViewItem & WXUNUSED(item),
62 unsigned int WXUNUSED(col) )
63 { return false; }
64 virtual bool RightClick( wxPoint WXUNUSED(cursor),
65 wxRect WXUNUSED(cell),
66 wxDataViewModel *WXUNUSED(model),
67 const wxDataViewItem & WXUNUSED(item),
68 unsigned int WXUNUSED(col) )
69 { return false; }
70 virtual bool StartDrag( wxPoint WXUNUSED(cursor),
71 wxRect WXUNUSED(cell),
72 wxDataViewModel *WXUNUSED(model),
73 const wxDataViewItem & WXUNUSED(item),
74 unsigned int WXUNUSED(col) )
75 { return false; }
76
77 // Create DC on request
78 virtual wxDC *GetDC();
79
80 void SetHasAttr( bool set ) { m_hasAttr = set; }
81 void SetAttr( const wxDataViewItemAttr &attr ) { m_attr = attr; }
82 bool GetWantsAttr() const { return m_wantsAttr; }
83
84 // implementation
85 int CalculateAlignment() const;
86
87 private:
88 wxDC *m_dc;
89 int m_align;
90 wxDataViewCellMode m_mode;
91
92 protected:
93 bool m_wantsAttr;
94 bool m_hasAttr;
95 wxDataViewItemAttr m_attr;
96
97 protected:
98 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewRenderer)
99 };
100
101 // ---------------------------------------------------------
102 // wxDataViewCustomRenderer
103 // ---------------------------------------------------------
104
105 class WXDLLIMPEXP_ADV wxDataViewCustomRenderer: public wxDataViewRenderer
106 {
107 public:
108 wxDataViewCustomRenderer( const wxString &varianttype = wxT("string"),
109 wxDataViewCellMode mode = wxDATAVIEW_CELL_INERT,
110 int align = wxDVR_DEFAULT_ALIGNMENT );
111
112 void RenderText( const wxString &text, int xoffset, wxRect cell, wxDC *dc, int state );
113
114 protected:
115 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewCustomRenderer)
116 };
117
118
119 // ---------------------------------------------------------
120 // wxDataViewTextRenderer
121 // ---------------------------------------------------------
122
123 class WXDLLIMPEXP_ADV wxDataViewTextRenderer: public wxDataViewCustomRenderer
124 {
125 public:
126 wxDataViewTextRenderer( const wxString &varianttype = wxT("string"),
127 wxDataViewCellMode mode = wxDATAVIEW_CELL_INERT,
128 int align = wxDVR_DEFAULT_ALIGNMENT );
129
130 bool SetValue( const wxVariant &value );
131 bool GetValue( wxVariant &value ) const;
132
133 bool Render( wxRect cell, wxDC *dc, int state );
134 wxSize GetSize() const;
135
136 // in-place editing
137 virtual bool HasEditorCtrl() const;
138 virtual wxControl* CreateEditorCtrl( wxWindow *parent, wxRect labelRect,
139 const wxVariant &value );
140 virtual bool GetValueFromEditorCtrl( wxControl* editor, wxVariant &value );
141
142 protected:
143 wxString m_text;
144
145 protected:
146 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewTextRenderer)
147 };
148
149 // ---------------------------------------------------------
150 // wxDataViewTextRendererAttr
151 // ---------------------------------------------------------
152
153 class WXDLLIMPEXP_ADV wxDataViewTextRendererAttr: public wxDataViewTextRenderer
154 {
155 public:
156 wxDataViewTextRendererAttr( const wxString &varianttype = wxT("string"),
157 wxDataViewCellMode mode = wxDATAVIEW_CELL_INERT,
158 int align = wxDVR_DEFAULT_ALIGNMENT );
159
160 bool Render( wxRect cell, wxDC *dc, int state );
161
162 protected:
163 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewTextRendererAttr)
164 };
165
166 // ---------------------------------------------------------
167 // wxDataViewBitmapRenderer
168 // ---------------------------------------------------------
169
170 class WXDLLIMPEXP_ADV wxDataViewBitmapRenderer: public wxDataViewCustomRenderer
171 {
172 public:
173 wxDataViewBitmapRenderer( const wxString &varianttype = wxT("wxBitmap"),
174 wxDataViewCellMode mode = wxDATAVIEW_CELL_INERT,
175 int align = wxDVR_DEFAULT_ALIGNMENT );
176
177 bool SetValue( const wxVariant &value );
178 bool GetValue( wxVariant &value ) const;
179
180 bool Render( wxRect cell, wxDC *dc, int state );
181 wxSize GetSize() const;
182
183 private:
184 wxIcon m_icon;
185 wxBitmap m_bitmap;
186
187 protected:
188 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewBitmapRenderer)
189 };
190
191 // ---------------------------------------------------------
192 // wxDataViewToggleRenderer
193 // ---------------------------------------------------------
194
195 class WXDLLIMPEXP_ADV wxDataViewToggleRenderer: public wxDataViewCustomRenderer
196 {
197 public:
198 wxDataViewToggleRenderer( const wxString &varianttype = wxT("bool"),
199 wxDataViewCellMode mode = wxDATAVIEW_CELL_INERT,
200 int align = wxDVR_DEFAULT_ALIGNMENT );
201
202 bool SetValue( const wxVariant &value );
203 bool GetValue( wxVariant &value ) const;
204
205 bool Render( wxRect cell, wxDC *dc, int state );
206 bool Activate( wxRect cell, wxDataViewModel *model, const wxDataViewItem & item,
207 unsigned int col );
208 wxSize GetSize() const;
209
210 private:
211 bool m_toggle;
212
213 protected:
214 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewToggleRenderer)
215 };
216
217 // ---------------------------------------------------------
218 // wxDataViewProgressRenderer
219 // ---------------------------------------------------------
220
221 class WXDLLIMPEXP_ADV wxDataViewProgressRenderer: public wxDataViewCustomRenderer
222 {
223 public:
224 wxDataViewProgressRenderer( const wxString &label = wxEmptyString,
225 const wxString &varianttype = wxT("long"),
226 wxDataViewCellMode mode = wxDATAVIEW_CELL_INERT,
227 int align = wxDVR_DEFAULT_ALIGNMENT );
228 virtual ~wxDataViewProgressRenderer();
229
230 bool SetValue( const wxVariant &value );
231 bool GetValue( wxVariant& value ) const;
232
233 virtual bool Render( wxRect cell, wxDC *dc, int state );
234 virtual wxSize GetSize() const;
235
236 private:
237 wxString m_label;
238 int m_value;
239
240 protected:
241 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewProgressRenderer)
242 };
243
244 // ---------------------------------------------------------
245 // wxDataViewIconTextRenderer
246 // ---------------------------------------------------------
247
248 class WXDLLIMPEXP_ADV wxDataViewIconTextRenderer: public wxDataViewCustomRenderer
249 {
250 public:
251 wxDataViewIconTextRenderer( const wxString &varianttype = wxT("wxDataViewIconText"),
252 wxDataViewCellMode mode = wxDATAVIEW_CELL_INERT,
253 int align = wxDVR_DEFAULT_ALIGNMENT );
254 virtual ~wxDataViewIconTextRenderer();
255
256 bool SetValue( const wxVariant &value );
257 bool GetValue( wxVariant &value ) const;
258
259 virtual bool Render( wxRect cell, wxDC *dc, int state );
260 virtual wxSize GetSize() const;
261
262 virtual bool HasEditorCtrl() const { return true; }
263 virtual wxControl* CreateEditorCtrl( wxWindow *parent, wxRect labelRect,
264 const wxVariant &value );
265 virtual bool GetValueFromEditorCtrl( wxControl* editor, wxVariant &value );
266
267 private:
268 wxDataViewIconText m_value;
269
270 protected:
271 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewIconTextRenderer)
272 };
273
274 // ---------------------------------------------------------
275 // wxDataViewDateRenderer
276 // ---------------------------------------------------------
277
278 class WXDLLIMPEXP_ADV wxDataViewDateRenderer: public wxDataViewCustomRenderer
279 {
280 public:
281 wxDataViewDateRenderer( const wxString &varianttype = wxT("datetime"),
282 wxDataViewCellMode mode = wxDATAVIEW_CELL_ACTIVATABLE,
283 int align = wxDVR_DEFAULT_ALIGNMENT );
284
285 bool SetValue( const wxVariant &value );
286 bool GetValue( wxVariant& value ) const;
287
288 virtual bool Render( wxRect cell, wxDC *dc, int state );
289 virtual wxSize GetSize() const;
290 virtual bool Activate( wxRect cell,
291 wxDataViewModel *model,
292 const wxDataViewItem& item,
293 unsigned int col );
294
295 private:
296 wxDateTime m_date;
297
298 protected:
299 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewDateRenderer)
300 };
301
302 // ---------------------------------------------------------
303 // wxDataViewColumn
304 // ---------------------------------------------------------
305
306 class WXDLLIMPEXP_ADV wxDataViewColumn : public wxDataViewColumnBase
307 {
308 public:
309 wxDataViewColumn(const wxString& title,
310 wxDataViewRenderer *renderer,
311 unsigned int model_column,
312 int width = wxDVC_DEFAULT_WIDTH,
313 wxAlignment align = wxALIGN_CENTER,
314 int flags = wxDATAVIEW_COL_RESIZABLE)
315 : wxDataViewColumnBase(renderer, model_column),
316 m_title(title)
317 {
318 Init(width, align, flags);
319 }
320
321 wxDataViewColumn(const wxBitmap& bitmap,
322 wxDataViewRenderer *renderer,
323 unsigned int model_column,
324 int width = wxDVC_DEFAULT_WIDTH,
325 wxAlignment align = wxALIGN_CENTER,
326 int flags = wxDATAVIEW_COL_RESIZABLE)
327 : wxDataViewColumnBase(bitmap, renderer, model_column)
328 {
329 Init(width, align, flags);
330 }
331
332 // implement wxHeaderColumnBase methods
333 virtual void SetTitle(const wxString& title) { m_title = title; }
334 virtual wxString GetTitle() const { return m_title; }
335
336 virtual void SetWidth(int width) { m_width = width; }
337 virtual int GetWidth() const { return m_width; }
338
339 virtual void SetMinWidth(int minWidth) { m_minWidth = minWidth; }
340 virtual int GetMinWidth() const { return m_minWidth; }
341
342 virtual void SetAlignment(wxAlignment align) { m_align = align; }
343 virtual wxAlignment GetAlignment() const { return m_align; }
344
345 virtual void SetFlags(int flags) { m_flags = flags; }
346 virtual int GetFlags() const { return m_flags; }
347
348 virtual void SetAsSortKey(bool sort = true) { m_sort = sort; }
349 virtual bool IsSortKey() const { return m_sort; }
350
351 virtual void SetSortOrder(bool ascending) { m_sortAscending = ascending; }
352 virtual bool IsSortOrderAscending() const { return m_sortAscending; }
353
354 private:
355 // common part of all ctors
356 void Init(int width, wxAlignment align, int flags)
357 {
358 m_width = width == wxCOL_WIDTH_DEFAULT ? wxDVC_DEFAULT_WIDTH : width;
359 m_minWidth = 0;
360 m_align = align;
361 m_flags = flags;
362 m_sort = false;
363 m_sortAscending = true;
364 }
365
366 wxString m_title;
367 int m_width,
368 m_minWidth;
369 wxAlignment m_align;
370 int m_flags;
371 bool m_sort,
372 m_sortAscending;
373
374 friend class wxDataViewHeaderWindowBase;
375 friend class wxDataViewHeaderWindow;
376 friend class wxDataViewHeaderWindowMSW;
377 };
378
379 // ---------------------------------------------------------
380 // wxDataViewCtrl
381 // ---------------------------------------------------------
382
383 WX_DECLARE_LIST_WITH_DECL(wxDataViewColumn, wxDataViewColumnList,
384 class WXDLLIMPEXP_ADV);
385
386 class WXDLLIMPEXP_ADV wxDataViewCtrl : public wxDataViewCtrlBase,
387 public wxScrollHelper
388 {
389 friend class wxDataViewMainWindow;
390 friend class wxDataViewHeaderWindowBase;
391 friend class wxDataViewHeaderWindow;
392 friend class wxDataViewHeaderWindowMSW;
393 friend class wxDataViewColumn;
394
395 public:
396 wxDataViewCtrl() : wxScrollHelper(this)
397 {
398 Init();
399 }
400
401 wxDataViewCtrl( wxWindow *parent, wxWindowID id,
402 const wxPoint& pos = wxDefaultPosition,
403 const wxSize& size = wxDefaultSize, long style = 0,
404 const wxValidator& validator = wxDefaultValidator )
405 : wxScrollHelper(this)
406 {
407 Create(parent, id, pos, size, style, validator );
408 }
409
410 virtual ~wxDataViewCtrl();
411
412 void Init();
413
414 bool Create(wxWindow *parent, wxWindowID id,
415 const wxPoint& pos = wxDefaultPosition,
416 const wxSize& size = wxDefaultSize, long style = 0,
417 const wxValidator& validator = wxDefaultValidator );
418
419 virtual bool AssociateModel( wxDataViewModel *model );
420
421 virtual bool AppendColumn( wxDataViewColumn *col );
422 virtual bool PrependColumn( wxDataViewColumn *col );
423 virtual bool InsertColumn( unsigned int pos, wxDataViewColumn *col );
424
425 virtual void DoSetExpanderColumn();
426 virtual void DoSetIndent();
427
428 virtual unsigned int GetColumnCount() const;
429 virtual wxDataViewColumn* GetColumn( unsigned int pos ) const;
430 virtual bool DeleteColumn( wxDataViewColumn *column );
431 virtual bool ClearColumns();
432 virtual int GetColumnPosition( const wxDataViewColumn *column ) const;
433
434 virtual wxDataViewColumn *GetSortingColumn() const;
435
436 virtual wxDataViewItem GetSelection() const;
437 virtual int GetSelections( wxDataViewItemArray & sel ) const;
438 virtual void SetSelections( const wxDataViewItemArray & sel );
439 virtual void Select( const wxDataViewItem & item );
440 virtual void Unselect( const wxDataViewItem & item );
441 virtual bool IsSelected( const wxDataViewItem & item ) const;
442
443 virtual void SelectAll();
444 virtual void UnselectAll();
445
446 virtual void EnsureVisible( const wxDataViewItem & item,
447 const wxDataViewColumn *column = NULL );
448 virtual void HitTest( const wxPoint & point, wxDataViewItem & item,
449 wxDataViewColumn* &column ) const;
450 virtual wxRect GetItemRect( const wxDataViewItem & item,
451 const wxDataViewColumn *column = NULL ) const;
452
453 virtual void Expand( const wxDataViewItem & item );
454 virtual void Collapse( const wxDataViewItem & item );
455 virtual bool IsExpanded( const wxDataViewItem & item ) const;
456
457 virtual void SetFocus();
458
459 #if wxUSE_DRAG_AND_DROP
460 virtual bool EnableDragSource( const wxDataFormat &format );
461 virtual bool EnableDropTarget( const wxDataFormat &format );
462 #endif // wxUSE_DRAG_AND_DROP
463
464 virtual wxBorder GetDefaultBorder() const;
465
466 protected:
467 virtual int GetSelections( wxArrayInt & sel ) const;
468 virtual void SetSelections( const wxArrayInt & sel );
469 virtual void Select( int row );
470 virtual void Unselect( int row );
471 virtual bool IsSelected( int row ) const;
472 virtual void SelectRange( int from, int to );
473 virtual void UnselectRange( int from, int to );
474
475 virtual void EnsureVisible( int row, int column );
476
477 virtual wxDataViewItem GetItemByRow( unsigned int row ) const;
478 virtual int GetRowByItem( const wxDataViewItem & item ) const;
479
480 int GetSortingColumnIndex() const { return m_sortingColumnIdx; }
481 void SetSortingColumnIndex(int idx) { m_sortingColumnIdx = idx; }
482
483 public: // utility functions not part of the API
484
485 // returns the "best" width for the idx-th column
486 unsigned int GetBestColumnWidth(int WXUNUSED(idx)) const
487 {
488 return GetClientSize().GetWidth() / GetColumnCount();
489 }
490
491 // called by header window after reorder
492 void ColumnMoved( wxDataViewColumn* col, unsigned int new_pos );
493
494 // update the display after a change to an individual column
495 void OnColumnChange(unsigned int idx);
496
497 // update after a change to the number of columns
498 void OnColumnsCountChanged();
499
500 wxWindow *GetMainWindow() { return (wxWindow*) m_clientArea; }
501
502 // return the index of the given column in m_cols
503 int GetColumnIndex(const wxDataViewColumn *column) const;
504
505 // return the column displayed at the given position in the control
506 wxDataViewColumn *GetColumnAt(unsigned int pos) const;
507
508 private:
509 wxDataViewColumnList m_cols;
510 wxDataViewModelNotifier *m_notifier;
511 wxDataViewMainWindow *m_clientArea;
512 wxDataViewHeaderWindow *m_headerArea;
513
514 // the index of the column currently used for sorting or -1
515 int m_sortingColumnIdx;
516
517 private:
518 void OnSize( wxSizeEvent &event );
519 virtual wxSize GetSizeAvailableForScrollTarget(const wxSize& size);
520
521 // we need to return a special WM_GETDLGCODE value to process just the
522 // arrows but let the other navigation characters through
523 #ifdef __WXMSW__
524 virtual WXLRESULT MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam);
525 #endif // __WXMSW__
526
527 WX_FORWARD_TO_SCROLL_HELPER()
528
529 private:
530 DECLARE_DYNAMIC_CLASS(wxDataViewCtrl)
531 wxDECLARE_NO_COPY_CLASS(wxDataViewCtrl);
532 DECLARE_EVENT_TABLE()
533 };
534
535
536 #endif // __GENERICDATAVIEWCTRLH__