% ProgressBar
%
% kindly supported by mrunix-forum: http://www.mrunix.de/
%
%% progressbar.sty
%% Copyright 2010-2013 Marcel Jira
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3c
% of this license or (at your option) any later version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3c or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
%
% This work has the LPPL maintenance status `maintained'.
% 
% The Current Maintainer of this work is Marcel Jira.
%
% This work consists of the files progressbar.sty and progressbar.tex.
%
\NeedsTeXFormat{LaTeX2e}%
%
\ProvidesPackage{progressbar}[2014/05/04 v1.0b~4: progressbar]%
%
\RequirePackage{calc}%
\RequirePackage{tikz}%
\RequirePackage{kvoptions}[2011/06/30]%
\RequirePackage{kvsetkeys}%
%
% Define lengths for later use
\newlength{\BarWidth}%
\newlength{\BarWidthCenter}%
\newlength{\BarHeight}%
\newlength{\BarHeightCenter}%
\newlength{\BarBorderWidth}%
\newlength{\BarRoundedCorners}%
% This length will be used for the realisation of the progressbars
\newlength{\BarPartWidth}%
% And the ticks
\newlength{\BarSubDivisionsWidthANU}%
\newlength{\BarSubDivisionsWidthBNU}%
\newlength{\BarSubDivisionsWidthCNU}%
%
% All user-variables
\def\progressbar@keylist{borderwidth,heighta,roundnessa,heightr,roundnessr,width,linecolor,emptycolor,filledcolor,tickswidth,ticksheight,tickscolor,subdivisions}%
% Keys corresponding to the user-variables
\def\progressbar@defaultvaluelist{0.8pt,0pt,0pt,1,0.15,6em,black,black!10,black!60,0.4pt,0.33,black,10}%
\newcounter{progressbar@counter}%
%
% KeyVal-Options
% Define default-Keyval-Options for this package
\setcounter{progressbar@counter}{0}%
\@for\progressbar@value:=\progressbar@defaultvaluelist\do{%
	\stepcounter{progressbar@counter}%
	\expandafter\edef\csname progressbar@value\the\value{progressbar@counter}\endcsname{\progressbar@value}%
}%
\setcounter{progressbar@counter}{0}%
\@for\progressbar@key:=\progressbar@keylist\do{%
	\stepcounter{progressbar@counter}%
	\expandafter\DeclareStringOption\expandafter[\csname progressbar@value\the\value{progressbar@counter}\endcsname]{\progressbar@key}%
}%
\ProcessKeyvalOptions*%
% Change Keyval family and prefix
\SetupKeyvalOptions{family=progressbarlocal,prefix=progressbarlocal@}%
% Define Keyval-Options that are used in the macro
\@for\progressbar@key:=\progressbar@keylist\do{%
	\DeclareStringOption{\progressbar@key}%
}%
\def\progressbar@lastheighta{false}%
\def\progressbar@lastroundnessa{false}%
\def\progressbarlocal@lastroundnessa{false}%
\def\progressbar@true{true}%
\AddToKeyvalOption{progressbar}{heightr}{%
	\def\progressbar@lastheighta{false}%
}%
\AddToKeyvalOption{progressbar}{heighta}{%
	\def\progressbar@lastheighta{true}%
}%
\AddToKeyvalOption{progressbar}{roundnessr}{%
	\def\progressbar@lastroundnessa{false}%
}%
\AddToKeyvalOption{progressbar}{roundnessa}{%
	\def\progressbar@lastroundnessa{true}%
}%
\newcommand{\setBarHeightCenter}{\setlength{\BarHeightCenter}{\BarHeight - \BarBorderWidth}}%
\newcommand{\setBarWidthCenter}{\setlength{\BarWidthCenter}{\BarWidth - \BarBorderWidth}}%
\AddToKeyvalOption{progressbarlocal}{heightr}{%
	\setlength{\BarHeight}{\heightof{I} * \real{\progressbarlocal@heightr}}%
	\kvsetkeys{progressbarlocal}{heighta=\BarHeight}%
}%
\AddToKeyvalOption{progressbarlocal}{roundnessr}{%
	\def\progressbarlocal@lastroundnessa{false}%
}%
\AddToKeyvalOption{progressbarlocal}{heighta}{%
	\setlength{\BarHeight}{\progressbarlocal@heighta}%
	\setBarHeightCenter%
}%
\AddToKeyvalOption{progressbarlocal}{width}{%
	\setlength{\BarWidth}{\progressbarlocal@width}%
	\setBarWidthCenter%
}%
\AddToKeyvalOption{progressbarlocal}{roundnessa}{%
	\def\progressbarlocal@lastroundnessa{true}%
	\setlength{\BarRoundedCorners}{\progressbarlocal@roundnessa}%
}%
\AddToKeyvalOption{progressbarlocal}{borderwidth}{%
	\setlength{\BarBorderWidth}{\progressbarlocal@borderwidth}%
	\setBarHeightCenter%
	\setBarWidthCenter%
}%
% Macor to change Options
\newcommand{\progressbarchange}[1]{%
	\kvsetkeys{progressbar}{#1}%
}%
%
% The actual realisation of the progressbar
\newcommand{\progressbar}[2][]{%
	% Get the global keys in the local keys
	\@for\progressbar@key:=\progressbar@keylist\do{%
		\@expandtwoargs\kvsetkeys{progressbarlocal}{\progressbar@key=\csname progressbar@\progressbar@key\endcsname}%
	}%
	\ifx\progressbar@lastheighta\progressbar@true%
		\kvsetkeys{progressbarlocal}{heighta=\progressbar@heighta}%
	\else%
		\kvsetkeys{progressbarlocal}{heightr=\progressbar@heightr}%
	\fi%
	\ifx\progressbar@lastroundnessa\progressbar@true%
		\kvsetkeys{progressbarlocal}{roundnessa=\progressbar@roundnessa}%
	\else%
		\kvsetkeys{progressbarlocal}{roundnessr=\progressbar@roundnessr}%
	\fi%
	% Now get the optional user input keys
	\kvsetkeys{progressbarlocal}{#1}%
	% Make a correct roundndess, if roundnessr was defined
	\ifx\progressbarlocal@lastroundnessa\progressbar@true%
	\else%
		\setlength{\BarRoundedCorners}{\BarHeightCenter * \real{\progressbarlocal@roundnessr}}%
	\fi%
	% BarSubDivision
	\setlength{\BarSubDivisionsWidthANU}{\BarWidthCenter / \progressbarlocal@subdivisions}%
	\setlength{\BarSubDivisionsWidthBNU}{\BarSubDivisionsWidthANU * 2}%
	\setlength{\BarSubDivisionsWidthCNU}{\BarWidthCenter - \BarSubDivisionsWidthANU}%
	% The filled part of the bar is calculated and set
	\setlength{\BarPartWidth}{\BarWidthCenter * \real{#2}}%
	% Now let's draw a bar ;)
	\begin{tikzpicture}%
		\coordinate (Begin) at (0,0);%
		\coordinate (End) at (\BarWidthCenter,\BarHeightCenter);%
		\coordinate (Part) at (\BarPartWidth,\BarHeightCenter);%
		% Rounded Corners
		\draw[rounded corners=\BarRoundedCorners,line width=\BarBorderWidth,color=\progressbarlocal@linecolor] (Begin) rectangle (End);%
		\clip[rounded corners=\BarRoundedCorners] (Begin) rectangle (End);%
		% Fill the bar
		\fill[fill=\progressbarlocal@emptycolor] (Begin) rectangle (End);%
		% the full part
		\fill[fill=\progressbarlocal@filledcolor] (Begin) rectangle (Part);%
		% Ticks
		\foreach \x in {\strip@pt\BarSubDivisionsWidthANU,\strip@pt\BarSubDivisionsWidthBNU,...,\strip@pt\BarSubDivisionsWidthCNU}%
		\draw[draw=\progressbarlocal@tickscolor,line width=\progressbarlocal@tickswidth] (\x pt,0) -- (\x pt,\BarHeightCenter*\progressbarlocal@ticksheight);%
		% Rounded Corners again
		\draw[rounded corners=\BarRoundedCorners,line width=\BarBorderWidth,color=\progressbarlocal@linecolor] (Begin) rectangle (End);%
	\end{tikzpicture}%
}%
