]> git.saurik.com Git - wxWidgets.git/blob - include/wx/osx/dataview.h
Use virtual functions to convert NSObject to the correct type in 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(NSObject *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 #if wxOSX_USE_COCOA
201 virtual void OSXOnCellChanged(NSObject *value,
202 const wxDataViewItem& item,
203 unsigned col);
204 #endif // Cocoa
205
206 private:
207 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewTextRenderer)
208 };
209
210 // ---------------------------------------------------------
211 // wxDataViewBitmapRenderer
212 // ---------------------------------------------------------
213
214 class WXDLLIMPEXP_ADV wxDataViewBitmapRenderer: public wxDataViewRenderer
215 {
216 public:
217 //
218 // constructors / destructor
219 //
220 wxDataViewBitmapRenderer(wxString const& varianttype=wxT("wxBitmap"), wxDataViewCellMode mode=wxDATAVIEW_CELL_INERT, int align=wxDVR_DEFAULT_ALIGNMENT);
221
222 //
223 // inherited functions from wxDataViewRenderer
224 //
225 virtual bool MacRender();
226
227 protected:
228 private:
229 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewBitmapRenderer)
230 };
231
232 #if !defined(wxUSE_GENERICDATAVIEWCTRL) && defined(__WXOSX_COCOA__)
233
234 // -------------------------------------
235 // wxDataViewChoiceRenderer
236 // -------------------------------------
237 class WXDLLIMPEXP_ADV wxDataViewChoiceRenderer: public wxDataViewRenderer
238 {
239 public:
240 //
241 // constructors / destructor
242 //
243 wxDataViewChoiceRenderer(wxArrayString const& choices,
244 wxDataViewCellMode mode = wxDATAVIEW_CELL_EDITABLE,
245 int alignment = wxDVR_DEFAULT_ALIGNMENT );
246
247 //
248 // inherited functions from wxDataViewRenderer
249 //
250 virtual bool MacRender();
251
252 //
253 // implementation
254 //
255 wxString GetChoice(size_t index) const
256 {
257 return m_Choices[index];
258 }
259 wxArrayString const& GetChoices() const
260 {
261 return m_Choices;
262 }
263
264 private:
265 //
266 // variables
267 //
268 wxArrayString m_Choices;
269
270 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewChoiceRenderer)
271 };
272
273 #endif
274
275 // ---------------------------------------------------------
276 // wxDataViewIconTextRenderer
277 // ---------------------------------------------------------
278 class WXDLLIMPEXP_ADV wxDataViewIconTextRenderer: public wxDataViewRenderer
279 {
280 public:
281 wxDataViewIconTextRenderer(wxString const& varianttype = wxT("wxDataViewIconText"), wxDataViewCellMode mode = wxDATAVIEW_CELL_INERT, int align=wxDVR_DEFAULT_ALIGNMENT);
282
283 //
284 // inherited functions from wxDataViewRenderer
285 //
286 virtual bool MacRender();
287
288 #if wxOSX_USE_COCOA
289 virtual void OSXOnCellChanged(NSObject *value,
290 const wxDataViewItem& item,
291 unsigned col);
292 #endif // Cocoa
293
294 protected:
295 private:
296 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewIconTextRenderer)
297 };
298
299 // ---------------------------------------------------------
300 // wxDataViewToggleRenderer
301 // ---------------------------------------------------------
302
303 class WXDLLIMPEXP_ADV wxDataViewToggleRenderer: public wxDataViewRenderer
304 {
305 public:
306 wxDataViewToggleRenderer(wxString const& varianttype = wxT("bool"), wxDataViewCellMode mode = wxDATAVIEW_CELL_INERT, int align=wxDVR_DEFAULT_ALIGNMENT);
307
308 //
309 // inherited functions from wxDataViewRenderer
310 //
311 virtual bool MacRender();
312
313 #if wxOSX_USE_COCOA
314 virtual void OSXOnCellChanged(NSObject *value,
315 const wxDataViewItem& item,
316 unsigned col);
317 #endif // Cocoa
318
319 private:
320 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewToggleRenderer)
321 };
322
323 // ---------------------------------------------------------
324 // wxDataViewProgressRenderer
325 // ---------------------------------------------------------
326
327 class WXDLLIMPEXP_ADV wxDataViewProgressRenderer: public wxDataViewRenderer
328 {
329 public:
330 wxDataViewProgressRenderer(wxString const& label = wxEmptyString, wxString const& varianttype=wxT("long"),
331 wxDataViewCellMode mode=wxDATAVIEW_CELL_INERT, int align=wxDVR_DEFAULT_ALIGNMENT);
332
333 //
334 // inherited functions from wxDataViewRenderer
335 //
336 virtual bool MacRender();
337
338 #if wxOSX_USE_COCOA
339 virtual void OSXOnCellChanged(NSObject *value,
340 const wxDataViewItem& item,
341 unsigned col);
342 #endif // Cocoa
343
344 private:
345 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewProgressRenderer)
346 };
347
348 // ---------------------------------------------------------
349 // wxDataViewDateRenderer
350 // ---------------------------------------------------------
351
352 class WXDLLIMPEXP_ADV wxDataViewDateRenderer: public wxDataViewRenderer
353 {
354 public:
355 wxDataViewDateRenderer(wxString const& varianttype=wxT("datetime"), wxDataViewCellMode mode=wxDATAVIEW_CELL_ACTIVATABLE, int align=wxDVR_DEFAULT_ALIGNMENT);
356
357 //
358 // inherited functions from wxDataViewRenderer
359 //
360 virtual bool MacRender();
361
362 #if wxOSX_USE_COCOA
363 virtual void OSXOnCellChanged(NSObject *value,
364 const wxDataViewItem& item,
365 unsigned col);
366 #endif // Cocoa
367
368 private:
369 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewDateRenderer)
370 };
371
372 // ---------------------------------------------------------
373 // wxDataViewColumn
374 // ---------------------------------------------------------
375
376 class WXDLLIMPEXP_ADV wxDataViewColumn: public wxDataViewColumnBase
377 {
378 public:
379 // constructors / destructor
380 wxDataViewColumn(const wxString& title,
381 wxDataViewRenderer* renderer,
382 unsigned int model_column,
383 int width = wxDVC_DEFAULT_WIDTH,
384 wxAlignment align = wxALIGN_CENTER,
385 int flags = wxDATAVIEW_COL_RESIZABLE);
386 wxDataViewColumn(const wxBitmap& bitmap,
387 wxDataViewRenderer* renderer,
388 unsigned int model_column,
389 int width = wxDVC_DEFAULT_WIDTH,
390 wxAlignment align = wxALIGN_CENTER,
391 int flags = wxDATAVIEW_COL_RESIZABLE);
392 virtual ~wxDataViewColumn();
393
394 // implement wxHeaderColumnBase pure virtual methods
395 virtual wxAlignment GetAlignment() const { return m_alignment; }
396 virtual int GetFlags() const { return m_flags; }
397 virtual int GetMaxWidth() const { return m_maxWidth; }
398 virtual int GetMinWidth() const { return m_minWidth; }
399 virtual wxString GetTitle() const { return m_title; }
400 virtual int GetWidth() const { return m_width; }
401 virtual bool IsHidden() const { return false; } // TODO
402 virtual bool IsSortOrderAscending() const { return m_ascending; }
403 virtual bool IsSortKey() const;
404
405 virtual void SetAlignment (wxAlignment align);
406 virtual void SetBitmap (wxBitmap const& bitmap);
407 virtual void SetFlags (int flags) { SetIndividualFlags(flags); }
408 virtual void SetHidden (bool WXUNUSED(hidden)) { } // TODO
409 virtual void SetMaxWidth (int maxWidth);
410 virtual void SetMinWidth (int minWidth);
411 virtual void SetReorderable(bool reorderable);
412 virtual void SetResizeable (bool resizeable);
413 virtual void SetSortable (bool sortable);
414 virtual void SetSortOrder (bool ascending);
415 virtual void SetTitle (wxString const& title);
416 virtual void SetWidth (int width);
417 virtual void SetAsSortKey (bool sort = true);
418
419 // implementation only
420 wxDataViewColumnNativeData* GetNativeData() const
421 {
422 return m_NativeDataPtr;
423 }
424
425 void SetNativeData(wxDataViewColumnNativeData* newNativeDataPtr); // class takes ownership of pointer
426 void SetWidthVariable(int NewWidth)
427 {
428 m_width = NewWidth;
429 }
430 void SetSortOrderVariable(bool NewOrder)
431 {
432 m_ascending = NewOrder;
433 }
434
435 private:
436 // common part of all ctors
437 void InitCommon(int width, wxAlignment align, int flags)
438 {
439 m_ascending = true;
440 m_flags = flags & ~wxDATAVIEW_COL_HIDDEN; // TODO
441 m_maxWidth = 30000;
442 m_minWidth = 0;
443 m_width = width >= 0 ? width : wxDVC_DEFAULT_WIDTH;
444 m_alignment = align;
445 }
446
447 bool m_ascending; // sorting order
448
449 int m_flags; // flags for the column
450 int m_maxWidth; // maximum width for the column
451 int m_minWidth; // minimum width for the column
452 int m_width; // column width
453
454 wxAlignment m_alignment; // column header alignment
455
456 wxDataViewColumnNativeData* m_NativeDataPtr; // storing environment dependent data for the native implementation
457
458 wxString m_title; // column title
459 };
460
461 //
462 // type definitions related to wxDataViewColumn
463 //
464 WX_DEFINE_ARRAY(wxDataViewColumn*,wxDataViewColumnPtrArrayType);
465
466 // ---------------------------------------------------------
467 // wxDataViewCtrl
468 // ---------------------------------------------------------
469 class WXDLLIMPEXP_ADV wxDataViewCtrl: public wxDataViewCtrlBase
470 {
471 public:
472 // Constructors / destructor:
473 wxDataViewCtrl()
474 {
475 Init();
476 }
477 wxDataViewCtrl(wxWindow *parent, wxWindowID id, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = 0,
478 const wxValidator& validator = wxDefaultValidator)
479 {
480 Init();
481 Create(parent, id, pos, size, style, validator );
482 }
483
484 ~wxDataViewCtrl();
485
486 // explicit control creation
487 bool Create(wxWindow *parent, wxWindowID id, const wxPoint& pos=wxDefaultPosition, const wxSize& size=wxDefaultSize, long style=0,
488 const wxValidator& validator=wxDefaultValidator);
489
490 virtual wxControl* GetMainWindow() // not used for the native implementation
491 {
492 return this;
493 }
494
495 // inherited methods from wxDataViewCtrlBase:
496 virtual bool AssociateModel(wxDataViewModel* model);
497
498 virtual bool AppendColumn (wxDataViewColumn* columnPtr);
499 virtual bool ClearColumns ();
500 virtual bool DeleteColumn (wxDataViewColumn* columnPtr);
501 virtual wxDataViewColumn* GetColumn (unsigned int pos) const;
502 virtual unsigned int GetColumnCount () const;
503 virtual int GetColumnPosition(const wxDataViewColumn* columnPtr) const;
504 virtual wxDataViewColumn* GetSortingColumn () const;
505 virtual bool InsertColumn (unsigned int pos, wxDataViewColumn *col);
506 virtual bool PrependColumn (wxDataViewColumn* columnPtr);
507
508 virtual void Collapse( const wxDataViewItem& item);
509 virtual void EnsureVisible(const wxDataViewItem& item, const wxDataViewColumn* columnPtr=NULL);
510 virtual void Expand(const wxDataViewItem& item);
511 virtual bool IsExpanded(const wxDataViewItem & item) const;
512
513
514 virtual unsigned int GetCount() const;
515 virtual wxRect GetItemRect(const wxDataViewItem& item, const wxDataViewColumn* columnPtr) const;
516 virtual wxDataViewItem GetSelection() const;
517 virtual int GetSelections(wxDataViewItemArray& sel) const;
518
519 virtual void HitTest(const wxPoint& point, wxDataViewItem& item, wxDataViewColumn*& columnPtr) const;
520
521 virtual bool IsSelected(const wxDataViewItem& item) const;
522
523 virtual void SelectAll();
524 virtual void Select(const wxDataViewItem& item);
525 virtual void SetSelections(const wxDataViewItemArray& sel);
526
527 virtual void Unselect(const wxDataViewItem& item);
528 virtual void UnselectAll();
529
530 //
531 // implementation
532 //
533 // returns a pointer to the native implementation
534 wxDataViewWidgetImpl* GetDataViewPeer() const;
535
536 // adds all children of the passed parent to the control; if 'parentItem' is invalid the root(s) is/are added:
537 void AddChildren(wxDataViewItem const& parentItem);
538
539 // finishes editing of custom items; if no custom item is currently edited the method does nothing
540 void FinishCustomItemEditing();
541
542 // returns the n-th pointer to a column;
543 // this method is different from GetColumn(unsigned int pos) because here 'n' is not a position in the control but the n-th
544 // position in the internal list/array of column pointers
545 wxDataViewColumn* GetColumnPtr(size_t n) const
546 {
547 return m_ColumnPtrs[n];
548 }
549 // returns the current being rendered item of the customized renderer (this item is only valid during editing)
550 wxDataViewItem const& GetCustomRendererItem() const
551 {
552 return m_CustomRendererItem;
553 }
554 // returns a pointer to a customized renderer (this pointer is only valid during editing)
555 wxDataViewCustomRenderer* GetCustomRendererPtr() const
556 {
557 return m_CustomRendererPtr;
558 }
559
560 // checks if currently a delete process is running
561 bool IsDeleting() const
562 {
563 return m_Deleting;
564 }
565
566 // with CG, we need to get the context from an kEventControlDraw event
567 // unfortunately, the DataBrowser callbacks don't provide the context
568 // and we need it, so we need to set/remove it before and after draw
569 // events so we can access it in the callbacks.
570 void MacSetDrawingContext(void* context)
571 {
572 m_cgContext = context;
573 }
574 void* MacGetDrawingContext() const
575 {
576 return m_cgContext;
577 }
578
579 // sets the currently being edited item of the custom renderer
580 void SetCustomRendererItem(wxDataViewItem const& NewItem)
581 {
582 m_CustomRendererItem = NewItem;
583 }
584 // sets the custom renderer
585 void SetCustomRendererPtr(wxDataViewCustomRenderer* NewCustomRendererPtr)
586 {
587 m_CustomRendererPtr = NewCustomRendererPtr;
588 }
589 // sets the flag indicating a deletion process:
590 void SetDeleting(bool deleting)
591 {
592 m_Deleting = deleting;
593 }
594
595 virtual wxVisualAttributes GetDefaultAttributes() const
596 {
597 return GetClassDefaultAttributes(GetWindowVariant());
598 }
599
600 static wxVisualAttributes
601 GetClassDefaultAttributes(wxWindowVariant variant = wxWINDOW_VARIANT_NORMAL);
602
603 protected:
604 // inherited methods from wxDataViewCtrlBase
605 virtual void DoSetExpanderColumn();
606 virtual void DoSetIndent();
607
608 virtual wxSize DoGetBestSize() const;
609
610 // event handling
611 void OnSize(wxSizeEvent &event);
612 void OnMouse(wxMouseEvent &event);
613
614 private:
615 // initializing of local variables:
616 void Init();
617
618 //
619 // variables
620 //
621 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
622 // after the actual deletion of the item; then, native callback functions/delegates may try to update data of variables that are already deleted;
623 // if this flag is set all native variable update requests will be ignored
624
625 void* m_cgContext; // pointer to core graphics context
626
627 wxDataViewCustomRenderer* m_CustomRendererPtr; // pointer to a valid custom renderer while editing; this class does NOT own the pointer
628
629 wxDataViewItem m_CustomRendererItem; // currently edited item by the customrenderer; it is invalid while not editing a custom item
630
631 wxDataViewColumnPtrArrayType m_ColumnPtrs; // all column pointers are stored in an array
632
633 // wxWidget internal stuff:
634 DECLARE_DYNAMIC_CLASS(wxDataViewCtrl)
635 DECLARE_NO_COPY_CLASS(wxDataViewCtrl)
636 DECLARE_EVENT_TABLE()
637 };
638
639 #endif // _WX_DATAVIEWCTRL_OSX_H_
640