Examples

Add a Custom Slot Proto

You might find that NewtApp does not have the perfect proto for displaying and editing data that your application needs. For example, what if you want to display a numeric value using a slider? In such cases, you will need to create a custom slot proto.

Custom slot protos have two responsibilities:

The custom slot proto we are going to create is a slider which allows the user to edit a numeric value. In order to make it reusable, we'll create it as a user proto.

A QuickTime movie of this example is available.

1. Create a user proto and draw a protoSlider. Add a Retarget slot with the following contents:

func()
begin
   if target and path and target.(path) then
      SetValue(self, 'viewValue, target.(path));
end
The code checks to make sure that there is indeed a current target, that the path slot is set, and that the slot referenced by path exists in the target. If so, it reads the value from target, and then sets the viewValue of the protoSlider to that value.

2. When the view is instantiated, it needs to also read from target. Modify the viewSetupFormScript to:

func()
begin
   :Retarget();
end
3. When the slider changes, it needs to update target. Modify the ChangedSlider method to:

func()
begin
   if target and path then begin
      target.(path) := viewValue;
      :StartFlush();
   end;
end
4. Now, remove the viewBounds slot of the protoSlider (as is normally done with user protos).

5. Name the proto "mySlider" and add it to the project.

At this point we can use our new proto in the Bookstore project. We could either draw out a template that is based on mySlider, or we could simply change one of the existing slot protos so that it protos from mySlider.

6. Change the _proto slot of the newtLabelNumInputLine that handles the number of books in stock to use mySlider as its proto.

7. Add minValue and maxValue slots to the mySlider template, so that the slider will have some reasonable minimum and maximum values.


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

Last modified: 1 DEC 1996