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