\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{beamerx}[2016/02/14 v1.0.0beta, standard scientific slideshows layout for Ecole polytechnique (Palaiseau,France).]

%=======================================================================
% Declaration des options
%=======================================================================
\ProcessOptions*
%=======================================================================
% Package requis
%=======================================================================
\RequirePackage{calc}
\RequirePackage{ifthen} % pour utiliser des booleens
% \RequirePackage{ifpdf} % mode de compilation
% \RequirePackage[pagestyles]{titlesec}
\RequirePackage{graphicx}
\RequirePackage{geometry}
\RequirePackage{xcolor}
% \RequirePackage{lmodern}
% \RequirePackage{multicol}
% \RequirePackage{amsmath}
% \RequirePackage{amssymb}
% \RequirePackage{mathrsfs}
% \RequirePackage{eso-pic}
% \RequirePackage{transparent}
\RequirePackage{xargs}
\RequirePackage{tikz}
% \RequirePackage{stmaryrd}
%\RequirePackage[hideothersubsections]{beamerthemeBerkeley}

%=======================================================================
% Definition des couleurs
%=======================================================================
\definecolor{bleu303}{RGB}{0,62,92}
\colorlet{darkbleu303}{black!20!bleu303}
\colorlet{lightbleu303}{white!20!bleu303}
\colorlet{ultralightbleu303}{white!80!bleu303}
\definecolor{rouge485}{RGB}{213,43,30}
\colorlet{darkrouge485}{black!20!rouge485}
\colorlet{lightrouge485}{white!20!rouge485}
\colorlet{ultralightrouge485}{white!80!rouge485}
\definecolor{vert575}{RGB}{107,126,81}
\colorlet{darkvert575}{black!20!vert575}
\colorlet{lightvert575}{white!20!vert575}
\colorlet{ultralightvert575}{white!80!vert575}

%=======================================================================
% Definition du theme beamer
%=======================================================================
%---------- Parametres generaux
\setbeamercovered{transparent=40}

\usefonttheme{professionalfonts}
\usefonttheme[stillsansseriflarge,stillsansserifsmall]{serif}
\setbeamertemplate{navigation symbols}{}

\setbeamersize{text margin left=3mm}
\setbeamersize{text margin right=3mm}

% \usecolortheme[named=bleu303]{structure}

\setbeamertemplate{footline}{%
  \hspace*{1mm}%
  \raisebox{1mm}{%
    \textbf{ÉCOLE POLYTECHNIQUE}-- \insertshorttitle%
  }%
  \hspace*{\fill}
  \raisebox{1mm}{%
    \insertframenumber%
  }%
  \hspace*{1mm}%
}
\setbeamertemplate{logo}{LOGO}
\setbeamertemplate{frametitle}{%
  \sffamily\Large\color{bleu303}\insertframetitle\\
  \sffamily\large\color{lightbleu303}\insertframesubtitle%
}
\setbeamertemplate{frametitle continuation}{this is the next part of the title}

%=======================================================================
% Commandes de mise en page utilisateur
%=======================================================================
%---------- Informations sur le document
\def\polytitresave{}
\def\polysoustitresave{}
\def\polyauteursave{}
\def\polydatesave{\today}
\def\polylogotitresave{}
\def\polylogopersosave{}

