/*
 *  Source machine generated by GadToolsBox V2.0b
 *  which is (c) Copyright 1991-1993 Jaba Development
 *  (The generated source is changed completely. -KSM.)
 *
 *  GUI Designed by : Kjetil S. Matheussen


	Various graphic.library and intuition.library specific
	functions needed for the tracknameswindow.

	Made by Kjetil S. Matheussen 1998.

 */

#include <exec/types.h>
#include <intuition/intuition.h>
#include <intuition/classes.h>
#include <intuition/classusr.h>
#include <intuition/imageclass.h>
#include <intuition/gadgetclass.h>
#include <libraries/gadtools.h>
#include <graphics/displayinfo.h>
#include <graphics/gfxbase.h>
#include <clib/exec_protos.h>
#include <clib/intuition_protos.h>
#include <clib/gadtools_protos.h>
#include <clib/graphics_protos.h>
#include <clib/utility_protos.h>
#include <string.h>
#include <stdio.h>

#include "windowcontrol.h"

struct Screen         *Scr = NULL;
UBYTE                 *PubScreenName = "OCTAMED";
APTR                   VisualInfo = NULL;
struct Window         *tnmWnd = NULL;
UWORD                  tnmLeft;
UWORD                  tnmTop;
UWORD                  tnmWidth;
UWORD                  tnmHeight;
UBYTE                 *tnmWdt = (UBYTE *)"01:";

extern int ypos;
extern char *editorfontname;
extern int editorfontsize;
extern int windowheight;
extern int topmarkcursorpos,botmarkcursorpos;
extern int fontpos;
extern int whitelinetop,whitelinebot;

int SetupScreen( void )
{
	if ( ! ( Scr = LockPubScreen( PubScreenName )))
		return( 1L );

	if ( ! ( VisualInfo = GetVisualInfo( Scr, TAG_DONE )))
		return( 2L );

	return( 0L );
}

void CloseDownScreen( void )
{
	if ( VisualInfo ) {
		FreeVisualInfo( VisualInfo );
		VisualInfo = NULL;
	}

	if ( Scr        ) {
		UnlockPubScreen( NULL, Scr );
		Scr = NULL;
	}
}


int OpentnmWindow( void )
{
	UWORD offy = Scr->WBorTop + Scr->RastPort.TxHeight + 1;

	if ( ! ( tnmWnd = OpenWindowTags( NULL,
				WA_Left,	tnmLeft,
				WA_Top,		tnmTop,
				WA_Width,	tnmWidth,
				WA_Height,	tnmHeight + offy,
				WA_IDCMP,	IDCMP_REFRESHWINDOW,
				WA_Flags,	WFLG_DRAGBAR|WFLG_DEPTHGADGET|WFLG_SMART_REFRESH,
				WA_Title,	tnmWdt,
				WA_ScreenTitle,	"Tracknamewindow for Octamed SS 1.03c. Made by Kjetil S. Matheussen 1999.",
				WA_PubScreen,	Scr,
				TAG_DONE )))
	return( 4L );

	GT_RefreshWindow( tnmWnd, NULL );


	return( 0L );
}

void ClosetnmWindow( void )
{
	if ( tnmWnd        ) {
		CloseWindow( tnmWnd );
		tnmWnd = NULL;
	}
}

struct TextFont *mytextfont;
UWORD fontsize;
UWORD screenfontsize;

int numvisibletracks;
int currvisibletrack=0;

int namewidth,startwidth;

void drawwhite(void){
	int lokke;
	SetAPen(tnmWnd->RPort,2);

	Move( tnmWnd->RPort, startwidth, whitelinetop);
	Draw( tnmWnd->RPort, startwidth, whitelinebot);

	Move( tnmWnd->RPort, startwidth+1, whitelinetop);
	Draw( tnmWnd->RPort, startwidth+1, whitelinebot);

	for(lokke=1;lokke<=numvisibletracks;lokke++){
		Move( tnmWnd->RPort, lokke*namewidth+startwidth, whitelinetop);
		Draw( tnmWnd->RPort, lokke*namewidth+startwidth, whitelinebot);

		Move( tnmWnd->RPort, lokke*namewidth+1+startwidth, whitelinetop);
		Draw( tnmWnd->RPort, lokke*namewidth+1+startwidth, whitelinebot);
	}
}

void inserttext(char *trackname,int pos,int pen){
	SetAPen(tnmWnd->RPort,1);
	SetBPen(tnmWnd->RPort,pen);
	Move(tnmWnd->RPort,startwidth+(namewidth*pos)+(startwidth/8)+1,fontpos);
	Text(tnmWnd->RPort,trackname,9);
}


void drawvisibletrack(int pos,int pen){
	SetAPen(tnmWnd->RPort,pen);

	Move(tnmWnd->RPort,startwidth+(namewidth*pos)+2,topmarkcursorpos);
	Draw(tnmWnd->RPort,startwidth+(namewidth*(pos+1))-2,topmarkcursorpos);
	Move(tnmWnd->RPort,startwidth+(namewidth*pos)+2,topmarkcursorpos+1);
	Draw(tnmWnd->RPort,startwidth+(namewidth*(pos+1))-2,topmarkcursorpos+1);

	Move(tnmWnd->RPort,startwidth+(namewidth*pos)+2,botmarkcursorpos);
	Draw(tnmWnd->RPort,startwidth+(namewidth*(pos+1))-2,botmarkcursorpos);
	Move(tnmWnd->RPort,startwidth+(namewidth*pos)+2,botmarkcursorpos+1);
	Draw(tnmWnd->RPort,startwidth+(namewidth*(pos+1))-2,botmarkcursorpos+1);
}

