home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga MA Magazine 1998 #3
/
amigamamagazinepolishissue1998.iso
/
maksiu
/
extensions
/
jwindows.lha
/
me.Asc
< prev
next >
Wrap
Text File
|
1996-04-25
|
8KB
|
236 lines
'*********************************************************************
'*
'* Me
'*
'*********************************************************************
' This little program demonstrates simple file handling. It opens a window
'in which the user can type his/her name, age and weight. They can then save
'this and load for viewing at another time.
' In the loading/saving procedures an attempt is made at more realistic
'file handling. If an error occurs, a requester appears to tell the user that
'the file cannot be loaded/saved. It does not jump to _CLEANUP and end the
'program. We also place an ID string at the start of the data file to identify
'files from this program. Finally, we keep track of whether the user has
'changed any data, and if he tries to load new data over this, we bring up
'a 'Load? All data will be lost" requester.
'*********************************************************************
Global _SCRAPTAGS,SCRAPTAGS,_PORTLIST,_MESSLIST
Global PATH$,OSVER
Global FHEIGHT,FWIDTH,MBAR,OX,OY,SW,SH
Dim _MEGADS(6)
Global _MEGADS()
'Oooops!! Made a spelling mistake in the GadToolsBox editor. _AGW should
'read _AGE. Well, I can live with that...
Global _NAMESTRING,_SURSTRING,_AGW,_WEIGHT,_SAVE,_LOAD
Dim _MEZOOM(1)
Global _MEZOOM()
Global _MEWIND
'** MYASL is a pointer to an ASL file requester we'll make later
' LOOSE keeps track of whether there is loosable data. False=No, True=Yes
Global MYASL
Global LOOSE
On Error Proc _CLEANUP
'** These two strings are the title texxts for the file requester, depending
' on whether we are loding or saving. This way, the strings are only
' defined once.
Global _LOADTXT,_SAVETXT
_LOADTXT=J Make String("Select a Me file to load")
_SAVETXT=J Make String("Save file as")
_INITIALIZE
_GUIDATA
_SETUPALL
_SETPORTS
'** here we create the file requester, with a few appropriate tags.
'Note that this is done AFTER opening the window otherwise ASLFR_Window
'would be rather pointless.
'Remember to add a J Free Asl Requester line to the _CLEANUP routine
J Tag _SCRAPTAGS,1,Equ("ASLFR_TitleText"),J Make String("Select a med file")
J Tag Equ("ASLFR_SleepWindow"),True
J Tag Equ("ASLFR_Window"),_MEWIND
J Tag Equ("ASLFR_RejectIcons"),True
J Tag 0,0
MYASL=J Create Asl Requester(Equ("ASL_FileRequest"),_SCRAPTAGS)
Do
K=J Wait Message
While K
C=J Tag Data(_MESSLIST,1)
If C=Equ("IDCMP_CLOSEWINDOW")
If J Easy Request(_MEWIND,"Me","Do you really"+Chr$(10)+"want to quit?"," Quit | Cancel ",0)
_CLEANUP
End If
Else If C=Equ("IDCMP_REFRESHWINDOW")
_DOREFRESH
Else If C=Equ("IDCMP_GADGETUP")
_HANDLEGADGETS
End If
K=J Next Message
Wend
Loop
'** This routine handles all the gadget presses
Procedure _HANDLEGADGETS
On Error Proc _CLEANUP
G=J Tag Data(_MESSLIST,4)
If G=_MEGADS(_SAVE)
'save the data:
'first, we set up and call the file requester. We then save the file
'with the _SAVEFILE procedure.
J Tag SCRAPTAGS,1,Equ("ASLFR_DoSaveMode"),True
J Tag Equ("ASLFR_TitleText"),_SAVETXT
J Tag 0,0
F$=J File Request$(MYASL,SCRAPTAGS)
If F$<>""
_SAVEFILE[F$]
End If
Else If G=_MEGADS(_LOAD)
'Load some data
'R states whether it's ok to load data over what's there. If LOOSE is
'false, it's automatically true, but if LOOSE is true, we bring up
'a requester for the user and let him decide.
'Then set up the file requester and get a file. load this.
R=True
If LOOSE
R=J Easy Request(_MEWIND,"Load?","All present data"+Chr$(10)+"will be lost!"," Ok | Cancel ",0)
End If
If R
J Tag SCRAPTAGS,1,Equ("ASLFR_DoSaveMode"),False
J Tag Equ("ASLFR_TitleText"),_LOADTXT
J Tag 0,0
F$=J File Request$(MYASL,SCRAPTAGS)
If F$<>""
_LOADFILE[F$]
End If
End If
Else
'If any other gadget has been selected, the data is presumed to have
'changed, and LOOSE is set to TRUE. The data gadgets don't need handling.
LOOSE=True
End If
End Proc
Procedure _SAVEFILE[F$]
On Error Proc _CLEANUP
'Open the file if possible. Note how we trap an error if it occurs
'then jump to E, rather than quiting.
Trap F=J Open(F$,Equ("MODE_NEWFILE"))
If Errtrap Then Goto E
'Initially, we set the ID of the file to Err!. If the load procedure
'finds this, it will know there was a writing error. We change this
'after writing the data.
Trap J Write Ascii$ F,"Err!"
If Errtrap Then Goto E
'Here, we write in the four items of data, reading the directly from the
'gadgets as we go. Note that we write 2 strings, then 2 numbers. Reading
'must take place in this order.
Trap J Write$ F,J Read String$(J Gadget String(_MEGADS(_NAMESTRING)))
If Errtrap Then Goto E
Trap J Write$ F,J Read String$(J Gadget String(_MEGADS(_SURSTRING)))
If Errtrap Then Goto E
Trap J Write F,J Gadget Number(_MEGADS(_AGW))
If Errtrap Then Goto E
Trap J Write F,J Gadget Number(_MEGADS(_WEIGHT))
If Errtrap Then Goto E
'Now the writing is done, we can go back to the start of the file and write
'in a valid ID string. Note that this MUST be the same length. 4 characters
'is conventional.
J Set Pof F,0
J Write Ascii$ F,"Jme."
'Close the file, set LOOSE to false and quit.
If F Then F=J Close(F)
LOOSE=False
Pop Proc
E:
'If an error has occured, close the file if open and put up an error
'requester. This doesn't actually request, it informs, but that's
'rather pedantic.
If F Then F=J Close(F)
V=J Easy Request(_MEWIND,"Error!","Couldn't save"+Chr$(10)+"the file!","Cancel",0)
End Proc
Procedure _LOADFILE[F$]
On Error Proc _CLEANUP
'Try and open the file
Trap F=J Open(F$,Equ("MODE_OLDFILE"))
If Errtrap Then Goto E
'Test the ID string of the file. If it's not what it should be, goto the
'error routine. Note the use of E$. This allows us to specify the error
'better for the users benefit. If E$ is not set, a default string is
'displayed (see later).
A$=J Read Ascii$(F,4)
If A$="Err!"
E$="There was an error"+Chr$(10)+"when writing this file!"
Goto E
Else If A$<>"Jme."
E$="This is not"+Chr$(10)+"one of my files!"
Goto E
End If
'Read off the data from the file. As long as the file is open and is
'a 'Me' file, there is no real need to trap the data. If something
'really goes wrong, the program quits, but this should never happen.
N$=J Read$(F)
S$=J Read$(F)
A=J Read(F)
W=J Read(F)
If F Then F=J Close(F)
'Here, we set the data in the four gadgets to the new value. Since
'the string gadgets will copy the string, a static string is unnecessary.
J Tag SCRAPTAGS,1,Equ("GTST_String"),Varptr(N$)
J Tag 0,0
J Set Gadget Data _MEGADS(_NAMESTRING),_MEWIND,SCRAPTAGS
J Tag SCRAPTAGS,1,Equ("GTST_String"),Varptr(S$)
J Set Gadget Data _MEGADS(_SURSTRING),_MEWIND,SCRAPTAGS
J Tag SCRAPTAGS,1,Equ("GTIN_Number"),A
J Set Gadget Data _MEGADS(_AGW),_MEWIND,SCRAPTAGS
J Tag SCRAPTAGS,1,Equ("GTIN_Number"),W
J Set Gadget Data _MEGADS(_WEIGHT),_MEWIND,SCRAPTAGS
LOOSE=False
Pop Proc
E:
'if the file is open, close it. If E$ is unset, use a default. Finally,
'display E$ in a requester.
If F Then F=J Close(F)
If E$=""
E$="Couldn't load"+Chr$(10)+"the file!"
End If
V=J Easy Request(_MEWIND,"Error!",E$," Cancel ",0)
End Proc
'Gadstools procs
Procedure _INITIALIZE
Procedure _SETUPALL
Procedure _GUIDATA
Procedure _MAKEMEGADS
Procedure _MAKEMEWIND[SC]
Procedure _MERENDER
Procedure _DOREFRESH
Procedure _SETPORTS
Procedure _FREEWIND[W,G,M,A,C]
Procedure _CLEANUP