\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{xeboiboites}[2015/06/18 version 0.9c]
\RequirePackage{xkeyval}
\RequirePackage{tikz}
\RequirePackage{pgf}
\RequirePackage{amsthm}
\RequirePackage{framed}
\usetikzlibrary{decorations.pathmorphing,calc}
\pgfdeclarelayer{background}
\pgfsetlayers{background,main}
\pgfmathsetseed{1} % To have predictable results



\define@key{boxedtheorem}{thcounter}{\def\thcounter{#1}}
\define@key{boxedtheorem}{size}{\def\size{#1}}
\define@key{boxedtheorem}{small box style}{\def\smallboxstyle{#1}}
\define@key{boxedtheorem}{big box style}{\def\bigboxstyle{#1}}
\define@key{boxedtheorem}{spanning style}{\def\spanningstyle{#1}}
\define@key{boxedtheorem}{headfont}{\def\headfont{#1}}
\define@key{boxedtheorem}{image}{\def\image{#1}}
\define@key{boxedtheorem}{broken edges}{\def\tornstyle{#1}}
\define@key{boxedtheorem}{other edges}{\def\otheredges{#1}}
\presetkeys{boxedtheorem}{headfont=, image=logo-tex, thcounter=, small box style=,%
                            big box style=, spanning style=, size = .9\textwidth,%
                            broken edges=, other edges=}{}


%To expand properly \smallboxstyle, \bigboxstyle and \spanningstyle in \tikset.
\tikzset{
  execute style/.style={#1},
  execute macro/.style={execute style/.expand once=#1},
}
\newcommand{\couleurs}[1][]{%
    \setkeys{boxedtheorem}{#1}
    \tikzset{fancytitle/.style={draw=black, rectangle,right=10pt}}
    \tikzset{fancytitle/.append style={execute macro=\smallboxstyle}}
    \tikzset{mybox/.style={draw=black, rectangle, inner sep=10pt, inner ysep=20pt}}
    \tikzset{mybox/.append style={execute macro=\bigboxstyle}}
    \tikzset{formulabox/.style={draw=black, rectangle}}
    \tikzset{formulabox/.append style={execute macro=\bigboxstyle}}
    \tikzset{span/.style={black}}
    \tikzset{span/.append style={execute macro=\spanningstyle}}
    \tikzset{torn/.style={execute macro=\tornstyle}}
    \tikzset{other edges/.style={execute macro=\otheredges}}
}

%Hand drawn style : http://tex.stackexchange.com/questions/39296/simulating-hand-drawn-lines
\pgfdeclaredecoration{penciline}{initial}{%
    \state{initial}[width=+\pgfdecoratedinputsegmentremainingdistance,auto corner on length=1mm,]{%
        \pgfpathcurveto%
        {% From
            \pgfqpoint{\pgfdecoratedinputsegmentremainingdistance}%
                            {\pgfdecorationsegmentamplitude}%
        }%
        {%  Control 1
        \pgfmathrand
        \pgfpointadd{\pgfqpoint{\pgfdecoratedinputsegmentremainingdistance}{0pt}}%
                        {\pgfqpoint{-\pgfdecorationsegmentaspect\pgfdecoratedinputsegmentremainingdistance}%
                                        {\pgfmathresult\pgfdecorationsegmentamplitude}%
                        }%
        }%
        {% To 
        \pgfpointadd{\pgfpointdecoratedinputsegmentlast}{\pgfpoint{1pt}{1pt}}%
        }%
    }%
    \state{final}{}%
}



% To create a nice looking box 
% http://www.texample.net/tikz/examples/boxes-with-text-and-math/
\newsavebox{\boiboite}
\newcommand{\titre}{Titre}
\newenvironment{boite}[2][]%
    {%
    \renewcommand{\titre}{#2}
    \couleurs[#1]
    \begin{lrbox}{\boiboite}%
     \begin{minipage}[!h]{\size}
    }%
    {%
     \end{minipage}
    \end{lrbox}
    \begin{center}
    \begin{tikzpicture}
        \node[mybox] (box) {\usebox{\boiboite}};
        \node[fancytitle] at (box.north west) {\titre};
    \end{tikzpicture}
    \end{center}
    }


\newcommand{\newboxedtheorem}[4][]{%
    \couleurs[#1]
    \@ifnotempty{#4}{%
      \@ifundefined{the#4}{\@ifundefined{\thcounter}{\newcounter{#4}}{%
      \newcounter{#4}[\thcounter ]}}{}%
    }
      \newenvironment{#2}[1][]{%
      \@ifnotempty{#4}{\refstepcounter{#4}}
      \begin{boite}[#1]{{\headfont#3\@ifnotempty{#4}{ \csname the#4\endcsname}}\@ifnotempty{##1}{ \headfont(##1)}}
    }%
    {%
      \end{boite}
    }
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Boxes with no title

\newsavebox{\boiboitedeux}
\newenvironment{notitlebox}[1][]%
    {%
      \couleurs[#1]
      \begin{lrbox}{\boiboitedeux}%
      \begin{minipage}[!h]{\size}
    }%
    {%
      \end{minipage}
      \end{lrbox}
      \begin{center}
        \begin{tikzpicture}
          \node [formulabox] (box){\usebox{\boiboitedeux}};
        \end{tikzpicture}
      \end{center}
    }

\newcommand{\newboxedequation}[2][]{%
      \couleurs[#1]
      \newenvironment{#2}[1][]{%
      \begin{notitlebox}[#1]
    }%
    {%
      \end{notitlebox}
    }
}    

%%%%%%%%%%%%%%%%%%%%%% Spanning environment (line in the margin + image)
% Macro to draw the shape behind the text, when it fits completly in the page

\def\spanframe#1{%
\tikz{%
  \node[inner xsep=1.1em] (A) {#1};% Draw the text of the node
  \begin{pgfonlayer}{background}% Draw the shape behind
  \draw[span] (A.north west) -- (A.south west);%
  \end{pgfonlayer}}}%

% Macro to draw the shape, when the text will continue in next page
\def\spanframetop#1{%
\tikz{%
  \node[inner xsep=1.1em] (A) {#1};%    % Draw the text of the node
  \begin{pgfonlayer}{background}%    
    \draw[span] (A.north west) -- (A.south west);%
  \end{pgfonlayer}}}%

% Macro to draw the shape, when the text continues from previous page
\def\spanframebottom#1{%
\tikz{%
  \node[inner xsep=1.1em] (A) {#1};%   % Draw the text of the node
  \begin{pgfonlayer}{background}%   
  \draw[span] (A.north west) -- (A.south west);%
  \end{pgfonlayer}}%
  }%

% Macro to draw the shape, when both the text continues from previous page
% and it will continue in next page
\def\spanframemiddle#1{%
\tikz{%
  \node[inner xsep=1.1em] (A) {#1};%   % Draw the text of the node
  \begin{pgfonlayer}{background}%   
  \draw[span] (A.north west) -- (A.south west);%
  \end{pgfonlayer}}}%

% Define the environment which puts the frame
% In this case, the environment also accepts an argument with an optional
% title (which defaults to ``Example'', which is typeset in a box overlaid
% on the top border

\newcommand{\newspanning}[4][]{%
    \couleurs[#1]%
    \@ifnotempty{#4}{%
      \@ifundefined{the#4}{\@ifundefined{\thcounter}{\newcounter{#4}}{%
      \newcounter{#4}[\thcounter ] }}{}%
    }
    \newenvironment{#2}[1][]{%
    \couleurs[#1]
    \@ifnotempty{#4}{\refstepcounter{#4}}
    \def\FrameCommand{\spanframe}%
    \def\FirstFrameCommand{\spanframetop}%
    \def\LastFrameCommand{\spanframebottom}%
    \def\MidFrameCommand{\spanframemiddle}%
    \vskip 1.3\baselineskip%
    \MakeFramed {\FrameRestore}%
    \noindent\tikz\node[inner sep=1ex,,fill=white, %
          anchor=west, overlay] at (-2em, 2em) {\headfont#3%
          \@ifnotempty{#4}{ \csname the#4\endcsname}\@ifnotempty{##1}{ \headfont(##1)}};%
    \tikz\node[anchor=west, overlay] at (-1.95cm,-.3cm)%
        {\includegraphics[width=1cm]{\image}};%
    \ignorespaces%    
    }%
    {%
        \endMakeFramed%
    }
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%  Breakable boxes
%%%%%%%%%%%%% http://www.texample.net/tikz/examples/framed-tikz/
% Macro to draw the shape behind the text, when it fits completly in the page
\def\someframe#1{
\tikz{
  \node[inner sep=8pt] (A) {#1};  % Draw the text of the node
  \noindent
  \begin{pgfonlayer}{background}  % Draw the shape behind
  \draw[mybox, other edges]
        (A.south east) -- (A.south west) -- (A.north west) -- (A.north east) -- cycle;      
  \end{pgfonlayer}}}

% Macro to draw the shape, when the text will continue in next page
\def\someframetop#1{
\tikz{
  \node[inner sep=5pt] (A) {#1};    % Draw the text of the node
  \begin{pgfonlayer}{background}    
  \draw[mybox]              % Draw the ``complete shape'' behind
        (A.south east) decorate[torn] {-- (A.south west)} 
        decorate[other edges] {-- (A.north west) -- (A.north east) -- cycle};
  \end{pgfonlayer}}}

% Macro to draw the shape, when the text continues from previous page
\def\someframebottom#1{
\tikz{
  \node[inner sep=5pt] (A) {#1};   % Draw the text of the node
  \begin{pgfonlayer}{background}   
  \draw[mybox]              % Draw the ``complete shape'' behind
        decorate[other edges] {(A.south east) -- (A.south west) -- (A.north west)}
        decorate[torn] {-- (A.north east)} decorate[other edges] {-- cycle};
  \end{pgfonlayer}}}

% Macro to draw the shape, when both the text continues from previous page
% and it will continue in next page
\def\someframemiddle#1{
\tikz{
  \node[inner sep=5pt] (A) {#1};   % Draw the text of the node
  \begin{pgfonlayer}{background}   
  \draw[mybox]              % Draw the ``complete shape'' behind
        (A.south east) decorate[torn] {-- (A.south west)} 
        decorate[other edges]{-- (A.north west)}
        decorate[torn] {-- (A.north east)} 
        decorate[other edges]{-- cycle};
  \end{pgfonlayer}}}


% Command to define a new environment that can span over multiple pages
\newcommand{\newbreakabletheorem}[4][]{%
    \couleurs[#1]%
    \@ifnotempty{#4}{%
      \@ifundefined{the#4}{\@ifundefined{\thcounter}{\newcounter{#4}}{%
      \newcounter{#4}[\thcounter ] }}{}%
    }
    \newenvironment{#2}[1][]{%
    \couleurs[#1]%
    \@ifnotempty{#4}{\refstepcounter{#4}}
    \def\FrameCommand{\someframe}%
    \def\FirstFrameCommand{\someframetop}%
    \def\LastFrameCommand{\someframebottom}%
    \def\MidFrameCommand{\someframemiddle}%
    \vskip 1.2\baselineskip
    \MakeFramed{\hsize\size\FrameRestore}
    \noindent  \tikz  \node[inner sep=1ex,anchor=west, overlay, fancytitle] at (0pt,6pt) %
        {\headfont#3 \@ifnotempty{#4}{\csname the#4\endcsname}\@ifnotempty{##1}{ \headfont(##1)}}; \par \noindent
    \ignorespaces%    
    }%
    {%
        \endMakeFramed%
    }
}



%%%%%%%%%%%%%%%%%%%%%%%%%%%  Parchment boxes
%%%%%%%%%%%%% http://www.texample.net/tikz/examples/framed-tikz/
% Macro to draw the shape behind the text, when it fits completly in the
% page

\def\parchmentframe#1{
\tikz{
  \node[inner sep=8pt] (A) {#1};  % Draw the text of the node
  \noindent
  \begin{pgfonlayer}{background}  % Draw the shape behind
  \draw[mybox,other edges]
        (A.south east) -- (A.south west) -- (A.north west) -- (A.north east) -- cycle;      
  \end{pgfonlayer}}}

% Macro to draw the shape, when the text will continue in next page
\def\parchmentframetop#1{
\tikz{
  \node[inner sep=8pt] (A) {#1};    % Draw the text of the node
  \begin{pgfonlayer}{background}
  \draw[torn,overlay]               % Add the torn lower border
        ($(A.south east)-(0,.3)$) -- ($(A.south west)-(0,.3)$) -- 
        ($(A.south west)+(0,.4)$) -- ($(A.south east)+(0,.4)$);
  \draw[mybox,overlay]              % Draw the ``complete shape'' behind
        ($(A.south east)+(0,.1)$) decorate[torn] {-- ($(A.south west)+(0,.1)$)}
        decorate[other edges] {-- (A.north west) -- (A.north east) -- cycle};
  \end{pgfonlayer}}}

% Macro to draw the shape, when the text continues from previous page
\def\parchmentframebottom#1{
\tikz{
  \node[inner sep=8pt] (A) {#1};   % Draw the text of the node
  \begin{pgfonlayer}{background}
  \draw[torn,overlay]               % Add the torn upper border
        ($(A.north east)-(0,.4)$) -- ($(A.north west)-(0,.4)$) -- 
        ($(A.north west)+(0,.3)$) -- ($(A.north east)+(0,.3)$) -- cycle;
  \draw[mybox,overlay]              % Draw the ``complete shape'' behind
        (A.south east) decorate[other edges] {-- (A.south west) -- (A.north west) }
        decorate[torn] {-- (A.north east)} decorate[other edges] {-- cycle};
  \end{pgfonlayer}}}

% Macro to draw the shape, when both the text continues from previous page
% and it will continue in next page
\def\parchmentframemiddle#1{
\tikz{
  \node[inner sep=8pt] (A) {#1};   % Draw the text of the node
  \begin{pgfonlayer}{background}
    \draw[torn,overlay]               % Add the torn upper border
        ($(A.north east)-(0,.4)$) -- ($(A.north west)-(0,.4)$) -- 
        ($(A.north west)+(0,.3)$) -- ($(A.north east)+(0,.3)$) -- cycle;
    \draw[torn,overlay]               % Add the torn lower border
        ($(A.south east)-(0,.3)$) -- ($(A.south west)-(0,.3)$) -- 
        ($(A.south west)+(0,.4)$) -- ($(A.south east)+(0,.4)$);
  \draw[mybox,overlay]              % Draw the ``complete shape'' behind
        (A.south east) decorate[torn] { -- (A.south west)}
        decorate[other edges]{-- (A.north west)}
        decorate[torn] {-- (A.north east)} 
        decorate[other edges]{-- cycle};
  \end{pgfonlayer}}}


% Command to define a new environment that can span over multiple pages
\newcommand{\newparchment}[4][]{%
    \couleurs[#1]%
    \@ifnotempty{#4}{%
      \@ifundefined{the#4}{\@ifundefined{\thcounter}{\newcounter{#4}}{%
      \newcounter{#4}[\thcounter ] }}{}%
    }
    \newenvironment{#2}[1][]{%
    \couleurs[#1]%
    \@ifnotempty{#4}{\refstepcounter{#4}}
    \def\FrameCommand{\parchmentframe}%
    \def\FirstFrameCommand{\parchmentframetop}%
    \def\LastFrameCommand{\parchmentframebottom}%
    \def\MidFrameCommand{\parchmentframemiddle}%
    \vskip 1.2\baselineskip
    \MakeFramed{\hsize\size\FrameRestore}
    \noindent  \tikz  \node[inner sep=1ex,anchor=west, overlay, fancytitle] at (0pt,10pt) %
        {\headfont#3 \@ifnotempty{#4}{\csname the#4\endcsname}\@ifnotempty{##1}{ \headfont(##1)}}; \par \noindent
    \ignorespaces%    
    }%
    {%
        \endMakeFramed%
    }
}
