<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:fo="http://www.w3.org/1999/XSL/Format"
                xmlns:q="http://www.xmlmind.com/xmleditor/schema/quickrefcard">

  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="no"/>

  <!--======================================================================
      Parameters
  =======================================================================-->

  <!-- Platform -->
  <xsl:param name="on-mac">0</xsl:param>
  <xsl:param name="xxe-version">4.8.0</xsl:param>

  <!-- page size -->
  <xsl:param name="paper-type">A4</xsl:param>
  <xsl:param name="page-margin-top">10mm</xsl:param>
  <xsl:param name="page-margin-bottom">10mm</xsl:param>
  <xsl:param name="page-margin-left">10mm</xsl:param>
  <xsl:param name="page-margin-right">10mm</xsl:param>
  <xsl:param name="page-footer-size">1.5em</xsl:param>

  <!-- multi column -->
  <xsl:param name="column-count">2</xsl:param>
  <xsl:param name="column-gap">10pt</xsl:param>

  <!-- text-align: justify | start -->
  <xsl:param name="text-align">justify</xsl:param>

  <!-- hyphenate: true | false -->
  <xsl:param name="hyphenate">true</xsl:param>

  <xsl:variable name="paper-width">
    <xsl:choose>
      <xsl:when test="$paper-type = 'Letter'">8.5in</xsl:when>
      <xsl:otherwise>210mm</xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:variable name="paper-height">
    <xsl:choose>
      <xsl:when test="$paper-type = 'Letter'">11in</xsl:when>
      <xsl:otherwise>297mm</xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <!--======================================================================
      Attribute Sets
  =======================================================================-->

  <xsl:attribute-set name="quickrefcard">
    <xsl:attribute name="font-family">sans-serif</xsl:attribute>
    <xsl:attribute name="font-size">9pt</xsl:attribute>
  </xsl:attribute-set>

  <xsl:attribute-set name="page">
    <xsl:attribute name="page-width"><xsl:value-of 
      select="$paper-height"/></xsl:attribute>
    <xsl:attribute name="page-height"><xsl:value-of 
      select="$paper-width"/></xsl:attribute>
    <xsl:attribute name="margin-top"><xsl:value-of 
      select="$page-margin-top"/></xsl:attribute>
    <xsl:attribute  name="margin-bottom"><xsl:value-of 
      select="$page-margin-bottom"/></xsl:attribute>
    <xsl:attribute name="margin-left"><xsl:value-of 
      select="$page-margin-left"/></xsl:attribute>
    <xsl:attribute name="margin-right"><xsl:value-of 
      select="$page-margin-right"/></xsl:attribute>
  </xsl:attribute-set>

  <xsl:attribute-set name="page-footer">
    <xsl:attribute name="border-before-width">1px</xsl:attribute>
    <xsl:attribute name="border-before-style">solid</xsl:attribute>
    <xsl:attribute name="padding">0.25em</xsl:attribute>
    <xsl:attribute name="font-family">serif</xsl:attribute>
    <xsl:attribute name="color">gray</xsl:attribute>
    <xsl:attribute name="font-size">0.83em</xsl:attribute>
    <xsl:attribute name="text-align">center</xsl:attribute>
  </xsl:attribute-set>

  <xsl:attribute-set name="title">
    <xsl:attribute name="font-family">serif</xsl:attribute>
    <xsl:attribute name="font-size">1.05em</xsl:attribute>
    <xsl:attribute name="font-weight">bold</xsl:attribute>
    <xsl:attribute name="color">#00599B</xsl:attribute>
    <xsl:attribute name="background-color">#E0F0FF</xsl:attribute>
    <xsl:attribute name="padding">0.25em</xsl:attribute>
    <xsl:attribute name="space-before">1em</xsl:attribute>
    <xsl:attribute name="space-after">0.5em</xsl:attribute>
    <xsl:attribute name="keep-with-next.within-column">always</xsl:attribute>
    <xsl:attribute name="keep-together.within-column">always</xsl:attribute>
  </xsl:attribute-set>

  <xsl:attribute-set name="note">
    <xsl:attribute 
      name="provisional-distance-between-starts">2em</xsl:attribute>
    <xsl:attribute name="provisional-label-separation">0.5em</xsl:attribute>
    <xsl:attribute name="start-indent">2em</xsl:attribute>
    <xsl:attribute name="end-indent">2em</xsl:attribute>
    <xsl:attribute name="space-before">0.5em</xsl:attribute>
    <xsl:attribute name="space-after">0.5em</xsl:attribute>
    <xsl:attribute name="background-color">#FFFFEE</xsl:attribute>
    <xsl:attribute name="border">0.5pt solid #EEEEDD</xsl:attribute>
    <xsl:attribute name="padding">0.25em</xsl:attribute>
  </xsl:attribute-set>

  <xsl:attribute-set name="item">
    <xsl:attribute 
      name="provisional-distance-between-starts">9em</xsl:attribute>
    <xsl:attribute name="provisional-label-separation">0.5em</xsl:attribute>
  </xsl:attribute-set>

  <xsl:attribute-set name="para">
    <xsl:attribute 
      name="hyphenate"><xsl:value-of select="$hyphenate"/></xsl:attribute>
    <xsl:attribute 
      name="text-align"><xsl:value-of select="$text-align"/></xsl:attribute>
  </xsl:attribute-set>

  <xsl:attribute-set name="user-input">
    <xsl:attribute name="font-family">sans-serif</xsl:attribute>
    <xsl:attribute name="font-size">0.83em</xsl:attribute>
  </xsl:attribute-set>

  <xsl:attribute-set name="key" use-attribute-sets="user-input">
  </xsl:attribute-set>

  <xsl:attribute-set name="char" use-attribute-sets="user-input">
    <xsl:attribute name="font-family">monospace</xsl:attribute>
  </xsl:attribute-set>

  <xsl:attribute-set name="mouse" use-attribute-sets="user-input">
  </xsl:attribute-set>

  <xsl:attribute-set name="dnd-drop" use-attribute-sets="user-input">
  </xsl:attribute-set>

  <xsl:attribute-set name="emphasis">
    <xsl:attribute name="font-style">italic</xsl:attribute>
  </xsl:attribute-set>

  <xsl:attribute-set name="gui">
    <xsl:attribute name="font-weight">bold</xsl:attribute>
  </xsl:attribute-set>

  <xsl:attribute-set name="literal">
    <xsl:attribute name="font-family">monospace</xsl:attribute>
    <xsl:attribute name="font-size">0.83em</xsl:attribute>
  </xsl:attribute-set>

  <!--======================================================================
      Templates
  =======================================================================-->

  <xsl:template match="q:quickrefcard">
    <fo:root xsl:use-attribute-sets="quickrefcard">
      <xsl:call-template name="make-layout-master-set"/>

      <fo:page-sequence master-reference="all-pages">
        <fo:static-content flow-name="page-footer">
          <fo:block xsl:use-attribute-sets="page-footer">
            <xsl:text>XMLmind XML Editor v</xsl:text>
            <xsl:value-of select="$xxe-version" />
            <xsl:text> - Quick Reference Card (</xsl:text>
            <xsl:value-of select="$paper-type" />
            <xsl:if test="$on-mac = 1">
              <xsl:text>, Mac</xsl:text>
            </xsl:if>
            <xsl:text>) - </xsl:text>
            <fo:page-number/><xsl:text> / 2</xsl:text>
          </fo:block>
        </fo:static-content>

        <fo:flow flow-name="xsl-region-body">
          <xsl:apply-templates/>
        </fo:flow>
      </fo:page-sequence>
    </fo:root>
  </xsl:template>

  <xsl:template name="make-layout-master-set">
    <fo:layout-master-set>
      <fo:simple-page-master master-name="all-pages"
                             xsl:use-attribute-sets="page">
        <fo:region-body column-count="{$column-count}"
                        column-gap="{$column-gap}"
                        margin-bottom="{$page-footer-size}"/>
        <fo:region-after region-name="page-footer"
                         extent="{$page-footer-size}"
                         display-align="after"/>
      </fo:simple-page-master>
    </fo:layout-master-set>
  </xsl:template>

  <xsl:template match="q:section">
    <fo:block>
      <xsl:apply-templates/>
    </fo:block>
  </xsl:template>

  <xsl:template match="q:title">
    <fo:block xsl:use-attribute-sets="title">
      <xsl:apply-templates/>
    </fo:block>
  </xsl:template>

  <xsl:template match="q:note">
    <fo:list-block xsl:use-attribute-sets="note">
      <fo:list-item>
        <fo:list-item-label end-indent="label-end()">
          <fo:block>
            <fo:external-graphic src="url(quickrefcard/note.gif)" />
          </fo:block>
        </fo:list-item-label>

        <fo:list-item-body start-indent="body-start()">
          <fo:block><xsl:apply-templates/></fo:block>
        </fo:list-item-body>
      </fo:list-item>
    </fo:list-block>
  </xsl:template>

  <xsl:template match="q:para">
    <fo:block xsl:use-attribute-sets="para">
      <xsl:apply-templates/>
    </fo:block>
  </xsl:template>

  <xsl:template match="q:item">
    <xsl:if test="$on-mac = 0 or 
                 ($on-mac != 0 and (not(./q:on-mac) or ./q:on-mac/*))">
      <fo:list-block xsl:use-attribute-sets="item">
        <fo:list-item>
          <fo:list-item-label end-indent="label-end()">
            <fo:block>
              <xsl:choose>
                <xsl:when test="$on-mac != 0 and ./q:on-mac">
                  <xsl:apply-templates select="./q:on-mac/q:key|
                                               ./q:on-mac/q:char|
                                               ./q:on-mac/q:press-1|
                                               ./q:on-mac/q:press-2|
                                               ./q:on-mac/q:press-3|
                                               ./q:on-mac/q:drag-1|
                                               ./q:on-mac/q:drag-2|
                                               ./q:on-mac/q:drag-3|
                                               ./q:on-mac/q:release-1|
                                               ./q:on-mac/q:release-2|
                                               ./q:on-mac/q:release-3|
                                               ./q:on-mac/q:click-1|
                                               ./q:on-mac/q:click-2|
                                               ./q:on-mac/q:click-3|
                                               ./q:on-mac/q:double-click-1|
                                               ./q:on-mac/q:double-click-2|
                                               ./q:on-mac/q:double-click-3|
                                               ./q:on-mac/q:triple-click-1|
                                               ./q:on-mac/q:triple-click-2|
                                               ./q:on-mac/q:triple-click-3|
                                               ./q:on-mac/q:wheel|
                                               ./q:on-mac/q:dnd-drop"/>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:apply-templates select="./q:key|
                                               ./q:char|
                                               ./q:press-1|
                                               ./q:press-2|
                                               ./q:press-3|
                                               ./q:drag-1|
                                               ./q:drag-2|
                                               ./q:drag-3|
                                               ./q:release-1|
                                               ./q:release-2|
                                               ./q:release-3|
                                               ./q:click-1|
                                               ./q:click-2|
                                               ./q:click-3|
                                               ./q:double-click-1|
                                               ./q:double-click-2|
                                               ./q:double-click-3|
                                               ./q:triple-click-1|
                                               ./q:triple-click-2|
                                               ./q:triple-click-3|
                                               ./q:wheel|
                                               ./q:dnd-drop"/>
                </xsl:otherwise>
              </xsl:choose>
              <xsl:if test="@important">
                <xsl:text>&#x00A0;</xsl:text>
                <fo:external-graphic src="url(quickrefcard/important.gif)" />
              </xsl:if>
            </fo:block>
          </fo:list-item-label>

          <fo:list-item-body start-indent="body-start()">
            <fo:block>
              <xsl:apply-templates select="./q:para"/>
            </fo:block>
          </fo:list-item-body>
        </fo:list-item>
      </fo:list-block>
    </xsl:if>
  </xsl:template>

  <xsl:template match="q:key">
    <fo:inline xsl:use-attribute-sets="key">
      <xsl:if test="preceding-sibling::q:key|preceding-sibling::q:char">
        <xsl:text>&#x00A0;</xsl:text>
      </xsl:if>
      <xsl:call-template name="process-modifiers"/>
      <xsl:value-of select="."/>
    </fo:inline>
  </xsl:template>

  <xsl:template match="q:char">
    <fo:inline xsl:use-attribute-sets="char">
      <xsl:if test="preceding-sibling::q:key|preceding-sibling::q:char">
        <xsl:text>&#x00A0;</xsl:text>
      </xsl:if>
      <xsl:value-of select="."/>
    </fo:inline>
  </xsl:template>

  <xsl:template match="q:press-1|
                       q:press-2|
                       q:press-3|
                       q:drag-1|
                       q:drag-2|
                       q:drag-3|
                       q:release-1|
                       q:release-2|
                       q:release-3|
                       q:click-1|
                       q:click-2|
                       q:click-3|
                       q:double-click-1|
                       q:double-click-2|
                       q:double-click-3|
                       q:triple-click-1|
                       q:triple-click-2|
                       q:triple-click-3|
                       q:wheel">
    <fo:inline xsl:use-attribute-sets="mouse">
      <xsl:call-template name="process-modifiers"/>
      <xsl:value-of select="local-name(.)"/>
    </fo:inline>
  </xsl:template>

  <xsl:template name="process-modifiers">
    <xsl:choose>
      <xsl:when test="@alt and @shift">Alt+Shift-</xsl:when>
      <xsl:when test="@mod and @shift">
        <xsl:choose>
          <xsl:when test="$on-mac != 0">Cmd+Shift-</xsl:when>
          <xsl:otherwise>Ctrl+Shift-</xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:when test="@ctrl and @shift">Ctrl+Shift-</xsl:when>
      <xsl:when test="@alt">Alt-</xsl:when>
      <xsl:when test="@mod">
        <xsl:choose>
          <xsl:when test="$on-mac != 0">Cmd-</xsl:when>
          <xsl:otherwise>Ctrl-</xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:when test="@ctrl">Ctrl-</xsl:when>
      <xsl:when test="@shift">Shift-</xsl:when>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="q:dnd-drop">
    <fo:inline xsl:use-attribute-sets="dnd-drop">Drop</fo:inline>
  </xsl:template>

  <xsl:template match="q:emphasis">
    <fo:inline xsl:use-attribute-sets="emphasis">
      <xsl:apply-templates/>
    </fo:inline>
  </xsl:template>

  <xsl:template match="q:gui">
    <fo:inline xsl:use-attribute-sets="gui">
      <xsl:apply-templates/>
    </fo:inline>
  </xsl:template>

  <xsl:template match="q:literal">
    <fo:inline xsl:use-attribute-sets="literal">
      <xsl:apply-templates/>
    </fo:inline>
  </xsl:template>

</xsl:stylesheet>
