% Bienvenue sur la classe lolipop.
\ProvidesClass{lolipop}[2012/03/31]

% Lolipop est une classe LaTeX moderne.
\NeedsTeXFormat{LaTeX2e}

% Elle se base pour l'essentiel sur la classe LaTeX book.
\LoadClass[10pt,twoside,french]{book}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Dépendances générales
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Nous écrivons en unicode.
\RequirePackage[utf8]{inputenc}

% Nous utilisons de jolies fontes vectorielles.
\RequirePackage[T1]{fontenc}
% Packagé dans tex-gyre
\RequirePackage{tgchorus}
% Packagé dans lmodern
\RequirePackage{lmodern}

% Nous étendons LaTeX avec le symbole €.
\RequirePackage{eurosym}
\DeclareUnicodeCharacter{20AC}{{\euro}}

% Nous aurons besoin de quadrichromie,
\RequirePackage[cmyk]{xcolor}
% ainsi que de différentes subtilités graphiques.
\RequirePackage{pgf}
\RequirePackage{tikz}
\usetikzlibrary{shapes,snakes,shadows,arrows}
\RequirePackage{amssymb}

% Le paquet environ permet facilement de switcher entre macro et environnement.
\usepackage{environ}

% Le paquet ifthen facilite le contrôle de flux.
\usepackage{ifthen}
\usepackage[strict]{changepage}

% Nous définissons de nouveaux types de flottants en nous appuyant sur le paquet
% float.
\usepackage{float}

