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