viewSetupFormScript()

Changing Slots in viewBounds

There is a subtle point in the preceding code: the portion that modifies viewBounds. You might have been tempted to write it like this:

viewBounds.right := viewBounds.left + textWidth;
The problem with this assignment is that viewBounds is a read-only frame that is in the package. This assignment tries to follow the proto chain back to viewBounds in the package and modify it there (see FIGURE 6.2).

FIGURE 6.2 : One wrong way to change a slot in viewBounds.


It does you no better to try this code either:

self.viewBounds := viewBounds;
viewBounds.right := viewBounds.left + textWidth;
This creates a new slot in the view, but that slot simply points to the same viewBounds frame as the template (see the right side of FIGURE 6.3). This is still attempting to write to read-only memory.

FIGURE 6.3 : Another wrong way to change a slot in viewBounds.


To change the viewBounds correctly, you must create a whole new frame. Here's one way to do it:

local bounds := Clone(viewBounds);
self. viewBounds := bounds;
viewBounds.right := viewBounds.left + textWidth;
First is some code that clones the viewBounds frame and assigns it to a local bounds. Next, a new viewBounds slot is created in the view with the code:

self.viewBounds := bounds;
Some slot must point at the new frame, and it can't be the viewBounds slot in the template (the template is in pseudo-ROM and can't be changed). So, a viewBounds slot is added to the frame (see FIGURE 6.4).

The last approach has the disadvantage that the left, top, and bottom slots are duplicated in the viewBounds frame in RAM, and in the package. A solution is to take advantage of proto inheritance. Inheritance can be used with any frame, not just for views and templates.

The best way to modify the slot is:

self. viewBounds  := {
   _proto: viewBounds,
   right: viewBounds.left + textWidth,
};
This creates a new frame, but just protos to the old viewBounds so that it inherits left, top, and bottom slots. FIGURE 6.5 shows the result.

FIGURE 6.4 : A better way to change a slot in viewBounds.


FIGURE 6.5 : The best way to change a slot in viewBounds.


An online version of Programming for the Newton using Macintosh, 2nd ed. ©1996, 1994, Julie McKeehan and Neil Rhodes.

Last modified: 1 DEC 1996