TweetFollow Us on Twitter

TextEdit from BASIC
Volume Number:3
Issue Number:6
Column Tag:BASIC School

TextEdit from MS BASIC

By Dave Kelly, MacTutor Editorial Board

As you may recall from previous discussions about MS Basic, there are several statements in version 3.0 of the interpreter and version 1.0 of the compiler which have not yet been documented. Well, at least these statements have not been documented by Microsoft. In fact, they are documented in Inside Macintosh (and books like Macintosh Revealed ). The statements I refer to are all calls to the Macintosh Toolbox text-editing routines. Before this version of Basic text-editing was limited to whatever you could do with the Edit Field statement. The Macintosh text-edit routines provide basic text editing, with no added features. Some basic text edit capabilities include cut, paste, copy, insert, delete, and scrolling are provided. Word wrap, double-clicking to select words, inverse highlighting is supported. It allows you to edit a single font, style, and size (display only one at a time). It provides the basics of what full blown word processors provide. Text Edit does NOT support the use of more than one font or style, fully justified text, tabs and adjustment of spaces between words in the current system file. However, under the new system file for the Mac II and SE, multiple fonts and style are supported, but tabs and 32K limitations remain a problem. The new system file is scheduled to ship in May and should bring these new features to Mac Plus systems as well. A good review of text edit’s capabilities is given in the Pascal multi-window example in the January issue of MacTutor.

The Inside Macintosh Edit Records are automatically prepared by MS Basic when the EDIT FIELD statement is first defined. The Edit Record contains information which defines the complete Editing environment. You don’t have to know or worry about what goes on with the Edit Records to use the new calls. Each Edit Field has a handle (teHandle) which refers to the Edit Record. Microsoft has beefed up the EDIT FIELD statement so that when the Text Edit field is defined the text string no longer defaults to a selected state. Otherwise, your program may not have quite the appearance that you desire, especially if you are creating a text editor.

In the text edit field, a character position is an index into the text. Position zero corresponds to the first character of the edit field. The selection range is indicated by a starting and ending index into the text. The text that will be highlighted when selected by the mouse is indicated by the start and end indexes. If the starting and ending position is the same then the position is indicated by the insertion point. The insertion point is the position where the characters will be inserted.

Fig. 1 Edit Field Text Wraps!

There are 10 new calls which are already built into Basic. These 10 are only about half of the total number of routines given in Inside Macintosh . The routines which are not implemented are already provided for by using the EDIT FIELD statement in Basic. The statements NOT available are:

TEClick TECopy TECut

TEDispose TEGetText TEIdle

TEInit TENew TEPaste

TESetJust TextBox

First a few words about some of the syntax used. The WINDOW(6) function is used to return the handle (same as teHandle) for the active edit field in the current output window. WINDOW(6) returns zero if the window has no active edit field. The SADD(<string-expression>) function returns the address of the first byte of data in <string-expression>. Note that this value is only valid until another string allocation of a new variable assignment takes place, since string allocations and variable assignments can cause existing strings to move in memory. The selection range referred to in some of the statements is the range of text which is selected in an edit field. Here are the new commands with a quick description of their use and syntax:



Description: TEACTIVATE displays the insertion point and highlights the selected text (unless the selection is the insertion point) in the text edit field specified by the WINDOW(6) function. Macintosh User Interface Guidelines indicate that when a window is inactive, the text selected in a text field should be inactive and the insertion point should be hidden. Use this call when your window is activated. Remember that text edit always works on the currently active grafPort; ie the SetPort result. See TEDEACTIVATE also.



Description: TECALTEXT recalculates each line of text in the edit field indicated by the WINDOW(6) function. This call should be used whenever the number of characters per line of the edit field has been changed.



Description: TEDEACTIVATE hides the insertion point and unhighlights the selected text in the text edit field specified by the WINDOW(6) function. Macintosh User Interface Guidelines indicate that when a window is inactive, the text selected in a text field should be inactive and the insertion point should be hidden. Use this call when your window is deactivated. See TEACTIVATE also.



