
proc APSSelectADLFont {args} {
    APSStrictParseArguments {height type}
    set font(0) -misc-fixed-medium-r-normal--8-60-100-100-c-50-iso8859-1
    set font(1) -misc-fixed-medium-r-normal--9-80-100-100-c-60-iso8859-1
    set font(2) -misc-fixed-medium-r-normal--10-100-75-75-c-60-iso8859-1
    set font(3) -misc-fixed-medium-r-normal--13-100-100-100-c-70-iso8859-1
    set font(4) -misc-fixed-medium-r-normal--14-110-100-100-c-70-iso8859-1
    set font(5) -misc-fixed-medium-r-normal--15-120-100-100-c-90-iso8859-1
    set font(6) -sony-fixed-medium-r-normal--16-120-100-100-c-80-iso8859-1
    set font(7) -misc-fixed-medium-r-normal--20-140-100-100-c-100-iso8859-1
    set font(8) -sony-fixed-medium-r-normal--24-170-100-100-c-120-iso8859-1
    set font(9) -adobe-times-medium-r-normal--25-180-100-100-p-125-iso8859-1
    set font(10) -adobe-helvetica-medium-r-normal--34-240-100-100-p-176-iso8859-1
    set font(11) -adobe-helvetica-bold-r-normal--34-240-100-100-p-182-iso8859-1	
    if {$type == "label"} {
        set i 0
        foreach size "8 9 12 13 14 15 19 20 25 26 34" {
            if {$height <= $size} {
                return $font($i)
            }
            incr i
        }
        return $font($i)
    }
    if {$type == "button"} {
        set i 0
        foreach size "14 15 18 19 21 22 26 31 33 43 43" {
            if {$height <= $size} {
                return $font($i)
            }
            incr i
        }
        return $font($i)
    }
    if {$type == "scale"} {
        set i 0
        #"19 22 28 31 33 35 44 53 58 77"
        foreach size "22 28 31 33 35 44 53 58 77" {
            if {$height <= $size} {
                return $font($i)
            }
            incr i
        }
        return $font($i)
    }
    if {$type == "bar"} {
        set i 0
        foreach size "71 79 103 111 119 128 159 167 207 215 279" {
            if {$height <= $size} {
                return $font($i)
            }
            incr i
        }
        return $font($i)
    }
    if {$type == "indicator"} {
        set i 0
        foreach size "71 79 103 111 119 128 159 167 207 215 279" {
            if {$height <= $size} {
                return $font($i)
            }
            incr i
        }
        return $font($i)
    }
}

proc APSCreateToplevelADLScreen {widget args} {
    set filename ""
    set macro ""
    APSStrictParseArguments {filename macro}

    if {[winfo exists ${widget}]} {
        wm deiconify ${widget}
        return
    }
    toplevel $widget
    set uniqueID [clock clicks]
    wm protocol $widget WM_DELETE_WINDOW "set apsADLInfo(${uniqueID}.exists) 0 ; destroy $widget"
    APSCreateADLScreen .medm -parent $widget -filename $filename -macro $macro -uniqueID $uniqueID
}

proc APSCreateADLScreenHelp {} {
    return "\
Usage: APSCreateADLScreen widget
\[-parent <string>\]\t
\[-filename <file>\]\t
\[-macro <string>\]\t

Creates\t\$parent\$widget
"
}

proc APSCreateADLScreen {widget args} {
    set filename ""
    set parent ""
    set macro ""
    set uniqueID [clock clicks]
    APSStrictParseArguments {filename parent macro uniqueID}

    if {![catch {pv} result]} {
        pv toggleErrorMode
    }

    global apsADLInfo

    canvas ${parent}${widget}
    
    APSParseADLFile -filename $filename \
      -uniqueID $uniqueID \
      -canvas ${parent}${widget} \
      -macro $macro

    set tclvar ""
    set iocvar ""
    foreach w "label entry bar indicator scale button radiobutton composite" \
      ws "labels entries bars indicators scales buttons radiobuttons composites" {
        for {set index 0} {$index < $apsADLInfo(${uniqueID}.${ws})} {incr index} {
            append tclvar "apsADLInfo(${uniqueID}.${w}${index}.value) "
            append iocvar "$apsADLInfo(${uniqueID}.${w}${index}.chan) "
            set apsADLInfo(${uniqueID}.${w}${index}.state) "__"
        }
    }
    foreach w "text rectangle polygon oval polyline arc image" \
      ws "texts rectangles polygons ovals polylines arcs images" {
        for {set index 0} {$index < $apsADLInfo(${uniqueID}.${ws})} {incr index} {
            append tclvar "apsADLInfo(${uniqueID}.${w}${index}.value) "
            append iocvar "$apsADLInfo(${uniqueID}.${w}${index}.chan) "
            set apsADLInfo(${uniqueID}.${w}${index}.state) "__"
            if {[llength $apsADLInfo(${uniqueID}.${w}${index}.chanB)]} {
                append tclvar "apsADLInfo(${uniqueID}.${w}${index}.valueB) "
                append iocvar "$apsADLInfo(${uniqueID}.${w}${index}.chanB) "
                set apsADLInfo(${uniqueID}.${w}${index}.stateB) "__"
            }
            if {[llength $apsADLInfo(${uniqueID}.${w}${index}.chanC)]} {
                append tclvar "apsADLInfo(${uniqueID}.${w}${index}.valueC) "
                append iocvar "$apsADLInfo(${uniqueID}.${w}${index}.chanC) "
                set apsADLInfo(${uniqueID}.${w}${index}.stateC) "__"
            }
            if {[llength $apsADLInfo(${uniqueID}.${w}${index}.chanD)]} {
                append tclvar "apsADLInfo(${uniqueID}.${w}${index}.valueD) "
                append iocvar "$apsADLInfo(${uniqueID}.${w}${index}.chanD) "
                set apsADLInfo(${uniqueID}.${w}${index}.stateD) "__"
            }
        }
    }
    for {set index 0} {$index < $apsADLInfo(${uniqueID}.plots)} {incr index} {
        for {set i 0} {$i < $apsADLInfo(${uniqueID}.plot${index}.lines)} {incr i} {
            append tclvar "apsADLInfo(${uniqueID}.plot${index}.${i}.xvalue) "
            append tclvar "apsADLInfo(${uniqueID}.plot${index}.${i}.yvalue) "
            append iocvar "$apsADLInfo(${uniqueID}.plot${index}.${i}.xchan) "
            append iocvar "$apsADLInfo(${uniqueID}.plot${index}.${i}.ychan) "
            set apsADLInfo(${uniqueID}.plot${index}.${i}.xstate) "__"
            set apsADLInfo(${uniqueID}.plot${index}.${i}.ystate) "__"
        }
    }
    for {set index 0} {$index < $apsADLInfo(${uniqueID}.stripcharts)} {incr index} {
        for {set i 0} {$i < $apsADLInfo(${uniqueID}.stripchart${index}.lines)} {incr i} {
            append tclvar "apsADLInfo(${uniqueID}.stripchart${index}.${i}.yvalue) "
            append iocvar "$apsADLInfo(${uniqueID}.stripchart${index}.${i}.ychan) "
            set apsADLInfo(${uniqueID}.stripchart${index}.${i}.ystate) "__"
        }
    }
    if {[llength tclvar]} {
        pv link $tclvar $iocvar
    }
    pack ${parent}${widget} -fill both -expand true

    after 50 APSInitiateADLLinks -uniqueID $uniqueID
    after 100 APSMonitorADLLinks -uniqueID $uniqueID
}

