#define MAXPOINTS   50
#define TMPRASWIDTH 640
#define TMPRASHEIGHT 200

#define NTSC_HEIGHT 200
#define PAL_HEIGHT 256

#define NTSC_MAX_HEIGHT 200
#define PAL_MAX_HEIGHT 256

#include <exec/types.h>
#include <exec/nodes.h>
#include <exec/lists.h>
#include <graphics/gfx.h>
#include <graphics/gfxbase.h>
#include <graphics/clip.h>
#include <graphics/rastport.h>
#include <graphics/view.h>
#include <graphics/text.h>
#include <graphics/gfxmacros.h>

#include <graphics/layers.h>
#include <intuition/intuition.h>

/*#define DEBUG*/
/*#define KPRINTF*/
/*#define NEWSCREEN*/

#ifdef KPRINTF
#define printf kprintf
#endif

#define TXHEIGHT 8

#define GADGETS
#define RBG_ID 0x0001
#define BBG_ID 0x0002

struct Image bb_knob_image;

struct PropInfo bb_knob =
{
    FREEHORIZ | AUTOKNOB,
    0,
    0,
    MAXBODY/8,
    MAXBODY
};

struct Gadget bb_gadget =
{
    NULL,
    0,-8,-16,9,
    GRELWIDTH|GRELBOTTOM,
    GADGIMMEDIATE|RELVERIFY|BOTTOMBORDER,
    GZZGADGET|PROPGADGET,
    &bb_knob_image,
    NULL,
    NULL,
    0,
    &bb_knob,
    BBG_ID,
    NULL
};

struct Image rb_knob_image;

struct PropInfo rb_knob =
{
    FREEVERT | AUTOKNOB,
    0,
    0,
    MAXBODY,
    MAXBODY/8
};

struct Gadget rb_gadget =
{
    &bb_gadget,
    -15,10,16,-18,
    GRELHEIGHT|GRELRIGHT,
    GADGIMMEDIATE|RELVERIFY|RIGHTBORDER,
    GZZGADGET|PROPGADGET,
    &rb_knob_image,
    NULL,
    NULL,
    0,
    &rb_knob,
    RBG_ID,
    NULL
};

struct Gadget *vgadget_ptr = &rb_gadget;
struct Gadget *hgadget_ptr = &bb_gadget;

struct TextAttr TestFont =
    {
    "topaz.font",
    TXHEIGHT,
    0,
    0,
    };

long GfxBase = 0;
long LayersBase = 0;
long IntuitionBase = 0;

#define IS_PAL (((struct GfxBase *)GfxBase)->DisplayFlags & PAL)

struct	AreaInfo areainfo;
UWORD	areafill[(MAXPOINTS/2)*5];

startgfx(x,y,height, width, n_bit_Planess, palette, gfxproc,s,flags,sbitmap)
WORD x,y;
WORD height, width, n_bit_Planess;
UWORD *palette;
int (*gfxproc)();
UBYTE *s;
struct BitMap *sbitmap;	    /* optional parameter */
{
    struct Window *window;
    struct RastPort *w=0;
    register i, j;
    int idcmp;
    struct NewWindow nw;
    UBYTE   *ras;

    GfxBase = OpenLibrary("graphics.library",32);
    if (GfxBase == NULL)
    {
#ifdef DEBUG
	printf("no graphics library\n");
#endif
	exit();
    }

    LayersBase = OpenLibrary("layers.library",32);
    if (LayersBase == NULL)
    {
#ifdef DEBUG
	printf("no layers library\n");
#endif
	exit();
    }

    IntuitionBase = OpenLibrary("intuition.library",32);
    if (IntuitionBase == NULL)
    {
#ifdef DEBUG
	printf("no intuition library\n");
#endif
	exit();
    }

