% Copyright (c) 2011, Serge Guelton <serge.guelton@enst-bretagne.fr>
% All rights reserved.
% 
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are met:
%     * Redistributions of source code must retain the above copyright
%       notice, this list of conditions and the following disclaimer.
%     * Redistributions in binary form must reproduce the above copyright
%       notice, this list of conditions and the following disclaimer in the
%       documentation and/or other materials provided with the distribution.
%     * Neither the name of the <organization> nor the
%       names of its contributors may be used to endorse or promote products
%       derived from this software without specific prior written permission.
% 
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
% ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
% WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
% DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
% DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
% (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
% LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
% ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
% (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
% SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


% This package provides a stub for a colorful report.

% author: serge Guelton <serge.guelton@enst-bretagne.fr>
% usage: \documentclass{creport}
% sensible to the \logo command that defines an optional path to a logo

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{creport}

% forward dependency
\RequirePackage{tikz}

\newcommand\creportBase{report}
\DeclareOption{article}{\renewcommand\creportBase{article}%
}

% forward all other options
\DeclareOption*{\PassOptionsToClass{\CurrentOption}{\creportBase}}
\ProcessOptions\relax

% declare dependencies
\LoadClass{\creportBase}
\RequirePackage{color}
\RequirePackage{graphicx}
\RequirePackage{listings}
\RequirePackage{hyperref}
\RequirePackage{manfnt}
\RequirePackage{ifthen}
\RequirePackage{titlesec}

% extra deps
\usetikzlibrary{shadows}


% define some color
\definecolor{freeblue}{rgb}{0.25,0.41,0.88}
\definecolor{dockerblue}{rgb}{0.11,0.56,0.98}
\definecolor{midgray}{rgb}{0.3,0.3,0.35}
\definecolor{cleangray}{rgb}{0.35,0.35,0.40}
\definecolor{lightgray}{rgb}{0.4,0.4,0.45}
\definecolor{thingray}{rgb}{0.9,0.9,0.95}

% renew maketitle command
\makeatletter
\global\let\@logo\@empty
\newcommand\logo[1]{\gdef\@logo{#1}}

\renewcommand{\maketitle}{
    \ifthenelse{\equal{\creportBase}{report}}{\begin{titlepage}}{}
	\ifx \@logo \@empty%
	\else
		\mbox{
			\hspace{4pt}%
			\includegraphics[width=.3\textwidth]{\@logo}%
			\hspace{4pt}
		}%
		\newline%
	\fi
	\vskip 4em%
	\hspace{.3\textwidth}%
	\parbox{.7\textwidth}{%
	\begin{flushright}
\Huge\bfseries\@title%
  \end{flushright}%
  }%
  \ifthenelse{\equal{\creportBase}{report}}{\vfill}{\vspace{2em}}
			\begin{flushleft}
			  \hspace{.1\textwidth}\large \@author
			\end{flushleft}
		\vspace{-1em}%
		\hspace{.1\textwidth}\textcolor{freeblue}{\rule{.9\textwidth}{3pt}}
		\vspace{-2em}%
			\begin{flushright}
			\large \@date \par
			\end{flushright}
			\ifthenelse{\equal{\creportBase}{report}}{\end{titlepage}}{\vspace{2em}}

  % page style for titlesec
  \pagestyle{plain}

  % item style
  \renewcommand\labelitemi{\textcolor{freeblue}{$\square$}}
  \renewcommand\labelitemii{\textcolor{freeblue}{$\square$}}
  \renewcommand\labelitemiii{\textcolor{freeblue}{$\square$}}
  \renewcommand\labelitemiv{\textcolor{freeblue}{$\square$}}

  % enumerate style
  \renewcommand\labelenumi{\textcolor{freeblue}{\fbox{\footnotesize\theenumi}}}
  \renewcommand\labelenumii{\textcolor{freeblue}{\fbox{\footnotesize\theenumii}}}
  \renewcommand\labelenumiii{\textcolor{freeblue}{\fbox{\footnotesize\theenumiii}}}
  \renewcommand\labelenumiv{\textcolor{freeblue}{\fbox{\footnotesize\theenumiv}}}
}
\makeatother

% colorful sectionning
\makeatletter
\titleformat{\chapter}[block]%
{\filleft\color{freeblue}\normalfont\LARGE\scshape}%
{\color{freeblue}%
	\ifx \@logo \@empty%
	\else%
	\begin{flushleft}%
			\includegraphics[width=.3\textwidth]{\@logo}%
  \end{flushleft}
	\fi%
\hfill\MakeUppercase{\chaptertitlename}\thechapter\newline}%
{1em}%
{}
\makeatother

\newlength{\sectionsize}%
\titleformat{\section}%
{\color{freeblue}\normalfont\Large\bfseries\titlerule[1pt]}%
{\settowidth{\sectionsize}{\thesection}%
\hspace{-\sectionsize}\hspace{-1em}%
\color{freeblue}\thesection}{1em}{}

\newlength{\subsectionsize}%
\titleformat{\subsection}%
{\color{freeblue}\normalfont\large\bfseries}%
{\settowidth{\subsectionsize}{\thesubsection}%
\hspace{-\subsectionsize}\hspace{-1em}%
\color{freeblue}\thesubsection}{1em}{}

\newlength{\subsubsectionsize}%
\titleformat{\subsubsection}%
{\color{freeblue}\normalfont\bfseries}%
{\settowidth{\subsubsectionsize}{\thesubsubsection}%
\hspace{-\subsubsectionsize}\hspace{-1em}%
\color{freeblue}\thesubsubsection}{1em}{}

% colorful footer
\renewpagestyle{plain}{%
	\renewcommand{\makefootrule}{\color{freeblue}\rule[1\baselineskip]{\linewidth}{1pt}}
	\setfoot[\thepage][][\makeatletter\@title\makeatother]{\thepage}{}{\makeatletter\@title\makeatother}
}

% nice lstlistings
\lstset{framerule=2mm,framexleftmargin=5mm, frame=leftline, rulecolor=\color{freeblue}, breaklines=true, backgroundcolor=\color{thingray} }

% nice hyperref
\hypersetup{bookmarksnumbered,colorlinks,backref, bookmarks, breaklinks,
linktocpage, linkcolor=freeblue}

% classical blocks
\newcommand\Warning[1]{\parbox{\textwidth}{%
\centering%
\begin{tikzpicture}%
  \node[%
  		text width=.9\textwidth,
		draw=freeblue,
		fill=freeblue!10!white,
		drop shadow,
		minimum height=3\baselineskip,
		rectangle] (0,0) {\hspace{.05\textwidth}\parbox{.9\textwidth}{#1}};%
		\node[minimum height=3\baselineskip,text width=.9\textwidth,yshift=.5\baselineskip]  {\large\dbend};
\end{tikzpicture}%
\vspace{.2\baselineskip}%
}%
}

% generic styling
\sloppy%make latex more strict about line breaks.
