1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/osx/carbon/dataview.h
3 // Purpose: wxDataViewCtrl native implementation header for carbon
5 // Id: $Id: dataview.h 57374 2009-01-27
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #ifndef _WX_DATAVIEWCTRL_COCOOA_H_
11 #define _WX_DATAVIEWCTRL_COCOOA_H_
18 #import <Cocoa/Cocoa.h>
21 #include "wx/osx/core/dataview.h"
22 #include "wx/osx/private.h"
24 // Forward declaration
25 class wxCocoaDataViewControl
;
27 // ============================================================================
29 // ============================================================================
31 // This is a helper class to store a pointer in an object. This object just
32 // stores the pointer but does not take any ownership.
33 // To pointer objects are equal if the containing pointers are equal. This
34 // means also that the hash value of a pointer object depends only on the
37 @interface wxPointerObject
: NSObject
43 // object initialization
45 -(id
) initWithPointer
:(void*)initPointer
;
51 -(void) setPointer
:(void*)newPointer
;
55 // ============================================================================
56 // wxSortDescriptorObject
57 // ============================================================================
59 // This is a helper class to use native sorting facilities.
61 @interface wxSortDescriptorObject
: NSSortDescriptor
<NSCopying
>
63 wxDataViewColumn
* columnPtr
; // pointer to the sorting column
65 wxDataViewModel
* modelPtr
; // pointer to model
71 -(id
) initWithModelPtr
:(wxDataViewModel
*)initModelPtr sortingColumnPtr
:(wxDataViewColumn
*)initColumnPtr ascending
:(BOOL
)sortAscending
;
74 // access to variables
76 -(wxDataViewColumn
*) columnPtr
;
77 -(wxDataViewModel
*) modelPtr
;
79 -(void) setColumnPtr
:(wxDataViewColumn
*)newColumnPtr
;
80 -(void) setModelPtr
:(wxDataViewModel
*)newModelPtr
;
84 // ============================================================================
85 // wxDataViewColumnNativeData
86 // ============================================================================
87 class wxDataViewColumnNativeData
91 // constructors / destructor
93 wxDataViewColumnNativeData(void) : m_NativeColumnPtr(NULL
)
96 wxDataViewColumnNativeData(NSTableColumn
* initNativeColumnPtr
) : m_NativeColumnPtr(initNativeColumnPtr
)
101 // data access methods
103 NSTableColumn
* GetNativeColumnPtr(void) const
105 return this->m_NativeColumnPtr
;
108 void SetNativeColumnPtr(NSTableColumn
* newNativeColumnPtr
)
110 this->m_NativeColumnPtr
= newNativeColumnPtr
;
118 NSTableColumn
* m_NativeColumnPtr
; // this class does not take over ownership of the pointer nor retains it
121 // ============================================================================
122 // wxDataViewRendererNativeData
123 // ============================================================================
124 class wxDataViewRendererNativeData
128 // constructors / destructor
130 wxDataViewRendererNativeData(void) : m_Object(NULL
), m_ColumnCell(NULL
)
133 wxDataViewRendererNativeData(NSCell
* initColumnCell
) : m_Object(NULL
), m_ColumnCell([initColumnCell retain
])
136 wxDataViewRendererNativeData(NSCell
* initColumnCell
, id initObject
) : m_Object([initObject retain
]), m_ColumnCell([initColumnCell retain
])
139 ~wxDataViewRendererNativeData(void)
141 [this->m_ColumnCell release
];
142 [this->m_Object release
];
146 // data access methods
148 NSCell
* GetColumnCell(void) const
150 return this->m_ColumnCell
;
152 NSTableColumn
* GetColumnPtr(void) const
154 return this->m_TableColumnPtr
;
156 id
GetItem(void) const
160 NSCell
* GetItemCell(void) const
162 return this->m_ItemCell
;
164 id
GetObject(void) const
166 return this->m_Object
;
169 void SetColumnCell(NSCell
* newCell
)
172 [this->m_ColumnCell release
];
173 this->m_ColumnCell
= newCell
;
175 void SetColumnPtr(NSTableColumn
* newColumnPtr
)
177 this->m_TableColumnPtr
= newColumnPtr
;
179 void SetItem(id newItem
)
181 this->m_Item
= newItem
;
183 void SetItemCell(NSCell
* newCell
)
185 this->m_ItemCell
= newCell
;
187 void SetObject(id newObject
)
190 [this->m_Object release
];
191 this->m_Object
= newObject
;
199 id m_Item
; // item NOT owned by renderer
200 id m_Object
; // object that can be used by renderer for storing special data (owned by renderer)
202 NSCell
* m_ColumnCell
; // column's cell is owned by renderer
203 NSCell
* m_ItemCell
; // item's cell is NOT owned by renderer
205 NSTableColumn
* m_TableColumnPtr
; // column NOT owned by renderer
208 // ============================================================================
209 // wxCocoaOutlineDataSource
210 // ============================================================================
212 // This class implements the data source delegate for the outline view.
213 // As only an informal protocol exists this class inherits from NSObject only.
215 // As mentioned in the documentation for NSOutlineView the native control does
216 // not own any data. Therefore, it has to be done by the data source.
217 // Unfortunately, wxWidget's data source is a C++ data source but
218 // NSOutlineDataSource requires objects as data. Therefore, the data (or better
219 // the native item objects) have to be stored additionally in the native data
221 // NSOutlineView requires quick access to the item objects and quick linear
222 // access to an item's children. This requires normally a hash type of storage
223 // for the item object itself and an array structure for each item's children.
224 // This means that basically two times the whole structure of wxWidget's model
225 // class has to be stored.
226 // This implementation is using a compromise: all items that are in use by the
227 // control are stored in a set (from there they can be easily retrieved) and
228 // owned by the set. Furthermore, children of the last parent are stored
231 @interface wxCocoaOutlineDataSource
: NSObject
233 NSArray
* sortDescriptors
; // descriptors specifying the sorting (currently the array only holds one object only)
235 NSMutableArray
* children
; // buffered children
237 NSMutableSet
* items
; // stores all items that are in use by the control
239 wxCocoaDataViewControl
* implementation
;
241 wxDataViewModel
* model
;
243 wxPointerObject
* currentParentItem
; // parent of the buffered children; the object is owned
247 // methods of informal protocol:
249 -(BOOL
) outlineView
:(NSOutlineView
*)outlineView acceptDrop
:(id
<NSDraggingInfo
>)info item
:(id
)item childIndex
:(NSInteger
)index
;
250 -(id
) outlineView
:(NSOutlineView
*)outlineView child
:(NSInteger
)index ofItem
:(id
)item
;
251 -(id
) outlineView
:(NSOutlineView
*)outlineView objectValueForTableColumn
:(NSTableColumn
*)tableColumn byItem
:(id
)item
;
252 -(BOOL
) outlineView
:(NSOutlineView
*)outlineView isItemExpandable
:(id
)item
;
253 -(NSInteger
) outlineView
:(NSOutlineView
*)outlineView numberOfChildrenOfItem
:(id
)item
;
254 -(NSDragOperation
) outlineView
:(NSOutlineView
*)outlineView validateDrop
:(id
<NSDraggingInfo
>)info proposedItem
:(id
)item proposedChildIndex
:(NSInteger
)index
;
255 -(BOOL
) outlineView
:(NSOutlineView
*)outlineView writeItems
:(NSArray
*)items toPasteboard
:(NSPasteboard
*)pasteboard
;
258 // buffer for items handling
260 -(void) addToBuffer
:(wxPointerObject
*)item
;
262 -(wxPointerObject
*) getDataViewItemFromBuffer
:(wxDataViewItem
const&)item
; // returns the item in the buffer that has got the same pointer as "item",
263 -(wxPointerObject
*) getItemFromBuffer
:(wxPointerObject
*)item
; // if such an item does not exist nil is returned
264 -(BOOL
) isInBuffer
:(wxPointerObject
*)item
;
265 -(void) removeFromBuffer
:(wxPointerObject
*)item
;
268 // buffered children handling
270 -(void) appendChild
:(wxPointerObject
*)item
;
271 -(void) clearChildren
;
272 -(wxPointerObject
*) getChild
:(NSUInteger
)index
;
273 -(NSUInteger
) getChildCount
;
274 -(void) removeChild
:(NSUInteger
)index
;
279 -(void) clearBuffers
;
284 -(NSArray
*) sortDescriptors
;
285 -(void) setSortDescriptors
:(NSArray
*)newSortDescriptors
;
288 // access to wxWidget's variables
290 -(wxPointerObject
*) currentParentItem
;
291 -(wxCocoaDataViewControl
*) implementation
;
292 -(wxDataViewModel
*) model
;
293 -(void) setCurrentParentItem
:(wxPointerObject
*)newCurrentParentItem
;
294 -(void) setImplementation
:(wxCocoaDataViewControl
*)newImplementation
;
295 -(void) setModel
:(wxDataViewModel
*)newModel
;
300 -(void) bufferItem
:(wxPointerObject
*)parentItem withChildren
:(wxDataViewItemArray
*)dataViewChildrenPtr
;
304 // ============================================================================
306 // ============================================================================
308 // This is a cell that is used for custom renderers.
310 @interface wxCustomCell
: NSTextFieldCell
321 // ============================================================================
323 // ============================================================================
325 // As the native cocoa environment does not have a cell displaying an icon/
326 // image and text at the same time, it has to be implemented by the user.
327 // This implementation follows the implementation of Chuck Pisula in Apple's
328 // DragNDropOutline sample application.
329 // Although in wxDataViewCtrl icons are used on OSX icons do not exist for
330 // display. Therefore, the cell is also called wxImageTextCell.
331 // Instead of displaying images of any size (which is possible) this cell uses
332 // a fixed size for displaying the image. Larger images are scaled to fit
333 // into their reserved space. Smaller or not existing images use the fixed
334 // reserved size and are scaled if necessary.
336 @interface wxImageTextCell
: NSTextFieldCell
339 CGFloat xImageShift
; // shift for the image in x-direction from border
340 CGFloat spaceImageText
; // space between image and text ("belongs" to the image)
342 NSImage
* image
; // the image itself
344 NSSize imageSize
; // largest size of the image; default size is (16, 16)
346 NSTextAlignment cellAlignment
; // the text alignment is used to align the whole
347 // cell (image and text)
353 -(NSTextAlignment
) alignment
;
354 -(void) setAlignment
:(NSTextAlignment
)newAlignment
;
360 -(void) setImage
:(NSImage
*)newImage
;
366 -(void) setImageSize
:(NSSize
) newImageSize
;
375 // ============================================================================
376 // wxCocoaOutlineView
377 // ============================================================================
378 @interface wxCocoaOutlineView
: NSOutlineView
381 BOOL isEditingCell
; // flag indicating if a cell is currently being edited
383 wxCocoaDataViewControl
* implementation
;
387 // access to wxWidget's implementation
389 -(wxCocoaDataViewControl
*) implementation
;
390 -(void) setImplementation
:(wxCocoaDataViewControl
*) newImplementation
;
394 // ============================================================================
395 // wxCocoaDataViewControl
396 // ============================================================================
398 // This is the internal interface class between wxDataViewCtrl (wxWidget) and
399 // the native source view (Mac OS X cocoa).
401 class wxCocoaDataViewControl
: public wxWidgetCocoaImpl
, public wxDataViewWidgetImpl
405 // constructors / destructor
407 wxCocoaDataViewControl(wxWindow
* peer
, wxPoint
const& pos
, wxSize
const& size
, long style
);
408 ~wxCocoaDataViewControl(void);
411 // column related methods (inherited from wxDataViewWidgetImpl)
413 virtual bool ClearColumns (void);
414 virtual bool DeleteColumn (wxDataViewColumn
* columnPtr
);
415 virtual void DoSetExpanderColumn(wxDataViewColumn
const* columnPtr
);
416 virtual wxDataViewColumn
* GetColumn (unsigned int pos
) const;
417 virtual int GetColumnPosition (wxDataViewColumn
const* columnPtr
) const;
418 virtual bool InsertColumn (unsigned int pos
, wxDataViewColumn
* columnPtr
);
421 // item related methods (inherited from wxDataViewWidgetImpl)
423 virtual bool Add (wxDataViewItem
const& parent
, wxDataViewItem
const& item
);
424 virtual bool Add (wxDataViewItem
const& parent
, wxDataViewItemArray
const& items
);
425 virtual void Collapse (wxDataViewItem
const& item
);
426 virtual void EnsureVisible(wxDataViewItem
const& item
, wxDataViewColumn
const* columnPtr
);
427 virtual void Expand (wxDataViewItem
const& item
);
428 virtual unsigned int GetCount (void) const;
429 virtual wxRect
GetRectangle (wxDataViewItem
const& item
, wxDataViewColumn
const* columnPtr
);
430 virtual bool IsExpanded (wxDataViewItem
const& item
) const;
431 virtual bool Reload (void);
432 virtual bool Remove (wxDataViewItem
const& parent
, wxDataViewItem
const& item
);
433 virtual bool Remove (wxDataViewItem
const& parent
, wxDataViewItemArray
const& item
);
434 virtual bool Update (wxDataViewColumn
const* columnPtr
);
435 virtual bool Update (wxDataViewItem
const& parent
, wxDataViewItem
const& item
);
436 virtual bool Update (wxDataViewItem
const& parent
, wxDataViewItemArray
const& items
);
439 // model related methods
441 virtual bool AssociateModel(wxDataViewModel
* model
); // informs the native control that a model is present
444 // selection related methods (inherited from wxDataViewWidgetImpl)
446 virtual int GetSelections(wxDataViewItemArray
& sel
) const;
447 virtual bool IsSelected (wxDataViewItem
const& item
) const;
448 virtual void Select (wxDataViewItem
const& item
);
449 virtual void SelectAll (void);
450 virtual void Unselect (wxDataViewItem
const& item
);
451 virtual void UnselectAll (void);
454 // sorting related methods
456 virtual wxDataViewColumn
* GetSortingColumn (void) const;
457 virtual void Resort (void);
460 // other methods (inherited from wxDataViewWidgetImpl)
462 virtual void DoSetIndent (int indent
);
463 virtual void HitTest (wxPoint
const& point
, wxDataViewItem
& item
, wxDataViewColumn
*& columnPtr
) const;
464 virtual void SetRowHeight(wxDataViewItem
const& item
, unsigned int height
);
465 virtual void OnSize (void);
470 wxDataViewCtrl
* GetDataViewCtrl(void) const
472 return dynamic_cast<wxDataViewCtrl
*>(this->GetWXPeer());
476 // drag & drop helper methods
478 wxDataFormat
GetDnDDataFormat(wxDataObjectComposite
* dataObjects
);
479 wxDataObjectComposite
* GetDnDDataObjects(NSData
* dataObject
) const; // create the data objects from the native dragged object
485 wxCocoaOutlineDataSource
* m_DataSource
;
487 wxCocoaOutlineView
* m_OutlineView
;
490 typedef wxCocoaDataViewControl
* wxCocoaDataViewControlPointer
;
494 #endif // _WX_DATAVIEWCTRL_COCOOA_H_