proc APSInitiateADLLinks {args} {
    APSStrictParseArguments {uniqueID}
    global apsADLInfo
    if {$apsADLInfo(${uniqueID}.exists) == 0} { return }
    set notconnected 0
    foreach w "label entry" \
      ws "labels entries" {
          if {$apsADLInfo(${uniqueID}.${ws})} {
              set tclvar ""
              set indexes ""
              for {set index 0} {$index < $apsADLInfo(${uniqueID}.${ws})} {incr index} {
                  if {$apsADLInfo(${uniqueID}.${w}${index}.state) != "OK"} {
                      append tclvar "apsADLInfo(${uniqueID}.${w}${index}.value) "
                      append indexes "$index "
                  }
              }
              if {[llength $tclvar]} {
                  set states [pv info $tclvar "state precision size severity"]
                  foreach state $states index $indexes {
                      if {[lindex $state 1] == "OK"} {
                          set apsADLInfo(${uniqueID}.${w}${index}.state) "OK"
                          set precision [lindex $state 2]
                          set size [lindex $state 3]
                          set alarm [lindex $state 4]
                          set apsADLInfo(${uniqueID}.${w}${index}.precision) $precision
                          set apsADLInfo(${uniqueID}.${w}${index}.size) $size
                          if {$precision != "__"} {
                              set apsADLInfo(${uniqueID}.${w}${index}.formatedvalue) [format %.${precision}f $apsADLInfo(${uniqueID}.${w}${index}.value)]
                          } else {
                              if {$size > 1} {
                                  set apsADLInfo(${uniqueID}.${w}${index}.formatedvalue) ""
                                  foreach c $apsADLInfo(${uniqueID}.${w}${index}.value) {
                                      if {$c == 0} {break}
                                      append apsADLInfo(${uniqueID}.${w}${index}.formatedvalue) [format %c $c]
                                  }
                              } else {
                                  set apsADLInfo(${uniqueID}.${w}${index}.formatedvalue) $apsADLInfo(${uniqueID}.${w}${index}.value)
                              }
                          }
                          if {($apsADLInfo(${uniqueID}.${w}${index}.clrmod) == "alarm") || ($apsADLInfo(${uniqueID}.${w}${index}.clrmod) == "discrete")} {
                              if {$alarm == "MAJOR"} {
                                  $apsADLInfo(${uniqueID}.canvas).label${index}.label configure -fg red
                              } elseif {$alarm == "MINOR"} {
                                  $apsADLInfo(${uniqueID}.canvas).label${index}.label configure -fg yellow
                              } elseif {$alarm == "INVALID"} {
                                  $apsADLInfo(${uniqueID}.canvas).label${index}.label configure -fg white
                              } else {
                                  $apsADLInfo(${uniqueID}.canvas).label${index}.label configure -fg \#00d800
                              }
                          }
                          pv mon apsADLInfo(${uniqueID}.${w}${index}.value)
                      } else {
                          set notconnected 1
                      }
                  }
              }
          }
      }
    foreach w "text rectangle polygon oval polyline arc image" \
      ws "texts rectangles polygons ovals polylines arcs images" {
          if {$apsADLInfo(${uniqueID}.${ws})} {
              set tclvar ""
              set indexes ""
              for {set index 0} {$index < $apsADLInfo(${uniqueID}.${ws})} {incr index} {
                  if {$apsADLInfo(${uniqueID}.${w}${index}.state) != "OK"} {
                      append tclvar "apsADLInfo(${uniqueID}.${w}${index}.value) "
                      append indexes "$index "
                  }
              }
              if {[llength $tclvar]} {
                  set states [pv info $tclvar "state choices severity"]
                  foreach state $states index $indexes {
                      if {[lindex $state 1] == "OK"} {
                          set breakout 0
                          foreach alpha "B C D" {
                              if {[llength $apsADLInfo(${uniqueID}.${w}${index}.chan${alpha})]} {
                                  set state${alpha} [lindex [pv info apsADLInfo(${uniqueID}.${w}${index}.value${alpha}) "state choices"] 0]
                                  if {[lindex [set state${alpha}] 1] != "OK"} {
                                      set notconnected 1
                                      set value${alpha} 0
                                      set breakout 1
                                      break
                                  }
                                  set apsADLInfo(${uniqueID}.${w}${index}.state${alpha}) "OK"
                                  set apsADLInfo(${uniqueID}.${w}${index}.enum${alpha}) [lindex [set state${alpha}] 2]
                                  set enum${alpha} [lindex [set state${alpha}] 2]
                                  if {[set enum${alpha}] == "__"} {
                                      set value${alpha} $apsADLInfo(${uniqueID}.${w}${index}.value${alpha})
                                  } else {
                                      set nelem${alpha} [llength [set enum${alpha}]]
                                      set value${alpha} 0
                                      for {set j 0} {$j < [set nelem${alpha}]} {incr j} {
                                          if {$apsADLInfo(${uniqueID}.${w}${index}.value${alpha}) == [lindex [set enum${alpha}] $j]} {
                                              set value${alpha} $j
                                              break
                                          }
                                      }
                                  }
                              }
                          }
                          if {$breakout} { break }
                          set apsADLInfo(${uniqueID}.${w}${index}.state) "OK"
                          set apsADLInfo(${uniqueID}.${w}${index}.formatedvalue) $apsADLInfo(${uniqueID}.${w}${index}.value)
                          set apsADLInfo(${uniqueID}.${w}${index}.enum) [lindex $state 2]
                          set enum [lindex $state 2]
                          if {$enum == "__"} {
                              set value $apsADLInfo(${uniqueID}.${w}${index}.formatedvalue)
                          } else {
                              set nelem [llength $enum]
                              set value 0
                              for {set j 0} {$j < $nelem} {incr j} {
                                  if {$apsADLInfo(${uniqueID}.${w}${index}.formatedvalue) == [lindex $enum $j]} {
                                      set value $j
                                      break
                                  }
                              }
                          }
                          set alarm [lindex $state 3]
                          set apsADLInfo(${uniqueID}.${w}${index}.alarm) $alarm
                          if {($apsADLInfo(${uniqueID}.${w}${index}.clr) == "alarm") || ($apsADLInfo(${uniqueID}.${w}${index}.clr) == "discrete")} {
                              if {($w == "text") || ($w == "polyline")} {
                                  if {$alarm == "MAJOR"} {
                                      $apsADLInfo(${uniqueID}.canvas) itemconfigure ${w}$index -fill red
                                  } elseif {$alarm == "MINOR"} {
                                      $apsADLInfo(${uniqueID}.canvas) itemconfigure ${w}$index -fill yellow
                                  } elseif {$alarm == "INVALID"} {
                                      $apsADLInfo(${uniqueID}.canvas) itemconfigure ${w}$index -fill white
                                  } else {
                                      $apsADLInfo(${uniqueID}.canvas) itemconfigure ${w}$index -fill \#00d800
                                  }
                              } elseif {$apsADLInfo(${uniqueID}.${w}${index}.fill) == "outline"} {
                                  if {$alarm == "MAJOR"} {
                                      $apsADLInfo(${uniqueID}.canvas) itemconfigure ${w}$index -outline red
                                  } elseif {$alarm == "MINOR"} {
                                      $apsADLInfo(${uniqueID}.canvas) itemconfigure ${w}$index -outline yellow
                                  } elseif {$alarm == "INVALID"} {
                                      $apsADLInfo(${uniqueID}.canvas) itemconfigure ${w}$index -outline white
                                  } else {
                                      $apsADLInfo(${uniqueID}.canvas) itemconfigure ${w}$index -outline \#00d800
                                  }
                              } else {
                                  if {$alarm == "MAJOR"} {
                                      $apsADLInfo(${uniqueID}.canvas) itemconfigure ${w}$index -fill red -outline red
                                  } elseif {$alarm == "MINOR"} {
                                      $apsADLInfo(${uniqueID}.canvas) itemconfigure ${w}$index -fill yellow -outline yellow
                                  } elseif {$alarm == "INVALID"} {
                                      $apsADLInfo(${uniqueID}.canvas) itemconfigure ${w}$index -fill yellow -outline white
                                  } else {
                                      $apsADLInfo(${uniqueID}.canvas) itemconfigure ${w}$index -fill \#00d800 -outline \#00d800
                                  }
                              }
                          }
                          if {$apsADLInfo(${uniqueID}.${w}${index}.vis) == "if not zero"} {
                              if {$value != 0} {
                                  $apsADLInfo(${uniqueID}.canvas) itemconfigure ${w}$index -state normal
                              } else {
                                  $apsADLInfo(${uniqueID}.canvas) itemconfigure ${w}$index -state hidden
                              }
                          } elseif {$apsADLInfo(${uniqueID}.${w}${index}.vis) == "if zero"} {
                              if {$value == 0} {
                                  $apsADLInfo(${uniqueID}.canvas) itemconfigure ${w}$index -state normal
                              } else {
                                  $apsADLInfo(${uniqueID}.canvas) itemconfigure ${w}$index -state hidden
                              }
                          } elseif {$apsADLInfo(${uniqueID}.${w}${index}.vis) == "calc"} {
                              set function $apsADLInfo(${uniqueID}.${w}${index}.calc)
                              #set function [lindex [split $apsADLInfo(${uniqueID}.${w}${index}.calc) "="] 0]
                              #set answer [lindex [split $apsADLInfo(${uniqueID}.${w}${index}.calc) "="] 1]
                              set equation ""
                              foreach item [split $function ""] {
                                  if {$item == "A"} {
                                      append equation "$value "
                                  } elseif {$item == "B"} {
                                      append equation "$valueB "
                                  } elseif {$item == "C"} {
                                      append equation "$valueC "
                                  } elseif {$item == "D"} {
                                      append equation "$valueD "
                                  } elseif {$item == "\#"} {
                                      append equation " != "
                                  } elseif {$item == "="} {
                                      append equation " == "
                                  } else {
                                      append equation "$item"
                                  }
                              }
                              if {[expr $equation]} {
                                  $apsADLInfo(${uniqueID}.canvas) itemconfigure ${w}$index -state normal
                              } else {
                                  $apsADLInfo(${uniqueID}.canvas) itemconfigure ${w}$index -state hidden
                              }
                          } else {
                              $apsADLInfo(${uniqueID}.canvas) itemconfigure ${w}$index -state normal
                          }
                          pv mon apsADLInfo(${uniqueID}.${w}${index}.value)
                          if {[llength $apsADLInfo(${uniqueID}.${w}${index}.chanB)]} {
                              pv mon apsADLInfo(${uniqueID}.${w}${index}.valueB)
                          }
                          if {[llength $apsADLInfo(${uniqueID}.${w}${index}.chanC)]} {
                              pv mon apsADLInfo(${uniqueID}.${w}${index}.valueC)
                          }
                          if {[llength $apsADLInfo(${uniqueID}.${w}${index}.chanD)]} {
                              pv mon apsADLInfo(${uniqueID}.${w}${index}.valueD)
                          }
                      } else {
                          set notconnected 1
                      }
                  }
              }
          }
      }
    if {$apsADLInfo(${uniqueID}.bars)} {
        set tclvar ""
        set indexes ""
        for {set index 0} {$index < $apsADLInfo(${uniqueID}.bars)} {incr index} {
            if {$apsADLInfo(${uniqueID}.bar${index}.state) != "OK"} {
                append tclvar "apsADLInfo(${uniqueID}.bar${index}.value) "
                append indexes "$index "
            }
        }
        if {[llength $tclvar]} {
            set states [pv info $tclvar "state lopr hopr"]
            foreach state $states index $indexes {
                if {[lindex $state 1] == "OK"} {
                    set apsADLInfo(${uniqueID}.bar${index}.state) "OK"
                    set apsADLInfo(${uniqueID}.bar${index}.formatedvalue) $apsADLInfo(${uniqueID}.bar${index}.value)
                    APSBarAdjust $apsADLInfo(${uniqueID}.canvas).bar$index -value $apsADLInfo(${uniqueID}.bar${index}.value) -min [lindex $state 2] -max [lindex $state 3]
                    pv mon apsADLInfo(${uniqueID}.bar${index}.value)
                } else {
                    set notconnected 1
                }
            }
        }
    }
    if {$apsADLInfo(${uniqueID}.indicators)} {
        set tclvar ""
        set indexes ""
        for {set index 0} {$index < $apsADLInfo(${uniqueID}.indicators)} {incr index} {
            if {$apsADLInfo(${uniqueID}.indicator${index}.state) != "OK"} {
                append tclvar "apsADLInfo(${uniqueID}.indicator${index}.value) "
                append indexes "$index "
            }
        }
        if {[llength $tclvar]} {
            set states [pv info $tclvar "state lopr hopr"]
            foreach state $states index $indexes {
                if {[lindex $state 1] == "OK"} {
                    set apsADLInfo(${uniqueID}.indicator${index}.state) "OK"
                    set apsADLInfo(${uniqueID}.indicator${index}.formatedvalue) $apsADLInfo(${uniqueID}.indicator${index}.value)
                    APSBarAdjust $apsADLInfo(${uniqueID}.canvas).indicator$index -value $apsADLInfo(${uniqueID}.indicator${index}.value) -min [lindex $state 2] -max [lindex $state 3]
                    pv mon apsADLInfo(${uniqueID}.indicator${index}.value)
                } else {
                    set notconnected 1
                }
            }
        }
    }
    if {$apsADLInfo(${uniqueID}.scales)} {
        set tclvar ""
        set indexes ""
        for {set index 0} {$index < $apsADLInfo(${uniqueID}.scales)} {incr index} {
            if {$apsADLInfo(${uniqueID}.scale${index}.state) != "OK"} {
                append tclvar "apsADLInfo(${uniqueID}.scale${index}.value) "
                append indexes "$index "
            }
        }
        if {[llength $tclvar]} {
            set states [pv info $tclvar "state lopr hopr precision"]
            foreach state $states index $indexes {
                if {[lindex $state 1] == "OK"} {
                    set apsADLInfo(${uniqueID}.scale${index}.state) "OK"
                    set precision [lindex $state 4]
                    set apsADLInfo(${uniqueID}.scale${index}.precision) $precision
                    $apsADLInfo(${uniqueID}.canvas).scale$index.scale configure -from [lindex $state 2] -to [lindex $state 3]
                    if {$precision != "__"} {
                        set apsADLInfo(${uniqueID}.scale${index}.formatedvalue) [format %.${precision}f $apsADLInfo(${uniqueID}.scale${index}.value)]
                    } else {
                        set apsADLInfo(${uniqueID}.scale${index}.formatedvalue) $apsADLInfo(${uniqueID}.scale${index}.value)
                    }
                    pv umon apsADLInfo(${uniqueID}.scale${index}.value) "APSADLEventHandler -type scale -uniqueID $uniqueID -index $index"
                } else {
                    set notconnected 1
                }
            }
        }
    }
    if {$apsADLInfo(${uniqueID}.radiobuttons)} {
        set tclvar ""
        set indexes ""
        for {set index 0} {$index < $apsADLInfo(${uniqueID}.radiobuttons)} {incr index} {
            if {$apsADLInfo(${uniqueID}.radiobutton${index}.state) != "OK"} {
                append tclvar "apsADLInfo(${uniqueID}.radiobutton${index}.value) "
                append indexes "$index "
            }
        }
        if {[llength $tclvar]} {
            set states [pv info $tclvar "state choices severity"]
            foreach state $states index $indexes {
                if {[lindex $state 1] == "OK"} {
                    set apsADLInfo(${uniqueID}.radiobutton${index}.state) "OK"
                    set apsADLInfo(${uniqueID}.radiobutton${index}.formatedvalue) $apsADLInfo(${uniqueID}.radiobutton${index}.value)

                    set enum [lindex $state 2]
                    set alarm [lindex $state 3]
                    set nenum [llength $enum]
                    set x $apsADLInfo(${uniqueID}.radiobutton${index}.x)
                    set y $apsADLInfo(${uniqueID}.radiobutton${index}.y)
                    if {$apsADLInfo(${uniqueID}.radiobutton${index}.stacking) == "column"} {
                        set width [expr {round ($apsADLInfo(${uniqueID}.radiobutton${index}.width) / ($nenum * 1.0))}]
                        set height $apsADLInfo(${uniqueID}.radiobutton${index}.height)
                    } else {
                        set width $apsADLInfo(${uniqueID}.radiobutton${index}.width)
                        set height [expr {round ($apsADLInfo(${uniqueID}.radiobutton${index}.height) / ($nenum * 1.0))}]
                    }
                    for {set j 0} {$j < $nenum} {incr j} {
                        radiobutton $apsADLInfo(${uniqueID}.canvas).radiobutton${index}_$j \
                          -bg \#[lindex $apsADLInfo(${uniqueID}.colors) $apsADLInfo(${uniqueID}.radiobutton${index}.bclr)] \
                          -fg \#[lindex $apsADLInfo(${uniqueID}.colors) $apsADLInfo(${uniqueID}.radiobutton${index}.clr)] \
                          -highlightbackground \#[lindex $apsADLInfo(${uniqueID}.colors) $apsADLInfo(${uniqueID}.radiobutton${index}.clr)] \
                          -selectcolor \#[lindex $apsADLInfo(${uniqueID}.colors) $apsADLInfo(${uniqueID}.radiobutton${index}.bclr)] \
                          -text [lindex $enum $j] \
                          -indicatoron no \
                          -variable apsADLInfo(${uniqueID}.radiobutton${index}.formatedvalue) \
                          -value [lindex $enum $j] \
                          -font [APSSelectADLFont -height $height -type button] \
                          -command "APSSetADLValue -type radiobutton -uniqueID $uniqueID -index $index"
                        place $apsADLInfo(${uniqueID}.canvas).radiobutton${index}_$j \
                          -x $x \
                          -y $y \
                          -width $width \
                          -height $height
                        if {$apsADLInfo(${uniqueID}.radiobutton${index}.stacking) == "column"} {
                            incr x $width
                        } else {
                            incr y $height
                        }
                    }
                    if {($apsADLInfo(${uniqueID}.radiobutton${index}.clrmod) == "alarm") || ($apsADLInfo(${uniqueID}.radiobutton${index}.clrmod) == "discrete")} {
                        for {set j 0} {$j < $nenum} {incr j} {
                            if {$alarm == "MAJOR"} {
                                $apsADLInfo(${uniqueID}.canvas).radiobutton${index}_$j configure -fg red
                            } elseif {$alarm == "MINOR"} {
                                $apsADLInfo(${uniqueID}.canvas).radiobutton${index}_$j configure -fg yellow
                            } elseif {$alarm == "INVALID"} {
                                $apsADLInfo(${uniqueID}.canvas).radiobutton${index}_$j configure -fg white
                            } else {
                                $apsADLInfo(${uniqueID}.canvas).radiobutton${index}_$j configure -fg \#00d800
                            }
                        }
                    }

                    pv umon apsADLInfo(${uniqueID}.radiobutton${index}.value) "APSADLEventHandler -type radiobutton -uniqueID $uniqueID -index $index"
                } else {
                    set notconnected 1
                }
            }
        }
    }
    if {$apsADLInfo(${uniqueID}.plots)} {
        set tclxvar ""
        set tclyvar ""
        set xindexes ""
        set yindexes ""
        set xlines ""
        set ylines ""
        for {set index 0} {$index < $apsADLInfo(${uniqueID}.plots)} {incr index} {
            for {set i 0} {$i < $apsADLInfo(${uniqueID}.plot${index}.lines)} {incr i} {
                if {$apsADLInfo(${uniqueID}.plot${index}.${i}.xstate) != "OK"} {
                    append tclxvar "apsADLInfo(${uniqueID}.plot${index}.${i}.xvalue) "
                    append xindexes "$index "
                    append xlines "$i "
                }
                if {$apsADLInfo(${uniqueID}.plot${index}.${i}.ystate) != "OK"} {
                    append tclyvar "apsADLInfo(${uniqueID}.plot${index}.${i}.yvalue) "
                    append yindexes "$index "
                    append ylines "$i "
                }
            }
        }
        if {[llength $tclxvar]} {
            set states [pv info $tclxvar "state"]
            foreach state $states index $xindexes line $xlines {
                if {[lindex $state 1] == "OK"} {
                    set apsADLInfo(${uniqueID}.plot${index}.${line}.xstate) "OK"
                    pv mon apsADLInfo(${uniqueID}.plot${index}.${line}.xvalue)
                } else {
                    set notconnected 1
                }
            }
        }
        if {[llength $tclyvar]} {
            set states [pv info $tclyvar "state"]
            foreach state $states index $yindexes line $ylines {
                if {[lindex $state 1] == "OK"} {
                    set apsADLInfo(${uniqueID}.plot${index}.${line}.ystate) "OK"
                    pv mon apsADLInfo(${uniqueID}.plot${index}.${line}.yvalue)
                } else {
                    set notconnected 1
                }
            }
        }
    }
    if {$apsADLInfo(${uniqueID}.stripcharts)} {
        set tclyvar ""
        set yindexes ""
        set ylines ""
        for {set index 0} {$index < $apsADLInfo(${uniqueID}.stripcharts)} {incr index} {
            for {set i 0} {$i < $apsADLInfo(${uniqueID}.stripchart${index}.lines)} {incr i} {
                if {$apsADLInfo(${uniqueID}.stripchart${index}.${i}.ystate) != "OK"} {
                    append tclyvar "apsADLInfo(${uniqueID}.stripchart${index}.${i}.yvalue) "
                    append yindexes "$index "
                    append ylines "$i "
                }
            }
        }
        if {[llength $tclyvar]} {
            set states [pv info $tclyvar "state"]
            foreach state $states index $yindexes line $ylines {
                if {[lindex $state 1] == "OK"} {
                    set apsADLInfo(${uniqueID}.stripchart${index}.${line}.ystate) "OK"
                    pv mon apsADLInfo(${uniqueID}.stripchart${index}.${line}.yvalue)
                } else {
                    set notconnected 1
                }
            }
        }
    }
    if {$apsADLInfo(${uniqueID}.composites)} {
        set tclvar ""
        set indexes ""
        for {set index 0} {$index < $apsADLInfo(${uniqueID}.composites)} {incr index} {
            if {$apsADLInfo(${uniqueID}.composite${index}.state) != "OK"} {
                append tclvar "apsADLInfo(${uniqueID}.composite${index}.value) "
                append indexes "$index "
            }
        }
        if {[llength $tclvar]} {
            set states [pv info $tclvar "state choices"]
            foreach state $states index $indexes {
                if {[lindex $state 1] == "OK"} {
                    set apsADLInfo(${uniqueID}.composite${index}.state) "OK"
                    set apsADLInfo(${uniqueID}.composite${index}.formatedvalue) $apsADLInfo(${uniqueID}.composite${index}.value)
                    set apsADLInfo(${uniqueID}.composite${index}.enum) [lindex $state 2]
                    set enum [lindex $state 2]
                    if {$enum == "__"} {
                        set value $apsADLInfo(${uniqueID}.composite${index}.formatedvalue)
                    } else {
                        set nelem [llength $enum]
                        set value 0
                        for {set j 0} {$j < $nelem} {incr j} {
                            if {$apsADLInfo(${uniqueID}.composite${index}.formatedvalue) == [lindex $enum $j]} {
                                set value $j
                                break
                            }
                        }
                    }
                    if {$apsADLInfo(${uniqueID}.composite${index}.vis) == "if not zero"} {
                        if {$value != 0} {
                            place forget $apsADLInfo(${uniqueID}.canvas).composite$index
                        } else {
                            place $apsADLInfo(${uniqueID}.canvas).composite$index \
                              -x $apsADLInfo(${uniqueID}.composite${index}.x) \
                              -y $apsADLInfo(${uniqueID}.composite${index}.y) \
                              -width $apsADLInfo(${uniqueID}.composite${index}.width) \
                              -height $apsADLInfo(${uniqueID}.composite${index}.height)
                        }
                    }
                    pv mon apsADLInfo(${uniqueID}.composite${index}.value)
                } else {
                    set notconnected 1
                }
            }
        }
    }
    if {$notconnected} {
        after 100 APSInitiateADLLinks -uniqueID $uniqueID
    }
}

