]> git.saurik.com Git - wxWidgets.git/blame - src/osx/cocoa/dataview.mm
Store menus titles in menus themselves in wxOSX.
[wxWidgets.git] / src / osx / cocoa / dataview.mm
CommitLineData
e86edab0
RR
1///////////////////////////////////////////////////////////////////////////////
2// Name: src/osx/cocoa/dataview.mm
3// Purpose: wxDataView
608129e5 4// Author:
e86edab0
RR
5// Modified by:
6// Created: 2009-01-31
7// RCS-ID: $Id: dataview.mm$
608129e5 8// Copyright:
e86edab0
RR
9// Licence: wxWindows licence
10///////////////////////////////////////////////////////////////////////////////
11
12#include "wx/wxprec.h"
13
14#if (wxUSE_DATAVIEWCTRL == 1) && !defined(wxUSE_GENERICDATAVIEWCTRL)
15
16#ifndef WX_PRECOMP
17 #include "wx/app.h"
18 #include "wx/toplevel.h"
19 #include "wx/font.h"
20 #include "wx/settings.h"
21 #include "wx/utils.h"
22#endif
23
e86edab0 24#include "wx/osx/private.h"
c8fdb345 25#include "wx/osx/cocoa/dataview.h"
e86edab0
RR
26#include "wx/renderer.h"
27
e86edab0
RR
28// ============================================================================
29// Constants used locally
30// ============================================================================
8e59cbe4 31
e86edab0
RR
32#define DataViewPboardType @"OutlineViewItem"
33
34// ============================================================================
35// Classes used locally in dataview.mm
36// ============================================================================
eda34276 37
2406d534
VZ
38// ============================================================================
39// wxPointerObject
40// ============================================================================
41
42@implementation wxPointerObject
43
44-(id) init
45{
46 self = [super init];
47 if (self != nil)
48 self->pointer = NULL;
49 return self;
50}
51
52-(id) initWithPointer:(void*) initPointer
53{
54 self = [super init];
55 if (self != nil)
56 self->pointer = initPointer;
57 return self;
58}
59
60//
61// inherited methods from NSObject
62//
63-(BOOL) isEqual:(id)object
64{
65 return (object != nil) &&
66 ([object isKindOfClass:[wxPointerObject class]]) &&
67 (pointer == [((wxPointerObject*) object) pointer]);
68}
69
70-(NSUInteger) hash
71{
72 return (NSUInteger) pointer;
73}
74
75-(void*) pointer
76{
77 return pointer;
78}
79
80-(void) setPointer:(void*) newPointer
81{
82 pointer = newPointer;
83}
84
85@end
86
87namespace
88{
89
90inline wxDataViewItem wxDataViewItemFromItem(id item)
91{
92 return wxDataViewItem([static_cast<wxPointerObject *>(item) pointer]);
93}
94
95inline wxDataViewItem wxDataViewItemFromMaybeNilItem(id item)
96{
97 return item == nil ? wxDataViewItem() : wxDataViewItemFromItem(item);
98}
99
100} // anonymous namespace
101
eda34276
VZ
102// ----------------------------------------------------------------------------
103// wxCustomRendererObject
104// ----------------------------------------------------------------------------
105
e86edab0
RR
106@interface wxCustomRendererObject : NSObject <NSCopying>
107{
108@public
8e59cbe4 109 wxDataViewCustomRenderer* customRenderer; // not owned by the class
e86edab0
RR
110}
111
8e59cbe4
VZ
112 -(id) init;
113 -(id) initWithRenderer:(wxDataViewCustomRenderer*)renderer;
e86edab0
RR
114@end
115
116@implementation wxCustomRendererObject
8e59cbe4 117
e86edab0
RR
118-(id) init
119{
8e59cbe4
VZ
120 self = [super init];
121 if (self != nil)
122 {
123 customRenderer = NULL;
124 }
125 return self;
e86edab0
RR
126}
127
a8afd748 128-(id) initWithRenderer:(wxDataViewCustomRenderer*)renderer
e86edab0 129{
8e59cbe4
VZ
130 self = [super init];
131 if (self != nil)
132 {
133 customRenderer = renderer;
134 }
135 return self;
e86edab0
RR
136}
137
138-(id) copyWithZone:(NSZone*)zone
139{
8e59cbe4 140 wxCustomRendererObject* copy;
608129e5 141
8e59cbe4
VZ
142 copy = [[[self class] allocWithZone:zone] init];
143 copy->customRenderer = customRenderer;
e86edab0 144
8e59cbe4 145 return copy;
e86edab0 146}
e86edab0
RR
147@end
148
eda34276
VZ
149// ----------------------------------------------------------------------------
150// wxDVCNSTableColumn: exists only to override NSTableColumn:dataCellForRow:
151// ----------------------------------------------------------------------------
152
153@interface wxDVCNSTableColumn : NSTableColumn
154{
155}
156
157 -(id) dataCellForRow:(NSInteger)row;
158@end
159
160@implementation wxDVCNSTableColumn
161
162-(id) dataCellForRow:(NSInteger)row
163{
164 // what we want to do here is to simply return nil for the cells which
165 // shouldn't show anything as otherwise we would show e.g. empty combo box
166 // or progress cells in the columns using the corresponding types even for
167 // the container rows which is wrong
168
169 // half of the problem is just finding the objects we need from the column
170 // pointer which is itself stashed inside wxPointerObject which we use as
171 // our identifier
172 const wxDataViewColumn * const
173 dvCol = static_cast<wxDataViewColumn *>(
174 [(wxPointerObject *)[self identifier] pointer]
175 );
176
177 const wxDataViewCtrl * const dvc = dvCol->GetOwner();
178 const wxCocoaDataViewControl * const
179 peer = static_cast<wxCocoaDataViewControl *>(dvc->GetPeer());
180
181
182 // once we do have everything, simply ask NSOutlineView for the item...
183 const id item = peer->GetItemAtRow(row);
184 if ( item )
185 {
186 // ... and if it succeeded, ask the model whether it has any value
2406d534 187 wxDataViewItem dvItem(wxDataViewItemFromItem(item));
eda34276
VZ
188
189 if ( !dvc->GetModel()->HasValue(dvItem, dvCol->GetModelColumn()) )
190 return nil;
191 }
192
193 return [super dataCellForRow:row];
194}
195
196@end
197
e86edab0 198// ============================================================================
8e59cbe4 199// local helpers
e86edab0 200// ============================================================================
e86edab0 201
8e59cbe4
VZ
202namespace
203{
e86edab0 204
9461dd8c
VZ
205// convert from NSObject to different C++ types: all these functions check
206// that the conversion really makes sense and assert if it doesn't
207wxString ObjectToString(NSObject *object)
208{
209 wxCHECK_MSG( [object isKindOfClass:[NSString class]], "",
210 wxString::Format
211 (
212 "string expected but got %s",
213 wxCFStringRef::AsString([object className])
214 ));
215
216 return wxCFStringRef([((NSString*) object) retain]).AsString();
217}
218
219bool ObjectToBool(NSObject *object)
220{
221 // actually the value must be of NSCFBoolean class but it's private so we
222 // can't check for it directly
223 wxCHECK_MSG( [object isKindOfClass:[NSNumber class]], false,
224 wxString::Format
225 (
226 "number expected but got %s",
227 wxCFStringRef::AsString([object className])
228 ));
229
230 return [(NSNumber *)object boolValue];
231}
232
233long ObjectToLong(NSObject *object)
234{
235 wxCHECK_MSG( [object isKindOfClass:[NSNumber class]], -1,
236 wxString::Format
237 (
238 "number expected but got %s",
239 wxCFStringRef::AsString([object className])
240 ));
241
242 return [(NSNumber *)object longValue];
243}
244
245wxDateTime ObjectToDate(NSObject *object)
246{
247 wxCHECK_MSG( [object isKindOfClass:[NSDate class]], wxInvalidDateTime,
248 wxString::Format
249 (
250 "date expected but got %s",
251 wxCFStringRef::AsString([object className])
252 ));
253
254 // get the number of seconds since 1970-01-01 UTC and this is the only
255 // way to convert a double to a wxLongLong
256 const wxLongLong seconds = [((NSDate*) object) timeIntervalSince1970];
257
258 wxDateTime dt(1, wxDateTime::Jan, 1970);
259 dt.Add(wxTimeSpan(0,0,seconds));
260
261 // the user has entered a date in the local timezone but seconds
262 // contains the number of seconds from date in the local timezone
263 // since 1970-01-01 UTC; therefore, the timezone information has to be
264 // transferred to wxWidgets, too:
265 dt.MakeFromTimezone(wxDateTime::UTC);
266
267 return dt;
268}
269
8e59cbe4
VZ
270NSInteger CompareItems(id item1, id item2, void* context)
271{
272 NSArray* const sortDescriptors = (NSArray*) context;
e86edab0 273
8e59cbe4 274 NSUInteger const count = [sortDescriptors count];
608129e5 275
8e59cbe4
VZ
276 NSInteger result = NSOrderedSame;
277 for ( NSUInteger i = 0; i < count && result == NSOrderedSame; ++i )
e86edab0 278 {
8e59cbe4
VZ
279 wxSortDescriptorObject* const
280 sortDescriptor = (wxSortDescriptorObject*)
281 [sortDescriptors objectAtIndex:i];
282
283 int rc = [sortDescriptor modelPtr]->Compare
284 (
2406d534
VZ
285 wxDataViewItemFromItem(item1),
286 wxDataViewItemFromItem(item2),
8e59cbe4
VZ
287 [sortDescriptor columnPtr]->GetModelColumn(),
288 [sortDescriptor ascending] == YES
289 );
290
291 if ( rc < 0 )
292 result = NSOrderedAscending;
293 else if ( rc > 0 )
294 result = NSOrderedDescending;
e86edab0 295 }
8e59cbe4
VZ
296
297 return result;
e86edab0
RR
298}
299
8e59cbe4 300NSTextAlignment ConvertToNativeHorizontalTextAlignment(int alignment)
e86edab0 301{
8e59cbe4
VZ
302 if (alignment & wxALIGN_CENTER_HORIZONTAL)
303 return NSCenterTextAlignment;
304 else if (alignment & wxALIGN_RIGHT)
305 return NSRightTextAlignment;
306 else
307 return NSLeftTextAlignment;
e86edab0
RR
308}
309
8e59cbe4 310NSTableColumn* CreateNativeColumn(const wxDataViewColumn *column)
e86edab0 311{
8e59cbe4
VZ
312 wxDataViewRenderer * const renderer = column->GetRenderer();
313
314 wxCHECK_MSG( renderer, NULL, "column should have a renderer" );
e86edab0 315
eda34276
VZ
316 wxDVCNSTableColumn * const nativeColumn(
317 [[wxDVCNSTableColumn alloc] initWithIdentifier:
8e59cbe4
VZ
318 [[[wxPointerObject alloc] initWithPointer:
319 const_cast<wxDataViewColumn*>(column)]
320 autorelease]]
321 );
e86edab0 322
8e59cbe4 323 // setting the size related parameters:
d831e2db 324 const int width = column->GetWidthVariable();
0e17930f 325 int resizingMask;
8e59cbe4 326 if (column->IsResizeable())
e86edab0 327 {
0e17930f 328 resizingMask = NSTableColumnUserResizingMask;
8e59cbe4
VZ
329 [nativeColumn setMinWidth:column->GetMinWidth()];
330 [nativeColumn setMaxWidth:column->GetMaxWidth()];
e86edab0 331 }
0e17930f 332 else // column is not resizeable [by user]
e86edab0 333 {
0e17930f
VZ
334 // if the control doesn't show a header, make the columns resize
335 // automatically, this is particularly important for the single column
336 // controls (such as wxDataViewTreeCtrl) as their unique column should
337 // always take up all the available splace
338 resizingMask = column->GetOwner()->HasFlag(wxDV_NO_HEADER)
339 ? NSTableColumnAutoresizingMask
340 : NSTableColumnNoResizing;
e86edab0 341 }
0e17930f 342 [nativeColumn setResizingMask:resizingMask];
d831e2db 343 [nativeColumn setWidth:width];
8e59cbe4 344
e86edab0 345#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
8e59cbe4
VZ
346 // setting the visibility:
347 [nativeColumn setHidden:static_cast<BOOL>(column->IsHidden())];
e86edab0 348#endif
c937bcac 349
8e59cbe4 350 wxDataViewRendererNativeData * const renderData = renderer->GetNativeData();
c937bcac 351
8e59cbe4
VZ
352 // setting the header:
353 [[nativeColumn headerCell] setAlignment:
354 ConvertToNativeHorizontalTextAlignment(column->GetAlignment())];
355 [[nativeColumn headerCell] setStringValue:
356 [[wxCFStringRef(column->GetTitle()).AsNSString() retain] autorelease]];
c937bcac
VZ
357 renderData->ApplyLineBreakMode([nativeColumn headerCell]);
358
8e59cbe4 359 // setting data cell's properties:
e86edab0 360 [[nativeColumn dataCell] setWraps:NO];
8e59cbe4 361 // setting the default data cell:
c937bcac 362 [nativeColumn setDataCell:renderData->GetColumnCell()];
8e59cbe4
VZ
363 // setting the editablility:
364 const bool isEditable = renderer->GetMode() == wxDATAVIEW_CELL_EDITABLE;
e86edab0 365
8e59cbe4
VZ
366 [nativeColumn setEditable:isEditable];
367 [[nativeColumn dataCell] setEditable:isEditable];
368
369 return nativeColumn;
e86edab0
RR
370}
371
8e59cbe4
VZ
372} // anonymous namespace
373
e86edab0
RR
374// ============================================================================
375// Public helper functions for dataview implementation on OSX
376// ============================================================================
8e59cbe4
VZ
377
378wxWidgetImplType* CreateDataView(wxWindowMac* wxpeer,
379 wxWindowMac* WXUNUSED(parent),
380 wxWindowID WXUNUSED(id),
381 const wxPoint& pos,
382 const wxSize& size,
383 long style,
384 long WXUNUSED(extraStyle))
e86edab0 385{
8e59cbe4 386 return new wxCocoaDataViewControl(wxpeer,pos,size,style);
e86edab0
RR
387}
388
e86edab0
RR
389// ============================================================================
390// wxSortDescriptorObject
391// ============================================================================
8e59cbe4 392
e86edab0 393@implementation wxSortDescriptorObject
e86edab0
RR
394-(id) init
395{
8e59cbe4
VZ
396 self = [super init];
397 if (self != nil)
398 {
399 columnPtr = NULL;
400 modelPtr = NULL;
401 }
402 return self;
e86edab0
RR
403}
404
8e59cbe4
VZ
405-(id)
406initWithModelPtr:(wxDataViewModel*)initModelPtr
407 sortingColumnPtr:(wxDataViewColumn*)initColumnPtr
408 ascending:(BOOL)sortAscending
e86edab0 409{
8e59cbe4
VZ
410 self = [super initWithKey:@"dummy" ascending:sortAscending];
411 if (self != nil)
412 {
413 columnPtr = initColumnPtr;
414 modelPtr = initModelPtr;
415 }
416 return self;
e86edab0
RR
417}
418
419-(id) copyWithZone:(NSZone*)zone
420{
8e59cbe4 421 wxSortDescriptorObject* copy;
608129e5
VZ
422
423
8e59cbe4
VZ
424 copy = [super copyWithZone:zone];
425 copy->columnPtr = columnPtr;
426 copy->modelPtr = modelPtr;
e86edab0 427
8e59cbe4 428 return copy;
e86edab0
RR
429}
430
431//
432// access to model column's index
433//
434-(wxDataViewColumn*) columnPtr
435{
8e59cbe4 436 return columnPtr;
e86edab0
RR
437}
438
439-(wxDataViewModel*) modelPtr
440{
8e59cbe4 441 return modelPtr;
e86edab0
RR
442}
443
444-(void) setColumnPtr:(wxDataViewColumn*)newColumnPtr
445{
8e59cbe4 446 columnPtr = newColumnPtr;
e86edab0
RR
447}
448
449-(void) setModelPtr:(wxDataViewModel*)newModelPtr
450{
8e59cbe4 451 modelPtr = newModelPtr;
e86edab0
RR
452}
453
454@end
455
456// ============================================================================
457// wxCocoaOutlineDataSource
458// ============================================================================
459@implementation wxCocoaOutlineDataSource
460
461//
462// constructors / destructor
463//
464-(id) init
465{
8e59cbe4
VZ
466 self = [super init];
467 if (self != nil)
468 {
469 implementation = NULL;
470 model = NULL;
e86edab0 471
8e59cbe4 472 currentParentItem = nil;
e86edab0 473
8e59cbe4
VZ
474 children = [[NSMutableArray alloc] init];
475 items = [[NSMutableSet alloc] init];
476 }
477 return self;
e86edab0
RR
478}
479
480-(void) dealloc
481{
8e59cbe4 482 [currentParentItem release];
e86edab0 483
8e59cbe4
VZ
484 [children release];
485 [items release];
608129e5 486
8e59cbe4 487 [super dealloc];
e86edab0
RR
488}
489
490//
491// methods of informal protocol:
492//
8e59cbe4
VZ
493-(BOOL)
494outlineView:(NSOutlineView*)outlineView
495 acceptDrop:(id<NSDraggingInfo>)info
496 item:(id)item childIndex:(NSInteger)index
e86edab0 497{
8e59cbe4
VZ
498 NSArray* supportedTypes(
499 [NSArray arrayWithObjects:DataViewPboardType,NSStringPboardType,nil]
500 );
e86edab0 501
8e59cbe4 502 NSPasteboard* pasteboard([info draggingPasteboard]);
e86edab0 503
8e59cbe4 504 NSString* bestType([pasteboard availableTypeFromArray:supportedTypes]);
e86edab0 505
8e59cbe4
VZ
506 if ( bestType == nil )
507 return FALSE;
e86edab0 508
8e59cbe4 509 wxDataViewCtrl * const dvc(implementation->GetDataViewCtrl());
608129e5 510
8e59cbe4
VZ
511 wxCHECK_MSG( dvc, false,
512 "Pointer to data view control not set correctly." );
513 wxCHECK_MSG( dvc->GetModel(), false,
514 "Pointer to model not set correctly." );
e86edab0 515
8e59cbe4
VZ
516 wxDataViewEvent event(wxEVT_COMMAND_DATAVIEW_ITEM_DROP, dvc->GetId());
517 event.SetEventObject(dvc);
2406d534 518 event.SetItem(wxDataViewItemFromItem(item));
8e59cbe4 519 event.SetModel(dvc->GetModel());
e86edab0 520
8e59cbe4
VZ
521 BOOL dragSuccessful;
522 if ( [bestType compare:DataViewPboardType] == NSOrderedSame )
e86edab0 523 {
2406d534
VZ
524 NSArray* dataArray((NSArray*)
525 [pasteboard propertyListForType:DataViewPboardType]);
8e59cbe4 526 NSUInteger indexDraggedItem, noOfDraggedItems([dataArray count]);
e86edab0 527
8e59cbe4
VZ
528 indexDraggedItem = 0;
529 while (indexDraggedItem < noOfDraggedItems)
e86edab0 530 {
2406d534
VZ
531 wxDataObjectComposite* dataObjects(
532 implementation->GetDnDDataObjects((NSData*)
533 [dataArray objectAtIndex:indexDraggedItem]));
8e59cbe4
VZ
534
535 if (dataObjects && (dataObjects->GetFormatCount() > 0))
536 {
537 wxMemoryBuffer buffer;
538
539 // copy data into data object:
540 event.SetDataObject(dataObjects);
2406d534
VZ
541 event.SetDataFormat(
542 implementation->GetDnDDataFormat(dataObjects));
8e59cbe4 543 // copy data into buffer:
2406d534
VZ
544 dataObjects->GetDataHere(
545 event.GetDataFormat().GetType(),
546 buffer.GetWriteBuf(event.GetDataSize()));
8e59cbe4
VZ
547 buffer.UngetWriteBuf(event.GetDataSize());
548 event.SetDataBuffer(buffer.GetData());
549 // finally, send event:
550 if (dvc->HandleWindowEvent(event) && event.IsAllowed())
551 {
552 dragSuccessful = true;
553 ++indexDraggedItem;
554 }
555 else
556 {
557 dragSuccessful = true;
558 indexDraggedItem = noOfDraggedItems; // stop loop
559 }
560 }
561 else
562 {
563 dragSuccessful = false;
564 indexDraggedItem = noOfDraggedItems; // stop loop
565 }
566 // clean-up:
567 delete dataObjects;
e86edab0 568 }
e86edab0
RR
569 }
570 else
571 {
2406d534
VZ
572 // needed to convert internally used UTF-16 representation to a UTF-8
573 // representation
574 CFDataRef osxData;
8e59cbe4
VZ
575 wxDataObjectComposite* dataObjects (new wxDataObjectComposite());
576 wxTextDataObject* textDataObject(new wxTextDataObject());
577
2406d534
VZ
578 osxData = ::CFStringCreateExternalRepresentation
579 (
580 kCFAllocatorDefault,
581 (CFStringRef)[pasteboard stringForType:NSStringPboardType],
582 kCFStringEncodingUTF8,
583 32
584 );
585 if (textDataObject->SetData(::CFDataGetLength(osxData),
586 ::CFDataGetBytePtr(osxData)))
8e59cbe4 587 dataObjects->Add(textDataObject);
e86edab0 588 else
8e59cbe4
VZ
589 delete textDataObject;
590 // send event if data could be copied:
591 if (dataObjects->GetFormatCount() > 0)
592 {
593 event.SetDataObject(dataObjects);
594 event.SetDataFormat(implementation->GetDnDDataFormat(dataObjects));
595 if (dvc->HandleWindowEvent(event) && event.IsAllowed())
596 dragSuccessful = true;
597 else
598 dragSuccessful = false;
599 }
600 else
601 dragSuccessful = false;
602 // clean up:
603 ::CFRelease(osxData);
604 delete dataObjects;
e86edab0 605 }
c8fdb345 606 return dragSuccessful;
e86edab0
RR
607}
608
2406d534
VZ
609-(id) outlineView:(NSOutlineView*)outlineView
610 child:(NSInteger)index
611 ofItem:(id)item
e86edab0 612{
2406d534
VZ
613 if ((item == currentParentItem) &&
614 (index < ((NSInteger) [self getChildCount])))
8e59cbe4 615 return [self getChild:index];
8e59cbe4 616
2406d534
VZ
617 wxDataViewItemArray dataViewChildren;
618
619 wxCHECK_MSG( model, 0, "Valid model in data source does not exist." );
620 model->GetChildren(wxDataViewItemFromMaybeNilItem(item), dataViewChildren);
621 [self bufferItem:item withChildren:&dataViewChildren];
622 if ([sortDescriptors count] > 0)
623 [children sortUsingFunction:CompareItems context:sortDescriptors];
624 return [self getChild:index];
e86edab0
RR
625}
626
627-(BOOL) outlineView:(NSOutlineView*)outlineView isItemExpandable:(id)item
628{
8e59cbe4 629 wxCHECK_MSG( model, 0, "Valid model in data source does not exist." );
2406d534 630 return model->IsContainer(wxDataViewItemFromItem(item));
e86edab0
RR
631}
632
633-(NSInteger) outlineView:(NSOutlineView*)outlineView numberOfChildrenOfItem:(id)item
634{
8e59cbe4 635 NSInteger noOfChildren;
e86edab0 636
8e59cbe4 637 wxDataViewItemArray dataViewChildren;
e86edab0
RR
638
639
8e59cbe4 640 wxCHECK_MSG( model, 0, "Valid model in data source does not exist." );
2406d534
VZ
641 noOfChildren = model->GetChildren(wxDataViewItemFromMaybeNilItem(item),
642 dataViewChildren);
8e59cbe4
VZ
643 [self bufferItem:item withChildren:&dataViewChildren];
644 if ([sortDescriptors count] > 0)
645 [children sortUsingFunction:CompareItems context:sortDescriptors];
646 return noOfChildren;
e86edab0
RR
647}
648
8e59cbe4
VZ
649-(id)
650outlineView:(NSOutlineView*)outlineView
651 objectValueForTableColumn:(NSTableColumn*)tableColumn
652 byItem:(id)item
e86edab0 653{
eda34276
VZ
654 wxCHECK_MSG( model, nil, "Valid model in data source does not exist." );
655
8e59cbe4 656 wxDataViewColumn* col(static_cast<wxDataViewColumn*>([[tableColumn identifier] pointer]));
eda34276 657 const unsigned colIdx = col->GetModelColumn();
e86edab0 658
2406d534 659 wxDataViewItem dataViewItem(wxDataViewItemFromItem(item));
e86edab0 660
eda34276
VZ
661 if ( model->HasValue(dataViewItem, colIdx) )
662 {
663 wxVariant value;
664 model->GetValue(value,dataViewItem, colIdx);
665 col->GetRenderer()->SetValue(value);
666 }
e86edab0 667
8e59cbe4 668 return nil;
e86edab0
RR
669}
670
8e59cbe4
VZ
671-(void)
672outlineView:(NSOutlineView*)outlineView
673 setObjectValue:(id)object
674 forTableColumn:(NSTableColumn*)tableColumn
675 byItem:(id)item
e86edab0 676{
8e59cbe4 677 wxDataViewColumn* col(static_cast<wxDataViewColumn*>([[tableColumn identifier] pointer]));
e86edab0 678
0599fe19 679 col->GetRenderer()->
2406d534 680 OSXOnCellChanged(object, wxDataViewItemFromItem(item), col->GetModelColumn());
e86edab0
RR
681}
682
683-(void) outlineView:(NSOutlineView*)outlineView sortDescriptorsDidChange:(NSArray*)oldDescriptors
e86edab0 684{
17f5d137
VZ
685 // Warning: the new sort descriptors are guaranteed to be only of type
686 // NSSortDescriptor! Therefore, the sort descriptors for the data source
687 // have to be converted.
8e59cbe4 688 NSArray* newDescriptors;
e86edab0 689
8e59cbe4 690 NSMutableArray* wxSortDescriptors;
608129e5 691
8e59cbe4 692 NSUInteger noOfDescriptors;
e86edab0 693
8e59cbe4 694 wxDataViewCtrl* const dvc = implementation->GetDataViewCtrl();
e86edab0
RR
695
696
8e59cbe4
VZ
697 // convert NSSortDescriptors to wxSortDescriptorObjects:
698 newDescriptors = [outlineView sortDescriptors];
699 noOfDescriptors = [newDescriptors count];
700 wxSortDescriptors = [NSMutableArray arrayWithCapacity:noOfDescriptors];
701 for (NSUInteger i=0; i<noOfDescriptors; ++i)
702 {
8e59cbe4 703 NSSortDescriptor* const newDescriptor = [newDescriptors objectAtIndex:i];
e86edab0 704
8e59cbe4
VZ
705 [wxSortDescriptors addObject:[[[wxSortDescriptorObject alloc] initWithModelPtr:model
706 sortingColumnPtr:dvc->GetColumn([[newDescriptor key] intValue])
707 ascending:[newDescriptor ascending]] autorelease]];
708 }
c8fdb345 709 [(wxCocoaOutlineDataSource*)[outlineView dataSource] setSortDescriptors:wxSortDescriptors];
e86edab0 710
17f5d137
VZ
711 // send first the event to wxWidgets that the sorting has changed so that
712 // the program can do special actions before the sorting actually starts:
8e59cbe4 713 wxDataViewEvent event(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED,dvc->GetId()); // variable defintion
e86edab0 714
8e59cbe4
VZ
715 event.SetEventObject(dvc);
716 if (noOfDescriptors > 0)
717 {
8e59cbe4 718 wxDataViewColumn* const col = [[wxSortDescriptors objectAtIndex:0] columnPtr];
e86edab0 719
8e59cbe4
VZ
720 event.SetColumn(dvc->GetColumnPosition(col));
721 event.SetDataViewColumn(col);
722 }
723 dvc->GetEventHandler()->ProcessEvent(event);
e86edab0 724
8e59cbe4
VZ
725 // start re-ordering the data;
726 // children's buffer must be cleared first because it contains the old order:
727 [self clearChildren];
728 // sorting is done while reloading the data:
729 [outlineView reloadData];
e86edab0
RR
730}
731
732-(NSDragOperation) outlineView:(NSOutlineView*)outlineView validateDrop:(id<NSDraggingInfo>)info proposedItem:(id)item proposedChildIndex:(NSInteger)index
733{
8e59cbe4 734 NSArray* supportedTypes([NSArray arrayWithObjects:DataViewPboardType,NSStringPboardType,nil]);
e86edab0 735
8e59cbe4 736 NSPasteboard* pasteboard([info draggingPasteboard]);
e86edab0 737
8e59cbe4
VZ
738 NSString* bestType([pasteboard availableTypeFromArray:supportedTypes]);
739 if (bestType == nil)
740 return NSDragOperationNone;
e86edab0 741
8e59cbe4
VZ
742 NSDragOperation dragOperation;
743 wxDataViewCtrl* const dvc(implementation->GetDataViewCtrl());
e86edab0 744
8e59cbe4
VZ
745 wxCHECK_MSG(dvc, false, "Pointer to data view control not set correctly.");
746 wxCHECK_MSG(dvc->GetModel(), false, "Pointer to model not set correctly.");
608129e5 747
8e59cbe4
VZ
748 wxDataViewEvent
749 event(wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE,dvc->GetId());
e86edab0 750
8e59cbe4 751 event.SetEventObject(dvc);
2406d534 752 event.SetItem(wxDataViewItemFromItem(item));
8e59cbe4 753 event.SetModel(dvc->GetModel());
e86edab0
RR
754 if ([bestType compare:DataViewPboardType] == NSOrderedSame)
755 {
8e59cbe4
VZ
756 NSArray* dataArray((NSArray*)[pasteboard propertyListForType:DataViewPboardType]);
757 NSUInteger indexDraggedItem, noOfDraggedItems([dataArray count]);
e86edab0 758
8e59cbe4
VZ
759 indexDraggedItem = 0;
760 while (indexDraggedItem < noOfDraggedItems)
e86edab0 761 {
8e59cbe4
VZ
762 wxDataObjectComposite* dataObjects(implementation->GetDnDDataObjects((NSData*)[dataArray objectAtIndex:indexDraggedItem]));
763
764 if (dataObjects && (dataObjects->GetFormatCount() > 0))
765 {
766 wxMemoryBuffer buffer;
767
768 // copy data into data object:
769 event.SetDataObject(dataObjects);
770 event.SetDataFormat(implementation->GetDnDDataFormat(dataObjects));
771 // copy data into buffer:
772 dataObjects->GetDataHere(event.GetDataFormat().GetType(),buffer.GetWriteBuf(event.GetDataSize()));
773 buffer.UngetWriteBuf(event.GetDataSize());
774 event.SetDataBuffer(buffer.GetData());
775 // finally, send event:
776 if (dvc->HandleWindowEvent(event) && event.IsAllowed())
777 {
778 dragOperation = NSDragOperationEvery;
779 ++indexDraggedItem;
780 }
781 else
782 {
783 dragOperation = NSDragOperationNone;
784 indexDraggedItem = noOfDraggedItems; // stop loop
785 }
786 }
787 else
788 {
789 dragOperation = NSDragOperationNone;
790 indexDraggedItem = noOfDraggedItems; // stop loop
791 }
792 // clean-up:
793 delete dataObjects;
e86edab0 794 }
e86edab0
RR
795 }
796 else
797 {
17f5d137
VZ
798 // needed to convert internally used UTF-16 representation to a UTF-8
799 // representation
800 CFDataRef osxData;
8e59cbe4
VZ
801 wxDataObjectComposite* dataObjects (new wxDataObjectComposite());
802 wxTextDataObject* textDataObject(new wxTextDataObject());
803
804 osxData = ::CFStringCreateExternalRepresentation(kCFAllocatorDefault,(CFStringRef)[pasteboard stringForType:NSStringPboardType],kCFStringEncodingUTF8,32);
805 if (textDataObject->SetData(::CFDataGetLength(osxData),::CFDataGetBytePtr(osxData)))
806 dataObjects->Add(textDataObject);
807 else
808 delete textDataObject;
809 // send event if data could be copied:
810 if (dataObjects->GetFormatCount() > 0)
811 {
812 event.SetDataObject(dataObjects);
813 event.SetDataFormat(implementation->GetDnDDataFormat(dataObjects));
814 if (dvc->HandleWindowEvent(event) && event.IsAllowed())
815 dragOperation = NSDragOperationEvery;
816 else
817 dragOperation = NSDragOperationNone;
818 }
e86edab0 819 else
8e59cbe4
VZ
820 dragOperation = NSDragOperationNone;
821 // clean up:
822 ::CFRelease(osxData);
823 delete dataObjects;
e86edab0 824 }
8e59cbe4
VZ
825
826 return dragOperation;
e86edab0
RR
827}
828
829-(BOOL) outlineView:(NSOutlineView*)outlineView writeItems:(NSArray*)writeItems toPasteboard:(NSPasteboard*)pasteboard
e86edab0 830{
17f5d137
VZ
831 // the pasteboard will be filled up with an array containing the data as
832 // returned by the events (including the data type) and a concatenation of
833 // text (string) data; the text data will only be put onto the pasteboard
834 // if for all items a string representation exists
8e59cbe4 835 wxDataViewCtrl* const dvc = implementation->GetDataViewCtrl();
608129e5 836
8e59cbe4 837 wxDataViewItemArray dataViewItems;
e86edab0
RR
838
839
8e59cbe4
VZ
840 wxCHECK_MSG(dvc, false,"Pointer to data view control not set correctly.");
841 wxCHECK_MSG(dvc->GetModel(),false,"Pointer to model not set correctly.");
e86edab0 842
8e59cbe4
VZ
843 if ([writeItems count] > 0)
844 {
845 bool dataStringAvailable(true); // a flag indicating if for all items a data string is available
846 NSMutableArray* dataArray = [[NSMutableArray arrayWithCapacity:[writeItems count]] retain]; // data of all items
847 wxString dataString; // contains the string data of all items
e86edab0 848
17f5d137
VZ
849 // send a begin drag event for all selected items and proceed with
850 // dragging unless the event is vetoed:
8e59cbe4
VZ
851 wxDataViewEvent
852 event(wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG,dvc->GetId());
e86edab0 853
8e59cbe4
VZ
854 event.SetEventObject(dvc);
855 event.SetModel(dvc->GetModel());
856 for (size_t itemCounter=0; itemCounter<[writeItems count]; ++itemCounter)
e86edab0 857 {
8e59cbe4
VZ
858 bool itemStringAvailable(false); // a flag indicating if for the current item a string is available
859 wxDataObjectComposite* itemObject(new wxDataObjectComposite()); // data object for current item
860 wxString itemString; // contains the TAB concatenated data of an item
861
2406d534
VZ
862 event.SetItem(
863 wxDataViewItemFromItem([writeItems objectAtIndex:itemCounter]));
8e59cbe4
VZ
864 itemString = ::ConcatenateDataViewItemValues(dvc,event.GetItem());
865 itemObject->Add(new wxTextDataObject(itemString));
866 event.SetDataObject(itemObject);
867 // check if event has not been vetoed:
868 if (dvc->HandleWindowEvent(event) && event.IsAllowed() && (event.GetDataObject()->GetFormatCount() > 0))
869 {
8e59cbe4 870 size_t const noOfFormats = event.GetDataObject()->GetFormatCount();
8e59cbe4
VZ
871 wxDataFormat* dataFormats(new wxDataFormat[noOfFormats]);
872
873 event.GetDataObject()->GetAllFormats(dataFormats,wxDataObject::Get);
874 for (size_t formatCounter=0; formatCounter<noOfFormats; ++formatCounter)
875 {
876 // constant definitions for abbreviational purposes:
877 wxDataFormatId const idDataFormat = dataFormats[formatCounter].GetType();
878 size_t const dataSize = event.GetDataObject()->GetDataSize(idDataFormat);
879 size_t const dataBufferSize = sizeof(wxDataFormatId)+dataSize;
880 // variable definitions (used in all case statements):
881 wxMemoryBuffer dataBuffer(dataBufferSize);
882
883 dataBuffer.AppendData(&idDataFormat,sizeof(wxDataFormatId));
884 switch (idDataFormat)
885 {
886 case wxDF_TEXT:
17f5d137
VZ
887 // otherwise wxDF_UNICODETEXT already filled up
888 // the string; and the UNICODE representation has
889 // priority
890 if (!itemStringAvailable)
8e59cbe4
VZ
891 {
892 event.GetDataObject()->GetDataHere(wxDF_TEXT,dataBuffer.GetAppendBuf(dataSize));
893 dataBuffer.UngetAppendBuf(dataSize);
894 [dataArray addObject:[NSData dataWithBytes:dataBuffer.GetData() length:dataBufferSize]];
895 itemString = wxString(static_cast<char const*>(dataBuffer.GetData())+sizeof(wxDataFormatId),wxConvLocal);
896 itemStringAvailable = true;
897 }
898 break;
899 case wxDF_UNICODETEXT:
900 {
901 event.GetDataObject()->GetDataHere(wxDF_UNICODETEXT,dataBuffer.GetAppendBuf(dataSize));
902 dataBuffer.UngetAppendBuf(dataSize);
903 if (itemStringAvailable) // does an object already exist as an ASCII text (see wxDF_TEXT case statement)?
904 [dataArray replaceObjectAtIndex:itemCounter withObject:[NSData dataWithBytes:dataBuffer.GetData() length:dataBufferSize]];
905 else
906 [dataArray addObject:[NSData dataWithBytes:dataBuffer.GetData() length:dataBufferSize]];
907 itemString = wxString::FromUTF8(static_cast<char const*>(dataBuffer.GetData())+sizeof(wxDataFormatId),dataSize);
908 itemStringAvailable = true;
909 } /* block */
910 break;
911 default:
912 wxFAIL_MSG("Data object has invalid or unsupported data format");
913 [dataArray release];
914 return NO;
915 }
916 }
917 delete[] dataFormats;
918 delete itemObject;
919 if (dataStringAvailable)
920 if (itemStringAvailable)
921 {
922 if (itemCounter > 0)
923 dataString << wxT('\n');
924 dataString << itemString;
925 }
926 else
927 dataStringAvailable = false;
928 }
929 else
930 {
931 [dataArray release];
932 delete itemObject;
933 return NO; // dragging was vetoed or no data available
934 }
e86edab0 935 }
e86edab0 936 if (dataStringAvailable)
8e59cbe4
VZ
937 {
938 wxCFStringRef osxString(dataString);
608129e5 939
8e59cbe4
VZ
940 [pasteboard declareTypes:[NSArray arrayWithObjects:DataViewPboardType,NSStringPboardType,nil] owner:nil];
941 [pasteboard setPropertyList:dataArray forType:DataViewPboardType];
942 [pasteboard setString:osxString.AsNSString() forType:NSStringPboardType];
943 }
944 else
945 {
946 [pasteboard declareTypes:[NSArray arrayWithObject:DataViewPboardType] owner:nil];
947 [pasteboard setPropertyList:dataArray forType:DataViewPboardType];
948 }
949 return YES;
e86edab0
RR
950 }
951 else
8e59cbe4 952 return NO; // no items to drag (should never occur)
e86edab0
RR
953}
954
955//
956// buffer handling
957//
958-(void) addToBuffer:(wxPointerObject*)item
959{
8e59cbe4 960 [items addObject:item];
e86edab0
RR
961}
962
963-(void) clearBuffer
964{
8e59cbe4 965 [items removeAllObjects];
e86edab0
RR
966}
967
8e59cbe4 968-(wxPointerObject*) getDataViewItemFromBuffer:(const wxDataViewItem&)item
e86edab0 969{
8e59cbe4 970 return [items member:[[[wxPointerObject alloc] initWithPointer:item.GetID()] autorelease]];
e86edab0
RR
971}
972
973-(wxPointerObject*) getItemFromBuffer:(wxPointerObject*)item
974{
8e59cbe4 975 return [items member:item];
e86edab0
RR
976}
977
978-(BOOL) isInBuffer:(wxPointerObject*)item
979{
8e59cbe4 980 return [items containsObject:item];
e86edab0
RR
981}
982
983-(void) removeFromBuffer:(wxPointerObject*)item
984{
8e59cbe4 985 [items removeObject:item];
e86edab0
RR
986}
987
988//
989// children handling
990//
991-(void) appendChild:(wxPointerObject*)item
992{
8e59cbe4 993 [children addObject:item];
e86edab0
RR
994}
995
996-(void) clearChildren
997{
8e59cbe4 998 [children removeAllObjects];
e86edab0
RR
999}
1000
1001-(wxPointerObject*) getChild:(NSUInteger)index
1002{
8e59cbe4 1003 return [children objectAtIndex:index];
e86edab0
RR
1004}
1005
1006-(NSUInteger) getChildCount
1007{
8e59cbe4 1008 return [children count];
e86edab0
RR
1009}
1010
1011-(void) removeChild:(NSUInteger)index
1012{
8e59cbe4 1013 [children removeObjectAtIndex:index];
e86edab0
RR
1014}
1015
1016//
1017// buffer handling
1018//
1019-(void) clearBuffers
1020{
8e59cbe4
VZ
1021 [self clearBuffer];
1022 [self clearChildren];
1023 [self setCurrentParentItem:nil];
e86edab0
RR
1024}
1025
1026//
1027// sorting
1028//
1029-(NSArray*) sortDescriptors
1030{
8e59cbe4 1031 return sortDescriptors;
e86edab0
RR
1032}
1033
1034-(void) setSortDescriptors:(NSArray*)newSortDescriptors
1035{
8e59cbe4
VZ
1036 [newSortDescriptors retain];
1037 [sortDescriptors release];
1038 sortDescriptors = newSortDescriptors;
e86edab0
RR
1039}
1040
1041//
1042// access to wxWidget's implementation
1043//
1044-(wxPointerObject*) currentParentItem
1045{
8e59cbe4 1046 return currentParentItem;
e86edab0
RR
1047}
1048
1049-(wxCocoaDataViewControl*) implementation
1050{
8e59cbe4 1051 return implementation;
e86edab0
RR
1052}
1053
1054-(wxDataViewModel*) model
1055{
8e59cbe4 1056 return model;
e86edab0
RR
1057}
1058
1059-(void) setCurrentParentItem:(wxPointerObject*)newCurrentParentItem
1060{
8e59cbe4
VZ
1061 [newCurrentParentItem retain];
1062 [currentParentItem release];
1063 currentParentItem = newCurrentParentItem;
e86edab0
RR
1064}
1065
1066-(void) setImplementation:(wxCocoaDataViewControl*) newImplementation
1067{
8e59cbe4 1068 implementation = newImplementation;
e86edab0
RR
1069}
1070
1071-(void) setModel:(wxDataViewModel*) newModel
1072{
8e59cbe4 1073 model = newModel;
e86edab0
RR
1074}
1075
1076//
1077// other methods
1078//
1079-(void) bufferItem:(wxPointerObject*)parentItem withChildren:(wxDataViewItemArray*)dataViewChildrenPtr
1080{
8e59cbe4
VZ
1081 NSInteger const noOfChildren = (*dataViewChildrenPtr).GetCount();
1082
1083 [self setCurrentParentItem:parentItem];
1084 [self clearChildren];
1085 for (NSInteger indexChild=0; indexChild<noOfChildren; ++indexChild)
e86edab0 1086 {
8e59cbe4
VZ
1087 wxPointerObject* bufferedPointerObject;
1088 wxPointerObject* newPointerObject([[wxPointerObject alloc] initWithPointer:(*dataViewChildrenPtr)[indexChild].GetID()]);
1089
1090 // The next statement and test looks strange but there is
1091 // unfortunately no workaround: due to the fact that two pointer
1092 // objects are identical if their pointers are identical - because the
1093 // method isEqual has been overloaded - the set operation will only
1094 // add a new pointer object if there is not already one in the set
1095 // having the same pointer. On the other side the children's array
1096 // would always add the new pointer object. This means that different
1097 // pointer objects are stored in the set and array. This will finally
1098 // lead to a crash as objects diverge. To solve this issue it is first
1099 // tested if the child already exists in the set and if it is the case
1100 // the sets object is going to be appended to the array, otheriwse the
1101 // new pointer object is added to the set and array:
1102 bufferedPointerObject = [self getItemFromBuffer:newPointerObject];
1103 if (bufferedPointerObject == nil)
1104 {
1105 [items addObject:newPointerObject];
1106 [children addObject:newPointerObject];
1107 }
1108 else
1109 [children addObject:bufferedPointerObject];
1110 [newPointerObject release];
e86edab0 1111 }
e86edab0
RR
1112}
1113
1114@end
1115
1116// ============================================================================
1117// wxCustomCell
1118// ============================================================================
a8afd748 1119
e86edab0 1120@implementation wxCustomCell
a8afd748 1121
e86edab0
RR
1122-(NSSize) cellSize
1123{
a8afd748
VZ
1124 wxCustomRendererObject * const
1125 obj = static_cast<wxCustomRendererObject *>([self objectValue]);
e86edab0
RR
1126
1127
a8afd748
VZ
1128 const wxSize size = obj->customRenderer->GetSize();
1129 return NSMakeSize(size.x, size.y);
e86edab0
RR
1130}
1131
1132//
1133// implementations
1134//
1135-(void) drawWithFrame:(NSRect)cellFrame inView:(NSView*)controlView
1136{
a8afd748
VZ
1137 wxCustomRendererObject * const
1138 obj = static_cast<wxCustomRendererObject *>([self objectValue]);
eda34276
VZ
1139 if ( !obj )
1140 {
1141 // this may happen for the custom cells in container rows: they don't
1142 // have any values
1143 return;
1144 }
1145
a8afd748 1146 wxDataViewCustomRenderer * const renderer = obj->customRenderer;
e86edab0 1147
62265c2c
VZ
1148 wxDC * const dc = renderer->GetDC();
1149 renderer->WXCallRender(wxFromNSRect(controlView, cellFrame), dc, 0);
a8afd748 1150 renderer->SetDC(NULL);
e86edab0 1151}
e86edab0
RR
1152
1153-(NSRect) imageRectForBounds:(NSRect)cellFrame
1154{
8e59cbe4 1155 return cellFrame;
e86edab0
RR
1156}
1157
1158-(NSRect) titleRectForBounds:(NSRect)cellFrame
1159{
8e59cbe4 1160 return cellFrame;
e86edab0
RR
1161}
1162
1163@end
1164
1165// ============================================================================
1166// wxImageTextCell
1167// ============================================================================
1168@implementation wxImageTextCell
1169//
1170// initialization
1171//
1172-(id) init
1173{
8e59cbe4
VZ
1174 self = [super init];
1175 if (self != nil)
1176 {
1177 // initializing the text part:
1178 [self setSelectable:YES];
1179 // initializing the image part:
1180 image = nil;
1181 imageSize = NSMakeSize(16,16);
1182 spaceImageText = 5.0;
1183 xImageShift = 5.0;
1184 }
1185 return self;
e86edab0
RR
1186}
1187
1188-(id) copyWithZone:(NSZone*)zone
1189{
8e59cbe4 1190 wxImageTextCell* cell;
608129e5
VZ
1191
1192
8e59cbe4
VZ
1193 cell = (wxImageTextCell*) [super copyWithZone:zone];
1194 cell->image = [image retain];
1195 cell->imageSize = imageSize;
1196 cell->spaceImageText = spaceImageText;
1197 cell->xImageShift = xImageShift;
e86edab0 1198
8e59cbe4 1199 return cell;
e86edab0
RR
1200}
1201
1202-(void) dealloc
1203{
8e59cbe4 1204 [image release];
e86edab0 1205
8e59cbe4 1206 [super dealloc];
e86edab0
RR
1207}
1208
1209//
1210// alignment
1211//
1212-(NSTextAlignment) alignment
1213{
8e59cbe4 1214 return cellAlignment;
e86edab0
RR
1215}
1216
1217-(void) setAlignment:(NSTextAlignment)newAlignment
1218{
8e59cbe4
VZ
1219 cellAlignment = newAlignment;
1220 switch (newAlignment)
1221 {
1222 case NSCenterTextAlignment:
1223 case NSLeftTextAlignment:
1224 case NSJustifiedTextAlignment:
1225 case NSNaturalTextAlignment:
1226 [super setAlignment:NSLeftTextAlignment];
1227 break;
1228 case NSRightTextAlignment:
1229 [super setAlignment:NSRightTextAlignment];
1230 break;
1231 default:
1232 wxFAIL_MSG("Unknown alignment type.");
1233 }
e86edab0
RR
1234}
1235
1236//
1237// image access
1238//
1239-(NSImage*) image
1240{
8e59cbe4 1241 return image;
e86edab0
RR
1242}
1243
1244-(void) setImage:(NSImage*)newImage
1245{
8e59cbe4
VZ
1246 [newImage retain];
1247 [image release];
1248 image = newImage;
e86edab0
RR
1249}
1250
1251-(NSSize) imageSize
1252{
8e59cbe4 1253 return imageSize;
e86edab0
RR
1254}
1255
1256-(void) setImageSize:(NSSize) newImageSize
1257{
8e59cbe4 1258 imageSize = newImageSize;
e86edab0
RR
1259}
1260
1261//
1262// other methods
1263//
1264-(NSSize) cellImageSize
1265{
8e59cbe4 1266 return NSMakeSize(imageSize.width+xImageShift+spaceImageText,imageSize.height);
e86edab0
RR
1267}
1268
1269-(NSSize) cellSize
1270{
8e59cbe4 1271 NSSize cellSize([super cellSize]);
e86edab0
RR
1272
1273
8e59cbe4
VZ
1274 if (imageSize.height > cellSize.height)
1275 cellSize.height = imageSize.height;
1276 cellSize.width += imageSize.width+xImageShift+spaceImageText;
e86edab0 1277
8e59cbe4 1278 return cellSize;
e86edab0
RR
1279}
1280
1281-(NSSize) cellTextSize
1282{
8e59cbe4 1283 return [super cellSize];
e86edab0
RR
1284}
1285
1286//
1287// implementations
1288//
1289-(void) determineCellParts:(NSRect)cellFrame imagePart:(NSRect*)imageFrame textPart:(NSRect*)textFrame
1290{
8e59cbe4
VZ
1291 switch (cellAlignment)
1292 {
1293 case NSCenterTextAlignment:
1294 {
1295 CGFloat const cellSpace = cellFrame.size.width-[self cellSize].width;
1296
17f5d137
VZ
1297 // if the cell's frame is smaller than its contents (at least
1298 // in x-direction) make sure that the image is visible:
1299 if (cellSpace <= 0)
8e59cbe4
VZ
1300 NSDivideRect(cellFrame,imageFrame,textFrame,xImageShift+imageSize.width+spaceImageText,NSMinXEdge);
1301 else // otherwise center the image and text in the cell's frame
1302 NSDivideRect(cellFrame,imageFrame,textFrame,xImageShift+imageSize.width+spaceImageText+0.5*cellSpace,NSMinXEdge);
1303 }
1304 break;
1305 case NSJustifiedTextAlignment:
1306 case NSLeftTextAlignment:
1307 case NSNaturalTextAlignment: // how to determine the natural writing direction? TODO
1308 NSDivideRect(cellFrame,imageFrame,textFrame,xImageShift+imageSize.width+spaceImageText,NSMinXEdge);
1309 break;
1310 case NSRightTextAlignment:
1311 {
1312 CGFloat const cellSpace = cellFrame.size.width-[self cellSize].width;
1313
17f5d137
VZ
1314 // if the cell's frame is smaller than its contents (at least
1315 // in x-direction) make sure that the image is visible:
1316 if (cellSpace <= 0)
8e59cbe4
VZ
1317 NSDivideRect(cellFrame,imageFrame,textFrame,xImageShift+imageSize.width+spaceImageText,NSMinXEdge);
1318 else // otherwise right align the image and text in the cell's frame
1319 NSDivideRect(cellFrame,imageFrame,textFrame,xImageShift+imageSize.width+spaceImageText+cellSpace,NSMinXEdge);
1320 }
1321 break;
1322 default:
1323 *imageFrame = NSZeroRect;
1324 *textFrame = NSZeroRect;
1325 wxFAIL_MSG("Unhandled alignment type.");
1326 }
e86edab0
RR
1327}
1328
1329-(void) drawWithFrame:(NSRect)cellFrame inView:(NSView*)controlView
1330{
8e59cbe4 1331 NSRect textFrame, imageFrame;
e86edab0
RR
1332
1333
8e59cbe4
VZ
1334 [self determineCellParts:cellFrame imagePart:&imageFrame textPart:&textFrame];
1335 // draw the image part by ourselves;
17f5d137
VZ
1336 // check if the cell has to draw its own background (checking is done by
1337 // the parameter of the textfield's cell):
8e59cbe4 1338 if ([self drawsBackground])
e86edab0 1339 {
8e59cbe4
VZ
1340 [[self backgroundColor] set];
1341 NSRectFill(imageFrame);
e86edab0 1342 }
8e59cbe4 1343 if (image != nil)
e86edab0 1344 {
17f5d137
VZ
1345 // the image is slightly shifted (xImageShift) and has a fixed size
1346 // but the image's frame might be larger and starts currently on the
1347 // left side of the cell's frame; therefore, the origin and the
1348 // image's frame size have to be adjusted:
8e59cbe4
VZ
1349 if (imageFrame.size.width >= xImageShift+imageSize.width+spaceImageText)
1350 {
1351 imageFrame.origin.x += imageFrame.size.width-imageSize.width-spaceImageText;
1352 imageFrame.size.width = imageSize.width;
1353 }
1354 else
1355 {
1356 imageFrame.origin.x += xImageShift;
1357 imageFrame.size.width -= xImageShift+spaceImageText;
1358 }
1359 // ...and the image has to be centered in the y-direction:
1360 if (imageFrame.size.height > imageSize.height)
1361 imageFrame.size.height = imageSize.height;
1362 imageFrame.origin.y += ceil(0.5*(cellFrame.size.height-imageFrame.size.height));
e86edab0 1363
17f5d137
VZ
1364 // according to the documentation the coordinate system should be
1365 // flipped for NSTableViews (y-coordinate goes from top to bottom); to
1366 // draw an image correctly the coordinate system has to be transformed
1367 // to a bottom-top coordinate system, otherwise the image's
8e59cbe4
VZ
1368 // content is flipped:
1369 NSAffineTransform* coordinateTransform([NSAffineTransform transform]);
608129e5 1370
8e59cbe4
VZ
1371 if ([controlView isFlipped])
1372 {
1373 [coordinateTransform scaleXBy: 1.0 yBy:-1.0]; // first the coordinate system is brought back to bottom-top orientation
1374 [coordinateTransform translateXBy:0.0 yBy:(-2.0)*imageFrame.origin.y-imageFrame.size.height]; // the coordinate system has to be moved to compensate for the
1375 [coordinateTransform concat]; // other orientation and the position of the image's frame
1376 }
1377 [image drawInRect:imageFrame fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0]; // suggested method to draw the image
1378 // instead of compositeToPoint:operation:
17f5d137
VZ
1379 // take back previous transformation (if the view is not flipped the
1380 // coordinate transformation matrix contains the identity matrix and
1381 // the next two operations do not change the content's transformation
1382 // matrix):
8e59cbe4
VZ
1383 [coordinateTransform invert];
1384 [coordinateTransform concat];
e86edab0 1385 }
8e59cbe4 1386 // let the textfield cell draw the text part:
17f5d137
VZ
1387 if (textFrame.size.width > [self cellTextSize].width)
1388 {
1389 // for unknown reasons the alignment of the text cell is ignored;
1390 // therefore change the size so that alignment does not influence the
1391 // visualization anymore
1392 textFrame.size.width = [self cellTextSize].width;
1393 }
8e59cbe4 1394 [super drawWithFrame:textFrame inView:controlView];
e86edab0
RR
1395}
1396
1397-(void) editWithFrame:(NSRect)aRect inView:(NSView*)controlView editor:(NSText*)textObj delegate:(id)anObject event:(NSEvent*)theEvent
1398{
8e59cbe4 1399 NSRect textFrame, imageFrame;
e86edab0
RR
1400
1401
8e59cbe4
VZ
1402 [self determineCellParts:aRect imagePart:&imageFrame textPart:&textFrame];
1403 [super editWithFrame:textFrame inView:controlView editor:textObj delegate:anObject event:theEvent];
e86edab0
RR
1404}
1405
1406#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
1407-(NSUInteger) hitTestForEvent:(NSEvent*)event inRect:(NSRect)cellFrame ofView:(NSView*)controlView
1408{
8e59cbe4 1409 NSPoint point = [controlView convertPoint:[event locationInWindow] fromView:nil];
e86edab0 1410
8e59cbe4 1411 NSRect imageFrame, textFrame;
e86edab0
RR
1412
1413
8e59cbe4
VZ
1414 [self determineCellParts:cellFrame imagePart:&imageFrame textPart:&textFrame];
1415 if (image != nil)
e86edab0 1416 {
8e59cbe4
VZ
1417 // the image is shifted...
1418 if (imageFrame.size.width >= xImageShift+imageSize.width+spaceImageText)
1419 {
1420 imageFrame.origin.x += imageFrame.size.width-imageSize.width-spaceImageText;
1421 imageFrame.size.width = imageSize.width;
1422 }
1423 else
1424 {
1425 imageFrame.origin.x += xImageShift;
1426 imageFrame.size.width -= xImageShift+spaceImageText;
1427 }
1428 // ...and centered:
1429 if (imageFrame.size.height > imageSize.height)
1430 imageFrame.size.height = imageSize.height;
1431 imageFrame.origin.y += ceil(0.5*(cellFrame.size.height-imageFrame.size.height));
17f5d137
VZ
1432 // If the point is in the image rect, then it is a content hit (see
1433 // documentation for hitTestForEvent:inRect:ofView):
8e59cbe4
VZ
1434 if (NSMouseInRect(point, imageFrame, [controlView isFlipped]))
1435 return NSCellHitContentArea;
e86edab0 1436 }
8e59cbe4 1437 // if the image was not hit let's try the text part:
17f5d137
VZ
1438 if (textFrame.size.width > [self cellTextSize].width)
1439 {
1440 // for unknown reasons the alignment of the text cell is ignored;
1441 // therefore change the size so that alignment does not influence the
1442 // visualization anymore
1443 textFrame.size.width = [self cellTextSize].width;
1444 }
1445
8e59cbe4 1446 return [super hitTestForEvent:event inRect:textFrame ofView:controlView];
e86edab0
RR
1447}
1448#endif
1449
1450-(NSRect) imageRectForBounds:(NSRect)cellFrame
1451{
8e59cbe4 1452 NSRect textFrame, imageFrame;
e86edab0
RR
1453
1454
8e59cbe4
VZ
1455 [self determineCellParts:cellFrame imagePart:&imageFrame textPart:&textFrame];
1456 if (imageFrame.size.width >= xImageShift+imageSize.width+spaceImageText)
1457 {
1458 imageFrame.origin.x += imageFrame.size.width-imageSize.width-spaceImageText;
1459 imageFrame.size.width = imageSize.width;
1460 }
1461 else
1462 {
1463 imageFrame.origin.x += xImageShift;
1464 imageFrame.size.width -= xImageShift+spaceImageText;
1465 }
1466 // ...and centered:
1467 if (imageFrame.size.height > imageSize.height)
1468 imageFrame.size.height = imageSize.height;
1469 imageFrame.origin.y += ceil(0.5*(cellFrame.size.height-imageFrame.size.height));
608129e5 1470
8e59cbe4 1471 return imageFrame;
e86edab0
RR
1472}
1473
1474-(void) selectWithFrame:(NSRect)aRect inView:(NSView*)controlView editor:(NSText*)textObj delegate:(id)anObject start:(NSInteger)selStart length:(NSInteger)selLength
1475{
8e59cbe4 1476 NSRect textFrame, imageFrame;
e86edab0
RR
1477
1478
8e59cbe4
VZ
1479 [self determineCellParts:aRect imagePart:&imageFrame textPart:&textFrame];
1480 [super selectWithFrame:textFrame inView:controlView editor:textObj delegate:anObject start:selStart length:selLength];
e86edab0
RR
1481}
1482
1483-(NSRect) titleRectForBounds:(NSRect)cellFrame
1484{
8e59cbe4 1485 NSRect textFrame, imageFrame;
e86edab0
RR
1486
1487
8e59cbe4
VZ
1488 [self determineCellParts:cellFrame imagePart:&imageFrame textPart:&textFrame];
1489 return textFrame;
e86edab0
RR
1490}
1491
1492@end
1493
1494// ============================================================================
1495// wxCocoaOutlineView
1496// ============================================================================
1497@implementation wxCocoaOutlineView
1498
1499//
1500// initializers / destructor
1501//
1502-(id) init
1503{
8e59cbe4
VZ
1504 self = [super init];
1505 if (self != nil)
1506 {
1507 currentlyEditedColumn =
1508 currentlyEditedRow = -1;
1509
1510 [self registerForDraggedTypes:[NSArray arrayWithObjects:DataViewPboardType,NSStringPboardType,nil]];
1511 [self setDelegate:self];
1512 [self setDoubleAction:@selector(actionDoubleClick:)];
1513 [self setDraggingSourceOperationMask:NSDragOperationEvery forLocal:NO];
1514 [self setDraggingSourceOperationMask:NSDragOperationEvery forLocal:YES];
1515 [self setTarget:self];
1516 }
1517 return self;
e86edab0
RR
1518}
1519
1520//
1521// access to wxWidget's implementation
1522//
1523-(wxCocoaDataViewControl*) implementation
1524{
8e59cbe4 1525 return implementation;
e86edab0
RR
1526}
1527
1528-(void) setImplementation:(wxCocoaDataViewControl*) newImplementation
1529{
8e59cbe4 1530 implementation = newImplementation;
e86edab0
RR
1531}
1532
1533//
1534// actions
1535//
1536-(void) actionDoubleClick:(id)sender
e86edab0 1537{
17f5d137
VZ
1538 // actually the documentation (NSTableView 2007-10-31) for doubleAction:
1539 // and setDoubleAction: seems to be wrong as this action message is always
1540 // sent whether the cell is editable or not
8e59cbe4 1541 wxDataViewCtrl* const dvc = implementation->GetDataViewCtrl();
e86edab0 1542
17f5d137 1543 wxDataViewEvent event(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED,dvc->GetId());
e86edab0
RR
1544
1545
8e59cbe4 1546 event.SetEventObject(dvc);
2406d534 1547 event.SetItem(wxDataViewItemFromItem([self itemAtRow:[self clickedRow]]));
8e59cbe4 1548 dvc->GetEventHandler()->ProcessEvent(event);
e86edab0
RR
1549}
1550
1551
1552//
1553// contextual menus
1554//
1555-(NSMenu*) menuForEvent:(NSEvent*)theEvent
e86edab0 1556{
17f5d137
VZ
1557 // this method does not do any special menu event handling but only sends
1558 // an event message; therefore, the user has full control if a context
1559 // menu should be shown or not
8e59cbe4 1560 wxDataViewCtrl* const dvc = implementation->GetDataViewCtrl();
608129e5 1561
8e59cbe4 1562 wxDataViewEvent event(wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU,dvc->GetId());
e86edab0 1563
8e59cbe4 1564 wxDataViewItemArray selectedItems;
e86edab0
RR
1565
1566
8e59cbe4
VZ
1567 event.SetEventObject(dvc);
1568 event.SetModel(dvc->GetModel());
1569 // get the item information;
17f5d137
VZ
1570 // theoretically more than one ID can be returned but the event can only
1571 // handle one item, therefore only the first item of the array is
1572 // returned:
8e59cbe4
VZ
1573 if (dvc->GetSelections(selectedItems) > 0)
1574 event.SetItem(selectedItems[0]);
1575 dvc->GetEventHandler()->ProcessEvent(event);
1576 // nothing is done:
1577 return nil;
e86edab0
RR
1578}
1579
1580//
1581// delegate methods
1582//
1583-(void) outlineView:(NSOutlineView*)outlineView mouseDownInHeaderOfTableColumn:(NSTableColumn*)tableColumn
1584{
8e59cbe4 1585 wxDataViewColumn* const col(static_cast<wxDataViewColumn*>([[tableColumn identifier] pointer]));
e86edab0 1586
8e59cbe4 1587 wxDataViewCtrl* const dvc = implementation->GetDataViewCtrl();
e86edab0 1588
8e59cbe4
VZ
1589 wxDataViewEvent
1590 event(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK,dvc->GetId());
e86edab0 1591
608129e5 1592
8e59cbe4
VZ
1593 // first, send an event that the user clicked into a column's header:
1594 event.SetEventObject(dvc);
1595 event.SetColumn(dvc->GetColumnPosition(col));
1596 event.SetDataViewColumn(col);
1597 dvc->HandleWindowEvent(event);
608129e5 1598
8e59cbe4 1599 // now, check if the click may have had an influence on sorting, too;
17f5d137
VZ
1600 // the sorting setup has to be done only if the clicked table column is
1601 // sortable and has not been used for sorting before the click; if the
1602 // column is already responsible for sorting the native control changes
1603 // the sorting direction automatically and informs the data source via
1604 // outlineView:sortDescriptorsDidChange:
8e59cbe4
VZ
1605 if (col->IsSortable() && ([tableColumn sortDescriptorPrototype] == nil))
1606 {
17f5d137
VZ
1607 // remove the sort order from the previously sorted column table (it
1608 // can also be that no sorted column table exists):
8e59cbe4
VZ
1609 UInt32 const noOfColumns = [outlineView numberOfColumns];
1610
1611 for (UInt32 i=0; i<noOfColumns; ++i)
1612 [[[outlineView tableColumns] objectAtIndex:i] setSortDescriptorPrototype:nil];
1613 // make column table sortable:
1614 NSArray* sortDescriptors;
1615 NSSortDescriptor* sortDescriptor;
1616
1617 sortDescriptor = [[NSSortDescriptor alloc] initWithKey:[NSString stringWithFormat:@"%d",[outlineView columnWithIdentifier:[tableColumn identifier]]]
1618 ascending:YES];
1619 sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
1620 [tableColumn setSortDescriptorPrototype:sortDescriptor];
1621 [outlineView setSortDescriptors:sortDescriptors];
1622 [sortDescriptor release];
1623 }
e86edab0
RR
1624}
1625
1626-(BOOL) outlineView:(NSOutlineView*)outlineView shouldCollapseItem:(id)item
1627{
8e59cbe4 1628 wxDataViewCtrl* const dvc = implementation->GetDataViewCtrl();
608129e5 1629
17f5d137 1630 wxDataViewEvent event(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING,dvc->GetId());
e86edab0
RR
1631
1632
8e59cbe4 1633 event.SetEventObject(dvc);
2406d534 1634 event.SetItem (wxDataViewItemFromItem(item));
8e59cbe4
VZ
1635 event.SetModel (dvc->GetModel());
1636 // finally send the equivalent wxWidget event:
1637 dvc->GetEventHandler()->ProcessEvent(event);
1638 // opening the container is allowed if not vetoed:
1639 return event.IsAllowed();
e86edab0
RR
1640}
1641
1642-(BOOL) outlineView:(NSOutlineView*)outlineView shouldExpandItem:(id)item
1643{
8e59cbe4 1644 wxDataViewCtrl* const dvc = implementation->GetDataViewCtrl();
608129e5 1645
17f5d137 1646 wxDataViewEvent event(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING,dvc->GetId());
e86edab0
RR
1647
1648
8e59cbe4 1649 event.SetEventObject(dvc);
2406d534 1650 event.SetItem (wxDataViewItemFromItem(item));
8e59cbe4
VZ
1651 event.SetModel (dvc->GetModel());
1652 // finally send the equivalent wxWidget event:
1653 dvc->GetEventHandler()->ProcessEvent(event);
1654 // opening the container is allowed if not vetoed:
1655 return event.IsAllowed();
e86edab0
RR
1656}
1657
1658-(BOOL) outlineView:(NSOutlineView*)outlineView shouldSelectTableColumn:(NSTableColumn*)tableColumn
1659{
8e59cbe4 1660 return NO;
e86edab0
RR
1661}
1662
f6b3bfba 1663-(void) outlineView:(wxCocoaOutlineView*)outlineView
b0607dd2
VZ
1664 willDisplayCell:(id)cell
1665 forTableColumn:(NSTableColumn*)tableColumn
1666 item:(id)item
f6b3bfba
VZ
1667{
1668 wxDataViewCtrl * const dvc = implementation->GetDataViewCtrl();
1669 wxDataViewModel * const model = dvc->GetModel();
1670
1671 wxDataViewColumn * const
8e59cbe4
VZ
1672 dvCol(static_cast<wxDataViewColumn*>(
1673 [[tableColumn identifier] pointer]
1674 )
1675 );
eda34276
VZ
1676 const unsigned colIdx = dvCol->GetModelColumn();
1677
2406d534 1678 wxDataViewItem dvItem(wxDataViewItemFromItem(item));
eda34276
VZ
1679
1680 if ( !model->HasValue(dvItem, colIdx) )
1681 return;
f6b3bfba 1682
09a0ece0
VZ
1683 wxDataViewRenderer * const renderer = dvCol->GetRenderer();
1684 wxDataViewRendererNativeData * const data = renderer->GetNativeData();
1685
b0607dd2 1686 // let the renderer know about what it's going to render next
f6b3bfba
VZ
1687 data->SetColumnPtr(tableColumn);
1688 data->SetItem(item);
1689 data->SetItemCell(cell);
e86edab0 1690
b0607dd2
VZ
1691 // use the attributes: notice that we need to do this whether we have them
1692 // or not as even if this cell doesn't have any attributes, the previous
1693 // one might have had some and then we need to reset them back to default
1694 wxDataViewItemAttr attr;
1695 model->GetAttr(dvItem, colIdx, attr);
1696 renderer->OSXApplyAttr(attr);
1697
1698 // and finally do draw it
f6b3bfba 1699 renderer->MacRender();
e86edab0
RR
1700}
1701
1702//
1703// notifications
1704//
1705-(void) outlineViewColumnDidMove:(NSNotification*)notification
1706{
8e59cbe4 1707 int const newColumnPosition = [[[notification userInfo] objectForKey:@"NSNewColumn"] intValue];
e86edab0 1708
8e59cbe4 1709 wxDataViewColumn* const col(static_cast<wxDataViewColumn*>([[[[self tableColumns] objectAtIndex:newColumnPosition] identifier] pointer]));
e86edab0 1710
8e59cbe4 1711 wxDataViewCtrl* const dvc = implementation->GetDataViewCtrl();
608129e5 1712
8e59cbe4 1713 wxDataViewEvent event(wxEVT_COMMAND_DATAVIEW_COLUMN_REORDERED,dvc->GetId());
e86edab0
RR
1714
1715
8e59cbe4
VZ
1716 event.SetEventObject(dvc);
1717 event.SetColumn(dvc->GetColumnPosition(col));
1718 event.SetDataViewColumn(col);
1719 dvc->GetEventHandler()->ProcessEvent(event);
e86edab0
RR
1720}
1721
1722-(void) outlineViewItemDidCollapse:(NSNotification*)notification
1723{
8e59cbe4 1724 wxDataViewCtrl* const dvc = implementation->GetDataViewCtrl();
608129e5 1725
8e59cbe4 1726 wxDataViewEvent event(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED,dvc->GetId());
e86edab0
RR
1727
1728
8e59cbe4 1729 event.SetEventObject(dvc);
2406d534
VZ
1730 event.SetItem(wxDataViewItemFromItem(
1731 [[notification userInfo] objectForKey:@"NSObject"]));
8e59cbe4 1732 dvc->GetEventHandler()->ProcessEvent(event);
e86edab0
RR
1733}
1734
1735-(void) outlineViewItemDidExpand:(NSNotification*)notification
1736{
8e59cbe4 1737 wxDataViewCtrl* const dvc = implementation->GetDataViewCtrl();
608129e5 1738
8e59cbe4 1739 wxDataViewEvent event(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED,dvc->GetId());
e86edab0
RR
1740
1741
8e59cbe4 1742 event.SetEventObject(dvc);
2406d534
VZ
1743 event.SetItem(wxDataViewItemFromItem(
1744 [[notification userInfo] objectForKey:@"NSObject"]));
8e59cbe4 1745 dvc->GetEventHandler()->ProcessEvent(event);
e86edab0
RR
1746}
1747
1748-(void) outlineViewSelectionDidChange:(NSNotification*)notification
1749{
8e59cbe4 1750 wxDataViewCtrl* const dvc = implementation->GetDataViewCtrl();
e86edab0 1751
17f5d137 1752 wxDataViewEvent event(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED,dvc->GetId());
e86edab0 1753
8e59cbe4 1754 event.SetEventObject(dvc);
f49f59d0
VZ
1755 event.SetModel(dvc->GetModel());
1756 event.SetItem(dvc->GetSelection());
8e59cbe4 1757 dvc->GetEventHandler()->ProcessEvent(event);
e86edab0
RR
1758}
1759
1760-(void) textDidBeginEditing:(NSNotification*)notification
e86edab0 1761{
17f5d137
VZ
1762 // this notification is only sent if the user started modifying the cell
1763 // (not when the user clicked into the cell and the cell's editor is
1764 // called!)
1765
1766 // call method of superclass (otherwise editing does not work correctly -
1767 // the outline data source class is not informed about a change of data):
8e59cbe4 1768 [super textDidBeginEditing:notification];
e86edab0 1769
8e59cbe4
VZ
1770 // remember the column being edited, it will be used in textDidEndEditing:
1771 currentlyEditedColumn = [self editedColumn];
1772 currentlyEditedRow = [self editedRow];
f32eb964 1773
8e59cbe4
VZ
1774 wxDataViewColumn* const col =
1775 static_cast<wxDataViewColumn*>(
1776 [[[[self tableColumns] objectAtIndex:currentlyEditedColumn] identifier] pointer]);
e86edab0 1777
8e59cbe4 1778 wxDataViewCtrl* const dvc = implementation->GetDataViewCtrl();
e86edab0
RR
1779
1780
8e59cbe4
VZ
1781 // stop editing of a custom item first (if necessary)
1782 dvc->FinishCustomItemEditing();
e86edab0 1783
8e59cbe4
VZ
1784 // now, send the event:
1785 wxDataViewEvent
17f5d137 1786 event(wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED,dvc->GetId());
e86edab0 1787
8e59cbe4
VZ
1788 event.SetEventObject(dvc);
1789 event.SetItem(
2406d534 1790 wxDataViewItemFromItem([self itemAtRow:currentlyEditedRow]));
8e59cbe4
VZ
1791 event.SetColumn(dvc->GetColumnPosition(col));
1792 event.SetDataViewColumn(col);
1793 dvc->GetEventHandler()->ProcessEvent(event);
e86edab0
RR
1794}
1795
1796-(void) textDidEndEditing:(NSNotification*)notification
1797{
17f5d137
VZ
1798 // call method of superclass (otherwise editing does not work correctly -
1799 // the outline data source class is not informed about a change of data):
8e59cbe4 1800 [super textDidEndEditing:notification];
e86edab0 1801
17f5d137
VZ
1802 // under OSX an event indicating the end of an editing session can be sent
1803 // even if no event indicating a start of an editing session has been sent
1804 // (see Documentation for NSControl controlTextDidEndEditing:); this is
1805 // not expected by a user of the wxWidgets library and therefore an
1806 // wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE event is only sent if a
1807 // corresponding wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED has been sent
1808 // before; to check if a wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED has
1809 // been sent the last edited column/row are valid:
8e59cbe4
VZ
1810 if ( currentlyEditedColumn != -1 && currentlyEditedRow != -1 )
1811 {
1812 wxDataViewColumn* const col =
1813 static_cast<wxDataViewColumn*>(
1814 [[[[self tableColumns] objectAtIndex:currentlyEditedColumn] identifier] pointer]);
1815
1816 wxDataViewCtrl* const dvc = implementation->GetDataViewCtrl();
e86edab0 1817
8e59cbe4
VZ
1818 // send event to wxWidgets:
1819 wxDataViewEvent
17f5d137 1820 event(wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE,dvc->GetId());
e86edab0 1821
8e59cbe4
VZ
1822 event.SetEventObject(dvc);
1823 event.SetItem(
2406d534 1824 wxDataViewItemFromItem([self itemAtRow:currentlyEditedRow]));
8e59cbe4
VZ
1825 event.SetColumn(dvc->GetColumnPosition(col));
1826 event.SetDataViewColumn(col);
1827 dvc->GetEventHandler()->ProcessEvent(event);
f32eb964
VZ
1828
1829
8e59cbe4
VZ
1830 // we're not editing any more
1831 currentlyEditedColumn =
1832 currentlyEditedRow = -1;
1833 }
e86edab0
RR
1834}
1835
1836@end
2406d534 1837
e86edab0
RR
1838// ============================================================================
1839// wxCocoaDataViewControl
1840// ============================================================================
2406d534
VZ
1841
1842wxCocoaDataViewControl::wxCocoaDataViewControl(wxWindow* peer,
1843 const wxPoint& pos,
1844 const wxSize& size,
1845 long style)
1846 : wxWidgetCocoaImpl
1847 (
1848 peer,
1849 [[NSScrollView alloc] initWithFrame:wxOSXGetFrameForControl(peer,pos,size)]
1850 ),
1851 m_DataSource(NULL),
1852 m_OutlineView([[wxCocoaOutlineView alloc] init])
e86edab0 1853{
8e59cbe4 1854 // initialize scrollview (the outline view is part of a scrollview):
2406d534 1855 NSScrollView* scrollview = (NSScrollView*) GetWXWidget();
e86edab0 1856
8e59cbe4
VZ
1857 [scrollview setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
1858 [scrollview setBorderType:NSNoBorder];
1859 [scrollview setHasVerticalScroller:YES];
1860 [scrollview setHasHorizontalScroller:YES];
1861 [scrollview setAutohidesScrollers:YES];
1862 [scrollview setDocumentView:m_OutlineView];
e86edab0 1863
a35169b6
VZ
1864 // initialize the native control itself too
1865 InitOutlineView(style);
1866}
e86edab0 1867
a35169b6
VZ
1868void wxCocoaDataViewControl::InitOutlineView(long style)
1869{
8e59cbe4
VZ
1870 [m_OutlineView setImplementation:this];
1871 [m_OutlineView setColumnAutoresizingStyle:NSTableViewSequentialColumnAutoresizingStyle];
1872 [m_OutlineView setIndentationPerLevel:GetDataViewCtrl()->GetIndent()];
a35169b6 1873 NSUInteger maskGridStyle(NSTableViewGridNone);
8e59cbe4
VZ
1874 if (style & wxDV_HORIZ_RULES)
1875 maskGridStyle |= NSTableViewSolidHorizontalGridLineMask;
1876 if (style & wxDV_VERT_RULES)
1877 maskGridStyle |= NSTableViewSolidVerticalGridLineMask;
1878 [m_OutlineView setGridStyleMask:maskGridStyle];
1879 [m_OutlineView setAllowsMultipleSelection: (style & wxDV_MULTIPLE) != 0];
1880 [m_OutlineView setUsesAlternatingRowBackgroundColors:(style & wxDV_ROW_LINES) != 0];
a35169b6
VZ
1881
1882 if ( style & wxDV_NO_HEADER )
1883 [m_OutlineView setHeaderView:nil];
e86edab0
RR
1884}
1885
de40d736 1886wxCocoaDataViewControl::~wxCocoaDataViewControl()
e86edab0 1887{
8e59cbe4
VZ
1888 [m_DataSource release];
1889 [m_OutlineView release];
e86edab0
RR
1890}
1891
1892//
1893// column related methods (inherited from wxDataViewWidgetImpl)
1894//
de40d736 1895bool wxCocoaDataViewControl::ClearColumns()
e86edab0 1896{
2406d534
VZ
1897 // as there is a bug in NSOutlineView version (OSX 10.5.6 #6555162) the
1898 // columns cannot be deleted if there is an outline column in the view;
8e59cbe4
VZ
1899 // therefore, the whole view is deleted and newly constructed:
1900 [m_OutlineView release];
1901 m_OutlineView = [[wxCocoaOutlineView alloc] init];
1902 [((NSScrollView*) GetWXWidget()) setDocumentView:m_OutlineView];
8e59cbe4 1903 [m_OutlineView setDataSource:m_DataSource];
a35169b6
VZ
1904
1905 InitOutlineView(GetDataViewCtrl()->GetWindowStyle());
1906
8e59cbe4 1907 return true;
e86edab0
RR
1908}
1909
1910bool wxCocoaDataViewControl::DeleteColumn(wxDataViewColumn* columnPtr)
1911{
8e59cbe4
VZ
1912 if ([m_OutlineView outlineTableColumn] == columnPtr->GetNativeData()->GetNativeColumnPtr())
1913 [m_OutlineView setOutlineTableColumn:nil]; // due to a bug this does not work
1914 [m_OutlineView removeTableColumn:columnPtr->GetNativeData()->GetNativeColumnPtr()]; // due to a confirmed bug #6555162 the deletion does not work for
1915 // outline table columns (... and there is no workaround)
1916 return (([m_OutlineView columnWithIdentifier:[[[wxPointerObject alloc] initWithPointer:columnPtr] autorelease]]) == -1);
e86edab0
RR
1917}
1918
8e59cbe4 1919void wxCocoaDataViewControl::DoSetExpanderColumn(const wxDataViewColumn *columnPtr)
e86edab0 1920{
8e59cbe4 1921 [m_OutlineView setOutlineTableColumn:columnPtr->GetNativeData()->GetNativeColumnPtr()];
e86edab0
RR
1922}
1923
1924wxDataViewColumn* wxCocoaDataViewControl::GetColumn(unsigned int pos) const
1925{
8e59cbe4 1926 return static_cast<wxDataViewColumn*>([[[[m_OutlineView tableColumns] objectAtIndex:pos] identifier] pointer]);
e86edab0
RR
1927}
1928
8e59cbe4 1929int wxCocoaDataViewControl::GetColumnPosition(const wxDataViewColumn *columnPtr) const
e86edab0 1930{
8e59cbe4 1931 return [m_OutlineView columnWithIdentifier:[[[wxPointerObject alloc] initWithPointer:const_cast<wxDataViewColumn*>(columnPtr)] autorelease]];
e86edab0
RR
1932}
1933
1934bool wxCocoaDataViewControl::InsertColumn(unsigned int pos, wxDataViewColumn* columnPtr)
1935{
8e59cbe4
VZ
1936 // create column and set the native data of the dataview column:
1937 NSTableColumn *nativeColumn = ::CreateNativeColumn(columnPtr);
1938 columnPtr->GetNativeData()->SetNativeColumnPtr(nativeColumn);
2406d534
VZ
1939 // as the native control does not allow the insertion of a column at a
1940 // specified position the column is first appended and - if necessary -
1941 // moved to its final position:
8e59cbe4
VZ
1942 [m_OutlineView addTableColumn:nativeColumn];
1943 if (pos != static_cast<unsigned int>([m_OutlineView numberOfColumns]-1))
1944 [m_OutlineView moveColumn:[m_OutlineView numberOfColumns]-1 toColumn:pos];
1945 // done:
1946 return true;
e86edab0
RR
1947}
1948
1949//
1950// item related methods (inherited from wxDataViewWidgetImpl)
1951//
8e59cbe4 1952bool wxCocoaDataViewControl::Add(const wxDataViewItem& parent, const wxDataViewItem& WXUNUSED(item))
e86edab0 1953{
8e59cbe4
VZ
1954 if (parent.IsOk())
1955 [m_OutlineView reloadItem:[m_DataSource getDataViewItemFromBuffer:parent] reloadChildren:YES];
1956 else
1957 [m_OutlineView reloadData];
1958 return true;
e86edab0
RR
1959}
1960
8e59cbe4 1961bool wxCocoaDataViewControl::Add(const wxDataViewItem& parent, const wxDataViewItemArray& WXUNUSED(items))
e86edab0 1962{
8e59cbe4
VZ
1963 if (parent.IsOk())
1964 [m_OutlineView reloadItem:[m_DataSource getDataViewItemFromBuffer:parent] reloadChildren:YES];
1965 else
1966 [m_OutlineView reloadData];
1967 return true;
e86edab0
RR
1968}
1969
8e59cbe4 1970void wxCocoaDataViewControl::Collapse(const wxDataViewItem& item)
e86edab0 1971{
8e59cbe4 1972 [m_OutlineView collapseItem:[m_DataSource getDataViewItemFromBuffer:item]];
e86edab0
RR
1973}
1974
8e59cbe4 1975void wxCocoaDataViewControl::EnsureVisible(const wxDataViewItem& item, const wxDataViewColumn *columnPtr)
e86edab0 1976{
8e59cbe4
VZ
1977 if (item.IsOk())
1978 {
1979 [m_OutlineView scrollRowToVisible:[m_OutlineView rowForItem:[m_DataSource getDataViewItemFromBuffer:item]]];
1980 if (columnPtr)
1981 [m_OutlineView scrollColumnToVisible:GetColumnPosition(columnPtr)];
1982 }
e86edab0
RR
1983}
1984
8e59cbe4 1985void wxCocoaDataViewControl::Expand(const wxDataViewItem& item)
e86edab0 1986{
8e59cbe4 1987 [m_OutlineView expandItem:[m_DataSource getDataViewItemFromBuffer:item]];
e86edab0
RR
1988}
1989
de40d736 1990unsigned int wxCocoaDataViewControl::GetCount() const
e86edab0 1991{
8e59cbe4 1992 return [m_OutlineView numberOfRows];
e86edab0
RR
1993}
1994
8e59cbe4 1995wxRect wxCocoaDataViewControl::GetRectangle(const wxDataViewItem& item, const wxDataViewColumn *columnPtr)
e86edab0 1996{
8e59cbe4
VZ
1997 return wxFromNSRect([m_osxView superview],[m_OutlineView frameOfCellAtColumn:GetColumnPosition(columnPtr)
1998 row:[m_OutlineView rowForItem:[m_DataSource getDataViewItemFromBuffer:item]]]);
e86edab0
RR
1999}
2000
8e59cbe4 2001bool wxCocoaDataViewControl::IsExpanded(const wxDataViewItem& item) const
e86edab0 2002{
8e59cbe4 2003 return [m_OutlineView isItemExpanded:[m_DataSource getDataViewItemFromBuffer:item]];
e86edab0
RR
2004}
2005
de40d736 2006bool wxCocoaDataViewControl::Reload()
e86edab0 2007{
8e59cbe4
VZ
2008 [m_DataSource clearBuffers];
2009 [m_OutlineView scrollColumnToVisible:0];
2010 [m_OutlineView scrollRowToVisible:0];
2011 [m_OutlineView reloadData];
2012 return true;
e86edab0
RR
2013}
2014
8e59cbe4 2015bool wxCocoaDataViewControl::Remove(const wxDataViewItem& parent, const wxDataViewItem& WXUNUSED(item))
e86edab0 2016{
8e59cbe4
VZ
2017 if (parent.IsOk())
2018 [m_OutlineView reloadItem:[m_DataSource getDataViewItemFromBuffer:parent] reloadChildren:YES];
2019 else
2020 [m_OutlineView reloadData];
2021 return true;
e86edab0
RR
2022}
2023
8e59cbe4 2024bool wxCocoaDataViewControl::Remove(const wxDataViewItem& parent, const wxDataViewItemArray& WXUNUSED(item))
e86edab0 2025{
8e59cbe4
VZ
2026 if (parent.IsOk())
2027 [m_OutlineView reloadItem:[m_DataSource getDataViewItemFromBuffer:parent] reloadChildren:YES];
2028 else
2029 [m_OutlineView reloadData];
2030 return true;
e86edab0
RR
2031}
2032
8e59cbe4 2033bool wxCocoaDataViewControl::Update(const wxDataViewColumn *columnPtr)
e86edab0 2034{
8e59cbe4 2035 return false;
e86edab0
RR
2036}
2037
8e59cbe4 2038bool wxCocoaDataViewControl::Update(const wxDataViewItem& WXUNUSED(parent), const wxDataViewItem& item)
e86edab0 2039{
8e59cbe4
VZ
2040 [m_OutlineView reloadItem:[m_DataSource getDataViewItemFromBuffer:item]];
2041 return true;
e86edab0
RR
2042}
2043
8e59cbe4 2044bool wxCocoaDataViewControl::Update(const wxDataViewItem& WXUNUSED(parent), const wxDataViewItemArray& items)
e86edab0 2045{
8e59cbe4
VZ
2046 for (size_t i=0; i<items.GetCount(); ++i)
2047 [m_OutlineView reloadItem:[m_DataSource getDataViewItemFromBuffer:items[i]]];
2048 return true;
e86edab0
RR
2049}
2050
2051//
2052// model related methods
2053//
2054bool wxCocoaDataViewControl::AssociateModel(wxDataViewModel* model)
2055{
8e59cbe4
VZ
2056 [m_DataSource release];
2057 if (model)
2058 {
2059 m_DataSource = [[wxCocoaOutlineDataSource alloc] init];
2060 [m_DataSource setImplementation:this];
2061 [m_DataSource setModel:model];
2062 }
2063 else
2064 m_DataSource = NULL;
2065 [m_OutlineView setDataSource:m_DataSource]; // if there is a data source the data is immediately going to be requested
2066 return true;
e86edab0
RR
2067}
2068
2069//
2070// selection related methods (inherited from wxDataViewWidgetImpl)
2071//
80ce465c
VZ
2072
2073wxDataViewItem wxCocoaDataViewControl::GetCurrentItem() const
2074{
2075 return wxDataViewItem([[m_OutlineView itemAtRow:[m_OutlineView selectedRow]] pointer]);
2076}
2077
2078void wxCocoaDataViewControl::SetCurrentItem(const wxDataViewItem& item)
2079{
2080 // We can't have unselected current item in a NSTableView, as the
2081 // documentation of its deselectRow method explains, the control will
2082 // automatically change the current item to the closest still selected item
2083 // if the current item is deselected. So we have no choice but to select
2084 // the item in the same time as making it current.
2085 Select(item);
2086}
2087
e86edab0
RR
2088int wxCocoaDataViewControl::GetSelections(wxDataViewItemArray& sel) const
2089{
8e59cbe4 2090 NSIndexSet* selectedRowIndexes([m_OutlineView selectedRowIndexes]);
608129e5 2091
8e59cbe4 2092 NSUInteger indexRow;
e86edab0 2093
608129e5 2094
8e59cbe4
VZ
2095 sel.Empty();
2096 sel.Alloc([selectedRowIndexes count]);
2097 indexRow = [selectedRowIndexes firstIndex];
2098 while (indexRow != NSNotFound)
2099 {
2100 sel.Add(wxDataViewItem([[m_OutlineView itemAtRow:indexRow] pointer]));
2101 indexRow = [selectedRowIndexes indexGreaterThanIndex:indexRow];
2102 }
2103 return sel.GetCount();
e86edab0
RR
2104}
2105
8e59cbe4 2106bool wxCocoaDataViewControl::IsSelected(const wxDataViewItem& item) const
e86edab0 2107{
8e59cbe4 2108 return [m_OutlineView isRowSelected:[m_OutlineView rowForItem:[m_DataSource getDataViewItemFromBuffer:item]]];
e86edab0
RR
2109}
2110
8e59cbe4 2111void wxCocoaDataViewControl::Select(const wxDataViewItem& item)
e86edab0 2112{
8e59cbe4
VZ
2113 if (item.IsOk())
2114 [m_OutlineView selectRowIndexes:[NSIndexSet indexSetWithIndex:[m_OutlineView rowForItem:[m_DataSource getDataViewItemFromBuffer:item]]]
1e184300 2115 byExtendingSelection:GetDataViewCtrl()->HasFlag(wxDV_MULTIPLE) ? YES : NO];
e86edab0
RR
2116}
2117
de40d736 2118void wxCocoaDataViewControl::SelectAll()
e86edab0 2119{
8e59cbe4 2120 [m_OutlineView selectAll:m_OutlineView];
e86edab0
RR
2121}
2122
8e59cbe4 2123void wxCocoaDataViewControl::Unselect(const wxDataViewItem& item)
e86edab0 2124{
8e59cbe4
VZ
2125 if (item.IsOk())
2126 [m_OutlineView deselectRow:[m_OutlineView rowForItem:[m_DataSource getDataViewItemFromBuffer:item]]];
e86edab0
RR
2127}
2128
de40d736 2129void wxCocoaDataViewControl::UnselectAll()
e86edab0 2130{
8e59cbe4 2131 [m_OutlineView deselectAll:m_OutlineView];
e86edab0
RR
2132}
2133
2134//
2135// sorting related methods
2136//
de40d736 2137wxDataViewColumn* wxCocoaDataViewControl::GetSortingColumn() const
e86edab0 2138{
8e59cbe4 2139 NSArray* const columns = [m_OutlineView tableColumns];
e86edab0 2140
8e59cbe4 2141 UInt32 const noOfColumns = [columns count];
e86edab0
RR
2142
2143
8e59cbe4
VZ
2144 for (UInt32 i=0; i<noOfColumns; ++i)
2145 if ([[columns objectAtIndex:i] sortDescriptorPrototype] != nil)
2146 return static_cast<wxDataViewColumn*>([[[columns objectAtIndex:i] identifier] pointer]);
2147 return NULL;
e86edab0
RR
2148}
2149
de40d736 2150void wxCocoaDataViewControl::Resort()
e86edab0 2151{
8e59cbe4
VZ
2152 [m_DataSource clearChildren];
2153 [m_OutlineView reloadData];
e86edab0
RR
2154}
2155
2156//
2157// other methods (inherited from wxDataViewWidgetImpl)
2158//
2159void wxCocoaDataViewControl::DoSetIndent(int indent)
2160{
8e59cbe4 2161 [m_OutlineView setIndentationPerLevel:static_cast<CGFloat>(indent)];
e86edab0
RR
2162}
2163
8e59cbe4 2164void wxCocoaDataViewControl::HitTest(const wxPoint& point, wxDataViewItem& item, wxDataViewColumn*& columnPtr) const
e86edab0 2165{
8e59cbe4 2166 NSPoint const nativePoint = wxToNSPoint((NSScrollView*) GetWXWidget(),point);
e86edab0 2167
8e59cbe4
VZ
2168 int indexColumn;
2169 int indexRow;
e86edab0 2170
608129e5 2171
8e59cbe4
VZ
2172 indexColumn = [m_OutlineView columnAtPoint:nativePoint];
2173 indexRow = [m_OutlineView rowAtPoint: nativePoint];
2174 if ((indexColumn >= 0) && (indexRow >= 0))
2175 {
2176 columnPtr = static_cast<wxDataViewColumn*>([[[[m_OutlineView tableColumns] objectAtIndex:indexColumn] identifier] pointer]);
2177 item = wxDataViewItem([[m_OutlineView itemAtRow:indexRow] pointer]);
2178 }
2179 else
2180 {
2181 columnPtr = NULL;
2182 item = wxDataViewItem();
2183 }
e86edab0
RR
2184}
2185
8e59cbe4
VZ
2186void wxCocoaDataViewControl::SetRowHeight(const wxDataViewItem& WXUNUSED(item), unsigned int WXUNUSED(height))
2187 // Not supported by the native control
e86edab0
RR
2188{
2189}
2190
de40d736 2191void wxCocoaDataViewControl::OnSize()
e86edab0 2192{
8e59cbe4
VZ
2193 if ([m_OutlineView numberOfColumns] == 1)
2194 [m_OutlineView sizeLastColumnToFit];
e86edab0
RR
2195}
2196
2197//
2198// drag & drop helper methods
2199//
2200wxDataFormat wxCocoaDataViewControl::GetDnDDataFormat(wxDataObjectComposite* dataObjects)
2201{
8e59cbe4
VZ
2202 wxDataFormat resultFormat;
2203 if ( !dataObjects )
2204 return resultFormat;
e86edab0 2205
e86edab0
RR
2206 bool compatible(true);
2207
2208 size_t const noOfFormats = dataObjects->GetFormatCount();
2209 size_t indexFormat;
2210
2211 wxDataFormat* formats;
608129e5 2212
8e59cbe4
VZ
2213 // get all formats and check afterwards if the formats are compatible; if
2214 // they are compatible the preferred format is returned otherwise
2215 // wxDF_INVALID is returned;
2216 // currently compatible types (ordered by priority are):
2217 // - wxDF_UNICODETEXT - wxDF_TEXT
e86edab0
RR
2218 formats = new wxDataFormat[noOfFormats];
2219 dataObjects->GetAllFormats(formats);
2220 indexFormat = 0;
2221 while ((indexFormat < noOfFormats) && compatible)
2222 {
8e59cbe4
VZ
2223 switch (resultFormat.GetType())
2224 {
2225 case wxDF_INVALID:
2226 resultFormat.SetType(formats[indexFormat].GetType()); // first format (should only be reached if indexFormat == 0)
2227 break;
2228 case wxDF_TEXT:
2229 if (formats[indexFormat].GetType() == wxDF_UNICODETEXT)
2230 resultFormat.SetType(wxDF_UNICODETEXT);
2231 else // incompatible
2232 {
2233 resultFormat.SetType(wxDF_INVALID);
2234 compatible = false;
2235 }
2236 break;
2237 case wxDF_UNICODETEXT:
2238 if (formats[indexFormat].GetType() != wxDF_TEXT)
2239 {
2240 resultFormat.SetType(wxDF_INVALID);
2241 compatible = false;
2242 }
2243 break;
2244 default:
2245 resultFormat.SetType(wxDF_INVALID); // not (yet) supported format
2246 compatible = false;
2247 }
2248 ++indexFormat;
2249 }
2250
e86edab0 2251 delete[] formats;
8e59cbe4
VZ
2252
2253 return resultFormat;
e86edab0
RR
2254}
2255
2256wxDataObjectComposite* wxCocoaDataViewControl::GetDnDDataObjects(NSData* dataObject) const
2257{
8e59cbe4 2258 wxDataFormatId dataFormatID;
e86edab0 2259
608129e5 2260
8e59cbe4
VZ
2261 [dataObject getBytes:&dataFormatID length:sizeof(wxDataFormatId)];
2262 switch (dataFormatID)
2263 {
2264 case wxDF_TEXT:
2265 case wxDF_UNICODETEXT:
2266 {
2267 wxTextDataObject* textDataObject(new wxTextDataObject());
608129e5 2268
8e59cbe4
VZ
2269 if (textDataObject->SetData(wxDataFormat(dataFormatID),[dataObject length]-sizeof(wxDataFormatId),static_cast<char const*>([dataObject bytes])+sizeof(wxDataFormatId)))
2270 {
2271 wxDataObjectComposite* dataObjectComposite(new wxDataObjectComposite());
e86edab0 2272
8e59cbe4
VZ
2273 dataObjectComposite->Add(textDataObject);
2274 return dataObjectComposite;
2275 }
2276 else
2277 {
2278 delete textDataObject;
2279 return NULL;
2280 }
2281 }
2282 break;
2283 default:
2284 return NULL;
2285 }
e86edab0
RR
2286}
2287
eda34276
VZ
2288id wxCocoaDataViewControl::GetItemAtRow(int row) const
2289{
2290 return [m_OutlineView itemAtRow:row];
2291}
2292
c937bcac
VZ
2293// ----------------------------------------------------------------------------
2294// wxDataViewRendererNativeData
2295// ----------------------------------------------------------------------------
2296
2297void wxDataViewRendererNativeData::Init()
2298{
2299 m_origFont = NULL;
2300 m_origTextColour = NULL;
2301 m_ellipsizeMode = wxELLIPSIZE_MIDDLE;
2302
2303 if ( m_ColumnCell )
2304 ApplyLineBreakMode(m_ColumnCell);
2305}
2306
2307void wxDataViewRendererNativeData::ApplyLineBreakMode(NSCell *cell)
2308{
2309 NSLineBreakMode nsMode = NSLineBreakByWordWrapping;
2310 switch ( m_ellipsizeMode )
2311 {
2312 case wxELLIPSIZE_NONE:
2313 nsMode = NSLineBreakByClipping;
2314 break;
2315
2316 case wxELLIPSIZE_START:
2317 nsMode = NSLineBreakByTruncatingHead;
2318 break;
2319
2320 case wxELLIPSIZE_MIDDLE:
2321 nsMode = NSLineBreakByTruncatingMiddle;
2322 break;
2323
2324 case wxELLIPSIZE_END:
2325 nsMode = NSLineBreakByTruncatingTail;
2326 break;
2327 }
2328
2329 wxASSERT_MSG( nsMode != NSLineBreakByWordWrapping, "unknown wxEllipsizeMode" );
2330
2331 [cell setLineBreakMode: nsMode];
2332}
2333
e86edab0
RR
2334// ---------------------------------------------------------
2335// wxDataViewRenderer
2336// ---------------------------------------------------------
0599fe19
VZ
2337
2338wxDataViewRenderer::wxDataViewRenderer(const wxString& varianttype,
2339 wxDataViewCellMode mode,
2340 int align)
2341 : wxDataViewRendererBase(varianttype, mode, align),
2342 m_alignment(align),
2343 m_mode(mode),
2344 m_NativeDataPtr(NULL)
e86edab0
RR
2345{
2346}
2347
de40d736 2348wxDataViewRenderer::~wxDataViewRenderer()
e86edab0 2349{
8e59cbe4 2350 delete m_NativeDataPtr;
e86edab0
RR
2351}
2352
2353void wxDataViewRenderer::SetAlignment(int align)
2354{
8e59cbe4
VZ
2355 m_alignment = align;
2356 [GetNativeData()->GetColumnCell() setAlignment:ConvertToNativeHorizontalTextAlignment(align)];
e86edab0
RR
2357}
2358
2359void wxDataViewRenderer::SetMode(wxDataViewCellMode mode)
2360{
8e59cbe4
VZ
2361 m_mode = mode;
2362 if ( GetOwner() )
2363 [GetOwner()->GetNativeData()->GetNativeColumnPtr() setEditable:(mode == wxDATAVIEW_CELL_EDITABLE)];
e86edab0
RR
2364}
2365
2366void wxDataViewRenderer::SetNativeData(wxDataViewRendererNativeData* newNativeDataPtr)
2367{
8e59cbe4
VZ
2368 delete m_NativeDataPtr;
2369 m_NativeDataPtr = newNativeDataPtr;
e86edab0
RR
2370}
2371
c937bcac
VZ
2372void wxDataViewRenderer::EnableEllipsize(wxEllipsizeMode mode)
2373{
2374 // we need to store this value to apply it to the columns headerCell in
2375 // CreateNativeColumn()
2376 GetNativeData()->SetEllipsizeMode(mode);
2377
2378 // but we may already apply it to the column cell which will be used for
2379 // this column
2380 GetNativeData()->ApplyLineBreakMode(GetNativeData()->GetColumnCell());
2381}
2382
2383wxEllipsizeMode wxDataViewRenderer::GetEllipsizeMode() const
2384{
2385 return GetNativeData()->GetEllipsizeMode();
2386}
2387
9461dd8c
VZ
2388void
2389wxDataViewRenderer::OSXOnCellChanged(NSObject *object,
2390 const wxDataViewItem& item,
2391 unsigned col)
0599fe19 2392{
9461dd8c
VZ
2393 // TODO: we probably should get rid of this code entirely and make this
2394 // function pure virtual, but currently we still have some native
2395 // renderers (wxDataViewChoiceRenderer) which don't override it and
2396 // there is also wxDataViewCustomRenderer for which it's not obvious
2397 // how it should be implemented so keep this "auto-deduction" of
2398 // variant type from NSObject for now
2399
2400 wxVariant value;
2401 if ( [object isKindOfClass:[NSString class]] )
2402 value = ObjectToString(object);
2403 else if ( [object isKindOfClass:[NSNumber class]] )
2404 value = ObjectToLong(object);
2405 else if ( [object isKindOfClass:[NSDate class]] )
2406 value = ObjectToDate(object);
2407 else
2408 {
2409 wxFAIL_MSG( wxString::Format
2410 (
2411 "unknown value type %s",
2412 wxCFStringRef::AsString([object className])
2413 ));
2414 return;
2415 }
2416
0599fe19 2417 wxDataViewModel *model = GetOwner()->GetOwner()->GetModel();
795dac4c 2418 model->ChangeValue(value, item, col);
0599fe19
VZ
2419}
2420
b0607dd2
VZ
2421void wxDataViewRenderer::OSXApplyAttr(const wxDataViewItemAttr& attr)
2422{
2423 wxDataViewRendererNativeData * const data = GetNativeData();
2424 NSCell * const cell = data->GetItemCell();
2425
2426 // set the font and text colour to use: we need to do it if we had ever
2427 // changed them before, even if this item itself doesn't have any special
2428 // attributes as otherwise it would reuse the attributes from the previous
2429 // cell rendered using the same renderer
2430 NSFont *font = NULL;
2431 NSColor *colText = NULL;
2432
2433 if ( attr.HasFont() )
2434 {
2435 font = data->GetOriginalFont();
2436 if ( !font )
2437 {
2438 // this is the first time we're setting the font, remember the
2439 // original one before changing it
2440 font = [cell font];
2441 data->SaveOriginalFont(font);
2442 }
2443
2444 if ( font )
2445 {
2446 // FIXME: using wxFont methods here doesn't work for some reason
2447 NSFontManager * const fm = [NSFontManager sharedFontManager];
2448 if ( attr.GetBold() )
2449 font = [fm convertFont:font toHaveTrait:NSBoldFontMask];
2450 if ( attr.GetItalic() )
2451 font = [fm convertFont:font toHaveTrait:NSItalicFontMask];
2452 }
2453 //else: can't change font if the cell doesn't have any
2454 }
2455
2456 if ( attr.HasColour() )
2457 {
2458 // we can set font for any cell but only NSTextFieldCell provides
2459 // a method for setting text colour so check that this method is
2460 // available before using it
2461 if ( [cell respondsToSelector:@selector(setTextColor:)] &&
2462 [cell respondsToSelector:@selector(textColor)] )
2463 {
2464 if ( !data->GetOriginalTextColour() )
2465 {
2466 // the cast to (untyped) id is safe because of the check above
2467 data->SaveOriginalTextColour([(id)cell textColor]);
2468 }
2469
2470 const wxColour& c = attr.GetColour();
2471 colText = [NSColor colorWithDeviceRed:c.Red() / 255.
2472 green:c.Green() / 255.
2473 blue:c.Blue() / 255.
2474 alpha:c.Alpha() / 255.];
2475 }
2476 }
2477
2478 if ( !font )
2479 font = data->GetOriginalFont();
2480 if ( !colText )
2481 colText = data->GetOriginalTextColour();
2482
2483 if ( font )
2484 [cell setFont:font];
2485
2486 if ( colText )
2487 [(id)cell setTextColor:colText];
2488}
2489
e86edab0
RR
2490IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer,wxDataViewRendererBase)
2491
2492// ---------------------------------------------------------
2493// wxDataViewCustomRenderer
2494// ---------------------------------------------------------
8e59cbe4
VZ
2495wxDataViewCustomRenderer::wxDataViewCustomRenderer(const wxString& varianttype,
2496 wxDataViewCellMode mode,
2497 int align)
6eec70b9 2498 : wxDataViewCustomRendererBase(varianttype, mode, align),
8e59cbe4
VZ
2499 m_editorCtrlPtr(NULL),
2500 m_DCPtr(NULL)
e86edab0 2501{
8e59cbe4 2502 SetNativeData(new wxDataViewRendererNativeData([[wxCustomCell alloc] init]));
e86edab0
RR
2503}
2504
8f2a8de6 2505bool wxDataViewCustomRenderer::MacRender()
e86edab0 2506{
8e59cbe4
VZ
2507 [GetNativeData()->GetItemCell() setObjectValue:[[[wxCustomRendererObject alloc] initWithRenderer:this] autorelease]];
2508 return true;
e86edab0
RR
2509}
2510
b0607dd2
VZ
2511void wxDataViewCustomRenderer::OSXApplyAttr(const wxDataViewItemAttr& attr)
2512{
2513 // simply save the attribute so that it could be reused from our Render()
2514 SetAttr(attr);
2515
2516 // it's not necessary to call the base class version which sets the cell
2517 // properties to correspond to this attribute because we currently don't
2518 // use any NSCell methods in custom renderers anyhow but if we ever start
2519 // doing this (e.g. override RenderText() here to use NSTextFieldCell
2520 // methods), then we should pass it on to wxDataViewRenderer here
2521}
2522
e86edab0
RR
2523IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer, wxDataViewRenderer)
2524
2525// ---------------------------------------------------------
2526// wxDataViewTextRenderer
2527// ---------------------------------------------------------
8e59cbe4
VZ
2528wxDataViewTextRenderer::wxDataViewTextRenderer(const wxString& varianttype,
2529 wxDataViewCellMode mode,
2530 int align)
2531 : wxDataViewRenderer(varianttype,mode,align)
e86edab0 2532{
8e59cbe4 2533 NSTextFieldCell* cell;
608129e5
VZ
2534
2535
8e59cbe4
VZ
2536 cell = [[NSTextFieldCell alloc] init];
2537 [cell setAlignment:ConvertToNativeHorizontalTextAlignment(align)];
2538 SetNativeData(new wxDataViewRendererNativeData(cell));
2539 [cell release];
e86edab0
RR
2540}
2541
8f2a8de6 2542bool wxDataViewTextRenderer::MacRender()
e86edab0 2543{
8e59cbe4
VZ
2544 if (GetValue().GetType() == GetVariantType())
2545 {
2546 [GetNativeData()->GetItemCell() setObjectValue:wxCFStringRef(GetValue().GetString()).AsNSString()];
2547 return true;
2548 }
2549 else
2550 {
2551 wxFAIL_MSG(wxString("Text renderer cannot render value because of wrong value type; value type: ") << GetValue().GetType());
2552 return false;
2553 }
e86edab0
RR
2554}
2555
9461dd8c
VZ
2556void
2557wxDataViewTextRenderer::OSXOnCellChanged(NSObject *value,
2558 const wxDataViewItem& item,
2559 unsigned col)
2560{
2561 wxDataViewModel *model = GetOwner()->GetOwner()->GetModel();
2562 model->ChangeValue(ObjectToString(value), item, col);
2563}
2564
e86edab0
RR
2565IMPLEMENT_CLASS(wxDataViewTextRenderer,wxDataViewRenderer)
2566
2567// ---------------------------------------------------------
2568// wxDataViewBitmapRenderer
2569// ---------------------------------------------------------
8e59cbe4
VZ
2570wxDataViewBitmapRenderer::wxDataViewBitmapRenderer(const wxString& varianttype,
2571 wxDataViewCellMode mode,
2572 int align)
2573 : wxDataViewRenderer(varianttype,mode,align)
e86edab0 2574{
8e59cbe4 2575 NSImageCell* cell;
608129e5
VZ
2576
2577
8e59cbe4
VZ
2578 cell = [[NSImageCell alloc] init];
2579 SetNativeData(new wxDataViewRendererNativeData(cell));
2580 [cell release];
e86edab0
RR
2581}
2582
17f5d137
VZ
2583// This method returns 'true' if
2584// - the passed bitmap is valid and it could be assigned to the native data
2585// browser;
2586// - the passed bitmap is invalid (or is not initialized); this case
2587// simulates a non-existing bitmap.
2588// In all other cases the method returns 'false'.
8f2a8de6 2589bool wxDataViewBitmapRenderer::MacRender()
e86edab0 2590{
8e59cbe4 2591 wxCHECK_MSG(GetValue().GetType() == GetVariantType(),false,wxString("Bitmap renderer cannot render value; value type: ") << GetValue().GetType());
e86edab0 2592
8e59cbe4 2593 wxBitmap bitmap;
e86edab0 2594
8e59cbe4
VZ
2595 bitmap << GetValue();
2596 if (bitmap.IsOk())
2597 [GetNativeData()->GetItemCell() setObjectValue:[[bitmap.GetNSImage() retain] autorelease]];
2598 return true;
e86edab0
RR
2599}
2600
2601IMPLEMENT_CLASS(wxDataViewBitmapRenderer,wxDataViewRenderer)
2602
2603// -------------------------------------
2604// wxDataViewChoiceRenderer
2605// -------------------------------------
8e59cbe4
VZ
2606wxDataViewChoiceRenderer::wxDataViewChoiceRenderer(const wxArrayString& choices,
2607 wxDataViewCellMode mode,
2608 int alignment)
6eec70b9
VZ
2609 : wxDataViewRenderer(wxT("string"), mode, alignment),
2610 m_choices(choices)
e86edab0 2611{
8e59cbe4 2612 NSPopUpButtonCell* cell;
608129e5
VZ
2613
2614
8e59cbe4
VZ
2615 cell = [[NSPopUpButtonCell alloc] init];
2616 [cell setControlSize:NSMiniControlSize];
2617 [cell setFont:[[NSFont fontWithName:[[cell font] fontName] size:[NSFont systemFontSizeForControlSize:NSMiniControlSize]] autorelease]];
2618 for (size_t i=0; i<choices.GetCount(); ++i)
2619 [cell addItemWithTitle:[[wxCFStringRef(choices[i]).AsNSString() retain] autorelease]];
2620 SetNativeData(new wxDataViewRendererNativeData(cell));
2621 [cell release];
e86edab0
RR
2622}
2623
8f2a8de6 2624bool wxDataViewChoiceRenderer::MacRender()
e86edab0 2625{
8e59cbe4
VZ
2626 if (GetValue().GetType() == GetVariantType())
2627 {
2628 [((NSPopUpButtonCell*) GetNativeData()->GetItemCell()) selectItemWithTitle:[[wxCFStringRef(GetValue().GetString()).AsNSString() retain] autorelease]];
2629 return true;
2630 }
2631 else
2632 {
2633 wxFAIL_MSG(wxString("Choice renderer cannot render value because of wrong value type; value type: ") << GetValue().GetType());
2634 return false;
2635 }
e86edab0
RR
2636}
2637
2638IMPLEMENT_CLASS(wxDataViewChoiceRenderer,wxDataViewRenderer)
2639
2640// ---------------------------------------------------------
2641// wxDataViewDateRenderer
2642// ---------------------------------------------------------
8e59cbe4
VZ
2643
2644wxDataViewDateRenderer::wxDataViewDateRenderer(const wxString& varianttype,
2645 wxDataViewCellMode mode,
2646 int align)
2647 : wxDataViewRenderer(varianttype,mode,align)
e86edab0 2648{
8e59cbe4 2649 NSTextFieldCell* cell;
e86edab0 2650
8e59cbe4 2651 NSDateFormatter* dateFormatter;
e86edab0 2652
608129e5 2653
8e59cbe4
VZ
2654 dateFormatter = [[NSDateFormatter alloc] init];
2655 [dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
2656 [dateFormatter setDateStyle:NSDateFormatterShortStyle];
2657 cell = [[NSTextFieldCell alloc] init];
2658 [cell setFormatter:dateFormatter];
2659 SetNativeData(new wxDataViewRendererNativeData(cell,[NSDate dateWithString:@"2000-12-30 20:00:00 +0000"]));
2660 [cell release];
2661 [dateFormatter release];
e86edab0
RR
2662}
2663
8f2a8de6 2664bool wxDataViewDateRenderer::MacRender()
e86edab0 2665{
8e59cbe4 2666 if (GetValue().GetType() == GetVariantType())
e86edab0 2667 {
8e59cbe4 2668 if (GetValue().GetDateTime().IsValid())
e86edab0 2669 {
8e59cbe4 2670 // -- find best fitting style to show the date --
17f5d137
VZ
2671 // as the style should be identical for all cells a reference date
2672 // instead of the actual cell's date value is used for all cells;
2673 // this reference date is stored in the renderer's native data
2674 // section for speed purposes; otherwise, the reference date's
2675 // string has to be recalculated for each item that may become
2676 // timewise long if a lot of rows using dates exist; the algorithm
2677 // has the preference to display as much information as possible
2678 // in the first instance; but as this is often impossible due to
2679 // space restrictions the style is shortened per loop; finally, if
2680 // the shortest time and date format does not fit into the cell
2681 // the time part is dropped; remark: the time part itself is not
2682 // modified per iteration loop and only uses the short style,
2683 // means that only the hours and minutes are being shown
2684
2685 // GetObject() returns a date for testing the size of a date object
2686 [GetNativeData()->GetItemCell() setObjectValue:GetNativeData()->GetObject()];
8e59cbe4
VZ
2687 [[GetNativeData()->GetItemCell() formatter] setTimeStyle:NSDateFormatterShortStyle];
2688 for (int dateFormatterStyle=4; dateFormatterStyle>0; --dateFormatterStyle)
2689 {
2690 [[GetNativeData()->GetItemCell() formatter] setDateStyle:(NSDateFormatterStyle)dateFormatterStyle];
2691 if (dateFormatterStyle == 1)
2692 {
17f5d137
VZ
2693 // if the shortest style for displaying the date and time
2694 // is too long to be fully visible remove the time part of
2695 // the date:
8e59cbe4
VZ
2696 if ([GetNativeData()->GetItemCell() cellSize].width > [GetNativeData()->GetColumnPtr() width])
2697 [[GetNativeData()->GetItemCell() formatter] setTimeStyle:NSDateFormatterNoStyle];
17f5d137
VZ
2698 {
2699 // basically not necessary as the loop would end anyway
2700 // but let's save the last comparison
2701 break;
2702 }
8e59cbe4
VZ
2703 }
2704 else if ([GetNativeData()->GetItemCell() cellSize].width <= [GetNativeData()->GetColumnPtr() width])
2705 break;
2706 }
17f5d137
VZ
2707 // set data (the style is set by the previous loop); on OSX the
2708 // date has to be specified with respect to UTC; in wxWidgets the
2709 // date is always entered in the local timezone; so, we have to do
2710 // a conversion from the local to UTC timezone when adding the
2711 // seconds to 1970-01-01 UTC:
8e59cbe4 2712 [GetNativeData()->GetItemCell() setObjectValue:[NSDate dateWithTimeIntervalSince1970:GetValue().GetDateTime().ToUTC().Subtract(wxDateTime(1,wxDateTime::Jan,1970)).GetSeconds().ToDouble()]];
e86edab0 2713 }
8e59cbe4
VZ
2714 return true;
2715 }
2716 else
2717 {
2718 wxFAIL_MSG(wxString("Date renderer cannot render value because of wrong value type; value type: ") << GetValue().GetType());
2719 return false;
e86edab0 2720 }
e86edab0
RR
2721}
2722
9461dd8c
VZ
2723void
2724wxDataViewDateRenderer::OSXOnCellChanged(NSObject *value,
2725 const wxDataViewItem& item,
2726 unsigned col)
2727{
2728 wxDataViewModel *model = GetOwner()->GetOwner()->GetModel();
2729 model->ChangeValue(ObjectToDate(value), item, col);
2730}
2731
e86edab0
RR
2732IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer,wxDataViewRenderer)
2733
2734// ---------------------------------------------------------
2735// wxDataViewIconTextRenderer
2736// ---------------------------------------------------------
8e59cbe4
VZ
2737wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(const wxString& varianttype,
2738 wxDataViewCellMode mode,
2739 int align)
2740 : wxDataViewRenderer(varianttype,mode)
e86edab0 2741{
8e59cbe4 2742 wxImageTextCell* cell;
608129e5
VZ
2743
2744
8e59cbe4
VZ
2745 cell = [[wxImageTextCell alloc] init];
2746 [cell setAlignment:ConvertToNativeHorizontalTextAlignment(align)];
2747 SetNativeData(new wxDataViewRendererNativeData(cell));
2748 [cell release];
e86edab0
RR
2749}
2750
8f2a8de6 2751bool wxDataViewIconTextRenderer::MacRender()
e86edab0 2752{
8e59cbe4
VZ
2753 if (GetValue().GetType() == GetVariantType())
2754 {
2755 wxDataViewIconText iconText;
608129e5 2756
8e59cbe4 2757 wxImageTextCell* cell;
e86edab0 2758
8e59cbe4
VZ
2759 cell = (wxImageTextCell*) GetNativeData()->GetItemCell();
2760 iconText << GetValue();
2761 if (iconText.GetIcon().IsOk())
2762 [cell setImage:[[wxBitmap(iconText.GetIcon()).GetNSImage() retain] autorelease]];
2763 [cell setStringValue:[[wxCFStringRef(iconText.GetText()).AsNSString() retain] autorelease]];
2764 return true;
2765 }
2766 else
2767 {
2768 wxFAIL_MSG(wxString("Icon & text renderer cannot render value because of wrong value type; value type: ") << GetValue().GetType());
2769 return false;
2770 }
e86edab0
RR
2771}
2772
0599fe19 2773void
9461dd8c 2774wxDataViewIconTextRenderer::OSXOnCellChanged(NSObject *value,
0599fe19
VZ
2775 const wxDataViewItem& item,
2776 unsigned col)
2777{
0599fe19 2778 wxVariant valueIconText;
9461dd8c 2779 valueIconText << wxDataViewIconText(ObjectToString(value));
0599fe19 2780
9461dd8c
VZ
2781 wxDataViewModel *model = GetOwner()->GetOwner()->GetModel();
2782 model->ChangeValue(valueIconText, item, col);
0599fe19
VZ
2783}
2784
e86edab0
RR
2785IMPLEMENT_ABSTRACT_CLASS(wxDataViewIconTextRenderer,wxDataViewRenderer)
2786
2787// ---------------------------------------------------------
2788// wxDataViewToggleRenderer
2789// ---------------------------------------------------------
8e59cbe4
VZ
2790wxDataViewToggleRenderer::wxDataViewToggleRenderer(const wxString& varianttype,
2791 wxDataViewCellMode mode,
2792 int align)
2793 : wxDataViewRenderer(varianttype,mode)
e86edab0 2794{
8e59cbe4 2795 NSButtonCell* cell;
608129e5
VZ
2796
2797
8e59cbe4
VZ
2798 cell = [[NSButtonCell alloc] init];
2799 [cell setAlignment:ConvertToNativeHorizontalTextAlignment(align)];
2800 [cell setButtonType:NSSwitchButton];
2801 [cell setImagePosition:NSImageOnly];
2802 SetNativeData(new wxDataViewRendererNativeData(cell));
2803 [cell release];
e86edab0
RR
2804}
2805
8f2a8de6 2806bool wxDataViewToggleRenderer::MacRender()
e86edab0 2807{
8e59cbe4
VZ
2808 if (GetValue().GetType() == GetVariantType())
2809 {
2810 [GetNativeData()->GetItemCell() setIntValue:GetValue().GetLong()];
2811 return true;
2812 }
2813 else
2814 {
2815 wxFAIL_MSG(wxString("Toggle renderer cannot render value because of wrong value type; value type: ") << GetValue().GetType());
2816 return false;
2817 }
e86edab0
RR
2818}
2819
9461dd8c
VZ
2820void
2821wxDataViewToggleRenderer::OSXOnCellChanged(NSObject *value,
2822 const wxDataViewItem& item,
2823 unsigned col)
2824{
2825 wxDataViewModel *model = GetOwner()->GetOwner()->GetModel();
2826 model->ChangeValue(ObjectToBool(value), item, col);
2827}
2828
e86edab0
RR
2829IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer,wxDataViewRenderer)
2830
2831// ---------------------------------------------------------
2832// wxDataViewProgressRenderer
2833// ---------------------------------------------------------
8e59cbe4
VZ
2834wxDataViewProgressRenderer::wxDataViewProgressRenderer(const wxString& label,
2835 const wxString& varianttype,
2836 wxDataViewCellMode mode,
2837 int align)
2838 : wxDataViewRenderer(varianttype,mode,align)
e86edab0 2839{
8e59cbe4 2840 NSLevelIndicatorCell* cell;
608129e5 2841
8e59cbe4
VZ
2842 cell = [[NSLevelIndicatorCell alloc] initWithLevelIndicatorStyle:NSContinuousCapacityLevelIndicatorStyle];
2843 [cell setMinValue:0];
2844 [cell setMaxValue:100];
2845 SetNativeData(new wxDataViewRendererNativeData(cell));
2846 [cell release];
e86edab0
RR
2847}
2848
8f2a8de6 2849bool wxDataViewProgressRenderer::MacRender()
e86edab0 2850{
8e59cbe4
VZ
2851 if (GetValue().GetType() == GetVariantType())
2852 {
2853 [GetNativeData()->GetItemCell() setIntValue:GetValue().GetLong()];
2854 return true;
2855 }
2856 else
2857 {
2858 wxFAIL_MSG(wxString("Progress renderer cannot render value because of wrong value type; value type: ") << GetValue().GetType());
2859 return false;
2860 }
e86edab0
RR
2861}
2862
9461dd8c
VZ
2863void
2864wxDataViewProgressRenderer::OSXOnCellChanged(NSObject *value,
2865 const wxDataViewItem& item,
2866 unsigned col)
2867{
2868 wxDataViewModel *model = GetOwner()->GetOwner()->GetModel();
2869 model->ChangeValue(ObjectToLong(value), item, col);
2870}
2871
e86edab0
RR
2872IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer,wxDataViewRenderer)
2873
2874// ---------------------------------------------------------
2875// wxDataViewColumn
2876// ---------------------------------------------------------
8e59cbe4
VZ
2877
2878wxDataViewColumn::wxDataViewColumn(const wxString& title,
2879 wxDataViewRenderer* renderer,
2880 unsigned int model_column,
2881 int width,
2882 wxAlignment align,
2883 int flags)
2884 : wxDataViewColumnBase(renderer, model_column),
2885 m_NativeDataPtr(new wxDataViewColumnNativeData()),
2886 m_title(title)
e86edab0 2887{
8e59cbe4
VZ
2888 InitCommon(width, align, flags);
2889 if (renderer && (renderer->GetAlignment() == wxDVR_DEFAULT_ALIGNMENT))
2890 renderer->SetAlignment(align);
e86edab0
RR
2891}
2892
8e59cbe4
VZ
2893wxDataViewColumn::wxDataViewColumn(const wxBitmap& bitmap,
2894 wxDataViewRenderer* renderer,
2895 unsigned int model_column,
2896 int width,
2897 wxAlignment align,
2898 int flags)
2899 : wxDataViewColumnBase(bitmap, renderer, model_column),
2900 m_NativeDataPtr(new wxDataViewColumnNativeData())
e86edab0 2901{
8e59cbe4
VZ
2902 InitCommon(width, align, flags);
2903 if (renderer && (renderer->GetAlignment() == wxDVR_DEFAULT_ALIGNMENT))
2904 renderer->SetAlignment(align);
e86edab0
RR
2905}
2906
de40d736 2907wxDataViewColumn::~wxDataViewColumn()
e86edab0 2908{
8e59cbe4 2909 delete m_NativeDataPtr;
e86edab0
RR
2910}
2911
d831e2db
VZ
2912int wxDataViewColumn::GetWidth() const
2913{
2914 return [m_NativeDataPtr->GetNativeColumnPtr() width];
2915}
2916
e86edab0
RR
2917bool wxDataViewColumn::IsSortKey() const
2918{
8e59cbe4
VZ
2919 NSTableColumn *nsCol = GetNativeData()->GetNativeColumnPtr();
2920 return nsCol && ([nsCol sortDescriptorPrototype] != nil);
e86edab0
RR
2921}
2922
2923void wxDataViewColumn::SetAlignment(wxAlignment align)
2924{
8e59cbe4
VZ
2925 m_alignment = align;
2926 [[m_NativeDataPtr->GetNativeColumnPtr() headerCell] setAlignment:ConvertToNativeHorizontalTextAlignment(align)];
2927 if (m_renderer && (m_renderer->GetAlignment() == wxDVR_DEFAULT_ALIGNMENT))
2928 m_renderer->SetAlignment(align);
e86edab0
RR
2929}
2930
8e59cbe4 2931void wxDataViewColumn::SetBitmap(const wxBitmap& bitmap)
e86edab0 2932{
17f5d137
VZ
2933 // bitmaps and titles cannot exist at the same time - if the bitmap is set
2934 // the title is removed:
8e59cbe4
VZ
2935 m_title = wxEmptyString;
2936 wxDataViewColumnBase::SetBitmap(bitmap);
2937 [[m_NativeDataPtr->GetNativeColumnPtr() headerCell] setImage:[[bitmap.GetNSImage() retain] autorelease]];
e86edab0
RR
2938}
2939
2940void wxDataViewColumn::SetMaxWidth(int maxWidth)
2941{
8e59cbe4
VZ
2942 m_maxWidth = maxWidth;
2943 [m_NativeDataPtr->GetNativeColumnPtr() setMaxWidth:maxWidth];
e86edab0
RR
2944}
2945
2946void wxDataViewColumn::SetMinWidth(int minWidth)
2947{
8e59cbe4
VZ
2948 m_minWidth = minWidth;
2949 [m_NativeDataPtr->GetNativeColumnPtr() setMinWidth:minWidth];
e86edab0
RR
2950}
2951
2952void wxDataViewColumn::SetReorderable(bool reorderable)
2953{
2954}
2955
f6cb92b8
RR
2956void wxDataViewColumn::SetHidden(bool hidden)
2957{
2958 // How to set flag here?
2959
2960 [m_NativeDataPtr->GetNativeColumnPtr() setHidden:hidden];
2961}
2962
2963bool wxDataViewColumn::IsHidden() const
2964{
2965 return [m_NativeDataPtr->GetNativeColumnPtr() isHidden];
2966}
2967
e86edab0
RR
2968void wxDataViewColumn::SetResizeable(bool resizeable)
2969{
8e59cbe4
VZ
2970 wxDataViewColumnBase::SetResizeable(resizeable);
2971 if (resizeable)
2972 [m_NativeDataPtr->GetNativeColumnPtr() setResizingMask:NSTableColumnUserResizingMask];
2973 else
2974 [m_NativeDataPtr->GetNativeColumnPtr() setResizingMask:NSTableColumnNoResizing];
e86edab0
RR
2975}
2976
2977void wxDataViewColumn::SetSortable(bool sortable)
2978{
a2d0722f
RD
2979 // wxDataViewColumnBase::SetSortable(sortable);
2980 // Avoid endless recursion and just set the flag here
2981 if (sortable)
2982 m_flags |= wxDATAVIEW_COL_SORTABLE;
2983 else
2984 m_flags &= ~wxDATAVIEW_COL_SORTABLE;
e86edab0
RR
2985}
2986
2987void wxDataViewColumn::SetSortOrder(bool ascending)
2988{
8e59cbe4 2989 if (m_ascending != ascending)
e86edab0 2990 {
8e59cbe4
VZ
2991 m_ascending = ascending;
2992 if (IsSortKey())
2993 {
2994 // change sorting order:
2995 NSArray* sortDescriptors;
2996 NSSortDescriptor* sortDescriptor;
2997 NSTableColumn* tableColumn;
2998
2999 tableColumn = m_NativeDataPtr->GetNativeColumnPtr();
3000 sortDescriptor = [[NSSortDescriptor alloc] initWithKey:[[tableColumn sortDescriptorPrototype] key] ascending:m_ascending];
3001 sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
3002 [tableColumn setSortDescriptorPrototype:sortDescriptor];
3003 [[tableColumn tableView] setSortDescriptors:sortDescriptors];
3004 [sortDescriptor release];
3005 }
e86edab0 3006 }
e86edab0
RR
3007}
3008
8e59cbe4 3009void wxDataViewColumn::SetTitle(const wxString& title)
e86edab0 3010{
17f5d137
VZ
3011 // bitmaps and titles cannot exist at the same time - if the title is set
3012 // the bitmap is removed:
8e59cbe4
VZ
3013 wxDataViewColumnBase::SetBitmap(wxBitmap());
3014 m_title = title;
3015 [[m_NativeDataPtr->GetNativeColumnPtr() headerCell] setStringValue:[[wxCFStringRef(title).AsNSString() retain] autorelease]];
e86edab0
RR
3016}
3017
3018void wxDataViewColumn::SetWidth(int width)
3019{
8e59cbe4
VZ
3020 [m_NativeDataPtr->GetNativeColumnPtr() setWidth:width];
3021 m_width = width;
e86edab0
RR
3022}
3023
3024void wxDataViewColumn::SetAsSortKey(bool WXUNUSED(sort))
3025{
8e59cbe4
VZ
3026 // see wxGTK native wxDataViewColumn implementation
3027 wxFAIL_MSG("not implemented");
e86edab0
RR
3028}
3029
3030void wxDataViewColumn::SetNativeData(wxDataViewColumnNativeData* newNativeDataPtr)
3031{
8e59cbe4
VZ
3032 delete m_NativeDataPtr;
3033 m_NativeDataPtr = newNativeDataPtr;
e86edab0 3034}
8e59cbe4 3035
e86edab0 3036#endif // (wxUSE_DATAVIEWCTRL == 1) && !defined(wxUSE_GENERICDATAVIEWCTRL)