% \iffalse
%<*copyright>
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% aeb_envelope.sty package,          2007-05-14        %%
%% Copyright (C) 2006  D. P. Story                      %%
%%   dpstory@acrotex.net                                %%
%%   storyd@owc.edu                                     %%
%%                                                      %%
%% This program can redistributed and/or modified under %%
%% the terms of the LaTeX Project Public License        %%
%% Distributed from CTAN archives in directory          %%
%% macros/latex/base/lppl.txt; either version 1 of the  %%
%% License, or (at your option) any later version.      %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%</copyright>
%<package>\NeedsTeXFormat{LaTeX2e}
%<package>\ProvidesPackage{aeb_envelope}
%<package> [2007/05/14 v1.0 AeB Pro eEnvelope (dps)]
%<*driver>
\documentclass{ltxdoc}
\usepackage[dviwindo,colorlinks,hyperindex]{hyperref}
\pdfstringdefDisableCommands{\let\\\textbackslash}%
\EnableCrossrefs \CodelineIndex
\OnlyDescription  % comment out for implementation details
\begin{document}
  \GetFileInfo{aeb_envelope.sty}
  \title{The \textsf{AeB Pro eEnvelope} Package\texorpdfstring{\\}{: }
  A member of the AeB Pro family}
  \author{D. P. Story\\
    Email: \texttt{storyd@owc.edu}}
  \date{processed \today}
  \maketitle
  \tableofcontents
  \let\Email\texttt
  \DocInput{aeb_envelope.dtx}
  \PrintIndex
\end{document}
%</driver>
% \fi
% \section{Introduction}
% The \textsf{AeB Pro eEnvelope} package (a.k.a. \texttt{aeb\_envelope}) is a package for creating
% electronic envelopes (some designs provided, courtesy Adobe and AcroTeX.Net), and some instructions
% for creating your own are provided in the documentation. The AeB Pro package is essential for attach
% your documents to the envelope.  After attachment, a mail dialog opens and you are asked to sent
    % the eEnvelope out to your recipients.