proc APSMonitorADLLinks {args} {
    APSStrictParseArguments {uniqueID}
    global apsADLInfo
    if {$apsADLInfo(${uniqueID}.exists) == 0} { return }

    if {$apsADLInfo(${uniqueID}.labels)} {
        for {set index 0} {$index < $apsADLInfo(${uniqueID}.labels)} {incr index} {
            if {$apsADLInfo(${uniqueID}.label${index}.state) == "OK"} {
                set precision $apsADLInfo(${uniqueID}.label${index}.precision)
                set size $apsADLInfo(${uniqueID}.label${index}.size)
                if {$precision != "__"} {
                    set apsADLInfo(${uniqueID}.label${index}.formatedvalue) [format %.${precision}f $apsADLInfo(${uniqueID}.label${index}.value)]
                } else {
                    if {$size > 1} {
                        set apsADLInfo(${uniqueID}.label${index}.formatedvalue) ""
                        foreach c $apsADLInfo(${uniqueID}.label${index}.value) {
                            if {$c == 0} {break}
                            append apsADLInfo(${uniqueID}.label${index}.formatedvalue) [format %c $c]
                        }
                    } else {
                        set apsADLInfo(${uniqueID}.label${index}.formatedvalue) $apsADLInfo(${uniqueID}.label${index}.value)
                    }
                }
            }
        }
    }
    if {$apsADLInfo(${uniqueID}.bars)} {
        for {set index 0} {$index < $apsADLInfo(${uniqueID}.bars)} {incr index} {
            if {$apsADLInfo(${uniqueID}.bar${index}.state) == "OK"} {
                if {$apsADLInfo(${uniqueID}.bar${index}.value) != $apsADLInfo(${uniqueID}.bar${index}.formatedvalue)} {
                    set apsADLInfo(${uniqueID}.bar${index}.formatedvalue) $apsADLInfo(${uniqueID}.bar${index}.value)
                    APSBarAdjust $apsADLInfo(${uniqueID}.canvas).bar$index -value $apsADLInfo(${uniqueID}.bar${index}.value)
                }
            }
        }
    }
    if {$apsADLInfo(${uniqueID}.indicators)} {
        for {set index 0} {$index < $apsADLInfo(${uniqueID}.indicators)} {incr index} {
            if {$apsADLInfo(${uniqueID}.indicator${index}.state) == "OK"} {
                if {$apsADLInfo(${uniqueID}.indicator${index}.value) != $apsADLInfo(${uniqueID}.indicator${index}.formatedvalue)} {
                    set apsADLInfo(${uniqueID}.indicator${index}.formatedvalue) $apsADLInfo(${uniqueID}.indicator${index}.value)
                    APSBarAdjust $apsADLInfo(${uniqueID}.canvas).indicator$index -value $apsADLInfo(${uniqueID}.indicator${index}.value)
                }
            }
        }
    }
    if {$apsADLInfo(${uniqueID}.plots)} {
        for {set index 0} {$index < $apsADLInfo(${uniqueID}.plots)} {incr index} {
            for {set j 0} {$j < $apsADLInfo(${uniqueID}.plot${index}.lines)} {incr j} {
                if {$apsADLInfo(${uniqueID}.plot${index}.${j}.ystate) == "OK"} {
                    if {$apsADLInfo(${uniqueID}.plot${index}.${j}.formatedxvalue) != $apsADLInfo(${uniqueID}.plot${index}.${j}.xvalue)} {
                        set apsADLInfo(${uniqueID}.plot${index}.${j}.formatedxvalue) $apsADLInfo(${uniqueID}.plot${index}.${j}.xvalue)
                    }
                    if {$apsADLInfo(${uniqueID}.plot${index}.${j}.formatedyvalue) != $apsADLInfo(${uniqueID}.plot${index}.${j}.yvalue)} {
                        set apsADLInfo(${uniqueID}.plot${index}.${j}.formatedyvalue) $apsADLInfo(${uniqueID}.plot${index}.${j}.yvalue)
                    }
                }
            }
        }
    }
    if {$apsADLInfo(${uniqueID}.stripcharts)} {
        for {set index 0} {$index < $apsADLInfo(${uniqueID}.stripcharts)} {incr index} {
            for {set j 0} {$j < $apsADLInfo(${uniqueID}.stripchart${index}.lines)} {incr j} {
                if {$apsADLInfo(${uniqueID}.stripchart${index}.${j}.ystate) == "OK"} {
                    if {$apsADLInfo(${uniqueID}.stripchart${index}.${j}.formatedyvalue) != $apsADLInfo(${uniqueID}.stripchart${index}.${j}.yvalue)} {
                        set apsADLInfo(${uniqueID}.stripchart${index}.${j}.formatedyvalue) $apsADLInfo(${uniqueID}.stripchart${index}.${j}.yvalue)
                    }
                }
            }
        }
    }
    foreach w "text rectangle polygon oval polyline arc image" \
      ws "texts rectangles polygons ovals polylines arcs images" {
          if {$apsADLInfo(${uniqueID}.${ws})} {
              for {set index 0} {$index < $apsADLInfo(${uniqueID}.${ws})} {incr index} {
                  if {$apsADLInfo(${uniqueID}.${w}${index}.state) == "OK"} {
                      set alarm [lindex [lindex [pv info apsADLInfo(${uniqueID}.${w}${index}.value) "severity"] 0] 1]
                      if {($apsADLInfo(${uniqueID}.${w}${index}.formatedvalue) != $apsADLInfo(${uniqueID}.${w}${index}.value)) ||
                          ($apsADLInfo(${uniqueID}.${w}${index}.alarm) != $alarm)} {
                          set apsADLInfo(${uniqueID}.${w}${index}.formatedvalue) $apsADLInfo(${uniqueID}.${w}${index}.value)
                          set apsADLInfo(${uniqueID}.${w}${index}.alarm) $alarm
                          set enum $apsADLInfo(${uniqueID}.${w}${index}.enum)
                          if {$enum == "__"} {
                              set value $apsADLInfo(${uniqueID}.${w}${index}.formatedvalue)
                          } else {
                              set nelem [llength $enum]
                              set value 0
                              for {set j 0} {$j < $nelem} {incr j} {
                                  if {$apsADLInfo(${uniqueID}.${w}${index}.formatedvalue) == [lindex $enum $j]} {
                                      set value $j
                                      break
                                  }
                              }
                          }
                          if {($apsADLInfo(${uniqueID}.${w}${index}.clr) == "alarm") || ($apsADLInfo(${uniqueID}.${w}${index}.clr) == "discrete")} {
                              if {$w == "text"} {
                                  if {$alarm == "MAJOR"} {
                                      $apsADLInfo(${uniqueID}.canvas) itemconfigure ${w}$index -fill red
                                  } elseif {$alarm == "MINOR"} {
                                      $apsADLInfo(${uniqueID}.canvas) itemconfigure ${w}$index -fill yellow
                                  } elseif {$alarm == "INVALID"} {
                                      $apsADLInfo(${uniqueID}.canvas) itemconfigure ${w}$index -fill white
                                  } else {
                                      $apsADLInfo(${uniqueID}.canvas) itemconfigure ${w}$index -fill \#00d800
                                  }
                              } elseif {$apsADLInfo(${uniqueID}.${w}${index}.fill) == "outline"} {
                                  if {$alarm == "MAJOR"} {
                                      $apsADLInfo(${uniqueID}.canvas) itemconfigure ${w}$index -outline red
                                  } elseif {$alarm == "MINOR"} {
                                      $apsADLInfo(${uniqueID}.canvas) itemconfigure ${w}$index -outline yellow
                                  } elseif {$alarm == "INVALID"} {
                                      $apsADLInfo(${uniqueID}.canvas) itemconfigure ${w}$index -outline white
                                  } else {
                                      $apsADLInfo(${uniqueID}.canvas) itemconfigure ${w}$index -outline \#00d800
                                  }
                              } else {
                                  if {$alarm == "MAJOR"} {
                                      $apsADLInfo(${uniqueID}.canvas) itemconfigure ${w}$index -fill red -outline red
                                  } elseif {$alarm == "MINOR"} {
                                      $apsADLInfo(${uniqueID}.canvas) itemconfigure ${w}$index -fill yellow -outline yellow
                                  } elseif {$alarm == "INVALID"} {
                                      $apsADLInfo(${uniqueID}.canvas) itemconfigure ${w}$index -fill yellow -outline white
                                  } else {
                                      $apsADLInfo(${uniqueID}.canvas) itemconfigure ${w}$index -fill \#00d800 -outline \#00d800
                                  }
                              }
                          }
                          if {$apsADLInfo(${uniqueID}.${w}${index}.vis) == "if not zero"} {
                              if {$value != 0} {
                                  $apsADLInfo(${uniqueID}.canvas) itemconfigure ${w}$index -state normal
                              } else {
                                  $apsADLInfo(${uniqueID}.canvas) itemconfigure ${w}$index -state hidden
                              }
                          } elseif {$apsADLInfo(${uniqueID}.${w}${index}.vis) == "if zero"} {
                              if {$value == 0} {
                                  $apsADLInfo(${uniqueID}.canvas) itemconfigure ${w}$index -state normal
                              } else {
                                  $apsADLInfo(${uniqueID}.canvas) itemconfigure ${w}$index -state hidden
                              }
                          } elseif {$apsADLInfo(${uniqueID}.${w}${index}.vis) == "calc"} {
                              set fuction $apsADLInfo(${uniqueID}.${w}${index}.calc)
                              #set function [lindex [split $apsADLInfo(${uniqueID}.${w}${index}.calc) "="] 0]
                              #set answer [lindex [split $apsADLInfo(${uniqueID}.${w}${index}.calc) "="] 1]
                              set equation ""
                              foreach item [split $function ""] {
                                  if {$item == "A"} {
                                      append equation "$value "
                                  } elseif {$item == "B"} {
                                      append equation "$apsADLInfo(${uniqueID}.${w}${index}.valueB) "
                                  } elseif {$item == "C"} {
                                      append equation "$apsADLInfo(${uniqueID}.${w}${index}.valueC) "
                                  } elseif {$item == "D"} {
                                      append equation "$apsADLInfo(${uniqueID}.${w}${index}.valueD) "
                                  } elseif {$item == "\#"} {
                                      append equation " != "
                                  } elseif {$item == "="} {
                                      append equation " == "
                                  } else {
                                      append equation "$item"
                                  }
                              }
                              if {[expr $equation]} {
                                  $apsADLInfo(${uniqueID}.canvas) itemconfigure ${w}$index -state normal
                              } else {
                                  $apsADLInfo(${uniqueID}.canvas) itemconfigure ${w}$index -state hidden
                              }
                          }
                      }
                  }
              }
          }
      }
    if {$apsADLInfo(${uniqueID}.composites)} {
        for {set index 0} {$index < $apsADLInfo(${uniqueID}.composites)} {incr index} {
            if {$apsADLInfo(${uniqueID}.composite${index}.state) == "OK"} {
                if {$apsADLInfo(${uniqueID}.composite${index}.formatedvalue) != $apsADLInfo(${uniqueID}.composite${index}.value)} {
                    set apsADLInfo(${uniqueID}.composite${index}.formatedvalue) $apsADLInfo(${uniqueID}.composite${index}.value)
                    set enum $apsADLInfo(${uniqueID}.composite${index}.enum)
                    if {$enum == "__"} {
                        set value $apsADLInfo(${uniqueID}.composite${index}.formatedvalue)
                    } else {
                        set nelem [llength $enum]
                        set value 0
                        for {set j 0} {$j < $nelem} {incr j} {
                            if {$apsADLInfo(${uniqueID}.composite${index}.formatedvalue) == [lindex $enum $j]} {
                                set value $j
                                break
                            }
                        }
                    }
                    if {$apsADLInfo(${uniqueID}.composite${index}.vis) == "if not zero"} {
                        if {$value != 0} {
                            place forget $apsADLInfo(${uniqueID}.canvas).composite$index
                        } else {
                            place $apsADLInfo(${uniqueID}.canvas).composite$index \
                              -x $apsADLInfo(${uniqueID}.composite${index}.x) \
                              -y $apsADLInfo(${uniqueID}.composite${index}.y) \
                              -width $apsADLInfo(${uniqueID}.composite${index}.width) \
                              -height $apsADLInfo(${uniqueID}.composite${index}.height)
                        }
                    }
                }
            }
        }
    }

    after 500 APSMonitorADLLinks -uniqueID $uniqueID
}

proc APSADLEventHandler {args} {
    APSStrictParseArguments {type uniqueID index}
    global apsADLInfo
    if {$apsADLInfo(${uniqueID}.exists) == 0} { return }

    if {$type == "entry"} {
        set precision $apsADLInfo(${uniqueID}.entry${index}.precision)
        if {$precision != "__"} {
            set apsADLInfo(${uniqueID}.entry${index}.formatedvalue) [format %.${precision}f $apsADLInfo(${uniqueID}.entry${index}.value)]
        } else {
            set apsADLInfo(${uniqueID}.entry${index}.formatedvalue) $apsADLInfo(${uniqueID}.entry${index}.value)
        }
        update
    }
    if {$type == "scale"} {
        set apsADLInfo(${uniqueID}.scale${index}.formatedvalue) $apsADLInfo(${uniqueID}.scale${index}.value)
    }
    if {$type == "radiobutton"} {
        set apsADLInfo(${uniqueID}.radiobutton${index}.formatedvalue) $apsADLInfo(${uniqueID}.radiobutton${index}.value)
    }
}

proc APSSetADLValue {args} {
    APSParseArguments {type uniqueID index}
    global apsADLInfo
    if {$type == "entry"} {
        if {$apsADLInfo(${uniqueID}.entry${index}.state) == "OK"} {
            set apsADLInfo(${uniqueID}.entry${index}.value) $apsADLInfo(${uniqueID}.entry${index}.formatedvalue)
            pv putq apsADLInfo(${uniqueID}.entry${index}.value)
        }
    }
    if {$type == "scale"} {
        if {$apsADLInfo(${uniqueID}.scale${index}.state) == "OK"} {
            set apsADLInfo(${uniqueID}.scale${index}.value) $apsADLInfo(${uniqueID}.scale${index}.formatedvalue)
            pv putq apsADLInfo(${uniqueID}.scale${index}.value)
        }
    }
    if {$type == "button"} {
        set apsADLInfo(${uniqueID}.button${index}.value) $apsADLInfo(${uniqueID}.button${index}.press_msg)
        pv putq apsADLInfo(${uniqueID}.button${index}.value)
    }
    if {$type == "radiobutton"} {
        set apsADLInfo(${uniqueID}.radiobutton${index}.value) $apsADLInfo(${uniqueID}.radiobutton${index}.formatedvalue)
        pv putq apsADLInfo(${uniqueID}.radiobutton${index}.value)
     #   apsADLInfo(${uniqueID}.radiobutton${index}.value)
    }
}

proc APSPrepADLElementFormat {data macro} {
    #Remove spaces after equal signs
    set i [string first "= " $data]
    while {$i != -1} {
        set data [string range $data 0 $i][string range $data [expr {$i + 2}] end]
        set i [string first "= " $data]
    }
    #Remove spaces before equal signs
    set i [string first " =" $data]
    while {$i != -1} {
        set data [string range $data 0 [expr {$i - 1}]][string range $data [expr {$i + 1}] end]
        set i [string first " =" $data]
    }
    #Add extra back slashes to existing back slashes
    set i [string first "\\" $data]
    while {$i != -1} {
        set data [string range $data 0 $i]\\\\[string range $data $i end]
        set i [string first "\\" $data [expr {$i + 4}]]
    }
    #Add back slashes to preserve spaces inside quotes
    set n 0
    for {set i 0} {$i < [string length $data]} {incr i} {
        if {$n == 0} {
            if {[string index $data $i] == "\""} {
                set n 1
            }
        } else {
            if {[string index $data $i] == "\""} {
                set n 0
            } elseif {[string index $data $i] == " "} {
                set data "[string range $data 0 [expr {$i - 1}]]\\\\\\[string range $data $i end]"
                incr i 3
            }
        }
    }
    #Remove quotes altogether
    for {set i 0} {$i < [string length $data]} {incr i} {
        if {[string index $data $i] == "\""} {
            set data "[string range $data 0 [expr {$i - 1}]][string range $data [expr {$i + 1}] end]"
            incr i -1
        }
    }
    
    if {[llength $macro]} {
        #Replace macro
        set macro [join [split [split $macro =] ,]] 
        set i [string first "\$(" $data]
        while {$i != -1} {
            set j [string first ")" [string range $data $i end]]
            set ele [string range $data [expr {$i + 2}] [expr {$i + $j - 1}]]
            foreach "name value" $macro {
                if {$name == $ele} {
                    set data [string range $data 0 [expr {$i - 1}]]${value}[string range $data [expr {$i + $j + 1}] end]
                    break
                }
            }
            set k [string first "\$(" $data]
            if {$i == $k} {
                puts "Missing macro for $ele"
                exit
            } else {
                set i $k
            }
        }
    }

    return $data
}

