% Context file
% Filename: untitled.tex
% 
% 28-02-2011
% Copyright (c) 2011 BOEDE. All rights reserved.

\usetypescriptfile[type-seravek]
\usetypescript[Seravek]
\setupbodyfont[Seravek,ss,12pt]
\setuppagenumbering[location=]
\setuplayout
	[topspace=2.5cm,
	backspace=1.5cm,
	header=0pt,
	footer=0pt,
	height=middle,
	width=middle]
\mainlanguage[nl]

%\showframe
\setuppapersize[A4][A4,oversized]

\setuplayout
  [width=middle,
   % margin=\dimexpr\backspace-\margindistance\relax
   location=middle]

\definecolor[chap_frame][g=1,t=1,a=12]
\definecolor[sect_frame][b=1,t=1,a=12]
\definecolor[textcolor] [r=1,t=1,a=12]

\startuseMPgraphic{chapter thumb}
  path chap_frame, sect_frame ;  pair pos,a,b ;  picture text ;

  if  \somenamedheadnumber{chapter}{current} > 0 :
	curr_chap := \somenamedheadnumber{chapter}{current} ; 
  else :
	curr_chap := 1 ;
  fi;
  if \somenamedheadnumber{chapter}{last} > 0 :
   	last_chap := \somenamedheadnumber{chapter}{last} ;
  else :
  	last_chap := 1 ;
	  fi;
	  if \somenamedheadnumber{section}{current} > 0 : 
	curr_sect := \somenamedheadnumber{section}{current} ; 
	  else: 
  	curr_sect := 1 ;
  fi;
  if \somenamedheadnumber{section}{last} > 0 :
  	last_sect := \somenamedheadnumber{section}{last};
  else:
	last_sect := 1;
  fi;
  skip   := TextHeight * (curr_chap-1)/last_chap ;
  height := TextHeight/last_chap ;
  sskip  := height*curr_sect/last_sect ;
 
  text   := textext("\ssbfb\textcolor Chapter\enspace\getmarking[chapternumber]");

  StartPage ;
    chap_frame := fullsquare xyscaled(RightMarginWidth,height) ;

    a := ulcorner chap_frame ;
    b := urcorner chap_frame ;

    sect_frame := a--b--(xpart b,ypart b-sskip)--(xpart a,ypart a-sskip)--cycle;

    pos := urcorner Field[Text][RightMarginSeparator] -
           ulcorner chap_frame - (0,skip);

    fill chap_frame shifted pos withcolor \MPcolor{chap_frame};
    unfill sect_frame shifted pos withcolor white ;
    fill sect_frame shifted pos withcolor \MPcolor{sect_frame};
    draw text rotated 90 shifted pos ;
  StopPage ;
\stopuseMPgraphic


\defineoverlay[chapter thumb][\useMPgraphic{chapter thumb}]

\setupbackgrounds[page][background=chapter thumb]


\starttext  \showframe
\dorecurse{6}{\chapter{Test Chapter #1}
  Chapter \headnumber[chapter][current] of
  \headnumber[chapter][last]
  \dorecurse{9}{\section{Test Section}
    Section \headnumber[section][current] of
    \headnumber[section][last] \blank
    \input tufte \par \input knuth \par
  }
}
\chapter 1
\stoptext
