From 028f8b26983da8a3b830087c59b57900a4a44e18 Mon Sep 17 00:00:00 2001 From: fredkiefer Date: Tue, 29 Dec 2009 11:24:01 +0000 Subject: [PATCH] Clean up and addition of cut/copy/paste methods, based on code by Nikolaus Schaller . git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@29178 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 46 ++++++++------- Headers/AppKit/NSImageView.h | 6 +- Source/NSImageView.m | 111 ++++++++++++++++++++++++++++++----- 3 files changed, 125 insertions(+), 38 deletions(-) diff --git a/ChangeLog b/ChangeLog index 38bc8f961..5711788e9 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2009-12-29 Fred Kiefer + + * Headers/AppKit/NSImageView.h: Clean up. + * Source/NSImageView.m: Clean up and addition of cut/copy/paste + methods, based on code by Nikolaus Schaller . + 2009-12-27 Quentin Mathe * Headers/NSTableView.h @@ -66,7 +72,7 @@ panel is requested. 2009-12-21 Fred Kiefer - + * Source/GSIconManager.h, * Source/GSIconManager.m: New files. Allows to get icon placement from separate process. @@ -102,7 +108,7 @@ occurred to an instance variable. 2009-12-21 Fred Kiefer - + * Source/NSTableColumn.m (-initWithCoder:, -encodeWithCoder:): Add the keyed encoding/decoding code for Quentin's extensions. @@ -444,7 +450,7 @@ * Source/NSSlider.m: Clean up. * Source/NSSliderCell.m: Remove isFlipped method, correct copyWithZone:, clean up. - + 2009-11-16 Fred Kiefer * Source/NSCell.m (-setObjectValue:, -setStringValue:): Handle a @@ -465,7 +471,7 @@ in NSTextView_actions.m. 2009-11-15 Fred Kiefer - + * Source/NSButtonCell.m: Change default value for keyEquivalentModifierMask to 0. Add keyed decoding and encoding of keyEquivalentModifierMask. @@ -549,7 +555,7 @@ * Source/NSTextFieldCell.m: White space changes. * Headers/AppKit/NSTableHeaderCell.h, * Source/NSTableHeaderCell.m: Add new 10.3 methods. - + 2009-11-09 Fred Kiefer * Source/NSControl.m, @@ -573,7 +579,7 @@ * Headers/AppKit/NSAnimation.h: Change to new MacOSX type definitions. * Source/NSAnimation.m: Remove externs and white space changes. * Source/externs.m: Move externs from NSAnimation to here. - + 2009-11-08 Fred Kiefer * Source/NSApplication.m (-finishLaunching): Only activate the @@ -616,7 +622,7 @@ * Source/NSColor.m (-colorUsingColorSpace): Support more color spaces. * Source/NSColorSpace.m (COLORSPACE): Initialize the static variable. - + 2009-11-03 Fred Kiefer * Source/GSThemeDrawing.m: Add more includes to apeace Riccardo's compiler. @@ -630,7 +636,7 @@ * Source/NSApplication.m (-nextEventMatchingMask:...): Only set the current event when dequeuing and never return the null_event. - + 2009-11-02 Fred Kiefer * Source/GSTheme.m: Remove some compiler warnings. @@ -639,7 +645,7 @@ * Source/GSThemeDrawing.m, * Headers/Additions/GNUstepGUI/GSTheme.h: New theme methods for toolbar and menu item drawing. - + 2009-11-01 Fred Kiefer * Source/NSImage.m: Switch to the new shared composite and @@ -663,7 +669,7 @@ * Images/common_ProgressIndeterminate_?.tiff New image files by Uli Kusterer . * Images/GNUmakefile: Add the new images. - + 2009-10-27 Thomas Gamper * Source/GSDisplayServer.m @@ -689,11 +695,11 @@ at geometry maths. 2009-10-23 Riccardo Mottola - + * Headers/AppKit/NSBezierPath.h: added some documentation 2009-10-22 Riccardo Mottola - + * Images/common_3DArrowRightH.tiff: Made background transparent, allows for better theming @@ -713,7 +719,7 @@ colour gradients. * Headers/AppKit/AppKit.h: Add include for NSGradient.h * Source/GNUmakefile: Add new files. - + 2009-10-20 Wolfgang Lux * Source/NSDocumentController.m(-openDocumentWithContentsOfURL:display:, @@ -886,13 +892,13 @@ into GSTheme. 2009-10-10 Fred Kiefer - + * Source/NSEvent.m, * Source/NSImage.m, * Source/GSTextStorage.m: Remove usage of NSDeallocateObject. 2009-10-10 Fred Kiefer - + * Source/NSApplication.m (-endModalSession:): Bring the window of the next modal session to the front. * Source/NSApplication.m (-runModalSession:): Better check whether @@ -901,25 +907,25 @@ 2009-09-27 Richard Frith-Macdonald - * Source/NSImage.m: ([-setName:]) fix bug which was removing the + * Source/NSImage.m: ([-setName:]) fix bug which was removing the wrong name when given an image which already had a name. Make named images thread-safe using a lock. Add private method to update names on theme change. * Source/GSTheme.m: Use new method to update theme images. - + 2009-09-26 Fred Kiefer - + * Source/NSApplication.m (-setApplicationIconImage:, -finishLaunching): Move the set name code to finishLaunching, as the image called NSApplicationIcon should stay the same all the time. 2009-09-26 Fred Kiefer - + * Source/NSTextContainer.m (init): Add this method as suggested by Derek Fawcus . 2009-09-21 Fred Kiefer - + * Source/NSWindow.m (+standardWindowButton:forStyleMask:): Add support for toolbar button. * Source/NSWindow.m (-toggleToolbarShown:): Reorder the code. diff --git a/Headers/AppKit/NSImageView.h b/Headers/AppKit/NSImageView.h index e277b7862..1d77e3a94 100644 --- a/Headers/AppKit/NSImageView.h +++ b/Headers/AppKit/NSImageView.h @@ -29,14 +29,14 @@ #define _GNUstep_H_NSImageView #import -#include -#include +#import +#import @interface NSImageView : NSControl { id _target; SEL _action; - BOOL _allowsCutCopyPaste; + BOOL _allowsCutCopyPaste; } - (NSImage *)image; diff --git a/Source/NSImageView.m b/Source/NSImageView.m index 408615e04..6a7dadc13 100644 --- a/Source/NSImageView.m +++ b/Source/NSImageView.m @@ -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 )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 )sender { @@ -188,6 +263,8 @@ static Class imageCellClass; } } +// NSDraggingDestination protocol + - (void) draggingExited: (id )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]) {