Re: help with copy: method
Re: help with copy: method
- Subject: Re: help with copy: method
- From: OS X AIBO <email@hidden>
- Date: Wed, 4 Dec 2002 19:05:57 -0800 (PST)
The application's menu normally calls copy: via the Copy command or its
key equivalent, cmd-c, at the behest of the user, so you probably want
to add a menu. I tried adding an Edit menu and a Copy menu item
programmatically a while ago, with some success.
Perhaps someone else knows better but here's the code I used as a proof
of concept of NIB-less menu creation. I think this may need to be
executed after the call to [NSApp run], so set the app delegate which
implements
- (void)applicationDidFinishLaunching:(NSNotification *)notification;
to do this:
NSMenu *mainMenu = [[NSMenu allocWithZone:[NSMenu
menuZone]]initWithTitle:@"Ignored"];
// not sure if we need both these calls.
[NSApp setAppleMenu:mainMenu]; // Sets the receiver's application
menu (the menu immediately to the right of the system Apple menu) to
menu. This method should be called at startup, otherwise the current
menu may not change.
[NSApp setMainMenu:mainMenu]; //Makes aMenu the receiver's main
menu.
// add Edit menu
NSMenu *menu = [[NSMenu allocWithZone:[NSMenu
menuZone]]initWithTitle:@"Edit"];
NSMenuItem *menuItem = [[NSMenuItem allocWithZone:[NSMenu
menuZone]]initWithTitle:@"Copy" action:@selector(copy:)
keyEquivalent:@"c"];
[mainMenu addItem:menuItem];
[mainMenu setSubmenu:menu forItem:menuItem];
One other thing: you should not have to implement your own version of
-(void)copy:sender;. Since the superclass of ZView is NSTextView, you
get this for free. But like you, I do appreciate the printf feedback,
so leave this in until you get it working:
-(IBAction)copy:(id)sender
{
printf("HERE\n");
[super copy:sender];
}
Let me know if that works for you,
--Dan
--- Sly Upah <email@hidden> wrote:
>
I'm wondering how one would get the copy: method to invoke in the
>
following program (I'm not using PowerBuilder/InterfaceBuilder so
>
there is no Edit menu/MainMenu.nib).
>
Thanks for any assistance!
>
>
#import <Cocoa/Cocoa.h>
>
>
@interface ZView : NSTextView
>
{
>
}
>
-(void)windowWillClose:(NSNotification *)notification;
>
-(IBAction)copy:(id)sender;
>
@end
>
>
@implementation ZView
>
-(void)windowWillClose:(NSNotification *)notification
>
{
>
[NSApp terminate:self];
>
}
>
-(IBAction)copy:(id)sender
>
{
>
NSPasteboard *pboard = [NSPasteboard generalPasteboard];
>
NSArray *types = [NSArray arrayWithObject:NSStringPboardType];
>
>
printf("HERE\n");
>
[pboard declareTypes:types owner:self];
>
[pboard addTypes:types owner:self];
>
// need some help with setString...
>
[pboard setString:??? forType:NSStringPboardType];
>
}
>
@end
>
>
setup()
>
{
>
NSWindow *window;
>
NSTextView *textView;
>
NSScrollView *scrollView;
>
NSTextView *zView;
>
NSRect scrollViewRect = {{0, 0}, {width, height}};
>
NSRect winRect = {{0, 0}, {width, height}};
>
NSRect textRect;
>
unsigned int style = NSTitledWindowMask | NSClosableWindowMask |
>
NSMiniaturizableWindowMask | NSResizableWindowMask;
>
>
window = [[NSWindow alloc] initWithContentRect: winRect
>
styleMask: style
>
backing: NSBackingStoreBuffered
>
defer: NO];
>
[window setMinSize:NSMakeSize(300, 300)];
>
>
scrollView = [[[NSScrollView alloc] initWithFrame:
>
scrollViewRect] autorelease];
>
[scrollView setHasHorizontalScroller: YES];
>
[scrollView setHasVerticalScroller: YES];
>
[scrollView setAutoresizingMask: NSViewHeightSizable |
>
NSViewWidthSizable];
>
[[scrollView contentView] setAutoresizingMask:
>
NSViewHeightSizable
>
| NSViewWidthSizable];
>
[[scrollView contentView] setAutoresizesSubviews:YES];
>
>
textRect = [[scrollView contentView] frame];
>
textView = [[[NSTextView alloc] initWithFrame: textRect]
>
autorelease];
>
[textView setBackgroundColor: [NSColor whiteColor]];
>
[textView setString:outbuf];
>
[textView setEditable: YES];
>
[textView setFont:[NSFont userFixedPitchFontOfSize:12]];
>
[textView setHorizontallyResizable: YES];
>
[textView setVerticallyResizable: YES];
>
[textView setMinSize: NSMakeSize(0, 0)];
>
[textView setMaxSize: NSMakeSize(1E7, 1E7)];
>
[textView setAutoresizingMask: NSViewHeightSizable |
>
NSViewWidthSizable];
>
[[textView textContainer] setContainerSize:NSMakeSize(1E7, 1E7)];
>
[[textView textContainer] setWidthTracksTextView: YES];
>
[scrollView setDocumentView: textView];
>
>
[window setContentView: scrollView];
>
[window setTitle: title];
>
zView = [[[ZView alloc] initWithFrame: scrollViewRect]
>
autorelease];
>
[textView setDelegate: zView];
>
[window setDelegate: zView];
>
[window makeKeyAndOrderFront: nil];
>
}
>
>
int main(int argc,char *argv[])
>
{
>
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
>
NSApp = [NSApplication sharedApplication];
>
setup();
>
[NSApp run];
>
// we get here when the window is closed
>
[NSApp release];
>
[pool release];
>
return(0);
>
}
>
_______________________________________________
>
cocoa-dev mailing list | email@hidden
>
Help/Unsubscribe/Archives:
>
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
>
Do not post admin requests to the list. They will be ignored.
Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
http://mailplus.yahoo.com
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.