X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/8a07956565ddccd1720abf4e510945593128d774..a51e79e87db3512823258d487849bb7e7947b700:/src/osx/cocoa/dataview.mm diff --git a/src/osx/cocoa/dataview.mm b/src/osx/cocoa/dataview.mm index e5314d3fd8..2e195d0317 100644 --- a/src/osx/cocoa/dataview.mm +++ b/src/osx/cocoa/dataview.mm @@ -156,11 +156,46 @@ inline wxDataViewItem wxDataViewItemFromMaybeNilItem(id item) { } + // Get the identifier we use for the specified column. This should be used + // for finding columns from identifier only, to initialize the identifier + // of a new column use initWithColumnPointer below instead. + +(NSString*) identifierForColumnPointer:(const wxDataViewColumn*)column; + + // Initialize the column with the given pointer to the associated + // wxDataViewColumn. This pointer can later be retrieved using + // getColumnPointer. + -(id) initWithColumnPointer:(const wxDataViewColumn*)column; + + // Retrieve the associated column. + -(wxDataViewColumn*) getColumnPointer; + -(id) dataCellForRow:(NSInteger)row; @end @implementation wxDVCNSTableColumn ++(NSString*) identifierForColumnPointer:(const wxDataViewColumn*)column +{ + // Starting from OS X 10.7 the column identifier must be an NSString and + // not just some arbitrary object, so we serialize the pointer into the + // string. Notice the use of NSInteger which is big enough to store a + // pointer in both 32 and 64 bit builds. + return [NSString stringWithFormat:@"%lu", reinterpret_cast(column)]; +} + +-(id) initWithColumnPointer:(const wxDataViewColumn*)column +{ + [self initWithIdentifier: [wxDVCNSTableColumn identifierForColumnPointer:column]]; + return self; +} + +-(wxDataViewColumn*) getColumnPointer +{ + // The case to NSString is needed for OS X < 10.7. + return reinterpret_cast( + [static_cast([self identifier]) integerValue]); +} + -(id) dataCellForRow:(NSInteger)row { // what we want to do here is to simply return nil for the cells which @@ -168,13 +203,7 @@ inline wxDataViewItem wxDataViewItemFromMaybeNilItem(id item) // or progress cells in the columns using the corresponding types even for // the container rows which is wrong - // half of the problem is just finding the objects we need from the column - // pointer which is itself stashed inside wxPointerObject which we use as - // our identifier - const wxDataViewColumn * const - dvCol = static_cast( - [(wxPointerObject *)[self identifier] pointer] - ); + const wxDataViewColumn * const dvCol = [self getColumnPointer]; const wxDataViewCtrl * const dvc = dvCol->GetOwner(); const wxCocoaDataViewControl * const @@ -316,10 +345,7 @@ NSTableColumn* CreateNativeColumn(const wxDataViewColumn *column) wxCHECK_MSG( renderer, NULL, "column should have a renderer" ); wxDVCNSTableColumn * const nativeColumn( - [[wxDVCNSTableColumn alloc] initWithIdentifier: - [[[wxPointerObject alloc] initWithPointer: - const_cast(column)] - autorelease]] + [[wxDVCNSTableColumn alloc] initWithColumnPointer: column] ); // setting the size related parameters: @@ -521,7 +547,7 @@ outlineView:(NSOutlineView*)outlineView event.SetItem(wxDataViewItemFromItem(item)); event.SetModel(dvc->GetModel()); - BOOL dragSuccessful; + BOOL dragSuccessful = false; if ( [bestType compare:DataViewPboardType] == NSOrderedSame ) { NSArray* dataArray((NSArray*) @@ -664,7 +690,8 @@ outlineView:(NSOutlineView*)outlineView wxCHECK_MSG( model, nil, "Valid model in data source does not exist." ); - wxDataViewColumn* col(static_cast([[tableColumn identifier] pointer])); + wxDataViewColumn* const + col([static_cast(tableColumn) getColumnPointer]); const unsigned colIdx = col->GetModelColumn(); wxDataViewItem dataViewItem(wxDataViewItemFromItem(item)); @@ -687,7 +714,8 @@ outlineView:(NSOutlineView*)outlineView { wxUnusedVar(outlineView); - wxDataViewColumn* col(static_cast([[tableColumn identifier] pointer])); + wxDataViewColumn* const + col([static_cast(tableColumn) getColumnPointer]); col->GetRenderer()-> OSXOnCellChanged(object, wxDataViewItemFromItem(item), col->GetModelColumn()); @@ -725,7 +753,7 @@ outlineView:(NSOutlineView*)outlineView // send first the event to wxWidgets that the sorting has changed so that // the program can do special actions before the sorting actually starts: - wxDataViewEvent event(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED,dvc->GetId()); // variable defintion + wxDataViewEvent event(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED,dvc->GetId()); // variable definition event.SetEventObject(dvc); if (noOfDescriptors > 0) @@ -757,7 +785,7 @@ outlineView:(NSOutlineView*)outlineView if (bestType == nil) return NSDragOperationNone; - NSDragOperation dragOperation; + NSDragOperation dragOperation = NSDragOperationNone; wxDataViewCtrl* const dvc(implementation->GetDataViewCtrl()); wxCHECK_MSG(dvc, false, "Pointer to data view control not set correctly."); @@ -863,7 +891,7 @@ outlineView:(NSOutlineView*)outlineView if ([writeItems count] > 0) { bool dataStringAvailable(true); // a flag indicating if for all items a data string is available - NSMutableArray* dataArray = [[NSMutableArray arrayWithCapacity:[writeItems count]] retain]; // data of all items + NSMutableArray* dataArray = [NSMutableArray arrayWithCapacity:[writeItems count]]; // data of all items wxString dataString; // contains the string data of all items // send a begin drag event for all selected items and proceed with @@ -1139,6 +1167,23 @@ outlineView:(NSOutlineView*)outlineView @implementation wxCustomCell +#if 0 // starting implementation for custom cell clicks + +- (id)init +{ + self = [super init]; + [self setAction:@selector(clickedAction)]; + [self setTarget:self]; + return self; +} + +- (void) clickedAction: (id) sender +{ + wxUnusedVar(sender); +} + +#endif + -(NSSize) cellSize { wxCustomRendererObject * const @@ -1179,7 +1224,11 @@ outlineView:(NSOutlineView*)outlineView wxGraphicsContext* gc = wxGraphicsContext::CreateFromNative(context); dc.SetGraphicsContext(gc); - renderer->WXCallRender(wxFromNSRect(controlView, cellFrame), &dc, 0); + int state = 0; + if ( [self isHighlighted] ) + state |= wxDATAVIEW_CELL_SELECTED; + + renderer->WXCallRender(wxFromNSRect(controlView, cellFrame), &dc, state); CGContextRestoreGState( context ); } @@ -1620,7 +1669,8 @@ outlineView:(NSOutlineView*)outlineView // -(void) outlineView:(NSOutlineView*)outlineView mouseDownInHeaderOfTableColumn:(NSTableColumn*)tableColumn { - wxDataViewColumn* const col(static_cast([[tableColumn identifier] pointer])); + wxDataViewColumn* const + col([static_cast(tableColumn) getColumnPointer]); wxDataViewCtrl* const dvc = implementation->GetDataViewCtrl(); @@ -1715,11 +1765,8 @@ outlineView:(NSOutlineView*)outlineView wxDataViewCtrl * const dvc = implementation->GetDataViewCtrl(); wxDataViewModel * const model = dvc->GetModel(); - wxDataViewColumn * const - dvCol(static_cast( - [[tableColumn identifier] pointer] - ) - ); + wxDataViewColumn* const + dvCol([static_cast(tableColumn) getColumnPointer]); const unsigned colIdx = dvCol->GetModelColumn(); wxDataViewItem dvItem(wxDataViewItemFromItem(item)); @@ -1756,7 +1803,10 @@ outlineView:(NSOutlineView*)outlineView { int const newColumnPosition = [[[notification userInfo] objectForKey:@"NSNewColumn"] intValue]; - wxDataViewColumn* const col(static_cast([[[[self tableColumns] objectAtIndex:newColumnPosition] identifier] pointer])); + NSTableColumn* + tableColumn = [[self tableColumns] objectAtIndex:newColumnPosition]; + wxDataViewColumn* const + col([static_cast(tableColumn) getColumnPointer]); wxDataViewCtrl* const dvc = implementation->GetDataViewCtrl(); @@ -1823,9 +1873,10 @@ outlineView:(NSOutlineView*)outlineView currentlyEditedColumn = [self editedColumn]; currentlyEditedRow = [self editedRow]; - wxDataViewColumn* const col = - static_cast( - [[[[self tableColumns] objectAtIndex:currentlyEditedColumn] identifier] pointer]); + NSTableColumn* + tableColumn = [[self tableColumns] objectAtIndex:currentlyEditedColumn]; + wxDataViewColumn* const + col([static_cast(tableColumn) getColumnPointer]); wxDataViewCtrl* const dvc = implementation->GetDataViewCtrl(); @@ -1861,9 +1912,10 @@ outlineView:(NSOutlineView*)outlineView // been sent the last edited column/row are valid: if ( currentlyEditedColumn != -1 && currentlyEditedRow != -1 ) { - wxDataViewColumn* const col = - static_cast( - [[[[self tableColumns] objectAtIndex:currentlyEditedColumn] identifier] pointer]); + NSTableColumn* + tableColumn = [[self tableColumns] objectAtIndex:currentlyEditedColumn]; + wxDataViewColumn* const + col([static_cast(tableColumn) getColumnPointer]); wxDataViewCtrl* const dvc = implementation->GetDataViewCtrl(); @@ -1965,7 +2017,7 @@ bool wxCocoaDataViewControl::DeleteColumn(wxDataViewColumn* columnPtr) [m_OutlineView setOutlineTableColumn:nil]; // due to a bug this does not work [m_OutlineView removeTableColumn:columnPtr->GetNativeData()->GetNativeColumnPtr()]; // due to a confirmed bug #6555162 the deletion does not work for // outline table columns (... and there is no workaround) - return (([m_OutlineView columnWithIdentifier:[[[wxPointerObject alloc] initWithPointer:columnPtr] autorelease]]) == -1); + return (([m_OutlineView columnWithIdentifier:[wxDVCNSTableColumn identifierForColumnPointer:columnPtr]]) == -1); } void wxCocoaDataViewControl::DoSetExpanderColumn(const wxDataViewColumn *columnPtr) @@ -1975,12 +2027,13 @@ void wxCocoaDataViewControl::DoSetExpanderColumn(const wxDataViewColumn *columnP wxDataViewColumn* wxCocoaDataViewControl::GetColumn(unsigned int pos) const { - return static_cast([[[[m_OutlineView tableColumns] objectAtIndex:pos] identifier] pointer]); + NSTableColumn* tableColumn = [[m_OutlineView tableColumns] objectAtIndex:pos]; + return [static_cast(tableColumn) getColumnPointer]; } int wxCocoaDataViewControl::GetColumnPosition(const wxDataViewColumn *columnPtr) const { - return [m_OutlineView columnWithIdentifier:[[[wxPointerObject alloc] initWithPointer:const_cast(columnPtr)] autorelease]]; + return [m_OutlineView columnWithIdentifier:[wxDVCNSTableColumn identifierForColumnPointer:columnPtr]]; } bool wxCocoaDataViewControl::InsertColumn(unsigned int pos, wxDataViewColumn* columnPtr) @@ -2180,9 +2233,9 @@ bool wxCocoaDataViewControl::IsExpanded(const wxDataViewItem& item) const bool wxCocoaDataViewControl::Reload() { [m_DataSource clearBuffers]; + [m_OutlineView reloadData]; [m_OutlineView scrollColumnToVisible:0]; [m_OutlineView scrollRowToVisible:0]; - [m_OutlineView reloadData]; return true; } @@ -2251,6 +2304,14 @@ wxDataViewItem wxCocoaDataViewControl::GetCurrentItem() const return wxDataViewItem([[m_OutlineView itemAtRow:[m_OutlineView selectedRow]] pointer]); } +wxDataViewColumn *wxCocoaDataViewControl::GetCurrentColumn() const +{ + int col = [m_OutlineView selectedColumn]; + if ( col == -1 ) + return NULL; + return GetColumn(col); +} + void wxCocoaDataViewControl::SetCurrentItem(const wxDataViewItem& item) { // We can't have unselected current item in a NSTableView, as the @@ -2261,6 +2322,11 @@ void wxCocoaDataViewControl::SetCurrentItem(const wxDataViewItem& item) Select(item); } +int wxCocoaDataViewControl::GetSelectedItemsCount() const +{ + return [m_OutlineView numberOfSelectedRows]; +} + int wxCocoaDataViewControl::GetSelections(wxDataViewItemArray& sel) const { NSIndexSet* selectedRowIndexes([m_OutlineView selectedRowIndexes]); @@ -2319,7 +2385,7 @@ wxDataViewColumn* wxCocoaDataViewControl::GetSortingColumn() const for (UInt32 i=0; i([[[columns objectAtIndex:i] identifier] pointer]); + return GetColumn(i); return NULL; } @@ -2354,7 +2420,7 @@ void wxCocoaDataViewControl::HitTest(const wxPoint& point, wxDataViewItem& item, indexRow = [m_OutlineView rowAtPoint: nativePoint]; if ((indexColumn >= 0) && (indexRow >= 0)) { - columnPtr = static_cast([[[[m_OutlineView tableColumns] objectAtIndex:indexColumn] identifier] pointer]); + columnPtr = GetColumn(indexColumn); item = wxDataViewItem([[m_OutlineView itemAtRow:indexRow] pointer]); } else @@ -2571,12 +2637,11 @@ wxDataViewRenderer::OSXOnCellChanged(NSObject *object, const wxDataViewItem& item, unsigned col) { - // TODO: we probably should get rid of this code entirely and make this - // function pure virtual, but currently we still have some native - // renderers (wxDataViewChoiceRenderer) which don't override it and - // there is also wxDataViewCustomRenderer for which it's not obvious - // how it should be implemented so keep this "auto-deduction" of - // variant type from NSObject for now + // TODO: This code should really be removed and this function be made pure + // virtual. We just need to decide what to do with custom renderers + // (i.e. wxDataViewCustomRenderer), currently OS X "in place" editing + // which doesn't really create an editor control is not compatible + // with the in place editing under other platforms. wxVariant value; if ( [object isKindOfClass:[NSString class]] ) @@ -2649,7 +2714,7 @@ void wxDataViewRenderer::OSXApplyAttr(const wxDataViewItemAttr& attr) } const wxColour& c = attr.GetColour(); - colText = [NSColor colorWithDeviceRed:c.Red() / 255. + colText = [NSColor colorWithCalibratedRed:c.Red() / 255. green:c.Green() / 255. blue:c.Blue() / 255. alpha:c.Alpha() / 255.]; @@ -2800,13 +2865,24 @@ wxDataViewChoiceRenderer::wxDataViewChoiceRenderer(const wxArrayString& choices, cell = [[NSPopUpButtonCell alloc] init]; [cell setControlSize:NSMiniControlSize]; - [cell setFont:[[NSFont fontWithName:[[cell font] fontName] size:[NSFont systemFontSizeForControlSize:NSMiniControlSize]] autorelease]]; + [cell setFont:[NSFont fontWithName:[[cell font] fontName] size:[NSFont systemFontSizeForControlSize:NSMiniControlSize]]]; for (size_t i=0; iGetOwner()->GetModel(); + model->ChangeValue(GetChoice(ObjectToLong(value)), item, col); +} + bool wxDataViewChoiceRenderer::MacRender() { if (GetValue().GetType() == GetVariantType()) @@ -2946,6 +3022,8 @@ bool wxDataViewIconTextRenderer::MacRender() iconText << GetValue(); if (iconText.GetIcon().IsOk()) [cell setImage:[[wxBitmap(iconText.GetIcon()).GetNSImage() retain] autorelease]]; + else + [cell setImage:nil]; [cell setStringValue:[[wxCFStringRef(iconText.GetText()).AsNSString() retain] autorelease]]; return true; } @@ -3234,12 +3312,6 @@ void wxDataViewColumn::SetWidth(int width) } } -void wxDataViewColumn::SetAsSortKey(bool WXUNUSED(sort)) -{ - // see wxGTK native wxDataViewColumn implementation - wxFAIL_MSG("not implemented"); -} - void wxDataViewColumn::SetNativeData(wxDataViewColumnNativeData* newNativeDataPtr) { delete m_NativeDataPtr;