% Beamer sTeX slide theme;
% Copyright (c) 20 Michael Kohlhase <michael.kohlhase@fau.de>.
\ProvidesPackage{beamerthemesTeX}
\typeout{Beamer sTeX theme}
\RequirePackage{xspace}
\RequirePackage{stex,notesslides,tikzinput}

\ExplSyntaxOn
\protected\def\stexbeamerthm#1#2#3#4#5#6#7#8{
  \tl_if_empty:nTF{#4}{
    \newcounter{#1}
  }{
    \newcounter{#1}[#4]
    \tl_set:cn{the#1}{
      \use:c{the#4}.\arabic{#1}
    }
  }
  \tl_set:cn{#1autorefname}{#6}

  \newenvironment{#1}{
    \ifhmode\par\fi

    \tl_if_empty:nF{#5}{
      \setcounter{#1}{\value{#5}}
      \stepcounter{#5}
    }
    \refstepcounter{#1}
    \noindent#7{
      #6~\use:c{the#1}
      \tl_if_empty:NF\thistitle{
        {}~(\thistitle)
      }.
    }
    #8
  }{\par}
  \csname stexstyle#2\endcsname[#3]{\begin{#1}}{\end{#1}}
}

\cs_set_protected:Nn \stex_beamertheme_do_en: {
  \def\stex@theorem@kw{Theorem}
  \def\stex@observation@kw{Observation}
  \def\stex@corollary@kw{Corollary}
  \def\stex@lemma@kw{Lemma}
  \def\stex@axiom@kw{Axiom}
  \def\stex@remark@kw{Remark}
  \def\stex@example@kw{Example}
  \def\stex@definition@kw{Definition}
}

\cs_set_protected:Nn \stex_beamertheme_do_de: {
  \def\stex@theorem@kw{Satz}
  \def\stex@observation@kw{Beobachtung}
  \def\stex@corollary@kw{Korollar}
  \def\stex@lemma@kw{Lemma}
  \def\stex@axiom@kw{Axiom}
  \def\stex@remark@kw{Bemerkung}
  \def\stex@example@kw{Beispiel}
  \def\stex@definition@kw{Definition}
}


\AtBeginDocument{
  \cs_if_exist:NTF\iflanguage{
    \cs_if_exist:NT \l@english {
      \iflanguage{english}{
        \stex_beamertheme_do_en:
      }{}
    }
    \cs_if_exist:NT \l@ngerman {
      \iflanguage{ngerman}{
        \stex_beamertheme_do_de:
      }{}
    }
  }{
    \stex_beamertheme_do_en:
  }
}
\ExplSyntaxOff

\stexbeamerthm{theorem}{assertion}{theorem}{section}{}{\stex@theorem@kw}\textbf\itshape
\stexbeamerthm{observation}{assertion}{observation}{section}{theorem}{\stex@observation@kw}\textbf\itshape
\stexbeamerthm{corollary}{assertion}{corollary}{section}{theorem}{\stex@corollary@kw}\textbf\itshape
\stexbeamerthm{lemma}{assertion}{lemma}{section}{theorem}{\stex@lemma@kw}\textbf\itshape
\stexbeamerthm{axiom}{assertion}{axiom}{section}{theorem}{\stex@axiom@kw}\textbf\itshape
\stexbeamerthm{remark}{assertion}{remark}{section}{theorem}{\stex@remark@kw}\emph{}
\stexstyleparagraph[remark]{\begin{remark}}{\end{remark}}
\stexbeamerthm{example}{example}{}{section}{theorem}{\stex@example@kw}\textbf{}
\stexbeamerthm{definition}{definition}{}{section}{theorem}{\stex@definition@kw}\textbf{}

\def\notesslidestitleemph#1{
  \textcolor{blue}{\Large\bf\sf#1}%
  \vskip0.1\baselineskip
  \leaders\vrule width \textwidth
  \vskip0.4pt%
  \nointerlineskip
}

\newlength{\slidelogoheight}

\ExplSyntaxOn
\bool_if:NTF \c_notesslides_notes_bool {
  \setlength{\slidelogoheight}{.4cm}
}{
  \setlength{\slidelogoheight}{.25cm}
}

\ifcsname slidelogo\endcsname\else
  \newsavebox{\slidelogo}
  \sbox{\slidelogo}{\sTeX}
\fi

\newrobustcmd{\setslidelogo}[2][]{
  \tl_if_empty:nTF{#1}{
    \sbox{\slidelogo}{\includegraphics[height=\slidelogoheight]{#2}}
  }{
    \sbox{\slidelogo}{\mhgraphics[height=\slidelogoheight,archive=#1]{#2}}
  }
}

\newrobustcmd{\setsource}[1]{\def\source{#1}}

\def\copyrightnotice{%
  \footnotesize\copyright :\hspace{.3ex}%
  \ifcsname source\endcsname\source\else%
  \ifcsname c_notesslides_shortauthor\endcsname\c_notesslides_shortauthor\else%
  \PackageWarning{notesslides}{Author/Source~undefined~in~copyright~notice}%
  ?source/author?\fi%
  \fi}

\newsavebox{\cclogo}
\sbox{\cclogo}{\includegraphics[height=\slidelogoheight]{stex-cc_somerights}}
\newif\ifcchref\cchreffalse

\AtBeginDocument{
  \@ifpackageloaded{hyperref}{\cchreftrue}{\cchreffalse}
}

\def\licensing{
  \ifcchref
    \href{http://creativecommons.org/licenses/by-sa/2.5/}{\usebox{\cclogo}}
  \else
    {\usebox{\cclogo}}
  \fi
}

\newrobustcmd{\setlicensing}[2][]{
  \def\@url{#1}
  \sbox{\cclogo}{\includegraphics[height=\slidelogoheight]{#2}}
  \ifx\@url\@empty
    \def\licensing{{\usebox{\cclogo}}}
  \else
    \def\licensing{
      \ifcchref
      \href{#1}{\usebox{\cclogo}}
      \else
      {\usebox{\cclogo}}
      \fi
    }
  \fi
}

\newrobustcmd\notesslides@slidelabel{
  \vbox to \slidelogoheight{
    \vss\hbox to \slidewidth
    {\licensing\hfill\copyrightnotice\hfill\arabic{slide}\hfill\usebox{\slidelogo}}
  }
}

\define@key{Gin}{label}{\def\@currentlabel{\arabic{slide}}\label{#1}}

%% Logo images
\pgfdeclareimage[height=2ex]{instlogo}{sTeX-logo}
\pgfdeclareimage[height=2ex]{cclogo}{stex-cc_somerights}

\ExplSyntaxOff

\def\notesslidesfooter{
  \usebox\slidelogo
  \hspace*{1cm}\insertshortauthor: \insertshorttitle\hfill{}%
  \insertframenumber\hfill\insertshortdate
  \hspace*{1cm}\pgfuseimage{cclogo}%
}

% THEME
\mode
<presentation>

\def\notesslidestitleemph#1{
  \textcolor{blue}{\Large\bf\sf#1}%
  \vskip-.1\baselineskip
  \leaders\vrule width \paperwidth\vskip0.4pt%
  \vskip0pt%
  \nointerlineskip
}

\notesslidesfont

%% A simple frame title with a long rule under it. 
\setbeamertemplate{frametitle}{%
    \usebeamerfont{frametitle}\insertframetitle\strut%
    \vskip-.1\baselineskip%
    \leaders\vrule width \paperwidth\vskip0.4pt%
    \vskip0pt%
    \nointerlineskip
}

%% A Footer line with a Institution and CC logos
\setbeamertemplate{footline}[text line]{%
  \usebeamercolor[fg]{titlelike}
  \notesslidesfooter
}

%% Turn off the navigation symbols
\setbeamertemplate{navigation symbols}{}

%% Inner theme
\useinnertheme{default}
%\setbeamertemplate{enumerate item}{(\alph{enumi})}
%\setbeamertemplate{enumerate subitem}{(\roman{enumii})}

\mode
<all>

%% Redefine various margin sizes 
\setbeamersize{text margin left=3mm}
\setbeamersize{text margin right=3mm}
\setbeamersize{description width=0.57cm}
\setlength\leftmargini{1em}
\setlength\leftmarginii{1em}
\setlength\leftmarginiii{1em}
\providecommand{\vstretch}[1]{\vspace*{\stretch{#1}}}

%% set the colors for sTeX content highlighting

\definecolor{steelblue}{rgb}{0.27, 0.51, 0.71}

\def\compemph#1{\textcolor{steelblue}{#1}}
\def\symrefemph#1{\textcolor{steelblue}{#1}}
\def\defemph#1{\textcolor{magenta}{#1}}
\def\varemph#1{\textcolor{gray}{#1}}

%% set PDF tooltips
\RequirePackage{pdfcomment}
\protected\def\compemph@uri#1#2{%
  \pdftooltip{%
    \compemph{#1}%\srefsymuri{#2}{\compemph{#1}}%
  }{%
    URI:~\detokenize{#2}%
  }%
}
\protected\def\symrefemph@uri#1#2{%
  \pdftooltip{%
    \symrefemph{#1}%\srefsymuri{#2}{\symrefemph{#1}}%
  }{%
    URI:~\detokenize{#2}%
  }%
}
\protected\def\defemph@uri#1#2{%
  \pdftooltip{%
    \defemph{#1}%
  }{%
    URI:~\detokenize{#2}%
  }%
}
\protected\def\varemph@uri#1#2{%
  \pdftooltip{%
    \varemph{#1}%
  }{%
    Variable~\detokenize{#2}%
  }%
}