proc APSConvertADLElementFormat {data} {
    set tmp ""
    set title 0
    foreach ele $data {
        if {(!$title) && ([llength $ele] == 1)} {
            set ele [string trim $ele]
            set i [string first = $ele]
            if {($i != -1)} {
                lappend tmp "[string range $ele 0 [expr {$i - 1}]] [string range $ele [expr {$i + 1}] end]"
                set title 0
            } else {
                lappend tmp $ele
                set title 1
            }
        } else {
            lappend tmp [APSConvertADLElementFormat $ele]
            set title 0
        }
    }
    return $tmp
}

proc APSParseADLFileElement {data} {
    set file(name) ""
    set file(version) ""
    set len [llength $data]
    for {set j 0} {$j < $len} {incr j} {
        set ele [lindex $data $j]
        set file([lindex $ele 0]) [lindex $ele 1]
    }
    return [array get file]
}

proc APSParseADLDisplayElement {data} {
    set display(clr) 0
    set display(bclr) 0
    set display(cmap) ""
    set display(gridSpacing) 1
    set display(gridOn) 0
    set display(snapToGrid) 0
    set display(object.x) 0
    set display(object.y) 0
    set display(object.width) 200
    set display(object.height) 200
    set len [llength $data]
    for {set j 0} {$j < $len} {incr j} {
        set ele [lindex $data $j]
        if {$ele == "object"} {
            incr j
            set ele [lindex $data $j]
            set len2 [llength $ele]
            for {set k 0} {$k < $len2} {incr k} {
                set ele2 [lindex $ele $k]
                set display(object.[lindex $ele2 0]) [lindex $ele2 1]
            }
        } else {
            set display([lindex $ele 0]) [lindex $ele 1]
        }
    }
    return [array get display]
}

proc APSParseADLColormapElement {data} {
    set colormap(ncolors) 0
    set colormap(colors) ""
    set len [llength $data]
    for {set j 0} {$j < $len} {incr j} {
        set ele [lindex $data $j]
        if {$ele == "colors"} {
            incr j
            set ele [join [split [lindex $data $j] ,]]
            set colormap(colors) $ele
        } else {
            set colormap([lindex $ele 0]) [lindex $ele 1]
        }
    }
    return [array get colormap]
}

proc APSParseADLRectangleElement {data} {
    set rectangle(object.x) 0
    set rectangle(object.y) 0
    set rectangle(object.width) 0
    set rectangle(object.height) 0
    set rectangle(basicattribute.clr) 0
    set rectangle(basicattribute.style) ""
    set rectangle(basicattribute.fill) "solid"
    set rectangle(basicattribute.width) 0
    set rectangle(dynamicattribute.clr) "static"
    set rectangle(dynamicattribute.vis) "static"
    set rectangle(dynamicattribute.calc) ""
    set rectangle(dynamicattribute.chan) ""
    set rectangle(dynamicattribute.chanB) ""
    set rectangle(dynamicattribute.chanC) ""
    set rectangle(dynamicattribute.chanD) ""
    set len [llength $data]
    for {set j 0} {$j < $len} {incr j} {
        set ele [lindex $data $j]
        if {$ele == "object"} {
            incr j
            set ele [lindex $data $j]
            set len2 [llength $ele]
            for {set k 0} {$k < $len2} {incr k} {
                set ele2 [lindex $ele $k]
                set rectangle(object.[lindex $ele2 0]) [lindex $ele2 1]
            }
        } elseif {$ele == "basic\\ attribute"} {
            incr j
            set ele [lindex $data $j]
            set len2 [llength $ele]
            for {set k 0} {$k < $len2} {incr k} {
                
                set ele2 [lindex $ele $k]
                set rectangle(basicattribute.[lindex $ele2 0]) [lindex $ele2 1]
            }                      
        } elseif {$ele == "dynamic\\ attribute"} {
            incr j
            set ele [lindex $data $j]
            set len2 [llength $ele]
            for {set k 0} {$k < $len2} {incr k} {
                
                set ele2 [lindex $ele $k]
                set rectangle(dynamicattribute.[lindex $ele2 0]) [lindex $ele2 1]
            }                      
        }
    }
    return [array get rectangle]
}

proc APSParseADLOvalElement {data} {
    set oval(object.x) 0
    set oval(object.y) 0
    set oval(object.width) 0
    set oval(object.height) 0
    set oval(basicattribute.clr) 0
    set oval(basicattribute.style) ""
    set oval(basicattribute.fill) "solid"
    set oval(basicattribute.width) 0
    set oval(dynamicattribute.clr) "static"
    set oval(dynamicattribute.vis) "static"
    set oval(dynamicattribute.calc) ""
    set oval(dynamicattribute.chan) ""
    set oval(dynamicattribute.chanB) ""
    set oval(dynamicattribute.chanC) ""
    set oval(dynamicattribute.chanD) ""
    set len [llength $data]
    for {set j 0} {$j < $len} {incr j} {
        set ele [lindex $data $j]
        if {$ele == "object"} {
            incr j
            set ele [lindex $data $j]
            set len2 [llength $ele]
            for {set k 0} {$k < $len2} {incr k} {
                set ele2 [lindex $ele $k]
                set oval(object.[lindex $ele2 0]) [lindex $ele2 1]
            }
        } elseif {$ele == "basic\\ attribute"} {
            incr j
            set ele [lindex $data $j]
            set len2 [llength $ele]
            for {set k 0} {$k < $len2} {incr k} {
                
                set ele2 [lindex $ele $k]
                set oval(basicattribute.[lindex $ele2 0]) [lindex $ele2 1]
            }                      
        } elseif {$ele == "dynamic\\ attribute"} {
            incr j
            set ele [lindex $data $j]
            set len2 [llength $ele]
            for {set k 0} {$k < $len2} {incr k} {
                
                set ele2 [lindex $ele $k]
                set oval(dynamicattribute.[lindex $ele2 0]) [lindex $ele2 1]
            }                      
        }
    }
    return [array get oval]
}

proc APSParseADLPolylineElement {data} {
    set polyline(object.x) 0
    set polyline(object.y) 0
    set polyline(object.width) 0
    set polyline(object.height) 0
    set polyline(basicattribute.clr) 0
    set polyline(basicattribute.style) ""
    set polyline(basicattribute.width) 1
    set polyline(dynamicattribute.clr) "static"
    set polyline(dynamicattribute.vis) "static"
    set polyline(dynamicattribute.calc) ""
    set polyline(dynamicattribute.chan) ""
    set polyline(dynamicattribute.chanB) ""
    set polyline(dynamicattribute.chanC) ""
    set polyline(dynamicattribute.chanD) ""
    set polyline(points) ""
    set len [llength $data]
    for {set j 0} {$j < $len} {incr j} {
        set ele [lindex $data $j]
        if {$ele == "object"} {
            incr j
            set ele [lindex $data $j]
            set len2 [llength $ele]
            for {set k 0} {$k < $len2} {incr k} {
                set ele2 [lindex $ele $k]
                set polyline(object.[lindex $ele2 0]) [lindex $ele2 1]
            }
        } elseif {$ele == "basic\\ attribute"} {
            incr j
            set ele [lindex $data $j]
            set len2 [llength $ele]
            for {set k 0} {$k < $len2} {incr k} {
                
                set ele2 [lindex $ele $k]
                set polyline(basicattribute.[lindex $ele2 0]) [lindex $ele2 1]
            }
        } elseif {$ele == "dynamic\\ attribute"} {
            incr j
            set ele [lindex $data $j]
            set len2 [llength $ele]
            for {set k 0} {$k < $len2} {incr k} {
                
                set ele2 [lindex $ele $k]
                set polyline(dynamicattribute.[lindex $ele2 0]) [lindex $ele2 1]
            }
        } elseif {$ele == "points"} {
            incr j
            set ele [lindex $data $j]
            set polyline(points) [join [split [join [split [join [split $ele \(]] \)]] ,]]
        }
    }
    return [array get polyline]
}

proc APSParseADLPolygonElement {data} {
    set polygon(object.x) 0
    set polygon(object.y) 0
    set polygon(object.width) 0
    set polygon(object.height) 0
    set polygon(basicattribute.clr) 0
    set polygon(basicattribute.style) ""
    set polygon(basicattribute.fill) "solid"
    set polygon(basicattribute.width) 1
    set polygon(dynamicattribute.clr) "static"
    set polygon(dynamicattribute.vis) "static"
    set polygon(dynamicattribute.calc) ""
    set polygon(dynamicattribute.chan) ""
    set polygon(dynamicattribute.chanB) ""
    set polygon(dynamicattribute.chanC) ""
    set polygon(dynamicattribute.chanD) ""
    set polygon(points) ""
    set len [llength $data]
    for {set j 0} {$j < $len} {incr j} {
        set ele [lindex $data $j]
        if {$ele == "object"} {
            incr j
            set ele [lindex $data $j]
            set len2 [llength $ele]
            for {set k 0} {$k < $len2} {incr k} {
                set ele2 [lindex $ele $k]
                set polygon(object.[lindex $ele2 0]) [lindex $ele2 1]
            }
        } elseif {$ele == "basic\\ attribute"} {
            incr j
            set ele [lindex $data $j]
            set len2 [llength $ele]
            for {set k 0} {$k < $len2} {incr k} {
                
                set ele2 [lindex $ele $k]
                set polygon(basicattribute.[lindex $ele2 0]) [lindex $ele2 1]
            }                      
        } elseif {$ele == "dynamic\\ attribute"} {
            incr j
            set ele [lindex $data $j]
            set len2 [llength $ele]
            for {set k 0} {$k < $len2} {incr k} {
                
                set ele2 [lindex $ele $k]
                set polygon(dynamicattribute.[lindex $ele2 0]) [lindex $ele2 1]
            }                      
        } elseif {$ele == "points"} {
            incr j
            set ele [lindex $data $j]
            set polygon(points) [join [split [join [split [join [split $ele \(]] \)]] ,]]
        }
    }
    return [array get polygon]
}

proc APSParseADLArcElement {data} {
    set arc(object.x) 0
    set arc(object.y) 0
    set arc(object.width) 0
    set arc(object.height) 0
    set arc(basicattribute.clr) 0
    set arc(basicattribute.style) ""
    set arc(basicattribute.fill) "solid"
    set arc(basicattribute.width) 1
    set arc(dynamicattribute.clr) "static"
    set arc(dynamicattribute.vis) "static"
    set arc(dynamicattribute.calc) ""
    set arc(dynamicattribute.chan) ""
    set arc(dynamicattribute.chanB) ""
    set arc(dynamicattribute.chanC) ""
    set arc(dynamicattribute.chanD) ""
    set arc(begin) ""
    set arc(path) ""
    set len [llength $data]
    for {set j 0} {$j < $len} {incr j} {
        set ele [lindex $data $j]
        if {$ele == "object"} {
            incr j
            set ele [lindex $data $j]
            set len2 [llength $ele]
            for {set k 0} {$k < $len2} {incr k} {
                set ele2 [lindex $ele $k]
                set arc(object.[lindex $ele2 0]) [lindex $ele2 1]
            }
        } elseif {$ele == "basic\\ attribute"} {
            incr j
            set ele [lindex $data $j]
            set len2 [llength $ele]
            for {set k 0} {$k < $len2} {incr k} {
                
                set ele2 [lindex $ele $k]
                set arc(basicattribute.[lindex $ele2 0]) [lindex $ele2 1]
            }                      
        } elseif {$ele == "dynamic\\ attribute"} {
            incr j
            set ele [lindex $data $j]
            set len2 [llength $ele]
            for {set k 0} {$k < $len2} {incr k} {
                
                set ele2 [lindex $ele $k]
                set arc(dynamicattribute.[lindex $ele2 0]) [lindex $ele2 1]
            }                      
        } else {
            set arc([lindex $ele 0]) [lindex $ele 1]
        }
    }
    return [array get arc]
}

proc APSParseADLImageElement {data} {
    set image(object.x) 0
    set image(object.y) 0
    set image(object.width) 0
    set image(object.height) 0
    set image(basicattribute.clr) 0
    set image(basicattribute.style) ""
    set image(basicattribute.fill) "solid"
    set image(basicattribute.width) 1
    set image(dynamicattribute.vis) "static"
    set image(dynamicattribute.calc) ""
    set image(dynamicattribute.chan) ""
    set image(dynamicattribute.chanB) ""
    set image(dynamicattribute.chanC) ""
    set image(dynamicattribute.chanD) ""
    set image(type) "gif"
    set image(image\ name) ""
    set len [llength $data]
    for {set j 0} {$j < $len} {incr j} {
        set ele [lindex $data $j]
        if {$ele == "object"} {
            incr j
            set ele [lindex $data $j]
            set len2 [llength $ele]
            for {set k 0} {$k < $len2} {incr k} {
                set ele2 [lindex $ele $k]
                set image(object.[lindex $ele2 0]) [lindex $ele2 1]
            }
        } elseif {$ele == "basic\\ attribute"} {
            incr j
            set ele [lindex $data $j]
            set len2 [llength $ele]
            for {set k 0} {$k < $len2} {incr k} {
                
                set ele2 [lindex $ele $k]
                set image(basicattribute.[lindex $ele2 0]) [lindex $ele2 1]
            }                      
        } elseif {$ele == "dynamic\\ attribute"} {
            incr j
            set ele [lindex $data $j]
            set len2 [llength $ele]
            for {set k 0} {$k < $len2} {incr k} {
                
                set ele2 [lindex $ele $k]
                set image(dynamicattribute.[lindex $ele2 0]) [lindex $ele2 1]
            }                      
        } else {
            set image([lindex $ele 0]) [lindex $ele 1]
        }
    }
    return [array get image]
}

proc APSParseADLCompositeElement {data uniqueID} {
    set composite(object.x) 0
    set composite(object.y) 0
    set composite(object.width) 200
    set composite(object.height) 200
    set composite(dynamicattribute.vis) ""
    set composite(dynamicattribute.chan) ""
   
    set len [llength $data]
    for {set j 0} {$j < $len} {incr j} {
        set ele [lindex $data $j]
        if {$ele == "object"} {
            incr j
            set ele [lindex $data $j]
            set len2 [llength $ele]
            for {set k 0} {$k < $len2} {incr k} {
                set ele2 [lindex $ele $k]
                set composite(object.[lindex $ele2 0]) [lindex $ele2 1]
            }
        } elseif {$ele == "children"} {
            incr j
            APSParseADLData [lindex $data $j] $uniqueID
        } elseif {$ele == "dynamic\\ attribute"} {
            incr j
            set ele [lindex $data $j]
            set len2 [llength $ele]
            for {set k 0} {$k < $len2} {incr k} {
                set ele2 [lindex $ele $k]
                set composite(dynamicattribute.[lindex $ele2 0]) [lindex $ele2 1]
            }
        } else {
            set composite([lindex $ele 0]) [lindex $ele 1]
        }
    }
    return [array get composite]
}

proc APSParseADLValuatorElement {data} {
    set valuator(object.x) 0
    set valuator(object.y) 0
    set valuator(object.width) 200
    set valuator(object.height) 200
    set valuator(control.chan) ""
    set valuator(control.clr) 0
    set valuator(control.bclr) 0
    set valuator(label) ""
    set valuator(dPrecision) 1

    set len [llength $data]
    for {set j 0} {$j < $len} {incr j} {
        set ele [lindex $data $j]
        if {$ele == "object"} {
            incr j
            set ele [lindex $data $j]
            set len2 [llength $ele]
            for {set k 0} {$k < $len2} {incr k} {
                set ele2 [lindex $ele $k]
                set valuator(object.[lindex $ele2 0]) [lindex $ele2 1]
            }
        } elseif {$ele == "control"} {
            incr j
            set ele [lindex $data $j]
            set len2 [llength $ele]
            for {set k 0} {$k < $len2} {incr k} {
                set ele2 [lindex $ele $k]
                set valuator(control.[lindex $ele2 0]) [lindex $ele2 1]
            }
        } else {
            set valuator([lindex $ele 0]) [lindex $ele 1]
        }
    }
    return [array get valuator]
}