    /* intuition direct comunication message port */
    idcmp = CLOSEWINDOW|REFRESHWINDOW|GADGETUP|GADGETDOWN|SIZEVERIFY|NEWSIZE;
    nw.LeftEdge = x;
    nw.TopEdge = y;
    nw.Width = width;
    nw.Height = height;
    nw.DetailPen = -1;
    nw.BlockPen = -1;
    nw.IDCMPFlags = idcmp;
    nw.Flags = WINDOWDEPTH|WINDOWSIZING|WINDOWDRAG|WINDOWCLOSE|flags;
    nw.FirstGadget = 0;

#ifdef GADGETS
    nw.FirstGadget = &rb_gadget;
#endif

    nw.CheckMark = 0;
    nw.Title = s;
    nw.Screen = 0;
    nw.BitMap = sbitmap;
    nw.MinWidth = 80;
    nw.MinHeight = 16;
    nw.MaxWidth = 640;
    nw.MaxHeight = ~0; /* V32 and above */
    nw.Type = WBENCHSCREEN;
    
#ifdef DEBUG
    printf("calling open window\n sbitmap=%lx\n",nw.BitMap);
#endif
    window = (struct Window *)OpenWindow(&nw);

    /* bart - 11.21.85 */
    if (!window) return;

#ifdef DEBUG
    printf("after call to open window\n");
#endif

    /* bart - 11.13.85 */
    SetWindowTitles(window,-1,s);

    w = window->RPort;
    w->Layer->Window = window;

    InitArea(&areainfo,areafill,MAXPOINTS);
    w->AreaInfo = &areainfo;

    (*gfxproc)(&window->WScreen->ViewPort,w);

    CloseWindow(window);

    /* restart workbench screen */

#ifdef NEWSCREEN
	CloseScreen(ActiveGroup.ActiveScreen);
	myscreen = OpenScreen(0,0,640,200,2,HIRES,WBENCHSCREEN,&TestFont);
#endif

}

Get_RWidth(rp)
struct RastPort *rp;
{
    int width;
    struct Layer *cw;
    if ( (cw = rp->Layer) == 0) width = rp->BitMap->BytesPerRow<<3;
    else
	if (cw->SuperBitMap == 0) width = cw->bounds.MaxX-cw->bounds.MinX + 1;
    else width = cw->SuperBitMap->BytesPerRow<<3;
    if (cw)
    {
	struct Window *window;
	window = cw->Window;
	if (window)
	{
	    if ((window->Flags & GIMMEZEROZERO) == 0)
		width -= window->BorderLeft+window->BorderRight;
	}
    }
    return (width);
}
 
Get_RHeight(rp)
struct RastPort *rp;
{
    int height;
    struct Layer *cw;
    if ( (cw = rp->Layer) == 0) height = rp->BitMap->Rows;
    else
	if (cw->SuperBitMap == 0) height = cw->bounds.MaxY-cw->bounds.MinY + 1;
    else height =  cw->SuperBitMap->Rows;
    if (cw)
    {
	struct Window *window;
	if (window = cw->Window)
	    if ((window->Flags & GIMMEZEROZERO) == 0)
		height -= window->BorderTop+window->BorderBottom;
    }
    return(height);
}

struct TmpRas *gettmpras(t,w,h)
struct TmpRas *t;
int w,h;
{
    char *ras;
    ras = (char *)AllocRaster(w,h);
    if (ras == 0)
    {
#ifdef DEBUG
	printf("no ram for tmpras\n");
#endif
	exit(1);
    }
    return((struct TmpRas *)InitTmpRas(t,ras,RASSIZE(w,h)));
}

Get_xoffset(rp)
struct RastPort *rp;
{
    struct Layer *l;
    struct Window *window;
    l = rp->Layer;
    if (l != 0)
    {
	window = l->Window;
	if (window->Flags & GIMMEZEROZERO)  return(0);
	else	return(window->BorderLeft);
    }
    else    return(0);
}

Get_yoffset(rp)
struct RastPort *rp;
{
    struct Layer *l;
    struct Window *window;
    l = rp->Layer;
    if (l != 0)
    {
	window = l->Window;
	if (window->Flags & GIMMEZEROZERO)  return(0);
	else	return(window->BorderTop);
    }
    else    return(0);
}


