#include "functions.h"
#include "exec/exec.h"
#include "intuition/intuition.h"
#include "graphics/gfx.h"
#include "graphics/gfxmacros.h"
#include "graphics/regions.h"

#define ERROR 0
#define SUCCESS 1

  struct Window *mywindow;
  struct Screen *myscreen;

  short fehler;

  struct IntuitionBase *IntuitionBase;
  struct GfxBase *GfxBase;
  struct LayersBase *LayersBase;

struct NewWindow mynewwindow =
  { 10,10,600,200,0,1,MOUSEBUTTONS,WINDOWSIZING|WINDOWDRAG|WINDOWCLOSE|
    WINDOWDEPTH,NULL,NULL,"Hallo",NULL,NULL,0,0,640,255,CUSTOMSCREEN
  };

struct NewScreen mynewscreen =
  { 0,0,640,255,4,0,1,HIRES,CUSTOMSCREEN,NULL,NULL,NULL,NULL
  };



char *monat[] = {
  "Jan","Feb","Mrz","Apr","Mai","Jun","Jul",
  "Aug","Sep","Okt","Nov","Dez" };

long umsatz[12]= {
  12345,45876,68294,79137,45498,89679,32885,49271,67466,
  44228,84733,38117 };

char  *uz[] = {
  "10000","20000","30000","40000","50000" };
  
struct Rectangle rect1,rect2;


clip(window,x,y,w,h)
struct Window *window;
short x,y,w,h;
{ register struct Region *clipregion;
  register struct RegionRectangle *regrect;
  register struct Layer *layer;

  layer = window->WLayer;
  clipregion = layer->ClipRegion;
  if (!clipregion)
  { clipregion = (struct Region *)NewRegion();
    if (!clipregion)
      return(ERROR);
  }

  LockLayer(layer->LayerInfo,layer);

  clipregion->bounds.MinX = 0;
  clipregion->bounds.MinY = 0;
  clipregion->bounds.MaxX = window->Width;
  clipregion->bounds.MaxY = window->Height;

  regrect = clipregion->RegionRectangle;
  if (!regrect)
  { regrect = (struct RegionRectangle *)AllocMem(sizeof(struct RegionRectangle),MEMF_CLEAR);
    if (!regrect)
      { UnlockLayer(layer);
	return (ERROR);
      }
  }

  regrect->bounds.MinX=x;
  regrect->bounds.MinY=y;
  regrect->bounds.MaxX=w;
  regrect->bounds.MaxY=h;
  
  clipregion->RegionRectangle = regrect;
  InstallClipRegion(layer,clipregion);
 
  UnlockLayer(layer);
  return(SUCCESS);
  }

clipw(win)
struct Window *win;
{ register short x,y,w,h;

  x = 0;
  y = 0;
  w = win->Width;
  h = win->Height;

  if (!(win->Flags & GIMMEZEROZERO))
    {
    x = win->BorderLeft;
    y = win->BorderTop;
    w -= win->BorderRight;
    h -= win->BorderBottom;
    }
  fehler = clip(win,x,y,w,h);
  }

clipoff(window)
struct Window *window;
{ register struct Layer *layer;
  struct Region *clipregion;

  layer = window->WLayer;
  if  (clipregion = layer->ClipRegion) 
  { LockLayer(layer->LayerInfo,layer);
    InstallClipRegion(layer,NULL);
    DisposeRegion(clipregion);
    UnlockLayer(layer);
  }
}


main()
{ register struct RastPort *rp;
  register struct IntuiMessage *message;

  if( ! (IntuitionBase = OpenLibrary("intuition.library",0)))
    exit( FALSE);

  if( ! (GfxBase = OpenLibrary( "graphics.library",0)))
    exit( FALSE);

  if( ! (LayersBase = OpenLibrary( "layers.library",0)))
    exit( FALSE);

  if (myscreen = OpenScreen(&mynewscreen))
  { mynewwindow.Screen = myscreen;
    if (mywindow = OpenWindow(&mynewwindow))
    { rp = mywindow->RPort;
      drawpicture();
      WaitPort(mywindow->UserPort);
      while (message=GetMsg(mywindow->UserPort))   
        ReplyMsg(message);
      Move(rp,0,0);
      ClearScreen(rp);
      RefreshWindowFrame(mywindow);
      clipw(mywindow);
      drawpicture();
      WaitPort(mywindow->UserPort);
      while (message=GetMsg(mywindow->UserPort))   
        ReplyMsg(message);
      clipoff(mywindow);
      CloseWindow (mywindow); 
    }
    CloseScreen(myscreen);
  }
}

drawpicture()
{ register short i,j,xpos,ypos;
  register struct RastPort *rp;

  rp = mywindow->RPort;

  SetAPen(rp,3);

  for(i=0;i<5;i++)
  { j=150-30*i;
    Move  (rp,20,j);
    Text  (rp,uz[i],5);
    Move  (rp,65,j-3);
    Draw  (rp,75,j-3);
 }

  xpos = 80;
  ypos = 170;

  Move (rp,70,-10);
  Draw (rp,70,180);
  Move (rp,70,180);
  Draw (rp,600,180);

  for (i=0;i<12;i++)
  { SetAPen(rp,3);
    Move (rp,xpos,192); 
    Text (rp,monat[i],3);
    SetAPen(rp,i);
    RectFill(rp,xpos,ypos-umsatz[i]/300,xpos+34,ypos);
    xpos += 35;
  }
}

