NSPasteboardItem: create base implementation

Implement the NSPasteboardItem class. For now, it remains relatively
useless since NSPasteboard does not yet know how to use it. However, it
should be pluggable into NSPasteboard once NSPasteboard learns about
NSPasteboardReading and NSPasteboardWriting.
This commit is contained in:
Daniel Ferreira 2017-07-18 03:58:27 +10:00 committed by Ivan Vučica
parent 34ecd57e4e
commit 5403e47b60
4 changed files with 263 additions and 0 deletions

View file

@ -96,6 +96,7 @@
#import <AppKit/NSPageLayout.h>
#import <AppKit/NSPanel.h>
#import <AppKit/NSPasteboard.h>
#import <AppKit/NSPasteboardItem.h>
#import <AppKit/NSPopover.h>
#import <AppKit/NSPopUpButton.h>
#import <AppKit/NSPopUpButtonCell.h>

View file

@ -0,0 +1,81 @@
/** <title>NSPasteboardItem</title>
<abstract>class for wrapping pasteboard content</abstract>
Copyright <copy>(C) 2017 Free Software Foundation, Inc.</copy>
Author: Daniel Ferreira <dtf@stanford.edu>
Date: July 2017
This file is part of the GNUstep GUI Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; see the file COPYING.LIB.
If not, write to the Free Software Foundation,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef _GNUstep_H_NSPasteboardItem
#define _GNUstep_H_NSPasteboardItem
#import <AppKit/AppKitDefines.h>
#import <AppKit/NSPasteboard.h>
#import <Foundation/NSArray.h>
#import <Foundation/NSDictionary.h>
#import <CoreFoundation/CFBase.h>
#if OS_API_VERSION(MAC_OS_X_VERSION_10_6, GS_API_LATEST)
@protocol NSPasteboardItemDataProvider;
@interface NSPasteboardItem : NSObject <NSPasteboardWriting, NSPasteboardReading> {
NSMutableDictionary *_providerMap;
NSMutableDictionary *_dataMap;
NSMutableArray *_types;
}
#if GS_HAS_DECLARED_PROPERTIES
@property (readonly, copy) NSArray *types;
#else
- (NSArray *)types;
#endif
- (NSString *)availableTypeFromArray:(NSArray *)types;
- (BOOL)setDataProvider:(id<NSPasteboardItemDataProvider>)dataProvider
forTypes:(NSArray *)types;
- (BOOL)setData:(NSData *)data forType:(NSString *)type;
- (BOOL)setString:(NSString *)string forType:(NSString *)type;
- (BOOL)setPropertyList:(id)propertyList forType:(NSString *)type;
- (NSData *)dataForType:(NSString *)type;
- (NSString *)stringForType:(NSString *)type;
- (id)propertyListForType:(NSString *)type;
@end
@protocol NSPasteboardItemDataProvider <NSObject>
- (void) pasteboard: (NSPasteboard *)pasteboard
item: (NSPasteboardItem *)item
provideDataForType: (NSString *)type;
#if GS_PROTOCOLS_HAVE_OPTIONAL
@optional
#else
@end
@interface NSObject (NSPasteboardItemDataProvider)
#endif
- (void)pasteboardFinishedWithDataProvider:(NSPasteboard *)pasteboard;
@end
#endif
#endif

View file

@ -136,6 +136,7 @@ NSOutlineView.m \
NSPageLayout.m \
NSPanel.m \
NSParagraphStyle.m \
NSPasteboardItem.m \
NSPopover.m \
NSPopUpButton.m \
NSPopUpButtonCell.m \
@ -365,6 +366,7 @@ NSPageLayout.h \
NSPanel.h \
NSParagraphStyle.h \
NSPasteboard.h \
NSPasteboardItem.h \
NSPopover.h \
NSPopUpButton.h \
NSPopUpButtonCell.h \

179
Source/NSPasteboardItem.m Normal file
View file

@ -0,0 +1,179 @@
/** <title>NSPasteboardItem</title>
<abstract>class for wrapping pasteboard content</abstract>
Copyright <copy>(C) 2017 Free Software Foundation, Inc.</copy>
Author: Daniel Ferreira <dtf@stanford.edu>
Date: July 2017
This file is part of the GNUstep GUI Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; see the file COPYING.LIB.
If not, write to the Free Software Foundation,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/*
* FIXME: this class is useless until NSPasteboard implements
* NSPasteboardReading + NSPasteboardWriting.
*/
#import <AppKit/NSPasteboardItem.h>
#import <Foundation/Foundation.h>
@implementation NSPasteboardItem
- (id)init
{
if ((self = [super init]))
{
_providerMap = [[NSMutableDictionary alloc] init];
_dataMap = [[NSMutableDictionary alloc] init];
_types = [[NSMutableArray alloc] init];
}
return self;
}
- (NSArray *)types
{
return RETAIN([NSArray arrayWithArray: _types]);
}
- (BOOL)setDataProvider:(id<NSPasteboardItemDataProvider>)dataProvider
forTypes:(NSArray *)types
{
if (![dataProvider conformsToProtocol: @protocol(NSPasteboardItemDataProvider)])
{
NSLog(@"Pasteboard item data provider %@ must conform to"
"NSPasteboardItemDataProviderProtocol", dataProvider);
return NO;
}
for (NSUInteger i = 0; i < [types count]; i++)
{
NSString *type = [types objectAtIndex: i];
[_providerMap setObject: dataProvider forKey: type];
}
[_types addObjectsFromArray: types];
return YES;
}
- (NSString *)availableTypeFromArray:(NSArray *)types
{
// FIXME: this would require a dependency on UTIs
return nil;
}
- (BOOL)setData:(NSData *)data forType:(NSString *)type
{
if (![data isKindOfClass: [NSData class]])
{
return NO;
}
[_dataMap setObject: data forKey: type];
return YES;
}
- (BOOL)setString:(NSString *)string forType:(NSString *)type
{
if (![string isKindOfClass: [NSString class]])
return NO;
[_dataMap setObject: string forKey: type];
return YES;
}
- (BOOL)setPropertyList:(id)propertyList forType:(NSString *)type
{
if (![NSPropertyListSerialization propertyList: propertyList
isValidForFormat: NSPropertyListXMLFormat_v1_0])
return NO;
[_dataMap setObject: propertyList forKey: type];
return YES;
}
- (NSData *)dataForType:(NSString *)type
{
id object = [_dataMap objectForKey: type];
if ([object isKindOfClass: [NSData class]])
return (NSData *)object;
return [NSPropertyListSerialization dataWithPropertyList: object
format: NSPropertyListXMLFormat_v1_0
options: 0
error: NULL];
}
- (NSString *)stringForType:(NSString *)type
{
id object = [_dataMap objectForKey: type];
if ([object isKindOfClass: [NSString class]])
return (NSString *)object;
NSData *data = [self dataForType: type];
return [[[NSString alloc] initWithData: data
encoding: NSUTF8StringEncoding] autorelease];
}
- (id)propertyListForType:(NSString *)type
{
id object = [_dataMap objectForKey: type];
if (![object isKindOfClass: [NSData class]])
return object;
return [NSPropertyListSerialization propertyListWithData: object
options: 0
format: NULL
error: NULL];
}
// Delegate Methods
- (id)initWithPasteboardPropertyList:(id)propertyList ofType:(NSString *)type
{
if ((self = [self init]))
{
[self setPropertyList: propertyList forType: type];
}
return self;
}
+ (NSArray *)readableTypesForPasteboard:(NSPasteboard *)pasteboard
{
return [NSArray arrayWithObject: @"public.data"];
}
- (NSArray *)writableTypesForPasteboard:(NSPasteboard *)pasteboard
{
return _types;
}
- (id)pasteboardPropertyListForType:(NSString *)type
{
return [self propertyListForType: type];
}
- (void)dealloc
{
RELEASE(_providerMap);
RELEASE(_dataMap);
RELEASE(_types);
[super dealloc];
}
@end