advertisement

Article:
  Building a Scratch Pad with Cocoa
Subject:   structs for ColorPath in padview
Date:   2001-12-08 16:02:05
From:   psheldon
Response to: small steps on my own to three color padview

I worry that I am building a struct on a stack and trying to retain it in NSMutableArray.
With this half baked code I got an error message :
PadView.m:93: parse error before `.'
PadView.m:102: parse error before `.'
PadView.m:103: parse error before `ColorPath'
PadView.m:104: warning: passing arg 1 of `addObject:' from incompatible pointer type


Here's full code :
//
// PadView.h
// ScratchPad
//
// Created by psheldon on Fri Nov 30 2001.
// Copyright (c) 2001 __MyCompanyName__. All rights reserved.
//


#import <AppKit/AppKit.h>


typedef struct ColorPath {
NSColor *color;
NSBezierPath *path;
} ColorPath;



@interface PadView : NSView {


NSBezierPath *pathRed;
NSBezierPath *pathGreen;
NSBezierPath *pathBlue;
NSBezierPath *pathCurrent;


NSMutableArray *ColorPaths;


}
- (IBAction)SetColorPath:(NSString *)NSstr;
- (void)ChooseBlue:(id)sender;
- (void)ChooseGreen:(id)sender;
- (void)ChooseRed:(id)sender;
@end



//
// PadView.m
// ScratchPad
//
// Created by psheldon on Fri Nov 30 2001.
// Copyright (c) 2001 __MyCompanyName__. All rights reserved.
//


#import "PadView.h"



@implementation PadView


- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
//convenience constructor autoreleases so need retain for PadView

ColorPaths = [[NSMutableArray alloc] init];

pathRed = [[NSBezierPath bezierPath] retain];
pathGreen = [[NSBezierPath bezierPath] retain];
pathBlue = [[NSBezierPath bezierPath] retain];
pathCurrent = pathRed;
}
return self;
}


- (void)drawRect:(NSRect)rect {
// Drawing code here.
[[NSColor redColor] set];
[pathRed stroke];
[[NSColor greenColor] set];
[pathGreen stroke];
[[NSColor blueColor] set];
[pathBlue stroke];
}
- (void)mouseDown:(NSEvent *)theEvent
{
//mouse location with window to view coordinate change
NSPoint loc = [theEvent locationInWindow];
loc.x -= [self frame].origin.x;
loc.y -= [self frame].origin.y;

//convenience constructor autoreleases so need retain for PadView
//path = [[NSBezierPath bezierPath] retain];
//put pencil down at point
[pathCurrent moveToPoint:loc];
}
- (void)mouseDragged:(NSEvent *)theEvent
{
//mouse location with window to view coordinate change
NSPoint loc = [theEvent locationInWindow];
loc.x -= [self frame].origin.x;
loc.y -= [self frame].origin.y;

//move pencil to dragged to point
[pathCurrent lineToPoint:loc];
//padview needs to redisplay with new path information
[self setNeedsDisplay:YES];
}
- (void)mouseUp:(NSEvent *)theEvent
{
//[path release];
}

- (void)keyDown:(NSEvent *)theEvent
{
NSString *keyChar = [theEvent characters];
if ( [keyChar isEqualToString:@"c"] )
{
[pathRed removeAllPoints];
[pathGreen removeAllPoints];
[pathBlue removeAllPoints];
[self setNeedsDisplay:YES];
}
}


- (BOOL)acceptsFirstResponder
{
return YES;
}

- (IBAction)SetColorPath:(NSString *)NSstr //wouldn't allow me (void *) for return type, not connected to button
{
BOOL test;
NSBezierPath *NewPath;
NSColor *NewColor;
ColorPath *NewColorPath;

NewPath = [[NSBezierPath bezierPath] retain];
ColorPath.path=NewPath;

test=[NSstr isEqualToString:@"red"];
if (test) NewColor = [[NSColor redColor] retain];
test=[NSstr isEqualToString:@"green"];
if (test) NewColor = [[NSColor greenColor] retain];
test=[NSstr isEqualToString:@"blue"];
if (test) NewColor = [[NSColor blueColor] retain];


ColorPath.color=NewColor;
pathCurrent=ColorPath.path;
[ColorPaths addObject:NewColorPath];

}


- (void)ChooseBlue:(id)sender
{
[self SetColorPath:@"blue"];
}


- (void)ChooseGreen:(id)sender
{
[self SetColorPath:@"green"];
}


- (void)ChooseRed:(id)sender
{
[self SetColorPath:@"red"];
}



@end