<?xml version="1.0"?>
<!-- XML Schema for grvl XMLs -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <!-- Elements -->

    <xs:element name="doc">
        <xs:complexType>
            <xs:choice minOccurs="0" maxOccurs="unbounded">
                <xs:element name="guiConfig" type="guiConfigType" />
                <xs:element name="stylesheet" type="stylesheetType" />
                <xs:element name="script" type="scriptType" />
                <xs:element name="keypadMapping" type="keypadMappingType"/>
                <xs:element ref="header"/>
                <xs:element ref="footer"/>
                <xs:element ref="popup" />
                <xs:element ref="prefab" />
                <xs:element ref="customView" />
                <xs:element ref="GridView" />
                <xs:element ref="ListView" />
            </xs:choice>
        </xs:complexType>
    </xs:element>

    <xs:element name="label">
        <xs:complexType>
            <xs:complexContent>
                <xs:extension base="componentType">
                    <xs:attribute name="text" type="xs:string"/>
                    <xs:attribute name="font" type="xs:string"/>
                    <xs:attribute name="color" type="colorType"/>
                    <xs:attribute name="alignment" type="alignmentType" />
                </xs:extension>
            </xs:complexContent>
        </xs:complexType>
    </xs:element>

    <xs:element name="button">
        <xs:complexType>
            <xs:complexContent>
                <xs:extension base="componentType">
                    <xs:attribute name="text" type="xs:string"/>
                    <xs:attribute name="font" type="xs:string"/>
                    <xs:attribute name="image" type="idType"/>
                    <xs:attribute name="activeTextColor" type="colorType"/>
                    <xs:attribute name="icoFont" type="xs:string"/>
                    <xs:attribute name="icoColor" type="colorType"/>
                    <xs:attribute name="activeIcoColor" type="colorType"/>
                    <xs:attribute name="frameColor" type="colorType"/>
                    <xs:attribute name="selectedFrameColor" type="colorType"/>
                    <xs:attribute name="text_top_offset" type="xs:integer"/>
                    <xs:attribute name="image_x" type="xs:integer"/>
                    <xs:attribute name="image_y" type="xs:integer"/>
                    <xs:attribute name="alignment" type="alignmentType" />
                    <xs:attribute name="contentLayout">
                        <xs:simpleType>
                            <xs:restriction base="xs:string">
                                <xs:enumeration value="Overlay" />
                                <xs:enumeration value="Inline"/>
                            </xs:restriction>
                        </xs:simpleType>
                    </xs:atribute>
                    <xs:attribute name="imageTextGap" type="xs:integer"/>
                    <xs:attribute name="padding" type="xs:integer"/>
                    <xs:attribute name="onLongPress" type="functionType"/>
                    <xs:attribute name="onLongPressRepeat" type="functionType"/>
                </xs:extension>
            </xs:complexContent>
        </xs:complexType>
    </xs:element>

    <xs:element name="clock">
        <xs:complexType>
            <xs:complexContent>
                <xs:extension base="componentType">
                    <xs:attribute name="font" type="xs:string"/>
                    <xs:attribute name="alignment" type="alignmentType"/>
                    <xs:attribute name="onRelease" type="functionType"/>
                    <xs:attribute name="format" type="xs:string" />
                </xs:extension>
            </xs:complexContent>
        </xs:complexType>
    </xs:element>

    <xs:element name="slider">
        <xs:complexType>
            <xs:complexContent>
                <xs:extension base="componentType">
                    <xs:attribute name="frameColor" type="colorType"/>
                    <xs:attribute name="selectedFrameColor" type="colorType"/>
                    <xs:attribute name="barColor" type="colorType"/>
                    <xs:attribute name="activeBarColor" type="colorType"/>
                    <xs:attribute name="scrollColor" type="colorType"/>
                    <xs:attribute name="activeScrollColor" type="colorType"/>
                    <xs:attribute name="maxValue" type="xs:integer"/>
                    <xs:attribute name="minValue" type="xs:integer"/>
                    <xs:attribute name="keepBoundaries" type="xs:boolean"/>
                    <xs:attribute name="type">
                        <xs:simpleType>
                            <xs:restriction base="xs:string">
                                <xs:enumeration value="Continuous" />
                                <xs:enumeration value="Discrete"/>
                                <xs:enumeration value="List"/>
                            </xs:restriction>
                        </xs:simpleType>
                    </xs:attribute>
                    <xs:attribute name="division" type="xs:integer"/>
                    <xs:attribute name="font" type="xs:string"/>
                    <xs:attribute name="image" type="idType"/>
                    <xs:attribute name="onValueChange" type="functionType"/>
                </xs:extension>
            </xs:complexContent>
        </xs:complexType>
    </xs:element>

    <xs:element name="header" type="containerType" />
    <xs:element name="popup" type="containerType" />
    <xs:element name="customView" type="screenType" />
    <xs:element name="panel" type="containerType" />
    <xs:element name="footer" type="containerType" />
    <xs:element name="prefab" type="containerType" />
    <xs:element name="div" type="containerType" />
    <xs:element name="separator" type="componentType" />

    <!--  NOTE: GridRow, although clearly a container type, interprets only those attributes in code: -->
    <!-- id, backgroundColor -->
    <xs:element name="GridRow" type="containerType" />

    <xs:element name="GridView">
        <xs:complexType>
            <xs:complexContent>
                <xs:extension base="verticalScrollType">
                    <xs:attribute name="scrollingEnabled" type="xs:boolean" />
                    <xs:attribute name="overscrollColor" type="colorType" />
                    <xs:attribute name="elementWidth" type="xs:integer" />
                    <xs:attribute name="elementHeight" type="xs:integer" />
                    <xs:attribute name="verticalOffset" type="xs:integer" />
                </xs:extension>
            </xs:complexContent>
        </xs:complexType>
    </xs:element>

    <xs:element name="ListView">
        <xs:complexType>
            <xs:complexContent>
                <xs:extension base="verticalScrollType">
                    <xs:attribute name="splitLineColor" type="colorType" />
                    <xs:attribute name="overscrollColor" type="colorType" />
                    <xs:attribute name="scrollIndicatorColor" type="colorType" />
                    <xs:attribute name="verticalGap" type="xs:integer" />
                </xs:extension>
            </xs:complexContent>
        </xs:complexType>
    </xs:element>

    <xs:element name="ListItem">
        <xs:complexType>
            <xs:complexContent>
                <xs:extension base="componentType">
                    <xs:attribute name="text" type="xs:string" />
                    <xs:attribute name="font" type="xs:string" />
                    <xs:attribute name="description" type="xs:string" />
                    <xs:attribute name="descriptionFont" type="xs:string" />
                    <xs:attribute name="activeTextColor" type="colorType" />
                    <xs:attribute name="descriptionColor" type="colorType" />
                    <xs:attribute name="activeDescriptionColor" type="colorType" />
                    <xs:attribute name="image" type="xs:string" />
                    <xs:attribute name="additionalImage" type="xs:string" />
                    <xs:attribute name="roundingImage" type="xs:string" />
                    <xs:attribute name="onLongPress" type="functionType" />
                    <xs:attribute name="onLongPressRepeat" type="functionType" />
                    <xs:attribute name="type">
                        <xs:simpleType>
                            <xs:restriction base="xs:string">
                                <xs:enumeration value="StdListField" />
                                <xs:enumeration value="LeftArrowField"/>
                                <xs:enumeration value="RightArrowField"/>
                                <xs:enumeration value="EmptyField"/>
                                <xs:enumeration value="Dots"/>
                                <xs:enumeration value="DoubleImageField"/>
                                <xs:enumeration value="AlarmField"/>
                            </xs:restriction>
                        </xs:simpleType>
                    </xs:attribute>
                </xs:extension>
            </xs:complexContent>
        </xs:complexType>
    </xs:element>

    <xs:element name="ProgressBar">
        <xs:complexType>
            <xs:complexContent>
                <xs:extension base="componentType">
                    <xs:attribute name="progressBarColor" type="colorType" />
                </xs:extension>
            </xs:complexContent>
        </xs:complexType>
    </xs:element>

    <xs:element name="CircleProgressBar">
        <xs:complexType>
            <xs:complexContent>
                <xs:extension base="componentType">
                    <xs:attribute name="progressBarColor" type="colorType" />
                    <xs:attribute name="startColor" type="colorType" />
                    <xs:attribute name="endColor" type="colorType" />
                    <xs:attribute name="startAngle" type="xs:integer" />
                    <xs:attribute name="endAngle" type="xs:integer" />
                    <xs:attribute name="radius" type="xs:integer" />
                    <xs:attribute name="thickness" type="xs:integer" />
                    <xs:attribute name="staticGradient" type="xs:boolean" />
                </xs:extension>
            </xs:complexContent>
        </xs:complexType>
    </xs:element>

    <xs:element name="SwitchButton">
        <xs:complexType>
            <xs:complexContent>
                <xs:extension base="componentType">
                    <xs:attribute name="text" type="xs:string"/>
                    <xs:attribute name="font" type="xs:string"/>
                    <xs:attribute name="frameColor" type="colorType"/>
                    <xs:attribute name="selectedFrameColor" type="colorType"/>
                    <xs:attribute name="switchColor" type="colorType"/>
                    <xs:attribute name="activeSwitchColor" type="colorType"/>
                    <xs:attribute name="onSwitchON" type="functionType"/>
                    <xs:attribute name="onSwitchOFF" type="functionType"/>
                    <xs:attribute name="onLongPress" type="functionType"/>
                    <xs:attribute name="onLongPressRepeat" type="functionType"/>
                    <xs:attribute name="image" type="idType"/>
                </xs:extension>
            </xs:complexContent>
        </xs:complexType>
    </xs:element>

    <xs:element name="image">
        <xs:complexType>
            <xs:complexContent>
                <xs:extension base="componentType">
                    <xs:attribute name="contentId" type="idType" />
                </xs:extension>
            </xs:complexContent>
        </xs:complexType>
    </xs:element>

    <!-- Complex types -->

    <xs:complexType name="guiConfigType">
        <xs:attribute name="touchRegionModificator" type="xs:integer" />
        <xs:attribute name="dotColor" type="colorType" />
        <xs:attribute name="dotActiveColor" type="colorType" />
        <xs:attribute name="dotDistance" type="xs:integer" />
        <xs:attribute name="dotRadius" type="xs:integer" />
        <xs:attribute name="dotYPos" type="xs:integer" />
        <xs:attribute name="debugDot" type="xs:integer" />
    </xs:complexType>

    <xs:complexType name="stylesheetType" mixed="true">
    </xs:complexType>
    
    <xs:complexType name="scriptType" mixed="true">
        <xs:attribute name="src" type="xs:string" />
    </xs:complexType>

    <xs:complexType name="keypadMappingType">
        <xs:sequence minOccurs="0" maxOccurs="unbounded">
            <xs:element name="key">
                <xs:complexType>
                    <xs:attribute name="id" type="idType" use="required"/>
                    <xs:attribute name="code" type="xs:integer" use="required"/>
                    <xs:attribute name="repeat" type="xs:integer" use="required"/>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="verticalScrollType">
        <xs:complexContent>
            <xs:extension base="screenType">
                <xs:attribute name="overscrollEnabled" type="xs:boolean"/>
                <xs:attribute name="overscrollHeight" type="xs:integer"/>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>

    <xs:complexType name="screenType">
        <xs:complexContent>
            <xs:extension base="containerType">
                <xs:choice minOccurs="0" maxOccurs="unbounded">
                    <xs:element name="key">
                        <xs:complexType>
                            <xs:attribute name="id" type="idType"/>
                            <xs:attribute name="onPress" type="functionType"/>
                            <xs:attribute name="onLongPress" type="functionType"/>
                            <xs:attribute name="onLongPressRepeat" type="functionType"/>
                            <xs:attribute name="onRelease" type="functionType"/>
                        </xs:complexType>
                    </xs:element>
                </xs:choice>
                <xs:attribute name="onSlideToLeft" type="functionType"/>
                <xs:attribute name="onSlideToRight" type="functionType"/>
                <xs:attribute name="onLongPress" type="functionType"/>
                <xs:attribute name="onLongPressRepeat" type="functionType"/>
                <xs:attribute name="collection" type="xs:string"/>
                <xs:attribute name="globalPanelVisible" type="xs:boolean"/>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>

    <xs:complexType name="containerType">
        <xs:complexContent>
            <xs:extension base="componentType">
                <xs:choice minOccurs="0" maxOccurs="unbounded">
                    <xs:element ref="label" />
                    <xs:element ref="header" />
                    <xs:element ref="popup" />
                    <xs:element ref="image" />
                    <xs:element ref="button" />
                    <xs:element ref="customView" />
                    <xs:element ref="clock" />
                    <xs:element ref="slider" />
                    <xs:element ref="GridView" />
                    <xs:element ref="GridRow" />
                    <xs:element ref="ListView" />
                    <xs:element ref="ListItem" />
                    <xs:element ref="ProgressBar" />
                    <xs:element ref="CircleProgressBar" />
                    <xs:element ref="SwitchButton" />
                </xs:choice>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>

    <xs:complexType name="componentType">
        <xs:attribute name="id" type="idType"/>
        <xs:attribute name="x" type="xs:integer" />
        <xs:attribute name="y" type="xs:integer" />
        <xs:attribute name="width" type="xs:integer" />
        <xs:attribute name="height" type="xs:integer" />
        <xs:attribute name="visible" type="xs:boolean" />
        <xs:attribute name="backgroundColor" type="colorType" />
        <xs:attribute name="activeBackgroundColor" type="colorType" />
        <xs:attribute name="foregroundColor" type="colorType" />
        <xs:attribute name="activeForegroundColor" type="colorType" />
        <xs:attribute name="textColor" type="colorType" />
        <xs:attribute name="onClick" type="functionType" />
        <xs:attribute name="onPress" type="functionType" />
        <xs:attribute name="borderType" type="xs:string" />
        <xs:attribute name="borderColor" type="colorType" />
        <xs:attribute name="borderArcRadius" type="xs:float" />
    </xs:complexType>

    <!-- Simple types -->

    <xs:simpleType name="idType">
        <xs:restriction base="xs:string">
            <xs:minLength value="1" />
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="colorType">
        <xs:restriction base="xs:token" >
            <xs:pattern value="(#[\dA-Fa-f]{8})|[\d]+" />
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="functionType">
        <xs:restriction base="xs:string">
            <xs:pattern value="[A-Za-z0-9]+(\([\-A-Za-z'_,\d\s\t\r]*\))?" />
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="alignmentType">
        <xs:restriction base="xs:string">
            <xs:enumeration value="Center" />
            <xs:enumeration value="Left"/>
            <xs:enumeration value="Right"/>
        </xs:restriction>
    </xs:simpleType>
</xs:schema>
