%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -*- Mode: Latex -*- %%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% aabbdb.cls -- A class for address and phone list from the BBDB database.
%% 
%% Author          : Anders Andersen
%% Created On      : Tue Feb 27 13:44:33 1996
%% Last Modified By: Anders Andersen
%% Last Modified On: Fri Feb 27 11:23:57 1998
%% Status          : Unknown, Use with caution!
%% 
%% Unless other notices are present in any part of this file
%% explicitly claiming copyrights for other people and/or 
%% organizations, the contents of this file is fully copyright 
%% (C) 1996, 1998 NORUT IT, all rights reserved. 
%% 
%% Permission is granted to make and distribute verbatim copies 
%% of this document provided that the copyright notice and this
%% permission notice are preserved on all copies.
%% 
%% Permission is granted to copy and distribute modified versions
%% of this document under the conditions for verbatim copying, 
%% provided that the entire resulting derived work is distributed 
%% under the terms of a permission notice identical to this one.
%% 
%% If any other present copyright notices interfere with the 
%% copyright claims above, these claims may be partially overruled 
%% by those notices.
%% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


% --------------------------------------%--------------------------------------
% ---------------------------- Identification part ----------------------------
% --------------------------------------%--------------------------------------

\RequirePackage{rcs}
\RequirePackage{ifthen}

\RCS$Date: 2002/07/09 23:45:44 $
\RCS$RCSfile: aabbdb.cls,v $
\RCS$Revision: 1.1 $
\RCS$State: Exp $
\RCS$Author: aa $

\def\aabasenamex#1.#2,#3{#1}
\def\aafiletypex#1.#2,#3{#2}
\def\aabasename{\expandafter\aabasenamex\RCSRCSfile}
\ifthenelse{\equal{\@clsextension}{\expandafter\aafiletypex\RCSRCSfile}}{%
  \def\aafiletype{Class}}{%
  \def\aafiletype{Package}}
\ifthenelse{\equal{OK}{\RCSState}}{%
  \def\aastate{ }}{%
  \def\aastate{ (\RCSState) }}
