More cleanup of OS X Cocoa wxDataViewCtrl code.

Add wxDataViewItemFromItem() and wxDataViewItemFromMaybeNilItem() functions
instead of repeating the same ugly casts several dozens of times.

Also wrap some more long lines.

No real changes.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@62600 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin 2009-11-10 17:42:38 +00:00
parent b0607dd221
commit 2406d5341f

View File

@ -35,6 +35,70 @@
// Classes used locally in dataview.mm // Classes used locally in dataview.mm
// ============================================================================ // ============================================================================
// ============================================================================
// wxPointerObject
// ============================================================================
@implementation wxPointerObject
-(id) init
{
self = [super init];
if (self != nil)
self->pointer = NULL;
return self;
}
-(id) initWithPointer:(void*) initPointer
{
self = [super init];
if (self != nil)
self->pointer = initPointer;
return self;
}
//
// inherited methods from NSObject
//
-(BOOL) isEqual:(id)object
{
return (object != nil) &&
([object isKindOfClass:[wxPointerObject class]]) &&
(pointer == [((wxPointerObject*) object) pointer]);
}
-(NSUInteger) hash
{
return (NSUInteger) pointer;
}
-(void*) pointer
{
return pointer;
}
-(void) setPointer:(void*) newPointer
{
pointer = newPointer;
}
@end
namespace
{
inline wxDataViewItem wxDataViewItemFromItem(id item)
{
return wxDataViewItem([static_cast<wxPointerObject *>(item) pointer]);
}
inline wxDataViewItem wxDataViewItemFromMaybeNilItem(id item)
{
return item == nil ? wxDataViewItem() : wxDataViewItemFromItem(item);
}
} // anonymous namespace
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// wxCustomRendererObject // wxCustomRendererObject
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -120,7 +184,7 @@
if ( item ) if ( item )
{ {
// ... and if it succeeded, ask the model whether it has any value // ... and if it succeeded, ask the model whether it has any value
wxDataViewItem dvItem([((wxPointerObject*) item) pointer]); wxDataViewItem dvItem(wxDataViewItemFromItem(item));
if ( !dvc->GetModel()->HasValue(dvItem, dvCol->GetModelColumn()) ) if ( !dvc->GetModel()->HasValue(dvItem, dvCol->GetModelColumn()) )
return nil; return nil;
@ -219,8 +283,8 @@ NSInteger CompareItems(id item1, id item2, void* context)
int rc = [sortDescriptor modelPtr]->Compare int rc = [sortDescriptor modelPtr]->Compare
( (
wxDataViewItem([((wxPointerObject*) item1) pointer]), wxDataViewItemFromItem(item1),
wxDataViewItem([((wxPointerObject*) item2) pointer]), wxDataViewItemFromItem(item2),
[sortDescriptor columnPtr]->GetModelColumn(), [sortDescriptor columnPtr]->GetModelColumn(),
[sortDescriptor ascending] == YES [sortDescriptor ascending] == YES
); );
@ -317,55 +381,6 @@ wxWidgetImplType* CreateDataView(wxWindowMac* wxpeer,
return new wxCocoaDataViewControl(wxpeer,pos,size,style); return new wxCocoaDataViewControl(wxpeer,pos,size,style);
} }
// ============================================================================
// wxPointerObject
// ============================================================================
@implementation wxPointerObject
-(id) init
{
self = [super init];
if (self != nil)
self->pointer = NULL;
return self;
}
-(id) initWithPointer:(void*) initPointer
{
self = [super init];
if (self != nil)
self->pointer = initPointer;
return self;
}
//
// inherited methods from NSObject
//
-(BOOL) isEqual:(id)object
{
return (object != nil) &&
([object isKindOfClass:[wxPointerObject class]]) &&
(pointer == [((wxPointerObject*) object) pointer]);
}
-(NSUInteger) hash
{
return (NSUInteger) pointer;
}
-(void*) pointer
{
return pointer;
}
-(void) setPointer:(void*) newPointer
{
pointer = newPointer;
}
@end
// ============================================================================ // ============================================================================
// wxSortDescriptorObject // wxSortDescriptorObject
// ============================================================================ // ============================================================================
@ -495,19 +510,22 @@ outlineView:(NSOutlineView*)outlineView
wxDataViewEvent event(wxEVT_COMMAND_DATAVIEW_ITEM_DROP, dvc->GetId()); wxDataViewEvent event(wxEVT_COMMAND_DATAVIEW_ITEM_DROP, dvc->GetId());
event.SetEventObject(dvc); event.SetEventObject(dvc);
event.SetItem(wxDataViewItem([((wxPointerObject*) item) pointer])); event.SetItem(wxDataViewItemFromItem(item));
event.SetModel(dvc->GetModel()); event.SetModel(dvc->GetModel());
BOOL dragSuccessful; BOOL dragSuccessful;
if ( [bestType compare:DataViewPboardType] == NSOrderedSame ) if ( [bestType compare:DataViewPboardType] == NSOrderedSame )
{ {
NSArray* dataArray((NSArray*)[pasteboard propertyListForType:DataViewPboardType]); NSArray* dataArray((NSArray*)
[pasteboard propertyListForType:DataViewPboardType]);
NSUInteger indexDraggedItem, noOfDraggedItems([dataArray count]); NSUInteger indexDraggedItem, noOfDraggedItems([dataArray count]);
indexDraggedItem = 0; indexDraggedItem = 0;
while (indexDraggedItem < noOfDraggedItems) while (indexDraggedItem < noOfDraggedItems)
{ {
wxDataObjectComposite* dataObjects(implementation->GetDnDDataObjects((NSData*)[dataArray objectAtIndex:indexDraggedItem])); wxDataObjectComposite* dataObjects(
implementation->GetDnDDataObjects((NSData*)
[dataArray objectAtIndex:indexDraggedItem]));
if (dataObjects && (dataObjects->GetFormatCount() > 0)) if (dataObjects && (dataObjects->GetFormatCount() > 0))
{ {
@ -515,9 +533,12 @@ outlineView:(NSOutlineView*)outlineView
// copy data into data object: // copy data into data object:
event.SetDataObject(dataObjects); event.SetDataObject(dataObjects);
event.SetDataFormat(implementation->GetDnDDataFormat(dataObjects)); event.SetDataFormat(
implementation->GetDnDDataFormat(dataObjects));
// copy data into buffer: // copy data into buffer:
dataObjects->GetDataHere(event.GetDataFormat().GetType(),buffer.GetWriteBuf(event.GetDataSize())); dataObjects->GetDataHere(
event.GetDataFormat().GetType(),
buffer.GetWriteBuf(event.GetDataSize()));
buffer.UngetWriteBuf(event.GetDataSize()); buffer.UngetWriteBuf(event.GetDataSize());
event.SetDataBuffer(buffer.GetData()); event.SetDataBuffer(buffer.GetData());
// finally, send event: // finally, send event:
@ -543,12 +564,21 @@ outlineView:(NSOutlineView*)outlineView
} }
else else
{ {
CFDataRef osxData; // needed to convert internally used UTF-16 representation to a UTF-8 representation // needed to convert internally used UTF-16 representation to a UTF-8
// representation
CFDataRef osxData;
wxDataObjectComposite* dataObjects (new wxDataObjectComposite()); wxDataObjectComposite* dataObjects (new wxDataObjectComposite());
wxTextDataObject* textDataObject(new wxTextDataObject()); wxTextDataObject* textDataObject(new wxTextDataObject());
osxData = ::CFStringCreateExternalRepresentation(kCFAllocatorDefault,(CFStringRef)[pasteboard stringForType:NSStringPboardType],kCFStringEncodingUTF8,32); osxData = ::CFStringCreateExternalRepresentation
if (textDataObject->SetData(::CFDataGetLength(osxData),::CFDataGetBytePtr(osxData))) (
kCFAllocatorDefault,
(CFStringRef)[pasteboard stringForType:NSStringPboardType],
kCFStringEncodingUTF8,
32
);
if (textDataObject->SetData(::CFDataGetLength(osxData),
::CFDataGetBytePtr(osxData)))
dataObjects->Add(textDataObject); dataObjects->Add(textDataObject);
else else
delete textDataObject; delete textDataObject;
@ -570,27 +600,28 @@ outlineView:(NSOutlineView*)outlineView
} }
} }
-(id) outlineView:(NSOutlineView*)outlineView child:(NSInteger)index ofItem:(id)item -(id) outlineView:(NSOutlineView*)outlineView
child:(NSInteger)index
ofItem:(id)item
{ {
if ((item == currentParentItem) && (index < ((NSInteger) [self getChildCount]))) if ((item == currentParentItem) &&
(index < ((NSInteger) [self getChildCount])))
return [self getChild:index]; return [self getChild:index];
else
{
wxDataViewItemArray dataViewChildren;
wxCHECK_MSG( model, 0, "Valid model in data source does not exist." ); wxDataViewItemArray dataViewChildren;
(void) model->GetChildren((item == nil) ? wxDataViewItem() : wxDataViewItem([((wxPointerObject*) item) pointer]),dataViewChildren);
[self bufferItem:item withChildren:&dataViewChildren]; wxCHECK_MSG( model, 0, "Valid model in data source does not exist." );
if ([sortDescriptors count] > 0) model->GetChildren(wxDataViewItemFromMaybeNilItem(item), dataViewChildren);
[children sortUsingFunction:CompareItems context:sortDescriptors]; [self bufferItem:item withChildren:&dataViewChildren];
return [self getChild:index]; if ([sortDescriptors count] > 0)
} [children sortUsingFunction:CompareItems context:sortDescriptors];
return [self getChild:index];
} }
-(BOOL) outlineView:(NSOutlineView*)outlineView isItemExpandable:(id)item -(BOOL) outlineView:(NSOutlineView*)outlineView isItemExpandable:(id)item
{ {
wxCHECK_MSG( model, 0, "Valid model in data source does not exist." ); wxCHECK_MSG( model, 0, "Valid model in data source does not exist." );
return model->IsContainer(wxDataViewItem([((wxPointerObject*) item) pointer])); return model->IsContainer(wxDataViewItemFromItem(item));
} }
-(NSInteger) outlineView:(NSOutlineView*)outlineView numberOfChildrenOfItem:(id)item -(NSInteger) outlineView:(NSOutlineView*)outlineView numberOfChildrenOfItem:(id)item
@ -601,7 +632,8 @@ outlineView:(NSOutlineView*)outlineView
wxCHECK_MSG( model, 0, "Valid model in data source does not exist." ); wxCHECK_MSG( model, 0, "Valid model in data source does not exist." );
noOfChildren = model->GetChildren((item == nil) ? wxDataViewItem() : wxDataViewItem([((wxPointerObject*) item) pointer]),dataViewChildren); noOfChildren = model->GetChildren(wxDataViewItemFromMaybeNilItem(item),
dataViewChildren);
[self bufferItem:item withChildren:&dataViewChildren]; [self bufferItem:item withChildren:&dataViewChildren];
if ([sortDescriptors count] > 0) if ([sortDescriptors count] > 0)
[children sortUsingFunction:CompareItems context:sortDescriptors]; [children sortUsingFunction:CompareItems context:sortDescriptors];
@ -618,7 +650,7 @@ outlineView:(NSOutlineView*)outlineView
wxDataViewColumn* col(static_cast<wxDataViewColumn*>([[tableColumn identifier] pointer])); wxDataViewColumn* col(static_cast<wxDataViewColumn*>([[tableColumn identifier] pointer]));
const unsigned colIdx = col->GetModelColumn(); const unsigned colIdx = col->GetModelColumn();
wxDataViewItem dataViewItem([((wxPointerObject*) item) pointer]); wxDataViewItem dataViewItem(wxDataViewItemFromItem(item));
if ( model->HasValue(dataViewItem, colIdx) ) if ( model->HasValue(dataViewItem, colIdx) )
{ {
@ -638,10 +670,8 @@ outlineView:(NSOutlineView*)outlineView
{ {
wxDataViewColumn* col(static_cast<wxDataViewColumn*>([[tableColumn identifier] pointer])); wxDataViewColumn* col(static_cast<wxDataViewColumn*>([[tableColumn identifier] pointer]));
wxDataViewItem dataViewItem([((wxPointerObject*) item) pointer]);
col->GetRenderer()-> col->GetRenderer()->
OSXOnCellChanged(object, dataViewItem, col->GetModelColumn()); OSXOnCellChanged(object, wxDataViewItemFromItem(item), col->GetModelColumn());
} }
-(void) outlineView:(NSOutlineView*)outlineView sortDescriptorsDidChange:(NSArray*)oldDescriptors -(void) outlineView:(NSOutlineView*)outlineView sortDescriptorsDidChange:(NSArray*)oldDescriptors
@ -714,7 +744,7 @@ outlineView:(NSOutlineView*)outlineView
event(wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE,dvc->GetId()); event(wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE,dvc->GetId());
event.SetEventObject(dvc); event.SetEventObject(dvc);
event.SetItem(wxDataViewItem([((wxPointerObject*) item) pointer])); event.SetItem(wxDataViewItemFromItem(item));
event.SetModel(dvc->GetModel()); event.SetModel(dvc->GetModel());
if ([bestType compare:DataViewPboardType] == NSOrderedSame) if ([bestType compare:DataViewPboardType] == NSOrderedSame)
{ {
@ -820,7 +850,8 @@ outlineView:(NSOutlineView*)outlineView
wxDataObjectComposite* itemObject(new wxDataObjectComposite()); // data object for current item wxDataObjectComposite* itemObject(new wxDataObjectComposite()); // data object for current item
wxString itemString; // contains the TAB concatenated data of an item wxString itemString; // contains the TAB concatenated data of an item
event.SetItem(wxDataViewItem([((wxPointerObject*) [writeItems objectAtIndex:itemCounter]) pointer])); event.SetItem(
wxDataViewItemFromItem([writeItems objectAtIndex:itemCounter]));
itemString = ::ConcatenateDataViewItemValues(dvc,event.GetItem()); itemString = ::ConcatenateDataViewItemValues(dvc,event.GetItem());
itemObject->Add(new wxTextDataObject(itemString)); itemObject->Add(new wxTextDataObject(itemString));
event.SetDataObject(itemObject); event.SetDataObject(itemObject);
@ -1479,7 +1510,7 @@ outlineView:(NSOutlineView*)outlineView
event.SetEventObject(dvc); event.SetEventObject(dvc);
event.SetItem(wxDataViewItem([((wxPointerObject*) [self itemAtRow:[self clickedRow]]) pointer])); event.SetItem(wxDataViewItemFromItem([self itemAtRow:[self clickedRow]]));
dvc->GetEventHandler()->ProcessEvent(event); dvc->GetEventHandler()->ProcessEvent(event);
} }
@ -1562,7 +1593,7 @@ outlineView:(NSOutlineView*)outlineView
event.SetEventObject(dvc); event.SetEventObject(dvc);
event.SetItem (wxDataViewItem([((wxPointerObject*) item) pointer])); event.SetItem (wxDataViewItemFromItem(item));
event.SetModel (dvc->GetModel()); event.SetModel (dvc->GetModel());
// finally send the equivalent wxWidget event: // finally send the equivalent wxWidget event:
dvc->GetEventHandler()->ProcessEvent(event); dvc->GetEventHandler()->ProcessEvent(event);
@ -1578,7 +1609,7 @@ outlineView:(NSOutlineView*)outlineView
event.SetEventObject(dvc); event.SetEventObject(dvc);
event.SetItem (wxDataViewItem([((wxPointerObject*) item) pointer])); event.SetItem (wxDataViewItemFromItem(item));
event.SetModel (dvc->GetModel()); event.SetModel (dvc->GetModel());
// finally send the equivalent wxWidget event: // finally send the equivalent wxWidget event:
dvc->GetEventHandler()->ProcessEvent(event); dvc->GetEventHandler()->ProcessEvent(event);
@ -1606,7 +1637,7 @@ outlineView:(NSOutlineView*)outlineView
); );
const unsigned colIdx = dvCol->GetModelColumn(); const unsigned colIdx = dvCol->GetModelColumn();
wxDataViewItem dvItem([static_cast<wxPointerObject *>(item) pointer]); wxDataViewItem dvItem(wxDataViewItemFromItem(item));
if ( !model->HasValue(dvItem, colIdx) ) if ( !model->HasValue(dvItem, colIdx) )
return; return;
@ -1658,7 +1689,8 @@ outlineView:(NSOutlineView*)outlineView
event.SetEventObject(dvc); event.SetEventObject(dvc);
event.SetItem(wxDataViewItem([((wxPointerObject*) [[notification userInfo] objectForKey:@"NSObject"]) pointer])); event.SetItem(wxDataViewItemFromItem(
[[notification userInfo] objectForKey:@"NSObject"]));
dvc->GetEventHandler()->ProcessEvent(event); dvc->GetEventHandler()->ProcessEvent(event);
} }
@ -1670,7 +1702,8 @@ outlineView:(NSOutlineView*)outlineView
event.SetEventObject(dvc); event.SetEventObject(dvc);
event.SetItem(wxDataViewItem([((wxPointerObject*) [[notification userInfo] objectForKey:@"NSObject"]) pointer])); event.SetItem(wxDataViewItemFromItem(
[[notification userInfo] objectForKey:@"NSObject"]));
dvc->GetEventHandler()->ProcessEvent(event); dvc->GetEventHandler()->ProcessEvent(event);
} }
@ -1714,7 +1747,7 @@ outlineView:(NSOutlineView*)outlineView
event.SetEventObject(dvc); event.SetEventObject(dvc);
event.SetItem( event.SetItem(
wxDataViewItem([((wxPointerObject*) [self itemAtRow:currentlyEditedRow]) pointer])); wxDataViewItemFromItem([self itemAtRow:currentlyEditedRow]));
event.SetColumn(dvc->GetColumnPosition(col)); event.SetColumn(dvc->GetColumnPosition(col));
event.SetDataViewColumn(col); event.SetDataViewColumn(col);
dvc->GetEventHandler()->ProcessEvent(event); dvc->GetEventHandler()->ProcessEvent(event);
@ -1745,7 +1778,7 @@ outlineView:(NSOutlineView*)outlineView
event.SetEventObject(dvc); event.SetEventObject(dvc);
event.SetItem( event.SetItem(
wxDataViewItem([((wxPointerObject*) [self itemAtRow:currentlyEditedRow]) pointer])); wxDataViewItemFromItem([self itemAtRow:currentlyEditedRow]));
event.SetColumn(dvc->GetColumnPosition(col)); event.SetColumn(dvc->GetColumnPosition(col));
event.SetDataViewColumn(col); event.SetDataViewColumn(col);
dvc->GetEventHandler()->ProcessEvent(event); dvc->GetEventHandler()->ProcessEvent(event);
@ -1758,19 +1791,25 @@ outlineView:(NSOutlineView*)outlineView
} }
@end @end
// ============================================================================ // ============================================================================
// wxCocoaDataViewControl // wxCocoaDataViewControl
// ============================================================================ // ============================================================================
//
// constructors / destructor wxCocoaDataViewControl::wxCocoaDataViewControl(wxWindow* peer,
// const wxPoint& pos,
wxCocoaDataViewControl::wxCocoaDataViewControl(wxWindow* peer, const wxPoint& pos, const wxSize& size, long style) const wxSize& size,
:wxWidgetCocoaImpl(peer,[[NSScrollView alloc] initWithFrame:wxOSXGetFrameForControl(peer,pos,size)]), long style)
m_DataSource(NULL), m_OutlineView([[wxCocoaOutlineView alloc] init]) : wxWidgetCocoaImpl
(
peer,
[[NSScrollView alloc] initWithFrame:wxOSXGetFrameForControl(peer,pos,size)]
),
m_DataSource(NULL),
m_OutlineView([[wxCocoaOutlineView alloc] init])
{ {
// initialize scrollview (the outline view is part of a scrollview): // initialize scrollview (the outline view is part of a scrollview):
NSScrollView* scrollview = (NSScrollView*) GetWXWidget(); // definition for abbreviational purposes NSScrollView* scrollview = (NSScrollView*) GetWXWidget();
[scrollview setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; [scrollview setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
[scrollview setBorderType:NSNoBorder]; [scrollview setBorderType:NSNoBorder];
@ -1808,7 +1847,8 @@ bool wxCocoaDataViewControl::ClearColumns()
bool const bufAllowsMultipleSelection = [m_OutlineView allowsMultipleSelection]; bool const bufAllowsMultipleSelection = [m_OutlineView allowsMultipleSelection];
// 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; // 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;
// therefore, the whole view is deleted and newly constructed: // therefore, the whole view is deleted and newly constructed:
[m_OutlineView release]; [m_OutlineView release];
m_OutlineView = [[wxCocoaOutlineView alloc] init]; m_OutlineView = [[wxCocoaOutlineView alloc] init];
@ -1854,8 +1894,9 @@ bool wxCocoaDataViewControl::InsertColumn(unsigned int pos, wxDataViewColumn* co
// create column and set the native data of the dataview column: // create column and set the native data of the dataview column:
NSTableColumn *nativeColumn = ::CreateNativeColumn(columnPtr); NSTableColumn *nativeColumn = ::CreateNativeColumn(columnPtr);
columnPtr->GetNativeData()->SetNativeColumnPtr(nativeColumn); columnPtr->GetNativeData()->SetNativeColumnPtr(nativeColumn);
// as the native control does not allow the insertion of a column at a specified position the column is first appended and // as the native control does not allow the insertion of a column at a
// - if necessary - moved to its final position: // specified position the column is first appended and - if necessary -
// moved to its final position:
[m_OutlineView addTableColumn:nativeColumn]; [m_OutlineView addTableColumn:nativeColumn];
if (pos != static_cast<unsigned int>([m_OutlineView numberOfColumns]-1)) if (pos != static_cast<unsigned int>([m_OutlineView numberOfColumns]-1))
[m_OutlineView moveColumn:[m_OutlineView numberOfColumns]-1 toColumn:pos]; [m_OutlineView moveColumn:[m_OutlineView numberOfColumns]-1 toColumn:pos];