Welcome to part 3 of resource programming in GFA Basic. This time 
                     
I will cover getting information in and out of resources  (dialog 
                     
boxes) using editable fields.
                     
                     
                     
You  may  have noticed,  that over the  last  three  issues,  the 
                     
programs  all start off the same way,  the management  of  dialog 
                     
boxes always follows these simple steps:
                     
                     
                     
1)   Reserve memory for the file - RESERVE.
                     
                     
                     
2)   Free any existing resource memory - ~RSRC_FRE().
                     
                     
                     
3)   Load in the resource file using RSRC_LOAD
                     
     (you can do error detection (ie.  file failed to load)if you 
                     
     want.)
                     
                     
                     
4)   Get the object numbers and tree address(es).
                     
     (For multiple form resources - you need all the trees!)
                     
                     
                     
5)   Put the dialog box onto the screen - using OBJC_DRAW.
                     
                     
                     
6)   Pass control over to GEM via the FORM_DO command.
                     
                     
                     
7)   Find out what objects were selected, and react as required.
                     
                     
                     
8)   Remove the resource from memory ~RSRC_FRE(),  and relinquish 
                     
     reserved memory.
                     
                     
                     
Steps  5,6 and 7 can be repeated as required for your  particular 
                     
program.
                     
                     
                     
This Month's Program
                     
====================
                     
                     
                     
This  time,  the program and resource,  demonstrate how  to  pass 
                     
information to,  and get information back from,  editable  fields 
                     
within  a FORM.  You should have seen this type of input,  it  is 
                     
presented by the ST when you want to create a folder,  or type in 
                     
a  file name via the file selector.  You can put  default  values 
                     
into these fields,  mask off unwanted entries,  and preformat the 
                     
way the information is presented.
                     
                     
                     
If you carefully read the introduction in the first part of  this 
                     
series,  you may have noticed that I said that an editable  field 
                     
can't be the last object within a form,  otherwise you will crash 
                     
the ST.  So with this in mind,  always sort the objects when  you 
                     
create them,  and if there is an editable object as the last one, 
                     
create  a  dummy IBOX and hide it.  This does not  apply  to  our 
                     
program, as the last object is an EXIT box.
                     
                     
                     
The  idea of the program,  is that it stores phone  numbers;  you 
                     
could  use this to auto phone a bulletin board,  or simply  as  a 
                     
phone book.  
                     
                     
                     
The resource file contains the usual EXIT boxes,  and  introduces 
                     
some  new objects:  the left and right arrows (within a  box)  is 
                     
called G_BOXCHAR,  the editable field is G_FTEXT.  The status  of 
                     
the  boxed  characters  (the left and right  arrows)  is  set  as 
                     
TOUCHEXIT.  This is slightly different to the normal DEFAULT/EXIT 
                     
box.  An  object  with TOUCHEXIT status will send a  message  via 
                     
FORM_DO  when  you click on it (as soon as you  press  the  mouse 
                     
button),  whereas  an object which is selectable (like  the  EXIT 
                     
box)  will only react when the mouse button has been pressed  and 
                     
released.
                     
                     
                     
Some of the editable fields have been preformatted, ie. they have 
                     
a mask.  If you type in a name in the file name area, it will put 
                     
the '.' in for you.  So if you type 'ABC' and the press the  full 
                     
stop,  the  cursor  will automatically go to the part  where  you 
                     
enter the file's extender. The phone number also, has minus signs 
                     
in it,  you cannot type over them, the AES does the formatting of 
                     
the text you type in, for you.
                     
                     
                     
                     
                     
The program flow looks like this:
                     
                     
                     
                     
                     
______________________  error in loading ??
                     
| Load resource file |------->-----------_______________________
                     
         |                               |Try finding resource |
                     
         |load ok?                          |             |     
                     
         |----------------------<---<-------|ok           |failed
                     
_________|______________                           _______|______
                     
| Is there a data file?|                           | End program|
                     
| then load it.        |                                         
                     
         |              
                     
         |
                     
_________|______________
                     
| If data exists - put |
                     
| it into resource     |-------<-----+
                     
         |        ^                  ^
                     
         |      < | >          ______|_________
                     
_________|________|____        | no file name |
                     
| edit resource boxes |               |        
                     
    |             |         __________^________
                     
    |             +-->------| clicked on SAVE |
                     
    |                               |          
                     
____|________________               |      
                     
| Clicked on Cancel |               |
                     
         |                          |
                     
         |                          |
                     
_________|_______                   |
                     
|  EXIT PROGRAM |--------<----------+  
                     
                 
                     
                     
                     
You can see from the flow chart, that the program is a continuous 
                     
loop,  waiting for input,  if you select the left or right arrows 
                     
(increase or decrease entry) you go back to the edit mode. If you 
                     
click on SAVE,  but there is no file name specified,  then you go 
                     
back to edit.
                     
                     
                     
                     
                     
First set up room for the variables
                     
DIM entry_no%(100),name$(100),location$(100)
                     
DIM comment$(100),phone_number$(100)
                     
                     
                     
next the normal load bit (see steps 1-3 above)
                     
RESERVE -732
                     
~RSRC_FREE()
                     
a&=RSRC_LOAD("i_o.rsc")
                     
IF a&=0
                     
  ALERT 1,"Can't find Resource file|Try and select it with|the fileselector",1,"OK",v|
                     
  FILESELECT "i_o.rsc","",r$
                     
  IF r$<>""
                     
    a&=RSRC_LOAD(r$)
                     
  ELSE
                     
    END
                     
  ENDIF
                     
ENDIF
                     
                     
                     
set up the variables
                     
