proc addFileObject {args} {
    global adlOutput
    set name newDisplay.adl
    set version 030117
    APSStrictParseArguments {name version}

    append adlOutput "file \{\n"
    append adlOutput "     name=\"$name\"\n"
    append adlOutput "     version=$version\n"
    append adlOutput "\}\n"
}

proc addDisplayObject {args} {
    global adlOutput
    set x 50
    set y 50
    set width 600
    set height 600
    set clr 14
    set bclr 6
    APSStrictParseArguments {x y width height clr bclr}

    append adlOutput "display \{\n"
    append adlOutput "        object \{\n"
    append adlOutput "               x=$x\n"
    append adlOutput "               y=$y\n"
    append adlOutput "               width=$width\n"
    append adlOutput "               height=$height\n"
    append adlOutput "        \}\n"
    append adlOutput "        clr=$clr\n"
    append adlOutput "        bclr=$bclr\n"
    append adlOutput "        cmap=\"\"\n"
    append adlOutput "        gridSpacing=5\n"
    append adlOutput "        gridOn=0\n"
    append adlOutput "        snapToGrid=0\n"
    append adlOutput "\}\n"
}

proc addColorMapObject {args} {
    global adlOutput
    append adlOutput "\"color map\" \{
	ncolors=65
	colors \{
		ffffff,
		ececec,
		dadada,
		c8c8c8,
		bbbbbb,
		aeaeae,
		9e9e9e,
		919191,
		858585,
		787878,
		696969,
		5a5a5a,
		464646,
		2d2d2d,
		000000,
		00d800,
		1ebb00,
		339900,
		2d7f00,
		216c00,
		fd0000,
		de1309,
		be190b,
		a01207,
		820400,
		5893ff,
		597ee1,
		4b6ec7,
		3a5eab,
		27548d,
		fbf34a,
		f9da3c,
		eeb62b,
		e19015,
		cd6100,
		ffb0ff,
		d67fe2,
		ae4ebc,
		8b1a96,
		610a75,
		a4aaff,
		8793e2,
		6a73c1,
		4d52a4,
		343386,
		c7bb6d,
		b79d5c,
		a47e3c,
		7d5627,
		58340f,
		99ffff,
		73dfff,
		4ea5f9,
		2a63e4,
		0a00b8,
		ebf1b5,
		d4db9d,
		bbc187,
		a6a462,
		8b8239,
		73ff6b,
		52da3b,
		3cb420,
		289315,
		1a7309,
	\}
\}
"
}

proc addTextObject {args} {
    global adlOutput
    set x 50
    set y 50
    set width 80
    set height 6
    set clr 14
    set textix TEXT
    set alignCentered 0
    APSStrictParseArguments {x y width height clr textix alignCentered}

    append adlOutput "text \{\n"
    append adlOutput "     object \{\n"
    append adlOutput "            x=$x\n"
    append adlOutput "            y=$y\n"
    append adlOutput "            width=$width\n"
    append adlOutput "            height=$height\n"
    append adlOutput "     \}\n"
    append adlOutput "     \"basic attribute\" \{\n"
    append adlOutput "            clr=$clr\n"
    append adlOutput "     \}\n"
    append adlOutput "     textix=\"${textix}\"\n"
    if {$alignCentered} {
        append adlOutput "     align=\"horiz. centered\"\n"
    }
    append adlOutput "\}\n"
}

proc addOvalObject {args} {
    global adlOutput
    set x 50
    set y 50
    set width 7
    set height 7
    set clr 14
    set vis ""
    set chan ""
    APSStrictParseArguments {x y width height clr vis chan}

    append adlOutput "oval \{\n"
    append adlOutput "     object \{\n"
    append adlOutput "            x=$x\n"
    append adlOutput "            y=$y\n"
    append adlOutput "            width=$width\n"
    append adlOutput "            height=$height\n"
    append adlOutput "     \}\n"
    append adlOutput "     \"basic attribute\" \{\n"
    append adlOutput "            clr=$clr\n"
    append adlOutput "     \}\n"
    if {$chan != ""} {
        append adlOutput "     \"dynamic attribute\" \{\n"
        append adlOutput "            vis=\"${vis}\"\n"
        append adlOutput "            chan=\"${chan}\"\n"
        append adlOutput "     \}\n"
    }
    append adlOutput "\}\n"
}