proc APSParseADLTextElement {data} {
    set text(object.x) 0
    set text(object.y) 0
    set text(object.width) 200
    set text(object.height) 200
    set text(basicattribute.clr) 0
    set text(basicattribute.fill) 0
    set text(textix) ""
    set text(align) "horiz. left"
    set text(dynamicattribute.clr) "static"
    set text(dynamicattribute.vis) "static"
    set text(dynamicattribute.calc) ""
    set text(dynamicattribute.chan) ""
    set text(dynamicattribute.chanB) ""
    set text(dynamicattribute.chanC) ""
    set text(dynamicattribute.chanD) ""

    set len [llength $data]
    for {set j 0} {$j < $len} {incr j} {
        set ele [lindex $data $j]
        if {$ele == "object"} {
            incr j
            set ele [lindex $data $j]
            set len2 [llength $ele]
            for {set k 0} {$k < $len2} {incr k} {
                set ele2 [lindex $ele $k]
                set text(object.[lindex $ele2 0]) [lindex $ele2 1]
            }
        } elseif {$ele == "basic\\ attribute"} {
            incr j
            set ele [lindex $data $j]
            set len2 [llength $ele]
            for {set k 0} {$k < $len2} {incr k} {
                set ele2 [lindex $ele $k]
                set text(basicattribute.[lindex $ele2 0]) [lindex $ele2 1]
            }
        } elseif {$ele == "dynamic\\ attribute"} {
            incr j
            set ele [lindex $data $j]
            set len2 [llength $ele]
            for {set k 0} {$k < $len2} {incr k} {
                set ele2 [lindex $ele $k]
                set text(dynamicattribute.[lindex $ele2 0]) [lindex $ele2 1]
            }
        } else {
            set text([lindex $ele 0]) [lindex $ele 1]
        }
    }

    return [array get text]
}

proc APSParseADLRelateddisplayElement {data} {
    set relateddisplay(object.x) 0
    set relateddisplay(object.y) 0
    set relateddisplay(object.width) 200
    set relateddisplay(object.height) 200
    set relateddisplay(clr) 0
    set relateddisplay(bclr) 0
    set relateddisplay(label) ""
    set relateddisplay(visual) ""

    for {set i 0} {$i < 8} {incr i} {
        set relateddisplay(display${i}.label) ""
        set relateddisplay(display${i}.name) ""
        set relateddisplay(display${i}.args) ""
    }

    set len [llength $data]
    for {set j 0} {$j < $len} {incr j} {
        set ele [lindex $data $j]
        if {$ele == "object"} {
            incr j
            set ele [lindex $data $j]
            set len2 [llength $ele]
            for {set k 0} {$k < $len2} {incr k} {
                set ele2 [lindex $ele $k]
                set relateddisplay(object.[lindex $ele2 0]) [lindex $ele2 1]
            }
        } elseif {[string match "display\\\[?\\\]" $ele]} {
            set i [string index $ele 8]
            incr j
            set ele [lindex $data $j]
            set len2 [llength $ele]
            for {set k 0} {$k < $len2} {incr k} {
                set ele2 [lindex $ele $k]
                set relateddisplay(display${i}.[lindex $ele2 0]) [lindex $ele2 1]
            }
        } else {
            set relateddisplay([lindex $ele 0]) [lindex $ele 1]
        }
    }

    return [array get relateddisplay]
}

proc APSParseADLShellcommandElement {data} {
    set shellcommand(object.x) 0
    set shellcommand(object.y) 0
    set shellcommand(object.width) 200
    set shellcommand(object.height) 200
    set shellcommand(clr) 0
    set shellcommand(bclr) 0

    for {set i 0} {$i < 8} {incr i} {
        set shellcommand(command${i}.label) ""
        set shellcommand(command${i}.name) ""
        set shellcommand(command${i}.args) ""
    }

    set len [llength $data]
    for {set j 0} {$j < $len} {incr j} {
        set ele [lindex $data $j]
        if {$ele == "object"} {
            incr j
            set ele [lindex $data $j]
            set len2 [llength $ele]
            for {set k 0} {$k < $len2} {incr k} {
                set ele2 [lindex $ele $k]
                set shellcommand(object.[lindex $ele2 0]) [lindex $ele2 1]
            }
        } elseif {[string match "command\\\[?\\\]" $ele]} {
            set i [string index $ele 8]
            incr j
            set ele [lindex $data $j]
            set len2 [llength $ele]
            for {set k 0} {$k < $len2} {incr k} {
                set ele2 [lindex $ele $k]
                set shellcommand(command${i}.[lindex $ele2 0]) [lindex $ele2 1]
            }
        } else {
            set shellcommand([lindex $ele 0]) [lindex $ele 1]
        }
    }

    return [array get shellcommand]
}

proc APSParseADLTextupdateElement {data} {
    set textupdate(object.x) 0
    set textupdate(object.y) 0
    set textupdate(object.width) 200
    set textupdate(object.height) 200
    set textupdate(monitor.chan) ""
    set textupdate(monitor.clr) 0
    set textupdate(monitor.bclr) 0
    set textupdate(clrmod) "" 
    set textupdate(align) ""
    set textupdate(format) ""

    set len [llength $data]
    for {set j 0} {$j < $len} {incr j} {
        set ele [lindex $data $j]
        if {$ele == "object"} {
            incr j
            set ele [lindex $data $j]
            set len2 [llength $ele]
            for {set k 0} {$k < $len2} {incr k} {
                set ele2 [lindex $ele $k]
                set textupdate(object.[lindex $ele2 0]) [lindex $ele2 1]
            }
        } elseif {$ele == "monitor"} {
            incr j
            set ele [lindex $data $j]
            set len2 [llength $ele]
            for {set k 0} {$k < $len2} {incr k} {
                set ele2 [lindex $ele $k]
                set textupdate(monitor.[lindex $ele2 0]) [lindex $ele2 1]
            }
        } else {
            set textupdate([lindex $ele 0]) [lindex $ele 1]
        }
    }

    return [array get textupdate]
}

proc APSParseADLTextentryElement {data} {
    set textentry(object.x) 0
    set textentry(object.y) 0
    set textentry(object.width) 200
    set textentry(object.height) 200
    set textentry(control.chan) ""
    set textentry(control.clr) 0
    set textentry(control.bclr) 0
    set textentry(clrmod) "" 
    set textentry(align) ""
    set textentry(format) ""

    set len [llength $data]
    for {set j 0} {$j < $len} {incr j} {
        set ele [lindex $data $j]
        if {$ele == "object"} {
            incr j
            set ele [lindex $data $j]
            set len2 [llength $ele]
            for {set k 0} {$k < $len2} {incr k} {
                set ele2 [lindex $ele $k]
                set textentry(object.[lindex $ele2 0]) [lindex $ele2 1]
            }
        } elseif {$ele == "control"} {
            incr j
            set ele [lindex $data $j]
            set len2 [llength $ele]
            for {set k 0} {$k < $len2} {incr k} {
                set ele2 [lindex $ele $k]
                set textentry(control.[lindex $ele2 0]) [lindex $ele2 1]
            }
        } else {
            set textentry([lindex $ele 0]) [lindex $ele 1]
        }
    }

    return [array get textentry]
}

proc APSParseADLMessagebuttonElement {data} {
    set messagebutton(object.x) 0
    set messagebutton(object.y) 0
    set messagebutton(object.width) 200
    set messagebutton(object.height) 200
    set messagebutton(control.chan) ""
    set messagebutton(control.clr) 0
    set messagebutton(control.bclr) 0
    set messagebutton(label) "" 
    set messagebutton(press_msg) 0

    set len [llength $data]
    for {set j 0} {$j < $len} {incr j} {
        set ele [lindex $data $j]
        if {$ele == "object"} {
            incr j
            set ele [lindex $data $j]
            set len2 [llength $ele]
            for {set k 0} {$k < $len2} {incr k} {
                set ele2 [lindex $ele $k]
                set messagebutton(object.[lindex $ele2 0]) [lindex $ele2 1]
            }
        } elseif {$ele == "control"} {
            incr j
            set ele [lindex $data $j]
            set len2 [llength $ele]
            for {set k 0} {$k < $len2} {incr k} {
                set ele2 [lindex $ele $k]
                set messagebutton(control.[lindex $ele2 0]) [lindex $ele2 1]
            }
        } else {
            set messagebutton([lindex $ele 0]) [lindex $ele 1]
        }
    }

    return [array get messagebutton]
}

proc APSParseADLChoicebuttonElement {data} {
    set choicebutton(object.x) 0
    set choicebutton(object.y) 0
    set choicebutton(object.width) 200
    set choicebutton(object.height) 200
    set choicebutton(control.chan) ""
    set choicebutton(control.clr) 0
    set choicebutton(control.bclr) 0
    set choicebutton(stacking) ""
    set choicebutton(clrmod) ""
    set len [llength $data]
    for {set j 0} {$j < $len} {incr j} {
        set ele [lindex $data $j]
        if {$ele == "object"} {
            incr j
            set ele [lindex $data $j]
            set len2 [llength $ele]
            for {set k 0} {$k < $len2} {incr k} {
                set ele2 [lindex $ele $k]
                set choicebutton(object.[lindex $ele2 0]) [lindex $ele2 1]
            }
        } elseif {$ele == "control"} {
            incr j
            set ele [lindex $data $j]
            set len2 [llength $ele]
            for {set k 0} {$k < $len2} {incr k} {
                set ele2 [lindex $ele $k]
                set choicebutton(control.[lindex $ele2 0]) [lindex $ele2 1]
            }
        } else {
            set choicebutton([lindex $ele 0]) [lindex $ele 1]
        }
    }

    return [array get choicebutton]
}

proc APSParseADLBarElement {data} {
    set bar(object.x) 0
    set bar(object.y) 0
    set bar(object.width) 200
    set bar(object.height) 200
    set bar(monitor.chan) ""
    set bar(monitor.clr) 0
    set bar(monitor.bclr) 0
    set bar(label) ""
    set bar(clrmod) ""
    set bar(direction) "right"

    set len [llength $data]
    for {set j 0} {$j < $len} {incr j} {
        set ele [lindex $data $j]
        if {$ele == "object"} {
            incr j
            set ele [lindex $data $j]
            set len2 [llength $ele]
            for {set k 0} {$k < $len2} {incr k} {
                set ele2 [lindex $ele $k]
                set bar(object.[lindex $ele2 0]) [lindex $ele2 1]
            }
        } elseif {$ele == "monitor"} {
            incr j
            set ele [lindex $data $j]
            set len2 [llength $ele]
            for {set k 0} {$k < $len2} {incr k} {
                set ele2 [lindex $ele $k]
                set bar(monitor.[lindex $ele2 0]) [lindex $ele2 1]
            }
        } else {
            set bar([lindex $ele 0]) [lindex $ele 1]
        }
    }

    return [array get bar]
}

proc APSParseADLIndicatorElement {data} {
    set indicator(object.x) 0
    set indicator(object.y) 0
    set indicator(object.width) 200
    set indicator(object.height) 200
    set indicator(monitor.chan) ""
    set indicator(monitor.clr) 0
    set indicator(monitor.bclr) 0
    set indicator(label) ""
    set indicator(clrmod) ""
    set indicator(direction) "right"

    set len [llength $data]
    for {set j 0} {$j < $len} {incr j} {
        set ele [lindex $data $j]
        if {$ele == "object"} {
            incr j
            set ele [lindex $data $j]
            set len2 [llength $ele]
            for {set k 0} {$k < $len2} {incr k} {
                set ele2 [lindex $ele $k]
                set indicator(object.[lindex $ele2 0]) [lindex $ele2 1]
            }
        } elseif {$ele == "monitor"} {
            incr j
            set ele [lindex $data $j]
            set len2 [llength $ele]
            for {set k 0} {$k < $len2} {incr k} {
                set ele2 [lindex $ele $k]
                set indicator(monitor.[lindex $ele2 0]) [lindex $ele2 1]
            }
        } else {
            set indicator([lindex $ele 0]) [lindex $ele 1]
        }
    }

    return [array get indicator]
}

proc APSParseADLStripChartElement {data} {
    set stripchart(object.x) 0
    set stripchart(object.y) 0
    set stripchart(object.width) 200
    set stripchart(object.height) 200
    set stripchart(plotcom.title) ""
    set stripchart(plotcom.clr) 0
    set stripchart(plotcom.bclr) 0
    set stripchart(plotcom.xlabel) ""
    set stripchart(plotcom.ylabel) ""
    set stripchart(period) 60
    set stripchart(units) "second"
    for {set i 0} {$i < 8} {incr i} {
        set stripchart(pen${i}.chan) ""
        set stripchart(pen${i}.clr) ""
    }

    set len [llength $data]
    for {set j 0} {$j < $len} {incr j} {
        set ele [lindex $data $j]
        if {$ele == "object"} {
            incr j
            set ele [lindex $data $j]
            set len2 [llength $ele]
            for {set k 0} {$k < $len2} {incr k} {
                set ele2 [lindex $ele $k]
                set stripchart(object.[lindex $ele2 0]) [lindex $ele2 1]
            }
        } elseif {$ele == "plotcom"} {
            incr j
            set ele [lindex $data $j]
            set len2 [llength $ele]
            for {set k 0} {$k < $len2} {incr k} {
                set ele2 [lindex $ele $k]
                set stripchart(plotcom.[lindex $ele2 0]) [lindex $ele2 1]
            }
        } elseif {[string match "pen\\\[?\\\]" $ele]} {
            set i [string index $ele 4]
            set stripchart(pen${i}.limits.hoprSrc) "channel"
            set stripchart(pen${i}.limits.loprSrc) "channel"
            incr j
            set ele [lindex $data $j]
            set len2 [llength $ele]
            for {set k 0} {$k < $len2} {incr k} {
                set ele2 [lindex $ele $k]
                if {$ele2 == "limits"} {
                    incr k
                    set ele3 [lindex $ele $k]
                    set len2 [llength $ele3]
                    for {set m 0} {$m < $len2} {incr m} {
                        set ele4 [lindex $ele3 $m]
                        set stripchart(pen${i}.limits.[lindex $ele4 0]) [lindex $ele4 1]
                    }
                } else {
                    set stripchart(pen${i}.[lindex $ele2 0]) [lindex $ele2 1]
                }
            }
        } else {
            set stripchart([lindex $ele 0]) [lindex $ele 1]
        }
    }

    return [array get stripchart]
}