%---------- Mise en page
\newcommand{\MakeUppercaseWithNewline}[1]{%
  \begingroup
  \let\SavedOrgNewline\\%
  \DeclareRobustCommand{\\}{\SavedOrgNewline}%
  \MakeUppercase{#1}%
  \endgroup
}

\let\oldtitle=\title%
\renewcommandx\title[3][1={},3={}]{%
    % use: \title[sous-titre]{titre}[titre court]
    \renewcommand\polytitresave{#2}
    \renewcommand\polysoustitresave{#1}
    \ifthenelse{\equal{#3}{}}{%
      \oldtitle{#2}%
    }{%
      \oldtitle[#3]{#2}%
    }
    \subtitle{#1}
}

\let\oldauthor=\author%
\renewcommandx\author[2][1={}]{%
    % use: \author[auteur court]{auteur}
    \renewcommand\polyauteursave{#2}
    \oldauthor{\rmfamily\bfseries #1}
}

% \let\olddate=\date
\renewcommand\date[1]{%
    \renewcommand\polydatesave{#1}
}

\let\oldlogo=\logo%
\renewcommand\logo[1]{%
    \renewcommand\polylogopersosave{%
        \includegraphics[width=30mm,height=14mm,keepaspectratio]{#1}
    }
}

\oldlogo{%
    \hspace*{\paperwidth}%
    \hspace*{-16mm}%
    \makebox[\paperwidth]{%
        \makebox[16mm]{%
            \includegraphics[height=14mm]{beamerxwhitelogo}%
        }%
        \hfill%
        \makebox[32mm][r]{%
            \polylogopersosave{}%
        }%
        \hspace*{1mm}%
    }%
}

%=======================================================================
% Diapos conforme charte graphique
%=======================================================================
%--- Maketitle % TODO : clean
\AtBeginDocument{%
  \renewcommand\maketitle{%
    \setlength{\unitlength}{0.00625\paperwidth}%
    \setbeamertemplate{background canvas}{%
      \color{white}%
      \begin{picture}(160,90)%
        \put(80,45){%
          \makebox(0,0)[c]{%
            \includegraphics[width=\paperwidth,keepaspectratio]{beamerxbackground}%
          }%
        }%
        \put(15,45){%
          \makebox(0,0)[c]{%
            \color{white}\includegraphics[height=0.25\paperheight,keepaspectratio]{beamerxlogo}%
          }%
        }%
        \put(1,1){%
          \makebox(0,0)[lb]{%
            \begin{minipage}{0.5\paperwidth}\noindent%
              \sffamily\footnotesize\polyauteursave%
            \end{minipage}%
          }%
        }%
        \put(159,1){%
          \makebox(0,0)[rb]{%
            \begin{minipage}{0.5\paperwidth}\noindent\flushright%
              \sffamily\footnotesize\polydatesave%
            \end{minipage}%
          }%
        }%
        \put(159,46){%
          \makebox(0,0)[rb]{%
            \begin{minipage}{0.75\paperwidth}\noindent\flushright%
              \Large\setlength{\baselineskip}{0.7\baselineskip}%
              \sffamily\bfseries\MakeUppercaseWithNewline{\polytitresave}%
            \end{minipage}%
          }%
        }%
        \put(159,47){%
          \makebox(0,0)[rt]{%
            \begin{minipage}{0.75\paperwidth}\noindent\flushright%
              \setlength{\baselineskip}{0.7\baselineskip}%
              \sffamily\polysoustitresave%
            \end{minipage}%
          }%
        }%
      \end{picture}%
    }
    \begin{frame}[plain,c]
    \end{frame}
    \setbeamertemplate{background canvas}{}
  }
}

%--- XSection
% Prototype :
% \xsection{color_choice}
%          [short_title]
%          {title}
%          [picture]
\newcommandx{\xsection}[4][{2={},4=welcome}]{%
  \ifthenelse{\equal{#2}{}}{%
    \section{#3}%
  }{%
    \section[#2]{#3}%
  }%
  \ifthenelse{\equal{#1}{bleu}}{%
    \xsectionframe{lightbleu303}{white}{#4}{\arabic{section}}{#3}%
  }{%
  }
}

%- XSectionFrame
% Prototype :
% \xsectionframe{bg_color}
%               {fg_color}
%               {picture}
%               {number}
%               {title}
\newcommand{\xsectionframe}[5]{%
  \setbeamertemplate{background canvas}{%
    \setlength{\unitlength}{0.00625\paperwidth}%
    \begin{picture}(160,90)%
      \put(80,0){%
        \makebox(0,0)[cb]{%
          \includegraphics[width=\paperwidth,keepaspectratio]{#3}%
        }%
      }%
      \put(0,60){%
        \color{#1}\rule{\paperwidth}{\paperheight/3}%
      }%
      \put(80,75){%
        \makebox(0,0)[cb]{%
          \begin{minipage}{140\unitlength}\centering%
            \Huge\sffamily\color{#2}#4%
          \end{minipage}%
        }%
      }%
      \put(80,70){%
        \makebox(0,0)[ct]{%
          \begin{minipage}{140\unitlength}\centering%
            \Large\sffamily\color{#2}\MakeUppercaseWithNewline{#5}%
          \end{minipage}%
        }%
      }%
    \end{picture}%
  }
  \begin{frame}[plain,c]
  \end{frame}
  \setbeamertemplate{background canvas}{}
}

%--- XSubSection
% Prototype :
% \xsubsection{color_choice}
%             [short_title]
%             {title}
%             {subtitle}
%             [picture]
\newcommandx{\xsubsection}[5][{2={},5=pei}]{%
  \ifthenelse{\equal{#2}{}}{%
    \section{#3}%
  }{%
    \section[#2]{#3}%
  }%
  \ifthenelse{\equal{#1}{bleu}}{%
    \xsubsectionframe{lightbleu303}{darkbleu303}{white}{#5}{#3}{#4}%
  }{%
  }
}

%- XSubSectionFrame
% Prototype :
% \xsubsectionframe{bg_color}
%                  {bg_altcolor}
%                  {fg_color}
%                  {picture}
%                  {title}
%                  {subtitle}
\newcommand{\xsubsectionframe}[6]{%
  \setbeamertemplate{background canvas}{%
    \setlength{\unitlength}{0.00625\paperwidth}%
    \begin{picture}(160,90)%
      \put(80,90){%
        \makebox(0,0)[ct]{%
          \includegraphics[width=\paperwidth,keepaspectratio]{#4}%
        }%
      }%
      \put(0,0){%
        \color{#1}\rule{\paperwidth}{0.5\paperheight}%
      }%
      \put(80,22.5){%
        \makebox(0,0)[c]{%
          \color{#3}\includegraphics[height=\paperheight/22,keepaspectratio]{beamerxx}%
        }%
      }%
      \put(160,22.5){%
        \makebox(0,0)[c]{%
          \color{#2}\includegraphics[height=3\paperheight/7,keepaspectratio]{beamerxarmes}%
        }%
      }%
      \put(80,28){%
        \makebox(0,0)[cb]{%
          \begin{minipage}{120\unitlength}\centering%
            \large\rmfamily\bfseries\color{#3}\MakeUppercaseWithNewline{#5}%
          \end{minipage}%
        }%
      }%
      \put(80,18){%
        \makebox(0,0)[ct]{%
          \begin{minipage}{120\unitlength}\centering%
            \large\rmfamily\color{#3}#6%
          \end{minipage}%
        }%
      }%
    \end{picture}%
  }
  \begin{frame}[plain,c]
  \end{frame}
  \setbeamertemplate{background canvas}{}
}

%--- XFrame
% Prototype:
% \begin{xframe}
%       {title}
%       {subtitle}
\newenvironment{xframe}[2]{%
  \setbeamertemplate{background canvas}{%
    \setlength{\unitlength}{0.00625\paperwidth}%
    \begin{picture}(160,90)%
      \put(158,88){%
        \makebox(0,0)[rt]{%
          \color{lightbleu303}\includegraphics[height=\paperheight/7,keepaspectratio]{beamerxarmes}%
        }%
      }%
    \end{picture}%
  }%
  \begin{frame}{#1}{#2}%
}{%
  \end{frame}%
}

%--- XPic
% Prototype:
% \begin{xpic}
%       [size adjustment]
%       {picture}
\newenvironment{xpic}[2][hw]{%
  \setbeamertemplate{background canvas}{%
    \setlength{\unitlength}{0.00625\paperwidth}%
    \begin{picture}(160,90)%
      \put(80,45){%
        \makebox(0,0)[c]{%
          \ifthenelse{\equal{#1}{hw}}{%
            \includegraphics[width=\paperwidth,height=\paperheight,keepaspectratio]{#2}%
          }{\ifthenelse{\equal{#1}{wh}}{%
            \includegraphics[width=\paperwidth,height=\paperheight,keepaspectratio]{#2}%
          }{\ifthenelse{\equal{#1}{h}}{%
            \includegraphics[height=\paperheight]{#2}%
          }{%
            \includegraphics[width=\paperwidth]{#2}%
          }}}%
        }%
      }%
    \end{picture}
  }%
  \begin{frame}[plain,b]\centering%
}{%
    \vspace*{5mm}
  \end{frame}%
}



\endinput
