#include	<exec/types.h>
#include	<exec/memory.h>

#include	<proto/xsl.h>
#include	<proto/gadtools.h>
#include	<proto/intuition.h>
#include	<proto/exec.h>
#include	<proto/dos.h>

#include <libraries/p4specials.h>
#include	<proto/picasso96.h>

#include	<stdio.h>

#include "cs.h"
#include "cs_sup.h"
#include "tv.h"

/* This file contains empty template routines that
 * the IDCMP handler will call uppon. Fill out these
 * routines with your code or use them as a reference
 * to create your program.
 */
 
int AutoscanClicked( void )
{
	/* routine when gadget "Autoscan" is clicked. */
	struct TVchannelHandle *new;
	BeginListAction();
	Autoscan();
	EndListAction();
	if((new = (struct TVchannelHandle *)TempList.lh_Head) && new->Node.ln_Succ){
		p96PIP_SetTags(wd, P4PALO_TV_Mute, TRUE, TAG_END);
		p96PIP_SetTags(wd, P4PALO_Tuner_Frequency, *((ULONG *)&(new->Channel)), TAG_END);
		Delay(3);
		p96PIP_SetTags(wd, P4PALO_TV_Mute, FALSE, TAG_END);
	}
	TempChannelIndex = 0;
	sprintf(WindowTitle,"Paloma TV Demo Channel: \"%s\"",TempNode->ln_Name);
	SetWindowTitles(wd,WindowTitle,WindowTitle);
	return(1);
}

int ChannelsClicked( void )
{
	/* routine when gadget "Channels" is clicked. */
	TempNode = FindNumber(&TempList, CSMsg.Code);
	TempChannelIndex = CSMsg.Code;
	GT_SetGadgetAttrs(CSGadgets[GD_Name], CSWnd, NULL, GTST_String, TempNode->ln_Name, TAG_END);

	p96PIP_SetTags(wd, P4PALO_TV_Mute, TRUE, TAG_END);
	p96PIP_SetTags(wd, P4PALO_Tuner_Frequency, ((struct TVchannelHandle *)TempNode)->Channel, TAG_END);
	Delay(3);
	p96PIP_SetTags(wd, P4PALO_TV_Mute, FALSE, TAG_END);

	sprintf(WindowTitle,"Paloma TV Demo Channel: \"%s\"",TempNode->ln_Name);
	SetWindowTitles(wd,WindowTitle,WindowTitle);
	return(1);
}

int UpClicked( void )
{
	/* routine when gadget "Up" is clicked. */
	struct Node *NewPred = TempNode->ln_Pred->ln_Pred;
	
	if(NewPred){
		BeginListAction();
		Remove(TempNode);
		Insert(&TempList,TempNode,NewPred);
		EndListAction();
		TempChannelIndex--;
	}
	return(1);
}

int DownClicked( void )
{
	/* routine when gadget "Down" is clicked. */
	struct Node *NewPred = TempNode->ln_Succ;
	
	if(NewPred){
		BeginListAction();
		Remove(TempNode);
		Insert(&TempList,TempNode,NewPred);
		EndListAction();
		TempChannelIndex++;
	}
	return(1);
}

int TopClicked( void )
{
	/* routine when gadget "Top" is clicked. */
	BeginListAction();
	Remove(TempNode);
	AddHead(&TempList,TempNode);
	EndListAction();
	TempChannelIndex = 0;
	return(1);
}

int BottomClicked( void )
{
	/* routine when gadget "Bottom" is clicked. */
	BeginListAction();
	Remove(TempNode);
	AddTail(&TempList,TempNode);
	EndListAction();
	TempChannelIndex = TempNumChannels-1;
	return(1);
}

int NameClicked( void )
{
	/* routine when gadget "" is clicked. */
	char *NewName;
	if(NewName = CloneStr(GetString(CSGadgets[GD_Name]))){
		BeginListAction();
		FreeVec(TempNode->ln_Name);
		TempNode->ln_Name = NewName;
		EndListAction();
	}
	return(1);
}

int DeleteClicked( void )
{
	/* routine when gadget "Delete" is clicked. */
	if(TempNode){
		BeginListAction();
		Remove(TempNode);
		FreeVec(TempNode->ln_Name);
		FreeVec(TempNode);
		EndListAction();
		TempNumChannels--;
	}
	return(1);
}

int NewClicked( void )
{
	/* routine when gadget "New" is clicked. */
	struct TVchannelHandle *New;

	if(New = AllocVec(sizeof(*New),MEMF_CLEAR)){
		if(New->Node.ln_Name = CloneStr("unnamed")){
			BeginListAction();
			if(TempNode){
				Insert(&TempList,(struct Node *)New,TempNode);
			}else{
				AddTail(&TempList,(struct Node *)New);
			}
			EndListAction();
			TempNode = (struct Node *)New;
			TempNumChannels++;
		}else{
			FreeVec(New);
		}
	}
	return(1);
}

int TuneUpClicked( void )
{
	/* routine when gadget "Tune Up" is clicked. */
	return(TuneUp());
}

int AdjustUpClicked( void )
{
	/* routine when gadget "Tune Up" is clicked. */
	return(AdjustUp());
}

int AdjustDownClicked( void )
{
	/* routine when gadget "Tune Up" is clicked. */
	return(AdjustDown());
}

int TuneDownClicked( void )
{
	/* routine when gadget "Tune Down" is clicked. */
	return(TuneDown());
}

int UseClicked( void )
{
	/* routine when gadget "Use" is clicked. */
	setupcode = SETUP_USE;
	return(0);
}

int SaveClicked( void )
{
	/* routine when gadget "Save" is clicked. */
	setupcode = SETUP_SAVE;
	return(0);
}

int CancelClicked( void )
{
	/* routine when gadget "Cancel" is clicked. */
	setupcode = SETUP_CANCEL;
	return(0);
}

int CSCloseWindow( void )
{
	/* routine for "IDCMP_CLOSEWINDOW". */
	setupcode = SETUP_CANCEL;
	return(0);
}

