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