dataview.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/osx/cocoa/dataview.h
  3. // Purpose: wxDataViewCtrl native implementation header for carbon
  4. // Author:
  5. // Copyright: (c) 2009
  6. // Licence: wxWindows licence
  7. /////////////////////////////////////////////////////////////////////////////
  8. #ifndef _WX_DATAVIEWCTRL_COCOOA_H_
  9. #define _WX_DATAVIEWCTRL_COCOOA_H_
  10. #include "wx/defs.h"
  11. #import <Cocoa/Cocoa.h>
  12. #include "wx/osx/core/dataview.h"
  13. #include "wx/osx/private.h"
  14. // Forward declaration
  15. class wxCocoaDataViewControl;
  16. /*
  17. Dramatis personae:
  18. [vertical arrows indicate inheritance, horizontal -- aggregation]
  19. wxWindow ---> wxWidgetCocoaImpl wxDataViewWidgetImpl NSOutlineView
  20. | \ / |
  21. | \ / |
  22. | \ / |
  23. v \/ \/ v
  24. wxDataViewCtrl -------> wxCocoaDataViewControl <-------> wxCocoaOutlineView
  25. The right most classes are Objective-C only and can't be used from (pure)
  26. C++ code.
  27. */
  28. // ============================================================================
  29. // wxPointerObject: simply stores a pointer, without taking its ownership
  30. // ============================================================================
  31. // Two pointer objects are equal if the containing pointers are equal. This
  32. // means also that the hash value of a pointer object depends only on the
  33. // stored pointer.
  34. @interface wxPointerObject : NSObject
  35. {
  36. void* pointer;
  37. }
  38. -(id) initWithPointer:(void*)initPointer;
  39. -(void*) pointer;
  40. -(void) setPointer:(void*)newPointer;
  41. @end
  42. // ============================================================================
  43. // wxSortDescriptorObject: helper class to use native sorting facilities
  44. // ============================================================================
  45. @interface wxSortDescriptorObject : NSSortDescriptor<NSCopying>
  46. {
  47. wxDataViewColumn* columnPtr; // pointer to the sorting column
  48. wxDataViewModel* modelPtr; // pointer to model
  49. }
  50. -(id)
  51. initWithModelPtr:(wxDataViewModel*)initModelPtr
  52. sortingColumnPtr:(wxDataViewColumn*)initColumnPtr
  53. ascending:(BOOL)sortAscending;
  54. -(wxDataViewColumn*) columnPtr;
  55. -(wxDataViewModel*) modelPtr;
  56. -(void) setColumnPtr:(wxDataViewColumn*)newColumnPtr;
  57. -(void) setModelPtr:(wxDataViewModel*)newModelPtr;
  58. @end
  59. // ============================================================================
  60. // wxDataViewColumnNativeData: extra data for wxDataViewColumn
  61. // ============================================================================
  62. class wxDataViewColumnNativeData
  63. {
  64. public:
  65. wxDataViewColumnNativeData() : m_NativeColumnPtr(NULL)
  66. {
  67. }
  68. wxDataViewColumnNativeData(NSTableColumn* initNativeColumnPtr)
  69. : m_NativeColumnPtr(initNativeColumnPtr)
  70. {
  71. }
  72. NSTableColumn* GetNativeColumnPtr() const
  73. {
  74. return m_NativeColumnPtr;
  75. }
  76. void SetNativeColumnPtr(NSTableColumn* newNativeColumnPtr)
  77. {
  78. m_NativeColumnPtr = newNativeColumnPtr;
  79. }
  80. private:
  81. // not owned by us
  82. NSTableColumn* m_NativeColumnPtr;
  83. };
  84. // ============================================================================
  85. // wxDataViewRendererNativeData: extra data for wxDataViewRenderer
  86. // ============================================================================
  87. class wxDataViewRendererNativeData
  88. {
  89. public:
  90. wxDataViewRendererNativeData()
  91. : m_Object(NULL), m_ColumnCell(NULL)
  92. {
  93. Init();
  94. }
  95. wxDataViewRendererNativeData(NSCell* initColumnCell)
  96. : m_Object(NULL), m_ColumnCell([initColumnCell retain])
  97. {
  98. Init();
  99. }
  100. wxDataViewRendererNativeData(NSCell* initColumnCell, id initObject)
  101. : m_Object([initObject retain]), m_ColumnCell([initColumnCell retain])
  102. {
  103. Init();
  104. }
  105. ~wxDataViewRendererNativeData()
  106. {
  107. [m_ColumnCell release];
  108. [m_Object release];
  109. [m_origFont release];
  110. [m_origTextColour release];
  111. }
  112. NSCell* GetColumnCell() const { return m_ColumnCell; }
  113. NSTableColumn* GetColumnPtr() const { return m_TableColumnPtr; }
  114. id GetItem() const { return m_Item; }
  115. NSCell* GetItemCell() const { return m_ItemCell; }
  116. id GetObject() const { return m_Object; }
  117. void SetColumnCell(NSCell* newCell)
  118. {
  119. [newCell retain];
  120. [m_ColumnCell release];
  121. m_ColumnCell = newCell;
  122. }
  123. void SetColumnPtr(NSTableColumn* newColumnPtr)
  124. {
  125. m_TableColumnPtr = newColumnPtr;
  126. }
  127. void SetItem(id newItem)
  128. {
  129. m_Item = newItem;
  130. }
  131. void SetItemCell(NSCell* newCell)
  132. {
  133. m_ItemCell = newCell;
  134. }
  135. void SetObject(id newObject)
  136. {
  137. [newObject retain];
  138. [m_Object release];
  139. m_Object = newObject;
  140. }
  141. // The original cell font and text colour stored here are NULL by default
  142. // and are only initialized to the values retrieved from the cell when we
  143. // change them from wxCocoaOutlineView:willDisplayCell:forTableColumn:item:
  144. // which calls our SaveOriginalXXX() methods before changing the cell
  145. // attributes.
  146. //
  147. // This allows us to avoid doing anything for the columns without any
  148. // attributes but still be able to restore the correct attributes for the
  149. // ones that do.
  150. NSFont *GetOriginalFont() const { return m_origFont; }
  151. NSColor *GetOriginalTextColour() const { return m_origTextColour; }
  152. void SaveOriginalFont(NSFont *font)
  153. {
  154. m_origFont = [font retain];
  155. }
  156. void SaveOriginalTextColour(NSColor *textColour)
  157. {
  158. m_origTextColour = [textColour retain];
  159. }
  160. // The ellipsization mode which we need to set for each cell being rendered.
  161. void SetEllipsizeMode(wxEllipsizeMode mode) { m_ellipsizeMode = mode; }
  162. wxEllipsizeMode GetEllipsizeMode() const { return m_ellipsizeMode; }
  163. // Set the line break mode for the given cell using our m_ellipsizeMode
  164. void ApplyLineBreakMode(NSCell *cell);
  165. private:
  166. // common part of all ctors
  167. void Init();
  168. id m_Item; // item NOT owned by renderer
  169. // object that can be used by renderer for storing special data (owned by
  170. // renderer)
  171. id m_Object;
  172. NSCell* m_ColumnCell; // column's cell is owned by renderer
  173. NSCell* m_ItemCell; // item's cell is NOT owned by renderer
  174. NSTableColumn* m_TableColumnPtr; // column NOT owned by renderer
  175. // we own those if they're non-NULL
  176. NSFont *m_origFont;
  177. NSColor *m_origTextColour;
  178. wxEllipsizeMode m_ellipsizeMode;
  179. };
  180. // ============================================================================
  181. // wxCocoaOutlineDataSource
  182. // ============================================================================
  183. // This class implements the data source delegate for the outline view.
  184. // As only an informal protocol exists this class inherits from NSObject only.
  185. //
  186. // As mentioned in the documentation for NSOutlineView the native control does
  187. // not own any data. Therefore, it has to be done by the data source.
  188. // Unfortunately, wxWidget's data source is a C++ data source but
  189. // NSOutlineDataSource requires objects as data. Therefore, the data (or better
  190. // the native item objects) have to be stored additionally in the native data
  191. // source.
  192. // NSOutlineView requires quick access to the item objects and quick linear
  193. // access to an item's children. This requires normally a hash type of storage
  194. // for the item object itself and an array structure for each item's children.
  195. // This means that basically two times the whole structure of wxWidget's model
  196. // class has to be stored.
  197. // This implementation is using a compromise: all items that are in use by the
  198. // control are stored in a set (from there they can be easily retrieved) and
  199. // owned by the set. Furthermore, children of the last parent are stored
  200. // in a linear list.
  201. //
  202. @interface wxCocoaOutlineDataSource : NSObject wxOSX_10_6_AND_LATER(<NSOutlineViewDataSource>)
  203. {
  204. // descriptors specifying the sorting (currently the array only holds one
  205. // object only)
  206. NSArray* sortDescriptors;
  207. NSMutableArray* children; // buffered children
  208. NSMutableSet* items; // stores all items that are in use by the control
  209. wxCocoaDataViewControl* implementation;
  210. wxDataViewModel* model;
  211. // parent of the buffered children; the object is owned
  212. wxPointerObject* currentParentItem;
  213. }
  214. // methods of informal protocol:
  215. -(BOOL)
  216. outlineView:(NSOutlineView*)outlineView
  217. acceptDrop:(id<NSDraggingInfo>)info
  218. item:(id)item
  219. childIndex:(NSInteger)index;
  220. -(id)
  221. outlineView:(NSOutlineView*)outlineView
  222. child:(NSInteger)index
  223. ofItem:(id)item;
  224. -(id)
  225. outlineView:(NSOutlineView*)outlineView
  226. objectValueForTableColumn:(NSTableColumn*)tableColumn
  227. byItem:(id)item;
  228. -(BOOL)
  229. outlineView:(NSOutlineView*)outlineView
  230. isItemExpandable:(id)item;
  231. -(NSInteger)
  232. outlineView:(NSOutlineView*)outlineView
  233. numberOfChildrenOfItem:(id)item;
  234. -(NSDragOperation)
  235. outlineView:(NSOutlineView*)outlineView
  236. validateDrop:(id<NSDraggingInfo>)info
  237. proposedItem:(id)item
  238. proposedChildIndex:(NSInteger)index;
  239. -(BOOL)
  240. outlineView:(NSOutlineView*)outlineView
  241. writeItems:(NSArray*)items
  242. toPasteboard:(NSPasteboard*)pasteboard;
  243. // buffer for items handling
  244. -(void) addToBuffer:(wxPointerObject*)item;
  245. -(void) clearBuffer;
  246. // returns the item in the buffer that has got the same pointer as "item",
  247. // if such an item does not exist nil is returned
  248. -(wxPointerObject*) getDataViewItemFromBuffer:(const wxDataViewItem&)item;
  249. -(wxPointerObject*) getItemFromBuffer:(wxPointerObject*)item;
  250. -(BOOL) isInBuffer:(wxPointerObject*)item;
  251. -(void) removeFromBuffer:(wxPointerObject*)item;
  252. // buffered children handling
  253. -(void) appendChild:(wxPointerObject*)item;
  254. -(void) clearChildren;
  255. -(wxPointerObject*) getChild:(NSUInteger)index;
  256. -(NSUInteger) getChildCount;
  257. -(void) removeChild:(NSUInteger)index;
  258. // buffer handling
  259. -(void) clearBuffers;
  260. // sorting
  261. -(NSArray*) sortDescriptors;
  262. -(void) setSortDescriptors:(NSArray*)newSortDescriptors;
  263. // access to wxWidgets variables
  264. -(wxPointerObject*) currentParentItem;
  265. -(wxCocoaDataViewControl*) implementation;
  266. -(wxDataViewModel*) model;
  267. -(void) setCurrentParentItem:(wxPointerObject*)newCurrentParentItem;
  268. -(void) setImplementation:(wxCocoaDataViewControl*)newImplementation;
  269. -(void) setModel:(wxDataViewModel*)newModel;
  270. // other methods
  271. -(void)
  272. bufferItem:(wxPointerObject*)parentItem
  273. withChildren:(wxDataViewItemArray*)dataViewChildrenPtr;
  274. @end
  275. // ============================================================================
  276. // wxCustomCell: used for custom renderers
  277. // ============================================================================
  278. @interface wxCustomCell : NSTextFieldCell
  279. {
  280. }
  281. -(NSSize) cellSize;
  282. @end
  283. // ============================================================================
  284. // wxImageTextCell
  285. // ============================================================================
  286. //
  287. // As the native cocoa environment does not have a cell displaying an icon/
  288. // image and text at the same time, it has to be implemented by the user.
  289. // This implementation follows the implementation of Chuck Pisula in Apple's
  290. // DragNDropOutline sample application.
  291. // Although in wxDataViewCtrl icons are used on OSX icons do not exist for
  292. // display. Therefore, the cell is also called wxImageTextCell.
  293. // Instead of displaying images of any size (which is possible) this cell uses
  294. // a fixed size for displaying the image. Larger images are scaled to fit
  295. // into their reserved space. Smaller or not existing images use the fixed
  296. // reserved size and are scaled if necessary.
  297. //
  298. @interface wxImageTextCell : NSTextFieldCell
  299. {
  300. @private
  301. CGFloat xImageShift; // shift for the image in x-direction from border
  302. CGFloat spaceImageText; // space between image and text
  303. NSImage* image; // the image itself
  304. NSSize imageSize; // largest size of the image; default size is (16, 16)
  305. // the text alignment is used to align the whole cell (image and text)
  306. NSTextAlignment cellAlignment;
  307. }
  308. -(NSTextAlignment) alignment;
  309. -(void) setAlignment:(NSTextAlignment)newAlignment;
  310. -(NSImage*) image;
  311. -(void) setImage:(NSImage*)newImage;
  312. -(NSSize) imageSize;
  313. -(void) setImageSize:(NSSize) newImageSize;
  314. -(NSSize) cellSize;
  315. @end
  316. // ============================================================================
  317. // wxCocoaOutlineView
  318. // ============================================================================
  319. @interface wxCocoaOutlineView : NSOutlineView wxOSX_10_6_AND_LATER(<NSOutlineViewDelegate>)
  320. {
  321. @private
  322. // column and row of the cell being edited or -1 if none
  323. int currentlyEditedColumn,
  324. currentlyEditedRow;
  325. wxCocoaDataViewControl* implementation;
  326. }
  327. -(wxCocoaDataViewControl*) implementation;
  328. -(void) setImplementation:(wxCocoaDataViewControl*) newImplementation;
  329. @end
  330. // ============================================================================
  331. // wxCocoaDataViewControl
  332. // ============================================================================
  333. // This is the internal interface class between wxDataViewCtrl (wxWidget) and
  334. // the native source view (Mac OS X cocoa).
  335. class wxCocoaDataViewControl : public wxWidgetCocoaImpl,
  336. public wxDataViewWidgetImpl
  337. {
  338. public:
  339. // constructors / destructor
  340. wxCocoaDataViewControl(wxWindow* peer,
  341. const wxPoint& pos,
  342. const wxSize& size,
  343. long style);
  344. virtual ~wxCocoaDataViewControl();
  345. wxDataViewCtrl* GetDataViewCtrl() const
  346. {
  347. return static_cast<wxDataViewCtrl*>(GetWXPeer());
  348. }
  349. // column related methods (inherited from wxDataViewWidgetImpl)
  350. virtual bool ClearColumns();
  351. virtual bool DeleteColumn(wxDataViewColumn* columnPtr);
  352. virtual void DoSetExpanderColumn(wxDataViewColumn const* columnPtr);
  353. virtual wxDataViewColumn* GetColumn(unsigned int pos) const;
  354. virtual int GetColumnPosition(wxDataViewColumn const* columnPtr) const;
  355. virtual bool InsertColumn(unsigned int pos, wxDataViewColumn* columnPtr);
  356. virtual void FitColumnWidthToContent(unsigned int pos);
  357. // item related methods (inherited from wxDataViewWidgetImpl)
  358. virtual bool Add(const wxDataViewItem& parent, const wxDataViewItem& item);
  359. virtual bool Add(const wxDataViewItem& parent,
  360. const wxDataViewItemArray& items);
  361. virtual void Collapse(const wxDataViewItem& item);
  362. virtual void EnsureVisible(const wxDataViewItem& item,
  363. wxDataViewColumn const* columnPtr);
  364. virtual void Expand(const wxDataViewItem& item);
  365. virtual unsigned int GetCount() const;
  366. virtual wxRect GetRectangle(const wxDataViewItem& item,
  367. wxDataViewColumn const* columnPtr);
  368. virtual bool IsExpanded(const wxDataViewItem& item) const;
  369. virtual bool Reload();
  370. virtual bool Remove(const wxDataViewItem& parent,
  371. const wxDataViewItem& item);
  372. virtual bool Remove(const wxDataViewItem& parent,
  373. const wxDataViewItemArray& item);
  374. virtual bool Update(const wxDataViewColumn* columnPtr);
  375. virtual bool Update(const wxDataViewItem& parent,
  376. const wxDataViewItem& item);
  377. virtual bool Update(const wxDataViewItem& parent,
  378. const wxDataViewItemArray& items);
  379. // model related methods
  380. virtual bool AssociateModel(wxDataViewModel* model);
  381. //
  382. // selection related methods (inherited from wxDataViewWidgetImpl)
  383. //
  384. virtual wxDataViewItem GetCurrentItem() const;
  385. virtual void SetCurrentItem(const wxDataViewItem& item);
  386. virtual wxDataViewColumn *GetCurrentColumn() const;
  387. virtual int GetSelectedItemsCount() const;
  388. virtual int GetSelections(wxDataViewItemArray& sel) const;
  389. virtual bool IsSelected(const wxDataViewItem& item) const;
  390. virtual void Select(const wxDataViewItem& item);
  391. virtual void SelectAll();
  392. virtual void Unselect(const wxDataViewItem& item);
  393. virtual void UnselectAll();
  394. //
  395. // sorting related methods
  396. //
  397. virtual wxDataViewColumn* GetSortingColumn () const;
  398. virtual void Resort();
  399. //
  400. // other methods (inherited from wxDataViewWidgetImpl)
  401. //
  402. virtual void DoSetIndent(int indent);
  403. virtual void HitTest(const wxPoint& point,
  404. wxDataViewItem& item,
  405. wxDataViewColumn*& columnPtr) const;
  406. virtual void SetRowHeight(const wxDataViewItem& item, unsigned int height);
  407. virtual void OnSize();
  408. virtual void StartEditor( const wxDataViewItem & item, unsigned int column );
  409. // drag & drop helper methods
  410. wxDataFormat GetDnDDataFormat(wxDataObjectComposite* dataObjects);
  411. wxDataObjectComposite* GetDnDDataObjects(NSData* dataObject) const;
  412. // Cocoa-specific helpers
  413. id GetItemAtRow(int row) const;
  414. private:
  415. void InitOutlineView(long style);
  416. wxCocoaOutlineDataSource* m_DataSource;
  417. wxCocoaOutlineView* m_OutlineView;
  418. };
  419. #endif // _WX_DATAVIEWCTRL_COCOOA_H_