Using the new Window Objects in NS BASIC 2.0

The enhancements to NS BASIC in Rev 2.0 do more than add some features to the language: properly used, they change the way you go about programming in BASIC to a more object oriented approach.

This deserves more explanation, especially to programmers who are used to the procedural orientation of traditional programming in languages such as FORTRAN, COBOL and of course, BASIC.

In a procedural language, execution of the program continues in a predictable fashion from one statement to the next. When input is required from the user, it is prompted for and execution continues.

An object oriented language places one or more objects (such as input lines, checkboxes, etc.) on the screen at the same time. When the user interacts with one, the code associated with it executes. The program is driven by the objects, not the other way around. Object oriented programming started to make real sense as soon as we put a mouse in the hands of the user and gave him the ability to click wherever he wanted; it is equally important when using a pen based system.

NS BASIC 1 was almost completely procedural. In Rev 2, we've added a number of window objects. You can program these to work either procedurally or using the object oriented technique.

Let's start by looking at some of the NS BASIC Window Objects and how to program them. The window objects are for the most part inherited from objects that are already built into the Newton's ROM, which cuts down on the overhead they require considerably and will make most of them look quite familiar to anyone who's used a Newton.

Here's a program that puts up a simple text input box:

0010 LET ws.viewBounds={left: 20, right: 200, top: 20, bottom: 100} 
0020 window w,ws,"text" 
0030 show w

In the above example, the variable ws is a frame which specifies how the object works. Each element in the frame is commonly called a slot. The slot ViewBounds is the boundary of the object. Each object has its own list of slots; default values for most of them make it easier to set up.

Some of the other slots that you might want to set for the text object are Text (to put an initial value in), viewLineSpacing, viewFlags (to set the recognition), viewFont and viewFormat (to define what the border and fill patterns are). Experienced NewtonScript programmers will recognize these slot names: since NS BASIC is reusing objects built into the Newton ROM, all the usual slots are available.

Now, we'll see how using an object to get input is different than the usual INPUT statement in BASIC. After the program executes line 30, it continues- with the window object remaining on the screen. So long as it remains on the screen, the user can write text into it.

Because the Newton is single threaded, the object won't recognize text while the NS BASIC program is busy running code. You'll want to stop the program and wait for the user to input something, using a WAIT statement.

The WAIT statement stops all activity in NS BASIC. It accepts a parameter for length of time to wait, so execution can automatically resume; or it will unpend if an object becomes active as the result of a user action.

You could do this by putting a GOTO or GOSUB slot into the ws frame, with a line number to resume execution. This would get called when the value of the Text in the frame changes (as when the user writes in the object). However, since happens as soon as the user writes a few characters into the window, it may be premature. The user might not be done yet.

A better strategy is to have a button on the screen that the operator taps when input is complete, as follows:

0100 LET ws2.viewBounds={left: 20, right:200, top:100, bottom: 115} 
0110 LET ws2.text="Tap here to Save" 
0120 LET ws2.viewJustify=2 
0130 LET ws2.GOTO=2000
0180 window w2,ws2 0190 show w2

Add some additional code to do the WAIT and print the contents of the Text object when the button is tapped, and here's the program:

0010 LET ws.viewBounds={left: 20, right: 200, top: 20, bottom: 100} 
0020 window w,ws,"text" 
0030 show w 
0100 LET ws2.viewBounds={left: 20, right:200,
top:100, bottom: 115} 
0110 LET ws2.text="Tap here to Save" 
0120 LET ws2.viewJustify=2 
0130 LET ws2.GOTO=2000 
0180 window w2,ws2 
0190 show w2 
1000 rem wait 
1010 wait 10000 
1020 end 
2000 rem button pressed 
2010 hide //hide all windows 
2020 print ws.text

(In your applications, you may want to format this a bit differently to conform to the usual Newton User Interface Guidelines)

Scroller is a much fancier version of the Text object. It allows you to display and edit text that is much larger than will display in the window at one time. We'll change line 20 to use it instead, and we'll have to give it enough text to fill it up:

0015 LET ws.text="Scroller is a much fancier version of the Text object. It
allows you to display and edit text that is much larger than will display in
the window at one time." 
0020 window w,ws,"scroller"

You can use the scroll arrows at the top of the box to scroll the text within the box. The "Large Mountains" at the top of the box, when tapped, expands the box to fill the whole screen and make it editable.

If your want to use the scroller to display text only, and not allow it to be edited, set the EditOK slot to NIL.

How much text can the scroller handle? It depends on how much heap space you have in your Newton. I expect most Newtons will allow it to be as much as 5 or 10K of text.

Another useful window object is the Gauge. It displays a bar, much like the battery indicator in the extras drawer. With a little hocus pocus, you can turn it into a progress indicator:

0010 LET ws.viewValue=0 
0020 window w,ws,"Gauge" 
0030 show w 
0040 for ws.viewValue=1 to 100 step 5 
0050 wprint w,"" 
0060 next ws.viewValue

The WPRINT command forces a redraw of the object with the new viewValue.

The last object I'll talk about is a new one called LabelInput. It's pretty handy as an all around input object. It automatically puts a label to the left of the field, which save you having to set up another object with the label for the object. More importantly, it also allows you to specify a pop up list of values for the text field.

Let's put LabelInput through its paces. First, let's make a simple version of it.

0090 window w,ws,"labelInput" 
0095 show w

Doesn't get much simpler. Let's put a proper label on it and supply an initial value.

0010 LET ws.Label="Bottle Size" 
0020 LET ws.text="Full"
To put a pick list on for values, add the following code:

0030 LET ws.labelCommands=["Full","Magnum","Imperial"]
For our last trick, let's put a checkmark beside one of the items on the list

0040 LET ws.curLabelCommand=2
Our final code is:

0010 LET ws.Label="Bottle Size" 
0020 LET ws.text="Full" 
0030 LET ws.labelCommands=["Full","Magnum","Imperial"] 
0040 LET ws.curLabelCommand=2
0090 window w,ws,"labelInput" 
0095 show w

Other slots of interest for the LabelInput object include entryFlags (to set the recognition), labelFont, viewValue, GOTO, GOSUB and of course, viewBounds.

NS BASIC has a whole bag full of objects for you to use. Besides the ones mentioned already, there's CheckBox, Draw, Glance, LabelPicker, Month, Paragraph, Picker, SetClock and Slider. If you are designing a form, using these objects to paint your screen will define much of your application. Still to come will be routines to verify the data, save it to a file and transmit it to another system.

All of the NS BASIC objects are quite simple to use: you'll rarely need more than a few lines of code to set them up. With the interactive nature of BASIC, it's quick to make modifications to your slot values to adjust them to work properly. If you have NS BASIC attached through the Newton's serial port to your PC or Mac, you can modify code using your real keyboard and screen, while viewing the results simultaneously on your Newton.


[This article first appeared in PDA Developers 3.2, Mar/Apr 1995. Copyright (C) 1995 by Creative Digital Inc. All rights reserved. Reprinted with permission. For more information about PDA Developers contact Creative Digital at 415.621.4252 or CDigital© eworld.com.]


Last modified: April 1, 1995
NS BASIC Corporation
Phone: 416 264-5999 Fax: 416 264-5888
Internet: info© nsbasic.com CompuServe: 74431,412