routine ANIMAL.CTL given FIELD.ID, FORM yielding STATUS
''
''    AUTHOR: Steve Bailey
''    CREATED: 9-8-93
''    DESCRIPTION:
''      Control routine for the menu bar of the program.
''

    define FIELD.ID as a text variable
    define FORM as a pointer variable
    define STATUS as an integer variable
    define FIELD.PTR as a pointer variable
    define OLD.ITEMS, ITEMS as 1-dim text arrays
    define I as an integer variable
    define COLUMN, ROW as integer saved variables  '' currently selected
    define .WIDTH to mean 4     '' width of table

    let FIELD.PTR = DFIELD.F(FIELD.ID, FORM)

    select case FIELD.ID
      case "TABLE" 
        let ITEMS(*) = DARY.A(DFIELD.F("TABLE", FORM))
        let ROW = DIV.F(INT.F(DDVAL.A(FIELD.PTR))-1, .WIDTH) + 1
        let COLUMN = MOD.F(DDVAL.A(FIELD.PTR)-1, .WIDTH) + 1

          '' Set the text displayed in the combo boxes to the attributes of 
          '' the animal at the selected row 
        let DTVAL.A(DFIELD.F("NAME", FORM)) = ITEMS((ROW-1) * .WIDTH +1)
        let DTVAL.A(DFIELD.F("CLASS", FORM)) = ITEMS((ROW-1) * .WIDTH +2)
        let DTVAL.A(DFIELD.F("LEGS", FORM)) = ITEMS((ROW-1) * .WIDTH +3)
        let DTVAL.A(DFIELD.F("REGION", FORM)) = ITEMS((ROW-1) * .WIDTH +4)

          '' redisplay the combo boxes to show changes.  We could have also
          '' modified the DARY.A attribute to change the list of items 
          '' displayed in the drop down
        display DFIELD.F("NAME", FORM)
        display DFIELD.F("CLASS", FORM)
        display DFIELD.F("LEGS", FORM)
        display DFIELD.F("REGION", FORM)
        
      case "ADD" 
          '' create a new array of table items containing all existing entries
          '' from current table
        let OLD.ITEMS(*) = DARY.A(DFIELD.F("TABLE", FORM))
        reserve ITEMS as DIM.F(OLD.ITEMS(*)) + .WIDTH
        for I = 1 to DIM.F(OLD.ITEMS(*)) do
            let ITEMS(I) = OLD.ITEMS(I)
        loop

          '' copy selected attributes from combo boxes to new table's last row 
        let ITEMS(DIM.F(OLD.ITEMS(*)) + 1) = DTVAL.A(DFIELD.F("NAME", FORM))
        let ITEMS(DIM.F(OLD.ITEMS(*)) + 2) = DTVAL.A(DFIELD.F("CLASS", FORM))
        let ITEMS(DIM.F(OLD.ITEMS(*)) + 3) = DTVAL.A(DFIELD.F("LEGS", FORM))
        let ITEMS(DIM.F(OLD.ITEMS(*)) + 4) = DTVAL.A(DFIELD.F("REGION", FORM))

          '' redisplay the table to show changes.  
        let DARY.A(DFIELD.F("TABLE", FORM)) = ITEMS(*)
        display DFIELD.F("TABLE", FORM)

      case "UPDATE"
          '' change the entity in the currently selected table row to the
          '' attributes selected in the combo boxes
        if ROW gt 0 
            let ITEMS(*) = DARY.A(DFIELD.F("TABLE", FORM))
            let ITEMS((ROW-1) * .WIDTH +1) = DTVAL.A(DFIELD.F("NAME", FORM)) 
            let ITEMS((ROW-1) * .WIDTH +2) = DTVAL.A(DFIELD.F("CLASS", FORM)) 
            let ITEMS((ROW-1) * .WIDTH +3) = DTVAL.A(DFIELD.F("LEGS", FORM)) 
            let ITEMS((ROW-1) * .WIDTH +4) = DTVAL.A(DFIELD.F("REGION", FORM)) 

              '' redisplay the table
            display DFIELD.F("TABLE", FORM)
        always

      case "REMOVE"
          '' remove the currently selected row from the table
          '' do this by creating a new array or items, and copying all existing
          '' items from the table except those from the deleted row
        let OLD.ITEMS(*) = DARY.A(DFIELD.F("TABLE", FORM))
        if ROW ge 0 and DIM.F(OLD.ITEMS(*)) > .WIDTH
            reserve ITEMS(*) as DIM.F(OLD.ITEMS(*)) - .WIDTH
            for I = 1 to (ROW-1) * .WIDTH do
                let ITEMS(I) = OLD.ITEMS(I)
            loop

              '' copy the items after deleted row
            for I = ROW * .WIDTH + 1 to DIM.F(OLD.ITEMS(*)) do
                let ITEMS(I - .WIDTH) = OLD.ITEMS(I)
            loop

            let DARY.A(DFIELD.F("TABLE", FORM)) = ITEMS(*)
            display DFIELD.F("TABLE", FORM)
        always

      case "QUIT"
        let STATUS = 1

      default
    endselect
end
