%
% Style (environment) for writing letters
%
% (c) 2004 Holger Schöner hfsch@cs.tu-berlin.de
% with contributions from Stefan Bunzel
%
% Feel free to use as you like. If modified, please remove the above
% copyright notice.
%

%=============================================================================
% Usage:
%
% \environment letterstyle
% \starttext
% \letter{<string>}
% \setdate{<string>}
% \place{<string>}
% \shortsender{<one-line address of sender>}
% \disposal{<orders>}
% \addressee{<address of receiver; use \\ and \| for new lines>}
% % Block with information about sender on the right hand side (name, phone,
% % fax, email, etc.). <key> is set flush right in a very small font (by
% % default, see \keystyle) and specifies the type of information, and <value>
% % is the actual information.
% \senderone{<key>}{<value>}
% \sendertwo{<key>}{<value>}
% ...
% \sendernine{<key>}{<value>}
% \addfirstheaderlayer[corner={top,left},location={bottom,right}]{\framed[width=\overlaywidth,height=\logoboxheight,frame=off]{}}
% \headercontent{<text>}		% Text placed in the header of every page except the first
% \addheaderlayer[corner={top,left},location={bottom,right}]{}
% \footercontent{<text>}		% Text placed in the footer of every page
% \addfooterlayer[corner={bottom,right},location={top,left}]{}
% \subject{<subject>}
% \greeting{<greetings formulation>}
% \signature{<name>}
% \attachments{<text>}
%
% ----------------------------------------------------------------------------
% Modes:
% demo:		Set an example letter specified at the end of this file for
% 		test/demo purposes.
% nofold:	Determines whether folding marks are set or not. By default,
% 		they are printed; using this mode they are left out.
%
% Set modes by using
% 	texexec -pdf --mode=<mode1,mode2,...> <file>
% 	or specify \enablemode[<mode>] in your input (before including the style)
%
% ----------------------------------------------------------------------------
% There are some nasty tricks involved here; e.g. I could not find a cleaner
% way to specify a header which is to be used for only one page and has a
% different height than that of the following ...
%
% I'd be glad about any hints for improvement of this style!
%

\startenvironment letterstyle

%=============================================================================
% Default values

% Standard Dimensions
\def\firstpageheaderheight{9.5cm}
%\def\firstpagetextheight{8.0cm}
\def\normalheaderheight{1.0cm}
\def\logoboxheight{3.4cm}
\def\normaltextheight{27.4cm}
\def\normalfooterheight{1.0cm}
\def\sendervalwidth{5.05cm}
\def\senderkeywidth{1.85cm}

%% Standard Styles
\def\keystyle{\tfxx}
\def\valstyle{}

%=============================================================================
% Initializations

\def\\{\vskip0mm}
\def\|{\vskip1.5ex}

\def\letterdate{}
\def\letterplace{}
\def\lettershortsender{}
\def\letterdisposal{}
\def\letteraddressee{}

\def\lettersenderkeyone{}
\def\lettersendervalone{}
\def\lettersenderkeytwo{}
\def\lettersendervaltwo{}
\def\lettersenderkeythree{}
\def\lettersendervalthree{}
\def\lettersenderkeyfour{}
\def\lettersendervalfour{}
\def\lettersenderkeyfive{}
\def\lettersendervalfive{}
\def\lettersenderkeysix{}
\def\lettersendervalsix{}
\def\lettersenderkeyseven{}
\def\lettersendervalseven{}
\def\lettersenderkeyeight{}
\def\lettersendervaleight{}
\def\lettersenderkeynine{}
\def\lettersendervalnine{}

\def\letterheadertext{}
\def\letterfootertext{}

%=============================================================================

\setuppapersize[A4][A4]
\setuplayout[location=doublesided]
%\setuppagenumbering[alternative=doublesided]
%\setuppagenumbering[alternative={singlesided,doublesided}]
%\setuppagenumbering[alternative=singlesided}]
\setuppagenumbering[location=]

%=============================================================================
%% Header and footer initializations

%% Start of a new letter; heading text is used only in an optional table of contents
\def\letter#1{%
  \setuplayer[normalheaderlayer][state=reset]%
  \setuplayer[allpagefooterlayer][state=reset]%
  \letterhead{#1}%
  \setuplayer[firstheaderlayer][state=reset]%
  }
\definehead[letterhead][title]
\setuphead [letterhead][
  placehead=empty,
  before={%
    \setups{firstpagelayoutsetup}%
    },
  after=,
  header=firstpageheader,
  page=firstpagebreak,
  coupling=title]

