% package identification
\def\tb@PACKAGENAME{pgfextra}
\def\tb@VERSION{1 }
\def\tb@DATE{2009/04/08}
\def\tb@AUTHOR{Fabien Dagnat <Fabien.Dagnat@telecom-bretagne.eu>}
\def\tb@PRESENTATION{Various extensions to pgf and pgfkeys}
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{\tb@PACKAGENAME}[\tb@DATE]
\def\tb@ten@spaces{\@spaces\@spaces\space\space}
\def\tb@twentyone@spaces{\tb@ten@spaces\tb@ten@spaces\space}
\def\tb@thirtyfive@spaces{\tb@ten@spaces\tb@ten@spaces\tb@ten@spaces\@spaces\space}
\def\tb@STAR@LINE{**************************************************************^^J}
\def\tb@LINE#1{* #1 *^^J}
\def\tb@LINE@PRESENTATION{\tb@LINE{\tb@PRESENTATION\tb@twentyone@spaces}}
\def\tb@LINE@AUTHOR{\tb@LINE{Author : \tb@AUTHOR}}
\def\tb@LINE@VERSION{\tb@LINE{Version \tb@VERSION of \tb@DATE\tb@thirtyfive@spaces}}
\typeout{^^J\tb@STAR@LINE\tb@LINE@PRESENTATION\tb@LINE@AUTHOR\tb@LINE@VERSION\tb@STAR@LINE}
%\newcommand*\tbClasseVersionDate{version \tb@VERSION du \tb@DATE@fr}
\RequirePackage{tikz}
\RequirePackage{relsize}
% give a label to a text (put in a node of a sub picture)
\newcommand{\marknode}[3][]{%
  \tikz[remember picture,baseline=(#2.base)]{\node[rectangle,inner sep=0pt,minimum size=0mm,outer sep=0mm,font=\relsize{0},#1](#2){#3};}%
}
% MnSymbols \filledmedtriangle{right,left,up,down} replacement
\usetikzlibrary{shapes.geometric}
\tikzset{letriangle/.style={regular polygon,regular polygon sides=3,rounded corners=0.5pt,inner sep=0pt,minimum size=2.7mm,outer sep=0mm}}
\newcommand*{\toup}{\tikz[baseline=-0.6ex]{\node[letriangle,fill]{};}}
\newcommand*{\toright}{\tikz[baseline=-0.6ex]{\node[letriangle,rotate=-90,fill]{};}}
\newcommand*{\toleft}{\tikz[baseline=-0.6ex]{\node[letriangle,rotate=90,fill]{};}}
\newcommand*{\todown}{\tikz[baseline=-0.6ex]{\node[letriangle,rotate=180,fill]{};}}
% get the coordinate of a point (#1) in #2 for x and #3 for y
\newcommand*{\getcoordinate}[3]{%#1 -> tikz point #2 -> dim for x #3 -> dim for y
  \tikz@scan@one@point\pgfextra@extract(#1){#2}{#3}%
}
\newcommand*{\pgfextra@extract}[3]{%
  \pgfextractx{#2}{#1}%
  \pgfextracty{#3}{#1}%
}
% extensions to pgfkeys
% a macro to test if a key has an empty value
\newif\ifnovalue%
\newcommand{\pgfextra@ifnovalue}[1]{%#1 key #2 code for then #3 code for else
  \expandThreeTimes{\pgfkeysvalueof{#1}}%
  \ifx\expandResult\pgfkeysnovalue@text%
    \novaluetrue%
  \else%
    \ifx\expandResult\@empty%
      \novaluetrue%
    \else%
      \novaluefalse%
    \fi%
  \fi%
}
% add before and after if the value is not empty
\newcommand*{\pgfextra@add@ifnonempty}[3]{%#1 -> key #2 -> before #3 -> after
  \pgfextra@ifnovalue{#1}%
  \ifnovalue%
  \else%
    \pgfkeysaddvalue{#1}{#2}{#3}%
  \fi%
}
% add text before and if the value is non empty add a sep between text and value
\newcommand*{\pgfextra@add@before}[3]{%#1 -> key #2 -> before #3 -> sep non empty
  \pgfextra@add@ifnonempty{#1}{#3}{}%
  \pgfkeysaddvalue{#1}{#2}{}%
}
% the corresponding keys
\pgfkeys{/handlers/.add if non empty/.code 2 args=\pgfextra@add@ifnonempty{\pgfkeyscurrentpath}{#1}{#2}}
\pgfkeys{/handlers/.add before/.code 2 args=\pgfextra@add@before{\pgfkeyscurrentpath}{#1}{#2}}


% init with reinit, the principle is to keep a reinit function in .@initfun subkey
% each call to reinit execute this function and assign the result to the key
% this function is a macro taking one argument being a macro in which it puts its result
% two versions of init exist, either we give it directly the function (.initfun subkey) or 
% one can give a value (.initval subkey)
% init functions
\pgfkeys{/handlers/.initfun/.code=%#1 is a macro taking one argument being a macro (in which it puts its result)
  % letting this macro in .@initfun 
  \pgfkeyslet{\pgfkeyscurrentpath/.@initfun}{#1}%
  % executing the init function and getting the result in @temp
  \def\@temp{}%
  #1{\@temp}%
  % initialising the key with the value @temp
  \expandafter\pgfkeyssetvalue\expandafter{\expandafter\pgfkeyscurrentpath\expandafter}\expandafter{\@temp}%
}
% init values
\pgfkeys{/handlers/.initval/.code=%
  % keep the init val in .@init
  \pgfkeyssetvalue{\pgfkeyscurrentpath/.@init}{#1}%
  % set the keys with this value
  \pgfkeyssetvalue{\pgfkeyscurrentpath}{#1}%
}
% reinit
\pgfkeys{/handlers/.reinit/.code=%
  % forbidding given value
  \def\pgfextra@temp{#1}%
  \ifx\pgfextra@temp\pgfkeysnovalue@text%
    % memorisation of the current path
    \edef\mypath{\pgfkeyscurrentpath}%
    % does a init val exists?
    \pgfkeysifdefined{\mypath/.@init}{%
       % yes, simply reset the key with this value (the value is obtained by expanding three times, ...)
      \expandThreeTimes{\pgfkeysvalueof{\mypath/.@init}}%
      \expandafter\pgfkeyssetvalue\expandafter{\expandafter\mypath\expandafter}\expandafter{\expandResult}%
     }{%
       % no, is there an init function?
       \pgfkeysifdefined{\mypath/.@initfun}{%
         % yes, getting the saved initfun
         \def\pgfextra@temp{\pgfkeysvalueof{\mypath/.@initfun}}%
         % execute the initfun
         \def\pgfextra@temp@result{}%
         \pgfextra@temp{\pgfextra@temp@result}%
         % reinitialising the key with the result of the init fun
         \expandafter\pgfkeyssetvalue\expandafter{\expandafter\mypath\expandafter}\expandafter{\pgfextra@temp@result}%
       }{%
         \PackageError{pgfextra}{reinit on a key '\mypath with no init value or function}{}%
       }%
     }%
   \else%
     \PackageError{pgfextra}{reinit has no value}{}%
   \fi%
}
% A macro to reinit a set of keys
\newcommand{\reinit}[2]{%#1 -> the path, #2 -> comma separated list of keys
  \@for\name:=#2\do{%
    \edef\key{#1/\name/.reinit}%
    \expandafter\pgfkeys\expandafter{\key}%
  }%
}
% Some utility macros
% result of expansion is in \result
\def\expandTwoTimes#1{%
  \expandafter\expandafter\expandafter\def\expandafter\expandafter\expandafter\expandResult\expandafter\expandafter\expandafter{#1}%
}
\def\expandThreeTimes#1{%
  \expandTwoTimes{#1}%
  \expandTwoTimes{\expandResult}%
}
\def\expandFourTimes#1{%
  \expandTwoTimes{#1}%
  \expandTwoTimes{\expandResult}%
  \expandTwoTimes{\expandResult}%
}
% make a macro global
\newcommand{\makeGlobal}[1]{%
  \let\forglobal#1%
  \expandafter\gdef\expandafter#1\expandafter{\forglobal}%
}
% make a key global
\newcommand{\makeKeyGlobal}[1]{%
  \expandafter\def\expandafter\myname\expandafter{\csname pgfk@#1\endcsname}%
  \expandafter\let\expandafter\forglobal\myname%
  \expandafter\expandafter\expandafter\gdef\expandafter\myname\expandafter{\forglobal}%
}


%%% Local Variables: 
%%% mode: latex
%%% ispell-dictionary: "english"
%%% End: 
