%===============================================================================
% Document class dsb_book for Digital Sheetbook documentation
% Sunday, March 27 2011, Dennis Schulmeister
%===============================================================================
\NeedsTeXFormat{LaTeX2e}[1995/12/01]
\ProvidesClass{dsb_book}[2011/03/27 Digital Sheetbook documentation (dsb_book)]

% Parent class: scrbook from KOMA-Script
\DeclareOption*{\PassOptionsToClass{\CurrentOption}{scrbook}}
\ProcessOptions\relax
\LoadClass[a4paper,fontsize=12pt]{scrbook}

\usepackage{scrpage2}
\usepackage{lmodern}
\usepackage{calc}
\usepackage{alltt}

\usepackage{array}
\usepackage{color}
\usepackage{colortbl}

\usepackage{fix-cm}
\usepackage{titlesec}
\usepackage{tikz}
\usepackage{calc}
\usetikzlibrary{calc}
\usetikzlibrary{decorations,decorations.shapes,shapes}
\usetikzlibrary{positioning,backgrounds,shapes}

% Minor optical changes
\setlength{\parindent}{0.0cm}
\setlength{\parskip}{1.5ex plus 0.5ex minus 0.5ex}
\clubpenalty = 10000
\widowpenalty = 10000
\displaywidowpenalty = 10000

\topmargin-0.5cm
\textheight23cm

% Color definitions
\definecolor{HeadBarColor}{RGB}{229,229,229}
\definecolor{ChapterBarColor}{RGB}{180,193,198}
\definecolor{ChapterTextColor}{RGB}{77,77,77}
\definecolor{TitlePageBarColor}{RGB}{180,193,198}

% New environments for note and warning boxes (Note, Warning)
\newcommand{\NoteImage}{img/dsb_book/note.png}
\newcommand{\WarningImage}{img/dsb_book/warning.png}

\newlength{\NoteImageSkip}
\newcommand{\PrevArrayStretch}{}

\newenvironment{Note}[0]{%
        \setlength{\NoteImageSkip}{\baselineskip * -1}
        \renewcommand{\PrevArrayStretch}{\arraystretch}
        \renewcommand{\arraystretch}{1.5}

        \begin{center}
            \begin{tabular}[h]{c p{0.75\textwidth}}
                \hline
                \cellcolor[RGB]{255,255,185}
                \raisebox{\NoteImageSkip}[\ht\strutbox]{\includegraphics*[width=1cm]{\NoteImage}}
                &
                \cellcolor[RGB]{255,255,185}
    }{%
                \\
                \hline
            \end{tabular}
        \end{center}

        \renewcommand{\arraystretch}{\PrevArrayStretch}
    }

\newenvironment{Warning}[0]{%
        \setlength{\NoteImageSkip}{\baselineskip * \real{-1.5}}
        \renewcommand{\PrevArrayStretch}{\arraystretch}
        \renewcommand{\arraystretch}{1.5}

        \begin{center}
            \begin{tabular}[h]{c p{0.75\textwidth}}
                \hline
                \cellcolor[RGB]{255,220,220}
                \raisebox{\NoteImageSkip}[\ht\strutbox]{\includegraphics*[width=1cm]{\WarningImage}}
                &
                \cellcolor[RGB]{255,220,220}
    }{%
                \\
                \hline
            \end{tabular}
        \end{center}

        \renewcommand{\arraystretch}{\PrevArrayStretch}
    }

% Render fancy header (pagestyle dsb_greybar)
% See: http://web.slzm.de/blog/latex/latex-schone-kopfzeilen/
% See: http://texblog.net/latex-archive/layout/fancy-chapter-tikz/
% \cfoot[Chapter start]{All other pages}
% \defpagestyle{name}{header}{footer}
\newcounter{OddPage}
\setcounter{OddPage}{0}

\newcommand{\ThePage}{\large\textbf{\thepage}}
\newcommand{\HeadMark}{\textsc{\headmark}}
\newcommand{\ColorHeader}{%
    \begin{tikzpicture}[overlay,remember picture]
        \begin{pgfonlayer}{background}
            \draw[white, fill=HeadBarColor] (-10,0.5) rectangle (\paperwidth+2cm,3cm);
            \draw (-1.5cm-\oddsidemargin,1.5cm) node[anchor=west]  {%
                \ifnum\value{OddPage}=0
                    \HeadMark
                \else
                    \ThePage
                \fi
            };
            \draw (\textwidth+1.5cm,1.5cm) node[anchor=east]  {%
                \ifnum\value{OddPage}=0
                    \ThePage
                \else
                    \HeadMark
                \fi
            };
        \end{pgfonlayer}
    \end{tikzpicture}
    \hfill
}

\clearscrheadfoot
\cfoot[\ThePage]{}

