% Logics needed
% ---
\usepackage{ifthen}

% Simple for loop construct as proposed by N. Setzer
\newcommand{\forloop}[5][1]
{
  \setcounter{#2}{#3}
  \ifthenelse{#4}
  {
    #5
    \addtocounter{#2}{#1}
    \forloop[#1]{#2}{\value{#2}}{#4}{#5}
  }
  {}
 }
% ---

% SansSerif font
\renewcommand{\familydefault}{\sfdefault}

% Abmessungen
\newlength{\rowheight}
\newlength{\colwidth}
\newcounter{rowcount}
\newcounter{colcount}
\newlength{\textframe}
\newlength{\totalheight}
\newlength{\totalwidth}
\newcounter{timemarks}
\newcounter{daymarks}

% Predefines
\setlength{\rowheight}{1.2cm}
\setlength{\colwidth}{2.8cm}
\setlength{\textframe}{0.8mm}
\setcounter{rowcount}9
\setcounter{colcount}5

% Eventdeklaration
\newlength{\lastCol}
\newlength{\lastRow}
\newlength{\newcolwidth}
\newlength{\newrowheight}
\newlength{\newcolleft}
\newlength{\newrowtop}

\newcommand{\event}[7]
  {
    % Arguments: {x}{y}{duration}{title}{lecturer}{place}{type}{textcolor}
    % This command resembles the \slot command of v1.1, i.e. this is now the one
    % and only way to define events.

    \renewcommand{\baselinestretch}{0.75}\normalsize
    \setlength{\lastRow}{-#2\rowheight}
    \addtolength{\lastRow}{-#3\rowheight}
    \setlength{\lastCol}{#1\colwidth}
    \addtolength{\lastCol}{\colwidth}
    \psframe[fillcolor=#7block,fillstyle=solid,linestyle=none](#1\colwidth,-#2\rowheight)(\lastCol,\lastRow)

    \setlength{\newrowheight}{\rowheight}
    \addtolength{\newrowheight}{-2\textframe}
    \setlength{\newcolwidth}{\colwidth}
    \addtolength{\newcolwidth}{-2\textframe}

    \setlength{\newrowtop}{-#2\rowheight}
    \addtolength{\newrowtop}{-\textframe}
    \setlength{\newcolleft}{#1\colwidth}
    \addtolength{\newcolleft}{\textframe}

    \rput[tl](\newcolleft,\newrowtop){\begin{minipage}[t]{\newcolwidth}\raggedright \color{#7text}{#4}\end{minipage}}

    \renewcommand{\baselinestretch}{0.5}\normalsize
    \setlength{\newrowtop}{\lastRow}
    \addtolength{\newrowtop}{2\textframe}
    \addtolength{\newrowtop}{8pt}

    \rput[lt](\newcolleft,\newrowtop){\begin{minipage}[t]{0.6\newcolwidth}\raggedright \color{#7text}{\scriptsize #5}\end{minipage}}

    \setlength{\newcolleft}{\lastCol}
    \addtolength{\newcolleft}{-\textframe}

    \rput[rt](\newcolleft,\newrowtop){\begin{minipage}[t]{0.4\newcolwidth}\raggedleft \color{#7text}{\scriptsize #6}\end{minipage}}
    \renewcommand{\baselinestretch}{1.0}\normalsize
  }

\newcommand{\legend}[3]
  {
    % Arguments: <horizontal> <vertical> <text>
    % Lets you manually set slot contents (use this really rarely and
    % SEE: \daymark, \timemark )

    \setlength{\lastCol}{#1\colwidth}
    \setlength{\lastRow}{-#2\rowheight}
    \setlength{\newrowtop}{\lastRow}
    \setlength{\newcolleft}{\lastCol}
    \addtolength{\newrowtop}{\rowheight}
    \addtolength{\newcolleft}{-\colwidth}
    \psline[linecolor=gray](\newcolleft,\newrowtop)(\newcolleft,\lastRow)
    \psline[linecolor=gray](\newcolleft,\newrowtop)(\lastCol,\newrowtop)
    \addtolength{\newcolleft}{\textframe}
    \addtolength{\newrowtop}{-\textframe}
    \rput[tl](\newcolleft,\newrowtop){#3}
  }

\newcommand{\timemark}[1]
  {
    \stepcounter{timemarks}
    \legend 1 {\value{timemarks}} {#1}
  }

\newcommand{\daymark}[1]
  {
    \stepcounter{daymarks}
    \legend {\value{daymarks}} 1 {#1}
  }

\newcommand{\defineevent}[7]
  {
    \definecolor{#1block}{rgb}{#2, #3, #4}
    \definecolor{#1text} {rgb}{#5, #6, #7}
    \newrgbcolor{#1block}{#2 #3 #4}
    \newrgbcolor{#1text} {#5 #6 #7}
  }

\newcommand{\printheading}[1]
  {
    {\LARGE #1}\hfill\\\hfill\\
  }

\newcommand{\setslotsize}[2]
  {
    % Arguments: <width> <height>
    % Defines the slot size

    \setlength{\rowheight}{#2}
    \setlength{\colwidth}{#1}
  }

\newcommand{\setslotcount}[2]
  {
    % Arguments: <days> <slots per day>
    % Defines the number of slots in both dimensions

    \setcounter{colcount}{#1}
    \setcounter{rowcount}{#2}

    \stepcounter{colcount}
    \stepcounter{rowcount}
  }

\newcommand{\settextframe}[1]
   {
     % Arguments: <width>
     % Defines the width of the border of each entry (i.e. the
     % distance between outer border and text)

     \setlength{\textframe}{#1}
   }

\newcommand{\hours}[1]
  {
    % Arguments: <start hour>
    % Automatically sets the time slots to full hours, beginning with <start hour>

    \newcounter{hour}
    \setcounter{hour}{#1}

    \newcounter{row}
    \forloop{row}{1}{\value{row} < \value{rowcount}}
    {
      \timemark{\ifthenelse{\value{hour} < 10}0{}\arabic{hour}:00 - \stepcounter{hour}\ifthenelse{\value{hour} < 10}0{}\arabic{hour}:00}

      \ifthenelse{\value{hour} > 23}{\addtocounter{hour}{-24}}{}
    }
  }

\newcommand{\englishdays}[1]
  {
    % Arguments: <number of start day>
    % Macro for the english week days

    \newcounter{day}
    \setcounter{day}{#1}

    \newcounter{column}
    \forloop{column}{1}{\value{column} < \value{colcount}}
    {
      \ifthenelse{\equal{\value{day}}1}{\daymark{Monday}}{}
      \ifthenelse{\equal{\value{day}}2}{\daymark{Tuesday}}{}
      \ifthenelse{\equal{\value{day}}3}{\daymark{Wednesday}}{}
      \ifthenelse{\equal{\value{day}}4}{\daymark{Thursday}}{}
      \ifthenelse{\equal{\value{day}}5}{\daymark{Friday}}{}
      \ifthenelse{\equal{\value{day}}6}{\daymark{Saturday}}{}
      \ifthenelse{\equal{\value{day}}7}{\daymark{Sunday}}{}

      \stepcounter{day}
      \ifthenelse{\value{day} > 7}{\addtocounter{day}{-7}}{}
    }
  }

\newcommand{\germandays}[1]
  {
    % Arguments: <number of start day>
    % Macro for the english week days

    \newcounter{day}
    \setcounter{day}{#1}

    \newcounter{column}
    \forloop{column}{1}{\value{column} < \value{colcount}}
    {
      \ifthenelse{\equal{\value{day}}1}{\daymark{Montag}}{}
      \ifthenelse{\equal{\value{day}}2}{\daymark{Dienstag}}{}
      \ifthenelse{\equal{\value{day}}3}{\daymark{Mittwoch}}{}
      \ifthenelse{\equal{\value{day}}4}{\daymark{Donnerstag}}{}
      \ifthenelse{\equal{\value{day}}5}{\daymark{Freitag}}{}
      \ifthenelse{\equal{\value{day}}6}{\daymark{Samstag}}{}
      \ifthenelse{\equal{\value{day}}7}{\daymark{Sonntag}}{}

      \stepcounter{day}
      \ifthenelse{\value{day} > 7}{\addtocounter{day}{-7}}{}
    }
  }

\newenvironment{timetable}
  {
    % Arguments: None
    % Starts a new timetable

    % Set the new layout setting
    \setlength{\totalheight}{-\value{colcount}\rowheight}
    \addtolength{\totalheight}{-\rowheight}
    \setlength{\totalwidth}{-\value{rowcount}\colwidth}
    \addtolength{\totalwidth}{-\colwidth}

    % Reset the counters
    \setcounter{timemarks}{1}
    \setcounter{daymarks}{1}

    \begin{pspicture}(0,0)(-\totalwidth,\totalheight)

      % Draw the grey frame
      \psframe[linestyle=none,fillstyle=solid,fillcolor=lightgray](0,-\value{rowcount}\rowheight)(\colwidth,0)
      \psframe[linestyle=none,fillstyle=solid,fillcolor=lightgray](0,-\rowheight)(\value{colcount}\colwidth,0)
  }
  {
      % Draw the contours
      \psline(0,0)(\value{colcount}\colwidth,0)
      \psline(\value{colcount}\colwidth,0)(\value{colcount}\colwidth,-\value{rowcount}\rowheight)
      \psline(\value{colcount}\colwidth,-\value{rowcount}\rowheight)(0,-\value{rowcount}\rowheight)
      \psline(0,-\value{rowcount}\rowheight)(0,0)
      \psline(\colwidth,-\value{rowcount}\rowheight)(\colwidth,-\rowheight)
      \psline(\colwidth,-\rowheight)(\value{colcount}\colwidth,-\rowheight)

    \end{pspicture}
  }