rsc_vars
                     
                     
                     
get the address of the structure
                     
~RSRC_GADDR(0,edit|,tree%)
                     
                     
                     
try to load the default file for the data
                     
@load_dial_dir
                     
                     
                     
set number of entries to the start
                     
entries%=1
                     
                     
                     
calculate the coordinates for the form
                     
~FORM_CENTER(tree%,x&,y&,w&,h&)
                     
                     
                     
now sit in a loop until finished
                     
REPEAT
                     
put your data onto the form, so you can edit it
                     
  CHAR{{OB_SPEC(tree%,entryno|)}}=STR$(entries%)
                     
  CHAR{{OB_SPEC(tree%,name|)}}=name$(entries%)
                     
  CHAR{{OB_SPEC(tree%,comment|)}}=comment$(entries%)
                     
  CHAR{{OB_SPEC(tree%,locatn|)}}=location$(entries%)
                     
  CHAR{{OB_SPEC(tree%,phoneno|)}}=phone_number$(entries%)
                     
  CHAR{{OB_SPEC(tree%,filename|)}}=fname$
                     
  
                     
draw the form on screen
                     
  ~OBJC_DRAW(tree%,0,7,x&,y&,w&,h&)
                     
                     
                     
hand over control to the AES,
                     
  result|=FORM_DO(tree%,0)
                     
                     
                     
clear the button that caused the exit from the form
                     
  OB_STATE(tree%,result|)=BCLR(OB_STATE(tree%,result|),0)
                     
  
                     
get all entries from the form
                     
  LET name$(entries%)=CHAR{{OB_SPEC(tree%,name|)}}
                     
  comment$(entries%)=CHAR{{OB_SPEC(tree%,comment|)}}
                     
  location$(entries%)=CHAR{{OB_SPEC(tree%,locatn|)}}
                     
  phone_number$(entries%)=CHAR{{OB_SPEC(tree%,phoneno|)}}
                     
  fname$=CHAR{{OB_SPEC(tree%,filename|)}}
                     
                     
                     
did you click on a right/left arrow?
                     
  SELECT result|
                     
  CASE decremnt|
                     
    IF entries%>1
                     
      DEC entries%
                     
    ENDIF
                     
  CASE incrmt|
                     
    IF entries%<100
                     
      INC entries%
                     
    ENDIF
                     
  ENDSELECT
                     
                     
                     
did you click on cancel?
                     
  EXIT IF result|=cancel|
                     
                     
                     
keep going until you click on SAVE and you have a file to save to
                     
UNTIL result|=save| AND fname$<>""
                     
                     
                     
save the file
                     
IF result|=save|
                     
  @save_dial_dir
                     
ENDIF
                     
                     
                     
all done
                     
~RSRC_FREE()
                     
RESERVE
                     
END
                     
                     
                     
the resource variables here
                     
PROCEDURE rsc_vars
                     
  LET edit|=0                         ! DIALOG TREE
                     
  LET decremnt|=1                     !   Boxed Char
                     
  LET entryno|=2                      !   Formatted Text
                     
  LET incrmt|=3                       !   Boxed Char
                     
  LET name|=4                         !   Formatted Text
                     
  LET locatn|=5                       !   Formatted Text
                     
  LET comment|=6                      !   Formatted Text
                     
  LET phoneno|=7                      !   Formatted Text
                     
  LET file|=8                         !   String
                     
  LET filename|=9                     !   Formatted Text
                     
  LET cancel|=10                      !   Button
                     
  LET save|=11                        !   Button
                     
RETURN
                     
                     
                     
if a data file exists - load it
                     
PROCEDURE load_dial_dir
                     
  IF EXIST("DIAL.DIR")<>0
                     
    fname$="DIAL    DIR"
                     
    OPEN "r",#1,"dial.dir",63
                     
    n%=1
                     
    FIELD #1,20 AS name$
                     
    FIELD #1,16 AS location$
                     
    FIELD #1,17 AS comment$
                     
    FIELD #1,10 AS n$
                     
    WHILE NOT EOF(#1)
                     
      GET #1,n%
                     
      LET name$(n%)=TRIM$(name$)
                     
      location$(n%)=TRIM$(location$)
                     
      comment$(n%)=TRIM$(comment$)
                     
      phone_number$(n%)=TRIM$(n$)
                     
      INC n%
                     
    WEND
                     
    CLOSE #1
                     
  ENDIF
                     
RETURN
                     
                     
                     
save your file
                     
PROCEDURE save_dial_dir
                     
  OPEN "r",#1,fname$,63
                     
  FIELD #1,20 AS name$
                     
  FIELD #1,16 AS location$
                     
  FIELD #1,17 AS comment$
                     
  FIELD #1,10 AS n$
                     
  FOR n%=1 TO entries%
                     
    LSET name$=name$(n%)
                     
    LSET location$=location$(n%)
                     
    LSET comment$=comment$(n%)
                     
    LSET n$=phone_number$(n%)
                     
    PUT #1,n%
                     
  NEXT n%
                     
  CLOSE #1
                     
RETURN
                     
                     
                     
I  have  used a random access file,  you could use any  type  you 
                     
wish. 
                     
                     
                     
If you would like more information on resource programming in GFA 
                     
Basic,  then I would reccomend two books.  For version 2 users  - 
                     
Advanced  Programming  in  GFA Basic.  For V3  users  -  Software 
                     
Development  in  GFA Basic.  Both available from GFA  Data  Media 
                     
(UK).
                     
                     
                     
John Peters
                     
                     
                     
                     

Takaisin

(C) Marko, Suomen Atari-sivut / ArkiSTo 2003