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