% Copyright 2007-2009 by Massimo Redaelli
%
% This file may be distributed and/or modified
%
% 1. under the LaTeX Project Public License and/or
% 2. under the GNU Public License.
%
% See the file doc/LICENSE for more details.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%  Voltage management


\ctikzset{v^>/.style = {
		v = #1,
		\circuitikzbasekey/bipole/voltage/direction = forward,
		\circuitikzbasekey/bipole/voltage/position = above
	}
}

\ctikzset{v^</.style = {
		v = #1,
		\circuitikzbasekey/bipole/voltage/direction = backward,
		\circuitikzbasekey/bipole/voltage/position = above
	}
}

\ctikzset{v_>/.style = {
		v = #1,
		\circuitikzbasekey/bipole/voltage/direction = forward,
		\circuitikzbasekey/bipole/voltage/position = below
	} 
}

\ctikzset{v_</.style = {
		v = #1,
		\circuitikzbasekey/bipole/voltage/direction = backward,
		\circuitikzbasekey/bipole/voltage/position = below
	} 
}


% Default position varies whether the component is a voltage source
% or not
\ctikzset{v/.code = {
			\ifpgf@circuit@bipole@isvoltage
				\pgfkeys{\circuitikzbasekey/bipole/voltage/position=above,
					\circuitikzbasekey/bipole/voltage/direction=forward}
			\else
				\pgfkeys{\circuitikzbasekey/bipole/voltage/position=below,
					\circuitikzbasekey/bipole/voltage/direction=backward}
			\fi
	\pgfkeys{/tikz/circuitikz/bipole/voltage/label/name=#1}
	\ctikzsetvalof{bipole/voltage/label/unit}{}
	\ifpgf@circ@siunitx 
		\pgf@circ@handleSI{#1}
		\ifpgf@circ@siunitx@res 
			\edef\pgf@temp{\pgf@circ@handleSI@val}
			\pgfkeyslet{/tikz/circuitikz/bipole/voltage/label/name}{\pgf@temp}
			\edef\pgf@temp{\pgf@circ@handleSI@unit}
			\pgfkeyslet{/tikz/circuitikz/bipole/voltage/label/unit}{\pgf@temp}
		\else
		\fi
	\else
	\fi
	} 
}

\ctikzset{v_/.style = {v = #1, \circuitikzbasekey/bipole/voltage/position = below} }
\ctikzset{v^/.style = {v = #1, \circuitikzbasekey/bipole/voltage/position  = above} }
\ctikzset{v>/.style = {v = #1, \circuitikzbasekey/bipole/voltage/direction = forward} }
\ctikzset{v</.style = {v = #1, \circuitikzbasekey/bipole/voltage/direction = backward} }


%% Output routine for generic bipoles

\def\pgf@circ@drawvoltagegeneric{
	\pgfextra{
		\ifnum \ctikzvalof{mirror value}=-1
						\ifpgf@circuit@bipole@voltage@below\pgf@circuit@bipole@voltage@belowfalse\else\pgf@circuit@bipole@voltage@belowtrue\fi
		\fi
		
		\ifpgf@circuit@bipole@voltage@below
			\def\pgf@circ@voltage@angle{90}
		\else
			\def\pgf@circ@voltage@angle{-90} 
		\fi	
		
		\edef\pgf@temp{/tikz/circuitikz/bipoles/\pgfkeysvalueof{/tikz/circuitikz/bipole/kind}/voltage/distance from node}
		\pgfkeysifdefined{\pgf@temp}
			{ \edef\distacefromnode{\ctikzvalof{bipoles/\pgfkeysvalueof{/tikz/circuitikz/bipole/kind}/voltage/distance from node}} }
			{ \edef\distacefromnode{\ctikzvalof{voltage/distance from node}} }
		\edef\pgf@temp{/tikz/circuitikz/bipoles/\pgfkeysvalueof{/tikz/circuitikz/bipole/kind}/voltage/bump b}
		\pgfkeysifdefined{\pgf@temp}
			{ \edef\bumpb{\ctikzvalof{bipoles/\pgfkeysvalueof{/tikz/circuitikz/bipole/kind}/voltage/bump b}} }
			{ \edef\bumpb{\ctikzvalof{voltage/bump b}} }
	}

	coordinate (pgfcirc@mid) at ($(\tikztostart) ! \distacefromnode ! (\ctikzvalof{bipole/name}.left)$)
	coordinate (pgfcirc@Vfrom) at ($(pgfcirc@mid) ! -\ctikzvalof{voltage/distance from line}\pgf@circ@Rlen ! \pgf@circ@voltage@angle:(\ctikzvalof{bipole/name}.left)$) 
   
	coordinate (pgfcirc@mid) at ($(\tikztotarget) ! \distacefromnode ! (\ctikzvalof{bipole/name}.right)$)
	coordinate (pgfcirc@Vto) at ($(pgfcirc@mid) ! \ctikzvalof{voltage/distance from line}\pgf@circ@Rlen ! \pgf@circ@voltage@angle : (\ctikzvalof{bipole/name}.right)$)

	\ifpgf@circuit@bipole@voltage@below
		coordinate (pgfcirc@Vcont1) at ($(\ctikzvalof{bipole/name}.center) ! \bumpb ! (\ctikzvalof{bipole/name}.-110)$)
		coordinate (pgfcirc@Vcont2) at ($(\ctikzvalof{bipole/name}.center) ! \bumpb ! (\ctikzvalof{bipole/name}.-70)$)
	\else
		coordinate (pgfcirc@Vcont1) at ($(\ctikzvalof{bipole/name}.center) ! \bumpb ! (\ctikzvalof{bipole/name}.110)$)
		coordinate (pgfcirc@Vcont2) at ($(\ctikzvalof{bipole/name}.center) ! \bumpb ! (\ctikzvalof{bipole/name}.70)$)
	\fi

	\ifpgf@circuit@europeanvoltage
		\ifpgf@circuit@bipole@voltage@backward
			(pgfcirc@Vto) .. controls (pgfcirc@Vcont2)  and (pgfcirc@Vcont1) .. 
				node[currarrow, sloped,  allow upside down, pos=1] {} 
			(pgfcirc@Vfrom) 
		\else
			(pgfcirc@Vfrom) .. controls (pgfcirc@Vcont1)  and (pgfcirc@Vcont2) ..
				node[currarrow, sloped,  allow upside down, pos=1] {}
			(pgfcirc@Vto) 	
		\fi	
	\else
		\ifpgf@circuit@bipole@voltage@backward
			(pgfcirc@Vfrom) node[inner sep=0, anchor=\pgf@circ@bipole@voltage@label@anchor]{\scriptsize$+$}   
			(pgfcirc@Vto) node[inner sep=0, anchor=\pgf@circ@bipole@voltage@label@anchor]{$-$}
		\else
			(pgfcirc@Vfrom) node[inner sep=0, anchor=\pgf@circ@bipole@voltage@label@anchor]{\scriptsize$-$}   
			(pgfcirc@Vto) node[inner sep=0, anchor=\pgf@circ@bipole@voltage@label@anchor]{$+$}
		\fi	
	\fi
}

%% Output routine for voltage sources

\def\pgf@circ@drawvoltagegenerator{
	\ifpgf@circuit@bipole@voltage@below
		coordinate (pgfcirc@Vcont1) at ($(\ctikzvalof{bipole/name}.center) ! \ctikzvalof{voltage/bump a} ! (\ctikzvalof{bipole/name}.-120)$)
		coordinate (pgfcirc@Vcont2) at ($(\ctikzvalof{bipole/name}.center) ! \ctikzvalof{voltage/bump a} ! (\ctikzvalof{bipole/name}.-60)$)
	\else
		coordinate (pgfcirc@Vcont1) at ($ (\ctikzvalof{bipole/name}.center) ! \ctikzvalof{voltage/bump a} ! (\ctikzvalof{bipole/name}.120)$)
		coordinate (pgfcirc@Vcont2) at ($ (\ctikzvalof{bipole/name}.center) ! \ctikzvalof{voltage/bump a} ! (\ctikzvalof{bipole/name}.60)$)
	\fi

	\ifpgf@circuit@europeanvoltage
		\ifpgf@circuit@bipole@voltage@backward
			(pgfcirc@Vcont2)  -- node[currarrow, sloped,  allow upside down, pos=1] {} (pgfcirc@Vcont1)
		\else
			(pgfcirc@Vcont1)  -- node[currarrow, sloped,  allow upside down, pos=1] {} (pgfcirc@Vcont2)
		\fi
	
	\else % american voltage
		
		\pgfextra{
			\def\pgf@temp{\pgfkeysvalueof{/tikz/circuitikz/bipole/kind}}
			\def\pgf@circ@temp{battery}
			\ifx\pgf@temp\pgf@circ@temp
				\edef\pgf@circ@batteria{battery}
			\else
				\def\pgf@circ@temp{battery1}
				\ifx\pgf@temp\pgf@circ@temp
					\edef\pgf@circ@batteria{battery}
				\else
					\edef\pgf@circ@batteria{false}
				\fi
			\fi
			\edef\pgf@circ@temp{battery}
		}
		
		\ifx\pgf@circ@batteria\pgf@circ@temp % if it is a battery, must put + and -
			\ifpgf@circuit@bipole@voltage@backward
				(pgfcirc@Vcont2)  node {$-$}  (pgfcirc@Vcont1) node {$+$}
			\else
				(pgfcirc@Vcont1)  node {$-$}  (pgfcirc@Vcont2) node {$+$}
			\fi
		\fi
		
	\fi
}

%% Output routine

\def\pgf@circ@drawvoltage{   % node name 
	\pgfextra{
		\edef\pgfcircmathresult{\expandafter\pgf@circ@stripdecimals\pgf@circ@direction\pgf@nil}

		\ifnum\pgfcircmathresult >4 \ifnum\pgfcircmathresult <86
			\ifpgf@circuit@bipole@voltage@below
				\def\pgf@circ@bipole@voltage@label@anchor{north west} 
			\else \def\pgf@circ@bipole@voltage@label@anchor{south east}
			\fi
		\fi\fi
		\ifnum\pgfcircmathresult >85 \ifnum\pgfcircmathresult <95
			\ifpgf@circuit@bipole@voltage@below
				\def\pgf@circ@bipole@voltage@label@anchor{west} 
			\else \def\pgf@circ@bipole@voltage@label@anchor{east} 
			\fi
		\fi\fi
		\ifnum\pgfcircmathresult >94 \ifnum\pgfcircmathresult <176
			\ifpgf@circuit@bipole@voltage@below
				 \def\pgf@circ@bipole@voltage@label@anchor{south west}
			\else \def\pgf@circ@bipole@voltage@label@anchor{north east}
			\fi
		\fi\fi
		\ifnum\pgfcircmathresult >175 \ifnum\pgfcircmathresult <185
			\ifpgf@circuit@bipole@voltage@below
				  \def\pgf@circ@bipole@voltage@label@anchor{south}
			\else\def\pgf@circ@bipole@voltage@label@anchor{north}
			\fi
		\fi\fi
		\ifnum\pgfcircmathresult >184 \ifnum\pgfcircmathresult <266
			\ifpgf@circuit@bipole@voltage@below
				 \def\pgf@circ@bipole@voltage@label@anchor{south east}
			\else\def\pgf@circ@bipole@voltage@label@anchor{north west}
			\fi
		\fi\fi
		\ifnum\pgfcircmathresult >265 \ifnum\pgfcircmathresult <275
			\ifpgf@circuit@bipole@voltage@below
				 \def\pgf@circ@bipole@voltage@label@anchor{east}
			\else \def\pgf@circ@bipole@voltage@label@anchor{west}
			\fi
		\fi\fi
		\ifnum\pgfcircmathresult >284 \ifnum\pgfcircmathresult <356
			\ifpgf@circuit@bipole@voltage@below
				  \def\pgf@circ@bipole@voltage@label@anchor{north east}
			\else\def\pgf@circ@bipole@voltage@label@anchor{south west}
			\fi
		\fi\fi
		\ifnum\pgfcircmathresult >-1 \ifnum\pgfcircmathresult <5
			\ifpgf@circuit@bipole@voltage@below
				 \def\pgf@circ@bipole@voltage@label@anchor{north}
			\else\def\pgf@circ@bipole@voltage@label@anchor{south} 
			\fi
		\fi\fi
		\ifnum\pgfcircmathresult >355 \ifnum\pgfcircmathresult <361
			\ifpgf@circuit@bipole@voltage@below
				  \def\pgf@circ@bipole@voltage@label@anchor{north}
			\else\def\pgf@circ@bipole@voltage@label@anchor{south}
			\fi
		\fi\fi

		\ifpgf@circuit@bipole@voltage@below
			\def\pgf@circ@bipole@voltage@label@where{-90}
		\else
			\def\pgf@circ@bipole@voltage@label@where{90}
		\fi
		
		\edef\pgf@temp{/tikz/circuitikz/bipoles/\pgfkeysvalueof{/tikz/circuitikz/bipole/kind}/voltage/european label distance}
		\pgfkeysifdefined{\pgf@temp}
			{ \edef\eudist{\ctikzvalof{bipoles/\pgfkeysvalueof{/tikz/circuitikz/bipole/kind}/voltage/european label distance}} }
			{ \edef\eudist{\ctikzvalof{voltage/european label distance}} }
	}

		\ifpgf@circuit@bipole@isvoltage
			\pgf@circ@drawvoltagegenerator 
		\else
			\pgf@circ@drawvoltagegeneric 
		\fi
		(\ctikzvalof{bipole/name}.\pgf@circ@bipole@voltage@label@where)
		coordinate (Vlab) at ($(\ctikzvalof{bipole/name}.center) !
			\ifpgf@circuit@europeanvoltage\eudist\else\ctikzvalof{voltage/american label distance}\fi !
			(\ctikzvalof{bipole/name}.\pgf@circ@bipole@voltage@label@where)$)
		node [anchor=\pgf@circ@bipole@voltage@label@anchor, inner sep=0] 
			  at (Vlab) { \pgf@circ@finallabel{voltage/} }
}




\endinput

%		coordinate (pgfcirc@Vcont1) at ($ (\ctikzvalof{bipole/name}.center) ! 
%			\ctikzvalof{voltage/bump b} !
%			(\ctikzvalof{bipole/name}.-110) $)
%		coordinate (pgfcirc@Vcont2) at ($ (\ctikzvalof{bipole/name}.center) ! 
%			\ctikzvalof{voltage/bump b} ! 
%			(\ctikzvalof{bipole/name}.-70) $)
%	\else
%		coordinate (pgfcirc@Vcont1) at ($ (\ctikzvalof{bipole/name}.center) ! 
%			\ctikzvalof{voltage/bump b} !
%			(\ctikzvalof{bipole/name}.110) $)
%		coordinate (pgfcirc@Vcont2) at ($ (\ctikzvalof{bipole/name}.center) ! 
%			\ctikzvalof{voltage/bump b} ! 
%			(\ctikzvalof{bipole/name}.70) $)
%	\fi