% Nous éditons les hyperliens, avec césure, unicode, et sans cadre.
\RequirePackage[hyphens]{url}
\RequirePackage[breaklinks=true, unicode=True, pdfborder={0 0 0}]{hyperref}
\def\UrlNoBreaks{\do\(\do\[\do\{\do\<\do\:}%

% Le paquet microtype permet les fonctionnalités de microtypographie.
% \RequirePackage{microtype}

% Et finalement, nous mettrons en oeuvre les subtilités de la typographie
% française.
\RequirePackage[french]{babel}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Chemins
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\def\@includes{includes/}
\def\@images{\@includes images/}
\def\@screenshots{\@includes screenshots/}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Mise en page
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Avec geometry, c'est trop facile.
\RequirePackage[
  paperwidth     = 168mm,
  paperheight    = 230mm,
  inner          = 17mm,
  outer          = 3.2cm,
  marginparwidth = 2.7cm,
  marginparsep   = .5cm,
  top            = 15mm,
  bottom         = 25mm,
  twoside
]{geometry}

% Toutefois, nous modifierons la géométrie pour le mainmatter, et la restaurons
% pour le backmatter.
\let\ltxmainmatter\mainmatter
\def\mainmatter{
    \newgeometry{
    inner          = 17mm,
    outer          = 6.2cm,
    marginparwidth = 5.7cm,
    marginparsep   = .5cm,
    top            = 15mm,
    bottom         = 25mm,
    twoside
  }
  \ltxmainmatter
}

\let\ltxbackmatter\backmatter
\def\backmatter{
  \restoregeometry
  \ltxbackmatter
}

% Headers et footers discrets
\pagestyle{plain}%

% Pas de césure pour les mots en majuscule.
\uchyph=0

% Traits de coupe et marques d'impression, en option
\usepackage{eso-pic}
\def\lolipop@addborder{
  \pgfdeclareimage{bordure}{\@images bordure}
  \newcommand\reflecteven[1]{\checkoddpage\ifoddpage{{##1}}\else\reflectbox{##1}\fi}
  \AddToShipoutPicture{%
    \parindent 0pt%
    \reflecteven{\pgfuseimage{bordure}}%
    \hspace{-168mm}
  }
}
\DeclareOption{imprimeur}{\lolipop@addborder}
\ProcessOptions

% Autorisons plus d'écarts entre mots.
\pretolerance=9999
% \tolerance=1000
% \emergencystretch 0.5pt
% \hbadness9999
% \vbadness9999

\setlength\parskip{0pt plus 4pt minus 0pt}

% Autorisons plus d'écarts entre flottants.
\setlength\floatsep    {12\p@ \@plus 4\p@ \@minus 4\p@}
\setlength\textfloatsep{20\p@ \@plus 4\p@ \@minus 6\p@}
\setlength\intextsep   {12\p@ \@plus 4\p@ \@minus 4\p@}
\setlength\abovecaptionskip{10\p@ \@plus 4\p@ \@minus 2\p@}
\setlength\belowcaptionskip{0\p@ \@plus 4\p@ \@minus 0\p@}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Mise en forme
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Commençons par écrire un constructeur pour les parametres à déclarer.
\def\lolipop@declare#1{%
  \global\long\expandafter%
  \def \csname lolipop@#1\endcsname {}%
  \global\long\expandafter%
  \def \csname #1\endcsname ##1{%
    \global\long\expandafter%
    \def \csname lolipop@#1\endcsname{##1}%
  }%
}

%%% Couleur %%%

% Le magenta est la couleur dominante du guide.
\definecolor{mgray}{cmyk}{0,0.191,0.080,0.263}
\definecolor{magenta}{cmyk}{0,1,0,0}
\newcommand{\magenta}{\color{magenta}}
% Nous l'utiliserons notamment pour les emphases.
\renewcommand{\emph}[1]{{\it#1}}
\newcommand{\strong}[1]{{\bf\magenta#1}}
% Ainsi que pour les exergues, que nous définirons comme un paragraphe centré
% dans la marge.
\NewEnviron{exergue}{
  \let\href\fnhref
  \marginpar{
   \checkoddpage\ifoddpage\raggedright\else\raggedleft\fi
   \begin{minipage}{4.0cm}
     \centering
     \bf\large\magenta
     \BODY
   \end{minipage}
  }
  \let\href\mphref
}

%%% Hyperliens %%%

% La coloration des liens est sans intérêt en général (on le fera à plus haut
% niveau si nécessaire).
\hypersetup{colorlinks=false}
% Par contre, ceux-ci doivent être imprimés vu que le guide est destiné à
% l'impression.  Pour cela, nous stockons l'interfaces de hyperref et
% redéfinissons les notres.
\let\hhref\href
\let\hurl\url
\let\href\mphref
\newcommand{\mphref}[2]{\hhref{#1}{#2\footnote{\url{#1}}}}
\renewcommand{\url}[1]{\hurl{#1}}
\newcommand{\email}[1]{\hhref{mailto:#1}{\tech{#1}}}
% Cependant, cela ne permet plus d'afficher un lien dans une footnote. Pour
% cela, on va hacker les footnotes.
\newcommand{\fnhref}[2]{#2 (\url{#1})}
\let\ltxfootnote\footnote
\renewcommand{\footnote}[1]{%
  \let\href\fnhref%
  \ltxfootnote{#1}%
  \let\href\mphref%
}

%%% Fontes %%%

% Une interface pour acceder aux fontes TgChorus.
\newcommand{\chorus}{\fontfamily{qzc}\selectfont}
\newcommand{\textchorus}[1]{{\chorus #1}}

% Nous utiliserons TgChorus pour les headers.
\newcommand{\headingfont}{\chorus}
\newcommand{\textheadingfont}[1]{{\chorus #1}}

%%% Autres interfaces %%%

% Pour l'écrit étranger.
\newcommand{\foreign}[1]{{\it #1}}
\let\etranger\foreign

% Pour les marques.
\newcommand{\brand}[1]{{#1}}
\let\marque\brand

% Pour les aspects techniques.
\newcommand{\tech}[1]{{\tt #1}}
\let\info\tech
\let\shell\tech
\let\command\tech

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Sectionning
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%% Décoration %%%
\newcommand\reflectodd[1]{\checkoddpage\ifoddpage{\reflectbox{#1}}\else{#1}\fi}
\newlength{\headwidth}
\pgfdeclareimage{tache}{\@images tache}

\lolipop@declare{manchette}

\newcommand{\fancychapter}[1]{
  \reflectodd{
    \setlength\headwidth{0.6\columnwidth}
    \setbox0=\hbox{\hspace{1em}\reflectodd{#1}}
    \ifthenelse{\lengthtest{\wd0>\headwidth}}
      {\setbox0=\hbox{\hspace{1em}\reflectodd{\parbox[b]{0.6\columnwidth}{\alignhead #1}}}}{}
    \pgfmathsetmacro{\largeur}{\wd0*0.07}
    \parindent\z@
    \begin{tikzpicture}
      \draw[thick,scale=0.5] (0,0) -- node[above=10pt-1.1\dp0]{\copy0} (\largeur + 0.35,0);
      \node {\pgfuseimage{tache}};
    \end{tikzpicture}
  }
}

\newcommand{\fancymark}[1]{
  \setbox0=\hbox{#1}
  \pgfmathsetmacro{\largeur}{\wd0*0.07}
  \begin{tikzpicture}
    \draw[thick,scale=0.5] (0,0) -- node[above=1pt-\dp0]{\copy0} (\largeur + 0.35,0);
    \draw[scale=0.5] (\largeur,0) to[in=175,out=0,looseness=1.25] (\largeur + 0.75,0.25) node {$\scriptscriptstyle\blacksquare$};
    \draw[thick,scale=0.5] (\largeur + 0.125,0) -- (\largeur + 0.75 + 0.25 + 0.125,0) node {$\scriptscriptstyle\blacktriangleright$};
    \draw[scale=0.5] (\largeur+0.25,0) to[in=170,out=0,looseness=1.25] (\largeur+1-0.125,-0.25) node {$\scriptscriptstyle\bigstar$};
  \end{tikzpicture}
}

%%% Chapitres %%%

\def\alignhead{\checkoddpage\ifoddpage\raggedright\else\raggedleft\fi}

\renewcommand\chapter{
  \clearpage
  \global\@topnum\z@
  \@afterindentfalse
  \secdef\@chapter\@schapter
}

\def\@chapapp{}

\def\@chapter[#1]#2{%
                    \let\oldmanchette\manchette%
                    \def\manchette##1{}%
                    \ifnum \c@secnumdepth >\m@ne
                       \if@mainmatter
                         \refstepcounter{chapter}%
                         \typeout{\@chapapp\space\thechapter.}%
                         \addcontentsline{toc}{chapter}%
                                   {\protect\numberline{\thechapter}#1}%
                       \else
                         \addcontentsline{toc}{chapter}{#1}%
                       \fi
                    \else
                      \addcontentsline{toc}{chapter}{#1}%
                    \fi
                    \chaptermark{#1}%
                    \addtocontents{lof}{\protect\addvspace{10\p@}}%
                    \addtocontents{lot}{\protect\addvspace{10\p@}}%
                    \let\manchette\oldmanchette%
                    \if@twocolumn
                      \@topnewpage[\@makechapterhead{#2}]%
                    \else
                      \@makechapterhead{#2}%
                      \@afterheading
                    \fi}

\def\@makechapterhead#1{%
  \vspace*{20\p@}%
  {\parindent \z@
    \alignhead
      \headingfont
      \ifnum \c@secnumdepth >\m@ne
        \if@mainmatter
          \Huge\bfseries
          {\Large\@chapapp}\space\thechapter.\par
          \vspace*{10\p@}%
        \fi
      \fi
      \interlinepenalty\@M
      \fancychapter {
        \headingfont
        \Huge \bfseries #1
      }
    \par\nobreak
    \if@not@empty{\lolipop@manchette}{\vskip 10\p@\large\lolipop@manchette}\manchette{}
    \vskip 20\p@
  }
}

\def\@makeschapterhead#1{%
  \vspace*{20\p@}%
  {\parindent \z@
    \alignhead
    \fancychapter {
      \headingfont
      \interlinepenalty\@M
      \Huge \bfseries  #1
    }
    \par\nobreak
    \if@not@empty{\lolipop@manchette}{\vskip 10\p@\large\lolipop@manchette}\manchette{}
    \vskip 20\p@
  }
}

%%% Autres %%%
\renewcommand\section{\@startsection {section}{1}{\z@}%
                                   {-2.5ex \@plus -2ex \@minus -.2ex}%
                                   {2.3ex \@plus.2ex \@minus-.1ex}%
                                   {\raggedright\magenta\headingfont\Large\bfseries}}
\renewcommand\subsection{\@startsection{subsection}{2}{\z@}%
                                     {-2.25ex\@plus -2ex \@minus -.2ex}%
                                     {1.5ex \@plus .1ex \@minus-.1ex}%
                                     {\raggedright\magenta\headingfont\large\bfseries}}
\renewcommand\subsubsection{\@startsection{subsubsection}{3}{\z@}%
                                     {-2.00ex\@plus -2ex \@minus -.2ex}%
                                     {1.2ex \@plus .1ex \@minus-.1ex}%
                                     {\raggedright\magenta\headingfont\normalsize\bfseries$\blacktriangleright$\hspace{1ex}}}
\renewcommand\paragraph{\@startsection{paragraph}{4}{\z@}%
                                    {-1.8ex \@plus-1ex \@minus-.1ex}%
                                    {0.5ex \@plus 0.1ex \@minus-.1ex}%
                                    {\raggedright\normalcolor\normalfont\normalsize\itshape$\looparrowright$\hspace{1ex}}}
\renewcommand\subparagraph{\@startsection{subparagraph}{5}{\parindent}%
                                       {2.25ex \@plus 1ex \@minus .1ex}%
                                       {-1em}%
                                      {\normalcolor\normalfont\normalsize\itshape}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Flottants : captures
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Redéfinissons un style "lolipop", inspiré de plain.
\newcommand\floatc@lolipop[2]{
  \setbox\@tempboxa\hbox{\textit{\normalcolor #2}}%
  \ifdim\wd\@tempboxa>\hsize \textit{\normalcolor #2}\par
    \else\hbox to\hsize{\hfil\box\@tempboxa\hfil}\fi
}
\newcommand\fs@lolipop{
  \let\@fs@capt\floatc@lolipop
  \def\@fs@pre{}\def\@fs@post{}%
  \def\@fs@mid{\vspace\abovecaptionskip\relax}%
  \let\@fs@iftopcapt\iffalse}
% Et utilisons le style lolipop
\floatstyle{lolipop}
\newfloat{screenshot}{tbp}{scr}
\floatname{screenshot}{Capture}
% Mais le flottant ne sera pas l'interface utilisateur. À la place, nous
% utiliserons un environnement "capture", pour simplifier.
\NewEnviron{capture}[2][tbp]{
  \let\href\fnhref
  \begin{screenshot}[#1]
    \begin{center}
      \pgfimage[width=0.9\columnwidth]{\@screenshots #2}
    \end{center}
  \caption{\BODY}
  \end{screenshot}
  \let\href\mphref
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Flottants : logiciels
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Déclarons les paramètres qui nous intéressent.
\lolipop@declare{nom}
\lolipop@declare{logo}
\lolipop@declare{genre}
\lolipop@declare{apprentissage}
\lolipop@declare{installation}
\lolipop@declare{lien}
\lolipop@declare{equivalent}

% L'interface utilisateur est l'environnement "logiciel". Là où le logiciel est
% déclaré, son résumé est inséré en marge et une référence est crée.
\NewEnviron{logiciel}{
  \nom{} \logo{} \genre{} \apprentissage{}
  \installation{} \lien{} \equivalent{}
  \BODY
  \let\href\fnhref
  \marginpara{
    \label{logiciel:\lolipop@nom}
    \begin{rosebox}{\lolipop@nom}
      \sffamily\footnotesize
%     Logo : \lolipop@logo
      \if@not@empty{\lolipop@genre        }{\lolipop@genre                                               \\ }
      \if@not@empty{\lolipop@apprentissage}{Apprentissage : \iconify{hirondelle}{\lolipop@apprentissage} \\ }
      \if@not@empty{\lolipop@installation }{Installation :  \iconify{marguerite}{\lolipop@installation}  \\ }
      \if@not@empty{\lolipop@lien         }{Lien :          \lolipop@lien                                \\ }
      \if@not@empty{\lolipop@equivalent   }{Équivalents :   \lolipop@equivalent                          \\ }
    \end{rosebox}
  }
  \let\href\mphref
}

% \marginpara est une version améliorée de \marginpar ...

\newlength\marginboxwidth
\setlength\marginboxwidth{57mm}
\def\marginpara#1{%
  \setbox0=\hbox{\parbox[c]{\marginboxwidth}{#1}}%
  \marginpar{\mbox{}\vspace{0.1\ht0}\copy0\vspace{0.1\ht0}}%
}

% Déclarons les icones utilisées de façon répétitives, pour optimiser l'espace.
\pgfdeclareimage[height=8pt]{hirondelle1}{\@images hirondelle1}
\pgfdeclareimage[height=8pt]{hirondelle2}{\@images hirondelle2}
\pgfdeclareimage[height=8pt]{marguerite1}{\@images marguerite1}
\pgfdeclareimage[height=8pt]{marguerite2}{\@images marguerite2}

% Une jolie boite de titre pour coller cela.
\tikzstyle{roseboxstyle}=[fill=magenta!20, very thick, drop shadow={color = magenta!60!black!40}, rectangle, inner sep=4pt, inner ysep=4pt]
\tikzstyle{rosetitlebox}=[fill=magenta, text=black, drop shadow={color = magenta!70!black!70}, inner ysep=4pt, inner sep=1ex]
\addtolength\marginboxwidth{-4pt}
\NewEnviron{rosebox}[1]{
  \begin{tikzpicture}
    \node [roseboxstyle] (box){
      \begin{minipage}{\marginboxwidth}
        \checkoddpage\ifoddpage\raggedright\else\raggedleft\fi
        \vspace{10pt}
        \begin{minipage}{40mm}
          \BODY
        \end{minipage}
        \vspace{-8pt}
      \end{minipage}
    };
    \checkoddpage\ifoddpage
      \node[rosetitlebox, rounded corners, right=6pt] at (box.north west) {\large\bf #1};
    \else
      \node[rosetitlebox, rounded corners, left=6pt] at (box.north east) {\large\bf #1};
    \fi
  \end{tikzpicture}
}

% Test si un contenu est vide.
\def\if@not@empty#1#2{%
  \ifthenelse{\equal{#1}{}}{}{#2}%
}

% Insert autant d'image1 que d'étoiles passées en argument. Complète avec autant
% d'image2 que nécessaire. Ce n'est pas codé très intelligemment, mais c'est
% efficace.
\def\iconify#1#2{
  \ifthenelse{\equal{#2}{*}}{
    \raisebox{-2pt}{\makebox[1em]{\pgfuseimage{#11}}}
    \raisebox{-2pt}{\makebox[1em]{\pgfuseimage{#12}}}
    \raisebox{-2pt}{\makebox[1em]{\pgfuseimage{#12}}}
  }
  {
    \ifthenelse{\equal{#2}{**}}{
      \raisebox{-2pt}{\makebox[1em]{\pgfuseimage{#11}}}
      \raisebox{-2pt}{\makebox[1em]{\pgfuseimage{#11}}}
      \raisebox{-2pt}{\makebox[1em]{\pgfuseimage{#12}}}
    }
    {
      \ifthenelse{\equal{#2}{***}}{
        \raisebox{-2pt}{\makebox[1em]{\pgfuseimage{#11}}}
        \raisebox{-2pt}{\makebox[1em]{\pgfuseimage{#11}}}
        \raisebox{-2pt}{\makebox[1em]{\pgfuseimage{#11}}}
      }
      {
        \raisebox{-2pt}{\makebox[1em]{\pgfuseimage{#12}}}
        \raisebox{-2pt}{\makebox[1em]{\pgfuseimage{#12}}}
        \raisebox{-2pt}{\makebox[1em]{\pgfuseimage{#12}}}
      }
    }
  }
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Flottants : ressources
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Définit un autre type de flottant, les encarts "Pour aller plus loin"
\NewEnviron{ressource}[1]{
  \let\href\fnhref
  \label{#1}
  \marginpara{
    \cyanbox{Pour aller plus loin}{
      \sffamily\footnotesize \BODY
      \ifthenelse{\equal{#1}{}}{}{%
        \begin{center}
          \pgfimage[width=35mm]{\@images #1}
        \end{center}
      }
    }
  }
  \let\href\mphref
}

% Ce flottant fait appel à une autre boite en marge, que l'on nomme les cyanbox
\tikzstyle{cyanboxstyle}=[fill=cyan!20, very thick, drop shadow={color = cyan!60!black!40}, rectangle, inner sep=4pt, inner ysep=4pt]
\tikzstyle{cyantitlebox}=[fill=cyan, text=black, drop shadow={color = cyan!70!black!70}, inner ysep=4pt, inner sep=1ex]
\newcommand{\cyanbox}[2]{
  \begin{tikzpicture}
    \node [cyanboxstyle] (box){
      \begin{minipage}{\marginboxwidth}
        \checkoddpage\ifoddpage\raggedright\else\raggedleft\fi
        \vspace{10pt}
        \begin{minipage}{40mm}
          \raggedright #2
        \end{minipage}
      \end{minipage}
    };
    \checkoddpage\ifoddpage
      \node[cyantitlebox, rounded corners, right=6pt] at (box.north west) {\bf #1};
    \else
      \node[cyantitlebox, rounded corners, left=6pt] at (box.north east) {\bf #1};
    \fi
  \end{tikzpicture}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Flottants : le saviez vous ?
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Définit un autre type de flottant, les encarts "Le saviez vous ?"
\def\lesaviezvous{Le saviez vous~?\Large?}
\newcommand\didyouknow[1]{
  \let\href\fnhref
  \label{#1}
  \marginpara{
    \begin{bluebox}{\lesaviezvous}
      \sffamily\footnotesize #1
    \end{bluebox}
  }
  \let\href\mphref
}

% Ce flottant fait appel à une autre boite en marge, que l'on nomme les bluebox
\tikzstyle{blueboxstyle}=[fill=cyan!20, very thick, drop shadow={color = cyan!60!black!40}, rectangle, inner sep=4pt, inner ysep=4pt]
\tikzstyle{bluetitlebox}=[fill=cyan, text=black, drop shadow={color = cyan!70!black!70}, inner ysep=4pt, inner sep=1ex]
\NewEnviron{bluebox}[1]{
  \begin{tikzpicture}
    \node [blueboxstyle] (box){
      \begin{minipage}{\marginboxwidth}
        \checkoddpage\ifoddpage\raggedright\else\raggedleft\fi
        \vspace{10pt}
        \begin{minipage}{40mm}
          \raggedright\BODY
        \end{minipage}
      \end{minipage}
    };
    \checkoddpage\ifoddpage
      \node[bluetitlebox, rounded corners, right=6pt] at (box.north west) {\bf #1};
%      \node[left=-8pt] at (box.west) {\bf\Huge\textcolor{black!60}{?}};
    \else
      \node[bluetitlebox, rounded corners, left=6pt] at (box.north east) {\bf #1};
%      \node[right=-8pt] at (box.east) {\bf\Huge\textcolor{black!60}{?}};
    \fi
  \end{tikzpicture}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Flottants : encarts
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newlength\boxwidth
\newlength\bottomspace
\setlength\boxwidth{131mm}
\setlength\bottomspace{-\baselineskip}
\addtolength\boxwidth{-10pt}
% Ce flottant fait appel à une autre boite en marge, que l'on nomme les whitebox
\tikzstyle{whiteboxstyle}=[draw=magenta, fill=white, very thick, rectangle, rounded corners, inner sep=4pt, inner ysep=4pt]
\tikzstyle{whitetitlebox}=[fill=white, text=black, drop shadow, inner ysep=4pt, inner sep=1ex]
\def\whitebox#1#2{
  \checkoddpage\ifoddpage\else\hspace{-\marginparwidth}\hspace{15mm}\fi
  \begin{tikzpicture}
    \node [whiteboxstyle] (box){
        \begin{minipage}{\boxwidth}
          \parindent 15pt
          \vspace{10pt}
          #2
        \end{minipage}
    };
    \node[whitetitlebox, rounded corners, right=6pt] at (box.north west) {\bf #1};
  \end{tikzpicture}
 \vspace{\bottomspace}
}

% Et utilisons le style lolipop
\floatstyle{lolipop}
\newfloat{encartflottant}{tbp}{scr}
\floatname{encartflottant}{Encart}
% Mais le flottant ne sera pas l'interface utilisateur. À la place, nous
% utiliserons un environnement "encart", pour simplifier.
\NewEnviron{encart}[2][tbp]{
  \begin{encartflottant}[#1]
    \label{#2}
    \whitebox{#2}{\BODY}
  \end{encartflottant}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Glossaire(s)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Entree de glossaire
\NewEnviron{Glossaire}[1]{%
  \setlength\parskip{0pt plus 0pt minus 0pt}
  \subsubsection{#1}%
  \hypertarget{glossaire:#1}{}%
  \BODY%
}

% Référence dans le texte
% TODO: améliorer la présentation.
% Idée : ne mettre en exergue qu'une fois par chapitre, à la première occurence.
\newcommand{\glossaire}[2][]{%
  \ifthenelse{\equal{#1}{}}{\def\target{#2}}{\def\target{#1}}%
  \hyperlink{glossaire:\target}{%\magenta#2\textsuperscript{?}}%
  #2}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 1er et 2e de couvertures
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Déclarons les paramètres qui nous intéressent
\lolipop@declare{title}
\lolipop@declare{subject}
\lolipop@declare{keywords}
\lolipop@declare{version}
\lolipop@declare{date}
\lolipop@declare{organisation}
\lolipop@declare{orgwebsite}
\lolipop@declare{orgcontact}
\lolipop@declare{orgaddress}
\lolipop@declare{sourcesaddress}

% Profitons en pour renseigner les métadonnées.
\AtBeginDocument{
  \hypersetup{
    pdftitle=\lolipop@title,
    pdfauthor=\lolipop@organisation,
    pdfsubject=\lolipop@subject,
    pdfkeywords=\lolipop@keywords,
  }
}

% Page de titre
\def\maketitle{%
  \thispagestyle{empty}
  \AddToShipoutPicture*{%
    \parindent 0pt%
    \pgfimage{\@images couverture}%
  }
  \mbox{}
  \clearpage
  \makeours
  \clearpage
}

% Ours
\def\makeours{
  \mbox{}
  \vfill
  \begin{center}\footnotesize
    \lolipop@title\ --- \reflectbox{\copyright} \lolipop@organisation \\ 
    Version \lolipop@version\ du \lolipop@date                        \\ 
    \lolipop@orgaddress                                               \\ 
    Site Web : \url{\lolipop@orgwebsite}                              \\ 
    \email{\lolipop@orgcontact}                                       \\ 
    \vspace{0.5\baselineskip}
    Licence de ce document : voir page \pageref{licence}.             \\ 
    Sources originales et version \info{pdf} maintenue :              \\ 
    \url{\lolipop@sourcesaddress}
  \end{center}
  \vspace{1cm}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 3e et 4e de couvertures
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\def\makecouv{
  \clearpage
  \checkoddpage\ifoddpage\mbox{}\clearpage\fi
  \setlength\bottomspace{0.0\baselineskip}
  \RenewEnviron{encart}[1]{
    \begin{encartflottant}[b!]
      \hspace{-16mm}\whitebox{##1}{\BODY}
    \end{encartflottant}
  }
  \newgeometry{
    inner          = 17mm,
    outer          = 20mm,
    marginparwidth = 0mm,
    marginparsep   = 0mm,
    top            = 14mm,
    bottom         = 14mm,
    twoside
  }
  \setlength\floatsep{16\p@ \@plus 8\p@ \@minus 4\p@}
  \thispagestyle{empty}
  \setcounter{footnote}{0}
  \mbox{}\par
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Table des matières
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\setcounter{tocdepth}{2}

\renewcommand*\l@chapter[2]{%
  \ifnum \c@tocdepth >\m@ne
    \addpenalty{-\@highpenalty}%
    \vskip 1.0em \@plus\p@
    \setlength\@tempdima{1.5em}%
    \begingroup
      \parindent \z@ \rightskip \@pnumwidth
      \parfillskip -\@pnumwidth
      \leavevmode \magenta\headingfont\bfseries\large
      \advance\leftskip\@tempdima
      \hskip -\leftskip
      #1\nobreak\hfil \nobreak\hb@xt@\@pnumwidth{\hss #2}\par
      \penalty\@highpenalty
    \endgroup
  \fi}
\renewcommand*\l@section{\@dottedtocline{1}{1.5em}{2.3em}}
\renewcommand*\l@subsection{\@dottedtocline{2}{3.8em}{3.2em}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Signature
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\usepackage{multicol}
\NewEnviron{signature}{
  \vfill
  \begin{center}
    \begin{tabular}{lll}
      \BODY
    \end{tabular}
  \end{center}
  \vfill
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Annexes
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\let\ltxappendix\appendix
\renewcommand\appendix{\par
  \ltxappendix
  %\addtocontents{toc}{\clearpage}
  \setcounter{chapter}{0}
  \setcounter{section}{0}
  \gdef\thechapter{\@Alph\c@chapter}
  \def\@chapapp{Annexe}
  \@mainmattertrue
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Index(es)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Divers
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% On utilise des tirets quadratins à la place de tous les tirets possibles ...
\DeclareUnicodeCharacter{2012}{{--}}
\DeclareUnicodeCharacter{2013}{{--}}
\DeclareUnicodeCharacter{2014}{{--}}
\DeclareUnicodeCharacter{2015}{{--}}
