%D \module
%D   [      file=x-e-1001,
%D        version=2002.12.01,
%D          title=\EXAMPLE\ GUI Files,
%D       subtitle=Page imposition,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA / Hans Hagen \& Ton Otten}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

%D This is experimental code. Don't change it, don't rely on
%D this interface remaining the same, don't use tricks used
%D here but not described in manuals, so: assume that this
%D file does not exists.

% exavars -> vars

\usemodule[e-0000]

% or setupvariables here

\loadEXArequest{\inputfilename}
\loadEXAmodes  {examodes.tex}

\checkvariables
  [background]
  [text=
   number=,
   gray=.5,
   alternative=1]

\checkvariables
  [imposition]
  [scheme=2UP,
   duplex=,
   mirror=,
   rotate=,
   negative=]

\checkvariables
  [insert]
  [width=\zeropoint,
   empty=]

\checkvariables
  [paper]
  [size=auto,
   orientation=]

\checkvariables
  [page]
  [size=auto,
   orientation=]

\setupoutput[pdftex]

\doif{\getEXAvariable{tex:var:page:size}}{auto}
  {\doifsomething{\mainEXAfilename}
     {\getfiguredimensions[\mainEXAfilename][object=no]
      \expanded
        {\definepapersize
           [auto]
           [width=\figurewidth,
            height=\figureheight]}}}

\setuppapersize
  [\getEXAvariable{tex:var:page:size},\getEXAvariable{tex:var:page:orientation}]
  [\getEXAvariable{tex:var:paper:size},\getEXAvariable{tex:var:paper:orientation}]

\setuparranging
  [\getvariable{imposition}{scheme},%
   \getvariable{imposition}{negative},%
   \getvariable{imposition}{duplex},%
   \getvariable{imposition}{rotate},%
   \getvariable{imposition}{mirror}]

\setMPtextXML{txt}{\getEXAvariable{tex:var:background:text}}

\startreusableMPgraphic{background}
%   color   c ; c := \getEXAvariable{tex:var:background:gray} * white ;
%   numeric a ; a := \getEXAvariable{tex:var:background:alternative} ;
  color   c ; c := \getvariable{background}{gray} * white ;
  numeric a ; a := \getvariable{background}{alternative} ;
  numeric d ; d := 10pt ;
  picture p ; p := textext(\MPstring{txt}) ;
  if     a = 1 : % diagonal
    p := p rotatedaround(center p, angle(OverlayWidth,OverlayHeight)) ;
    draw p xysized (OverlayWidth,OverlayHeight) withcolor c ;
  elseif a = 2 : % horizontal
    draw p xysized (OverlayWidth,OverlayHeight) withcolor c ;
  elseif a = 3 : % vertical
    p := p rotatedaround(center p, 90) ;
    draw p xysized (OverlayWidth,OverlayHeight) withcolor c ;
  elseif a = 4 : % t/b
    setbounds currentpicture to OverlayBox enlarged -d ;
    p := p ysized (OverlayHeight/20) ;
    draw thelabel.llft(p,urcorner currentpicture) withcolor c ;
    draw thelabel.urt (p,llcorner currentpicture) withcolor c ;
    setbounds currentpicture to OverlayBox ;
  elseif a = 5 : % l/r
    setbounds currentpicture to OverlayBox enlarged -d ;
    p := p rotatedaround(center p, 90) ;
    p := p xsized (OverlayWidth/20) ;
    draw thelabel.llft(p,urcorner currentpicture) withcolor c ;
    draw thelabel.urt (p,llcorner currentpicture) withcolor c ;
    setbounds currentpicture to OverlayBox ;
  fi ;
\stopreusableMPgraphic

\setupcolors
  [state=start]

\startMPenvironment[global]
  \usetypescript[palatino][\defaultencoding]
  \setupbodyfont[palatino]
\stopMPenvironment

\startsetups [background text]

  \defineoverlay
    [sometext]
    [\reuseMPgraphic{background}]

\stopsetups

\startsetups [background number]

  \defineoverlay
    [somenumber]
    [{\definedfont[SerifBold at .25\textwidth]\pagenumber}]

\stopsetups

\doifnotEXAvariable{tex:var:background:text}{}
  {\setups[background text]}

\doif{\getEXAvariable{tex:var:background:number}}{yes}
  {\setups[background number]}

\setuplayout
  [width=middle,
   height=middle,
   location=middle]

\setuppagenumbering
  [alternative=doublesided]

\startsetups[no file]

  \setuplayout
    [backspace=2cm,
     topspace=2cm,
     margin=1cm,
     header=1cm,
     headerdistance=.5cm,
     footer=1cm,
     footerdistance=.5cm]

   \setupbackgrounds
     [text]
     [background={sometext,somenumber}]

   \setupbackgrounds
     [header,text,footer]
     [text]
     [frame=on,rulethickness=1pt]

   \dorecurse{32}{\input tufte \wordright{\bf E.R. Tufte} \page}

\stopsetups

\startsetups [a file]

  \setuplayout
    [backspace=0pt,
     topspace=0pt,
     header=0pt,
     footer=0pt]

  \setupbackgrounds
    [page]
    [background=sometext]

  \setupbackgrounds
    [text]
    [background=somenumber]


  \expanded
    {\insertpages
       [\mainEXAfilename]
       [\getEXAvariable{tex:var:insert:empty}]
      %[width=\getEXAvariable{tex:var:insert:width}]}
       [width=\getvariable{insert}{width}]}

\stopsetups

\starttext

\doifelsenothing{\mainEXAfilename}
  {\setups[no file]}
  {\setups[a  file]}

\stoptext
