]> git.saurik.com Git - wxWidgets.git/blob - include/wx/osx/dataview.h
Rewrite handling cell value changes in wxOSX/Cocoa wxDVC.
[wxWidgets.git] / include / wx / osx / dataview.h
1
2 /////////////////////////////////////////////////////////////////////////////
3 // Name: wx/osx/dataview.h
4 // Purpose: wxDataViewCtrl native implementation header for OSX
5 // Author:
6 // Id: $Id$
7 // Copyright: (c) 2009
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #ifndef _WX_DATAVIEWCTRL_OSX_H_
12 #define _WX_DATAVIEWCTRL_OSX_H_
13
14 #ifdef __WXMAC_CLASSIC__
15 # error "Native wxDataViewCtrl for classic environment not defined. Please use generic control."
16 #endif
17
18 // --------------------------------------------------------
19 // Class declarations to mask native types
20 // --------------------------------------------------------
21 class wxDataViewColumnNativeData; // class storing environment dependent data for the native implementation
22 class wxDataViewRendererNativeData; // class storing environment dependent data for the native renderer
23 class wxDataViewWidgetImpl; // class used as a common interface for carbon and cocoa implementation
24
25 // ---------------------------------------------------------
26 // wxDataViewRenderer
27 // ---------------------------------------------------------
28 class WXDLLIMPEXP_ADV wxDataViewRenderer : public wxDataViewRendererBase
29 {
30 public:
31 //
32 // constructors / destructor
33 //
34 wxDataViewRenderer(wxString const& varianttype, wxDataViewCellMode mode=wxDATAVIEW_CELL_INERT, int align=wxDVR_DEFAULT_ALIGNMENT);
35
36 virtual ~wxDataViewRenderer();
37
38 //
39 // inherited methods from wxDataViewRendererBase
40 //
41 virtual int GetAlignment() const
42 {
43 return m_alignment;
44 }
45 virtual wxDataViewCellMode GetMode() const
46 {
47 return m_mode;
48 }
49 virtual bool GetValue(wxVariant& value) const
50 {
51 value = m_value;
52 return true;
53 }
54
55 virtual void SetAlignment(int align); // carbon: is always identical to the header alignment;
56 // cocoa: cell alignment is independent from header alignment
57 virtual void SetMode(wxDataViewCellMode mode);
58 virtual bool SetValue(wxVariant const& newValue)
59 {
60 m_value = newValue;
61 return true;
62 }
63
64 virtual void EnableEllipsize(wxEllipsizeMode mode = wxELLIPSIZE_MIDDLE);
65 virtual wxEllipsizeMode GetEllipsizeMode() const;
66
67 //
68 // implementation
69 //
70 wxVariant const& GetValue() const
71 {
72 return m_value;
73 }
74
75 wxDataViewRendererNativeData* GetNativeData() const
76 {
77 return m_NativeDataPtr;
78 }
79
80 virtual bool MacRender() = 0; // a call to the native data browser function to render the data;
81 // returns true if the data value could be rendered, false otherwise
82
83 void SetNativeData(wxDataViewRendererNativeData* newNativeDataPtr);
84
85
86 #if wxOSX_USE_COCOA
87 // called when a value was edited by user
88 virtual void OSXOnCellChanged(const wxVariant& value,
89 const wxDataViewItem& item,
90 unsigned col);
91 #endif // Cocoa
92
93 private:
94 //
95 // variables
96 //
97 int m_alignment; // contains the alignment flags
98
99 wxDataViewCellMode m_mode; // storing the mode that determines how the cell is going to be shown
100
101 wxDataViewRendererNativeData* m_NativeDataPtr; // data used by implementation of the native renderer
102
103 wxVariant m_value; // value that is going to be rendered
104
105 //
106 // wxWidget internal stuff
107 //
108 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewRenderer)
109 };
110
111 // ---------------------------------------------------------
112 // wxDataViewCustomRenderer
113 // ---------------------------------------------------------
114 class WXDLLIMPEXP_ADV wxDataViewCustomRenderer: public wxDataViewRenderer
115 {
116 public:
117 //
118 // constructors / destructor
119 //
120 wxDataViewCustomRenderer(wxString const& varianttype=wxT("string"), wxDataViewCellMode mode=wxDATAVIEW_CELL_INERT, int align=wxDVR_DEFAULT_ALIGNMENT);
121
122 virtual ~wxDataViewCustomRenderer();
123
124 void RenderText( const wxString &text, int xoffset, wxRect cell, wxDC *dc, int state );
125
126 //
127 // methods handling render space
128 //
129 virtual wxSize GetSize() const = 0;
130
131 //
132 // methods handling user actions
133 //
134 virtual bool Render(wxRect cell, wxDC* dc, int state) = 0;
135
136 virtual bool Activate( wxRect WXUNUSED(cell),
137 wxDataViewModel *WXUNUSED(model),
138 const wxDataViewItem & WXUNUSED(item),
139 unsigned int WXUNUSED(col) )
140 { return false; }
141
142 virtual bool LeftClick( wxPoint WXUNUSED(cursor),
143 wxRect WXUNUSED(cell),
144 wxDataViewModel *WXUNUSED(model),
145 const wxDataViewItem & WXUNUSED(item),
146 unsigned int WXUNUSED(col) )
147 { return false; }
148
149 virtual bool StartDrag( wxPoint WXUNUSED(cursor),
150 wxRect WXUNUSED(cell),
151 wxDataViewModel *WXUNUSED(model),
152 const wxDataViewItem & WXUNUSED(item),
153 unsigned int WXUNUSED(col) )
154 { return false; }
155
156 //
157 // device context handling
158 //
159 virtual wxDC* GetDC(); // creates a device context and keeps it
160
161 //
162 // implementation
163 //
164 virtual bool MacRender();
165
166 void SetDC(wxDC* newDCPtr); // this method takes ownership of the pointer
167
168 protected:
169 private:
170 //
171 // variables
172 //
173 wxControl* m_editorCtrlPtr; // pointer to an in-place editor control
174
175 wxDC* m_DCPtr;
176
177 //
178 // wxWidget internal stuff
179 //
180 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewCustomRenderer)
181 };
182
183 // ---------------------------------------------------------
184 // wxDataViewTextRenderer
185 // ---------------------------------------------------------
186
187 class WXDLLIMPEXP_ADV wxDataViewTextRenderer: public wxDataViewRenderer
188 {
189 public:
190 //
191 // constructors / destructor
192 //
193 wxDataViewTextRenderer(wxString const& varianttype=wxT("string"), wxDataViewCellMode mode=wxDATAVIEW_CELL_INERT, int align=wxDVR_DEFAULT_ALIGNMENT);
194
195 //
196 // inherited functions from wxDataViewRenderer
197 //
198 virtual bool MacRender();
199
200 protected:
201 private:
202 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewTextRenderer)
203 };
204
205 // ---------------------------------------------------------
206 // wxDataViewBitmapRenderer
207 // ---------------------------------------------------------
208
209 class WXDLLIMPEXP_ADV wxDataViewBitmapRenderer: public wxDataViewRenderer
210 {
211 public:
212 //
213 // constructors / destructor
214 //
215 wxDataViewBitmapRenderer(wxString const& varianttype=wxT("wxBitmap"), wxDataViewCellMode mode=wxDATAVIEW_CELL_INERT, int align=wxDVR_DEFAULT_ALIGNMENT);
216
217 //
218 // inherited functions from wxDataViewRenderer
219 //
220 virtual bool MacRender();
221
222 protected:
223 private:
224 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewBitmapRenderer)
225 };
226
227 #if !defined(wxUSE_GENERICDATAVIEWCTRL) && defined(__WXOSX_COCOA__)
228
229 // -------------------------------------
230 // wxDataViewChoiceRenderer
231 // -------------------------------------
232 class WXDLLIMPEXP_ADV wxDataViewChoiceRenderer: public wxDataViewRenderer
233 {
234 public:
235 //
236 // constructors / destructor
237 //
238 wxDataViewChoiceRenderer(wxArrayString const& choices,
239 wxDataViewCellMode mode = wxDATAVIEW_CELL_EDITABLE,
240 int alignment = wxDVR_DEFAULT_ALIGNMENT );
241
242 //
243 // inherited functions from wxDataViewRenderer
244 //
245 virtual bool MacRender();
246
247 //
248 // implementation
249 //
250 wxString GetChoice(size_t index) const
251 {
252 return m_Choices[index];
253 }
254 wxArrayString const& GetChoices() const
255 {
256 return m_Choices;
257 }
258
259 private:
260 //
261 // variables
262 //
263 wxArrayString m_Choices;
264
265 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewChoiceRenderer)
266 };
267
268 #endif
269
270 // ---------------------------------------------------------
271 // wxDataViewIconTextRenderer
272 // ---------------------------------------------------------
273 class WXDLLIMPEXP_ADV wxDataViewIconTextRenderer: public wxDataViewRenderer
274 {
275 public:
276 wxDataViewIconTextRenderer(wxString const& varianttype = wxT("wxDataViewIconText"), wxDataViewCellMode mode = wxDATAVIEW_CELL_INERT, int align=wxDVR_DEFAULT_ALIGNMENT);
277
278 //
279 // inherited functions from wxDataViewRenderer
280 //
281 virtual bool MacRender();
282
283 #if wxOSX_USE_COCOA
284 // called when a value was edited by user
285 virtual void OSXOnCellChanged(const wxVariant& value,
286 const wxDataViewItem& item,
287 unsigned col);
288 #endif // Cocoa
289
290 protected:
291 private:
292 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewIconTextRenderer)
293 };
294
295 // ---------------------------------------------------------
296 // wxDataViewToggleRenderer
297 // ---------------------------------------------------------
298
299 class WXDLLIMPEXP_ADV wxDataViewToggleRenderer: public wxDataViewRenderer
300 {
301 public:
302 wxDataViewToggleRenderer(wxString const& varianttype = wxT("bool"), wxDataViewCellMode mode = wxDATAVIEW_CELL_INERT, int align=wxDVR_DEFAULT_ALIGNMENT);
303
304 //
305 // inherited functions from wxDataViewRenderer
306 //
307 virtual bool MacRender();
308
309 protected:
310 private:
311 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewToggleRenderer)
312 };
313
314 // ---------------------------------------------------------
315 // wxDataViewProgressRenderer
316 // ---------------------------------------------------------
317
318 class WXDLLIMPEXP_ADV wxDataViewProgressRenderer: public wxDataViewRenderer
319 {
320 public:
321 wxDataViewProgressRenderer(wxString const& label = wxEmptyString, wxString const& varianttype=wxT("long"),
322 wxDataViewCellMode mode=wxDATAVIEW_CELL_INERT, int align=wxDVR_DEFAULT_ALIGNMENT);
323
324 //
325 // inherited functions from wxDataViewRenderer
326 //
327 virtual bool MacRender();
328
329 protected:
330 private:
331 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewProgressRenderer)
332 };
333
334 // ---------------------------------------------------------
335 // wxDataViewDateRenderer
336 // ---------------------------------------------------------
337
338 class WXDLLIMPEXP_ADV wxDataViewDateRenderer: public wxDataViewRenderer
339 {
340 public:
341 wxDataViewDateRenderer(wxString const& varianttype=wxT("datetime"), wxDataViewCellMode mode=wxDATAVIEW_CELL_ACTIVATABLE, int align=wxDVR_DEFAULT_ALIGNMENT);
342
343 //
344 // inherited functions from wxDataViewRenderer
345 //
346 virtual bool MacRender();
347
348 protected:
349 private:
350 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewDateRenderer)
351 };
352
353 // ---------------------------------------------------------
354 // wxDataViewColumn
355 // ---------------------------------------------------------
356
357 class WXDLLIMPEXP_ADV wxDataViewColumn: public wxDataViewColumnBase
358 {
359 public:
360 // constructors / destructor
361 wxDataViewColumn(const wxString& title,
362 wxDataViewRenderer* renderer,
363 unsigned int model_column,
364 int width = wxDVC_DEFAULT_WIDTH,
365 wxAlignment align = wxALIGN_CENTER,
366 int flags = wxDATAVIEW_COL_RESIZABLE);
367 wxDataViewColumn(const wxBitmap& bitmap,
368 wxDataViewRenderer* renderer,
369 unsigned int model_column,
370 int width = wxDVC_DEFAULT_WIDTH,
371 wxAlignment align = wxALIGN_CENTER,
372 int flags = wxDATAVIEW_COL_RESIZABLE);
373 virtual ~wxDataViewColumn();
374
375 // implement wxHeaderColumnBase pure virtual methods
376 virtual wxAlignment GetAlignment() const { return m_alignment; }
377 virtual int GetFlags() const { return m_flags; }
378 virtual int GetMaxWidth() const { return m_maxWidth; }
379 virtual int GetMinWidth() const { return m_minWidth; }
380 virtual wxString GetTitle() const { return m_title; }
381 virtual int GetWidth() const { return m_width; }
382 virtual bool IsHidden() const { return false; } // TODO
383 virtual bool IsSortOrderAscending() const { return m_ascending; }
384 virtual bool IsSortKey() const;
385
386 virtual void SetAlignment (wxAlignment align);
387 virtual void SetBitmap (wxBitmap const& bitmap);
388 virtual void SetFlags (int flags) { SetIndividualFlags(flags); }
389 virtual void SetHidden (bool WXUNUSED(hidden)) { } // TODO
390 virtual void SetMaxWidth (int maxWidth);
391 virtual void SetMinWidth (int minWidth);
392 virtual void SetReorderable(bool reorderable);
393 virtual void SetResizeable (bool resizeable);
394 virtual void SetSortable (bool sortable);
395 virtual void SetSortOrder (bool ascending);
396 virtual void SetTitle (wxString const& title);
397 virtual void SetWidth (int width);
398 virtual void SetAsSortKey (bool sort = true);
399
400 // implementation only
401 wxDataViewColumnNativeData* GetNativeData() const
402 {
403 return m_NativeDataPtr;
404 }
405
406 void SetNativeData(wxDataViewColumnNativeData* newNativeDataPtr); // class takes ownership of pointer
407 void SetWidthVariable(int NewWidth)
408 {
409 m_width = NewWidth;
410 }
411 void SetSortOrderVariable(bool NewOrder)
412 {
413 m_ascending = NewOrder;
414 }
415
416 private:
417 // common part of all ctors
418 void InitCommon(int width, wxAlignment align, int flags)
419 {
420 m_ascending = true;
421 m_flags = flags & ~wxDATAVIEW_COL_HIDDEN; // TODO
422 m_maxWidth = 30000;
423 m_minWidth = 0;
424 m_width = width >= 0 ? width : wxDVC_DEFAULT_WIDTH;
425 m_alignment = align;
426 }
427
428 bool m_ascending; // sorting order
429
430 int m_flags; // flags for the column
431 int m_maxWidth; // maximum width for the column
432 int m_minWidth; // minimum width for the column
433 int m_width; // column width
434
435 wxAlignment m_alignment; // column header alignment
436
437 wxDataViewColumnNativeData* m_NativeDataPtr; // storing environment dependent data for the native implementation
438
439 wxString m_title; // column title
440 };
441
442 //
443 // type definitions related to wxDataViewColumn
444 //
445 WX_DEFINE_ARRAY(wxDataViewColumn*,wxDataViewColumnPtrArrayType);
446
447 // ---------------------------------------------------------
448 // wxDataViewCtrl
449 // ---------------------------------------------------------
450 class WXDLLIMPEXP_ADV wxDataViewCtrl: public wxDataViewCtrlBase
451 {
452 public:
453 // Constructors / destructor:
454 wxDataViewCtrl()
455 {
456 Init();
457 }
458 wxDataViewCtrl(wxWindow *parent, wxWindowID id, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = 0,
459 const wxValidator& validator = wxDefaultValidator)
460 {
461 Init();
462 Create(parent, id, pos, size, style, validator );
463 }
464
465 ~wxDataViewCtrl();
466
467 // explicit control creation
468 bool Create(wxWindow *parent, wxWindowID id, const wxPoint& pos=wxDefaultPosition, const wxSize& size=wxDefaultSize, long style=0,
469 const wxValidator& validator=wxDefaultValidator);
470
471 virtual wxControl* GetMainWindow() // not used for the native implementation
472 {
473 return this;
474 }
475
476 // inherited methods from wxDataViewCtrlBase:
477 virtual bool AssociateModel(wxDataViewModel* model);
478
479 virtual bool AppendColumn (wxDataViewColumn* columnPtr);
480 virtual bool ClearColumns ();
481 virtual bool DeleteColumn (wxDataViewColumn* columnPtr);
482 virtual wxDataViewColumn* GetColumn (unsigned int pos) const;
483 virtual unsigned int GetColumnCount () const;
484 virtual int GetColumnPosition(const wxDataViewColumn* columnPtr) const;
485 virtual wxDataViewColumn* GetSortingColumn () const;
486 virtual bool InsertColumn (unsigned int pos, wxDataViewColumn *col);
487 virtual bool PrependColumn (wxDataViewColumn* columnPtr);
488
489 virtual void Collapse( const wxDataViewItem& item);
490 virtual void EnsureVisible(const wxDataViewItem& item, const wxDataViewColumn* columnPtr=NULL);
491 virtual void Expand(const wxDataViewItem& item);
492 virtual bool IsExpanded(const wxDataViewItem & item) const;
493
494
495 virtual unsigned int GetCount() const;
496 virtual wxRect GetItemRect(const wxDataViewItem& item, const wxDataViewColumn* columnPtr) const;
497 virtual wxDataViewItem GetSelection() const;
498 virtual int GetSelections(wxDataViewItemArray& sel) const;
499
500 virtual void HitTest(const wxPoint& point, wxDataViewItem& item, wxDataViewColumn*& columnPtr) const;
501
502 virtual bool IsSelected(const wxDataViewItem& item) const;
503
504 virtual void SelectAll();
505 virtual void Select(const wxDataViewItem& item);
506 virtual void SetSelections(const wxDataViewItemArray& sel);
507
508 virtual void Unselect(const wxDataViewItem& item);
509 virtual void UnselectAll();
510
511 //
512 // implementation
513 //
514 // returns a pointer to the native implementation
515 wxDataViewWidgetImpl* GetDataViewPeer() const;
516
517 // adds all children of the passed parent to the control; if 'parentItem' is invalid the root(s) is/are added:
518 void AddChildren(wxDataViewItem const& parentItem);
519
520 // finishes editing of custom items; if no custom item is currently edited the method does nothing
521 void FinishCustomItemEditing();
522
523 // returns the n-th pointer to a column;
524 // this method is different from GetColumn(unsigned int pos) because here 'n' is not a position in the control but the n-th
525 // position in the internal list/array of column pointers
526 wxDataViewColumn* GetColumnPtr(size_t n) const
527 {
528 return m_ColumnPtrs[n];
529 }
530 // returns the current being rendered item of the customized renderer (this item is only valid during editing)
531 wxDataViewItem const& GetCustomRendererItem() const
532 {
533 return m_CustomRendererItem;
534 }
535 // returns a pointer to a customized renderer (this pointer is only valid during editing)
536 wxDataViewCustomRenderer* GetCustomRendererPtr() const
537 {
538 return m_CustomRendererPtr;
539 }
540
541 // checks if currently a delete process is running
542 bool IsDeleting() const
543 {
544 return m_Deleting;
545 }
546
547 // with CG, we need to get the context from an kEventControlDraw event
548 // unfortunately, the DataBrowser callbacks don't provide the context
549 // and we need it, so we need to set/remove it before and after draw
550 // events so we can access it in the callbacks.
551 void MacSetDrawingContext(void* context)
552 {
553 m_cgContext = context;
554 }
555 void* MacGetDrawingContext() const
556 {
557 return m_cgContext;
558 }
559
560 // sets the currently being edited item of the custom renderer
561 void SetCustomRendererItem(wxDataViewItem const& NewItem)
562 {
563 m_CustomRendererItem = NewItem;
564 }
565 // sets the custom renderer
566 void SetCustomRendererPtr(wxDataViewCustomRenderer* NewCustomRendererPtr)
567 {
568 m_CustomRendererPtr = NewCustomRendererPtr;
569 }
570 // sets the flag indicating a deletion process:
571 void SetDeleting(bool deleting)
572 {
573 m_Deleting = deleting;
574 }
575
576 virtual wxVisualAttributes GetDefaultAttributes() const
577 {
578 return GetClassDefaultAttributes(GetWindowVariant());
579 }
580
581 static wxVisualAttributes
582 GetClassDefaultAttributes(wxWindowVariant variant = wxWINDOW_VARIANT_NORMAL);
583
584 protected:
585 // inherited methods from wxDataViewCtrlBase
586 virtual void DoSetExpanderColumn();
587 virtual void DoSetIndent();
588
589 virtual wxSize DoGetBestSize() const;
590
591 // event handling
592 void OnSize(wxSizeEvent &event);
593 void OnMouse(wxMouseEvent &event);
594
595 private:
596 // initializing of local variables:
597 void Init();
598
599 //
600 // variables
601 //
602 bool m_Deleting; // flag indicating if a delete process is running; this flag is necessary because the notifier indicating an item deletion in the model may be called
603 // after the actual deletion of the item; then, native callback functions/delegates may try to update data of variables that are already deleted;
604 // if this flag is set all native variable update requests will be ignored
605
606 void* m_cgContext; // pointer to core graphics context
607
608 wxDataViewCustomRenderer* m_CustomRendererPtr; // pointer to a valid custom renderer while editing; this class does NOT own the pointer
609
610 wxDataViewItem m_CustomRendererItem; // currently edited item by the customrenderer; it is invalid while not editing a custom item
611
612 wxDataViewColumnPtrArrayType m_ColumnPtrs; // all column pointers are stored in an array
613
614 // wxWidget internal stuff:
615 DECLARE_DYNAMIC_CLASS(wxDataViewCtrl)
616 DECLARE_NO_COPY_CLASS(wxDataViewCtrl)
617 DECLARE_EVENT_TABLE()
618 };
619
620 #endif // _WX_DATAVIEWCTRL_OSX_H_
621