%% Making empty pages completely empty (no page numbers):
\definepagebreak
  [firstpagebreak]
  [yes,header,footer,right] % yes instead of emtpy taken from the sources page-ini.tex line 1611

%% Layout of first page
\startsetups firstpagelayoutsetup
  \setuplayout[
    backspace=2.46cm,topspace=1.2cm,
    top=0cm,bottom=0cm,topdistance=0cm,bottomdistance=0cm,
    header=\firstpageheaderheight,footer=\normalfooterheight,headerdistance=0cm,footerdistance=0cm,
    leftedge=0cm,rightedge=0cm,leftedgedistance=0cm,rightedgedistance=0cm,
    leftmargin=1.5cm,rightmargin=1.5cm,leftmargindistance=0.42cm,rightmargindistance=0.42cm,
    width=16.5cm,height=\normaltextheight,
    ]
\stopsetups
% Layout of other pages
\startsetups normalpagelayoutsetup
  \setuplayout[
    backspace=2.46cm,topspace=1.2cm,
    top=0cm,bottom=0cm,topdistance=0cm,bottomdistance=0cm,
    header=\normalheaderheight,footer=\normalfooterheight,headerdistance=0cm,footerdistance=0cm,
    leftedge=0cm,rightedge=0cm,leftedgedistance=0cm,rightedgedistance=0cm,
    leftmargin=1.5cm,rightmargin=1.5cm,leftmargindistance=0.42cm,rightmargindistance=0.42cm,
    width=16.5cm,
    height=\normaltextheight,textheight=\normaltextheight,
    ]
\stopsetups

%% Header for first pages
\definetext[firstpageheader][header][\setups{firstpagesetup}]
\startsetups firstpagesetup
  \setups{firstpageintrosetup}
  \setups{foldingmarkslayersetup}
  \framed[width=\makeupwidth,height=\headerheight,
    background={firstpagelayer,firstheaderlayer},frame=off]
    {}
\stopsetups

%% Header for other pages
\setups{normalpagelayoutsetup}
\appendtoks\setups{normalpagelayoutsetup}\to\everyaftershipout
\startsetups normalheadersetups
  \setups{normalheaderlayersetup}
  \framed[width=\makeupwidth,height=\headerheight,
    background=normalheaderlayer,frame=off]
    {}
\stopsetups
\setupheadertexts[\setups{normalheadersetups}]

%% Footer for all pages
\startsetups allpagefootersetup
  \setups{allpagefooterlayersetup}
  \framed[width=\makeupwidth,height=\footerheight,
    background=allpagefooterlayer,frame=off]
    {}
\stopsetups
\setupfootertexts[\setups{allpagefootersetup}]

%=============================================================================
%% First page header setups

\definelayer[firstpagelayer][width=\makeupwidth,height=\firstpageheaderheight]

\startsetups firstpageintrosetup
  %% Logo frame
  \setlayerframed[firstpagelayer]
    [corner={left,top},location={right,bottom}]
    [height=3.4cm,width=\overlaywidth,frame=off,offset=none]
    {}
  %% Short sender frame
  \doiftextelse{\lettershortsender}{
    \setlayerframed[firstpagelayer]
      [y=3.8cm,location={right,bottom}]
      [height=0.3cm,width=8cm,frame=off,bottomframe=on,offset=none,align={right,lohi}]
      {\tfxx \lettershortsender}}{
    \setlayerframed[firstpagelayer]
      [y=3.8cm,location={right,bottom}]
      [height=0.3cm,width=8cm,frame=off,bottomframe=off,offset=none,align={right,lohi}]
      {\tfxx \lettershortsender}}
  %% Addressee frame
  \setlayerframed[firstpagelayer]
    [y=4.1cm,location={right,bottom}]
    [height=3.6cm,width=8cm,frame=off,offset=none,align={right,top}]
    {
      \doiftextelse{\letterdisposal}{\vskip1.0ex\letterdisposal}{}
      \vskip1.5ex
      \letteraddressee
    }
  %% Sender frame
  \setlayerframed[firstpagelayer]
    [corner={right},y=4.1cm,location={left,bottom}]
    [height=4.6cm,width=\dimexpr(\senderkeywidth+\sendervalwidth),frame=off,offset=none,align={left,top}]	% [height=4.6cm,width=6.4cm,frame=off,offset=none,align={left,top}]
    {\setups{sendertablesetup}}
  %% Date field
  \setlayerframed[firstpagelayer]
    [corner={right},y=8.7cm,location={left,bottom}]
    [height=0.5cm,width=\dimexpr(\senderkeywidth+\sendervalwidth),align={left,top},frame=off,offset=none]	% [height=0.5cm,width=6.4cm,align={left,top},frame=off,offset=none]
    {
      \bTABLE
      \setupTABLE[frame=off]
      \setupTABLE[x][1][width=\senderkeywidth,align=left]	% width: std 1.85cm
      \setupTABLE[x][2][width=\sendervalwidth,align=right]	% width: std 4.55cm
      %\setupTABLE[y][maxheight=1ex]
      \bTR \bTD
        {\keystyle\translate[en=date,de=Datum]:}
      \eTD \bTD
        \valstyle
        \doiftextelse{\letterplace}
          {\letterplace,\translate[en=, de=]\ }{}
        \doiftextelse{\letterdate}{\letterdate}{\currentdate}	% \currentdate[weekday,month,day,{, },year]
      \eTD \eTR
      \eTABLE
    }
