1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/osx/cocoa/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_
15 #import <Cocoa/Cocoa.h>
17 #include "wx/osx/core/dataview.h"
18 #include "wx/osx/private.h"
20 // Forward declaration
21 class wxCocoaDataViewControl
;
26 [vertical arrows indicate inheritance, horizontal -- aggregation]
29 wxWindow ---> wxWidgetCocoaImpl wxDataViewWidgetImpl NSOutlineView
34 wxDataViewCtrl -------> wxCocoaDataViewControl <-------> wxCocoaOutlineView
37 The right most classes are Objective-C only and can't be used from (pure)
41 // ============================================================================
42 // wxPointerObject: simply stores a pointer, without taking its ownership
43 // ============================================================================
45 // Two pointer objects are equal if the containing pointers are equal. This
46 // means also that the hash value of a pointer object depends only on the
49 @interface wxPointerObject
: NSObject
54 -(id
) initWithPointer
:(void*)initPointer
;
57 -(void) setPointer
:(void*)newPointer
;
60 // ============================================================================
61 // wxSortDescriptorObject: helper class to use native sorting facilities
62 // ============================================================================
64 @interface wxSortDescriptorObject
: NSSortDescriptor
<NSCopying
>
66 wxDataViewColumn
* columnPtr
; // pointer to the sorting column
68 wxDataViewModel
* modelPtr
; // pointer to model
72 initWithModelPtr
:(wxDataViewModel
*)initModelPtr
73 sortingColumnPtr
:(wxDataViewColumn
*)initColumnPtr
74 ascending
:(BOOL
)sortAscending
;
76 -(wxDataViewColumn
*) columnPtr
;
77 -(wxDataViewModel
*) modelPtr
;
79 -(void) setColumnPtr
:(wxDataViewColumn
*)newColumnPtr
;
80 -(void) setModelPtr
:(wxDataViewModel
*)newModelPtr
;
83 // ============================================================================
84 // wxDataViewColumnNativeData: extra data for wxDataViewColumn
85 // ============================================================================
87 class wxDataViewColumnNativeData
90 wxDataViewColumnNativeData() : m_NativeColumnPtr(NULL
)
94 wxDataViewColumnNativeData(NSTableColumn
* initNativeColumnPtr
)
95 : m_NativeColumnPtr(initNativeColumnPtr
)
99 NSTableColumn
* GetNativeColumnPtr() const
101 return m_NativeColumnPtr
;
104 void SetNativeColumnPtr(NSTableColumn
* newNativeColumnPtr
)
106 m_NativeColumnPtr
= newNativeColumnPtr
;
111 NSTableColumn
* m_NativeColumnPtr
;
114 // ============================================================================
115 // wxDataViewRendererNativeData: extra data for wxDataViewRenderer
116 // ============================================================================
118 class wxDataViewRendererNativeData
121 wxDataViewRendererNativeData()
122 : m_Object(NULL
), m_ColumnCell(NULL
)
127 wxDataViewRendererNativeData(NSCell
* initColumnCell
)
128 : m_Object(NULL
), m_ColumnCell([initColumnCell retain
])
133 wxDataViewRendererNativeData(NSCell
* initColumnCell
, id initObject
)
134 : m_Object([initObject retain
]), m_ColumnCell([initColumnCell retain
])
139 ~wxDataViewRendererNativeData()
141 [m_ColumnCell release
];
144 [m_origFont release
];
145 [m_origTextColour release
];
148 NSCell
* GetColumnCell() const { return m_ColumnCell
; }
149 NSTableColumn
* GetColumnPtr() const { return m_TableColumnPtr
; }
150 id
GetItem() const { return m_Item
; }
151 NSCell
* GetItemCell() const { return m_ItemCell
; }
152 id
GetObject() const { return m_Object
; }
154 void SetColumnCell(NSCell
* newCell
)
157 [m_ColumnCell release
];
158 m_ColumnCell
= newCell
;
160 void SetColumnPtr(NSTableColumn
* newColumnPtr
)
162 m_TableColumnPtr
= newColumnPtr
;
164 void SetItem(id newItem
)
168 void SetItemCell(NSCell
* newCell
)
170 m_ItemCell
= newCell
;
172 void SetObject(id newObject
)
176 m_Object
= newObject
;
179 // The original cell font and text colour stored here are NULL by default
180 // and are only initialized to the values retrieved from the cell when we
181 // change them from wxCocoaOutlineView:willDisplayCell:forTableColumn:item:
182 // which calls our SaveOriginalXXX() methods before changing the cell
185 // This allows us to avoid doing anything for the columns without any
186 // attributes but still be able to restore the correct attributes for the
188 NSFont
*GetOriginalFont() const { return m_origFont
; }
189 NSColor
*GetOriginalTextColour() const { return m_origTextColour
; }
191 void SaveOriginalFont(NSFont
*font
)
193 m_origFont
= [font retain
];
196 void SaveOriginalTextColour(NSColor
*textColour
)
198 m_origTextColour
= [textColour retain
];
201 // The ellipsization mode which we need to set for each cell being rendered.
202 void SetEllipsizeMode(wxEllipsizeMode mode
) { m_ellipsizeMode
= mode
; }
203 wxEllipsizeMode
GetEllipsizeMode() const { return m_ellipsizeMode
; }
205 // Set the line break mode for the given cell using our m_ellipsizeMode
206 void ApplyLineBreakMode(NSCell
*cell
);
209 // common part of all ctors
212 id m_Item
; // item NOT owned by renderer
214 // object that can be used by renderer for storing special data (owned by
218 NSCell
* m_ColumnCell
; // column's cell is owned by renderer
219 NSCell
* m_ItemCell
; // item's cell is NOT owned by renderer
221 NSTableColumn
* m_TableColumnPtr
; // column NOT owned by renderer
223 // we own those if they're non-NULL
225 NSColor
*m_origTextColour
;
227 wxEllipsizeMode m_ellipsizeMode
;
230 // ============================================================================
231 // wxCocoaOutlineDataSource
232 // ============================================================================
234 // This class implements the data source delegate for the outline view.
235 // As only an informal protocol exists this class inherits from NSObject only.
237 // As mentioned in the documentation for NSOutlineView the native control does
238 // not own any data. Therefore, it has to be done by the data source.
239 // Unfortunately, wxWidget's data source is a C++ data source but
240 // NSOutlineDataSource requires objects as data. Therefore, the data (or better
241 // the native item objects) have to be stored additionally in the native data
243 // NSOutlineView requires quick access to the item objects and quick linear
244 // access to an item's children. This requires normally a hash type of storage
245 // for the item object itself and an array structure for each item's children.
246 // This means that basically two times the whole structure of wxWidget's model
247 // class has to be stored.
248 // This implementation is using a compromise: all items that are in use by the
249 // control are stored in a set (from there they can be easily retrieved) and
250 // owned by the set. Furthermore, children of the last parent are stored
253 @interface wxCocoaOutlineDataSource
: NSObject
wxOSX_10_6_AND_LATER(<NSOutlineViewDataSource
>)
255 // descriptors specifying the sorting (currently the array only holds one
257 NSArray
* sortDescriptors
;
259 NSMutableArray
* children
; // buffered children
261 NSMutableSet
* items
; // stores all items that are in use by the control
263 wxCocoaDataViewControl
* implementation
;
265 wxDataViewModel
* model
;
267 // parent of the buffered children; the object is owned
268 wxPointerObject
* currentParentItem
;
271 // methods of informal protocol:
273 outlineView
:(NSOutlineView
*)outlineView
274 acceptDrop
:(id
<NSDraggingInfo
>)info
276 childIndex
:(NSInteger
)index
;
279 outlineView
:(NSOutlineView
*)outlineView
280 child
:(NSInteger
)index
284 outlineView
:(NSOutlineView
*)outlineView
285 objectValueForTableColumn
:(NSTableColumn
*)tableColumn
289 outlineView
:(NSOutlineView
*)outlineView
290 isItemExpandable
:(id
)item
;
293 outlineView
:(NSOutlineView
*)outlineView
294 numberOfChildrenOfItem
:(id
)item
;
297 outlineView
:(NSOutlineView
*)outlineView
298 validateDrop
:(id
<NSDraggingInfo
>)info
299 proposedItem
:(id
)item
300 proposedChildIndex
:(NSInteger
)index
;
303 outlineView
:(NSOutlineView
*)outlineView
304 writeItems
:(NSArray
*)items
305 toPasteboard
:(NSPasteboard
*)pasteboard
;
307 // buffer for items handling
308 -(void) addToBuffer
:(wxPointerObject
*)item
;
310 // returns the item in the buffer that has got the same pointer as "item",
311 // if such an item does not exist nil is returned
312 -(wxPointerObject
*) getDataViewItemFromBuffer
:(const wxDataViewItem
&)item
;
313 -(wxPointerObject
*) getItemFromBuffer
:(wxPointerObject
*)item
;
314 -(BOOL
) isInBuffer
:(wxPointerObject
*)item
;
315 -(void) removeFromBuffer
:(wxPointerObject
*)item
;
317 // buffered children handling
318 -(void) appendChild
:(wxPointerObject
*)item
;
319 -(void) clearChildren
;
320 -(wxPointerObject
*) getChild
:(NSUInteger
)index
;
321 -(NSUInteger
) getChildCount
;
322 -(void) removeChild
:(NSUInteger
)index
;
325 -(void) clearBuffers
;
328 -(NSArray
*) sortDescriptors
;
329 -(void) setSortDescriptors
:(NSArray
*)newSortDescriptors
;
331 // access to wxWidgets variables
332 -(wxPointerObject
*) currentParentItem
;
333 -(wxCocoaDataViewControl
*) implementation
;
334 -(wxDataViewModel
*) model
;
335 -(void) setCurrentParentItem
:(wxPointerObject
*)newCurrentParentItem
;
336 -(void) setImplementation
:(wxCocoaDataViewControl
*)newImplementation
;
337 -(void) setModel
:(wxDataViewModel
*)newModel
;
341 bufferItem
:(wxPointerObject
*)parentItem
342 withChildren
:(wxDataViewItemArray
*)dataViewChildrenPtr
;
345 // ============================================================================
346 // wxCustomCell: used for custom renderers
347 // ============================================================================
349 @interface wxCustomCell
: NSTextFieldCell
356 // ============================================================================
358 // ============================================================================
360 // As the native cocoa environment does not have a cell displaying an icon/
361 // image and text at the same time, it has to be implemented by the user.
362 // This implementation follows the implementation of Chuck Pisula in Apple's
363 // DragNDropOutline sample application.
364 // Although in wxDataViewCtrl icons are used on OSX icons do not exist for
365 // display. Therefore, the cell is also called wxImageTextCell.
366 // Instead of displaying images of any size (which is possible) this cell uses
367 // a fixed size for displaying the image. Larger images are scaled to fit
368 // into their reserved space. Smaller or not existing images use the fixed
369 // reserved size and are scaled if necessary.
371 @interface wxImageTextCell
: NSTextFieldCell
374 CGFloat xImageShift
; // shift for the image in x-direction from border
375 CGFloat spaceImageText
; // space between image and text
377 NSImage
* image
; // the image itself
379 NSSize imageSize
; // largest size of the image; default size is (16, 16)
381 // the text alignment is used to align the whole cell (image and text)
382 NSTextAlignment cellAlignment
;
385 -(NSTextAlignment
) alignment
;
386 -(void) setAlignment
:(NSTextAlignment
)newAlignment
;
389 -(void) setImage
:(NSImage
*)newImage
;
392 -(void) setImageSize
:(NSSize
) newImageSize
;
397 // ============================================================================
398 // wxCocoaOutlineView
399 // ============================================================================
401 @interface wxCocoaOutlineView
: NSOutlineView
wxOSX_10_6_AND_LATER(<NSOutlineViewDelegate
>)
404 // column and row of the cell being edited or -1 if none
405 int currentlyEditedColumn
,
408 wxCocoaDataViewControl
* implementation
;
411 -(wxCocoaDataViewControl
*) implementation
;
412 -(void) setImplementation
:(wxCocoaDataViewControl
*) newImplementation
;
415 // ============================================================================
416 // wxCocoaDataViewControl
417 // ============================================================================
419 // This is the internal interface class between wxDataViewCtrl (wxWidget) and
420 // the native source view (Mac OS X cocoa).
421 class wxCocoaDataViewControl
: public wxWidgetCocoaImpl
,
422 public wxDataViewWidgetImpl
425 // constructors / destructor
426 wxCocoaDataViewControl(wxWindow
* peer
,
430 virtual ~wxCocoaDataViewControl();
432 wxDataViewCtrl
* GetDataViewCtrl() const
434 return static_cast<wxDataViewCtrl
*>(GetWXPeer());
437 // column related methods (inherited from wxDataViewWidgetImpl)
438 virtual bool ClearColumns();
439 virtual bool DeleteColumn(wxDataViewColumn
* columnPtr
);
440 virtual void DoSetExpanderColumn(wxDataViewColumn
const* columnPtr
);
441 virtual wxDataViewColumn
* GetColumn(unsigned int pos
) const;
442 virtual int GetColumnPosition(wxDataViewColumn
const* columnPtr
) const;
443 virtual bool InsertColumn(unsigned int pos
, wxDataViewColumn
* columnPtr
);
444 virtual void FitColumnWidthToContent(unsigned int pos
);
446 // item related methods (inherited from wxDataViewWidgetImpl)
447 virtual bool Add(const wxDataViewItem
& parent
, const wxDataViewItem
& item
);
448 virtual bool Add(const wxDataViewItem
& parent
,
449 const wxDataViewItemArray
& items
);
450 virtual void Collapse(const wxDataViewItem
& item
);
451 virtual void EnsureVisible(const wxDataViewItem
& item
,
452 wxDataViewColumn
const* columnPtr
);
453 virtual void Expand(const wxDataViewItem
& item
);
454 virtual unsigned int GetCount() const;
455 virtual wxRect
GetRectangle(const wxDataViewItem
& item
,
456 wxDataViewColumn
const* columnPtr
);
457 virtual bool IsExpanded(const wxDataViewItem
& item
) const;
458 virtual bool Reload();
459 virtual bool Remove(const wxDataViewItem
& parent
,
460 const wxDataViewItem
& item
);
461 virtual bool Remove(const wxDataViewItem
& parent
,
462 const wxDataViewItemArray
& item
);
463 virtual bool Update(const wxDataViewColumn
* columnPtr
);
464 virtual bool Update(const wxDataViewItem
& parent
,
465 const wxDataViewItem
& item
);
466 virtual bool Update(const wxDataViewItem
& parent
,
467 const wxDataViewItemArray
& items
);
469 // model related methods
470 virtual bool AssociateModel(wxDataViewModel
* model
);
473 // selection related methods (inherited from wxDataViewWidgetImpl)
475 virtual wxDataViewItem
GetCurrentItem() const;
476 virtual void SetCurrentItem(const wxDataViewItem
& item
);
477 virtual int GetSelections(wxDataViewItemArray
& sel
) const;
478 virtual bool IsSelected(const wxDataViewItem
& item
) const;
479 virtual void Select(const wxDataViewItem
& item
);
480 virtual void SelectAll();
481 virtual void Unselect(const wxDataViewItem
& item
);
482 virtual void UnselectAll();
485 // sorting related methods
487 virtual wxDataViewColumn
* GetSortingColumn () const;
488 virtual void Resort();
491 // other methods (inherited from wxDataViewWidgetImpl)
493 virtual void DoSetIndent(int indent
);
494 virtual void HitTest(const wxPoint
& point
,
495 wxDataViewItem
& item
,
496 wxDataViewColumn
*& columnPtr
) const;
497 virtual void SetRowHeight(const wxDataViewItem
& item
, unsigned int height
);
498 virtual void OnSize();
500 virtual void StartEditor( const wxDataViewItem
& item
, unsigned int column
);
502 // drag & drop helper methods
503 wxDataFormat
GetDnDDataFormat(wxDataObjectComposite
* dataObjects
);
504 wxDataObjectComposite
* GetDnDDataObjects(NSData
* dataObject
) const;
506 // Cocoa-specific helpers
507 id
GetItemAtRow(int row
) const;
510 void InitOutlineView(long style
);
512 wxCocoaOutlineDataSource
* m_DataSource
;
514 wxCocoaOutlineView
* m_OutlineView
;
517 #endif // _WX_DATAVIEWCTRL_COCOOA_H_