next up previous
Next: A Case Study: the Up: RECView.m Program Listing Previous: Browser delegate methods

View methods

There are several methods which the programmer must supply to create or modify the content of a View. Which ones are needed and their complexity depends on the intricacy of the planned view. The most essential methods are:

Beyond these essentials there are other possibilities, such as assigning a delegate.

/* = = = = = = = = = = = = = =   R E C V I E W   = = = = = = = = = = = = = */

- initFrame:(const NXRect *)frameRect
/*
 * Initializes the new RECView object.  First, an initFrame: message is sent 
 * to super to initialize RECView as a View.  Next, the RECView sets its own 
 * state -- that it is opaque and that the origin of its coordinate system 
 * lies in the center of its area.  
 */
{        
        [super initFrame:frameRect];
        [self setOpaque:YES];
        [self translate:floor(frame.size.width/2) :floor(frame.size.height/2)];
        return self;
}

- drawSelf:(const NXRect *)rects :(int)rectCount
/*
 * Draws the RECView's background and axes.  If there are any points,
 * these are drawn too.
 */
{        
        if (rects == NULL) return self;

//        PSsetgray(NX_WHITE);
        PSsetgray(NX_LTGRAY);
        if (drawBack) NXRectFill(&rects[0]);
        PSsetgray(NX_DKGRAY);
//        PSsetgray(NX_BLACK);

                    isErrm=FALSE;
                    [value setStringValue: " "];
                    if (rec(cstr)) [value setStringValue: "T"];
                        else [value setStringValue: "F"];
                    if (isErrm) {[comment setStringValue: errm]; isErrm=FALSE;}
                    PSstroke();

        return self;
}

- sizeTo:(NXCoord)width :(NXCoord)height
/*
 * Ensures that whenever the RECView is resized, the origin of its 
 * coordinate system is repositioned to the center of its area.
 */
{
        [super sizeTo:width :height];
        [self setDrawOrigin:-floor(width/2) : -floor(height/2)]; 

        return self;
}

- mouseDown:(NXEvent *) theEvent
/*
 * Responds to a message the system sends whenever the user presses the 
 * mouse button when the cursor is over the RECView. The RECView changes 
 * the cursor to a cross-hairs image and then starts asking for mouse-dragged 
 * or mouse-up events. As it receives mouse-dragged events, the RECView 
 * updates the readOut text Cell with the cursor's coordinates. If the user 
 * releases the mouse button while the cursor is over the RECView,the RECView 
 * registers the point and then sends a message to its delegate notifying it 
 * of the new point.
 */
{
        int        looping = YES, oldMask;
        NXPoint        aPoint;
        NXRect        plotRect;

        [crossCursor set];
           [self getBounds:&plotRect];

        oldMask = [window addToEventMask:NX_MOUSEDRAGGEDMASK];
        [self lockFocus];
        do {
                aPoint = theEvent->location;
                [window flushWindow];
                if (theEvent->type == NX_MOUSEUP) {
        /* on mouse-up, register point, inform delegate, and clean up state */
                        [window flushWindow];
                        looping = NO;
                }        
           } while (looping && 
    (theEvent=[NXApp getNextEvent:NX_MOUSEUPMASK|NX_MOUSEDRAGGEDMASK]));
        [self unlockFocus];
        [window setEventMask:oldMask];
        [NXArrow set];
        return self;
}



Microcomputadoras
2000-09-28