proc addRectangleObject {args} {
    global adlOutput
    set x 50
    set y 50
    set width 7
    set height 7
    set clr 14
    set alarmPV ""
    set alarmLimit ""
    set fill ""
    set vis ""
    set chan ""
    APSStrictParseArguments {x y width height clr alarmPV alarmLimit fill vis chan}

    append adlOutput "rectangle \{\n"
    append adlOutput "          object \{\n"
    append adlOutput "                 x=$x\n"
    append adlOutput "                 y=$y\n"
    append adlOutput "                 width=$width\n"
    append adlOutput "                 height=$height\n"
    append adlOutput "          \}\n"
    append adlOutput "          \"basic attribute\" \{\n"
    append adlOutput "                 clr=$clr\n"
    if {[llength $fill]} {
        append adlOutput "                 fill=${fill}\n"
    }
    append adlOutput "          \}\n"
    if {$alarmPV != ""} {
        append adlOutput "          \"dynamic attribute\" \{\n"
        if {$alarmLimit != ""} {
            append adlOutput "                 vis=\"calc\"\n"
            append adlOutput "                 calc=\"A<${alarmLimit}\"\n"
            append adlOutput "                 chan=\"${alarmPV}\"\n"
        } elseif {$vis == "static"} {
            append adlOutput "                 clr=\"alarm\"\n"
            append adlOutput "                 chan=\"${alarmPV}\"\n"
        } else {
            append adlOutput "                 clr=\"alarm\"\n"
            append adlOutput "                 vis=\"calc\"\n"
            append adlOutput "                 calc=\"J\"\n"
            append adlOutput "                 chan=\"${alarmPV}\"\n"
        }
        append adlOutput "          \}\n"
    } elseif {[llength $vis] || [llength $chan]} {
        append adlOutput "          \"dynamic attribute\" \{\n"
        if {[llength $vis]} {
            append adlOutput "    vis=$vis\n"
        }
        if {[llength $chan]} {
            append adlOutput "    chan=\"${chan}\"\n"
        }

        append adlOutput "          \}\n"
    }
    append adlOutput "\}\n"
}

proc addPolygonObject {args} {
    global adlOutput
    set x 50
    set y 50
    set width 7
    set height 7
    set clr 14
    set alarmPV ""
    set alarmLimit ""
    set fill ""
    set vis ""
    set chan ""
    APSStrictParseArguments {x y width height clr alarmPV alarmLimit fill vis chan points}

    append adlOutput "polygon \{\n"
    append adlOutput "          object \{\n"
    append adlOutput "                 x=$x\n"
    append adlOutput "                 y=$y\n"
    append adlOutput "                 width=$width\n"
    append adlOutput "                 height=$height\n"
    append adlOutput "          \}\n"
    append adlOutput "          \"basic attribute\" \{\n"
    append adlOutput "                 clr=$clr\n"
    if {[llength $fill]} {
        append adlOutput "                 fill=${fill}\n"
    }
    append adlOutput "          \}\n"
    append adlOutput "          points \{\n"
    append adlOutput "                 ${points}\n"
    append adlOutput "          \}\n"

    if {$alarmPV != ""} {
        append adlOutput "          \"dynamic attribute\" \{\n"
        if {$alarmLimit != ""} {
            append adlOutput "                 vis=\"calc\"\n"
            append adlOutput "                 calc=\"A<${alarmLimit}\"\n"
            append adlOutput "                 chan=\"${alarmPV}\"\n"
        } else {
            append adlOutput "                 clr=\"alarm\"\n"
            append adlOutput "                 vis=\"calc\"\n"
            append adlOutput "                 calc=\"J\"\n"
            append adlOutput "                 chan=\"${alarmPV}\"\n"
        }
        append adlOutput "          \}\n"
    } elseif {[llength $vis] || [llength $chan]} {
        append adlOutput "          \"dynamic attribute\" \{\n"
        if {[llength $vis]} {
            append adlOutput "    vis=$vis\n"
        }
        if {[llength $chan]} {
            append adlOutput "    chan=\"${chan}\"\n"
        }

        append adlOutput "          \}\n"
    }
    append adlOutput "\}\n"
}

proc addLineObject {args} {
    global adlOutput
    set x 50
    set y 50
    set width 7
    set height 7
    set clr 14
    APSStrictParseArguments {x y width height clr}

    append adlOutput "polyline \{\n"
    append adlOutput "          object \{\n"
    append adlOutput "                 x=$x\n"
    append adlOutput "                 y=$y\n"
    append adlOutput "                 width=$width\n"
    append adlOutput "                 height=$height\n"
    append adlOutput "          \}\n"
    append adlOutput "          \"basic attribute\" \{\n"
    append adlOutput "                 clr=$clr\n"
    append adlOutput "          \}\n"
    append adlOutput "          points \{\n"
    append adlOutput "                 (${x},${y})\n"
    append adlOutput "                 ([expr ${x} + $width],[expr ${y} + $height])\n"
    append adlOutput "          \}\n"
    append adlOutput "\}\n"
}