%    \begin{macrocode}
%<*package>
\RequirePackage{xkeyval}
%    \end{macrocode}
%    \begin{macro}{donotmail}
% Do not mail this eEnvelope immediately. The default is to mail immediately.
%    \begin{macrocode}
\DeclareOptionX{donotmail}{\def\mailitNow{false}}
\def\mailitNow{true}
%    \end{macrocode}
%    \end{macro}
%    \begin{macro}{path2folder}
% The path to the folder that contains the eEnvelope templates. Not needed if you use
% the set the envelope.cfg file to the path to the envelopes folder.
%    \begin{macrocode}
\DeclareOptionX{path2folder}{\def\aebEnvPath{#1}}
\def\aebEnvPath{/C}
%    \end{macrocode}
%    \end{macro}
%    \begin{macro}{envelope}
% The \texttt{envelope} option is used to select any of the four supplied envelopes: \texttt{aeb1},
% \texttt{aeb2}, \texttt{adobe1} and \texttt{adobe2}. the default is \texttt{aeb1}.
%    \begin{macrocode}
\define@choicekey+{aeb_envelope.sty}{envelope}[\val\nr]{aeb1,aeb2,adobe1,adobe2}
{%
    \ifcase\nr\relax
        \def\pathtoEnv{\aebEnvPath/aebenvelope1.pdf}
        \def\addressEnv{\addressEnvAebi}
        \def\aebenvDimensions{\margins{0pt}{0pt}{0pt}{0pt}%
            \screensize{50mm}{112mm}}
    \or
        \def\pathtoEnv{\aebEnvPath/aebenvelope2.pdf}
        \def\addressEnv{\addressEnvAebii}
        \def\aebenvDimensions{\margins{0pt}{0pt}{0pt}{0pt}%
            \screensize{50mm}{112mm}}
    \or
        \def\pathtoEnv{\aebEnvPath/template1.pdf}
        \def\addressEnv{\addressEnvAdobei}
        \def\aebenvDimensions{\margins{0pt}{0pt}{0pt}{0pt}%
            \screensize{135.5mm}{212.7mm}}
    \or
        \def\pathtoEnv{\aebEnvPath/template2.pdf}
        \def\addressEnv{\addressEnvAdobeii}
        \def\aebenvDimensions{\margins{0pt}{0pt}{0pt}{0pt}%
            \screensize{120mm}{250mm}}
    \fi
}{\PackageWarning{aeb_envelope}{%
    Bad choice for the envelope, permissible values\MessageBreak
    are aeb1,aeb2,adobe1 and adobe2.\MessageBreak
    Assuming a custom envelope, \val.\MessageBreak
    Be sure to set the values of \string\addressEnv\MessageBreak
    and \string\aebenvDimensions}%
    \def\pathtoEnv{\aebEnvPath/\val}%
}
%    \end{macrocode}
%    \end{macro}
% Set the defaults for the \texttt{envelope} option. If the user specifies \texttt{path2folder},
% this choice overrides the configuration file.
%    \begin{macrocode}
\def\pathtoEnv{\aebEnvPath/aebenvelope1.pdf}
%    \end{macrocode}
%    \begin{macro}{\setAddressEnv}
%    \begin{macro}{\setEnvDimensions}
% These are two convenience commands to be used by anyone who want to create
% and use their own eEnvelope. Use \cs{setAddressEnv} to place the elements
% of the envelope; and \cs{setEnvDimensions} to set the dimensions of the papersize
% to match the dimensions of the eEnvelope.
%
% The argument of \cs{setAddressEnv} should be \cs{put} commands, similar to the
% definitions of \cs{addressEnvAebi}, \cs{addressEnvAebii}, \cs{addressEnvAdobei}
% or \cs{addressEnvAdobeii}.
%    \begin{macrocode}
\newcommand{\setAddressEnv}[1]{\def\addressEnv{\noindent#1}}
\def\addressEnv{\addressEnvAebi}
\newcommand{\setEnvDimensions}[2]% #1 = width, #2 = height
    {\def\aebenvDimensions{\margins{0pt}{0pt}{0pt}{0pt}%
        \screensize{#2}{#1}}}
\setEnvDimensions{112mm}{50mm}
%    \end{macrocode}
%    \end{macro}
%    \end{macro}
% Get the configuration file, if it exists. The
%    \begin{macrocode}
\InputIfFileExists{envelope.cfg}{}{}
\ProcessOptionsX
\RequirePackage{graphicx}
\RequirePackage{aeb_pro}
%    \end{macrocode}
% \section{Main Macro Code}
% eEnvelope!
%    \begin{macro}{\mailTo}
% The main data structure for setting the parameters of the eEnvelope,
%    \begin{macrocode}
\define@boolkey{mailto}[mailto]{UI}[true]{}
\define@key{mailto}{ToName}{\def\mailtoName{#1}}
\define@key{mailto}{From}{\def\mailtoFrom{#1}}
\define@key{mailto}{To}{\def\mailtoEmail{#1}}
\define@key{mailto}{path}{\def\pathtoEnv{#1}}
\define@key{mailto}{CC}{\def\mailtoCC{#1}}
\define@key{mailto}{BCC}{\def\mailtoBCC{#1}}
\define@key{mailto}{Subject}{\def\mailtoSubject{#1}}
\define@key{mailto}{MessageEnvelope}{\def\mailtoMessageEnvelope{#1}}
\define@key{mailto}{MessageBody}{\def\mailtoMessageBody{#1}}
\mailtoUItrue
\def\mailtoUI{\ifmailtoUI true\else false\fi}
\let\mailtoName\@empty
\let\mailtoEmail\@empty
\let\mailtoCC\@empty
\let\mailtoBCC\@empty
\let\mailtoSubject\@empty
\let\mailtoMessageEnvelope\@empty
\let\mailtoMessageBody\@empty
%    \end{macrocode}
% With the preliminaries of defining the parameters, here is the defiinition
% of \cs{mailTo}.
%    \begin{macrocode}
\newcommand\mailTo[1]{\setkeys{mailto}{#1}}
%    \end{macrocode}
%    \end{macro}
%    \begin{macro}{\toggleAttachmentsPanel}
% A convenience command that toggles the attachments panel. The first parameter
% is the color of the text, the second parameter is the text. Sample usage
% \verb!\toggleAttachmentsPanel{red}{Click to view attachments}!
% If user does not have eforms loaded, then this command just typesets the text
% rather than creating a link with action.
%    \begin{macrocode}
\@ifpackageloaded{eforms}
{
    \providecommand{\toggleAttachmentsPanel}[2]{%
        \setLinkText[\Border{0 0 0}\A{/N/ShowHideFileAttachment/S/Named}]
        {\textcolor{#1}{#2}}}%
}{%
        \providecommand{\toggleAttachmentsPanel}[2]{\textcolor{#1}{#2}}%
}
%    \end{macrocode}
%    \end{macro}
%    \begin{macrocode}
\def\displayAddr#1{\makebox[0pt][r]{\makebox[.6in][r]{#1:}\enspace}\ignorespaces}
%    \end{macrocode}
%    \begin{macro}{\addressEnvAebi}
%    \begin{macro}{\addressEnvAebii}
%    \begin{macro}{\addressEnvAdobei}
%    \begin{macro}{\addressEnvAdobeii}
% The commands to put address info on the envelope, each correspond to
% the four envelope option: \texttt{aeb1}, \texttt{aeb2}, \texttt{adobe1}
% and \texttt{adobe2}, in that order.
%    \begin{macrocode}
\newcommand{\addressEnvAebi}
{%
    \noindent\put(125,60){%
    \begin{minipage}[t]{2in}\parindent0pt\raggedright\small
         \displayAddr{To}\mailtoName\\
         \displayAddr{From}\mailtoFrom\\
         \displayAddr{Message}\mailtoMessageEnvelope
    \end{minipage}}%
}
\newcommand{\addressEnvAebii}
{%
    \small
    \noindent
    \put(12,120){\mailtoFrom}%
    \put(100,60){\makebox[0pt][r]{To:\enspace}\mailtoName}%
    \put(100,45){\makebox[0pt][r]
        {Message:\enspace}\parbox[t]{120pt}
        {\raggedright\mailtoMessageEnvelope}}%
}
\newcommand{\addressEnvAdobei}
{%
    \large
    \noindent\put(32,160){\today}%
    \put(32,120){\parbox[t]{540pt}{\mailtoMessageEnvelope}}%
    \put(189,160){\mailtoName}%
    \put(390,160){\mailtoFrom}%
}

\newcommand{\addressEnvAdobeii}
{%
    \Large
    \noindent\put(334.5,90){\parbox[t]{250pt}{\mailtoMessageEnvelope}}%
    \put(364,119){\mailtoName}%
    \put(90,305){\mailtoFrom}%
}
%    \end{macrocode}
%    \end{macro}
%    \end{macro}
%    \end{macro}
%    \end{macro}
%    \begin{macro}{\assembleEnvelope}
% The end of the eEnvelope file must have this command, it replaces the
% \verb!\begin{document}! and \verb!\end{document}! environment.
%
% \texttt{envexecjs.def} inputs the doc assembly commands for inserting the background
% of the eEnvelope.
%    \begin{macrocode}
\def\inputEnvExecJS{\InputIfFileExists{envexecjs.def}{}}
\newcommand{\assembleEnvelope}
{%
    \par\aebenvDimensions
    \setlength{\unitlength}{1pt}%
    \AddToTemplate{addressEnv}
    \inputEnvExecJS
    \par\begin{document}\parskip0pt\strut\end{document}
}
%    \end{macrocode}
%    \end{macro}
%    \begin{macrocode}
%</package>
%<*envexecjs>
%    \end{macrocode}
% These are the after distill document assembly commands. We insert the
% eEnvelope file as background, we define the \texttt{mailItNow()} function to mail
% the package if the mail dialog is dismissed or if the document author
% used the \texttt{donotmail} option. Mail data is saved as a global variable
% \texttt{globa.oEmailArgs} that should be remembered by Acrobat until it is closed.
%    \begin{macrocode}
\begin{execJS}{envexecjs}
global.oEmailArgs = {
    bUI:\mailtoUI,
    cTo: "\mailtoEmail",
    cCc: "\mailtoCC",
    cBcc: "\mailtoBCC",
    cSubject: "\mailtoSubject",
    cMsg: "\mailtoMessageBody"
};
\addWatermarkFromFile({
    bOnTop:false,
    cDIPath:"\pathtoEnv"
});
function mailItNow()
{
    try { this.mailDoc(global.oEmailArgs) } catch(e) {}
}
console.println("mailItNow();");
\executeSave();
if ( \mailitNow\space)
    aebTrustedFunctions(this, aebMailDoc, global.oEmailArgs);
\end{execJS}
%    \end{macrocode}
%    \begin{macrocode}
%</envexecjs>
%<*package>
\thispagestyle{empty}
%</package>
%    \end{macrocode}
%  \Finale
\endinput