Description: TEDELETE removes the text which has previously been selected in the edit field indicated by the WINDOW(6) function. The text which is deleted is not recoverable.


Syntax: TEINSERT SADD(string ), LEN (string ), WINDOW(6)

Description: TEINSERT takes the specified string and inserts it at the insertion point of the edit field indicated by the WINDOW(6) function. Any selected text is not replaced by the newly inserted text. Use TEDELETE to delete selected text.


Syntax: TEKEY key , WINDOW(6)

Description: TEKEY replaces the selection range with the character key typed at the keyboard. If the selection range is an insertion point then the character is inserted. All types of characters (normal and control characters) are inserted so your routine should filter out any unwanted characters. TEKEY re-draws the text as necessary. It is not clear if this can be used with an Edit field in Basic, since the Edit field statement prevents INKEY$ from allowing you to get single keystrokes from the keyboard. The only apparent way to get text typed by the user during an active Edit field statement is to use EDIT$, which returns the entire string contents of the text edit record.


Syntax: TESCROLL horiz, vert, WINDOW(6)

Description: TESCROLL scrolls the text within the edit field specified by WINDOW(6) by the number of pixels specified by horiz and vert parameters. Positive parameters move the text to the right and down. Negative parameters move the text to the left and up. See Inside Macintosh for more information.


Syntax: TESETSELECT selStart, selEnd, WINDOW(6)

Description: TESETSELECT sets the selection range of the edit field text indicated by the WINDOW(6) function. The old text selection is unhighlighted and the text between selstart and selend is highlighted. Selstart and Selend can range from 0 to 32767. If Selend is beyond the last character of text, the position just past the last character is used.


Syntax: TESETTEXT SADD(string), LEN (string), WINDOW(6)

Description: TESETTEXT changes the text string of the edit field pointed to by the WINDOW(6) function. The edit field will now point to the string set by the new string parameter. The selection point is set to the end of the new text. However, the text is not re-drawn. Follow this with an ERASERECT and the next function below, TEUPDATE.


Syntax: TEUPDATE VARPTR(rect%(0)), WINDOW(6)

Description: TEUPDATE draws the text of the edit field specified by the WINDOW(6) function within the rectangle rect%.

The call descriptions are somewhat brief. It will be useful to look at a few examples. Some of the new calls will probably only be used occasionally since the Basic Edit Field routines automatically provide some of the functions. The text edit demo program I have prepared will show some working examples of some of the text edit calls. The TEKEY, and TESCROLL, calls will be saved for a later column. Now you can write your text editor in BASIC (well, sort of)!! The DOUBLE-CLICK ME programs that come with the Basic interpreter use a few of the calls, the QnD (Quick-N-Dirty) editor demo (included with Basic) uses a few more of the calls. The problem with these examples is that they don’t make use of the ToolLib routines for creating scroll bars. Instead, they draw and manipulate their own set of scroll bars and window bars by drawing rectangles in a blank window and keeping track of them all. This seems to make things a lot more confusing than it has to be. This might be an indication that these demos were actually written before it was decided that the ToolLib routines would be included. If so, then it seems to me that Microsoft would have had plenty of time to document the text edit routines by now. As of this time, no one at Microsoft has sent me any information about the text edit calls which I had requested. If the QnD edit was written after ToolLib was slated to be included in MS Basic then it appears that the authors of QnD didn’t yet know how to use the ToolLib Libraries. At any rate, QnD will give you some ideas of ways to implement a text editor.

The Text Edit Demo will show you the effects of each call as you click through each step by pressing the mouse button outside of the Edit Field. If you click inside the Edit Field (which is controlled automatically by Basic) you will be changing the insertion point or selecting and highlighting text, if you drag the mouse. If you type, characters will be inserted at the insertion point, and the text will wrap when it gets to the end of the line. More on this later. The demo will first create an edit field and put default text in it. Next you click the mouse button and the default text is replaced by a new string (set with the TESETTEXT call). The TEUPDATE call re-draws the edit field with the new string. Click the mouse again and the selection range is set up by the TESETSELECT call such that the text is selected from the 1st up to the 6th character. Another click of the mouse and the TEDELETE call deletes the selected text. The next clicks deactivate and activate the edit field followed by text inserted by the TEINSERT call. Most uses of these calls are demonstrated by this demo.

