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