%% This is file `beamerouterthemeBFH.sty' version 2.2.9 (2026-09-30),
%% it is part of
%% BFH-CI -- Corporate Design for Bern University of Applied Sciences
%% ----------------------------------------------------------------------------
%%
%%  Copyright (C) 2021--2026 by
%%      Marei Peischl <marei@peitex.de>
%%      Andreas Habegger <andreas.habegger@bfh.ch>
%%
%% ============================================================================
%% 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 <bfh-ci@peitex.de>
%%   Andreas Habegger <andreas.habegger@bfh.ch>
%%
%% The development respository can be found at
%% https://gitlab.ti.bfh.ch/bfh-latex/bfh-ci/
%% Please use the issue tracker for feedback!
%%
%% ============================================================================
%%
\ProvidesPackage{beamerouterthemeBFH}[2026-09-30 v2.2.9 BFH-CI beamer outer theme, CI of Bern University of Applied Sciences]

\mode<presentation>
\ExplSyntaxOn

\box_new:N \g__ptxcd_frame_logo_box

\defbeamertemplate{icon~in~head/foot/sidebar}{BFH}[1][logobox_A]{
		\box_if_empty:NTF \g__ptxcd_frame_logo_box {
			#1
		}{
			\box_use:N \g__ptxcd_frame_logo_box
		}
}[action]{
	\dim_compare:nNnTF {\l__ptxcd_frame_icon_ht_dim} = {\c_zero_dim} {
		\box_gclear:N  \g__ptxcd_frame_logo_box
	} {
		\str_if_eq:nnTF {#1} {logobox_A} {
			\cs_if_exist:NT \__ptxcd_set_logobox_A:N {
				\__ptxcd_set_logobox_A:N \g__ptxcd_frame_logo_box
			}
		}{
			\hbox_gset:Nn \g__ptxcd_frame_logo_box {#1}
		}
		\box_if_empty:NF \g__ptxcd_frame_logo_box {
			\box_gresize_to_ht_plus_dp:Nn \g__ptxcd_frame_logo_box {\l__ptxcd_frame_icon_ht_dim}
		}
	}
}

\defbeamertemplate{version~in~head/foot/sidebar}{BFH}{
  \ifx\insertversion\@empty
					\else
						{%
							\usebeamercolor[fg]{version~in~head/foot}%
							\usebeamerfont{version~in~head/foot}%
							\insertversion
						}%
	\fi
}

\bool_new:N \l__ptxcd_footline_title_bool
\bool_new:N \l__ptxcd_footline_author_bool
\tl_new:N \l__ptxcd_pagenumber_unit_tl

\keys_define:nn {ptxcd/beamer/outer} {
	footline .multichoice:,
	footline / author .code:n = \bool_set:Nn \l__ptxcd_footline_author_bool {#1},
	footline / author .default:n = true,
	footline / title .code:n = \bool_set:Nn \l__ptxcd_footline_title_bool {#1},
	footline / title .default:n = true,
	sidebar .bool_gset:N = \g__ptxcd_sidebar_bool,
	sidebar .initial:n = false,
	sidebar .default:n = true,
	sidebar .usage:n = load,
	%
	headbar .bool_gset:N = \g__ptxcd_headbar_bool,
	headbar .initial:n = false,
	headbar .default:n = true,
	headbar .usage:n = load,
	% framenumber-unit used to output the numbers + calculate progress bars
	framenumber-unit .choice:,
	framenumber-unit / frame .code:n = {
			\tl_set:Nn \l__ptxcd_pagenumber_unit_tl {frame}
	},
	framenumber-unit / page .code:n = {
			\tl_set:Nn \l__ptxcd_pagenumber_unit_tl {page}
		},
	framenumber-unit .initial:n = frame,
	% Option interface to adjust the template page number in head/foot
	show-page-number .choice:,
	show-page-number / false .code:n = {\setbeamertemplate{page~number~in~head/foot}[default]},
	show-page-number / current .code:n ={\setbeamertemplate{page~number~in~head/foot}[\l__ptxcd_pagenumber_unit_tl number]},
	show-page-number / total .code:n = {\setbeamertemplate{page~number~in~head/foot}[total\l__ptxcd_pagenumber_unit_tl number]},
	%
	framenumber .choice:,
  framenumber / current .meta:n = {framenumber-unit=frame,show-page-number=current},
  framenumber / total  .meta:n = {framenumber-unit=frame,show-page-number=total},
	framenumber / true .meta:n = {framenumber=current},
	framenumber / false .meta:n = {show-page-number=false},
	framenumber .default:n = true
	%
	pagenumber .choice:,
  pagenumber / current .meta:n = {framenumber-unit=page,show-page-number=current},
  pagenumber / total .meta:n = {framenumber-unit=page,show-page-number=total},
	pagenumber / true .meta:n = {pagenumber=current},
	pagenumber / false .meta:n = {show-page-number=false},
	pagenumber .default:n = true,
	%
	progressbar .tl_gset:N = \g__ptxcd_progressbar_init_options_tl,
	progressbar .initial:n = ,
	progressbar .default:n = true,
	%
	compress .choices:nn = {true, false}{\use:c {beamer@compress#1}},
	compress .default:n    = true,
	%%%%%%%%%%%%%%%%%%%%
	% shared options for sidebar + headbar
	%%%%%%%%%%%%%%%%%%%%
	icon .choice:,
	icon / true .code:n = \setbeamertemplate{icon~in~head/foot/sidebar}[BFH],
	icon / false .code:n = \setbeamertemplate{icon~in~head/foot/sidebar}{},
	icon / unknown .code:n = \setbeamertemplate{icon~in~head/foot/sidebar}[BFH][#1],
	icon* / unknown .meta:n = {icon=#1,iconheight=0pt},
	icon .initial:n    = false,
  iconheight .dim_set:N = \l__ptxcd_frame_icon_ht_dim,
% sidebar + headbar
	version .choice:,
	version / true .code:n = \setbeamertemplate{version~in~head/foot/sidebar}[BFH],
	version / false .code:n = \setbeamertemplate{version~in~head/foot/sidebar}{},
	version .initial:n    = true,
	version .default:n    = true,
}

\ProcessKeyOptions[ptxcd/beamer/outer]

\hook_gput_code:nnn {package/beamerouterthemeBFH/after} {sidebar-option} {
	\dim_compare:nNnT {\l__ptxcd_frame_icon_ht_dim} = {\c_zero_dim} {
		\bool_if:NTF \g__ptxcd_sidebar_bool {
			\dim_set:Nn  \l__ptxcd_frame_icon_ht_dim {.173\paperheight}
		}{
			\dim_set:Nn  \l__ptxcd_frame_icon_ht_dim {.006\paperheight}
		}
	}
	\bool_if:NT \g__ptxcd_headbar_bool {
		\useoutertheme{BFH-headbar}
	}
  \bool_if:NT \g__ptxcd_sidebar_bool {
		\useoutertheme{BFH-sidebar}
	}
}

\ExplSyntaxOff
\defbeamertemplate*{plain footline}{BFH}{
	\begin{beamercolorbox}[wd=\paperwidth,sep=.5\beamer@leftmargin,
			leftskip=.5\beamer@leftmargin,rightskip=.5\beamer@rightmargin,]{footline}%
		\usebeamerfont{footline}%
		\ifbeamertemplateempty{author in head/foot}{}{%
			\usebeamertemplate{author in head/foot}%
			\hfill
		}%
		Berner~Fachhochschule | Haute~école~spécialisée~bernoise | Bern~University~of~Applied~Sciences
		\hfill\usebeamertemplate{page number in head/foot}%
	\end{beamercolorbox}%
}

\defbeamertemplate*{footline}{BFH-progressbar}{%
	\usebeamertemplate*{progressbar footline}
	\usebeamertemplate{plain footline}
	\usebeamertemplate*{progressbar bottom}
}

%bases on default frametitle, see beameroutherthemedefault.sty
% Copyright 2007 by Till Tantau
% Copyright 2012,2015 by Vedran Mileti\'c, Joseph Wright
% Copyright 2017,2018 by Louis Stuart, Joseph Wright
\defbeamertemplate*{frametitle}{BFH}[1][left]
{%
	\ifbeamercolorempty[bg]{frametitle}{}{\nointerlineskip}%
	\@tempdima=\textwidth%
	\advance\@tempdima by.6cm%
	\begin{beamercolorbox}[sep=.3cm,#1,wd=\the\@tempdima]{frametitle}
		\usebeamerfont{frametitle}%
		\if@tempswa\else\csname beamer@fte#1\endcsname\fi%
		\strut\insertframetitle\strut\par%
		{%
			\ifx\insertframesubtitle\@empty%
				\else%
				{\usebeamerfont{framesubtitle}\usebeamercolor[fg]{framesubtitle}\strut\insertframesubtitle\strut\par}%
			\fi
		}%
		\if@tempswa\else\vskip-.3cm\fi% set inside beamercolorbox... evil here...
	\end{beamercolorbox}%
}

\ExplSyntaxOn

%% ============================================================================
%%
%% This is an unofficial extension the BFH-CI beamer outer theme. It adds a
%% progress bar showing how far the presentation has advanced. Colors, fonts,
%% footline, frame titles and title pages are inherited unchanged from the
%% BFH-CI themes; the bar uses BFH-Orange on a light BFH-Gray track, matching
%% the orange rule of the BFH title page.
%%
%% Usage:
%%   \documentclass[progressbar={…}]{bfhbeamer}
%%
%% To change settings afterwards use
%%
%%   \BFHBeamerSetup{progressbar={<options>}}
%%
%% which takes the same keys and can also be used between specific frames.
%%
%% It can be combined with `BFH-headbar' as long as the bar is not put at the
%% top (both would define the headline):
%%
%%   \useoutertheme{BFH-headbar}
%%   \useoutertheme{BFH-progressbar}
%%
%% Options (for \useoutertheme[<options>]{BFH-progressbar} or the setup command):
%%   position = footline|bottom|top|none
%%                             where the bar goes: directly above the BFH
%%                             footline text (default), flush with the bottom
%%                             edge below it, as a head line at the very top,
%%                             or nowhere
%%   unit     = frame|page     advance the bar once per frame (default) or
%%                             once per slide, i.e. per overlay
%%   height   = <dimen>        thickness of the bar (default: 2pt)
%%   titlepage = true|false     show the progress bar on title pages
%%                             (default: false)
%%   track    = true|false     draw the not-yet-covered part of the bar in
%%                             light gray (default: true)
%%   inset    = true|false     keep the bar inside the text margins instead of
%%                             running the full paper width (default: false)
%%   counter  = none|current|total
%%                             what the BFH footline shows next to the
%%                             institution line. Beamer's default template is
%%                             empty, so BFH slides are unnumbered unless this
%%                             is set (default: none). It follows `unit', i.e.
%%                             with unit=page it counts slides, not frames.
%%
%% Note: \inserttotalframenumber is only known after the first run, so LaTeX
%% has to be run twice before the bar is correct.

% What the bar measures. Both have to be usable as an <integer> inside
% \dimexpr, so they expand to a counter or to plain digits.
\cs_new:Nn \__ptxcd_pb_current: {\use:c {insert \l__ptxcd_pagenumber_unit_tl number}}
\cs_new:Nn \__ptxcd_pb_total: {\use:c {inserttotal \l__ptxcd_pagenumber_unit_tl number}}


%% =======================================================================
%% The progress bar itself
%% ============================================================================
%
% Drawn from two abutting rules rather than with TikZ, which keeps the theme
% free of further dependencies. \dimexpr keeps the intermediate result of
% <width> * <current> / <total> in 64 bit, so there is no overflow even for
% long presentations.

\dim_new:N \l__ptxcd_pb_fill_dim

\defbeamertemplate*{progressbar}{BFH-progressbar}[1][\paperwidth]{
	\dim_set:Nn \l__ptxcd_pb_width_dim {#1}
	\dim_set:Nn \l__ptxcd_pb_fill_dim {
		\int_compare:nNnTF {\__ptxcd_pb_total:} > {0}
		{
			\dim_min:nn
			{\l__ptxcd_pb_width_dim}
			{\l__ptxcd_pb_width_dim * \__ptxcd_pb_current: / \__ptxcd_pb_total:}
		}
		{\c_zero_dim}
	}
	% The template is page-relative. \addtobeamertemplate inserts this code
	% before/after the BFH footline box, not inside that box, so no horizontal
	% compensation for the footline's leftskip is required. This also keeps the
	% same geometry for the footline, bottom, and top positions.
	\ifbeamer@plainframe\hspace*{-\beamer@leftmargin}\fi
	\hbox to \paperwidth {
		\hskip0pt plus\beamer@leftmargin
		\makebox[\l__ptxcd_pb_width_dim][l]{
			\bool_if:NT \l__ptxcd_pb_track_bool {
				\rlap{\usebeamercolor{progress~bar}\color{bg}
				\rule{\l__ptxcd_pb_width_dim}{\l__ptxcd_pb_height_dim}}
			}
			\usebeamercolor[fg]{progress~bar}
			\rule{\l__ptxcd_pb_fill_dim}{\l__ptxcd_pb_height_dim}
		}
		\hskip0pt plus\beamer@rightmargin
	}%
}%

\ExplSyntaxOff

% Templates have to be defined before the options are processed

\defbeamertemplate{progressbar top}{BFH-progressbar}{
			\usebeamertemplate{progressbar}\par
			\nointerlineskip
}

\defbeamertemplate{progressbar headline}{BFH-progressbar}{
			\usebeamertemplate{progressbar}
}

\defbeamertemplate{progressbar footline}{BFH-progressbar}{
	\raisebox{-\height}[0pt][0pt]{\rlap{\usebeamertemplate{progressbar}}}
	\par\nointerlineskip
}

\defbeamertemplate{progressbar bottom}{BFH-progressbar}{
	\par\nointerlineskip
	\smash{\rlap{\usebeamertemplate{progressbar}}}
}

\defbeamertemplate{headline}{BFH-progressbar}{
		\usebeamertemplate{progressbar top}
}

\ExplSyntaxOn

\providecommand*{\thepagenumber}{\value{page}}
\providecommand*{\inserttotalpagenumber}{\beamer@endpageofdocument}

%% ============================================================================
%% Progressbar Options
%% ============================================================================
\keys_define:nn {ptxcd/beamer/outer/progressbar} {
	position .choices:nn = {footline, bottom, headline} {
		\keys_set:nn {ptxcd/beamer/outer/progressbar}{position=none}
		\setbeamertemplate{progressbar~\l_keys_choice_tl}[BFH-progressbar]
	},
	position / headbar .code:n = {
		\keys_set:nn {ptxcd/beamer/outer/progressbar}{position=none}
		\setbeamertemplate{headbar~rule}[BFH-progressbar]
	},
	position / top .meta:n = {position= headline},
	position / none .code:n = {
		\setbeamertemplate{progressbar~footline}{}
		\setbeamertemplate{progressbar~bottom}{}
		\setbeamertemplate{headbar~rule}[BFH]
		\setbeamertemplate{progressbar~headline}{}
	},

	height .dim_set:N = \l__ptxcd_pb_height_dim,
	height .initial:n  = 2pt,

	width .dim_set:N = \l__ptxcd_pb_width_dim,
	width .initial:n  = \paperwidth, 

	track .bool_set:N = \l__ptxcd_pb_track_bool,
	track .initial:n    = true,
	track .default:n    = true,

	inset .choice:,
	inset /true .meta:n ={width=\dim_eval:n {\paperwidth-\beamer@leftmargin-\beamer@rightmargin}},
	inset /false .meta:n ={width=\paperwidth},

	true .meta:n = {position=footline},
	top .meta:n = {position=top},
	bottom .meta:n = {position=bottom},
	footline .meta:n = {position=footline},
}

\tl_if_blank:VF \g__ptxcd_progressbar_init_options_tl {
	\keys_set:nV {ptxcd/beamer/outer/progressbar} \g__ptxcd_progressbar_init_options_tl
}

\NewDocumentCommand \BFHBeamerSetup {O{/outer}m} {
	\keys_set:nn {ptxcd/beamer#1} {#2}
}

\ExplSyntaxOff


\mode<all>

\endinput
