home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
magazine
/
drdobbs
/
1990
/
02
/
bergman.lst
next >
Wrap
File List
|
1989-12-26
|
7KB
|
350 lines
_C_TALK/VIEWS_
by Noel Bergman
[LISTING ONE]
!SketchApp subclass of: Object !
className: SketchApp
superClass: Object
header:/* Class name: SketchApp */
/* Super name: Object */
/* File name: Sketchpp */
/* header */
#include "objtypes.h"
extern id Object;
extern id Archiver;
extern id Notifier;
classVariables:/* class variables */instanceVariables:id objects;
id filename; /* Filename string */
int changed; /* changed flag */
fileName:Sketchpp !
!SketchApp class methods !
!SketchApp instance methods !
add_ drawObj
id drawObj;
{
@self->objects add_ drawObj@; /* add to collection */
@self changed@;
}
!
asCollect
{
return self->objects;
}
!
changed
{
self->changed = TRUE;
}
!
clear
/* Erase the objects in our collection. */
{
extern id OrdCollect;
if (self->filename) self->filename = @self->filename free@;
if (self->objects) @self->objects freeAll@;
self->objects = @OrdCollect new@;
@self changed@;
if (self->objects) return TRUE; else return FALSE;
}
!
getFname
{
return self->filename;
}
!
loadFrom_ fname
id fname;
{
id fstr, arc;
if (fname)
{
arc = @Archiver openForLoad_ fname@;
if (arc)
{
if (@self clear@) @self->objects freeAll@;
self->objects = @arc getObject@;
@self changed@;
@arc free@;
self->filename = fname;
return TRUE;
}
}
return FALSE;
}
!
main
{
}
main()
{
id aView, aModel;
extern id SketchView;
aModel = @SketchApp new@;
aView = @SketchView create_ "Sketch" ViewOf_ aModel@;
if (aView)
{
@aView show@;
@Notifier start@;
}
}
!
save
{
id arc;
if (arc = @Archiver openForStore_ self->filename@)
{
@Notifier beginWait@;
@arc putObject_ self->objects@;
@arc free@;
self->changed = FALSE;
@Notifier endWait@;
}
}
!
saveAs_ filename
id filename;
{
if(self->filename) @self->filename free@;
self->filename = filename;
@self save@;
}
!
[LISTING TWO]
*** New PointArray methods:
getFrom_ anArchive
/* restore the receiver's size, recSize and size objects from the archive. */
id anArchive; /* id of an Archiver object to store the receiver to */
{
id anId;
unsigned i;
@super getFrom_ anArchive@;
self->howMany = @anArchive getInt@;
}
putTo_ anArchive
/* Store the receiver's size, recSize and size objects to the archive. */
id anArchive; /* id of an Archiver object to store the receiver to */
{
id anId;
unsigned i;
@super putTo_ anArchive@;
@anArchive putInt_ self->howMany@;
}
*** New Rect methods:
getFrom_ anArchiver
{
self->left = @anArchiver getInt@;
self->top = @anArchiver getInt@;
self->right = @anArchiver getInt@;
self->bottom = @anArchiver getInt@;
}
putTo_ anArchiver
{
@anArchiver putInt_ self->left@;
@anArchiver putInt_ self->top@;
@anArchiver putInt_ self->right@;
@anArchiver putInt_ self->bottom@;
}
*** New PolyLine methods:
getFrom_ anArchiver
{
self->ptArray = @anArchiver getObject@;
}
putTo_ anArchiver
{
@anArchiver putObject_ self->ptArray@;
}
*** New Line methods:
getFrom_ anArchiver
{
self->x0 = @anArchiver getInt@;
self->y0 = @anArchiver getInt@;
self->x1 = @anArchiver getInt@;
self->y1 = @anArchiver getInt@;
}
putTo_ anArchiver
{
@anArchiver putInt_ self->x0@;
@anArchiver putInt_ self->y0@;
@anArchiver putInt_ self->x1@;
@anArchiver putInt_ self->y1@;
}
*** New Box methods:
getFrom_ anArchiver
{
self->rec = @anArchiver getObject@;
}
putTo_ anArchiver
{
@anArchiver putObject_ self->rec@;
}
*** New or changed SketchView methods:
*
* You must add external ids for SketchApp, String, and FileSelect to the
* SketchView class. Also remove the instance variable "objects" and the
* main method.
*
erase
/* Erase the objects drawn on the receiver. */
{
if (self->model)
{
@self->model clear@;
@self update@;
}
}
initialize
/* Initialize receiver by creating a port to scribble on. */
{
id aMenu;
extern id Port, Menu, PopupMenu;
extern char *scribNmes[];
extern int scribSels[], scribKeys[];
if (aMenu = @Menu new@)
{
if (self->fMenu = @PopupMenu new_ "&Functions"@)
{
@self->fMenu setNames_ scribNmes sels_ scribSels keys_ scribKeys for_ self@;
@aMenu append_ self->fMenu@;
@self setMenu_ aMenu@;
if (@self->model clear@)
if (self->aPort = @Port createOn_ self@) /* create graphics port on receiver */
return self;
}
}
return NIL;
}
char *scribNmes[] = {"S&ketch\tF1", "&Line\tF2", "&Box\tF3", "E&rase\tF4", "&Save\tF5", "Save&As\tF6", "&Open\tF7", NULL};
int scribKeys[] = { K_F1, K_F2, K_F3, K_F4, K_F5, K_F6, K_F7, K_NULL};
int scribSels[] = {`sketch_`, `line_`, `box_`, `erase`, `save`, `saveAs`, `open`, 0};
mouseDnX_ x Y_ y
/* Method to respond to mouse DOWN events. */
int x,y; /* position in receiver where mouse down event occured */
{
if (self->drawClass)
{
@Notifier captureMouseFor_ self@; /* capture all mouse events to this window */
@Notifier mouseTrkOn@; /* enable mouse traking => mouse movement events */
self->drawObj = @self->drawClass new@; /* new instance of draw object */
@self->model add_ self->drawObj@; /* add to model */
@self->drawObj setPort_ self->aPort@;
return @self->drawObj mouseDnX_ x Y_ y@; /* pass forward mouse event */
}
else return FALSE;
}
open
{
id fstr, str;
fstr = @String newCStr_ "*.skh"@;
str = @FileSelect ask_ "Select a Sketch file" for_ fstr of_ self@;
if (str)
{
if (!!@self->model loadFrom_ str@) @str free@;
@self update@;
}
@fstr free@;
}
paint
/* Draw all graphic objects contained by receiver in the objects ordered */
/* collection. */
{
int sel = `drawOn_`;
id retainedObjs;
if (retainedObjs = @self->model asCollect@)
@retainedObjs do_ sel with_ self->aPort@;
return TRUE;
}
save
{
if (@self->model getFname@) @self->model save@;
else @self saveAs@;
}
saveAs
{
id fstr, str;
fstr = @String newCStr_ "*.skh"@;
str = @FileSelect ask_ "Select a Sketch file" for_ fstr of_ self@;
if (str)
{
@self->model saveAs_ str@;
}
@fstr free@;
}
Example 1: A generic shape method
*** Generic "shape" method for SketchView ***
<shape>_ aMenuItem
id aMenuItem; /* id of selected menu item */
{
@self->fMenu uncheckAll@;
self->drawClass = <ShapeClass>;
@aMenuItem check@;
}