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