Patch from Bo for recent API changes
[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 { m_align=align; }
46 virtual int GetAlignment() const
47 { return m_align; }
48
49 virtual void SetMode( wxDataViewCellMode mode )
50 { m_mode=mode; }
51 virtual wxDataViewCellMode GetMode() const
52 { return m_mode; }
53
54 virtual bool Activate( wxRect WXUNUSED(cell),
55 wxDataViewModel *WXUNUSED(model),
56 const wxDataViewItem & WXUNUSED(item),
57 unsigned int WXUNUSED(col) )
58 { return false; }
59
60 virtual bool LeftClick( wxPoint WXUNUSED(cursor),
61 wxRect WXUNUSED(cell),
62 wxDataViewModel *WXUNUSED(model),
63 const wxDataViewItem & WXUNUSED(item),
64 unsigned int WXUNUSED(col) )
65 { return false; }
66 virtual bool RightClick( wxPoint WXUNUSED(cursor),
67 wxRect WXUNUSED(cell),
68 wxDataViewModel *WXUNUSED(model),
69 const wxDataViewItem & WXUNUSED(item),
70 unsigned int WXUNUSED(col) )
71 { return false; }
72 virtual bool StartDrag( wxPoint WXUNUSED(cursor),
73 wxRect WXUNUSED(cell),
74 wxDataViewModel *WXUNUSED(model),
75 const wxDataViewItem & WXUNUSED(item),
76 unsigned int WXUNUSED(col) )
77 { return false; }
78
79 // Create DC on request
80 virtual wxDC *GetDC();
81
82 private:
83 wxDC *m_dc;
84 int m_align;
85 wxDataViewCellMode m_mode;
86
87 protected:
88 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewRenderer)
89 };
90
91 // ---------------------------------------------------------
92 // wxDataViewCustomRenderer
93 // ---------------------------------------------------------
94
95 class WXDLLIMPEXP_ADV wxDataViewCustomRenderer: public wxDataViewRenderer
96 {
97 public:
98 wxDataViewCustomRenderer( const wxString &varianttype = wxT("string"),
99 wxDataViewCellMode mode = wxDATAVIEW_CELL_INERT,
100 int align = wxDVR_DEFAULT_ALIGNMENT );
101
102 protected:
103 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewCustomRenderer)
104 };
105
106
107 // ---------------------------------------------------------
108 // wxDataViewTextRenderer
109 // ---------------------------------------------------------
110
111 class WXDLLIMPEXP_ADV wxDataViewTextRenderer: public wxDataViewCustomRenderer
112 {
113 public:
114 wxDataViewTextRenderer( const wxString &varianttype = wxT("string"),
115 wxDataViewCellMode mode = wxDATAVIEW_CELL_INERT,
116 int align = wxDVR_DEFAULT_ALIGNMENT );
117
118 bool SetValue( const wxVariant &value );
119 bool GetValue( wxVariant &value ) const;
120
121 bool Render( wxRect cell, wxDC *dc, int state );
122 wxSize GetSize() const;
123
124 // in-place editing
125 virtual bool HasEditorCtrl();
126 virtual wxControl* CreateEditorCtrl( wxWindow *parent, wxRect labelRect, const wxVariant &value );
127 virtual bool GetValueFromEditorCtrl( wxControl* editor, wxVariant &value );
128
129 private:
130 wxString m_text;
131
132 protected:
133 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewTextRenderer)
134 };
135
136 // ---------------------------------------------------------
137 // wxDataViewBitmapRenderer
138 // ---------------------------------------------------------
139
140 class WXDLLIMPEXP_ADV wxDataViewBitmapRenderer: public wxDataViewCustomRenderer
141 {
142 public:
143 wxDataViewBitmapRenderer( const wxString &varianttype = wxT("wxBitmap"),
144 wxDataViewCellMode mode = wxDATAVIEW_CELL_INERT,
145 int align = wxDVR_DEFAULT_ALIGNMENT );
146
147 bool SetValue( const wxVariant &value );
148 bool GetValue( wxVariant &value ) const;
149
150 bool Render( wxRect cell, wxDC *dc, int state );
151 wxSize GetSize() const;
152
153 private:
154 wxIcon m_icon;
155 wxBitmap m_bitmap;
156
157 protected:
158 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewBitmapRenderer)
159 };
160
161 // ---------------------------------------------------------
162 // wxDataViewToggleRenderer
163 // ---------------------------------------------------------
164
165 class WXDLLIMPEXP_ADV wxDataViewToggleRenderer: public wxDataViewCustomRenderer
166 {
167 public:
168 wxDataViewToggleRenderer( const wxString &varianttype = wxT("bool"),
169 wxDataViewCellMode mode = wxDATAVIEW_CELL_INERT,
170 int align = wxDVR_DEFAULT_ALIGNMENT );
171
172 bool SetValue( const wxVariant &value );
173 bool GetValue( wxVariant &value ) const;
174
175 bool Render( wxRect cell, wxDC *dc, int state );
176 bool Activate( wxRect cell, wxDataViewModel *model, const wxDataViewItem & item,
177 unsigned int col );
178 wxSize GetSize() const;
179
180 private:
181 bool m_toggle;
182
183 protected:
184 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewToggleRenderer)
185 };
186
187 // ---------------------------------------------------------
188 // wxDataViewProgressRenderer
189 // ---------------------------------------------------------
190
191 class WXDLLIMPEXP_ADV wxDataViewProgressRenderer: public wxDataViewCustomRenderer
192 {
193 public:
194 wxDataViewProgressRenderer( const wxString &label = wxEmptyString,
195 const wxString &varianttype = wxT("long"),
196 wxDataViewCellMode mode = wxDATAVIEW_CELL_INERT,
197 int align = wxDVR_DEFAULT_ALIGNMENT );
198 virtual ~wxDataViewProgressRenderer();
199
200 bool SetValue( const wxVariant &value );
201 bool GetValue( wxVariant& value ) const;
202
203 virtual bool Render( wxRect cell, wxDC *dc, int state );
204 virtual wxSize GetSize() const;
205
206 private:
207 wxString m_label;
208 int m_value;
209
210 protected:
211 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewProgressRenderer)
212 };
213
214 // ---------------------------------------------------------
215 // wxDataViewDateRenderer
216 // ---------------------------------------------------------
217
218 class WXDLLIMPEXP_ADV wxDataViewDateRenderer: public wxDataViewCustomRenderer
219 {
220 public:
221 wxDataViewDateRenderer( const wxString &varianttype = wxT("datetime"),
222 wxDataViewCellMode mode = wxDATAVIEW_CELL_ACTIVATABLE,
223 int align = wxDVR_DEFAULT_ALIGNMENT );
224
225 bool SetValue( const wxVariant &value );
226 bool GetValue( wxVariant& value ) const;
227
228 virtual bool Render( wxRect cell, wxDC *dc, int state );
229 virtual wxSize GetSize() const;
230 virtual bool Activate( wxRect cell,
231 wxDataViewModel *model, const wxDataViewItem & item, unsigned int col );
232
233 private:
234 wxDateTime m_date;
235
236 protected:
237 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewDateRenderer)
238 };
239
240 // ---------------------------------------------------------
241 // wxDataViewColumn
242 // ---------------------------------------------------------
243
244 class WXDLLIMPEXP_ADV wxDataViewColumn: public wxDataViewColumnBase
245 {
246 friend class wxDataViewHeaderWindowBase;
247 friend class wxDataViewHeaderWindow;
248 friend class wxDataViewHeaderWindowMSW;
249
250 public:
251 wxDataViewColumn( const wxString &title, wxDataViewRenderer *renderer,
252 unsigned int model_column, int width = wxDVC_DEFAULT_WIDTH,
253 wxAlignment align = wxALIGN_CENTER,
254 int flags = wxDATAVIEW_COL_RESIZABLE );
255 wxDataViewColumn( const wxBitmap &bitmap, wxDataViewRenderer *renderer,
256 unsigned int model_column, int width = wxDVC_DEFAULT_WIDTH,
257 wxAlignment align = wxALIGN_CENTER,
258 int flags = wxDATAVIEW_COL_RESIZABLE );
259 virtual ~wxDataViewColumn();
260
261 // setters:
262
263 virtual void SetTitle( const wxString &title )
264 { m_title=title; }
265 virtual void SetAlignment( wxAlignment align )
266 { m_align=align; }
267 virtual void SetMinWidth( int minWidth )
268 { m_minWidth=minWidth; }
269 virtual void SetWidth( int width );
270 virtual void SetSortable( bool sortable );
271 virtual void SetResizeable( bool resizeable );
272 virtual void SetHidden( bool hidden );
273 virtual void SetSortOrder( bool ascending );
274
275
276 // getters:
277
278 virtual wxString GetTitle() const
279 { return m_title; }
280 virtual wxAlignment GetAlignment() const
281 { return m_align; }
282 virtual int GetWidth() const
283 { return m_width; }
284 virtual int GetMinWidth() const
285 { return m_minWidth; }
286 virtual bool IsSortable() const
287 { return (m_flags & wxDATAVIEW_COL_SORTABLE) != 0; }
288 virtual bool IsResizeable() const
289 { return (m_flags & wxDATAVIEW_COL_RESIZABLE) != 0; }
290 virtual bool IsHidden() const
291 { return (m_flags & wxDATAVIEW_COL_HIDDEN) != 0; }
292 virtual bool IsSortOrderAscending() const;
293
294
295 private:
296 int m_width;
297 int m_minWidth;
298 int m_flags;
299 wxAlignment m_align;
300 wxString m_title;
301 bool m_ascending;
302
303 void Init(int width);
304
305 // like SetWidth() but does not ask the header window of the
306 // wxDataViewCtrl to reflect the width-change.
307 void SetInternalWidth(int width);
308
309 protected:
310 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewColumn)
311 };
312
313 // ---------------------------------------------------------
314 // wxDataViewCtrl
315 // ---------------------------------------------------------
316
317 WX_DECLARE_LIST(wxDataViewColumn, wxDataViewColumnList );
318
319 class WXDLLIMPEXP_ADV wxDataViewCtrl: public wxDataViewCtrlBase,
320 public wxScrollHelperNative
321 {
322 friend class wxDataViewMainWindow;
323 friend class wxDataViewHeaderWindowBase;
324 friend class wxDataViewHeaderWindow;
325 friend class wxDataViewHeaderWindowMSW;
326 friend class wxDataViewColumn;
327
328 public:
329 wxDataViewCtrl() : wxScrollHelperNative(this)
330 {
331 m_sortingColumn = 0;
332 Init();
333 }
334
335 wxDataViewCtrl( wxWindow *parent, wxWindowID id,
336 const wxPoint& pos = wxDefaultPosition,
337 const wxSize& size = wxDefaultSize, long style = 0,
338 const wxValidator& validator = wxDefaultValidator )
339 : wxScrollHelperNative(this)
340 {
341 Create(parent, id, pos, size, style, validator );
342 }
343
344 virtual ~wxDataViewCtrl();
345
346 void Init();
347
348 bool Create(wxWindow *parent, wxWindowID id,
349 const wxPoint& pos = wxDefaultPosition,
350 const wxSize& size = wxDefaultSize, long style = 0,
351 const wxValidator& validator = wxDefaultValidator );
352
353 virtual bool AssociateModel( wxDataViewModel *model );
354 virtual bool AppendColumn( wxDataViewColumn *col );
355
356 virtual void DoSetExpanderColumn();
357 virtual void DoSetIndent();
358
359 virtual unsigned int GetColumnCount() const;
360 virtual wxDataViewColumn* GetColumn( unsigned int pos ) const;
361 virtual bool DeleteColumn( wxDataViewColumn *column );
362 virtual bool ClearColumns();
363
364 virtual wxDataViewItem GetSelection() const;
365 virtual int GetSelections( wxDataViewItemArray & sel ) const;
366 virtual void SetSelections( const wxDataViewItemArray & sel );
367 virtual void Select( const wxDataViewItem & item );
368 virtual void Unselect( const wxDataViewItem & item );
369 virtual bool IsSelected( const wxDataViewItem & item ) const;
370
371 virtual void SelectAll();
372 virtual void UnselectAll();
373
374 virtual void EnsureVisible( const wxDataViewItem & item,
375 const wxDataViewColumn *column = NULL );
376 virtual void HitTest( const wxPoint & point, wxDataViewItem & item, wxDataViewColumn* &column ) const;
377 virtual wxRect GetItemRect( const wxDataViewItem & item, const wxDataViewColumn *column = NULL ) const;
378
379 virtual void Expand( const wxDataViewItem & item );
380 virtual void Collapse( const wxDataViewItem & item );
381
382 protected:
383 virtual int GetSelections( wxArrayInt & sel ) const;
384 virtual void SetSelections( const wxArrayInt & sel );
385 virtual void Select( int row );
386 virtual void Unselect( int row );
387 virtual bool IsSelected( int row ) const;
388 virtual void SelectRange( int from, int to );
389 virtual void UnselectRange( int from, int to );
390
391 virtual void EnsureVisible( int row, int column );
392
393 virtual wxDataViewItem GetItemByRow( unsigned int row ) const;
394 virtual int GetRowByItem( const wxDataViewItem & item ) const;
395
396 unsigned int GetSortingColumn() { return m_sortingColumn; }
397 void SetSortingColumn( unsigned int column ) { m_sortingColumn = column; }
398
399 public: // utility functions not part of the API
400
401 // returns the "best" width for the idx-th column
402 unsigned int GetBestColumnWidth(int WXUNUSED(idx)) const
403 {
404 return GetClientSize().GetWidth() / GetColumnCount();
405 }
406
407 // updates the header window after a change in a column setting
408 void OnColumnChange();
409
410 wxWindow *GetMainWindow() { return (wxWindow*) m_clientArea; }
411
412 private:
413 wxDataViewColumnList m_cols;
414 wxDataViewModelNotifier *m_notifier;
415 wxDataViewMainWindow *m_clientArea;
416 wxDataViewHeaderWindow *m_headerArea;
417 unsigned int m_sortingColumn;
418
419 private:
420 void OnSize( wxSizeEvent &event );
421
422 // we need to return a special WM_GETDLGCODE value to process just the
423 // arrows but let the other navigation characters through
424 #ifdef __WXMSW__
425 virtual WXLRESULT MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam);
426 #endif // __WXMSW__
427
428 WX_FORWARD_TO_SCROLL_HELPER()
429
430 private:
431 DECLARE_DYNAMIC_CLASS(wxDataViewCtrl)
432 DECLARE_NO_COPY_CLASS(wxDataViewCtrl)
433 DECLARE_EVENT_TABLE()
434 };
435
436
437 #endif // __GENERICDATAVIEWCTRLH__