\defpagestyle{dsb_greybar}{
        {% Even Page Header
            \setcounter{OddPage}{0}
            \ColorHeader
        }{% Odd Page Header
            \setcounter{OddPage}{1}
            \ColorHeader
        }{% Single Page Header
            \setcounter{OddPage}{0}
            \ColorHeader
        }
    }{
        {% Even Page Footer
        }{% Odd Page Footer
        }{% Single Page Footer
        }
    }


% Render fancy chapter titles
% See: http://web.slzm.de/blog/latex/schone-kapiteltitelseiten-in-latex/
% \titleformat{ command }[ shape ]{ format }{ label }{ sep }{ before }[ after ]
\newcounter{NoNumber}
\setcounter{NoNumber}{1}

\newlength{\TCW}                % TheChapterWidth: Width of chapter number
\newlength{\TCH}                % TheChapterHeight: Height of chapter number
\newlength{\TRX}                % TopRightX: Base point X value
\newlength{\TRY}                % TopRightY: Base point Y value
\newlength{\TCPaddingTopBottom} % Padding around chapter number
\newlength{\TCPaddingLeftRight} % Padding around chapter number
\newlength{\ChapterBarSpace}    % Space between blue bar and chapter number

\setlength{\TRX}{0.5cm}
\setlength{\TRY}{2.25cm}
\setlength{\TCPaddingTopBottom}{1.25cm}
\setlength{\TCPaddingLeftRight}{0.75cm}
\setlength{\ChapterBarSpace}{0.25cm}

\newcommand{\TheChapter}{%
    \fontsize{65}{50}\selectfont\color{ChapterTextColor}\textbf{\thechapter}% <-- DON'T REMOVE THIS COMMENT
}

\titleformat{\chapter}[display]{%
        \bfseries\Large
    }{% This and next are ignored for \chapter*
        \setlength{\TCW}{\widthof{\TheChapter}}
        \setlength{\TCH}{\heightof{\TheChapter}}
        \hfill
        \begin{tikzpicture}[overlay,remember picture]
            \coordinate (TopRight) at (\TRX,\TRY);
            \draw ($(TopRight) - 0.5*(\TCW+\TCPaddingLeftRight,\TCH+\TCPaddingTopBottom)$) node[rectangle] {\TheChapter~};
            \draw[white, fill=ChapterBarColor] ($(TopRight) - (\paperwidth,0)$) rectangle ($(TopRight) - (\TCW+\TCPaddingLeftRight+\ChapterBarSpace,\TCH+\TCPaddingTopBottom)$);
            \draw[ChapterTextColor, line width=1mm] (TopRight) rectangle ($(TopRight) - (\TCW+\TCPaddingLeftRight,\TCH+\TCPaddingTopBottom)$);
        \end{tikzpicture}
        \setcounter{NoNumber}{0}
    }{-4ex}{%
        \hfill
        \ifnum\value{NoNumber}=1
            % Print blue bar even if no chapter number
            \setlength{\TCW}{\widthof{\TheChapter}}
            \setlength{\TCH}{\heightof{\TheChapter}}
            \begin{tikzpicture}[overlay,remember picture]
                \coordinate (TopRight) at (\TRX,\TRY);
                \draw[white, fill=ChapterBarColor] ($(TopRight) - (\paperwidth,0)$) rectangle ($(TopRight) - (0,\TCH+\TCPaddingTopBottom)$);
            \end{tikzpicture}
        \fi
        \\ \\ \\ \\
        \filleft\fontsize{36}{10}\selectfont\scshape
        \setcounter{NoNumber}{1}
    }
    [\vspace{0ex}]

% Render fancy title page
\newcommand{\LogoImage}{img/dsb_book/logo.png}

\renewcommand{\maketitle}{%
    \begin{titlepage}
        \begin{tikzpicture}[overlay,remember picture]
            \draw[TitlePageBarColor, fill=TitlePageBarColor] (current page.north west) rectangle (2.5cm,-\paperheight);
        \end{tikzpicture}
        \begin{tikzpicture}[overlay,remember picture]
            \draw (\textwidth,2cm) node[anchor=north east]  {%
                \includegraphics*[width=10cm]{\LogoImage}
            };
        \end{tikzpicture}

        \vspace{2cm}

        \hfill\parbox{12.5cm}{
            \begin{flushright}
                \fontsize{32}{50}\selectfont\textbf{\@title} \\
                \fontsize{24}{50}\selectfont\@date %\\
            \end{flushright}
        }

        \vfill

        \hfill\parbox{12.5cm}{
            \begin{flushright}
                \fontsize{20}{25}\selectfont\textsc{\@author} %\\#
            \end{flushright}
        }
    \end{titlepage}

    \cleardoublepage
    \let\thanks\relax
    \let\maketitle\relax

    % Set PDF info fields
    \begingroup
        \def\\{, }
        \def\and{& }
        \pdfinfo{
          /Author (\@author)
          /Title (\@title)
        }
    \endgroup
}
