Newton Toolkit
Volume Number: 9
Issue Number: 11
Column Tag: Newton
Newton Toolkit
You’ve seen Newt and what it can do - now take a look at the tools
developers will work with.
By Don Bresee and Neil Ticktin, MacTech Magazine Editorial Staff
Tony Espinoza, Newton Software Tools, Apple Computer, Inc.
By now, you’ve taken a look at Newton. (If not, you should go and read the
editorial starting on page 4 of this issue.) But as developers, you want to know about
the tools for Newton - specifically the development environment. The Newton Toolkit
(NTK for short) is how you develop apps for Newton. Below is a description of
Newton’s built in tools, NTK and NewtonScript.
The Newton Toolkit
First, you need to know some basics about Newton programming and about NTK
itself.
Newton Basics
The Newton system is an extremely versatile object oriented environment
designed around the concept of the view. A view is any graphic component of the
Newton user interface. Views also serve as the basic building blocks of Newton
applications. You design and build these views with the Newton Toolkit for the Mac,
which we will talk about later. First, it is necessary to get aquainted with the
NewtonScript programming language.
NewtonScript
NewtonScript is a full featured, object oriented programming language. It has all
the programming constructs and data structures needed to produce sophisticated
applications. Some highlights:
• All statements are expressions (they return a value),
• Statements are grouped with BeginEnd,
• Loop constructs For, While, and RepeatUntil are provided,
• A Foreach statement allows looping through the elements of a data structure,
• Break statement for terminating loops,
• Functions and Methods with parameters and local variables,
• Return statement for functions and methods,
• Arithmetic and string operators,
• Class and parent / child inheritance.
The most basic data reference in the Newton system the long word (32 bits). A
reference is either a pointer or an immediate reference. Immediate references can be
integer, character, or boolean and use 30 bits for data and 2 bits for flags. Booleans
have two possible values, TRUE or NIL. Characters are represented in Unicode (in two
bytes) to facilitate international conversions. Pointers reference objects in the heap.
These objects include:
• Symbols (variable names, field names, script names),
• Strings,
• Reals (64 bit floating point numbers),
• Arrays (numbered elements, starting with 0),
• Frames (named elements).
Symbols are useful in writing general code. For instance, you could write a
function that takes a symbol parameter that is the name of another function to call in a
certain situation. The same idea can be used for field names in data structures. A
routine could take a field name as a parameter, and perform some operation on that
field, allowing you to write more general code.
The array data structure is extremely useful. The elements can be of different
types in the same array. Elements can be added, deleted, and sorted. This type is ideal
for lists of objects. Arrays can be given a class type. This seems to be for readability
reasons since the class does not give an array any special properties, just a name.
This is different than frames, which we will talk about now.
The frame is the most important type in the Newton system. A frame consists of a
collection of slots. Each slot has a name and a value. A slot value can be a function,
making the frame type capable of class definitions. Frames have special slots for class
names and links to other classes that are needed for the inheritance mechanisms.
There are two types of inheritance in NewtonScript. The normal class
inheritance that we are used to is called prototype inheritance in NewtonScript. When
a frame is defined, the superclass name should be put in the “_proto” slot. Then the
new frame will have access to (“inherit”) all the slots of that class, this includes data
and methods.
The second type of inheritance is called parent inheritance. This mechanism is
similar to the “chain of command” used in the THINK Class Library (TCL). This type
of inheritance is used to set up hierarchies (views that contain and manage subviews).
When a frame is defined, the class name of the parent (or supervisor) should be put in
the “_parent” slot.
These two inheritance mechanisms interact in much the same way as they do in
TCL. When a message is sent to an object, that object is checked first. If it can handle
the message, it does. If not, the prototype (superclass) is checked, and then its
prototype. If this process bottoms out (no prototype of the initial object can handle the
message), the message is passed along to the parent (supervisor). Then the same
process is used for the parent object (if the parent object can’t handle the message, the
prototypes are checked and then that objects parent, and so on). For example, in a TCL
application, when a quit message is sent to the gopher (assume it is a view), the
gopher and its superclasses attempt to handle the message and cannot. The message is
passed up the chain of command to the window, then to the document, and then to the
application which completes the handling of the message (probably by a superclass of
your application object).
The Newton System
The view is the most important object in Newton applications. Views are created
from templates. A template is a frame containing the data description of an object (a
view). The relationship between views and templates is similar to that of objects and
classes.
Dozens of “proto templates” are provided in the Newton ROM. These templates
offer most of the user interface functionality you will need when creating applications.
It is quite complete. It includes buttons, pictures, text (static and editable), geometric
shapes, pick-lists, and plenty of others. In addition to a view classes specific messages
it handles, there are several system messages that can be sent to any view. These
messages will allow you to customize your views behavior by supplementing the
normal system behavior when certain events occur. For instance, you may want a
certain view class to do something every time it is drawn. You simply define a
“viewDrawScript” method for your view class and it will be called each time the
system draws a view of that type.
Newton’s data storage and retrieval model is very interesting. It uses a unified
data model, all data is stored in a common format. Therefore, no translation is
required to read another application’s data. The model is built around soups. A soup
holds a collection of related data objects called entries. Each entry is a frame. There is
no restriction on the format of the entries. Different entries in the same soup can have
different slots. Of course, this can get out of hand very quickly and it is a good idea to
keep the structure of the entries uniform within each soup. Optional slots are useful
for minimizing the number of bytes needed for an entry. This is an important point
because a soup is a persistent storage object and memory is precious. In fact, when
entries are saved to or retrieved from a soup, they are compressed or decompressed
behind the scenes. This is done to make best use of memory. You can also define an
index for each slot in a soup that will be searched frequently.
Each soup has a name and is contained in a store, which is also named. A store can
be thought of as a disk volume. The Newton contains one internal store. Additional
stores can be present on PCMCIA cards and other devices. To retrieve entries from a
soup, you call the “Query” function. This function returns a cursor object. A cursor
accesses the set of entries that satisfy the query, and gives the ability to step through
those entries in indexed order. The system provides functions to do whatever you
might want with stores, soups, entries, and cursors.
Another important concept on the Newton is Routing, which simplifies
communications. Print, Fax, and Mail are all handled through one API. You define a
format (a view) for the data and specify that format in the routing frame. You also
design a routing slip that will get any additional information needed from the user
before the operation is commenced (such as a name and fax number to send a fax). The
system takes care of the other details for you.
Newton Toolkit
The Newton Toolkit (NTK) is the environment provided for the development of
Newton applications. The main part of NTK is a Mac application that allows you to
define views and write scripts. It also allows you to combine layout (templates),
resource, data, and driver files into a project. NTK does this with a project document
in much the same way THINK C does. The project window lists all the files in the
project. Double clicking on a file in the list opens it for modification. “Building”
your project results in what is called a package.
Apple recommends that this application be run on a Quadra with 8 MB of RAM and
System 7.1. I played with it on a IIci and it took one minute fifteen seconds to launch
from the Finder, and one minute ten seconds to open the checkbook sample. Once a
project is open, it runs plenty fast enough. Expect installation of NTK to use about 2.5
MB of disk space. [We are reviewing 1.0b4 version. 1.0b6 is reportedly five times
faster and will therefore easily run on a 68030 machine or better. Expect the release
version to have better performance. - Ed.]
NTK also provides a Newton application called Toolkit which allows you to
download your package to the Newton and test it (accessible on the Newton through the
Extras drawer). You can test it and see the results immediately. This allows you to
develop your application incrementally. You work on a view, build a package, download
the package to the Newton, and then test it. You do this routine until that view is
working right and then you build the next view.
Views and templates are created using the Template Editor. This editor has a
layout window for drawing views (which is done by dragging out rectangles like in you
would any drawing application), and a palette for selecting the view class. This palette
has all the predefined Newton interface elements (templates), as well as any templates
you have defined in the current project. Items are added by selecting an item from the