%% This is file `beamerouterthemeTUDa.sty' version 2.10 (2020/04/25),
%% it is part of
%% TUDa-CI -- Corporate Design for TU Darmstadt
%% ----------------------------------------------------------------------------
%%
%%  Copyright (C) 2018--2020 by Marei Peischl <marei@peitex.de>
%%
%% ============================================================================
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3c
%% of this license or (at your option) any later version.
%% The latest version of this license is in
%% http://www.latex-project.org/lppl.txt
%% and version 1.3c or later is part of all distributions of LaTeX
%% version 2008/05/04 or later.
%%
%% This work has the LPPL maintenance status `maintained'.
%%
%% The Current Maintainers of this work are
%%   Marei Peischl <tuda-ci@peitex.de>
%%   Markus Lazanowski <latex@ce.tu-darmstadt.de>
%%
%% The development respository can be found at
%% https://github.com/tudace/tuda_latex_templates
%% Please use the issue tracker for feedback!
%%
%% ============================================================================
%%
\def\fileversion{2.10}
\def\filedate{2020/04/25}
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{beamerouterthemeTUDa}
 [\filedate\space\fileversion]

\RequirePackage{l3keys2e}

\ExplSyntaxOn
\keys_define:nn {TUDa/beamer/outer} {
	logo .bool_gset:N = \g_TUDa_headlinelogo_bool,
	logo .default:n =true,
	logo .initial:n =true,
	logofile .tl_gset:N = \g_TUDa_logofile_tl,
	logofile .initial:n = tuda_logo,
    colorframetitle .bool_gset:N = \g_TUDa_colorframetitle_bool,
    colorframetitle .initial:n = false,
    colorframetitle .default:n = true,
    centerframetitle .bool_gset:N = \g_TUDa_centerframetitle_bool,
    centerframetitle .initial:n = false,
    centerframetitle .default:n = true,
}


\ProcessKeysOptions{TUDa/beamer/outer}


\mode<presentation>


\RequirePackage{graphicx}
\RequirePackage[paper=slide]{tudarules}

\newlength{\TUDa@beamer@sep}
\setlength{\TUDa@beamer@sep}{1em}
\newlength{\TUDa@beamer@logosep}
\setlength{\TUDa@beamer@logosep}{2mm}
\newlength{\TUDa@beamer@extraindent}
\ExplSyntaxOn
\bool_if:NT \g_TUDa_colorframetitle_bool {\setlength{\TUDa@beamer@extraindent}{\TUDa@beamer@logosep}}

\setbeamersize{
  text~margin~left= \dimexpr\TUDa@beamer@sep+\TUDa@beamer@extraindent\relax,
  text~margin~right=\dimexpr\TUDa@beamer@sep+\TUDa@beamer@extraindent\relax,
}

\newlength{\TUDa@beamer@linewidth}
\setlength{\TUDa@beamer@linewidth}{\dimexpr\paperwidth-2\TUDa@beamer@sep-2\TUDa@beamer@extraindent\relax}

%Logo

\newsavebox{\TUDa@logobox}
\sbox{\TUDa@logobox}{%
	\makebox[2.2\c_TUDa_logoheight_dim][l]{\includegraphics[height=\c_TUDa_logoheight_dim]{\g_TUDa_logofile_tl}}%
}

\TUDa@makeheadrule[color=identbarcolor,width=\paperwidth-2\TUDa@beamer@sep]{TUDa@beamer@headrule}
\TUDa@makefootrule[width=\paperwidth-2\TUDa@beamer@sep]{TUDa@beamer@rule}

\newlength{\TUDa@beamer@headheight}
\setlength{\TUDa@beamer@headheight}{
\dim_eval:n {
	\ht\TUDa@logobox+
	\dp\TUDa@logobox +
	2\TUDa@beamer@logosep+
	\box_ht:N \TUDa@beamer@rule_box +
	\box_dp:N \TUDa@beamer@rule_box
}}


\ExplSyntaxOff


\def\tudrule{\TUDa@beamer@rule}


\newcommand\insertsmalllogo{\setbeamertemplate{logo}[small]\usebeamertemplate{logo}}


\defbeamertemplate*{sidebar left}{TUD theme}{}
\defbeamertemplate*{sidebar right}{TUD theme}{}


\ExplSyntaxOn

\bool_if_exist:NF \l_TUDa_tmpa_bool {\bool_new:N \l_TUDa_tmpa_bool}

\defbeamertemplate*{footline}{TUDa theme}
{
	\begin{beamercolorbox}[leftskip=\TUDa@beamer@sep,rightskip=\TUDa@beamer@sep,
		ht=\csname c_TUDa_smallrule_dim\endcsname,dp=1mm]{footline}
	\TUDa@beamer@rule
	\end{beamercolorbox}
  	\begin{beamercolorbox}[
  	leftskip=\TUDa@beamer@sep,
  	rightskip=\TUDa@beamer@sep, ht=2.75mm,dp=2.75mm]{footline}
	\usebeamerfont{author~ in~ head/foot}%
	\bool_set_false:N \l_TUDa_tmpa_bool
	\clist_map_variable:nNn {date, department, institute, author} \l_tmpa_tl {
		\tl_if_empty:cF {beamer@short\l_tmpa_tl} {
			\bool_if:NTF \l_TUDa_tmpa_bool {~|~}
			{\bool_set_true:N \l_TUDa_tmpa_bool}
			\use:c {insertshort\l_tmpa_tl}
		}
	}
	\bool_if:NT \l_TUDa_tmpa_bool {~|~}
	\insertframenumber
	\hfill\raisebox{\dimexpr-.5\height+\dp\strutbox\relax}{\insertlogo}
    \end{beamercolorbox}
}



