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