void movecursorleft(void){
	if(currvisibletrack<numvisibletracks-1){
		drawvisibletrack(currvisibletrack,0);
		currvisibletrack++;
		drawvisibletrack(currvisibletrack,1);
	}
}

void movecursorright(void){
	if(currvisibletrack>0){
		drawvisibletrack(currvisibletrack,0);
		currvisibletrack--;
		drawvisibletrack(currvisibletrack,1);
	}
}

void scrollleft(void){
	ScrollRaster(
		tnmWnd->RPort,
		namewidth,0,
		startwidth,topmarkcursorpos+3,
		numvisibletracks*namewidth+startwidth-1,botmarkcursorpos-2
	);

	SetAPen(tnmWnd->RPort,2);
	Move( tnmWnd->RPort,(numvisibletracks-1)*namewidth+startwidth, whitelinetop); 
	Draw(tnmWnd->RPort,(numvisibletracks-1)*namewidth+startwidth, whitelinebot);
	Move( tnmWnd->RPort,(numvisibletracks-1)*namewidth+startwidth+1, whitelinetop); 
	Draw(tnmWnd->RPort,(numvisibletracks-1)*namewidth+startwidth+1, whitelinebot);

	SetAPen(tnmWnd->RPort,0);
	Move( tnmWnd->RPort,(numvisibletracks-1)*namewidth+startwidth+2, whitelinetop); 
	Draw(tnmWnd->RPort,(numvisibletracks-1)*namewidth+startwidth+2, whitelinebot);
	Move( tnmWnd->RPort,(numvisibletracks-1)*namewidth+startwidth+3, whitelinetop); 
	Draw(tnmWnd->RPort,(numvisibletracks-1)*namewidth+startwidth+3, whitelinebot);

	Move( tnmWnd->RPort,(numvisibletracks)*namewidth+startwidth-1, whitelinetop); 
	Draw(tnmWnd->RPort,(numvisibletracks)*namewidth+startwidth-1, whitelinebot);
	Move( tnmWnd->RPort,(numvisibletracks)*namewidth+startwidth-2, whitelinetop); 
	Draw(tnmWnd->RPort,(numvisibletracks)*namewidth+startwidth-2, whitelinebot);



}

void scrollright(void){
	ScrollRaster(
		tnmWnd->RPort,
		-namewidth,0,
		startwidth+4,topmarkcursorpos+3,
		numvisibletracks*namewidth+startwidth,botmarkcursorpos-2
	);

	SetAPen(tnmWnd->RPort,2);
	Move( tnmWnd->RPort,namewidth+startwidth, whitelinetop); 
	Draw(tnmWnd->RPort,namewidth+startwidth, whitelinebot);
	Move( tnmWnd->RPort,namewidth+startwidth+1, whitelinetop); 
	Draw(tnmWnd->RPort,namewidth+startwidth+1, whitelinebot);

	SetAPen(tnmWnd->RPort,0);
	Move( tnmWnd->RPort,namewidth+startwidth+2, whitelinetop); 
	Draw(tnmWnd->RPort,namewidth+startwidth+2, whitelinebot);
	Move( tnmWnd->RPort,namewidth+startwidth+3, whitelinetop); 
	Draw(tnmWnd->RPort,namewidth+startwidth+3, whitelinebot);

	Move( tnmWnd->RPort,namewidth+startwidth-1, whitelinetop); 
	Draw(tnmWnd->RPort,namewidth+startwidth-1, whitelinebot);
	Move( tnmWnd->RPort,namewidth+startwidth-2, whitelinetop); 
	Draw(tnmWnd->RPort,namewidth+startwidth-2, whitelinebot);

}

void showinumname(char *name){
	SetWindowTitles(tnmWnd,name,(UBYTE *)-1);
}

struct TextAttr standardfontstruct={"topaz.font",8,0,0};
struct TextAttr *fontstruct;

void initwindow(
	char *editfontTextAttrstruct,
	UWORD edittopleft,
	UWORD editwidth,
	int numvisibletrakks,
	UWORD screenfontYsize
)
{
	numvisibletracks=numvisibletrakks;

	screenfontsize=screenfontYsize;
	if(screenfontsize==0) screenfontsize=8;

	if(*editorfontname==NULL){
		fontstruct=(struct TextAttr *)editfontTextAttrstruct;
		if(fontstruct==NULL) fontstruct=&standardfontstruct;
	}else{
		fontstruct=&standardfontstruct;
		fontstruct->ta_Name=editorfontname;
		fontstruct->ta_YSize=editorfontsize;
	}

	fontsize=fontstruct->ta_YSize;

	tnmLeft = edittopleft;

	tnmTop = ypos; if(tnmTop==-1) tnmTop=10;

	tnmWidth = editwidth+22;

	if(windowheight==-1){
		tnmHeight = fontsize+screenfontsize+2;
	}else{
		tnmHeight = windowheight;
	}

	if(topmarkcursorpos==-1) topmarkcursorpos=screenfontsize+fontsize-4;

	if(botmarkcursorpos==-1) botmarkcursorpos=screenfontsize+8+fontsize;

	if(fontpos==-1) fontpos=screenfontsize+5+fontsize;

	if(whitelinetop==-1) whitelinetop=screenfontsize+3;

	if(whitelinebot==-1) whitelinebot=screenfontsize+(fontsize*2)+2;

	SetupScreen();
	OpentnmWindow();

	mytextfont=OpenFont(fontstruct);
	SetFont(tnmWnd->RPort,mytextfont);
	SetDrMd(tnmWnd->RPort,JAM2);

	startwidth=TextLength(tnmWnd->RPort,"1234",4 );
	namewidth=TextLength( tnmWnd->RPort,"123456789b",10 );
	drawwhite();
}

void shutdownwindow(void){
	CloseFont(mytextfont);
	ClosetnmWindow();
	CloseDownScreen();
}
