%
% This package complements the thloria class and is aimed to replace
% the hyperref package in this context. The hyperref package does not
% address the intricacies of thloria and this is what we are doing here.
% This package load hyperref and passes its options so that one can write
% \usepackage[pdftex]{tlhypref} or whatever.
%
% Versions:
%     0.92 (1999/04/08): added a fourth parameter to \@writetorep to make it 
%                        again compatible with hyperref.sty. 
%                        Needs hyperref 6.53 or later.
%     0.93 (1999/07/13): again compatible with hyperref 6.63;
%                        use of higher-level commands. 
%                        \TLpdfbookmark removed (use \texorpdfstring)
%     0.94 (1999/07/15): \@toc@hook added 
%
\def\fileversion{0.94}
\def\filedate{1999/07/15}
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{tlhypref}[\filedate\space\fileversion\space
thloria wrapper for hyperref package (D. Roegel)]
\DeclareOption*{\PassOptionsToPackage{\CurrentOption}{hyperref}}
\ProcessOptions
\RequirePackage{hyperref}

% We cancel modifications from hyperref (in hpdftex.def and pdftex.def).

\let\@sect\H@old@sect
\let\@schapter\H@old@schapter
\let\@spart\H@old@spart   

\let\refstepcounter\H@refstepcounter

% Now, \@part, \@chapter, \@sect and \refstepcounter have the good values
% of thloria.

% Addition of \TLtopbookmark in front of \tableofcontents
% in order to make sure that the bookmark entry for the table of contents
% is at the top level. Works with the chapter hook.
\let\@old@tableofcontents\tableofcontents
\def\tableofcontents{\TLtopbookmark\@old@tableofcontents}

% These definitions are for the purpose of comparisons in \refstepcounter:
\def\@partcountername{part}
\def\@chaptercountername{chapter}
\def\@annexcountername{annex}

% \refstepcounter is redefined so that \@currentHref is uptodate
% in each chapter, section, etc.
\def\refstepcounter#1{%
  \H@refstepcounter{#1}%
  \edef\This@name{#1}%
  \ifx\This@name\name@of@slide
  \else
    \if@skiphyperref
    \else
      \if@hyper@item
        \stepcounter{Item}%
        \hyper@refstepcounter{Item}%
      \else
        % \hyper@refstepcounter is only performed when it is neither a part,
        % nor a chapter/annex; the \hyper@refstepcounter is done
        % in the hook of thloria with \hyper@refstepcounter{division}.
        \def\@countername{#1}%
        \ifx\@countername\@partcountername
        \else
          \ifx\@countername\@chaptercountername
          \else
            \ifx\@countername\@annexcountername
            \else
              \hyper@refstepcounter{#1}%
            \fi
          \fi
        \fi
      \fi
    \fi
  \fi
}


% We add \hyper@refstepcounter{division} to the division counter hook

\let\@old@division@inc@hook\@division@inc@hook
\renewcommand{\@division@inc@hook}{%
  \@old@division@inc@hook
  \hyper@refstepcounter{division}
  }

\newcommand\theHdivision  {\arabic{division}}
\renewcommand\theHfigure  {\theHdivision.\arabic{figure}}
\renewcommand\theHtable   {\theHdivision.\arabic{table}}
\renewcommand\theHsection {\theHdivision.\arabic{section}}

% This boolean forces a bookmark to be at the topmost level;
% it is only taken into account at the level of chapters (\chapter & \chapter*)
% and it will thus act on the next \chapter(*).

\newif\if@top@bookm@rk
\@top@bookm@rkfalse
\newcommand{\TLtopbookmark}{\@top@bookm@rktrue}

% We input the file \jobname.flg in order to get the value of
% `@there@were@p@rts':
\InputIfFileExists{\jobname.flg}{}{}%

% Numerical equivalent of `@there@were@p@rts':

\if@there@were@p@rts
  \newcommand{\TL@topbookmarkpos}{-1}
\else
  \newcommand{\TL@topbookmarkpos}{0}
\fi

\newcommand{\TL@bookmarkpos}{0}

% needed by \addcontentsline :
\newcommand{\toclevel@tlstarchapter}{0}
\newcommand{\toclevel@starpart}{-1}
\newcommand{\toclevel@spsection}{\TL@topbookmarkpos}

% Changes of various hooks in thloria:

\renewcommand{\@tlann@hook}[1]{%
  \@division@inc@hook
  \if@top@bookm@rk
    \renewcommand{\toclevel@tlstarchapter}{\TL@topbookmarkpos}
  \fi
  }

\renewcommand{\@tlann@lasthook}[1]{\renewcommand{\toclevel@tlstarchapter}{0}}

\renewcommand\@generic@schapter@hook[2]{%
  \if@top@bookm@rk
    \renewcommand{\toclevel@tlstarchapter}{\TL@topbookmarkpos}%
  \fi
  }

\renewcommand\@generic@schapter@lasthook[2]{%
  \renewcommand{\toclevel@tlstarchapter}{0}%
  }

\renewcommand\@generic@chapter@hook[3]{%
    \@generic@schapter@hook{#1}{#2}%
  }

\renewcommand\@generic@chapter@lasthook[3]{%
    \@generic@schapter@lasthook{#1}{#2}%
  }

\renewcommand{\@acknowledgment@hook}{%
     \@division@inc@hook
     \pdfbookmark[\TL@topbookmarkpos]{\AcknowledgmentName}%
              {acknowledgment.\theHdivision}%
  }
\newcommand{\DedicationName}{D\'edicace}
\renewcommand{\@dedication@hook}{%
   \@division@inc@hook
   \pdfbookmark[\TL@topbookmarkpos]{\DedicationName}{dedication.\theHdivision}%
  }

\renewcommand{\@abstract@hook}[1]{%
   \@division@inc@hook
   \pdfbookmark[\TL@topbookmarkpos]{#1}{abstract.\theHdivision}%
  }

\newcommand{\CoverName}{Couverture}
\renewcommand{\@cover@hook}{%
   \@division@inc@hook
   \pdfbookmark[\TL@topbookmarkpos]{\CoverName}{cover.\theHdivision}%
  }

\renewcommand{\@toc@hook}{%
   \pdfbookmark[\TL@topbookmarkpos]{\contentsname}{}%
  }

\endinput
% end of file tlhypref.sty

