% 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 & customization
% =======================
%
% To use any of options below, call them when invoking `mtheme` in the
% preamble of your slides:
%
%   \usetheme[<options>]{m}
%

% Option: usetitleprogressbar
%         adds a thin progress bar underneath each frame title

\newif\if@useTitleProgressBar
\@useTitleProgressBarfalse
\DeclareOptionBeamer{usetitleprogressbar}{
  \@useTitleProgressBartrue
}

% Option: usetotalslideindicator
%         formats slide numbering as #current/#total

\newif\if@useTotalSlideIndicator
\@useTotalSlideIndicatorfalse
\DeclareOptionBeamer{usetotalslideindicator}{
  \@useTotalSlideIndicatortrue
}

% Option: noslidenumbers
%         omits slide numbering entirely

\newif\if@noSlideNumbers
\@noSlideNumbersfalse
\DeclareOptionBeamer{noslidenumbers}{
  \@noSlideNumberstrue
}

% Option: nosectionslide
%         omits the dedicated slide at the start of each new section

\newif\if@noSectionSlide
\@noSectionSlidefalse
\DeclareOptionBeamer{nosectionslide}{
  \@noSectionSlidetrue
}

% Option: protectframetitle
%         applies \protect to \insertframetitle to allow \cite, \ref, etc.

\newif\if@protectFrameTitle
\@protectFrameTitlefalse
\DeclareOptionBeamer{protectframetitle}{
  \@protectFrameTitletrue
}

% Option: nooffset
%         turns off extra space used to vertically center content on frames

\newlength{\@mtheme@voffset}
\setlength{\@mtheme@voffset}{2em}
\DeclareOptionBeamer{nooffset}{
  \setlength{\@mtheme@voffset}{0em}
}

% Option: blockbg
%         applies a gray background to blocks (via color theme `metropolis`)

\DeclareOptionBeamer{blockbg}{
  \PassOptionsToPackage{blockbg}{beamercolorthememetropolis}%
}

% Option: ...

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

\ProcessOptionsBeamer

% Customize: \mthemetitleformat
%            formats the main title, section titles, and frame titles

\def\mthemetitleformat#1{\scshape #1}

\mode<presentation>

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

\RequirePackage{etoolbox}
\RequirePackage{tikz}
\RequirePackage{pgfplots}
\RequirePackage{ifxetex,ifluatex}
\newif\ifxetexorluatex
\ifxetex
  \xetexorluatextrue
\else
  \ifluatex
    \xetexorluatextrue
  \else
    \xetexorluatexfalse
  \fi
\fi

\usetikzlibrary{backgrounds}
\usetikzlibrary{calc}

\usecolortheme{metropolis}

\ifxetexorluatex
  \usefonttheme{metropolis}
\else
  \PackageWarning{beamerthemem}{You need to compile with XeLaTeX or LuaLaTeX for the Fira fonts.}
\fi

%}}}
%{{{ --- 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
    {% \inserttitlegraphic is nonempty
      \vbox to 0pt
      {% display title graphic without changing the position of other elements
        \vspace*{2em}
        \usebeamercolor[fg]{titlegraphic}%
        \inserttitlegraphic%
      }%
      \nointerlineskip%
    }
    \fi

    \vfill%

    \ifx\inserttitle\@empty\else
    {{% \inserttitle is nonempty
      \raggedright%
      \linespread{1.0}%
      \usebeamerfont{title}%
      \usebeamercolor[fg]{title}%
      \mthemetitleformat{\inserttitle}%
      \vspace*{0.5em}
    }}
    \fi

    \ifx\insertsubtitle\@empty\else
    {{% \insertsubtitle is nonempty
      \usebeamerfont{subtitle}%
      \usebeamercolor[fg]{subtitle}%
      \insertsubtitle%
      \vspace*{0.5em}%
    }}
    \fi

    \begin{tikzpicture}
      \draw[alerted text.fg] (0, 0) -- (\textwidth, 0);
    \end{tikzpicture}%
    \vspace*{1em}%

    \ifx\beamer@shortauthor\@empty\else
    {{% \insertauthor is always nonempty by beamer's definition, so we must
      % test another macro which is initialized by \author{...}
      % For details, see http://tex.stackexchange.com/questions/241306/
      \usebeamerfont{author}%
      \usebeamercolor[fg]{author}%
      \insertauthor%
      \par%
      \vspace*{0.25em}
    }}
    \fi

    \ifx\insertdate\@empty\else
    {{% \insertdate is nonempty
      \usebeamerfont{date}%
      \usebeamercolor[fg]{date}%
      \insertdate%
      \par%
    }}
    \fi

    \ifx\insertinstitute\@empty\else
    {{% \insertinstitute is nonempty
      \vspace*{3mm}
      \usebeamerfont{institute}%
      \usebeamercolor[fg]{institute}%
      \insertinstitute%
      \par%
    }}
    \fi

    \vfill
    \vspace*{\@mtheme@voffset}
  \end{minipage}
}

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

\RequirePackage{calc}
\newlength{\progressbar@percent}
\newcommand{\progressbar}[1]{%
  \setlength{\progressbar@percent}{%
    #1 * \ratio{\insertframenumber pt}{\inserttotalframenumber pt}%
  }%
  \begin{tikzpicture}[tight background]
    \draw[bg!50!fg, fill=bg!50!fg] (0,0) rectangle (#1, 0.4pt);
    \draw[alerted text.fg, fill=alerted text.fg] (0,0) rectangle (\progressbar@percent, 0.4pt);
  \end{tikzpicture}%
}


%}}}
%{{{ --- Commands ---------------------

\newcommand{\insertsectionHEAD}{%
  \expandafter\insertsectionHEADaux\insertsectionhead}

\newcommand{\insertsectionHEADaux}[3]{\mthemetitleformat{#3}}%

\newcommand{\plain}[2][]{%
  \begingroup
    \setbeamercolor{background canvas}{use=structure,parent=structure}
    \begin{frame}{#1}
      \centering
      \vfill
      \vspace{1em}
      \usebeamercolor[fg]{structure}
      \usebeamerfont{section title}
      \mthemetitleformat{#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 ---------------------

\setbeamertemplate{section page}
{
  \vspace{2em}
  \centering
  \begin{minipage}{22em}
    \usebeamercolor[fg]{section title}
    \usebeamerfont{section title}
    \insertsectionHEAD\\[-1ex]
    \progressbar{\textwidth}
  \end{minipage}
  \par
}


% Insert frame with section title at every section start
\if@noSectionSlide\else%
  \AtBeginSection[]
  {
    \ifbeamer@inframe
      \sectionpage
    \else
      \frame[plain]{\sectionpage}
    \fi
  }
\fi

%}}}
%{{{ --- 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]{footline}%
  \hfill\usebeamerfont{page number in head/foot}%
\if@noSlideNumbers%
  %Purposefully left blank to display no slide number.%
  \else%
    \if@useTotalSlideIndicator%
    \insertframenumber/\inserttotalframenumber%
    \else%
    \insertframenumber%
    \fi%
  \fi%
\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}
\usebeamerfont{frametitle}%
\if@protectFrameTitle%
    \mthemetitleformat{\protect\insertframetitle}%
\else%
    \mthemetitleformat{\insertframetitle}%
\fi%
\end{beamercolorbox}%
\if@useTitleProgressBar
  \nointerlineskip
  \begin{beamercolorbox}[wd=\paperwidth,ht=0.4pt,dp=0pt]{frametitle}
    \progressbar{\paperwidth}
  \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}

%}}}