\RenewDocumentCommand{\logo}{sm}{
	\IfBooleanTF{#1}{
	\setbeamertemplate{logo}{\resizebox{!}{.06\beamer@paperheight}{\mbox{#2}}}%TODO change with aspectratio
	}{
	\setbeamertemplate{logo}{#2}
	}
}

\defbeamertemplate{headline}{TUDa theme.logo} {%
	\begin{beamercolorbox}[
		leftskip=\TUDa@beamer@sep,rightskip=\TUDa@beamer@sep,
		ht=.75\TUDa@beamer@sep,dp=\box_dp:N \TUDa@beamer@headrule_box]{headline}
		\TUDa@beamer@headrule
	\end{beamercolorbox}
}


\box_new:N \l_TUDa_frametitle_box

\defbeamertemplate{frametitle}{TUDa~theme.logo}{%
\nointerlineskip%
\begin{beamercolorbox}[
  sep=\TUDa@beamer@logosep,
  wd=\dimexpr\TUDa@beamer@linewidth+2\TUDa@beamer@extraindent\relax,
  rightskip=-\TUDa@beamer@logosep,
  leftskip=\dimexpr-\TUDa@beamer@logosep+\TUDa@beamer@extraindent\relax,
	]{frametitle}
	\hbox_set:Nn \l_TUDa_frametitle_box {
	\parbox[t]
		{\dimexpr\linewidth- \wd\TUDa@logobox-1em\relax\relax}{%
		\usebeamerfont{frametitle}\strut\insertframetitle
		\ifx\insertframesubtitle\@empty
		\else
		\par
		\usebeamercolor[fg]{framesubtitle}
		\usebeamerfont{framesubtitle}
		\insertframesubtitle
		\fi
	}}
	\leavevmode
	\bool_if:NTF \g_TUDa_centerframetitle_bool
		{\raisebox{\dimexpr-.5\height+.5\depth}}
		{\use:n}
			{\box_use:N \l_TUDa_frametitle_box}
	\hfill%
	\bool_if:NTF \g_TUDa_centerframetitle_bool
		{\raisebox{\dimexpr-.5\height+.5\depth}}
		{\raisebox{\dimexpr-\height+\ht\strutbox}}
		{\usebox\TUDa@logobox}
\end{beamercolorbox}%
\nointerlineskip
\leavevmode\color{black}\hspace*{-\TUDa@beamer@extraindent}\rlap{\TUDa@beamer@rule}%
}

\defbeamertemplate*{headline}{TUDa~theme.nologo}
{%
	\vspace{.75\TUDa@beamer@sep}
	\begin{beamercolorbox}[
		leftskip=\TUDa@beamer@sep,rightskip=\TUDa@beamer@sep]{headline}
		\TUDa@beamer@headrule
	\end{beamercolorbox}
      }

\defbeamertemplate*{frametitle}{TUDa~theme.nologo}{%
	\nointerlineskip%
\begin{beamercolorbox}[
  sep=\TUDa@beamer@logosep,
  wd=\dimexpr\TUDa@beamer@linewidth+2\TUDa@beamer@extraindent\relax,
  leftskip=\dimexpr-\TUDa@beamer@logosep+\TUDa@beamer@extraindent\relax,
	]{frametitle}
	\hbox_set:Nn \l_TUDa_frametitle_box {
	\parbox[t]
	{\dimexpr\linewidth- \wd\TUDa@logobox-1em\relax\relax}{%
		\usebeamerfont{frametitle}\strut\insertframetitle
		\ifx\insertframesubtitle\@empty
		\else
		\par
		\usebeamercolor[fg]{framesubtitle}
		\usebeamerfont{framesubtitle}
		\insertframesubtitle
		\fi
}}
\leavevmode
\bool_if:NTF \g_TUDa_centerframetitle_bool
{\raisebox{\dimexpr-.5\height+.5\depth}}
{\use:n}
{\box_use:N \l_TUDa_frametitle_box}
%	Rule for voffset similar to logo
	\rule[\dim_eval:n {
		\bool_if:NTF \g_TUDa_centerframetitle_bool
		{-.5\ht\TUDa@logobox+.5\dp\TUDa@logobox} {-\ht\TUDa@logobox+\ht\strutbox}
	}]{0pt}{\dimexpr\ht\TUDa@logobox+\dp\TUDa@logobox\relax}
\end{beamercolorbox}
	\nointerlineskip
\TUDa@beamer@rule
}

\cs_new:Nn \TUDa_setup_frame: {
	\bool_if:NTF \g_TUDa_headlinelogo_bool {
		\setbeamertemplate{frametitle}[TUDa~ theme.logo]
		\setbeamertemplate{headline}[TUDa~ theme.logo]
	}{
		\setbeamertemplate{frametitle}[TUDa~ theme.nologo]
		\setbeamertemplate{headline}[TUDa~ theme.nologo]
	}
}
\TUDa_setup_frame:

\newcommand*{\setupTUDaFrame}[1]{
	\keys_set:nn {TUDa/beamer/outer} {#1}
	\TUDa_setup_frame:
}

\ExplSyntaxOff


\mode
<all>

\endinput