proc APSParseADLPlotElement {data} {
    set plot(object.x) 0
    set plot(object.y) 0
    set plot(object.width) 200
    set plot(object.height) 200
    set plot(plotcom.title) ""
    set plot(plotcom.clr) 0
    set plot(plotcom.bclr) 0
    set plot(style) "line"
    set plot(erase_oldest) ""
    set plot(x_axis.maxRange) 0
    set plot(y1_axis.maxRange) 0
    set plot(y2_axis.maxRange) 0
    for {set i 0} {$i < 8} {incr i} {
        set plot(trace${i}.xdata) ""
        set plot(trace${i}.ydata) ""
        set plot(trace${i}.data_clr) ""
    }

    set len [llength $data]
    for {set j 0} {$j < $len} {incr j} {
        set ele [lindex $data $j]
        if {$ele == "object"} {
            incr j
            set ele [lindex $data $j]
            set len2 [llength $ele]
            for {set k 0} {$k < $len2} {incr k} {
                set ele2 [lindex $ele $k]
                set plot(object.[lindex $ele2 0]) [lindex $ele2 1]
            }
        } elseif {$ele == "plotcom"} {
            incr j
            set ele [lindex $data $j]
            set len2 [llength $ele]
            for {set k 0} {$k < $len2} {incr k} {
                set ele2 [lindex $ele $k]
                set plot(plotcom.[lindex $ele2 0]) [lindex $ele2 1]
            }
        } elseif {$ele == "x_axis"} {
            incr j
            set ele [lindex $data $j]
            set len2 [llength $ele]
            for {set k 0} {$k < $len2} {incr k} {
                set ele2 [lindex $ele $k]
                set plot(x_axis.[lindex $ele2 0]) [lindex $ele2 1]
            }
        } elseif {$ele == "y1_axis"} {
            incr j
            set ele [lindex $data $j]
            set len2 [llength $ele]
            for {set k 0} {$k < $len2} {incr k} {
                set ele2 [lindex $ele $k]
                set plot(y1_axis.[lindex $ele2 0]) [lindex $ele2 1]
            }
        } elseif {$ele == "y2_axis"} {
            incr j
            set ele [lindex $data $j]
            set len2 [llength $ele]
            for {set k 0} {$k < $len2} {incr k} {
                set ele2 [lindex $ele $k]
                set plot(y2_axis.[lindex $ele2 0]) [lindex $ele2 1]
            }
        } elseif {[string match "trace\\\[?\\\]" $ele]} {
            set i [string index $ele 6]
            incr j
            set ele [lindex $data $j]
            set len2 [llength $ele]
            for {set k 0} {$k < $len2} {incr k} {
                set ele2 [lindex $ele $k]
                set plot(trace${i}.[lindex $ele2 0]) [lindex $ele2 1]
            }
        } else {
            set plot([lindex $ele 0]) [lindex $ele 1]
        }
    }

    return [array get plot]

}


proc APSCartesianPlot { widget args } {
    if {![llength [info commands blt::bgexec]]} {
	package require BLT
    }

    global apsContextHelp
    set parent ""
    set noPack 0
    set packOption "-side top"
    set title ""
    set xlabel ""
    set ylabel ""
    set backgroundColor black
    set foregroundColor white
    set axisColor white
    set plotBackgroundColor \#737373
    set plotForegroundColors white
    set selectionColor red
    set xmin 99e99
    set xmax -99e99
    set ymin 99e99
    set ymax -99e99
    set width 300
    set height 300
    set gridPack ""
    set xVariables ""
    set yVariables ""
    set contextHelp ""
    APSStrictParseArguments {parent noPack packOption title xlabel ylabel backgroundColor foregroundColor axisColor plotBackgroundColor plotForegroundColors selectionColor xmin xmax ymin ymax width height gridPack xVariables yVariables contextHelp}
    
    if {$contextHelp != ""} {
        set apsContextHelp($parent$widget) $contextHelp
    }
    if {$parent == ""} {
        toplevel $widget -bd 2
        wm title $widget $title
    } else {
        frame $parent$widget -bd 2
        if {!$noPack} {
            if {[string length $gridPack]} {
                eval grid $parent$widget $gridPack
            } else {
                eval pack $parent$widget $packOption
            }
        }
    }
    foreach xVariable $xVariables  {
        set i [string first "(" $xVariable]
        if {$i != -1} {
            global [string range $xVariable 0 [expr {$i - 1}]]
        } else {
            global $xVariable
        }
        set xMin [lindex [lsort -real [set $xVariable]] 0]
        set xMax [lindex [lsort -real [set $xVariable]] end]
        if {$xMin < $xmin} {
            set xmin $xMin
        }
        if {$xMax > $xmax} {
            set xmax $xMax
        }
    }
    foreach yVariable $yVariables  {
        set i [string first "(" $yVariable]
        if {$i != -1} {
            global [string range $yVariable 0 [expr {$i - 1}]]
        } else {
            global $yVariable
        }
        set yMin [lindex [lsort -real [set $yVariable]] 0]
        set yMax [lindex [lsort -real [set $yVariable]] end]
        if {$yMin < $ymin} {
            set ymin $yMin
        }
        if {$yMax > $ymax} {
            set ymax $yMax
        }
    }
    if {$xmin == $xmax} {
        set xmin [expr {$xmin - 1}]
        set xmax [expr {$xmax + 1}]
    }
    if {$ymin == $ymax} {
        set ymin [expr {$ymin - 1}]
        set ymax [expr {$ymax + 1}]
    }
    blt::graph $parent$widget.graph \
      -cursor crosshair \
      -width $width \
      -height $height \
      -bg $backgroundColor \
      -fg $foregroundColor \
      -title $title \
      -plotbackground $plotBackgroundColor
    $parent$widget.graph xaxis configure \
      -color $axisColor \
      -background $backgroundColor \
      -min $xmin \
      -max $xmax \
      -title $xlabel \
      -titlecolor $axisColor
    $parent$widget.graph yaxis configure \
      -color $axisColor \
      -background $backgroundColor \
      -min $ymin \
      -max $ymax \
      -title $ylabel \
      -titlecolor $axisColor
    $parent$widget.graph legend configure -hide yes
    $parent$widget.graph grid configure -hide no -dashes "2 2"

    set lines [llength $yVariables]
    set i 0
    foreach xVariable $xVariables yVariable $yVariables plotForegroundColor $plotForegroundColors {
        $parent$widget.graph element create line1_$i \
          -xdata [set $xVariable] \
          -ydata [set $yVariable] \
          -color $plotForegroundColor \
          -pixels 1 \
          -linewidth 1
        $parent$widget.graph element create line2_$i \
          -xdata [set $xVariable] \
          -ydata [set $yVariable] \
          -color $plotForegroundColor \
          -pixels 2  \
          -linewidth 0 -fill $plotForegroundColor
        trace variable $yVariable w "APSCartesianPlotUpdate $parent$widget.graph $i $lines y"
        trace variable $xVariable w "APSCartesianPlotUpdate $parent$widget.graph $i $lines x"
        incr i
    }
    pack $parent$widget.graph -fill both -expand true
    
}

proc APSCartesianPlotUpdate {widget index lines plane var element action} {
    global $var
    if {[array exists $var]} {
        set var ${var}(${element})
    }
    set values [set $var]
    $widget element configure line1_$index -${plane}data $values
    $widget element configure line2_$index -${plane}data $values
    set ${plane}min 99e99
    set ${plane}max -99e99
    for {set line 0} {$line < $lines} {incr line} {
        set sorted [lsort -real [$widget element cget line1_$line -${plane}data]]
        set ${plane}Min [lindex $sorted 0]
        set ${plane}Max [lindex $sorted end]
        if {[set ${plane}Min] < [set ${plane}min]} {
            set ${plane}min [set ${plane}Min]
        }
        if {[set ${plane}Max] > [set ${plane}max]} {
            set ${plane}max [set ${plane}Max]
        }
    }
    if {[set ${plane}max] == [set ${plane}min]} {
        set ${plane}max [expr {[set ${plane}max] + 1}]
        set ${plane}min [expr {[set ${plane}min] - 1}]
    }
    $widget ${plane}axis configure -min [set ${plane}min] -max [set ${plane}max]
}

proc APSStripChart { widget args } {
    if {![llength [info commands blt::bgexec]]} {
	package require BLT
    }

    global apsContextHelp
    set parent ""
    set noPack 0
    set packOption "-side top"
    set title ""
    set xlabel ""
    set ylabel ""
    set backgroundColor black
    set foregroundColor white
    set axisColor white
    set plotBackgroundColor \#737373
    set plotForegroundColors white
    set selectionColor red
    set xmin 99e99
    set xmax -99e99
    set ymin 99e99
    set ymax -99e99
    set width 300
    set height 300
    set gridPack ""
    set xVariables ""
    set yVariables ""
    set period 60
    set units "second"
    set contextHelp ""
    APSStrictParseArguments {parent noPack packOption title xlabel ylabel backgroundColor foregroundColor axisColor plotBackgroundColor plotForegroundColors selectionColor xmin xmax ymin ymax width height gridPack xVariables yVariables contextHelp period units}

    if {$contextHelp != ""} {
        set apsContextHelp($parent$widget) $contextHelp
    }
    if {$parent == ""} {
        toplevel $widget -bd 2
        wm title $widget $title
    } else {
        frame $parent$widget -bd 2
        if {!$noPack} {
            if {[string length $gridPack]} {
                eval grid $parent$widget $gridPack
            } else {
                eval pack $parent$widget $packOption
            }
        }
    }
    foreach yVariable $yVariables  {
        set i [string first "(" $yVariable]
        if {$i != -1} {
            global [string range $yVariable 0 [expr {$i - 1}]]
        } else {
            global $yVariable
        }
    }
    blt::stripchart $parent$widget.stripchart \
      -cursor crosshair \
      -width $width \
      -height $height \
      -bg $backgroundColor \
      -fg $foregroundColor \
      -title $title \
      -plotbackground $plotBackgroundColor -plotborderwidth 1
    $parent$widget.stripchart xaxis configure \
      -color $axisColor \
      -background $backgroundColor \
      -title $xlabel \
      -titlecolor $axisColor \
      -autorange $period \
      -descending true
    $parent$widget.stripchart yaxis configure \
      -color $axisColor \
      -background $backgroundColor \
      -title $ylabel \
      -titlecolor $axisColor
    $parent$widget.stripchart axis configure y -min $ymin -max $ymax
    $parent$widget.stripchart legend configure -hide yes
    $parent$widget.stripchart grid configure -hide no -dashes "2 2"
    set lines [llength $yVariables]

    set xdata ""
    set ydata ""
    for {set i 0} {$i < $period} {incr i} {
        append xdata "$i "
        append ydata "0 "
    }
    
    set lines [llength $yVariables]
    set i 0
    foreach yVariable $yVariables plotForegroundColor $plotForegroundColors {
        $parent$widget.stripchart element create line1_$i \
          -xdata $xdata \
          -ydata $ydata \
          -color $plotForegroundColor \
          -pixels 1 \
          -linewidth 1
        if {$units == "minute"} {
            after 60000 "APSStripChartUpdate $parent$widget.stripchart $i $lines y $yVariable 60000"
        } elseif {$units == "milli-second"} {
            after 1 "APSStripChartUpdate $parent$widget.stripchart $i $lines y $yVariable 1"
        } else {
            after 1000 "APSStripChartUpdate $parent$widget.stripchart $i $lines y $yVariable 1000"
        }
        incr i
    }
    pack $parent$widget.stripchart -fill both -expand true
    
}

proc APSStripChartUpdate {widget index lines plane var period} {
    global apsADLInfo
    set value [set $var]
    set ydata [lrange "$value [$widget element cget line1_$index -${plane}data]" 0 end-1]
    $widget element configure line1_$index -${plane}data $ydata
    after $period "APSStripChartUpdate $widget $index $lines $plane $var $period"
}

