Re: cocoa app from scratch
Re: cocoa app from scratch
- Subject: Re: cocoa app from scratch
- From: Public Look <email@hidden>
- Date: Sun, 18 Jul 2004 21:59:09 -0400
I butchered my last reply. Here is a correction:
The example referenced in
http://examples.oreilly.com/buildcocoa/ is
Chapter 4.
Here is my slightly modified version of that example. I corrected an
assertion failure error and made the set of points that are drawn
survive window redraws. Any errors were probaby added by me, but it
works fine on my computer.
Compile with following command line:
cc -Wall -o Tiny2 Tiny2.m -framework Cocoa
Paste the following text into a file named Tiny2.m:
/* Tiny2.m
*
* A tiny Cocoa application which creates a
* a window and displays graphics in it.
*
* (C) 2002 by Simson L. Garfinkel and Michael K. Mahoney.
* all rights reserved.
*/
#import <Cocoa/Cocoa.h>
static NSMutableArray *pointArray = nil;
/****************************************************************
** The CView draws the image.
****************************************************************/
@interface CView : NSView
{
}
- (void)drawRect:(NSRect)rect;
- (void)mouseDown:(NSEvent *)theEvent;
- (void)windowWillClose:(NSNotification *)notification;
@end
@implementation CView
- (void)drawRect:(NSRect)rect
{
/* draw the background */
[[NSColor whiteColor] set];
NSRectFill([self bounds]);
[[NSColor greenColor] set];
NSRectFill(NSInsetRect([self bounds], 20.0f, 20.0f));
/* Draw a little box at each stored point */
NSEnumerator *objectEnumerator = [pointArray objectEnumerator];
NSValue *currentValue;
[[NSColor blackColor] set];
while(nil != (currentValue = [objectEnumerator nextObject]))
{
NSRect r;
r.origin = [currentValue pointValue];
r.origin.x -= 5;
r.origin.y -= 5;
r.size.width = 10;
r.size.height = 10;
[NSBezierPath fillRect:r];
}
}
-(void)windowWillClose:(NSNotification *)notification
{
[NSApp terminate:self];
}
- (void)mouseDown:(NSEvent *)theEvent
{
/* Create an array to store points if necessary */
if(nil == pointArray)
{
pointArray = [[NSMutableArray alloc] init];
}
/* Create a value object to contain the new point and store the
value
in the array */
[pointArray addObject:[NSValue valueWithPoint:
[self convertPoint:[theEvent locationInWindow] fromView:nil]]];
[self setNeedsDisplay:YES];
}
@end
void setup()
{
NSWindow *myWindow;
NSView *theView;
NSRect graphicsRect;
/* Now create the window */
graphicsRect = NSMakeRect(100.0, 350.0, 400.0, 400.0);
myWindow = [ [NSWindow alloc]
initWithContentRect: graphicsRect
styleMask: NSTitledWindowMask
|NSClosableWindowMask
|NSMiniaturizableWindowMask
|NSResizableWindowMask
backing: NSBackingStoreBuffered
defer: NO];
[myWindow setTitle:@"Tiny Application Window"];
/* Create the CView for the Window. */
theView = [ [CView alloc] init];
[myWindow setContentView:theView ];
[myWindow setContentMinSize:NSMakeSize(90.0f, 90.0f)];
[myWindow setDelegate:theView ];
[myWindow makeKeyAndOrderFront: nil];
}
int main()
{
/* Create the autorelease pool */
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
/* Create the application object. */
NSApp = [NSApplication sharedApplication];
setup();
/* run the main event loop until application terminates */
[NSApp run];
/* We get here when the user closes the application terminates. */
[pointArray release];
[NSApp release]; /* free the app */
[pool release]; /* release the autorelease pool */
return(EXIT_SUCCESS);
}
_______________________________________________
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.