% Copyright 2006 by Till Tantau
%
% This file may be distributed and/or modified
%
% 1. under the LaTeX Project Public License and/or
% 2. under the GNU Public License.
%
% See the file doc/generic/pgf/licenses/LICENSE for more details.

\ProvidesFileRCS[v\pgfversion] $Header: /cvsroot/pgf/pgf/generic/pgf/libraries/pgflibrarytikzcalendar.code.tex,v 1.12 2007/03/09 17:49:32 tantau Exp $


\pgfutil@usemodule{pgfcalendar}


%
% General options (these options affect all calendars typeset unsind \calendar):
%
% Start, end, and code executed for each current date
%

\tikzoption{dates}{\tikz@lib@cal@parse#1\relax}
\def\tikz@lib@cal@parse#1to#2\relax{%
  \def\tikz@lib@cal@start{#1}
  \def\tikz@lib@cal@end{#2}%
}
\tikzoption{execute at begin day scope}{\expandafter\def\expandafter\tikz@atbegin@day\expandafter{\tikz@atbegin@day#1}}
\tikzoption{execute at end day scope}{%
  \def\pgf@temp{#1}%
  \expandafter\expandafter\expandafter\def%
  \expandafter\expandafter\expandafter\tikz@atend@day%
  \expandafter\expandafter\expandafter{\expandafter\pgf@temp\tikz@atend@day}}
\tikzoption{execute before day scope}{\expandafter\def\expandafter\tikz@before@day\expandafter{\tikz@before@day#1}}
\tikzoption{execute after day scope}{\expandafter\def\expandafter\tikz@after@day\expandafter{\tikz@after@day#1}}

\let\tikz@atbegin@day=\pgfutil@empty
\let\tikz@atend@day=\pgfutil@empty
\let\tikz@before@day=\pgfutil@empty
\let\tikz@after@day=\pgfutil@empty



% 
% Options affecting some calendars:
%

%
% Shifts and skips
%

% Shift between days 

\tikzoption{day xshift}{\def\tikz@lib@cal@xshift{#1}}
\tikzoption{day yshift}{\def\tikz@lib@cal@yshift{#1}}

\def\tikz@lib@cal@yshift{3ex}
\def\tikz@lib@cal@xshift{3.5ex}


% Shift between months

\tikzoption{month xshift}{\def\tikz@lib@cal@month@xshift{#1}}
\tikzoption{month yshift}{\def\tikz@lib@cal@month@yshift{#1}}

\def\tikz@lib@cal@month@yshift{9ex}
\def\tikz@lib@cal@month@xshift{9ex}




% Templates for typesetting days, month, years

\tikzoption{day code}{\def\tikzdaycode{#1}}
\tikzoption{day text}{\def\tikzdaytext{#1}}
\tikzoption{days}{\tikzstyle{every day}+=[#1]}
\tikzstyle{every day}=[anchor=base east]

\tikzoption{month code}{\def\tikzmonthcode{#1}}
\tikzoption{month text}{\def\tikzmonthtext{#1}}
\tikzstyle{every month}=[]

\tikzoption{year code}{\def\tikzyearcode{#1}}
\tikzoption{year text}{\def\tikzyeartext{#1}}
\tikzstyle{every year}=[]

\def\tikzdaycode{\node[name=\pgfcalendarsuggestedname,every day]{\tikzdaytext};}
\def\tikzmonthcode{\node[every month]{\tikzmonthtext};}
\def\tikzyearcode{\node[every year]{\tikzyeartext};}

\def\tikzdaytext{\%d-}
\def\tikzmonthtext{\%mt}
\def\tikzyeartext{\%y0}


% Internal option for storing the "width" of a calendar as a number of
% days 

\tikzoption{tikz@lib@cal@width}{\def\tikz@lib@cal@width{#1}}

\def\tikz@lib@cal@width{1}

%
% Days on a line
%

\tikzstyle{day list downward}=[%
  execute before day scope={
    \ifdate{day of month=1}{\ifdate{equals=\pgfcalendarbeginiso}{}
      {%
        % On first of month, except when first date in calendar.
        \pgfmathsetlength{\pgf@y}{\tikz@lib@cal@month@yshift}%
        \pgftransformyshift{-\pgf@y}
      }%
    }{}%
  },  
  execute after day scope={\pgfmathsetlength{\pgf@y}{\tikz@lib@cal@yshift}\pgftransformyshift{-\pgf@y}},
  tikz@lib@cal@width=1
]
\tikzstyle{day list upward}=[%
  execute before day scope={
    \ifdate{day of month=1}{\ifdate{equals=\pgfcalendarbeginiso}{}
      {%
        % On first of month, except when first date in calendar.
        \pgftransformyshift{\tikz@lib@cal@month@yshift}%
      }%
    }{}%
  },  
  execute after day scope={\pgftransformyshift\tikz@lib@cal@yshift},
  tikz@lib@cal@width=1
]
\tikzstyle{day list right}=[%
  execute before day scope={
    \ifdate{day of month=1}{\ifdate{equals=\pgfcalendarbeginiso}{}
      {%
        % On first of month, except when first date in calendar.
        \pgftransformxshift{\tikz@lib@cal@month@xshift}%
      }%
    }{}%
  },  
  execute after day scope={\pgftransformxshift\tikz@lib@cal@xshift},
  tikz@lib@cal@width=30% not quite right, but close enough in most cases...
]

\tikzstyle{day list left}=[%
  execute before day scope={
    \ifdate{day of month=1}{\ifdate{equals=\pgfcalendarbeginiso}{}
      {%
        % On first of month, except when first date in calendar.
        \pgfmathsetlength{\pgf@x}{\tikz@lib@cal@month@xshift}
        \pgftransformxshift{-\pgf@x}%
      }%
    }{}%
  },  
  execute after day scope={
    \pgfmathsetlength{\pgf@x}{\tikz@lib@cal@xshift}
    \pgftransformxshift{-\pgf@x}%
  },
  tikz@lib@cal@width=30% not quite right, but close enough in most cases...
]



%
% Week list
%

\tikzstyle{week list}=[%
  execute before day scope={%
    \ifdate{day of month=1}{\ifdate{equals=\pgfcalendarbeginiso}{}
      {%
        % On first of month, except when first date in calendar.
        \pgfmathsetlength{\pgf@y}{\tikz@lib@cal@month@yshift}%
        \pgftransformyshift{-\pgf@y}
      }%
    }{}%
  }, 
  execute at begin day scope={%
    \pgfmathsetlength\pgf@x{\tikz@lib@cal@xshift}%
    \pgf@x=\pgfcalendarcurrentweekday\pgf@x%
    \pgftransformxshift{\pgf@x}%
  },
  execute after day scope={%
    \ifdate{Sunday}{%
      \pgfmathsetlength{\pgf@y}{\tikz@lib@cal@yshift}%
      \pgftransformyshift{-\pgf@y}
    }{}%
  },
  tikz@lib@cal@width=7
]



%
% Month list
%

\tikzstyle{month list}=[%
  execute before day scope={%
    \ifdate{day of month=1}{\ifdate{equals=\pgfcalendarbeginiso}{}
      {%
        % On first of month, except when first date in calendar.
        \pgfmathsetlength{\pgf@y}{\tikz@lib@cal@month@yshift}%
        \pgftransformyshift{-\pgf@y}
      }%
    }{}%
    \ifdate{day of month=1}
    {%
      \edef\tikz@lib@cal@month@list@start{\pgfcalendarcurrentweekday}%
    }{}%
    \ifdate{equals=\pgfcalendarbeginiso}%
    {%
      % Computer day of week for start of month
      {%
        \c@pgf@counta=\pgfcalendarcurrentjulian%
        \advance\c@pgf@counta by-\pgfcalendarcurrentday%
        \advance\c@pgf@counta by1\relax%
        \pgfcalendarjuliantoweekday{\c@pgf@counta}{\c@pgf@countb}%
        \xdef\pgf@temp{\the\c@pgf@countb}%
      }%
      \let\tikz@lib@cal@month@list@start=\pgf@temp%
    }{}%
  }, 
  execute at begin day scope={%
    \pgfmathsetlength\pgf@xa{\tikz@lib@cal@xshift}%
    \pgf@xb=\pgfcalendarcurrentday\pgf@xa%
    \advance\pgf@xb by\tikz@lib@cal@month@list@start\pgf@xa%
    \advance\pgf@xb by-\pgf@xa\relax%
    \pgftransformxshift{\pgf@xb}%
  },
  tikz@lib@cal@width=37
]




%
% Month labels
%

\tikzstyle{month label left}=[%
  execute before day scope={\ifdate{day of month=1}{\tikzmonthcode}{}},
  set style={{every month}+=[anchor=base east,xshift=-3.5ex]}
]

\tikzstyle{month label left vertical}=[%
  execute before day scope={\ifdate{day of month=1}{\tikzmonthcode}{}},
  set style={{every month}+=[anchor=base east,xshift=-4.5ex,yshift=2.25ex,rotate=90]}
]
  
\tikzstyle{month label right}=[%
execute before day scope={\ifdate{day of month=1}{%
    {%
      \pgfmathsetlength{\pgf@xa}{\tikz@lib@cal@xshift}%
      \pgftransformxshift{\tikz@lib@cal@width\pgf@xa}%
      \pgftransformxshift{-\pgf@xa}%
      \tikzmonthcode%
    }}{}},
  set style={{every month}+=[anchor=base west,xshift=1ex]}
]

\tikzstyle{month label right vertical}=[%
  execute before day scope={\ifdate{day of month=1}{%
    {%
      \pgfmathsetlength{\pgf@xa}{\tikz@lib@cal@xshift}%
      \pgftransformxshift{\tikz@lib@cal@width\pgf@xa}%
      \pgftransformxshift{-\pgf@xa}%
      \tikzmonthcode%
    }}{}},
  set style={{every month}+=[anchor=base west,xshift=2ex,yshift=2.25ex,rotate=-90]}
]

\tikzstyle{month label above centered}=[%
  execute before day scope={%
    \ifdate{day of month=1}{%
      {
        \pgfmathsetlength{\pgf@xa}{\tikz@lib@cal@xshift}%
        \pgf@xb=\tikz@lib@cal@width\pgf@xa%
        \advance\pgf@xb by-\pgf@xa%
        \pgf@xb=.5\pgf@xb%
        \pgftransformxshift{\pgf@xb}%
        \pgftransformxshift{-1.5ex}%
        \pgfmathsetlength{\pgf@y}{\tikz@lib@cal@yshift}%
        \pgftransformyshift{1.25\pgf@y}
        \tikzmonthcode%
      }
    }{}},
  set style={{every month}+=[anchor=base]}
]
  
\tikzstyle{month label above left}=[%
  execute before day scope={%
    \ifdate{day of month=1}{%
      {
        \pgftransformxshift{-3.25ex}%
        \pgfmathsetlength{\pgf@y}{\tikz@lib@cal@yshift}%
        \pgftransformyshift{1.25\pgf@y}
        \tikzmonthcode%
      }
    }{}},
  set style={{every month}+=[anchor=base west]}
]



\tikzstyle{month label above right}=[%
  execute before day scope={%
    \ifdate{day of month=1}{%
      {
        \pgfmathsetlength{\pgf@xa}{\tikz@lib@cal@xshift}%
        \pgf@xb=\tikz@lib@cal@width\pgf@xa%
        \advance\pgf@xb by-\pgf@xa%
        \pgftransformxshift{\pgf@xb}%
        \pgfmathsetlength{\pgf@y}{\tikz@lib@cal@yshift}%
        \pgftransformyshift{1.25\pgf@y}
        \tikzmonthcode%
      }
    }{}},
  set style={{every month}+=[anchor=base east]}
]
  
\tikzstyle{month label below centered}=[%
  execute before day scope={%
    \ifdate{day of month=1}{%
      {
        \pgfmathsetlength{\pgf@xa}{\tikz@lib@cal@xshift}%
        \pgf@xb=\tikz@lib@cal@width\pgf@xa%
        \advance\pgf@xb by-\pgf@xa%
        \pgf@xb=.5\pgf@xb%
        \pgftransformxshift{\pgf@xb}%
        \pgftransformxshift{-1.5ex}%
        \pgfmathsetlength{\pgf@y}{\tikz@lib@cal@yshift}%
        \pgftransformyshift{-1.25\pgf@y}
        \tikzmonthcode%
      }
    }{}},
  set style={{every month}+=[anchor=base]}
]
  
\tikzstyle{month label below left}=[%
  execute before day scope={%
    \ifdate{day of month=1}{%
      {
        \pgftransformxshift{-3.25ex}%
        \pgfmathsetlength{\pgf@y}{\tikz@lib@cal@yshift}%
        \pgftransformyshift{-1.25\pgf@y}
        \tikzmonthcode%
      }
    }{}},
  set style={{every month}+=[anchor=base west]}
]



%
% Code of the actual \calendar command (tikz.code.tex contains \let\calendar=\tikz@lib@cal@calendar):
%

\def\tikz@lib@cal@calendar{%
  \begingroup%
    \let\tikz@lib@cal@ifs=\pgfutil@empty%
    \tikz@expandcount=1000\relax%
    \tikz@setkeys{name=,at={(0,0)}}%
    \let\%=\pgfcalendarshorthand%
    \tikz@every@style{every calendar}%
    \tikz@lib@cal@scanner%
}

\def\tikz@lib@cal@scanner{%
  \afterassignment\tikz@lib@cal@handle\let\@let@token=%
}

\def\tikz@lib@cal@handle{%
  \let\@next=\tikz@lib@cal@expand%
  \ifx\@let@token;%
    \let\@next=\tikz@lib@cal@stop%
  \else%
    \ifx\@let@token(%)
      \let\@next=\tikz@lib@cal@name%
    \else%
      \ifx\@let@token a%
        \let\@next=\tikz@lib@cal@at%
      \else%
        \ifx\@let@token[%
          \let\@next=\tikz@lib@cal@option%
        \else%
          \ifx\@let@token i%
            \let\@next=\tikz@lib@cal@if%
          \fi%
        \fi%
      \fi%
    \fi%
  \fi%
  \@next%
}
\def\tikz@lib@cal@expand{%
  \advance\tikz@expandcount by -1%
  \ifnum\tikz@expandcount<0\relax%
    \PackageError{tikz}{Giving up on this calendar}{}%
    \let\@next=\tikz@lib@cal@end%
  \else%
    \let\@next=\tikz@lib@cal@@expand
  \fi%
  \@next}

\def\tikz@lib@cal@@expand{\expandafter\tikz@lib@cal@scanner\@let@token}


\def\tikz@lib@cal@name#1){%
  \tikz@setkeys{name=#1}%
  \tikz@lib@cal@scanner%
}
\def\tikz@lib@cal@at t#1(#2){%
  \tikz@setkeys{at={(#2)}}%
  \tikz@lib@cal@scanner%
}
\def\tikz@lib@cal@option#1]{%
  \tikz@setkeys{#1}%
  \tikz@lib@cal@scanner%
}
\def\tikz@lib@cal@if f#1(#2){%
  \pgfutil@ifnextchar[{\tikz@lib@cal@if@opt{#2}}{\tikz@lib@cal@if@code{#2}}}%}

\def\tikz@lib@cal@if@opt#1[#2]{\tikz@lib@cal@if@code{#1}{\tikz@setkeys{#2}}}
\def\tikz@lib@cal@if@code#1#2{%
  \pgfutil@ifnextchar e{\tikz@lib@cal@if@else{#1}{#2}}{\tikz@lib@cal@if@else{#1}{#2}else{}}}

\def\tikz@lib@cal@if@else#1#2else{%
  \pgfutil@ifnextchar[{\tikz@lib@cal@if@else@opt{#1}{#2}}{\tikz@lib@cal@if@else@code{#1}{#2}}}%}
\def\tikz@lib@cal@if@else@opt#1#2[#3]{\tikz@lib@cal@if@else@code{#1}{#2}{\tikz@setkeys{#3}}}
\def\tikz@lib@cal@if@else@code#1#2#3{%
  \expandafter\def\expandafter\tikz@lib@cal@ifs\expandafter{\tikz@lib@cal@ifs\ifdate{#1}{#2}{#3}}%
  \tikz@lib@cal@scanner%
}
\def\tikz@lib@cal@stop{%
    \pgftransformshift{\tikz@node@at}%
    \expandafter\pgfcalendar\expandafter{\tikz@fig@name}{\tikz@lib@cal@start}{\tikz@lib@cal@end}%
    {%
      \tikz@before@day%  
      \scope%
        \tikz@atbegin@day%
        \tikz@lib@cal@ifs%
        \tikzdaycode%
        \tikz@atend@day%
      \endscope%
      \tikz@after@day%  
    }%
  \endgroup%
}

\tikzoption{if}{%
  \let\tikz@lib@cal@scanner@orig=\tikz@lib@cal@scanner%
  \let\tikz@lib@cal@scanner=\relax%
  \tikz@lib@cal@if f#1\relax%
  \let\tikz@lib@cal@scanner=\tikz@lib@cal@scanner@orig}



\endinput
