]> git.saurik.com Git - wxWidgets.git/blame - include/wx/osx/cocoa/dataview.h
cleaning up key handling, closes #10406
[wxWidgets.git] / include / wx / osx / cocoa / dataview.h
CommitLineData
e86edab0
RR
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
25class 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// ============================================================================
87class wxDataViewColumnNativeData
88{
89public:
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(void) const
104 {
105 return this->m_NativeColumnPtr;
106 }
03647350 107
e86edab0
RR
108 void SetNativeColumnPtr(NSTableColumn* newNativeColumnPtr)
109 {
110 this->m_NativeColumnPtr = newNativeColumnPtr;
111 }
112
113protected:
114private:
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// ============================================================================
124class wxDataViewRendererNativeData
125{
126public:
127//
128// constructors / destructor
129//
130 wxDataViewRendererNativeData(void) : m_Object(NULL), m_ColumnCell(NULL)
131 {
132 }
133 wxDataViewRendererNativeData(NSCell* initColumnCell) : m_Object(NULL), m_ColumnCell([initColumnCell retain])
134 {
135 }
136 wxDataViewRendererNativeData(NSCell* initColumnCell, id initObject) : m_Object([initObject retain]), m_ColumnCell([initColumnCell retain])
137 {
138 }
139 ~wxDataViewRendererNativeData(void)
140 {
141 [this->m_ColumnCell release];
142 [this->m_Object release];
143 }
144
145 //
146 // data access methods
147 //
148 NSCell* GetColumnCell(void) const
149 {
150 return this->m_ColumnCell;
151 }
152 NSTableColumn* GetColumnPtr(void) const
153 {
154 return this->m_TableColumnPtr;
155 }
156 id GetItem(void) const
157 {
158 return this->m_Item;
159 }
160 NSCell* GetItemCell(void) const
161 {
162 return this->m_ItemCell;
163 }
164 id GetObject(void) const
165 {
166 return this->m_Object;
167 }
168
169 void SetColumnCell(NSCell* newCell)
170 {
171 [newCell retain];
172 [this->m_ColumnCell release];
173 this->m_ColumnCell = newCell;
174 }
175 void SetColumnPtr(NSTableColumn* newColumnPtr)
176 {
177 this->m_TableColumnPtr = newColumnPtr;
178 }
179 void SetItem(id newItem)
180 {
181 this->m_Item = newItem;
182 }
183 void SetItemCell(NSCell* newCell)
184 {
185 this->m_ItemCell = newCell;
186 }
187 void SetObject(id newObject)
188 {
189 [newObject retain];
190 [this->m_Object release];
191 this->m_Object = newObject;
192 }
193
194protected:
195private:
196//
197// variables
198//
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)
201
202 NSCell* m_ColumnCell; // column's cell is owned by renderer
203 NSCell* m_ItemCell; // item's cell is NOT owned by renderer
204
205 NSTableColumn* m_TableColumnPtr; // column NOT owned by renderer
206};
207
208// ============================================================================
209// wxCocoaOutlineDataSource
210// ============================================================================
211//
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.
214//
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
220// source.
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
229// in a linear list.
230//
231@interface wxCocoaOutlineDataSource : NSObject
232{
233 NSArray* sortDescriptors; // descriptors specifying the sorting (currently the array only holds one object only)
234
235 NSMutableArray* children; // buffered children
236
237 NSMutableSet* items; // stores all items that are in use by the control
03647350 238
e86edab0 239 wxCocoaDataViewControl* implementation;
03647350 240
e86edab0 241 wxDataViewModel* model;
03647350 242
e86edab0
RR
243 wxPointerObject* currentParentItem; // parent of the buffered children; the object is owned
244}
245
246 //
247 // methods of informal protocol:
248 //
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;
256
257 //
258 // buffer for items handling
259 //
260 -(void) addToBuffer:(wxPointerObject*)item;
261 -(void) clearBuffer;
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;
266
267 //
268 // buffered children handling
269 //
270 -(void) appendChild:(wxPointerObject*)item;
271 -(void) clearChildren;
272 -(wxPointerObject*) getChild:(NSUInteger)index;
273 -(NSUInteger) getChildCount;
274 -(void) removeChild:(NSUInteger)index;
275
276 //
277 // buffer handling
278 //
279 -(void) clearBuffers;
280
281 //
282 // sorting
283 //
284 -(NSArray*) sortDescriptors;
285 -(void) setSortDescriptors:(NSArray*)newSortDescriptors;
286
287 //
288 // access to wxWidget's variables
289 //
290 -(wxPointerObject*) currentParentItem;
291 -(wxCocoaDataViewControl*) implementation;
292 -(wxDataViewModel*) model;
293 -(void) setCurrentParentItem:(wxPointerObject*)newCurrentParentItem;
294 -(void) setImplementation:(wxCocoaDataViewControl*)newImplementation;
295 -(void) setModel:(wxDataViewModel*)newModel;
296
297 //
298 // other methods
299 //
300 -(void) bufferItem:(wxPointerObject*)parentItem withChildren:(wxDataViewItemArray*)dataViewChildrenPtr;
301
302@end
303
304// ============================================================================
305// wxCustomCell
306// ============================================================================
307//
308// This is a cell that is used for custom renderers.
309//
310@interface wxCustomCell : NSTextFieldCell
311{
312}
313
314 //
315 // other methods
316 //
317 -(NSSize) cellSize;
318
319@end
320
321// ============================================================================
322// wxImageTextCell
323// ============================================================================
324//
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.
335//
336@interface wxImageTextCell : NSTextFieldCell
337{
338@private
339 CGFloat xImageShift; // shift for the image in x-direction from border
340 CGFloat spaceImageText; // space between image and text ("belongs" to the image)
03647350 341
e86edab0 342 NSImage* image; // the image itself
03647350 343
e86edab0 344 NSSize imageSize; // largest size of the image; default size is (16, 16)
03647350 345
e86edab0
RR
346 NSTextAlignment cellAlignment; // the text alignment is used to align the whole
347 // cell (image and text)
348}
349
350 //
351 // alignment
352 //
353 -(NSTextAlignment) alignment;
354 -(void) setAlignment:(NSTextAlignment)newAlignment;
355
356 //
357 // image access
358 //
359 -(NSImage*) image;
360 -(void) setImage:(NSImage*)newImage;
361
362 //
363 // size access
364 //
365 -(NSSize) imageSize;
366 -(void) setImageSize:(NSSize) newImageSize;
367
368 //
369 // other methods
370 //
371 -(NSSize) cellSize;
372
373@end
374
375// ============================================================================
376// wxCocoaOutlineView
377// ============================================================================
378@interface wxCocoaOutlineView : NSOutlineView
379{
380@private
381 BOOL isEditingCell; // flag indicating if a cell is currently being edited
382
383 wxCocoaDataViewControl* implementation;
384}
385
386 //
387 // access to wxWidget's implementation
388 //
389 -(wxCocoaDataViewControl*) implementation;
390 -(void) setImplementation:(wxCocoaDataViewControl*) newImplementation;
391
392@end
393
394// ============================================================================
395// wxCocoaDataViewControl
396// ============================================================================
397//
398// This is the internal interface class between wxDataViewCtrl (wxWidget) and
399// the native source view (Mac OS X cocoa).
400//
401class wxCocoaDataViewControl : public wxWidgetCocoaImpl, public wxDataViewWidgetImpl
402{
403public:
404 //
405 // constructors / destructor
406 //
407 wxCocoaDataViewControl(wxWindow* peer, wxPoint const& pos, wxSize const& size, long style);
408 ~wxCocoaDataViewControl(void);
409
410 //
411 // column related methods (inherited from wxDataViewWidgetImpl)
412 //
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);
419
420 //
421 // item related methods (inherited from wxDataViewWidgetImpl)
422 //
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);
437
438 //
439 // model related methods
440 //
441 virtual bool AssociateModel(wxDataViewModel* model); // informs the native control that a model is present
442
443 //
444 // selection related methods (inherited from wxDataViewWidgetImpl)
445 //
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);
452
453 //
454 // sorting related methods
455 //
456 virtual wxDataViewColumn* GetSortingColumn (void) const;
457 virtual void Resort (void);
458
459 //
460 // other methods (inherited from wxDataViewWidgetImpl)
461 //
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);
466
467 //
468 // other methods
469 //
470 wxDataViewCtrl* GetDataViewCtrl(void) const
471 {
472 return dynamic_cast<wxDataViewCtrl*>(this->GetWXPeer());
473 }
474
475//
476// drag & drop helper methods
477//
478 wxDataFormat GetDnDDataFormat(wxDataObjectComposite* dataObjects);
479 wxDataObjectComposite* GetDnDDataObjects(NSData* dataObject) const; // create the data objects from the native dragged object
480protected:
481private:
482//
483// variables
484//
485 wxCocoaOutlineDataSource* m_DataSource;
486
487 wxCocoaOutlineView* m_OutlineView;
488};
489
490typedef wxCocoaDataViewControl* wxCocoaDataViewControlPointer;
491
492
493#endif // wxUSE_GUI
494#endif // _WX_DATAVIEWCTRL_COCOOA_H_