% Beamer mtheme
%
% Copyright 2014 Matthias Vogelgesang
% Licensed under CC-BY-SA 4.0 International.
%
% The initial template comes from the HSRM beamer theme by Benjamin Weiss, which
% you can find at https://github.com/hsrmbeamertheme/hsrmbeamertheme.
%

\ProvidesPackage{beamerthemem}

%{{{ --- Options ----------------------

\newif\if@useTitleProgressBar
\newif\if@protectFrameTitle

\@useTitleProgressBarfalse
\@protectFrameTitlefalse

\newlength{\@mtheme@voffset}
\setlength{\@mtheme@voffset}{2em}

\DeclareOptionBeamer{usetitleprogressbar}{\@useTitleProgressBartrue}
\DeclareOptionBeamer{protectframetitle}{\@protectFrameTitletrue}
\DeclareOptionBeamer{blockbg}{%
  \PassOptionsToPackage{blockbg}{beamercolorthememetropolis}%
}
\DeclareOptionBeamer{nooffset}{\setlength{\@mtheme@voffset}{0em}}

\DeclareOptionBeamer*{%
  \PackageWarning{beamerthemem}{Unknown option `\CurrentOption'}%
}

\ProcessOptionsBeamer

%}}}

\mode<presentation>

%{{{ --- Packages ---------------------

\RequirePackage[no-math]{fontspec}
\RequirePackage{etoolbox}
\RequirePackage{tikz}
\RequirePackage{pgfplots}

\usetikzlibrary{backgrounds}
\usetikzlibrary{calc}

\usecolortheme{metropolis}
\usefonttheme{metropolis}

%}}}
%{{{ --- Titlepage --------------------

\def\maketitle{\ifbeamer@inframe\titlepage\else\frame[plain]{\titlepage}\fi}

\def\titlepage{\usebeamertemplate{title page}}
\setbeamertemplate{title page}
{
  \begin{minipage}[b][\paperheight]{\textwidth}
    \vspace*{\@mtheme@voffset}
    \ifx\inserttitlegraphic\@empty%
    \else%
    {
    	% actual output of titlegraphic
   	    \usebeamercolor[fg]{titlegraphic}\inserttitlegraphic\par%
    	% measurement and add negative vspace
		\newdimen\logoheight
		\setbox0=\vbox{\inserttitlegraphic}%
		\logoheight=\ht0 \advance\logoheight by \dp0 %
		\vspace*{-\logoheight}%
		\vspace*{-1em}% I don't know why this additional negative space is needed
    }%
    \fi%
    \vfill
    \ifx\inserttitle\@empty%
    \else%
    {\raggedright\linespread{1.0}\usebeamerfont{title}\usebeamercolor[fg]{title}\scshape\MakeLowercase{\inserttitle}\par}%
    \vspace*{0.5em}
    \fi%
    \ifx\insertsubtitle\@empty%
    \else%
    {\usebeamerfont{subtitle}\usebeamercolor[fg]{subtitle}\insertsubtitle\par}%
    \vspace*{0.5em}
    \fi%
    \begin{tikzpicture}\draw[alerted text.fg] (0, 0) -- (\textwidth, 0);\end{tikzpicture}%
    \vspace*{1em}
    \ifx\insertauthor\@empty%
    \else%
    {\usebeamerfont{author}\usebeamercolor[fg]{author}\insertauthor\par}%
    \vspace*{0.25em}
    \fi%
    \ifx\insertdate\@empty%
    \else%
    {\usebeamerfont{date}\usebeamercolor[fg]{date}\insertdate\par}%
    \fi%
    \ifx\insertinstitut\@empty%
    \else%
    \vspace*{3mm}
    {\usebeamerfont{institute}\usebeamercolor[fg]{institute}\insertinstitute\par}%
    \fi%
    \vfill
    \vspace*{\@mtheme@voffset}
  \end{minipage}
}

%}}}
%{{{ --- Progressbar ------------------

\makeatletter
\def\progressbar@sectionprogressbar{}
\def\progressbar@titleprogressbar{}
\newcount\progressbar@tmpcounta % auxiliary counter
\newcount\progressbar@tmpcountb % auxiliary counter
\newdimen\progressbar@pbht      % progressbar height
\newdimen\progressbar@pbwd      % progressbar width
\newdimen\progressbar@tmpdim    % auxiliary dimension

\progressbar@pbwd=22em
\progressbar@pbht=0.4pt

% the progress bar
\def\progressbar@sectionprogressbar{%
  {\usebeamercolor{palette primary}%
    \progressbar@tmpcounta=\insertframenumber
    \progressbar@tmpcountb=\inserttotalframenumber
    \progressbar@tmpdim=\progressbar@pbwd
    \divide\progressbar@tmpdim by 100
    \multiply\progressbar@tmpdim by \progressbar@tmpcounta
    \divide\progressbar@tmpdim by \progressbar@tmpcountb
    \multiply\progressbar@tmpdim by 100

    % fixes very high linespacing introduced via \textsc{\MakeLowercase{...}}
    \fontsize{1em}{1em}\selectfont

    \makebox[\textwidth][c]{
      \begin{tikzpicture}[tight background]

        \node[anchor=south west, fg, inner sep=0pt, text width=\progressbar@pbwd] at (0pt, 0pt) {\insertsectionHEAD};

        \draw[anchor=west, fg!20, fill=fg!20, inner sep=0pt]
        (0, -1ex) rectangle ++ (\progressbar@pbwd, \progressbar@pbht);

        \draw[anchor=west, fg, fill=fg, inner sep=0pt]
        (0, -1ex) rectangle ++ (\progressbar@tmpdim, \progressbar@pbht);
      \end{tikzpicture}%
    }
  } % end usebeamercolor{palette primary}
}

\if@useTitleProgressBar
\def\progressbar@titleprogressbar{%
  \progressbar@tmpcounta=\insertframenumber
  \progressbar@tmpcountb=\inserttotalframenumber
  \progressbar@tmpdim=\paperwidth
  \divide\progressbar@tmpdim by 100
  \multiply\progressbar@tmpdim by \progressbar@tmpcounta
  \divide\progressbar@tmpdim by \progressbar@tmpcountb
  \multiply\progressbar@tmpdim by 100
  {%
    \usebeamercolor{palette quaternary}%
    \usebeamercolor{alerted text}%
    \begin{tikzpicture}[tight background]
      \draw[palette quaternary.fg, fill=palette quaternary.fg] (0, 0) rectangle ($(\paperwidth, 0.6pt) - (0.4pt, 0)$);
      \draw[alerted text.fg, fill=alerted text.fg] (0, 0) rectangle (\progressbar@tmpdim, 0.6pt);
    \end{tikzpicture}%
  }%
}
\fi
%}}}
%{{{ --- Commands ---------------------

\newcommand{\insertsectionHEAD}{%
  \expandafter\insertsectionHEADaux\insertsectionhead}
  \newcommand{\insertsectionHEADaux}[3]{\textsc{\MakeLowercase{#3}}
}

\newcommand{\plain}[2][]{%
  \begingroup
  \setbeamercolor{background canvas}{use=palette primary,bg=palette primary.fg}
  \begin{frame}{#1}
    \centering
    \vfill\vspace{1em}\usebeamerfont{section title}\textcolor{white}{\scshape #2}\vfill
  \end{frame}
  \endgroup
}

%}}}
%{{{ --- Itemize ----------------------

\setlength{\leftmargini}{1em}

% Actually one level should be enough but ...
\setlength{\leftmarginii}{1em}
\setlength{\leftmarginiii}{1em}

\newcommand{\itemBullet}{∙}

\setbeamertemplate{itemize item}{\itemBullet}
\setbeamertemplate{itemize subitem}{\itemBullet}
\setbeamertemplate{itemize subsubitem}{\itemBullet}
\setlength{\parskip}{0.5em}

%}}}
%{{{ --- Sections ---------------------

% Insert frame with section title at every section start
\AtBeginSection[]
{
  \begingroup
  \setbeamercolor{background canvas}{parent=palette primary}
  \begin{frame}[plain]
    \vspace{2em}\usebeamerfont{section title}
    \progressbar@sectionprogressbar%
  \end{frame}
  \endgroup
}

%}}}
%{{{ --- Captions ---------------------

\setbeamertemplate{caption label separator}{: }
\setbeamertemplate{caption}[numbered]

%}}}
%{{{ --- Footline/footnote ------------

\usenavigationsymbolstemplate{}
\setbeamertemplate{footline}
{%
\begin{beamercolorbox}[wd=\textwidth,ht=3ex,dp=3ex,leftskip=0.3cm,rightskip=0.3cm]{structure}%
  \hfill\usebeamerfont{page number in head/foot}%
  \insertframenumber%
\end{beamercolorbox}%
}

\setbeamertemplate{footnote}
{%
  \parindent 0em\noindent%
  \raggedright
  \usebeamercolor{footnote}\hbox to 0.8em{\hfil\insertfootnotemark}\insertfootnotetext\par%
}

%}}}
%{{{ --- Frametitle -------------------

\setbeamertemplate{frametitle}{%
\nointerlineskip
\begin{beamercolorbox}[wd=\paperwidth,leftskip=0.3cm,rightskip=0.3cm,ht=2.5ex,dp=1.5ex]{frametitle}
\if@protectFrameTitle
  \usebeamerfont{frametitle}\MakeLowercase{\protect\insertframetitle}%
\else
  \usebeamerfont{frametitle}\MakeLowercase{\insertframetitle}%
\fi
\end{beamercolorbox}%
\if@useTitleProgressBar
  \vspace{-.5em}
  \begin{beamercolorbox}[wd=\paperwidth,ht=1pt,dp=0pt]{frametitle}
    \progressbar@titleprogressbar
  \end{beamercolorbox}
\fi
\vspace{\@mtheme@voffset}
}

%}}}
%{{{ --- pgfplots ---------------------

%{{{ Colors

% TolColors from http://www.r-bloggers.com/the-paul-tol-21-color-salute/
\definecolor{TolColor1}{HTML}{332288}   % dark purple
\definecolor{TolColor2}{HTML}{6699CC}   % dark blue
\definecolor{TolColor3}{HTML}{88CCEE}   % light blue
\definecolor{TolColor4}{HTML}{44AA99}   % light green
\definecolor{TolColor5}{HTML}{117733}   % dark green
\definecolor{TolColor6}{HTML}{999933}   % dark brown
\definecolor{TolColor7}{HTML}{DDCC77}   % light brown
\definecolor{TolColor8}{HTML}{661100}   % dark red
\definecolor{TolColor9}{HTML}{CC6677}   % light red
\definecolor{TolColor10}{HTML}{AA4466}  % light pink
\definecolor{TolColor11}{HTML}{882255}  % dark pink
\definecolor{TolColor12}{HTML}{AA4499}  % light purple

%}}}
%{{{ Color cycles

\pgfplotscreateplotcyclelist{mbarplot cycle}{%
  {draw=TolColor2, fill=TolColor2!70},
  {draw=TolColor7, fill=TolColor7!70},
  {draw=TolColor4, fill=TolColor4!70},
  {draw=TolColor11, fill=TolColor11!70},
  {draw=TolColor1, fill=TolColor1!70},
  {draw=TolColor8, fill=TolColor8!70},
  {draw=TolColor6, fill=TolColor6!70},
  {draw=TolColor9, fill=TolColor9!70},
  {draw=TolColor10, fill=TolColor10!70},
  {draw=TolColor12, fill=TolColor12!70},
  {draw=TolColor3, fill=TolColor3!70},
  {draw=TolColor5, fill=TolColor5!70},
}

\pgfplotscreateplotcyclelist{mlineplot cycle}{%
  {TolColor2, mark=*, mark size=1.5pt},
  {TolColor7, mark=square*, mark size=1.3pt},
  {TolColor4, mark=triangle*, mark size=1.5pt},
  {TolColor6, mark=diamond*, mark size=1.5pt},
}

%}}}
%{{{ Styles

\pgfplotsset{
  compat=1.9,
  mbaseplot/.style={
    legend style={
      draw=none,
      fill=none,
      cells={anchor=west},
    },
    x tick label style={
      font=\footnotesize
    },
    y tick label style={
      font=\footnotesize
    },
    legend style={
      font=\footnotesize
    },
    major grid style={
      dotted,
    },
    axis x line*=bottom,
  },
  mlineplot/.style={
    mbaseplot,
    xmajorgrids=true,
    ymajorgrids=true,
    major grid style={dotted},
    axis x line=bottom,
    axis y line=left,
    legend style={
      cells={anchor=west},
      draw=none
    },
    cycle list name=mlineplot cycle,
  },
  mbarplot base/.style={
    mbaseplot,
    bar width=6pt,
    axis y line*=none,
  },
  mbarplot/.style={
    mbarplot base,
    ybar,
    xmajorgrids=false,
    ymajorgrids=true,
    area legend,
    legend image code/.code={%
      \draw[#1] (0cm,-0.1cm) rectangle (0.15cm,0.1cm);
    },
    cycle list name=mbarplot cycle,
  },
  horizontal mbarplot/.style={
    mbarplot base,
    xmajorgrids=true,
    ymajorgrids=false,
    xbar stacked,
    area legend,
    legend image code/.code={%
      \draw[#1] (0cm,-0.1cm) rectangle (0.15cm,0.1cm);
    },
    cycle list name=mbarplot cycle,
  },
  disable thousands separator/.style={
    /pgf/number format/.cd,
      1000 sep={}
  },
}

%}}}

\mode<all>

%{{{ misc
\let\otp\titlepage
\renewcommand{\titlepage}{\otp\addtocounter{framenumber}{-1}}
\newcommand{\mreducelistspacing}{\vspace{-\topsep}}

\linespread{1.15}

%}}}
