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