\stopsetups

%% Table with sender information
\startsetups sendertablesetup
  \bTABLE
  \setupTABLE[frame=off]
  \setupTABLE[x][1][width=\senderkeywidth,align=left]
  \setupTABLE[x][2][width=\sendervalwidth,align=right]
  \bTR \bTD \strut\doiftextelse{\lettersenderkeyone}  {\keystyle\lettersenderkeyone:}  {} \eTD \bTD \valstyle\lettersendervalone   \eTD \eTR
  \bTR \bTD \strut\doiftextelse{\lettersenderkeytwo}  {\keystyle\lettersenderkeytwo:}  {} \eTD \bTD \valstyle\lettersendervaltwo   \eTD \eTR
  \bTR \bTD \strut\doiftextelse{\lettersenderkeythree}{\keystyle\lettersenderkeythree:}{} \eTD \bTD \valstyle\lettersendervalthree \eTD \eTR
  \bTR \bTD \strut\doiftextelse{\lettersenderkeyfour} {\keystyle\lettersenderkeyfour:} {} \eTD \bTD \valstyle\lettersendervalfour  \eTD \eTR
  \bTR \bTD \strut\doiftextelse{\lettersenderkeyfive} {\keystyle\lettersenderkeyfive:} {} \eTD \bTD \valstyle\lettersendervalfive  \eTD \eTR
  \bTR \bTD \strut\doiftextelse{\lettersenderkeysix}  {\keystyle\lettersenderkeysix:}  {} \eTD \bTD \valstyle\lettersendervalsix   \eTD \eTR
  \bTR \bTD \strut\doiftextelse{\lettersenderkeyseven}{\keystyle\lettersenderkeyseven:}{} \eTD \bTD \valstyle\lettersendervalseven \eTD \eTR
  \bTR \bTD \strut\doiftextelse{\lettersenderkeyeight}{\keystyle\lettersenderkeyeight:}{} \eTD \bTD \valstyle\lettersendervaleight \eTD \eTR
  \bTR \bTD \strut\doiftextelse{\lettersenderkeynine} {\keystyle\lettersenderkeynine:} {} \eTD \bTD \valstyle\lettersendervalnine  \eTD \eTR
  \eTABLE
\stopsetups

%=============================================================================
%% First page header setups

\definelayer[firstheaderlayer][width=\makeupwidth,height=\logoboxheight,width=\overlaywidth]

%=============================================================================
%% Normal page header setups

\definelayer[normalheaderlayer][width=\makeupwidth,height=\normalheaderheight]

\startsetups normalheaderlayersetup
  \setlayerframed[normalheaderlayer]
    [corner={top,left},location={bottom,right}]
    [height=\normalheaderheight,width=\makeupwidth,frame=off,offset=none,align={high,middle}]
    {\letterheadertext}
\stopsetups

%=============================================================================
%% Footer setups

\definelayer[allpagefooterlayer][width=\makeupwidth,height=\footerheight]

\startsetups allpagefooterlayersetup
  \setlayerframed[allpagefooterlayer]
    [corner={right,bottom},location={left,top}]
    [height=\footerheight,width=\makeupwidth,frame=off,offset=none,align={low,middle}]
    {\letterfootertext}
\stopsetups

%=============================================================================
%% Folding marks on left

\startnotmode[nofold]
  \setupbackgrounds[rightpage][page][state=start,background=foldingmarkslayer]
\stopnotmode

\definelayer[foldingmarkslayer][state=repeat]

\startsetups foldingmarkslayersetup
  \setlayer[foldingmarkslayer][y=10.7cm, x=0.5cm,align={lohi,right}]{---}
  \setlayer[foldingmarkslayer][y=14.85cm,x=0.5cm,align={lohi,right}]{--}
  \setlayer[foldingmarkslayer][y=20.2cm, x=0.5cm,align={lohi,right}]{---}
\stopsetups

%=============================================================================
%% Commonly used letter parts