proc APSParseADLData {data uniqueID} {
    global apsADLInfo
    
    set canvas $apsADLInfo(${uniqueID}.canvas)
    set colormap(colors) $apsADLInfo(${uniqueID}.colors)
    
    set len [llength $data]
    for {set i 0} {$i < $len} {incr i} {
        switch  [lindex $data $i] \
          "file" {
              incr i
              array set file [APSParseADLFileElement [lindex $data $i]]
          } "display" {
              incr i
              array set display [APSParseADLDisplayElement [lindex $data $i]]
              #wm geometry . +${display(object.x)}+${display(object.y)}
              $canvas configure -width ${display(object.width)} -height ${display(object.height)}
          } "color\\ map" {
              incr i
              array set colormap [APSParseADLColormapElement [lindex $data $i]]
              $canvas configure -background \#[lindex $colormap(colors) $display(bclr)]
              set apsADLInfo(${uniqueID}.colors) $colormap(colors)
          } "related\\ display" {
              incr i
              array set relateddisplay [APSParseADLRelateddisplayElement [lindex $data $i]]
              set index $apsADLInfo(${uniqueID}.relatedbuttons)
              incr apsADLInfo(${uniqueID}.relatedbuttons)

              if {$relateddisplay(visual) == "invisible"} {
                  #don't know what to do with this yet
              } elseif {$relateddisplay(visual) == "a column of buttons"} {
                  #don't know what to do with this yet
              } elseif {$relateddisplay(visual) == "a row of buttons"} {
                  #don't know what to do with this yet
              } else {
                  menubutton ${canvas}.relatedbutton$index \
                    -indicatoron 1 \
                    -menu ${canvas}.relatedbutton${index}.m \
                    -state normal \
                    -relief raised \
                    -bg \#[lindex $colormap(colors) $relateddisplay(bclr)] \
                    -fg \#[lindex $colormap(colors) $relateddisplay(clr)] \
                    -text $relateddisplay(label) \
                    -font [APSSelectADLFont -height $relateddisplay(object.height) -type button]
                  menu ${canvas}.relatedbutton${index}.m \
                    -tearoff 0 \
                    -bg \#[lindex $colormap(colors) $relateddisplay(bclr)] \
                    -fg \#[lindex $colormap(colors) $relateddisplay(clr)] \
                    -font [APSSelectADLFont -height $relateddisplay(object.height) -type button]
                  place ${canvas}.relatedbutton$index \
                    -x $relateddisplay(object.x) \
                    -y $relateddisplay(object.y) \
                    -width $relateddisplay(object.width) \
                    -height $relateddisplay(object.height)
                  for {set j 0} {$j < 8} {incr j} {
                      set relateddisplay(display${j}.name) [file native $relateddisplay(display${j}.name)]
                      if {[string length $relateddisplay(display${j}.name)]} {
                          if {![file exists $relateddisplay(display${j}.name)]} {
                              
                              if {[file exists [file native [file join $apsADLInfo(${uniqueID}.pwd) $relateddisplay(display${j}.name)]]]} {
                                  set relateddisplay(display${j}.name) [file native [file join $apsADLInfo(${uniqueID}.pwd) $relateddisplay(display${j}.name)]]
                              } elseif {[file exists [file native [file join /net/helios/usr/local-sun4u/iocapps/adlsys $relateddisplay(display${j}.name)]]]} {
                                  set relateddisplay(display${j}.name) [file native [file join /net/helios/usr/local-sun4u/iocapps/adlsys $relateddisplay(display${j}.name)]]
                              } elseif {[file exists [file native [file join /net/helios/usr/local-sun4u/iocapps $relateddisplay(display${j}.name)]]]} {
                                  set relateddisplay(display${j}.name) [file native [file join /net/helios/usr/local-sun4u/iocapps $relateddisplay(display${j}.name)]]
                              }
                          }
                          ${canvas}.relatedbutton${index}.m add command -label $relateddisplay(display${j}.label) -command "APSCreateToplevelADLScreen .[APSUniqueName tmpID] -filename $relateddisplay(display${j}.name)"
                      }
                  }
              }
          } "rectangle" {
              incr i
              array set rectangle [APSParseADLRectangleElement [lindex $data $i]]
              set index ""
              if {($rectangle(dynamicattribute.vis) != "static") || ($rectangle(dynamicattribute.clr) != "static")} {
                  set index $apsADLInfo(${uniqueID}.rectangles)
                  set apsADLInfo(${uniqueID}.rectangle${index}.chan) $rectangle(dynamicattribute.chan)
                  set apsADLInfo(${uniqueID}.rectangle${index}.chanB) $rectangle(dynamicattribute.chanB)
                  set apsADLInfo(${uniqueID}.rectangle${index}.chanC) $rectangle(dynamicattribute.chanC)
                  set apsADLInfo(${uniqueID}.rectangle${index}.chanD) $rectangle(dynamicattribute.chanD)
                  set apsADLInfo(${uniqueID}.rectangle${index}.vis) $rectangle(dynamicattribute.vis)
                  set apsADLInfo(${uniqueID}.rectangle${index}.clr) $rectangle(dynamicattribute.clr)
                  set apsADLInfo(${uniqueID}.rectangle${index}.calc) $rectangle(dynamicattribute.calc)
                  set apsADLInfo(${uniqueID}.rectangle${index}.fill) $rectangle(basicattribute.fill)
                  set apsADLInfo(${uniqueID}.rectangle${index}.formatedvalue) 0
                  incr apsADLInfo(${uniqueID}.rectangles)
                  set state hidden
              } else {
                  set state normal
              }
              if {$rectangle(basicattribute.fill) == "outline"} {
                  if {$rectangle(basicattribute.width) <= 0} {
                      set rectangle(basicattribute.width) 1
                  }
                  set fillOption ""
              } else {
                  set fillOption "-fill \#[lindex $colormap(colors) $rectangle(basicattribute.clr)]"
              }
              if {$rectangle(basicattribute.style) == "dash"} {
                  set dashOption "-dash {2 2}"
              } else {
                  set dashOption ""
              }
              eval $canvas create rectangle \
                $rectangle(object.x) \
                $rectangle(object.y) \
                [expr {$rectangle(object.x) + $rectangle(object.width)}] \
                [expr {$rectangle(object.y) + $rectangle(object.height)}] \
                $fillOption \
                -outline \#[lindex $colormap(colors) $rectangle(basicattribute.clr)] \
                -width $rectangle(basicattribute.width) \
                -tags rectangle$index \
                -state $state \
                $dashOption
          } "polygon" {
              incr i
              array set polygon [APSParseADLPolygonElement [lindex $data $i]]
              set index ""
              if {($polygon(dynamicattribute.vis) != "static") || ($polygon(dynamicattribute.clr) != "static")} {
                  set index $apsADLInfo(${uniqueID}.polygons)
                  set apsADLInfo(${uniqueID}.polygon${index}.chan) $polygon(dynamicattribute.chan)
                  set apsADLInfo(${uniqueID}.polygon${index}.chanB) $polygon(dynamicattribute.chanB)
                  set apsADLInfo(${uniqueID}.polygon${index}.chanC) $polygon(dynamicattribute.chanC)
                  set apsADLInfo(${uniqueID}.polygon${index}.chanD) $polygon(dynamicattribute.chanD)
                  set apsADLInfo(${uniqueID}.polygon${index}.vis) $polygon(dynamicattribute.vis)
                  set apsADLInfo(${uniqueID}.polygon${index}.clr) $polygon(dynamicattribute.clr)
                  set apsADLInfo(${uniqueID}.polygon${index}.calc) $polygon(dynamicattribute.calc)
                  set apsADLInfo(${uniqueID}.polygon${index}.fill) $polygon(basicattribute.fill)
                  set apsADLInfo(${uniqueID}.polygon${index}.formatedvalue) 0
                  incr apsADLInfo(${uniqueID}.polygons)
                  set state hidden
              } else {
                  set state normal
              }
              if {$polygon(basicattribute.style) == "dash"} {
                  set dashOption "-dash {2 2}"
              } else {
                  set dashOption ""
              }
              if {$polygon(basicattribute.fill) == "outline"} {
                  if {$polygon(basicattribute.width) <= 0} {
                      set polygon(basicattribute.width) 1
                  }
                  eval $canvas create line \
                    $polygon(points) \
                    -fill \#[lindex $colormap(colors) $polygon(basicattribute.clr)] \
                    -width $polygon(basicattribute.width) \
                    -tags polygon$index \
                    -state $state \
                    $dashOption
              } else {
                  eval $canvas create polygon \
                    $polygon(points) \
                    -fill \#[lindex $colormap(colors) $polygon(basicattribute.clr)] \
                    -outline \#[lindex $colormap(colors) $polygon(basicattribute.clr)] \
                    -width $polygon(basicattribute.width) \
                    -tags polygon$index \
                    -state $state \
                    $dashOption
              }
          } "oval" {
              incr i
              array set oval [APSParseADLOvalElement [lindex $data $i]]
              set index ""
              if {($oval(dynamicattribute.vis) != "static") || ($oval(dynamicattribute.clr) != "static")} {
                  set index $apsADLInfo(${uniqueID}.ovals)
                  set apsADLInfo(${uniqueID}.oval${index}.chan) $oval(dynamicattribute.chan)
                  set apsADLInfo(${uniqueID}.oval${index}.chanB) $oval(dynamicattribute.chanB)
                  set apsADLInfo(${uniqueID}.oval${index}.chanC) $oval(dynamicattribute.chanC)
                  set apsADLInfo(${uniqueID}.oval${index}.chanD) $oval(dynamicattribute.chanD)
                  set apsADLInfo(${uniqueID}.oval${index}.vis) $oval(dynamicattribute.vis)
                  set apsADLInfo(${uniqueID}.oval${index}.clr) $oval(dynamicattribute.clr)
                  set apsADLInfo(${uniqueID}.oval${index}.calc) $oval(dynamicattribute.calc)
                  set apsADLInfo(${uniqueID}.oval${index}.fill) $oval(basicattribute.fill)
                  set apsADLInfo(${uniqueID}.oval${index}.formatedvalue) 0
                  incr apsADLInfo(${uniqueID}.ovals)
                  set state hidden
              } else {
                  set state normal
              }
              if {$oval(basicattribute.fill) == "outline"} {
                  if {$oval(basicattribute.width) <= 0} {
                      set oval(basicattribute.width) 1
                  }
                  set fillOption ""
              } else {
                  set fillOption "-fill \#[lindex $colormap(colors) $oval(basicattribute.clr)]"
              }
              if {$oval(basicattribute.style) == "dash"} {
                  set dashOption "-dash {2 2}"
              } else {
                  set dashOption ""
              }
              eval $canvas create oval \
                $oval(object.x) \
                $oval(object.y) \
                [expr {$oval(object.x) + $oval(object.width)}] \
                [expr {$oval(object.y) + $oval(object.height)}] \
                $fillOption \
                -outline \#[lindex $colormap(colors) $oval(basicattribute.clr)] \
                -width $oval(basicattribute.width) \
                -tags oval$index \
                -state $state \
                $dashOption
          } "image" {
              incr i
              array set image [APSParseADLImageElement [lindex $data $i]]
              set index ""
              if {$image(dynamicattribute.vis) != "static"} {
                  set index $apsADLInfo(${uniqueID}.images)
                  set apsADLInfo(${uniqueID}.image${index}.chan) $image(dynamicattribute.chan)
                  set apsADLInfo(${uniqueID}.image${index}.chanB) $image(dynamicattribute.chanB)
                  set apsADLInfo(${uniqueID}.image${index}.chanC) $image(dynamicattribute.chanC)
                  set apsADLInfo(${uniqueID}.image${index}.chanD) $image(dynamicattribute.chanD)
                  set apsADLInfo(${uniqueID}.image${index}.vis) $image(dynamicattribute.vis)
                  set apsADLInfo(${uniqueID}.image${index}.calc) $image(dynamicattribute.calc)
                  set apsADLInfo(${uniqueID}.image${index}.clr) 0
                  incr apsADLInfo(${uniqueID}.images)
                  set state hidden
              } else {
                  set state normal
              }
              if {[file exists $image(image\ name)]} {
                  set imageid1 [image create photo -file $image(image\ name)]
              } else {
                  set imageid1 [image create photo -file [file join $apsADLInfo(${uniqueID}.pwd) $image(image\ name)]]
              }
              set imageid2 [image create photo -height $image(object.height) -width $image(object.width)]
              set imageid3 [image create photo -height [expr {$image(object.height) * [image height $imageid1]}] -width [expr {$image(object.width) * [image width $imageid1]}]]
              $imageid3 copy $imageid1 -zoom [expr {round(1.0 * [image width $imageid3] / [image width $imageid1])}] [expr {round(1.0 * [image height $imageid3] / [image height $imageid1])}]
              $imageid2 copy $imageid3 -subsample [expr {round(1.0 * [image width $imageid3] / [image width $imageid2])}] [expr {round(1.0 * [image height $imageid3] / [image height $imageid2])}]
              image delete $imageid1 $imageid3
              eval $canvas create image \
                [expr {$image(object.x) + round($image(object.width) * .5)}] \
                [expr {$image(object.y) + round($image(object.height) * .5)}] \
                -image $imageid2 \
                -tags image$index \
                -state $state
          } "arc" {
              incr i
              array set arc [APSParseADLArcElement [lindex $data $i]]
              set index ""
              if {($arc(dynamicattribute.vis) != "static") || ($arc(dynamicattribute.clr) != "static")} {
                  set index $apsADLInfo(${uniqueID}.arcs)
                  set apsADLInfo(${uniqueID}.arc${index}.chan) $arc(dynamicattribute.chan)
                  set apsADLInfo(${uniqueID}.arc${index}.chanB) $arc(dynamicattribute.chanB)
                  set apsADLInfo(${uniqueID}.arc${index}.chanC) $arc(dynamicattribute.chanC)
                  set apsADLInfo(${uniqueID}.arc${index}.chanD) $arc(dynamicattribute.chanD)
                  set apsADLInfo(${uniqueID}.arc${index}.vis) $arc(dynamicattribute.vis)
                  set apsADLInfo(${uniqueID}.arc${index}.clr) $arc(dynamicattribute.clr)
                  set apsADLInfo(${uniqueID}.arc${index}.calc) $arc(dynamicattribute.calc)
                  set apsADLInfo(${uniqueID}.arc${index}.fill) $arc(basicattribute.fill)
                  set apsADLInfo(${uniqueID}.arc${index}.formatedvalue) 0
                  incr apsADLInfo(${uniqueID}.arcs)
                  set state hidden
              } else {
                  set state normal
              }
              if {$arc(basicattribute.fill) == "outline"} {
                  if {$arc(basicattribute.width) <= 0} {
                      set arc(basicattribute.width) 1
                  }
                  set fillOption "-style arc"
              } else {
                  set fillOption "-fill \#[lindex $colormap(colors) $arc(basicattribute.clr)]"
              }
              if {$arc(basicattribute.style) == "dash"} {
                  set dashOption "-dash {2 2}"
              } else {
                  set dashOption ""
              }
              eval $canvas create arc \
                $arc(object.x) \
                $arc(object.y) \
                [expr {$arc(object.x) + $arc(object.width)}] \
                [expr {$arc(object.y) + $arc(object.height)}] \
                $fillOption \
                -outline \#[lindex $colormap(colors) $arc(basicattribute.clr)] \
                -width $arc(basicattribute.width) \
                -tags arc$index \
                -state $state \
                -start [expr $arc(begin) / 64.0] \
                -extent [expr $arc(path) / 64.0] \
                $dashOption
          } "polyline" {
              incr i
              array set polyline [APSParseADLPolylineElement [lindex $data $i]]
              set index ""
              if {($polyline(dynamicattribute.vis) != "static") || ($polyline(dynamicattribute.clr) != "static")} {
                  set index $apsADLInfo(${uniqueID}.polylines)
                  set apsADLInfo(${uniqueID}.polyline${index}.chan) $polyline(dynamicattribute.chan)
                  set apsADLInfo(${uniqueID}.polyline${index}.chanB) $polyline(dynamicattribute.chanB)
                  set apsADLInfo(${uniqueID}.polyline${index}.chanC) $polyline(dynamicattribute.chanC)
                  set apsADLInfo(${uniqueID}.polyline${index}.chanD) $polyline(dynamicattribute.chanD)
                  set apsADLInfo(${uniqueID}.polyline${index}.vis) $polyline(dynamicattribute.vis)
                  set apsADLInfo(${uniqueID}.polyline${index}.clr) $polyline(dynamicattribute.clr)
                  set apsADLInfo(${uniqueID}.polyline${index}.calc) $polyline(dynamicattribute.calc)
                  set apsADLInfo(${uniqueID}.polyline${index}.formatedvalue) 0
                  incr apsADLInfo(${uniqueID}.polylines)
                  set state hidden
              } else {
                  set state normal
              }
              if {$polyline(basicattribute.width) <= 0} {
                  set polyline(basicattribute.width) 1
              }
              if {$polyline(basicattribute.style) == "dash"} {
                  set dashOption "-dash {2 2}"
              } else {
                  set dashOption ""
              }
              if {[llength $polyline(points)]} {
                  eval $canvas create line \
                    {$polyline(points)} \
                    -fill \#[lindex $colormap(colors) $polyline(basicattribute.clr)] \
                    -width $polyline(basicattribute.width) \
                    -tags polyline$index \
                    -state $state \
                    $dashOption
              }
          } "composite" {
              incr i
              array set composite [APSParseADLCompositeElement [lindex $data $i] $uniqueID]
              set index ""
              if {[string length $composite(dynamicattribute.chan)]} {
                  set index $apsADLInfo(${uniqueID}.composites)
                  set apsADLInfo(${uniqueID}.composite${index}.chan) $composite(dynamicattribute.chan)
                  set apsADLInfo(${uniqueID}.composite${index}.vis) $composite(dynamicattribute.vis)
                  set apsADLInfo(${uniqueID}.composite${index}.formatedvalue) 0
                  incr apsADLInfo(${uniqueID}.composites)
                  frame ${canvas}.composite$index \
                    -bg [$canvas cget -background]
                  place ${canvas}.composite$index \
                    -x $composite(object.x) \
                    -y $composite(object.y) \
                    -width [expr {$composite(object.width) + 1}] \
                    -height [expr {$composite(object.height) + 1}]
                  set apsADLInfo(${uniqueID}.composite${index}.x) $composite(object.x)
                  set apsADLInfo(${uniqueID}.composite${index}.y) $composite(object.y)
                  set apsADLInfo(${uniqueID}.composite${index}.width) [expr {$composite(object.width) + 1}]
                  set apsADLInfo(${uniqueID}.composite${index}.height) [expr {$composite(object.height) + 1}]
              }
          } "cartesian\\ plot" {
              incr i
              array set plot [APSParseADLPlotElement [lindex $data $i]]
              set index $apsADLInfo(${uniqueID}.plots)

              set xVariables ""
              set yVariables ""
              set plotForegroundColors ""
              for {set j 0} {$j < 8} {incr j} {
                  if {[string length $plot(trace${j}.xdata)]} {
                      append xVariables "apsADLInfo(${uniqueID}.plot${index}.${j}.formatedxvalue) "
                      append yVariables "apsADLInfo(${uniqueID}.plot${index}.${j}.formatedyvalue) "
                      append plotForegroundColors "\#[lindex $colormap(colors) $plot(trace${j}.data_clr)] "
                      
                  }
              }
              set apsADLInfo(${uniqueID}.plot${index}.lines) [llength $xVariables]
              for {set j 0} {$j < 8} {incr j} {
                  if {[string length $plot(trace${j}.xdata)]} {
                      set apsADLInfo(${uniqueID}.plot${index}.${j}.xchan) $plot(trace${j}.xdata)
                      set apsADLInfo(${uniqueID}.plot${index}.${j}.ychan) $plot(trace${j}.ydata)
                      set apsADLInfo(${uniqueID}.plot${index}.${j}.formatedxvalue) 0
                      set apsADLInfo(${uniqueID}.plot${index}.${j}.formatedyvalue) 0
                  }
              }
              if {[llength $xVariables]} {
                  incr apsADLInfo(${uniqueID}.plots)
                  frame $canvas.plot$index -bg \#[lindex $colormap(colors) $plot(plotcom.bclr)]
                  APSCartesianPlot .plot \
                    -parent $canvas.plot$index \
                    -backgroundColor \#[lindex $colormap(colors) $plot(plotcom.bclr)] \
                    -foregroundColor \#[lindex $colormap(colors) $plot(plotcom.clr)] \
                    -plotBackgroundColor \#[lindex $colormap(colors) $plot(plotcom.bclr)] \
                    -plotForegroundColors $plotForegroundColors \
                    -axisColor \#[lindex $colormap(colors) $plot(plotcom.clr)] \
                    -title $plot(plotcom.title) \
                    -xVariables $xVariables \
                    -yVariables $yVariables \
                    -packOption "-side top -fill x -padx 30"
                  place ${canvas}.plot$index \
                    -x $plot(object.x) \
                    -y $plot(object.y) \
                    -width $plot(object.width) \
                    -height $plot(object.height)
              }

          } "valuator" {
              incr i
              array set valuator [APSParseADLValuatorElement [lindex $data $i]]
              set index $apsADLInfo(${uniqueID}.scales)
              set apsADLInfo(${uniqueID}.scale${index}.chan) $valuator(control.chan)
              incr apsADLInfo(${uniqueID}.scales)
	      frame ${canvas}.scale$index -bd 0
	      
              scale ${canvas}.scale$index.scale \
                -bg \#[lindex $colormap(colors) $valuator(control.bclr)] \
                -fg \#[lindex $colormap(colors) $valuator(control.clr)] \
                -orient horizontal \
                -resolution $valuator(dPrecision) \
                -from "-5" \
                -to 5 \
                -width [expr {round ($valuator(object.height) / 4.0)}] \
                -font [APSSelectADLFont -height $valuator(object.height) -type scale] \
                -variable apsADLInfo(${uniqueID}.scale${index}.formatedvalue) \
                -command "APSSetADLValue -type scale -uniqueID $uniqueID -index $index"
              place ${canvas}.scale$index \
                -x $valuator(object.x) \
                -y $valuator(object.y) \
                -width $valuator(object.width) \
                -height $valuator(object.height)
              place ${canvas}.scale$index.scale \
                -x -2 \
                -y -7 \
		  -width [expr {$valuator(object.width) + 4}] \
		  -height [expr {$valuator(object.height) + 9}]
          } "text" {
              incr i
              array set text [APSParseADLTextElement [lindex $data $i]]
              set index ""
              if {($text(dynamicattribute.vis) != "static") || ($text(dynamicattribute.clr) != "static")} {
                  set index $apsADLInfo(${uniqueID}.texts)
                  set apsADLInfo(${uniqueID}.text${index}.chan) $text(dynamicattribute.chan)
                  set apsADLInfo(${uniqueID}.text${index}.chanB) $text(dynamicattribute.chanB)
                  set apsADLInfo(${uniqueID}.text${index}.chanC) $text(dynamicattribute.chanC)
                  set apsADLInfo(${uniqueID}.text${index}.chanD) $text(dynamicattribute.chanD)
                  set apsADLInfo(${uniqueID}.text${index}.vis) $text(dynamicattribute.vis)
                  set apsADLInfo(${uniqueID}.text${index}.clr) $text(dynamicattribute.clr)
                  set apsADLInfo(${uniqueID}.text${index}.calc) $text(dynamicattribute.calc)
                  set apsADLInfo(${uniqueID}.text${index}.formatedvalue) 0
                  incr apsADLInfo(${uniqueID}.texts)
                  set state hidden
              } else {
                  set state normal
              }
              set x $text(object.x)
              set y $text(object.y)
              if {$text(align) == "horiz. centered"} {
                  set anchor center
                  set x [expr {abs($text(object.x) + ($text(object.width) / 2.0))}]
                  set y [expr {abs($text(object.y) + ($text(object.height) / 2.0))}]
              } elseif {$text(align) == "horiz. right"} {
                  set anchor e
                  set x [expr {abs($text(object.x) + $text(object.width))}]
                  set y [expr {abs($text(object.y) + ($text(object.height) / 2.0))}]
              } else {
                  set anchor w
                  set x $text(object.x)
                  set y [expr {abs($text(object.y) + ($text(object.height) / 2.0))}]
              }
              $canvas create text $x $y \
                -text $text(textix) \
                -anchor $anchor \
                -fill \#[lindex $colormap(colors) $text(basicattribute.clr)] \
                -font [APSSelectADLFont -height $text(object.height) -type label] \
                -tags text$index \
                -state $state
          } "text\\ update" {
              incr i
              array set textupdate [APSParseADLTextupdateElement [lindex $data $i]]
              if {$textupdate(align) == "horiz. centered"} {
                  set anchor center
              } elseif {$textupdate(align) == "horiz. right"} {
                  set anchor e
              } else {
                  set anchor w
              }

              set index $apsADLInfo(${uniqueID}.labels)
              set apsADLInfo(${uniqueID}.label${index}.chan) $textupdate(monitor.chan)
              set apsADLInfo(${uniqueID}.label${index}.format) $textupdate(format)
              set apsADLInfo(${uniqueID}.label${index}.clrmod) $textupdate(clrmod)
              incr apsADLInfo(${uniqueID}.labels)
              frame ${canvas}.label$index \
                -bg \#[lindex $colormap(colors) $textupdate(monitor.bclr)]
              place ${canvas}.label$index \
                -x $textupdate(object.x) \
                -y $textupdate(object.y) \
                -width $textupdate(object.width) \
                -height $textupdate(object.height)

              label ${canvas}.label${index}.label \
                -bg \#[lindex $colormap(colors) $textupdate(monitor.bclr)] \
                -fg \#[lindex $colormap(colors) $textupdate(monitor.clr)] \
                -textvariable apsADLInfo(${uniqueID}.label${index}.formatedvalue) \
                -font [APSSelectADLFont -height $textupdate(object.height) -type label]
              pack ${canvas}.label${index}.label -anchor $anchor
          } "text\\ entry" {
              incr i
              array set textentry [APSParseADLTextentryElement [lindex $data $i]]
              if {$textentry(align) == "horiz. centered"} {
                  set justify center
              } elseif {$textentry(align) == "horiz. right"} {
                  set justify right
              } else {
                  set justify left
              }

              set index $apsADLInfo(${uniqueID}.entries)
              set apsADLInfo(${uniqueID}.entry${index}.chan) $textentry(control.chan)
              set apsADLInfo(${uniqueID}.entry${index}.format) $textentry(format)
              set apsADLInfo(${uniqueID}.entry${index}.clrmod) $textentry(clrmod)
              incr apsADLInfo(${uniqueID}.entries)
              entry ${canvas}.entry$index \
                -bg \#[lindex $colormap(colors) $textentry(control.bclr)] \
                -fg \#[lindex $colormap(colors) $textentry(control.clr)] \
                -justify $justify \
                -textvariable apsADLInfo(${uniqueID}.entry${index}.formatedvalue)
              place ${canvas}.entry$index \
                -x $textentry(object.x) \
                -y $textentry(object.y) \
                -width $textentry(object.width) \
                -height $textentry(object.height)
              bind ${canvas}.entry$index <Return> "APSSetADLValue -type entry -uniqueID $uniqueID -index $index"
          } "choice\\ button" {
              incr i
              array set choicebutton [APSParseADLChoicebuttonElement [lindex $data $i]]

              set index $apsADLInfo(${uniqueID}.radiobuttons)
              set apsADLInfo(${uniqueID}.radiobutton${index}.chan) $choicebutton(control.chan)
              incr apsADLInfo(${uniqueID}.radiobuttons)

              set apsADLInfo(${uniqueID}.radiobutton${index}.x) $choicebutton(object.x)
              set apsADLInfo(${uniqueID}.radiobutton${index}.y) $choicebutton(object.y)
              set apsADLInfo(${uniqueID}.radiobutton${index}.width) $choicebutton(object.width)
              set apsADLInfo(${uniqueID}.radiobutton${index}.height) $choicebutton(object.height)
              set apsADLInfo(${uniqueID}.radiobutton${index}.clr) $choicebutton(control.clr)
              set apsADLInfo(${uniqueID}.radiobutton${index}.bclr) $choicebutton(control.bclr)
              set apsADLInfo(${uniqueID}.radiobutton${index}.stacking) $choicebutton(stacking)
              set apsADLInfo(${uniqueID}.radiobutton${index}.clrmod) $choicebutton(clrmod)
          } "message\\ button" {
              incr i
              array set messagebutton [APSParseADLMessagebuttonElement [lindex $data $i]]

              set index $apsADLInfo(${uniqueID}.buttons)
              set apsADLInfo(${uniqueID}.button${index}.chan) $messagebutton(control.chan)
              set apsADLInfo(${uniqueID}.button${index}.press_msg) $messagebutton(press_msg)
              incr apsADLInfo(${uniqueID}.buttons)
              button ${canvas}.button$index \
                -bg \#[lindex $colormap(colors) $messagebutton(control.bclr)] \
                -fg \#[lindex $colormap(colors) $messagebutton(control.clr)] \
                -text $messagebutton(label) \
                -font [APSSelectADLFont -height $messagebutton(object.height) -type button] \
                -command "APSSetADLValue -type button -uniqueID $uniqueID -index $index"
              place ${canvas}.button$index \
                -x $messagebutton(object.x) \
                -y $messagebutton(object.y) \
                -width $messagebutton(object.width) \
                -height $messagebutton(object.height)
          } "shell\\ command" {
              incr i
              array set shellcommand [APSParseADLShellcommandElement [lindex $data $i]]
              set index $apsADLInfo(${uniqueID}.commandbuttons)
              incr apsADLInfo(${uniqueID}.commandbuttons)

              menubutton ${canvas}.commandbutton$index \
                -indicatoron 0 \
                -menu ${canvas}.commandbutton${index}.m \
                -state normal \
                -relief raised \
                -bg \#[lindex $colormap(colors) $shellcommand(bclr)] \
                -fg \#[lindex $colormap(colors) $shellcommand(clr)] \
                -text "!" \
                -font [APSSelectADLFont -height $shellcommand(object.height) -type button]
              menu ${canvas}.commandbutton${index}.m \
                -tearoff 0 \
                -bg \#[lindex $colormap(colors) $shellcommand(bclr)] \
                -fg \#[lindex $colormap(colors) $shellcommand(clr)] \
                -font [APSSelectADLFont -height $shellcommand(object.height) -type button]
              place ${canvas}.commandbutton$index \
                -x $shellcommand(object.x) \
                -y $shellcommand(object.y) \
                -width $shellcommand(object.width) \
                -height $shellcommand(object.height)
              for {set j 0} {$j < 8} {incr j} {
                  if {[string length $shellcommand(command${j}.name)]} {
                      ${canvas}.commandbutton${index}.m add command -label $shellcommand(command${j}.label) -command "eval exec $shellcommand(command${j}.name) $shellcommand(command${j}.args)"
                  }
              }
          } "bar" {
              incr i
              array set bar [APSParseADLBarElement [lindex $data $i]]

              set index $apsADLInfo(${uniqueID}.bars)
              set apsADLInfo(${uniqueID}.bar${index}.chan) $bar(monitor.chan)
              set apsADLInfo(${uniqueID}.bar${index}.formatedvalue) 0
              incr apsADLInfo(${uniqueID}.bars)

              APSBar .bar$index \
                -parent $canvas \
                -background \#[lindex $colormap(colors) $bar(monitor.bclr)] \
                -color \#[lindex $colormap(colors) $bar(monitor.clr)] \
                -width $bar(object.width) \
                -height $bar(object.height) \
                -noPack 1 \
                -direction $bar(direction) \
                -label $bar(label)
              place ${canvas}.bar$index \
                -x $bar(object.x) \
                -y $bar(object.y) \
                -width $bar(object.width) \
                -height $bar(object.height)
          } "indicator" {
              incr i
              array set indicator [APSParseADLIndicatorElement [lindex $data $i]]

              set index $apsADLInfo(${uniqueID}.indicators)
              set apsADLInfo(${uniqueID}.indicator${index}.chan) $indicator(monitor.chan)
              set apsADLInfo(${uniqueID}.indicator${index}.formatedvalue) 0
              incr apsADLInfo(${uniqueID}.indicators)

              APSBar .indicator$index \
                -parent $canvas \
                -background \#[lindex $colormap(colors) $indicator(monitor.bclr)] \
                -color \#[lindex $colormap(colors) $indicator(monitor.clr)] \
                -width $indicator(object.width) \
                -height $indicator(object.height) \
                -noPack 1 \
                -direction $indicator(direction) \
                -label $indicator(label)
              place ${canvas}.indicator$index \
                -x $indicator(object.x) \
                -y $indicator(object.y) \
                -width $indicator(object.width) \
                -height $indicator(object.height)
          } "image" {
              incr i
              #not sure what to do with this yet
          } "strip\\ chart" {
              incr i
              array set stripchart [APSParseADLStripChartElement [lindex $data $i]]
              set index $apsADLInfo(${uniqueID}.stripcharts)
              
              set yVariables ""
              set plotForegroundColors ""
              for {set j 0} {$j < 8} {incr j} {
                  if {[string length $stripchart(pen${j}.chan)]} {
                      append yVariables "apsADLInfo(${uniqueID}.stripchart${index}.${j}.formatedyvalue) "
                      append plotForegroundColors "\#[lindex $colormap(colors) $stripchart(pen${j}.clr)] "
                  }
              }
              set apsADLInfo(${uniqueID}.stripchart${index}.lines) [llength $yVariables]
              for {set j 0} {$j < 8} {incr j} {
                  if {[string length $stripchart(pen${j}.chan)]} {
                      set apsADLInfo(${uniqueID}.stripchart${index}.${j}.ychan) $stripchart(pen${j}.chan)
                      set apsADLInfo(${uniqueID}.stripchart${index}.${j}.formatedyvalue) 0
                      if {$stripchart(pen${j}.limits.loprSrc) == "default"} {
                          set ymin $stripchart(pen${j}.limits.loprDefault)
                      } else {
                          set ymin ""
                      }
                      if {$stripchart(pen${j}.limits.hoprSrc) == "default"} {
                          set ymax $stripchart(pen${j}.limits.hoprDefault)
                      } else {
                          set ymax ""
                      }
                  }
              }
             

              incr apsADLInfo(${uniqueID}.stripcharts)
              frame $canvas.stripchart$index -bg \#[lindex $colormap(colors) $stripchart(plotcom.bclr)]
              APSStripChart .stripchart \
                -parent $canvas.stripchart$index \
                -backgroundColor \#[lindex $colormap(colors) $stripchart(plotcom.bclr)] \
                -foregroundColor \#[lindex $colormap(colors) $stripchart(plotcom.clr)] \
                -plotBackgroundColor \#[lindex $colormap(colors) $stripchart(plotcom.bclr)] \
                -plotForegroundColors $plotForegroundColors \
                -axisColor \#[lindex $colormap(colors) $stripchart(plotcom.clr)] \
                -title $stripchart(plotcom.title) \
                -yVariables $yVariables \
                -period $stripchart(period) \
                -units $stripchart(units) \
                -xlabel $stripchart(plotcom.xlabel) \
                -ylabel $stripchart(plotcom.ylabel) \
                -ymin $ymin -ymax $ymax \
                -packOption "-side top -fill x -padx 30"
              place ${canvas}.stripchart$index \
                -x $stripchart(object.x) \
                -y $stripchart(object.y) \
                -width $stripchart(object.width) \
                -height $stripchart(object.height)

          } default {
              puts "Unknown element: [lindex $data $i]"
              incr i
          }
    }

}

