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