]> git.saurik.com Git - wxWidgets.git/blob - include/wx/osx/cocoa/dataview.h
1fffc75bd7d98425a98f6abd19c00314c26e185a
[wxWidgets.git] / include / wx / osx / cocoa / dataview.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/osx/carbon/dataview.h
3 // Purpose: wxDataViewCtrl native implementation header for carbon
4 // Author:
5 // Id: $Id: dataview.h 57374 2009-01-27
6 // Copyright: (c) 2009
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifndef _WX_DATAVIEWCTRL_COCOOA_H_
11 #define _WX_DATAVIEWCTRL_COCOOA_H_
12
13 #include "wx/defs.h"
14
15 #if wxUSE_GUI
16
17 #ifdef __OBJC__
18 #import <Cocoa/Cocoa.h>
19 #endif
20
21 #include "wx/osx/core/dataview.h"
22 #include "wx/osx/private.h"
23
24 // Forward declaration
25 class wxCocoaDataViewControl;
26
27 // ============================================================================
28 // wxPointerObject
29 // ============================================================================
30 //
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
35 // stored pointer.
36 //
37 @interface wxPointerObject : NSObject
38 {
39 void* pointer;
40 }
41
42 //
43 // object initialization
44 //
45 -(id) initWithPointer:(void*)initPointer;
46
47 //
48 // access to pointer
49 //
50 -(void*) pointer;
51 -(void) setPointer:(void*)newPointer;
52
53 @end
54
55 // ============================================================================
56 // wxSortDescriptorObject
57 // ============================================================================
58 //
59 // This is a helper class to use native sorting facilities.
60 //
61 @interface wxSortDescriptorObject : NSSortDescriptor<NSCopying>
62 {
63 wxDataViewColumn* columnPtr; // pointer to the sorting column
64
65 wxDataViewModel* modelPtr; // pointer to model
66 }
67
68 //
69 // initialization
70 //
71 -(id) initWithModelPtr:(wxDataViewModel*)initModelPtr sortingColumnPtr:(wxDataViewColumn*)initColumnPtr ascending:(BOOL)sortAscending;
72
73 //
74 // access to variables
75 //
76 -(wxDataViewColumn*) columnPtr;
77 -(wxDataViewModel*) modelPtr;
78
79 -(void) setColumnPtr:(wxDataViewColumn*)newColumnPtr;
80 -(void) setModelPtr:(wxDataViewModel*)newModelPtr;
81
82 @end
83
84 // ============================================================================
85 // wxDataViewColumnNativeData
86 // ============================================================================
87 class wxDataViewColumnNativeData
88 {
89 public:
90 //
91 // constructors / destructor
92 //
93 wxDataViewColumnNativeData(void) : m_NativeColumnPtr(NULL)
94 {
95 }
96 wxDataViewColumnNativeData(NSTableColumn* initNativeColumnPtr) : m_NativeColumnPtr(initNativeColumnPtr)
97 {
98 }
99
100 //
101 // data access methods
102 //
103 NSTableColumn* GetNativeColumnPtr() const
104 {
105 return m_NativeColumnPtr;
106 }
107
108 void SetNativeColumnPtr(NSTableColumn* newNativeColumnPtr)
109 {
110 m_NativeColumnPtr = newNativeColumnPtr;
111 }
112
113 protected:
114 private:
115 //
116 // variables
117 //
118 NSTableColumn* m_NativeColumnPtr; // this class does not take over ownership of the pointer nor retains it
119 };
120
121 // ============================================================================
122 // wxDataViewRendererNativeData
123 // ============================================================================
124 class wxDataViewRendererNativeData
125 {
126 public:
127 //
128 // constructors / destructor
129 //
130 wxDataViewRendererNativeData(void) : m_Object(NULL), m_ColumnCell(NULL)
131 {
132 Init();
133 }
134 wxDataViewRendererNativeData(NSCell* initColumnCell) : m_Object(NULL), m_ColumnCell([initColumnCell retain])
135 {
136 Init();
137 }
138 wxDataViewRendererNativeData(NSCell* initColumnCell, id initObject) : m_Object([initObject retain]), m_ColumnCell([initColumnCell retain])
139 {
140 Init();
141 }
142
143 ~wxDataViewRendererNativeData()
144 {
145 [m_ColumnCell release];
146 [m_Object release];
147
148 [m_origFont release];
149 [m_origTextColour release];
150 }
151
152 //
153 // data access methods
154 //
155 NSCell* GetColumnCell() const
156 {
157 return m_ColumnCell;
158 }
159 NSTableColumn* GetColumnPtr() const
160 {
161 return m_TableColumnPtr;
162 }
163 id GetItem() const
164 {
165 return m_Item;
166 }
167 NSCell* GetItemCell() const
168 {
169 return m_ItemCell;
170 }
171 id GetObject() const
172 {
173 return m_Object;
174 }
175
176 void SetColumnCell(NSCell* newCell)
177 {
178 [newCell retain];
179 [m_ColumnCell release];
180 m_ColumnCell = newCell;
181 }
182 void SetColumnPtr(NSTableColumn* newColumnPtr)
183 {
184 m_TableColumnPtr = newColumnPtr;
185 }
186 void SetItem(id newItem)
187 {
188 m_Item = newItem;
189 }
190 void SetItemCell(NSCell* newCell)
191 {
192 m_ItemCell = newCell;
193 }
194 void SetObject(id newObject)
195 {
196 [newObject retain];
197 [m_Object release];
198 m_Object = newObject;
199 }
200
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.
205 //
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
208 // ones that do.
209 NSFont *GetOriginalFont() const { return m_origFont; }
210 NSColor *GetOriginalTextColour() const { return m_origTextColour; }
211
212 void SaveOriginalFont(NSFont *font)
213 {
214 m_origFont = [font retain];
215 }
216
217 void SaveOriginalTextColour(NSColor *textColour)
218 {
219 m_origTextColour = [textColour retain];
220 }
221
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; }
225
226 // Set the line break mode for the given cell using our m_ellipsizeMode
227 void ApplyLineBreakMode(NSCell *cell);
228
229 private:
230 // common part of all ctors
231 void Init();
232
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)
235
236 NSCell* m_ColumnCell; // column's cell is owned by renderer
237 NSCell* m_ItemCell; // item's cell is NOT owned by renderer
238
239 NSTableColumn* m_TableColumnPtr; // column NOT owned by renderer
240
241 // we own those if they're non-NULL
242 NSFont *m_origFont;
243 NSColor *m_origTextColour;
244
245 wxEllipsizeMode m_ellipsizeMode;
246 };
247
248 // ============================================================================
249 // wxCocoaOutlineDataSource
250 // ============================================================================
251 //
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.
254 //
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
260 // source.
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
269 // in a linear list.
270 //
271 @interface wxCocoaOutlineDataSource : NSObject
272 {
273 NSArray* sortDescriptors; // descriptors specifying the sorting (currently the array only holds one object only)
274
275 NSMutableArray* children; // buffered children
276
277 NSMutableSet* items; // stores all items that are in use by the control
278
279 wxCocoaDataViewControl* implementation;
280
281 wxDataViewModel* model;
282
283 wxPointerObject* currentParentItem; // parent of the buffered children; the object is owned
284 }
285
286 //
287 // methods of informal protocol:
288 //
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;
296
297 //
298 // buffer for items handling
299 //
300 -(void) addToBuffer:(wxPointerObject*)item;
301 -(void) clearBuffer;
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;
306
307 //
308 // buffered children handling
309 //
310 -(void) appendChild:(wxPointerObject*)item;
311 -(void) clearChildren;
312 -(wxPointerObject*) getChild:(NSUInteger)index;
313 -(NSUInteger) getChildCount;
314 -(void) removeChild:(NSUInteger)index;
315
316 //
317 // buffer handling
318 //
319 -(void) clearBuffers;
320
321 //
322 // sorting
323 //
324 -(NSArray*) sortDescriptors;
325 -(void) setSortDescriptors:(NSArray*)newSortDescriptors;
326
327 //
328 // access to wxWidget's variables
329 //
330 -(wxPointerObject*) currentParentItem;
331 -(wxCocoaDataViewControl*) implementation;
332 -(wxDataViewModel*) model;
333 -(void) setCurrentParentItem:(wxPointerObject*)newCurrentParentItem;
334 -(void) setImplementation:(wxCocoaDataViewControl*)newImplementation;
335 -(void) setModel:(wxDataViewModel*)newModel;
336
337 //
338 // other methods
339 //
340 -(void) bufferItem:(wxPointerObject*)parentItem withChildren:(wxDataViewItemArray*)dataViewChildrenPtr;
341
342 @end
343
344 // ============================================================================
345 // wxCustomCell
346 // ============================================================================
347 //
348 // This is a cell that is used for custom renderers.
349 //
350 @interface wxCustomCell : NSTextFieldCell
351 {
352 }
353
354 //
355 // other methods
356 //
357 -(NSSize) cellSize;
358
359 @end
360
361 // ============================================================================
362 // wxImageTextCell
363 // ============================================================================
364 //
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.
375 //
376 @interface wxImageTextCell : NSTextFieldCell
377 {
378 @private
379 CGFloat xImageShift; // shift for the image in x-direction from border
380 CGFloat spaceImageText; // space between image and text ("belongs" to the image)
381
382 NSImage* image; // the image itself
383
384 NSSize imageSize; // largest size of the image; default size is (16, 16)
385
386 NSTextAlignment cellAlignment; // the text alignment is used to align the whole
387 // cell (image and text)
388 }
389
390 //
391 // alignment
392 //
393 -(NSTextAlignment) alignment;
394 -(void) setAlignment:(NSTextAlignment)newAlignment;
395
396 //
397 // image access
398 //
399 -(NSImage*) image;
400 -(void) setImage:(NSImage*)newImage;
401
402 //
403 // size access
404 //
405 -(NSSize) imageSize;
406 -(void) setImageSize:(NSSize) newImageSize;
407
408 //
409 // other methods
410 //
411 -(NSSize) cellSize;
412
413 @end
414
415 // ============================================================================
416 // wxCocoaOutlineView
417 // ============================================================================
418 @interface wxCocoaOutlineView : NSOutlineView
419 {
420 @private
421 BOOL isEditingCell; // flag indicating if a cell is currently being edited
422
423 wxCocoaDataViewControl* implementation;
424 }
425
426 //
427 // access to wxWidget's implementation
428 //
429 -(wxCocoaDataViewControl*) implementation;
430 -(void) setImplementation:(wxCocoaDataViewControl*) newImplementation;
431
432 @end
433
434 // ============================================================================
435 // wxCocoaDataViewControl
436 // ============================================================================
437 //
438 // This is the internal interface class between wxDataViewCtrl (wxWidget) and
439 // the native source view (Mac OS X cocoa).
440 //
441 class wxCocoaDataViewControl : public wxWidgetCocoaImpl, public wxDataViewWidgetImpl
442 {
443 public:
444 //
445 // constructors / destructor
446 //
447 wxCocoaDataViewControl(wxWindow* peer, wxPoint const& pos, wxSize const& size, long style);
448 ~wxCocoaDataViewControl();
449
450 //
451 // column related methods (inherited from wxDataViewWidgetImpl)
452 //
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);
459
460 //
461 // item related methods (inherited from wxDataViewWidgetImpl)
462 //
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);
477
478 //
479 // model related methods
480 //
481 virtual bool AssociateModel(wxDataViewModel* model); // informs the native control that a model is present
482
483 //
484 // selection related methods (inherited from wxDataViewWidgetImpl)
485 //
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 ();
492
493 //
494 // sorting related methods
495 //
496 virtual wxDataViewColumn* GetSortingColumn () const;
497 virtual void Resort ();
498
499 //
500 // other methods (inherited from wxDataViewWidgetImpl)
501 //
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 ();
506
507 //
508 // other methods
509 //
510 wxDataViewCtrl* GetDataViewCtrl() const
511 {
512 return dynamic_cast<wxDataViewCtrl*>(GetWXPeer());
513 }
514
515 //
516 // drag & drop helper methods
517 //
518 wxDataFormat GetDnDDataFormat(wxDataObjectComposite* dataObjects);
519 wxDataObjectComposite* GetDnDDataObjects(NSData* dataObject) const; // create the data objects from the native dragged object
520 protected:
521 private:
522 //
523 // variables
524 //
525 wxCocoaOutlineDataSource* m_DataSource;
526
527 wxCocoaOutlineView* m_OutlineView;
528 };
529
530 typedef wxCocoaDataViewControl* wxCocoaDataViewControlPointer;
531
532
533 #endif // wxUSE_GUI
534 #endif // _WX_DATAVIEWCTRL_COCOOA_H_