' Test to create a SimpeText file with styl resource
' by Al Staffieri Jr.
' 10/8/99
LOCAL FN createTextFile (efNum, useStyle)
' efNum is Edit Field number
' useStyle is boolean: 1 = create style res; 0 = text only
GET FIELD editHndl&, efNum
textSize& = PEEK WORD ([editHndl&]) ' length of text
fullSize& = FN GETHANDLESIZE (editHndl&) ' length of text + style
styleSize& = fullSize& - textSize& ' length of style info
fil$ = FILES$(0, "Save text file as:",, volRefNum%)
LONG IF fil$ > ""
' WARNING: Should check if file exists here.
' Open data fork and write text to data fork
DEF OPEN "TEXTttxt"
OPEN "O", #1, fil$,,volRefNum%
osErr = FN HLOCK (editHndl&)
WRITE FILE #1, [editHndl&] + 2, textSize&
osErr = FN HUNLOCK (editHndl&)
CLOSE #1
LONG IF useStyle <> 0 ' non 0 = create styl resource
' Open resource fork of new file
volRefNum% = FOLDER ("", volRefNum%)
CALL CREATERESFILE (fil$)
tempRef% = FN OPENRESFILE (fil$)
LONG IF tempRef% > 0
' create styl handle and write to resource
styleHndl& = FN NEWHANDLE (styleSize&)
LONG IF styleHndl& <> 0
osErr = FN HLOCK (styleHndl&)
BLOCKMOVE [editHndl&] + textSize& + 2, [styleHndl&], styleSize&
CALL ADDRESOURCE (styleHndl&, _"styl", 128, "")
CALL UPDATERESFILE (tempRef%)
osErr = FN HUNLOCK (styleHndl&)
DEF DISPOSEH (editHndl&)
END IF
CALL CLOSERESFILE (tempRef%)
END IF
END IF
END IF
DEF DISPOSEH (editHndl&)
END FN
' if you don't want to show the edit field, use a negative window id (-1)
WINDOW 1, "", (0,0)-(508,338),1
a$ = "This is a test of creating a SimpleText file with a styl resource."
TEXT _Monaco, 12, 0
EDIT FIELD -1, a$, (10,10)-(490,300)
FN createTextFile (1,1)
END
Al Staffieri Jr.