Word Wrapping Edit Fields

by David E. Smith

Normally, Basic sets up an Edit Field with a very large destination rectangle and with the text edit field crOnly set to FF00, a negative value. This indicates that lines will only break at a carriage return. Hence when you type in the Edit Field, the text never wraps at the end of the line as you would expect from normal use of text edit functions in other languages. Many people have been frustrated at trying to get text to word wrap in Edit Fields (see letters column this month for a few such frustrations!). In order to get text to wrap, the destination rectangle must be the same size or smaller than the view rectangle, which Basic sets equal to the Edit Field rectangle. Also the crOnly field must be positive. In our demo program, we call a subroutine called Wrap, which pokes the proper values into the text edit record so the destination rectangle is changed and the crOnly field is changed. Hence, if you type in the demo program, you’ll see that when you get to the edge of the Edit Field box, the text does word wrap properly. However, there is a penalty for this.

With word wrapping enabled, Basic no longer processes or returns carriage returns in the Edit Field, even when you set the type parameter to indicate that it should process carriage returns. Whatever mechanism Basic uses to intercept and process control characters in an active Edit Field apparently will not process carriage returns if the crOnly field is positive. You can try this with the demo. As you type (assuming you type at the end of the text), each character will be displayed on the Edit Buffer line. If a carriage return is received, then the program will print “CR” there. But with the subroutine call to Wrap, you’ll never see a CR. Now if you remark out the Wrap subroutine call, then carriage returns will be returned properly, but of course then the Edit Field won‘t wrap text to the view rectangle! This seems like a design bug in Basic as there is no obvious correlation between the text edit record set-up and whether or not carriage returns get filtered from the Edit Field.

Peeking at the text edit record and changing it’s fields can be a very interesting pursuit. To do this, you need to get the handle to the text edit record. This is returned with the WINDOW(6) function. You can store this in a double precision variable as shown in the program listing under the TextEdit subroutine.

myHandle# = WINDOW(6)

On our Text Buffer line, we’ve printed some important debugging information. The first large number is the teHandle to the text edit record. The next number is the address of the text edit record itself. The final small number is one of the text edit record fields, ie the length of text in the buffer. As you type, you’ll see this number increment. To dereference the handle, we use the PEEK function:

ptr1 = PEEK(myHandle#+3)
ptr2 = PEEK(myHandle#+2)
ptr3 = PEEK(myHandle#+1)
ptr4 = PEEK(myHandle#+0)

This returns the four individual bytes at the handle. They now have to be re-constructed into the master pointer:

ptr# = (ptr3*65536!)) + (ptr2*256) + ptr1

Now we have the address of the text edit record itself. This address is valid as long as we don’t do anything that would move things in memory, like call a toolbox routine. Now we can peek at individual text edit record items, like the length:

telength1 = PEEK(ptr#+61)
telength2 = PEEK(ptr#+60)
telength = telength2 * 256 + telength1

Once we’ve identified the text edit record, then our Wrap routine can poke new values into the destination rectangle and the crOnly field. The address of the crOnly field will be ptr#+72. The address of the destination rectangle is ptr# since it is the first field in the record. The address of the view rectangle, which is the second item in the field is ptr#+8. Rectangles are eight bytes, so if we poke the destination rectangle field with the view rectangle field, then the two will be the same, and the text will wrap. A little for next loop does this for us in the Wrap subroutine. To find the address of other items in the text edit record, just add up the size of each field. A rectangle is 8 bytes; a point is 4 bytes, as are pointers and handles; an integer is two bytes. Starting at zero, it is easy to determine the offset of each field in the record from the definition in Inside Macintosh.

Can you really make a text editor? It doesn’t look too good, even with these new routines. The problem is, you don’t really have an honest event loop, so how do you get keystrokes. The only events Basic lets you have are menu events and dialog type events. Key events are intercepted by Basic. For example, if you have an active Edit Field as we do in this example, how do you get the actual single character last pressed? INKEY$ is ignored when the Edit Field is active. The only option seems to be the EDIT$ statement, which returns the entire edit field contents. So you don’t really have a text editor if you can’t process keystrokes. Remember, TEKey only inserts characters into a text edit record, it doesn’t poll the keyboard. That is supposed to happen during an event loop, but Basic only lets you see certain events. If you find some more information on this subject of using MS Basic 3.0 with text edit, share it with our MacTutor audience.

Fig. 2 Making a selection

‘Text Edit Demo
‘By Dave Kelly
‘teRec Wrap by Dave Smith
‘©MacTutor 1987

OriginalEditString$=”This is the first string.”
‘ Set up rectangle for TEUPDATE call

WINDOW 1,”Text Edit Demo”,(15,40)-(495,330),2
EDIT FIELD 1,OriginalEditString$,(10,20)-(460,200),6,1
PRINT “Click mouse button outside of edit field or type  ”
PRINT “The Text Edit field has been created. (EDIT FIELD statement)”
LOCATE 17,1:PRINT “TextEdit Buffer:”
GOSUB wrap: REM make textedit wrap to view rectangle
TESETSELECT endofstring+1,endofstring+1,WINDOW(6)
GOSUB eventloop

‘Set the edit field text to a new string
NewEditString$=”This the second string. (replaced old string)”
TESETTEXT SADD(NewEditString$), LEN(NewEditString$), WINDOW(6)

‘Re-draw the edit field (needs to be re-drawn after setting new text) 
PRINT “The Text Edit field has been set to a new string (TESETTEXT call).”
GOSUB eventloop

‘Set the selection range 
PRINT “The text has been selected  selection range”; STRING$(50,” “)
PRINT “is from before character 1 until before character 6. (TESETSELECT 
GOSUB eventloop

‘Delete the selected text 
PRINT “The selected text has been deleted. (TEDELETE call)”;STRING$(30,” 
PRINT STRING$(150,” “)
GOSUB eventloop

‘De-activate the edit field 
PRINT “The text has been deactivated (TEDEACTIVATE call)”
GOSUB eventloop

‘Re-activate the edit field...
PRINT “The text has been activated (TEACTIVATE call)”; STRING$(10,” “)
GOSUB eventloop

‘Insert new text at the insertion point 
TextToInsert$=”-This text was inserted-”
TEINSERT SADD(TextToInsert$), LEN(TextToInsert$), WINDOW(6)
PRINT “Text has been inserted at the insertion point. (TEINSERT call)”;STRING$(20,” 
GOSUB eventloop


GOSUB TextEdit
IF key$<>oldkey$ THEN GOSUB processtext

chartyped$=RIGHT$(key$,1) :REM how do you get last char typed?
LOCATE 17,18
LOCATE 17,18
IF chartyped$<>CHR$(13) THEN PRINT chartyped$ ELSE PRINT “CR”

LOCATE 17,24:PRINT myHandle#
ptr#=(ptr3*(65536!)) + (ptr2*256) + ptr1
telength1 = PEEK(ptr#+61)
teLength = teLength2*256+telength1
LOCATE 17,35:PRINT ptr#
LOCATE 17,45:PRINT teLength

GOSUB TextEdit : REM get teRec address
REM poke new values in teRec
cronly%=0:POKE crOnlyAdd#,0 :REM turn OFF cronly
FOR i=0 TO 7
POKE ptr#+8+i,PEEK(ptr#+i): REM dest rect = view rect
TECALTEXT WINDOW(6): REM re-do line endings

Fig. 3 Set new text


Community Search:
MacTech Search:

Software Updates via MacUpdate

1Password 6.3.5 - Powerful password mana...
1Password is a password manager that uniquely brings you both security and convenience. It is the only program that provides anti-phishing protection and goes beyond password management by adding Web... Read more
QuickBooks R9 - Financial ma...
QuickBooks helps you manage your business easily and efficiently. Organize your finances all in one place, track money going in and out of your business, and spot areas where you can save. Built for... Read more
TextMate 2.0-beta.12.25 - Code/markup ed...
TextMate is a versatile plain text editor with a unique and innovative feature set which caused it to win an Apple Design Award for Best Mac OS X Developer Tool in August 2006. A rapidly growing... Read more
TunnelBear 3.0.4 - Subscription-based pr...
TunnelBear is a subscription-based virtual private network (VPN) service and companion app, enabling you to browse the internet privately and securely. Features Browse privately - Secure your data... Read more
PDFpen 8.2.1 - $74.95
PDFpen allows users to easily edit PDF's. Add text, images and signatures. Fill out PDF forms. Merge or split PDF documents. Reorder and delete pages. Even correct text and edit graphics! Features... Read more
PDFpenPro 8.2.1 - $124.95
PDFpenPro allows users to edit PDF's easily. Add text, images and signatures. Fill out PDF forms. Merge or split PDF documents. Reorder and delete pages. Create fillable forms and tables of content... Read more
Cosmo Run 2.5.1 - Arcade game with ever-...
Cosmo Run is an arcade game with ever-changing aspect of the play-world. Survive long enough and you will meet special alternative paths - both hardcore and rewarding. Are you worthy to command the... Read more
Box Sync 4.0.7724 - Online synchronizati...
Box Sync gives you a hard-drive in the Cloud for online storage. Note: You must first sign up to use Box. What if the files you need are on your laptop -- but you're on the road with your iPhone? No... Read more
VueScan 9.5.59 - Scanner software with a...
VueScan is a scanning program that works with most high-quality flatbed and film scanners to produce scans that have excellent color fidelity and color balance. VueScan is easy to use, and has... Read more
Skype - Voice-over-internet p...
Skype allows you to talk to friends, family and co-workers across the Internet without the inconvenience of long distance telephone charges. Using peer-to-peer data transmission technology, Skype... Read more

Latest Forum Discussions

See All

Best Fiends Forever Guide: How to collec...
The fiendship in Seriously's hit Best Fiends has been upgraded this time around in Best Fiends Forever. It’s a fast-paced clicker with lots of color and style--kind of reminiscent of a ‘90s animal mascot game like Crash Bandicoot. The game... | Read more »
5 apps for the budding mixologist
Creating your own cocktails is something of an art form, requiring a knack for unique tastes and devising interesting combinations. It's easy to get started right in your own kitchen, though, even if you're a complete beginner. Try using one of... | Read more »
5 mobile strategy games to try when you...
Strategy enthusiasts everywhere are celebrating the release of Civilization VI this week, and so far everyone seems pretty satisfied with the first full release in the series since 2010. The series has always been about ultra-addictive gameplay... | Read more »
Popclaire talk to us about why The Virus...
Humanity has succumbed to a virus that’s spread throughout the world. Now the dead have risen with a hunger for human flesh, and all that remain are a few survivors. One of those survivors has just called you for help. That’s the plot in POPCLAIRE’... | Read more »
Oceans & Empires preview build sets...
Hugely ambitious sea battler Oceans & Empires is available to play in preview form now on Google Play - but download it quickly, as it’s setting sail away in just a few days. [Read more] | Read more »
Rusty Lake: Roots (Games)
Rusty Lake: Roots 1.1.4 Device: iOS Universal Category: Games Price: $2.99, Version: 1.1.4 (iTunes) Description: James Vanderboom's life drastically changes when he plants a special seed in the garden of the house he has inherited.... | Read more »
Flippy Bottle Extreme! and 3 other physi...
Flippy Bottle Extreme! takes on the bottle flipping craze with a bunch of increasingly tricky physics platforming puzzles. It's difficult and highly frustrating, but also addictive. When you begin to master the game, the sense of achievement is... | Read more »
Plants vs. Zombies Heroes guide: How to...
Plants vs. Zombies Heroes surprised us all, presenting a deep deck building experience. It's a great CCG that stands up well to the competition. There are a lot of CCGs vying for players' attention at the moment, but PvZ Heroes is definitely one... | Read more »
Arcane Online takes Online RPG’s to anot...
If you think that you need a desktop to enjoy high quality MMO gaming then Arcane Online hopes to prove you emphatically wrong. An epic fantasy Online RPG set in the land of Eldine, Arcane Online offers an abundance of features and content that... | Read more »
It’s time to step up and start your spoo...
So you’ve just downloaded Seekers Notes: Hidden Mystery, and you obviously want to hit the ground running. Well you’re in luck because you just so happen to be reading our very useful guide to playing. First released in July last year, the game has... | Read more »

Price Scanner via

Check Apple prices on any device with the iTr...
MacPrices is proud to offer readers a free iOS app (iPhones, iPads, & iPod touch) and Android app (Google Play and Amazon App Store) called iTracx, which allows you to glance at today’s lowest... Read more
Apple, Samsung, Lead J.D. Power Smartphone Sa...
Customer satisfaction is much higher among smartphone owners currently subscribing to full-service wireless carriers, compared with those purchasing service through a non-contract carrier, according... Read more
Select 9-inch Apple WiFi iPad Pros on sale fo...
B&H Photo has select 9.7″ Apple WiFi iPad Pros on sale for up to $50 off MSRP, each including free shipping. B&H charges sales tax in NY only: - 9″ Space Gray 256GB WiFi iPad Pro: $799 $0 off... Read more
Apple refurbished 13-inch Retina MacBook Pros...
Apple has Certified Refurbished 13″ Retina MacBook Pros available for up to $270 off the cost of new models. An Apple one-year warranty is included with each model, and shipping is free: - 13″ 2.7GHz... Read more
Apple refurbished 15-inch Retina MacBook Pros...
Apple has Certified Refurbished 2015 15″ Retina MacBook Pros available for up to $380 off the cost of new models. An Apple one-year warranty is included with each model, and shipping is free: - 15″ 2... Read more
Apple refurbished 11-inch MacBook Airs availa...
Apple has Certified Refurbished 11″ MacBook Airs (the latest models), available for up to $170 off the cost of new models. An Apple one-year warranty is included with each MacBook, and shipping is... Read more
Apple refurbished Apple TVs available for up...
Apple has Certified Refurbished 32GB and 64GB Apple TVs available for up to $30 off the cost of new models. Apple’s standard one-year warranty is included with each model, and shipping is free: -... Read more
15-inch Retina MacBook Pros on sale for $200...
B&H Photo has 15″ Retina Apple MacBook Pros on sale for $200 off MSRP. Shipping is free, and B&H charges NY tax only: - 15″ 2.2GHz Retina MacBook Pro: $1799 $200 off MSRP - 15″ 2.5GHz Retina... Read more
Apple refurbished 2016 13-inch MacBook Airs a...
Apple has Certified Refurbished 2016 13″ MacBook Airs available starting at $849. An Apple one-year warranty is included with each MacBook, and shipping is free: - 2016 13″ 1.6GHz/8GB/128GB MacBook... Read more
Apple refurbished iMacs available for up to $...
Apple has Certified Refurbished 2015 21″ & 27″ iMacs available for up to $350 off MSRP. Apple’s one-year warranty is standard, and shipping is free. The following models are available: - 21″ 3.... Read more

Jobs Board

*Apple* Retail - Multiple Positions (Multi-L...
Job Description: Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, Read more
*Apple* Retail - Multiple Positions- New Yor...
Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, you're also the Read more
*Apple* Retail - Multiple Positions- Yonkers...
Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, you're also the Read more
Senior Manager, Security Engineering - *Appl...
Changing the world is all in a day's work at Apple . If you love innovation, here's your chance to make a career of it. You'll work hard. But the job comes with more Read more
Manager, Threat Intelligence - *Apple* Info...
Changing the world is all in a day's work at Apple . If you love innovation, here's your chance to make a career of it. You'll work hard. But the job comes with more Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.