% Date; if not used, current date is inserted
\def\setdate#1{\def\letterdate{#1}}
% Place, for the date line
\def\place#1{\def\letterplace{#1}}
% Short sender information for address window in envelope
\def\shortsender#1{\def\lettershortsender{#1}}
% Order
\def\disposal#1{\def\letterdisposal{#1}}
% Address of receiver of the letter
\def\addressee#1{\def\letteraddressee{{#1}}}	% \let\crlf=\\ 
% Information about sender
\def\senderone#1#2{\def\lettersenderkeyone{#1}\def\lettersendervalone{#2}}
\def\sendertwo#1#2{\def\lettersenderkeytwo{#1}\def\lettersendervaltwo{#2}}
\def\senderthree#1#2{\def\lettersenderkeythree{#1}\def\lettersendervalthree{#2}}
\def\senderfour#1#2{\def\lettersenderkeyfour{#1}\def\lettersendervalfour{#2}}
\def\senderfive#1#2{\def\lettersenderkeyfive{#1}\def\lettersendervalfive{#2}}
\def\sendersix#1#2{\def\lettersenderkeysix{#1}\def\lettersendervalsix{#2}}
\def\senderseven#1#2{\def\lettersenderkeyseven{#1}\def\lettersendervalseven{#2}}
\def\sendereight#1#2{\def\lettersenderkeyeight{#1}\def\lettersendervaleight{#2}}
\def\sendernine#1#2{\def\lettersenderkeynine{#1}\def\lettersendervalnine{#2}}
% Subject line
\def\subject#1{{\bf #1}\vskip2ex}
% Signature: Name, Position
\def\greeting#1{\testpage[8]\vskip1ex{#1}}
% Signature: Name, Position
\def\signature#1{{\let\\=\crlf \vskip8ex\vtop{#1}}}
% Attachments included
\def\attachments#1{{\let\\=\crlf \vskip3ex{{\bf\translate[en=Attachments, de=Anlagen]:}\\ #1}}}

%% First header
%\def\resetfirstheaderlayer{\setuplayer[firstheaderlayer][state=reset]}
\def\addfirstheaderlayer[#1]#2{%
%  \setuplayer[firstheaderlayer][state=repeat]%
  \setlayer[firstheaderlayer][#1]{#2}%
  }

%% Header
\def\letterheadertext{}
\def\headercontent#1{\def\letterheadertext{#1}}
\def\resetheaderlayer{\setuplayer[normalheaderlayer][state=reset]}
\def\addheaderlayer[#1]#2{%
  \def\letterheadertext{}%
  \setuplayer[normalheaderlayer][state=repeat]%
  \setlayer[normalheaderlayer][#1]{#2}%
  }

%% Footer
\def\letterfootertext{\translate[en=page, de=Seite]~\pagenumber/\totalnumberofpages}
\def\footercontent#1{\def\letterfootertext{#1}}
\def\resetfooterlayer{\setuplayer[allpagefooterlayer][state=reset]}
\def\addfooterlayer[#1]#2{%
  \def\letterfootertext{}%
  \setuplayer[allpagefooterlayer][state=repeat]%
  \setlayer[allpagefooterlayer][#1]{#2}%
  }

%=============================================================================
\stopenvironment

%=============================================================================
\startmode[demo]

% Necessary, if you want to print folding marks only on odd pages
\setuppagenumbering[alternative={singlesided,doublesided}]

% \input letterstyle

\starttext
\letter{Example Letter}
%\setdate{<string>}
\place{Berlin}

\shortsender{My Name $\cdot$ My Street 123 $\cdot$ 12345 My Town}
\disposal{confidential}
\addressee{Mr. Example\\ Test Drive 99\| 98765 Other Town}

\senderone{tel.}{+49 30 12345678}
\sendertwo{email}{xyz@abc.com}

%\addfirstheaderlayer[corner={top,left},location={bottom,right}]{\framed[width=\overlaywidth,height=\logoboxheight,frame=off]{}}
\headercontent{\hfill test letter, page \pagenumber}
%\addheaderlayer[corner={top,left},location={bottom,right}]{}
\footercontent{From MyName\hfill To Mr. Example}
%\addfooterlayer[corner={bottom,right},location={top,left}]{}

\subject{What do you think about the Letterstyle?}

Dear Reader,

\dorecurse{11}{\input tufte\par}

\greeting{Best regards,}
\signature{My Name}

\attachments{The Letterstyle}

%-----------------------------------------------------------------------------
% Necessary to suppress emtpy last page in doublesided pagenumbering mode
\installpagebreakhandler{last}{}

\stoptext

\stopmode