proc addBarObject {args} {
    global adlOutput
    set x 50
    set y 50
    set width 40
    set height 6
    set clr 14
    set bclr 50
    set chan ""
    set label "no decorations"
    set clrmod ""
    set hoprSrc ""
    set hoprDefault ""
    set direction ""
    
    APSStrictParseArguments {x y width height clr bclr chan label clrmod hoprSrc hoprDefault direction}

    append adlOutput "bar \{\n"
    append adlOutput "    object \{\n"
    append adlOutput "           x=$x\n"
    append adlOutput "           y=$y\n"
    append adlOutput "           width=$width\n"
    append adlOutput "           height=$height\n"
    append adlOutput "    \}\n"
    append adlOutput "    monitor \{\n"
    append adlOutput "            chan=\"${chan}\"\n"
    append adlOutput "            clr=$clr\n"
    append adlOutput "            bclr=$bclr\n"
    append adlOutput "    \}\n"
    append adlOutput "    label=\"${label}\"\n"
    if {$direction != "" } {
        append adlOutput "    direction=\"${direction}\"\n"
    }
    if {$clrmod != "" } {
        append adlOutput "    clrmod=\"${clrmod}\"\n"
    }
    append adlOutput "    limits \{\n"
    if {$hoprSrc != ""} {
        append adlOutput "           hoprSrc=\"${hoprSrc}\"\n"
        append adlOutput "           hoprDefault=${hoprDefault}\n"
    }
    append adlOutput "    \}\n"
    append adlOutput "\}\n"
}

proc addRelatedDisplay {args} {
    global adlOutput
    set invisible 0
    set arguments ""
    set clr 14
    set bclr 4
    set label ""
    set display1label ""
    set display2label ""
    set display3label ""
    set display4label ""
    set display5label ""
    set display6label ""
    set display7label ""
    set display8label ""
    set display9label ""
    set display1name ""
    set display2name ""
    set display3name ""
    set display4name ""
    set display5name ""
    set display6name ""
    set display7name ""
    set display8name ""
    set display9name ""
    set display1arguments ""
    set display2arguments ""
    set display3arguments ""
    set display4arguments ""
    set display5arguments ""
    set display6arguments ""
    set display7arguments ""
    set display8arguments ""
    set display9arguments ""
    APSStrictParseArguments {x y width height clr bclr invisible arguments label \
                               display1label display2label display3label display4label display5label \
                               display6label display7label display8label display9label \
                               display1name display2name display3name display4name display5name \
                               display6name display7name display8name display9name \
                               display1arguments display2arguments display3arguments display4arguments display5arguments \
                               display6arguments display7arguments display8arguments display9arguments \
}
    append adlOutput "\"related display\" \{\n"
    append adlOutput "  object \{\n"
    append adlOutput "    x=$x\n"
    append adlOutput "    y=$y\n"
    append adlOutput "    width=$width\n"
    append adlOutput "    height=$height\n"
    append adlOutput "  \}\n"
    for {set i 1} {$i <= 9} {incr i} {
        if {[llength [set display${i}name]]} {
            append adlOutput "  display\[${i}\] \{\n"
            append adlOutput "    label=\"[set display${i}label]\"\n"
            append adlOutput "    name=\"[set display${i}name]\"\n"
            append adlOutput "    args=\"[set display${i}arguments]\"\n"
            append adlOutput "  \}\n"
        }
    }
    append adlOutput "  clr=$clr\n"
    append adlOutput "  bclr=$bclr\n"
    if {$label != ""} {
        append adlOutput "  label=\"$label\"\n"
    }
    if {$invisible} {
        append adlOutput "  visual=\"invisible\"\n"
    }
    append adlOutput "\}\n"
}

proc addShellCommand {args} {
    global adlOutput
    set arguments ""
    set clr 14
    set bclr 4
    set label ""
    set command ""
    APSStrictParseArguments {x y width height clr bclr arguments label command}

    append adlOutput "\"shell command\" \{\n"
    append adlOutput "  object \{\n"
    append adlOutput "    x=$x\n"
    append adlOutput "    y=$y\n"
    append adlOutput "    width=$width\n"
    append adlOutput "    height=$height\n"
    append adlOutput "  \}\n"
    append adlOutput "  command\[0\] \{\n"
    append adlOutput "    label=\"$label\"\n"
    append adlOutput "    name=\"$command\"\n"
    append adlOutput "  \}\n"
    append adlOutput "  clr=$clr\n"
    append adlOutput "  bclr=$bclr\n"
    append adlOutput "  label=\"$label\"\n"
    append adlOutput "\}\n"
}

proc addTextUpdateObject {args} {
    global adlOutput
    set x 50
    set y 50
    set width 7
    set height 7
    set clr 60
    set bclr 14
    set clrmod ""
    set chan ""
    APSStrictParseArguments {x y width height clr bclr clrmod chan}

    append adlOutput "\"text update\" \{\n"
    append adlOutput "          object \{\n"
    append adlOutput "                 x=$x\n"
    append adlOutput "                 y=$y\n"
    append adlOutput "                 width=$width\n"
    append adlOutput "                 height=$height\n"
    append adlOutput "          \}\n"
    append adlOutput "          monitor \{\n"
    append adlOutput "                 chan=\"${chan}\"\n"
    append adlOutput "                 clr=$clr\n"
    append adlOutput "                 bclr=$bclr\n"
    append adlOutput "          \}\n"
    if {$clrmod != "" } {
        append adlOutput "          clrmod=\"${clrmod}\"\n"
    }
    append adlOutput "          limits \{\n"
    append adlOutput "          \}\n"
    append adlOutput "\}\n"
}
