% 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
				\ifpgf@circ@oldvoltagedirection
				\pgfkeys{\circuitikzbasekey/bipole/voltage/position=below,
					\circuitikzbasekey/bipole/voltage/direction=backward}
				\else
				\pgfkeys{\circuitikzbasekey/bipole/voltage/position=below,
					\circuitikzbasekey/bipole/voltage/direction=forward}
				\fi
			\fi
			\ifpgf@circ@oldvoltagedirection\else
			\ifpgf@circuit@bipole@iscurrent
			\ifpgf@circuit@bipole@current@backward
					\pgfkeys{\circuitikzbasekey/bipole/voltage/position=below,
					\circuitikzbasekey/bipole/voltage/direction=forward}
			\else
					\pgfkeys{\circuitikzbasekey/bipole/voltage/position=below,
					\circuitikzbasekey/bipole/voltage/direction=backward}
			\fi\fi\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{
	
	
	\edef\pgf@temp{/tikz/circuitikz/bipoles/\pgfkeysvalueof{/tikz/circuitikz/bipole/kind}/voltage/straight label distance}
			\pgfkeysifdefined{\pgf@temp}
				{ \edef\partwidth{\ctikzvalof{bipoles/\pgfkeysvalueof{/tikz/circuitikz/bipole/kind}/voltage/straight label distance}} 
					\def\tmpdistfromline{\partwidth\pgf@circ@Rlen}}
				{ 
		\edef\partwidth{0.5*\ctikzvalof{bipoles/\pgfkeysvalueof{/tikz/circuitikz/bipole/kind}/height}}
			\def\tmpdistfromline{(\partwidth\pgf@circ@Rlen+0.2\pgf@circ@Rlen)}
	}
		\ifnum \ctikzvalof{mirror value}=-1
			\ifpgf@circuit@bipole@inverted
				\ifpgf@circuit@bipole@voltage@straight
					\def\distfromline{\tmpdistfromline}
				\else
					\def\distfromline{\ctikzvalof{voltage/distance from line}\pgf@circ@Rlen}
				\fi
			\else
				\ifpgf@circuit@bipole@voltage@straight
					\def\distfromline{-\tmpdistfromline}
				\else
					\def\distfromline{-\ctikzvalof{voltage/distance from line}\pgf@circ@Rlen}
				\fi
			\fi
		\else
			\ifpgf@circuit@bipole@inverted
				\ifpgf@circuit@bipole@voltage@straight
					\def\distfromline{-\tmpdistfromline}
				\else
					\def\distfromline{-\ctikzvalof{voltage/distance from line}\pgf@circ@Rlen}
				\fi
			\else
				\ifpgf@circuit@bipole@voltage@straight
					\def\distfromline{\tmpdistfromline}
				\else
					\def\distfromline{\ctikzvalof{voltage/distance from line}\pgf@circ@Rlen}
				\fi
			\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}} }
	}
	
	% %\pgfkeysvalueof{/tikz/circuitikz/bipoles/length}/16 is equal to the length of the currarrow
	coordinate (pgfcirc@midtmp) at ($(\tikztostart) ! \pgfkeysvalueof{/tikz/circuitikz/bipoles/length}/16 ! (anchorstartnode)$) %absolute move, minimum space is length of arrowhead
	coordinate (pgfcirc@midtmp) at ($(pgfcirc@midtmp) ! \distacefromnode ! (anchorstartnode)$)

	
	coordinate (pgfcirc@Vfrom) at ($(pgfcirc@midtmp) ! -\distfromline ! \pgf@circ@voltage@angle:(anchorstartnode)$) 
	coordinate (pgfcirc@midtmp) at ($(\tikztotarget) ! \pgfkeysvalueof{/tikz/circuitikz/bipoles/length}/16 ! (anchorendnode)$)%absolute move, minimum space is length of arrowhead
	coordinate (pgfcirc@midtmp) at ($(pgfcirc@midtmp) ! \distacefromnode ! (anchorendnode)$)

	coordinate (pgfcirc@Vto) at ($(pgfcirc@midtmp) ! \distfromline ! \pgf@circ@voltage@angle : (anchorendnode)$)


	\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@straight
			\ifpgf@circuit@bipole@voltage@backward
				(pgfcirc@Vto) --(pgfcirc@Vfrom) node[currarrow, sloped,  allow upside down, pos=1,anchor=tip] {} 
			\else
				(pgfcirc@Vfrom) --(pgfcirc@Vto) node[currarrow, sloped,  allow upside down, pos=1,anchor=tip] {} 
				 	
			\fi	
		\else
			\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	
		\fi
	\else
		\ifpgf@circuit@bipole@voltage@backward
			\ifpgf@circ@oldvoltagedirection
				(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
		\else
			\ifpgf@circ@oldvoltagedirection
				(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	
	\fi
}

%% Output routine for voltage sources

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

	\ifpgf@circuit@europeanvoltage
		\ifpgf@circuit@bipole@voltage@backward
			(pgfcirc@Vto)  -- node[currarrow, sloped,  allow upside down, pos=1] {} (pgfcirc@Vfrom)
		\else
			(pgfcirc@Vfrom)  -- node[currarrow, sloped,  allow upside down, pos=1] {} (pgfcirc@Vto)
		\fi
	
	\else % american voltage
		
	\ifpgf@circuit@bipole@voltageoutsideofsymbol
		% if it is a battery, must put + and -
			\ifpgf@circuit@bipole@voltage@backward
				(pgfcirc@Vfrom)  node {$-$}  (pgfcirc@Vto) node {$+$}
			\else
				(pgfcirc@Vfrom)  node {$+$}  (pgfcirc@Vto) 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
		
		\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@inverted
			\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@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}} }
	
	\pgfsetcornersarced{\pgfpointorigin}% do not use rounded corners!
	} %end pgfextra

		\ifpgf@circuit@bipole@isvoltage
			\pgf@circ@drawvoltagegenerator 
		\else
			\pgf@circ@drawvoltagegeneric 
		\fi
		
	%	(\ctikzvalof{bipole/name}.\pgf@circ@bipole@voltage@label@where) %Zeile sinnlos!?
		\ifpgf@circuit@bipole@voltage@straight
			coordinate (Vlab) at ($(pgfcirc@Vto)!0.5!(pgfcirc@Vfrom) $)
			node [anchor=\pgf@circ@bipole@voltage@label@anchor, inner sep=2pt] 
					  at (Vlab) { \pgf@circ@finallabels{voltage/label} }
		\else
		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=2pt] 
					  at (Vlab) { \pgf@circ@finallabels{voltage/label} }
		
		\fi
		}%end drawvoltages

\endinput