proc APSParseADLFile {args} {
    set filename ""
    set uniqueID ""
    set canvas ""
    set macro ""
    APSStrictParseArguments {filename uniqueID canvas macro}

    set fid [open $filename r]
    set adldata [read $fid]
    close $fid

    set adldata [APSPrepADLElementFormat $adldata $macro]
    set data [APSConvertADLElementFormat $adldata]

    global apsADLInfo
    set apsADLInfo(${uniqueID}.colors) ""
    set apsADLInfo(${uniqueID}.canvas) $canvas
    set apsADLInfo(${uniqueID}.scales) 0
    set apsADLInfo(${uniqueID}.labels) 0
    set apsADLInfo(${uniqueID}.texts) 0
    set apsADLInfo(${uniqueID}.rectangles) 0
    set apsADLInfo(${uniqueID}.polylines) 0
    set apsADLInfo(${uniqueID}.ovals) 0
    set apsADLInfo(${uniqueID}.arcs) 0
    set apsADLInfo(${uniqueID}.images) 0
    set apsADLInfo(${uniqueID}.polygons) 0
    set apsADLInfo(${uniqueID}.entries) 0
    set apsADLInfo(${uniqueID}.buttons) 0
    set apsADLInfo(${uniqueID}.radiobuttons) 0
    set apsADLInfo(${uniqueID}.bars) 0
    set apsADLInfo(${uniqueID}.indicators) 0
    set apsADLInfo(${uniqueID}.relatedbuttons) 0
    set apsADLInfo(${uniqueID}.commandbuttons) 0
    set apsADLInfo(${uniqueID}.plots) 0
    set apsADLInfo(${uniqueID}.composites) 0
    set apsADLInfo(${uniqueID}.stripcharts) 0
    set apsADLInfo(${uniqueID}.pwd) [file dirname $filename]
    set apsADLInfo(${uniqueID}.exists) 1

    APSParseADLData $data $uniqueID
}

