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() const
105 return m_NativeColumnPtr
;
108 void SetNativeColumnPtr(NSTableColumn
* newNativeColumnPtr
)
110 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
)
134 wxDataViewRendererNativeData(NSCell
* initColumnCell
) : m_Object(NULL
), m_ColumnCell([initColumnCell retain
])
138 wxDataViewRendererNativeData(NSCell
* initColumnCell
, id initObject
) : m_Object([initObject retain
]), m_ColumnCell([initColumnCell retain
])
143 ~wxDataViewRendererNativeData()
145 [m_ColumnCell release
];
148 [m_origFont release
];
149 [m_origTextColour release
];
153 // data access methods
155 NSCell
* GetColumnCell() const
159 NSTableColumn
* GetColumnPtr() const
161 return m_TableColumnPtr
;
167 NSCell
* GetItemCell() const
176 void SetColumnCell(NSCell
* newCell
)
179 [m_ColumnCell release
];
180 m_ColumnCell
= newCell
;
182 void SetColumnPtr(NSTableColumn
* newColumnPtr
)
184 m_TableColumnPtr
= newColumnPtr
;
186 void SetItem(id newItem
)
190 void SetItemCell(NSCell
* newCell
)
192 m_ItemCell
= newCell
;
194 void SetObject(id newObject
)
198 m_Object
= newObject
;
201 // The original cell font and text colour stored here are NULL by default and
202 // are only initialized to the values retrieved from the cell when we change
203 // them from wxCocoaOutlineView:willDisplayCell:forTableColumn:item: which
204 // calls our SaveOriginalXXX() methods before changing the cell attributes.
206 // This allows us to avoid doing anything for the columns without any
207 // attributes but still be able to restore the correct attributes for the
209 NSFont
*GetOriginalFont() const { return m_origFont
; }
210 NSColor
*GetOriginalTextColour() const { return m_origTextColour
; }
212 void SaveOriginalFont(NSFont
*font
)
214 m_origFont
= [font retain
];
217 void SaveOriginalTextColour(NSColor
*textColour
)
219 m_origTextColour
= [textColour retain
];
222 // The ellipsization mode which we need to set for each cell being rendered.
223 void SetEllipsizeMode(wxEllipsizeMode mode
) { m_ellipsizeMode
= mode
; }
224 wxEllipsizeMode
GetEllipsizeMode() const { return m_ellipsizeMode
; }
226 // Set the line break mode for the given cell using our m_ellipsizeMode
227 void ApplyLineBreakMode(NSCell
*cell
);
230 // common part of all ctors
233 id m_Item
; // item NOT owned by renderer
234 id m_Object
; // object that can be used by renderer for storing special data (owned by renderer)
236 NSCell
* m_ColumnCell
; // column's cell is owned by renderer
237 NSCell
* m_ItemCell
; // item's cell is NOT owned by renderer
239 NSTableColumn
* m_TableColumnPtr
; // column NOT owned by renderer
241 // we own those if they're non-NULL
243 NSColor
*m_origTextColour
;
245 wxEllipsizeMode m_ellipsizeMode
;
248 // ============================================================================
249 // wxCocoaOutlineDataSource
250 // ============================================================================
252 // This class implements the data source delegate for the outline view.
253 // As only an informal protocol exists this class inherits from NSObject only.
255 // As mentioned in the documentation for NSOutlineView the native control does
256 // not own any data. Therefore, it has to be done by the data source.
257 // Unfortunately, wxWidget's data source is a C++ data source but
258 // NSOutlineDataSource requires objects as data. Therefore, the data (or better
259 // the native item objects) have to be stored additionally in the native data
261 // NSOutlineView requires quick access to the item objects and quick linear
262 // access to an item's children. This requires normally a hash type of storage
263 // for the item object itself and an array structure for each item's children.
264 // This means that basically two times the whole structure of wxWidget's model
265 // class has to be stored.
266 // This implementation is using a compromise: all items that are in use by the
267 // control are stored in a set (from there they can be easily retrieved) and
268 // owned by the set. Furthermore, children of the last parent are stored
271 @interface wxCocoaOutlineDataSource
: NSObject
273 NSArray
* sortDescriptors
; // descriptors specifying the sorting (currently the array only holds one object only)
275 NSMutableArray
* children
; // buffered children
277 NSMutableSet
* items
; // stores all items that are in use by the control
279 wxCocoaDataViewControl
* implementation
;
281 wxDataViewModel
* model
;
283 wxPointerObject
* currentParentItem
; // parent of the buffered children; the object is owned
287 // methods of informal protocol:
289 -(BOOL
) outlineView
:(NSOutlineView
*)outlineView acceptDrop
:(id
<NSDraggingInfo
>)info item
:(id
)item childIndex
:(NSInteger
)index
;
290 -(id
) outlineView
:(NSOutlineView
*)outlineView child
:(NSInteger
)index ofItem
:(id
)item
;
291 -(id
) outlineView
:(NSOutlineView
*)outlineView objectValueForTableColumn
:(NSTableColumn
*)tableColumn byItem
:(id
)item
;
292 -(BOOL
) outlineView
:(NSOutlineView
*)outlineView isItemExpandable
:(id
)item
;
293 -(NSInteger
) outlineView
:(NSOutlineView
*)outlineView numberOfChildrenOfItem
:(id
)item
;
294 -(NSDragOperation
) outlineView
:(NSOutlineView
*)outlineView validateDrop
:(id
<NSDraggingInfo
>)info proposedItem
:(id
)item proposedChildIndex
:(NSInteger
)index
;
295 -(BOOL
) outlineView
:(NSOutlineView
*)outlineView writeItems
:(NSArray
*)items toPasteboard
:(NSPasteboard
*)pasteboard
;
298 // buffer for items handling
300 -(void) addToBuffer
:(wxPointerObject
*)item
;
302 -(wxPointerObject
*) getDataViewItemFromBuffer
:(wxDataViewItem
const&)item
; // returns the item in the buffer that has got the same pointer as "item",
303 -(wxPointerObject
*) getItemFromBuffer
:(wxPointerObject
*)item
; // if such an item does not exist nil is returned
304 -(BOOL
) isInBuffer
:(wxPointerObject
*)item
;
305 -(void) removeFromBuffer
:(wxPointerObject
*)item
;
308 // buffered children handling
310 -(void) appendChild
:(wxPointerObject
*)item
;
311 -(void) clearChildren
;
312 -(wxPointerObject
*) getChild
:(NSUInteger
)index
;
313 -(NSUInteger
) getChildCount
;
314 -(void) removeChild
:(NSUInteger
)index
;
319 -(void) clearBuffers
;
324 -(NSArray
*) sortDescriptors
;
325 -(void) setSortDescriptors
:(NSArray
*)newSortDescriptors
;
328 // access to wxWidget's variables
330 -(wxPointerObject
*) currentParentItem
;
331 -(wxCocoaDataViewControl
*) implementation
;
332 -(wxDataViewModel
*) model
;
333 -(void) setCurrentParentItem
:(wxPointerObject
*)newCurrentParentItem
;
334 -(void) setImplementation
:(wxCocoaDataViewControl
*)newImplementation
;
335 -(void) setModel
:(wxDataViewModel
*)newModel
;
340 -(void) bufferItem
:(wxPointerObject
*)parentItem withChildren
:(wxDataViewItemArray
*)dataViewChildrenPtr
;
344 // ============================================================================
346 // ============================================================================
348 // This is a cell that is used for custom renderers.
350 @interface wxCustomCell
: NSTextFieldCell
361 // ============================================================================
363 // ============================================================================
365 // As the native cocoa environment does not have a cell displaying an icon/
366 // image and text at the same time, it has to be implemented by the user.
367 // This implementation follows the implementation of Chuck Pisula in Apple's
368 // DragNDropOutline sample application.
369 // Although in wxDataViewCtrl icons are used on OSX icons do not exist for
370 // display. Therefore, the cell is also called wxImageTextCell.
371 // Instead of displaying images of any size (which is possible) this cell uses
372 // a fixed size for displaying the image. Larger images are scaled to fit
373 // into their reserved space. Smaller or not existing images use the fixed
374 // reserved size and are scaled if necessary.
376 @interface wxImageTextCell
: NSTextFieldCell
379 CGFloat xImageShift
; // shift for the image in x-direction from border
380 CGFloat spaceImageText
; // space between image and text ("belongs" to the image)
382 NSImage
* image
; // the image itself
384 NSSize imageSize
; // largest size of the image; default size is (16, 16)
386 NSTextAlignment cellAlignment
; // the text alignment is used to align the whole
387 // cell (image and text)
393 -(NSTextAlignment
) alignment
;
394 -(void) setAlignment
:(NSTextAlignment
)newAlignment
;
400 -(void) setImage
:(NSImage
*)newImage
;
406 -(void) setImageSize
:(NSSize
) newImageSize
;
415 // ============================================================================
416 // wxCocoaOutlineView
417 // ============================================================================
418 @interface wxCocoaOutlineView
: NSOutlineView
421 BOOL isEditingCell
; // flag indicating if a cell is currently being edited
423 wxCocoaDataViewControl
* implementation
;
427 // access to wxWidget's implementation
429 -(wxCocoaDataViewControl
*) implementation
;
430 -(void) setImplementation
:(wxCocoaDataViewControl
*) newImplementation
;
434 // ============================================================================
435 // wxCocoaDataViewControl
436 // ============================================================================
438 // This is the internal interface class between wxDataViewCtrl (wxWidget) and
439 // the native source view (Mac OS X cocoa).
441 class wxCocoaDataViewControl
: public wxWidgetCocoaImpl
, public wxDataViewWidgetImpl
445 // constructors / destructor
447 wxCocoaDataViewControl(wxWindow
* peer
, wxPoint
const& pos
, wxSize
const& size
, long style
);
448 ~wxCocoaDataViewControl();
451 // column related methods (inherited from wxDataViewWidgetImpl)
453 virtual bool ClearColumns ();
454 virtual bool DeleteColumn (wxDataViewColumn
* columnPtr
);
455 virtual void DoSetExpanderColumn(wxDataViewColumn
const* columnPtr
);
456 virtual wxDataViewColumn
* GetColumn (unsigned int pos
) const;
457 virtual int GetColumnPosition (wxDataViewColumn
const* columnPtr
) const;
458 virtual bool InsertColumn (unsigned int pos
, wxDataViewColumn
* columnPtr
);
461 // item related methods (inherited from wxDataViewWidgetImpl)
463 virtual bool Add (wxDataViewItem
const& parent
, wxDataViewItem
const& item
);
464 virtual bool Add (wxDataViewItem
const& parent
, wxDataViewItemArray
const& items
);
465 virtual void Collapse (wxDataViewItem
const& item
);
466 virtual void EnsureVisible(wxDataViewItem
const& item
, wxDataViewColumn
const* columnPtr
);
467 virtual void Expand (wxDataViewItem
const& item
);
468 virtual unsigned int GetCount () const;
469 virtual wxRect
GetRectangle (wxDataViewItem
const& item
, wxDataViewColumn
const* columnPtr
);
470 virtual bool IsExpanded (wxDataViewItem
const& item
) const;
471 virtual bool Reload ();
472 virtual bool Remove (wxDataViewItem
const& parent
, wxDataViewItem
const& item
);
473 virtual bool Remove (wxDataViewItem
const& parent
, wxDataViewItemArray
const& item
);
474 virtual bool Update (wxDataViewColumn
const* columnPtr
);
475 virtual bool Update (wxDataViewItem
const& parent
, wxDataViewItem
const& item
);
476 virtual bool Update (wxDataViewItem
const& parent
, wxDataViewItemArray
const& items
);
479 // model related methods
481 virtual bool AssociateModel(wxDataViewModel
* model
); // informs the native control that a model is present
484 // selection related methods (inherited from wxDataViewWidgetImpl)
486 virtual int GetSelections(wxDataViewItemArray
& sel
) const;
487 virtual bool IsSelected (wxDataViewItem
const& item
) const;
488 virtual void Select (wxDataViewItem
const& item
);
489 virtual void SelectAll ();
490 virtual void Unselect (wxDataViewItem
const& item
);
491 virtual void UnselectAll ();
494 // sorting related methods
496 virtual wxDataViewColumn
* GetSortingColumn () const;
497 virtual void Resort ();
500 // other methods (inherited from wxDataViewWidgetImpl)
502 virtual void DoSetIndent (int indent
);
503 virtual void HitTest (wxPoint
const& point
, wxDataViewItem
& item
, wxDataViewColumn
*& columnPtr
) const;
504 virtual void SetRowHeight(wxDataViewItem
const& item
, unsigned int height
);
505 virtual void OnSize ();
510 wxDataViewCtrl
* GetDataViewCtrl() const
512 return dynamic_cast<wxDataViewCtrl
*>(GetWXPeer());
516 // drag & drop helper methods
518 wxDataFormat
GetDnDDataFormat(wxDataObjectComposite
* dataObjects
);
519 wxDataObjectComposite
* GetDnDDataObjects(NSData
* dataObject
) const; // create the data objects from the native dragged object
525 wxCocoaOutlineDataSource
* m_DataSource
;
527 wxCocoaOutlineView
* m_OutlineView
;
530 typedef wxCocoaDataViewControl
* wxCocoaDataViewControlPointer
;
534 #endif // _WX_DATAVIEWCTRL_COCOOA_H_