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:

TEACTIVATE

Syntax: TEACTIVATE WINDOW(6)

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.

TECALTEXT

Syntax: TECALTEXT WINDOW(6)

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.

TEDEACTIVATE

Syntax: TEDEACTIVATE WINDOW(6)

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.

TEDELETE

Syntax: TEDELETE WINDOW(6)

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.

TEINSERT

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.

TEKEY

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.

TESCROLL

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.

TESETSELECT

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.

TESETTEXT

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.

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.”
endofstring=LEN(OriginalEditString$)
‘ Set up rectangle for TEUPDATE call
rect%(0)=20:rect%(1)=10:rect%(2)=200:rect%(3)=460

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  ”
LOCATE 15,1
PRINT “The Text Edit field has been created. (EDIT FIELD statement)”
LOCATE 17,1:PRINT “TextEdit Buffer:”
oldkey$=””
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) 
ERASERECT VARPTR(rect%(0))
TEUPDATE VARPTR(rect%(0)),WINDOW(6)
LOCATE 15,1
PRINT “The Text Edit field has been set to a new string (TESETTEXT call).”
GOSUB eventloop

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

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

‘De-activate the edit field 
TEDEACTIVATE WINDOW(6)
LOCATE 15,1
PRINT “The text has been deactivated (TEDEACTIVATE call)”
GOSUB eventloop

‘Re-activate the edit field...
TEACTIVATE WINDOW(6)
LOCATE 15,1
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)
LOCATE 15,1
PRINT “Text has been inserted at the insertion point. (TEINSERT call)”;STRING$(20,” 
“)
GOSUB eventloop

quit:
EDIT FIELD CLOSE 1
CLS: END

eventloop:
WHILE MOUSE(0)<>1
GOSUB TextEdit
key$=EDIT$(1)
IF key$<>oldkey$ THEN GOSUB processtext
oldkey$=key$
WEND
RETURN

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

