% Copyright 2007 by Nathanaël COTTIN <www.ncottin.net>.
%
% This program can be redistributed and/or modified under the terms
% of the GNU Public License, version 3.

%
% Caution : \footnote places note beside minipage
% \footnotemark to place note in page footer
%

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{imageboxes}[2007/09/21 version 0.2.4]

\RequirePackage{ifthen}	% To get \provideboolean command

\provideboolean{@french}
\DeclareOption{french}{\@frenchtrue}
\ProcessOptions\relax

\RequirePackage{qsymbols}

\def\@thedeflabel{Definition}
\newcommand{\definitionlabel}[1]
	{\def\@thedeflabel{#1}}

\def\@thetheoremlabel{Theorem}
\newcommand{\theoremlabel}[1]
	{\def\@thetheoremlabel{#1}}
	
\def\@theexamplelabel{Example}
\newcommand{\examplelabel}[1]
	{\def\@theexamplelabel{#1}}

\def\@thelemmalabel{Lemma}
\newcommand{\lemmalabel}[1]
	{\def\@thelemmalabel{#1}}

\def\@theproplabel{Proposition}
\newcommand{\propositionlabel}[1]
	{\def\@theproplabel{#1}}

\def\@theremlabel{Note}
\newcommand{\remarklabel}[1]
	{\def\@theremlabel{#1}}

\def\@thedemlabel{Demonstration}
\newcommand{\demonstrationlabel}[1]
	{\def\@thedemlabel{#1}}

\def\@thealertlabel{Caution}
\newcommand{\alertlabel}[1]
	{\def\@thealertlabel{#1}}

\def\@thedefimage{}
\def\definitionimage#1{\def\@thedefimage{#1}}

\def\@thetheoremimage{}
\def\theoremimage#1{\def\@thetheoremimage{#1}}

\def\@theexampleimage{}
\def\exampleimage#1{\def\@theexampleimage{#1}}

\def\@thelemmaimage{}
\def\lemmaimage#1{\def\@thelemmaimage{#1}}

\def\@thepropimage{}
\def\propositionimage#1{\def\@thepropimage{#1}}

\def\@theremimage{}
\def\remarkimage#1{\def\@theremimage{#1}}

\def\@thedemimage{}
\def\demonstrationimage#1{\def\@thedemimage{#1}}

\def\@thealertimage{}
\def\alertimage#1{\def\@thealertimage{#1}}

\newlength{\@theboximageleftmargin}
\setlength{\@theboximageleftmargin}{0pt}
\newcommand{\imageboxleftmargin}[1]
	{\setlength{\@theboximageleftmargin}{#1}}

\newlength{\@theboximagerightmargin}
\setlength{\@theboximagerightmargin}{0pt}
\newcommand{\imageboxrightmargin}[1]
	{\setlength{\@theboximagerightmargin}{#1}}

\newlength{\@theboximagetopmargin}
\setlength{\@theboximagetopmargin}{0pt}
\newcommand{\imageboxtopmargin}[1]
	{\setlength{\@theboximagetopmargin}{#1}}

\newlength{\@theboximagebottommargin}
\setlength{\@theboximagebottommargin}{0pt}
\newcommand{\imageboxbottommargin}[1]
	{\setlength{\@theboximagebottommargin}{#1}}

\newcommand{\imageboxallmargins}[1]
	{\imageboxmargins{#1}{#1}{#1}{#1}}

\newcommand{\imageboxmargins}[4]
	{\imageboxtopmargin{#1}
	\imageboxleftmargin{#2}
	\imageboxrightmargin{#3}
	\imageboxbottommargin{#4}}

\newcommand{\imageboxdefaultmargins}
	{\imageboxmargins{\parskip}{0pt}{0pt}{0pt}}

\newlength{\@theboximagewidth}
\newcommand{\imageboximagewidth}[1]
	{\setlength{\@theboximagewidth}{#1}}

\newcommand{\imageboxdefaultimagewidth}
	{\imageboximagewidth{.9cm}}

\imageboxdefaultimagewidth

\newlength{\@theboximageinnerpadding}
\newcommand{\imageboxinnerpadding}[1]
	{\setlength{\@theboximageinnerpadding}{#1}}

\newcommand{\imageboxdefaultinnerpadding}
	{\imageboxinnerpadding{.2cm}}

\imageboxdefaultinnerpadding

\newlength{\@theboximagewithinnerpadding}
\newlength{\@theboximageremainingwidth}

\newlength{\@theimageboxwidth}
\newcommand{\imageboxwidth}[1]
	{\setlength{\@theimageboxwidth}{#1}}

\newcommand{\imageboxdefaultwidth}
	{\imageboxwidth{\linewidth}}

\newcommand{\imageboxdefaults}
	{\imageboxdefaultwidth
	\imageboxdefaultimagewidth
	\imageboxdefaultmargins
	\imageboxdefaultinnerpadding}

\provideboolean{@imageboxopened}
\provideboolean{@imageboxhasimage}

\def\@boxtype{}

\def\beginblockinfo#1#2#3
	{\ifthenelse
		{\equal{#2}{}}
		{\csname #1\endcsname{#3}}
		{\csname #1\endcsname{#2}}}

\def\beginimageboxenvironment#1#2#3#4	% #1 = type (block, alertblock, ...), #2 = alt label, #3 = label, #4 = image
	{\@imageboxopenedtrue
	\setlength{\@theboximageremainingwidth}{\@theimageboxwidth}
	\addtolength{\@theboximageremainingwidth}{-\@theboximageleftmargin}
	\addtolength{\@theboximageremainingwidth}{-\@theboximagerightmargin}
	
	\vspace{\@theboximagetopmargin}%
	\hspace{\@theboximageleftmargin}%
	\ifthenelse
		{\equal{#4}{}}	% No image
		{\@imageboxhasimagefalse%
		\begin{minipage}{\@theboximageremainingwidth}%
		\beginblockinfo{#1}{#2}{#3} } % This space is necessary (is this a LateX bug?)
		{\@imageboxhasimagetrue%
		\setlength{\@theboximagewithinnerpadding}{\@theboximagewidth}%
		\addtolength{\@theboximagewithinnerpadding}{\@theboximageinnerpadding}%
		\addtolength{\@theboximageremainingwidth}{-\@theboximagewithinnerpadding}%
		\protect\parbox{\@theboximagewithinnerpadding}{\includegraphics[width=\@theboximagewidth]{#4}}%
		\begin{minipage}{\@theboximageremainingwidth}}}

\def\endblockinfo#1
	{\csname end#1\endcsname}

% \newcommand does not work!
\def\endimageboxenvironment#1
	{\@imageboxopenedfalse%
	\if@imageboxhasimage
	\else
		\endblockinfo{#1}
	\fi
	\end{minipage}%
	\hspace{\@theboximagerightmargin}
	\vspace{\@theboximagebottommargin}}

\newenvironment{imagebox}[4][]	% #1 = type (optional), #2 = alt label, #3 = label, #4 = image
	{\ifthenelse
		{\equal{#1}{}}
		{\def\@boxtype{block}}
		{\def\@boxtype{#1}}
	\beginimageboxenvironment{\@boxtype}{#2}{#3}{#4}}
	{\endimageboxenvironment{\@boxtype}
	\def\@boxtype{}}

\@ifundefined{definition}
	{\newenvironment{definition}[1][]{}{}}
	{}

\@ifundefined{theorem}
	{\newenvironment{theorem}[1][]{}{}}
	{}

\@ifundefined{example}
	{\newenvironment{example}[1][]{}{}}
	{}

\@ifundefined{lemma}
	{\newenvironment{lemma}[1][]{}{}}
	{}

\@ifundefined{alert}
	{\newenvironment{alert}[1][]{}{}}
	{}

\renewenvironment{definition}[1][]
	{\begin{imagebox}{#1}{\@thedeflabel}{\@thedefimage} }	% The space is necessary (bug ?)
	{\end{imagebox}}
\renewenvironment{theorem}[1][]
	{\begin{imagebox}{#1}{\@thetheoremlabel}{\@thetheoremimage} }
	{\end{imagebox}}
\renewenvironment{example}[1][]
	{\begin{imagebox}[exampleblock]{#1}{\@theexamplelabel}{\@theexampleimage} }
	{\end{imagebox}}
\renewenvironment{lemma}[1][]
	{\begin{imagebox}{#1}{\@thelemmalabel}{\@thelemmaimage} }
	{\end{imagebox}}
\newenvironment{proposition}[1][]
	{\begin{imagebox}{#1}{\@theproplabel}{\@thepropimage} }
	{\end{imagebox}}
\newenvironment{remark}[1][]
	{\begin{imagebox}{#1}{\@theremlabel}{\@theremimage} }
	{\end{imagebox}}
\newenvironment{demonstration}[1][]
	{\begin{imagebox}{#1}{\@thedemlabel}{\@thedemimage} }
	{\hfill\ensuremath{\Box}\end{imagebox}}	%\qed or \blacksquare are other possibilities
\renewenvironment{alert}[1][]
	{\begin{imagebox}[alertblock]{#1}{\@thealertlabel}{\@thealertimage} }
	{\end{imagebox}}

\newcommand{\frenchenv}
	{\definitionlabel{D\'efinition}
	\theoremlabel{Th\'eor\`eme}
	\examplelabel{Exemple}
	\lemmalabel{Lemme}
	\propositionlabel{Proposition}
	\remarklabel{Remarque}
	\demonstrationlabel{D\'emonstration}
	\alertlabel{Attention}}

\newcommand{\footnotepage}[1]
	{\if@imageboxopened
		\footnotemark%
		\footnotetext{#1}%
	\else
		\footnote{#1}
	\fi}

\if@french
	\frenchenv
\fi

\AtBeginDocument{
	\imageboxdefaultwidth
	\imageboxtopmargin{\parskip}
}

\endinput