\typeout{\aafiletype: `\aabasename' v\RCSRevision\aastate
  <\RCSRawDate> (\RCSAuthor)}

\NeedsTeXFormat{LaTeX2e}
\ifthenelse{\equal{\aafiletype}{Class}}{%
  \ProvidesClass{\aabasename}[\RCSRawDate]}{%
  \ProvidesPackage{\aabasename}[\RCSRawDate]}


% --------------------------------------%--------------------------------------
% -------------------------------- Start code ---------------------------------
% --------------------------------------%--------------------------------------

% Load some packages
\RequirePackage{calc}
\RequirePackage{ifthen}

% This is true if we are using marks
\newboolean{@aamarks}
\setboolean{@aamarks}{false}

% This is true if left and right margins should have the same size
\newboolean{@aaeqmargs}
\setboolean{@aaeqmargs}{false}

% The width and height of the page when using page type custom
\newlength{\@aawidth}
\newlength{\@aaheight}

% Margins
\newlength{\@aalmargin}
\newlength{\@aatmargin}
\newlength{\@aarmargin}
\newlength{\@aabmargin}
\newlength{\@aahheight}
\newlength{\@aahsep}
\newlength{\@aafheight}
\newlength{\@aafskip}

% --------------------------------------%--------------------------------------
% -------------------------- Declaration of options ---------------------------
% --------------------------------------%--------------------------------------

% Paper size options
\DeclareOption{custom}{%
  \def\@aaptype{custom}
  \ExecuteOptions{small}}
\DeclareOption{filofax}{%
  \def\@aaptype{custom}%
  \setlength{\@aawidth}{95mm}%
  \setlength{\@aaheight}{170mm}%
  \ExecuteOptions{medium}}
\DeclareOption{pocket}{%
  \def\@aaptype{custom}%
  \setlength{\@aawidth}{80mm}%
  \setlength{\@aaheight}{120mm}%
  \ExecuteOptions{small}}
\DeclareOption{mini}{%
  \def\@aaptype{custom}%
  \setlength{\@aawidth}{66mm}%
  \setlength{\@aaheight}{103mm}%
  \ExecuteOptions{smaller}}
\DeclareOption{aplan}{%
  \def\@aaptype{custom}%
  \setlength{\@aawidth}{91mm}%
  \setlength{\@aaheight}{167mm}%
  \ExecuteOptions{medium}}
\DeclareOption{a4paper}{%
  \def\@aaptype{A4}%
  \ExecuteOptions{large}}
\DeclareOption{A4}{%
  \def\@aaptype{A4}%
  \ExecuteOptions{large}}
\DeclareOption{a5paper}{%
  \def\@aaptype{A5}%
  \ExecuteOptions{medium}}
\DeclareOption{A5}{%
  \def\@aaptype{A5}%
  \ExecuteOptions{medium}}
\DeclareOption{b5paper}{%
  \def\@aaptype{B5}%
  \ExecuteOptions{medium}}
\DeclareOption{B5}{%
  \def\@aaptype{B5}%
  \ExecuteOptions{medium}}
\DeclareOption{letterpaper}{%
  \def\@aaptype{USletter}%
  \ExecuteOptions{large}}
\DeclareOption{USletter}{%
  \def\@aaptype{USletter}%
  \ExecuteOptions{large}}
\DeclareOption{legalpaper}{%
  \def\@aaptype{USlegal}%
  \ExecuteOptions{large}}
\DeclareOption{USlegal}{%
  \def\@aaptype{USlegal}%
  \ExecuteOptions{large}}
\DeclareOption{executivepaper}{%
  \def\@aaptype{USexecutive}%
  \ExecuteOptions{large}}
\DeclareOption{USexecutive}{%
  \def\@aaptype{USexecutive}%
  \ExecuteOptions{large}}

% Language option
\DeclareOption{norsk}{%
  \def\@aaname{Navn:~}
  \def\@aaddra{Adresse:~}
  \def\@aaddrb{~}
  \def\@aaddrc{~}
  \def\@aaphoneh{Priv:~}
  \def\@aaphonew{Arb:~}
  \def\@aaphonef{Fax:~}
  \def\@aaphonee{Mob:~}
  \def\@aaplh{\@aaphoneh}
  \def\@aaplw{\@aaphonew}
  \def\@aaplf{\@aaphonef}
  \def\@aaple{\@aaphonee}
  \def\@aacopy{{\footnotesize \copyright~\textsc{Anders Andersen}}}}
\DeclareOption{english}{%
  \def\@aaname{Name:~}
  \def\@aaddra{Address:~}
  \def\@aaddrb{~}
  \def\@aaddrc{~}
  \def\@aaphoneh{Home:~}
  \def\@aaphonew{Work:~}
  \def\@aaphonef{Fax:~}
  \def\@aaphonee{Mob:~}
  \def\@aaplh{\@aaphoneh}
  \def\@aaplw{\@aaphonew}
  \def\@aaplf{\@aaphonef}
  \def\@aaple{\@aaphonee}
  \def\@aacopy{{\footnotesize \copyright~\textsc{Anders Andersen}}}}

% Layout options
\DeclareOption{marks}{\setboolean{@aamarks}{true}}
\DeclareOption{eqmargins}{\setboolean{@aaeqmargs}{true}}

% Size options
\DeclareOption{smaller}{%
  % Text size
  \def\@aarp{aa9pt}
  % Margins
  \setlength{\@aalmargin}{7mm}
  \setlength{\@aatmargin}{2mm}
  \setlength{\@aarmargin}{3mm}
  \setlength{\@aabmargin}{2mm}
  \setlength{\@aahheight}{6.5pt}
  \setlength{\@aahsep}{3pt}
  \setlength{\@aafheight}{6pt}
  \setlength{\@aafskip}{9pt}}
\DeclareOption{small}{%
  % Text size
  \PassOptionsToClass{10pt}{article}
  % Margins
  \setlength{\@aalmargin}{10mm}
  \setlength{\@aatmargin}{5mm}
  \setlength{\@aarmargin}{5mm}
  \setlength{\@aabmargin}{5mm}
  \setlength{\@aahheight}{7.5pt}
  \setlength{\@aahsep}{5pt}
  \setlength{\@aafheight}{7.5pt}
  \setlength{\@aafskip}{12.5pt}}
\DeclareOption{medium}{%
  % Text size
  \PassOptionsToClass{11pt}{article}
  % Margins
  \setlength{\@aalmargin}{15mm}
  \setlength{\@aatmargin}{10mm}
  \setlength{\@aarmargin}{10mm}
  \setlength{\@aabmargin}{10mm}
  \setlength{\@aahheight}{8pt}
  \setlength{\@aahsep}{5pt}
  \setlength{\@aafheight}{8pt}
  \setlength{\@aafskip}{13pt}}
\DeclareOption{large}{%
  % Text size
  \PassOptionsToClass{12pt}{article}
  % Margins
  \setlength{\@aalmargin}{25mm}
  \setlength{\@aatmargin}{15mm}
  \setlength{\@aarmargin}{15mm}
  \setlength{\@aabmargin}{15mm}
  \setlength{\@aahheight}{9pt}
  \setlength{\@aahsep}{5pt}
  \setlength{\@aafheight}{9pt}
  \setlength{\@aafskip}{14pt}}

% Options to article
\DeclareOption*{\PassOptionsToClass{\CurrentOption}{article}}


% --------------------------------------%--------------------------------------
% --------------------------- Execution of options ----------------------------
% --------------------------------------%--------------------------------------

\ExecuteOptions{mini}
\ExecuteOptions{english}
\ProcessOptions


% --------------------------------------%--------------------------------------
% --------------------------- Class/Package loading ---------------------------
% --------------------------------------%--------------------------------------

\LoadClass{article}
\RequirePackage{vmargin}
\@ifundefined{@aarp}{}{\RequirePackage{\@aarp}}


% --------------------------------------%--------------------------------------
% --------------------------------- Main code ---------------------------------
% --------------------------------------%--------------------------------------

%
% Calculate and define some values
%

% Font selectors
\def\@aafname#1{{\small\textsf{#1}}}
\def\@aafcont#1{{\small\textrm{#1}}}

% Set papersize
\ifthenelse{\equal{\@aaptype}{custom}}{%
  \setpapersize{custom}{\@aawidth}{\@aaheight}}{%
  \setpapersize{\@aaptype}}

% Set margins
\ifthenelse{\boolean{@aaeqmargs}}{\setlength{\@aalmargin}{\@aarmargin}}{}
\setmarginsrb{\@aalmargin}{\@aatmargin}{\@aarmargin}{\@aabmargin}{%
    \@aahheight}{\@aahsep}{\@aafheight}{\@aafskip}

% No paragraph indention
\setlength{\parindent}{0pt}

% Som new lengths for different lines (pagestyle)
\newlength{\@aalllen}    % Lower lines
\newlength{\@aalrlen}    % Raising lines
\newlength{\@aaltlen}    % Line thickness
\setlength{\@aalllen}{-0.6ex}
\setlength{\@aalrlen}{\baselineskip}
\addtolength{\@aalrlen}{\@aalllen}
\setlength{\@aaltlen}{0.2pt}

% Marks
\ifthenelse{\boolean{@aamarks}}{%
  \setlength{\unitlength}{1mm}%
  \newcounter{@aaxosize}
  \newcounter{@aaxesize}
  \newcounter{@aaysize}
  \newcounter{@aaleftm}
  \newcounter{@aarightm}
  \newcounter{@aabottomm}
  \setcounter{@aaleftm}{\@aalmargin/\unitlength}
  \setcounter{@aarightm}{\@aarmargin/\unitlength}
  \setcounter{@aabottomm}{\@aabmargin/\unitlength}
  \setcounter{@aaxosize}{(\PaperWidth/\unitlength)-\value{@aaleftm}}
  \setcounter{@aaxesize}{(\PaperWidth/\unitlength)-\value{@aarightm}}
  \setcounter{@aaysize}{(\PaperHeight/\unitlength)-\value{@aabottomm}}
  \thinlines%
  \gdef\@aaoddframe{%
    \begin{picture}(0,0)
      \put(-\value{@aaleftm},-\value{@aabottomm}){\line(1,0){2}}
      \put(-\value{@aaleftm},-\value{@aabottomm}){\line(0,1){2}}
      \put(-\value{@aaleftm},\value{@aaysize}){\line(1,0){2}}
      \put(-\value{@aaleftm},\value{@aaysize}){\line(0,-1){2}}
      \put(\value{@aaxosize},\value{@aaysize}){\line(-1,0){2}}
      \put(\value{@aaxosize},\value{@aaysize}){\line(0,-1){2}}
      \put(\value{@aaxosize},-\value{@aabottomm}){\line(-1,0){2}}
      \put(\value{@aaxosize},-\value{@aabottomm}){\line(0,1){2}}      
    \end{picture}}%
  \gdef\@aaevenframe{%
    \begin{picture}(0,0)
      \put(-\value{@aarightm},-\value{@aabottomm}){\line(1,0){2}}
      \put(-\value{@aarightm},-\value{@aabottomm}){\line(0,1){2}}
      \put(-\value{@aarightm},\value{@aaysize}){\line(1,0){2}}
      \put(-\value{@aarightm},\value{@aaysize}){\line(0,-1){2}}
      \put(\value{@aaxesize},\value{@aaysize}){\line(-1,0){2}}
      \put(\value{@aaxesize},\value{@aaysize}){\line(0,-1){2}}
      \put(\value{@aaxesize},-\value{@aabottomm}){\line(-1,0){2}}
      \put(\value{@aaxesize},-\value{@aabottomm}){\line(0,1){2}} 
    \end{picture}}}{%
  \gdef\@aaoddframe{}%
  \gdef\@aaevenframe{}}

% Header and footer
\newcommand{\ps@aafilostyle}{%
  \renewcommand{\@oddhead}{\makebox[0pt][l]{%
      \rule[\@aalllen]{\textwidth}{0.8pt}}%
    \leftmark\hfil\rightmark}% 
  \renewcommand{\@evenhead}{\makebox[0pt][l]{%
      \rule[\@aalllen]{\textwidth}{0.8pt}}%
    \rightmark\hfil\leftmark}% 
  \renewcommand{\@oddfoot}{\@aaoddframe\@aacopy\hfil\@aafname{\thepage}}
  \renewcommand{\@evenfoot}{\@aaevenframe\@aafname{\thepage}\hfil\@aacopy}}
\pagestyle{aafilostyle}

% New lenghts for textboxes
\newlength{\@aanamelen}    % Length of the name of an address and phone field
\newlength{\@aacontlen}    % Length of the contents of an address field
\newlength{\@aaphonelen}   % Length of the contents of a phone field
\newlength{\@aaldesclen}   % Length of the description of a phone list field
\newlength{\@aalnumlen}    % Length of the number of a phone list field
\newlength{\@aalnamelen}   % Length of the name of a phone list field
\AtBeginDocument{%
  \settowidth{\@aanamelen}{\@aafname{\@aaddra}}
  \setlength{\@aacontlen}{\linewidth}
  \addtolength{\@aacontlen}{-\@aanamelen}
  \setlength{\@aaphonelen}{0.5\linewidth}
  \addtolength{\@aaphonelen}{-\@aanamelen}
  \settowidth{\@aaldesclen}{\@aafname{nnnnn:~}}
  \settowidth{\@aalnumlen}{\@aafcont{00~00~00~00}}
  \setlength{\@aalnamelen}{\linewidth}
  \addtolength{\@aalnamelen}{-\@aaldesclen}
  \addtolength{\@aalnamelen}{-\@aalnumlen}}


%
% Commands for an address list
%

% One line in an address
\newcommand{\@aaddrline}[1]{%
  \makebox[0pt][l]{\rule[\@aalllen]{\linewidth}{\@aaltlen}}%
  \makebox[\linewidth][l]{#1}}

% Three types of address and phone fields
\newcommand{\@aaddrfield}[2]{%
  \makebox[\@aanamelen][l]{\@aafname{#1}}%
  \makebox[\@aacontlen][l]{\@aafcont{#2}}}
\newcommand{\@aaddrfielddbl}[3]{%
  \makebox[\@aanamelen][l]{\@aafname{#1}}%
  \makebox[0.5\@aacontlen][l]{\@aafcont{#2}}%
  \makebox[0.5\@aacontlen][l]{\@aafcont{#3}}}
\newcommand{\@aaphonefield}[2]{%
  \makebox[\@aanamelen][l]{\@aafname{#1}}%
  \makebox[\@aaphonelen][l]{\@aafcont{#2}}}


\def\@@aaname{\@ifnextchar[{\@@aanameb}{%
    \gdef\@@name{\@aaddrfield{\@anamen}{\@aname}}}}
\def\@@aanameb[#1]{\gdef\@@name{\@aaddrfielddbl{\@anamen}{\@aname}{#1}}}
\def\@@aaddra{\@ifnextchar[{\@@aaddrd}{%
    \gdef\@@addra{\@aaddrfield{\@addran}{\@addra}}}}
\def\@@aaddrd[#1]{\gdef\@@addra{\@aaddrfielddbl{\@addran}{\@addra}{#1}}}
\def\@@aaddrb{\@ifnextchar[{\@@aaddre}{%
    \gdef\@@addrb{\@aaddrfield{\@addrbn}{\@addrb}}}}
\def\@@aaddre[#1]{\gdef\@@addrb{\@aaddrfielddbl{\@addrbn}{\@addrb}{#1}}}
\def\@@aaddrc{\@ifnextchar[{\@@aaddrf}{%
    \gdef\@@addrc{\@aaddrfield{\@addrcn}{\@addrc}}}}
\def\@@aaddrf[#1]{\gdef\@@addrc{\@aaddrfielddbl{\@addrcn}{\@addrc}{#1}}}

% Eight different address list fields (three different types)
\newcommand{\aana}[2][\@aaname]{\gdef\@anamen{#1}\gdef\@aname{#2}\@@aaname}
\newcommand{\aaaa}[2][\@aaddra]{\gdef\@addran{#1}\gdef\@addra{#2}\@@aaddra}
\newcommand{\aaab}[2][\@aaddrb]{\gdef\@addrbn{#1}\gdef\@addrb{#2}\@@aaddrb}
\newcommand{\aaac}[2][\@aaddrc]{\gdef\@addrcn{#1}\gdef\@addrc{#2}\@@aaddrc}
\newcommand{\aaph}[2][\@aaphoneh]{\gdef\@@phoneh{\@aaphonefield{#1}{#2}}}
\newcommand{\aapw}[2][\@aaphonew]{\gdef\@@phonew{\@aaphonefield{#1}{#2}}}
\newcommand{\aapf}[2][\@aaphonef]{\gdef\@@phonef{\@aaphonefield{#1}{#2}}}
\newcommand{\aape}[2][\@aaphonee]{\gdef\@@phonee{\@aaphonefield{#1}{#2}}}

% One record in a address list (one person/company)
\newcommand{\aaddress}[2]{%
  \aana{~}\aaaa{~}\aaab{~}\aaac{~}%
  \aaph{~}\aapw{~}\aapf{~}\aape{~}%
  #2
  \parbox{\linewidth}{%
    \makebox[0pt][l]{\rule[\@aalrlen]{\linewidth}{\@aaltlen}}%
    \@aaddrline{\@@name}
    \@aaddrline{\@@addra}
    \@aaddrline{\@@addrb}
    \@aaddrline{\@@addrc}
    \@aaddrline{%
      \makebox[0.5\linewidth][l]{\@@phoneh}%
      \makebox[0.5\linewidth][l]{\@@phonef}}
    \@aaddrline{%
      \makebox[0.5\linewidth][l]{\@@phonew}%
      \makebox[0.5\linewidth][l]{\@@phonee}}}%
  \markboth{\textsf{\@title}}{\textsf{#1}}}


%
% Commands for a phone list
%

% Generic phone list field
\newcommand{\@aaphonelist}[2][]{%
  \makebox[0pt][l]{\rule[\@aalllen]{\linewidth}{\@aaltlen}}%
  \makebox[\@aalnamelen][l]{\@aafcont{\@aanamephone}}%
  \makebox[\@aaldesclen][l]{\@aafname{#1}}%
  \makebox[\@aalnumlen][r]{\@aafcont{#2}}\hfil%
  \def\@aanamephone{~}}

% Four different phone list fields (almost the same)
\newcommand{\aalph}[2][\@aaplh]{\@aaphonelist[#1]{#2}}
\newcommand{\aalpw}[2][\@aaplw]{\@aaphonelist[#1]{#2}}
\newcommand{\aalpf}[2][\@aaplf]{\@aaphonelist[#1]{#2}}
\newcommand{\aalpe}[2][\@aaple]{\@aaphonelist[#1]{#2}}

% One record in a phone list (one person/company)
\newcommand{\aaphone}[3]{%
  \markboth{\textsf{\@title}}{\textsf{#1}}%
  \def\@aanamephone{#2}%
  \parbox{\linewidth}{%
    \makebox[0pt][l]{\rule[\@aalrlen]{\linewidth}{\@aaltlen}}%
    #3}
  \def\@aanamephone{~}}

      