TextEdit:
myHandle#=WINDOW(6)
LOCATE 17,24:PRINT myHandle#
ptr1=PEEK(myHandle#+3)
ptr2=PEEK(myHandle#+2)
ptr3=PEEK(myHandle#+1)
ptr4=PEEK(myHandle#+0)
ptr#=(ptr3*(65536!)) + (ptr2*256) + ptr1
telength1 = PEEK(ptr#+61)
teLength2=PEEK(ptr#+60)
teLength = teLength2*256+telength1
LOCATE 17,35:PRINT ptr#
LOCATE 17,45:PRINT teLength
RETURN

wrap:
GOSUB TextEdit : REM get teRec address
REM poke new values in teRec
crOnlyAdd#=ptr#+72
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
NEXT i
TECALTEXT WINDOW(6): REM re-do line endings
RETURN

Fig. 3 Set new text

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Whitethorn Games combines two completely...
If you have ever gone fishing then you know that it is a lesson in patience, sitting around waiting for a bite that may never come. Well, that's because you have been doing it wrong, since as Whitehorn Games now demonstrates in new release Skate... | Read more »
Call of Duty Warzone is a Waiting Simula...
It's always fun when a splashy multiplayer game comes to mobile because they are few and far between, so I was excited to see the notification about Call of Duty: Warzone Mobile (finally) launching last week and wanted to try it out. As someone who... | Read more »
Albion Online introduces some massive ne...
Sandbox Interactive has announced an upcoming update to its flagship MMORPG Albion Online, containing massive updates to its existing guild Vs guild systems. Someone clearly rewatched the Helms Deep battle in Lord of the Rings and spent the next... | Read more »
Chucklefish announces launch date of the...
Chucklefish, the indie London-based team we probably all know from developing Terraria or their stint publishing Stardew Valley, has revealed the mobile release date for roguelike deck-builder Wildfrost. Developed by Gaziter and Deadpan Games, the... | Read more »
Netmarble opens pre-registration for act...
It has been close to three years since Netmarble announced they would be adapting the smash series Solo Leveling into a video game, and at last, they have announced the opening of pre-orders for Solo Leveling: Arise. [Read more] | Read more »
PUBG Mobile celebrates sixth anniversary...
For the past six years, PUBG Mobile has been one of the most popular shooters you can play in the palm of your hand, and Krafton is celebrating this milestone and many years of ups by teaming up with hit music man JVKE to create a special song for... | Read more »
ASTRA: Knights of Veda refuse to pump th...
In perhaps the most recent example of being incredibly eager, ASTRA: Knights of Veda has dropped its second collaboration with South Korean boyband Seventeen, named so as it consists of exactly thirteen members and a video collaboration with Lee... | Read more »
Collect all your cats and caterpillars a...
If you are growing tired of trying to build a town with your phone by using it as a tiny, ineffectual shover then fear no longer, as Independent Arts Software has announced the upcoming release of Construction Simulator 4, from the critically... | Read more »
Backbone complete its lineup of 2nd Gene...
With all the ports of big AAA games that have been coming to mobile, it is becoming more convenient than ever to own a good controller, and to help with this Backbone has announced the completion of their 2nd generation product lineup with their... | Read more »
Zenless Zone Zero opens entries for its...
miHoYo, aka HoYoverse, has become such a big name in mobile gaming that it's hard to believe that arguably their flagship title, Genshin Impact, is only three and a half years old. Now, they continue the road to the next title in their world, with... | Read more »

Price Scanner via MacPrices.net

B&H has Apple’s 13-inch M2 MacBook Airs o...
B&H Photo has 13″ MacBook Airs with M2 CPUs and 256GB of storage in stock and on sale for up to $150 off Apple’s new MSRP, starting at only $849. Free 1-2 day delivery is available to most US... Read more
M2 Mac minis on sale for $100-$200 off MSRP,...
B&H Photo has Apple’s M2-powered Mac minis back in stock and on sale today for $100-$200 off MSRP. Free 1-2 day shipping is available for most US addresses: – Mac mini M2/256GB SSD: $499, save $... Read more
Mac Studios with M2 Max and M2 Ultra CPUs on...
B&H Photo has standard-configuration Mac Studios with Apple’s M2 Max & Ultra CPUs in stock today and on Easter sale for $200 off MSRP. Their prices are the lowest available for these models... Read more
Deal Alert! B&H Photo has Apple’s 14-inch...
B&H Photo has new Gray and Black 14″ M3, M3 Pro, and M3 Max MacBook Pros on sale for $200-$300 off MSRP, starting at only $1399. B&H offers free 1-2 day delivery to most US addresses: – 14″ 8... Read more
Department Of Justice Sets Sights On Apple In...
NEWS – The ball has finally dropped on the big Apple. The ball (metaphorically speaking) — an antitrust lawsuit filed in the U.S. on March 21 by the Department of Justice (DOJ) — came down following... Read more
New 13-inch M3 MacBook Air on sale for $999,...
Amazon has Apple’s new 13″ M3 MacBook Air on sale for $100 off MSRP for the first time, now just $999 shipped. Shipping is free: – 13″ MacBook Air (8GB RAM/256GB SSD/Space Gray): $999 $100 off MSRP... Read more
Amazon has Apple’s 9th-generation WiFi iPads...
Amazon has Apple’s 9th generation 10.2″ WiFi iPads on sale for $80-$100 off MSRP, starting only $249. Their prices are the lowest available for new iPads anywhere: – 10″ 64GB WiFi iPad (Space Gray or... Read more
Discounted 14-inch M3 MacBook Pros with 16GB...
Apple retailer Expercom has 14″ MacBook Pros with M3 CPUs and 16GB of standard memory discounted by up to $120 off Apple’s MSRP: – 14″ M3 MacBook Pro (16GB RAM/256GB SSD): $1691.06 $108 off MSRP – 14... Read more
Clearance 15-inch M2 MacBook Airs on sale for...
B&H Photo has Apple’s 15″ MacBook Airs with M2 CPUs (8GB RAM/256GB SSD) in stock today and on clearance sale for $999 in all four colors. Free 1-2 delivery is available to most US addresses.... Read more
Clearance 13-inch M1 MacBook Airs drop to onl...
B&H has Apple’s base 13″ M1 MacBook Air (Space Gray, Silver, & Gold) in stock and on clearance sale today for $300 off MSRP, only $699. Free 1-2 day shipping is available to most addresses in... Read more

Jobs Board

Medical Assistant - Surgical Oncology- *Apple...
Medical Assistant - Surgical Oncology- Apple Hill Location: WellSpan Medical Group, York, PA Schedule: Full Time Sign-On Bonus Eligible Remote/Hybrid Regular Apply Read more
Omnichannel Associate - *Apple* Blossom Mal...
Omnichannel Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Business Analyst | *Apple* Pay - Banco Popu...
Business Analyst | Apple PayApply now " Apply now + Apply Now + Start applying with LinkedIn Start + Please wait Date:Mar 19, 2024 Location: San Juan-Cupey, PR Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.