Clean up and addition of cut/copy/paste methods, based on code

by Nikolaus Schaller <hns@computer.org>.


git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@29178 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Fred Kiefer 2009-12-29 11:24:01 +00:00
parent c444ae92d2
commit a7a8c31c4c
3 changed files with 125 additions and 38 deletions

View file

@ -26,13 +26,14 @@
Boston, MA 02110-1301, USA.
*/
#include "AppKit/NSDragging.h"
#include "AppKit/NSEvent.h"
#include "AppKit/NSImage.h"
#include "AppKit/NSImageCell.h"
#include "AppKit/NSImageView.h"
#include "AppKit/NSPasteboard.h"
#include "AppKit/NSWindow.h"
#import "AppKit/NSDragging.h"
#import "AppKit/NSEvent.h"
#import "AppKit/NSImage.h"
#import "AppKit/NSImageCell.h"
#import "AppKit/NSImageView.h"
#import "AppKit/NSMenuItem.h"
#import "AppKit/NSPasteboard.h"
#import "AppKit/NSWindow.h"
/*
* Class variables
@ -69,14 +70,11 @@ static Class imageCellClass;
}
- (id) init
{
return [self initWithFrame: NSZeroRect];
}
- (id) initWithFrame: (NSRect)aFrame
{
[super initWithFrame: aFrame];
self = [super initWithFrame: aFrame];
if (!self)
return self;
// set the default values
[self setImageAlignment: NSImageAlignCenter];
@ -170,9 +168,86 @@ static Class imageCellClass;
_allowsCutCopyPaste = flag;
}
@end
- (void) delete: (id)sender
{
if (_allowsCutCopyPaste)
[self setImage: nil];
}
@implementation NSImageView (NSDraggingDestination)
- (void) deleteBackward: (id)sender
{
if (_allowsCutCopyPaste)
[self setImage: nil];
}
- (void) cut: (id)sender
{
if (_allowsCutCopyPaste)
{
[self copy];
[self setImage: nil];
}
}
- (void) copy: (id)sender
{
if (_allowsCutCopyPaste)
{
NSImage *anImage = [self image];
if (anImage != nil)
{
// copy to pasteboard
NSPasteboard *pboard = [NSPasteboard generalPasteboard];
[pboard declareTypes: [NSArray arrayWithObject: NSTIFFPboardType]
owner: self];
[pboard setData: [anImage TIFFRepresentation]
forType: NSTIFFPboardType];
}
}
}
- (void) paste: (id)sender
{
if (_allowsCutCopyPaste)
{
// paste from pasteboard
NSPasteboard *pboard = [NSPasteboard generalPasteboard];
NSImage *image = [[NSImage alloc] initWithPasteboard: pboard];
if (image != nil)
{
[self setImage: image];
RELEASE(image);
}
}
}
- (BOOL) validateUserInterfaceItem: (id <NSValidatedUserInterfaceItem>)anItem
{
SEL action = [anItem action];
if (_allowsCutCopyPaste)
{
if (sel_eq(action, @selector(cut:)) ||
sel_eq(action, @selector(copy:)) ||
sel_eq(action, @selector(deleteBackward:)) ||
sel_eq(action, @selector(delete:)))
return [self image] != nil;
if (sel_eq(action, @selector(paste:)))
{
return [NSImage canInitWithPasteboard:
[NSPasteboard generalPasteboard]];
}
}
return NO;
}
- (BOOL) validateMenuItem: (NSMenuItem *)anItem
{
return [self validateUserInterfaceItem: anItem];
}
- (NSDragOperation) draggingEntered: (id <NSDraggingInfo>)sender
{
@ -188,6 +263,8 @@ static Class imageCellClass;
}
}
// NSDraggingDestination protocol
- (void) draggingExited: (id <NSDraggingInfo>)sender
{
[_cell setHighlighted: NO];
@ -303,6 +380,8 @@ static Class imageCellClass;
[super encodeWithCoder: aCoder];
if ([aCoder allowsKeyedCoding])
{
[aCoder encodeObject: [NSImage imagePasteboardTypes]
forKey: @"NSDragTypes"];
[aCoder encodeBool: [self isEditable] forKey: @"NSEditable"];
}
else
@ -315,6 +394,8 @@ static Class imageCellClass;
- (id) initWithCoder: (NSCoder *)aDecoder
{
self = [super initWithCoder: aDecoder];
if (!self)
return self;
if ([aDecoder allowsKeyedCoding])
{