/*	Split.c

	© 1989 Blue Ribbon Bakery
*/

#include "bars.h"
#include <libraries/dos.h>
#include <exec/memory.h>
#include <string.h>
#include <intuition/intuition.h>

#define ID_SPLT	0x53504C54

static struct Gadget splitGadget2 = {
	NULL,
	112,18,
	26,13,
	GADGHBOX+GADGHIMAGE,
	RELVERIFY+GADGIMMEDIATE,
	BOOLGADGET,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	2,
	NULL
};

static SHORT splitBorderVectors1[] = {
	0,0,
	126,0,
	126,14,
	0,14,
	0,1
};
static struct Border splitBorder1 = {
	-66,-1,
	4,0,JAM1,
	5,
	splitBorderVectors1,
	NULL
};

static struct IntuiText splitIText1 = {
	7,0,JAM2,
	-61,3,
	NULL,
	"Split:",
	NULL
};

static struct Gadget splitGadget1 = {
	&splitGadget2,
	82,18,
	27,13,
	GADGHBOX+GADGHIMAGE,
	RELVERIFY+GADGIMMEDIATE,
	BOOLGADGET,
	(APTR)&splitBorder1,
	NULL,
	&splitIText1,
	NULL,
	NULL,
	1,
	NULL
};

#define splitGadgetList1 splitGadget1

static struct NewWindow splitNewWindowStructure1 = {
	150,33,
	159,40,
	6,1,
	GADGETDOWN+GADGETUP+CLOSEWINDOW,
	WINDOWDRAG+WINDOWDEPTH+WINDOWCLOSE+ACTIVATE+NOCAREREFRESH,
	&splitGadget1,
	NULL,
	"Split",
	NULL,
	NULL,
	5,5,
	-1,-1,
	CUSTOMSCREEN
};

/*	The Keyboard Splitter has two icons, one for when it connects
	down, and one for when it connects up.
*/

static UWORD splitup[] = { 
/*------ plane # 0: --------*/

  0x0,   0x0, 
  0x3f00,   0xfc00, 
  0x201a,   0x4c00, 
  0x201a,   0x4c00, 
  0x201a,   0x4c00, 
  0x201a,   0x4f00, 
  0x201b,   0x6f00, 
  0x201b,   0x6c00, 
  0x201b,   0x6c00, 
  0x201b,   0x6c00, 
  0x3fff,   0xfc00, 
  0x0,   0x0,
 /*------ plane # 1: --------*/

  0xff,   0x0, 
  0xff,   0x0, 
  0x49a,   0x4000, 
  0x49a,   0x4000, 
  0xc49a,   0x4f00, 
  0xc49a,   0x4f00, 
  0xd6db,   0x6f00, 
  0xd6db,   0x6f00, 
  0x16db,   0x6000, 
  0x16db,   0x6000, 
  0x0,   0x0, 
  0x0,   0x0,
 /*------ plane # 2: --------*/

  0x3c,   0x0, 
  0x3fff,   0xfc00, 
  0x249a,   0x4c00, 
  0x249a,   0x4c00, 
  0x249a,   0x4c00, 
  0x249a,   0x4f00, 
  0x36db,   0x6f00, 
  0x36db,   0x6c00, 
  0x36db,   0x6c00, 
  0x36db,   0x6c00, 
  0x3fff,   0xfc00, 
  0x0,   0x0};
static  struct  Image splitupimage =
 {
 0,0,
 24 , 12 , 3 ,
 &splitup[0],
 0x1f,0x00,
 NULL,
 };
 
static UWORD splitdown[] = { 
/*------ plane # 0: --------*/

  0x0,   0x0, 
  0x3fff,   0xfc00, 
  0x201a,   0x4c00, 
  0x201a,   0x4c00, 
  0x201a,   0x4c00, 
  0x201a,   0x4f00, 
  0x201b,   0x6f00, 
  0x201b,   0x6c00, 
  0x201b,   0x6c00, 
  0x201b,   0x6c00, 
  0x3f00,   0xfc00, 
  0x0,   0x0,
 /*------ plane # 1: --------*/

  0x0,   0x0, 
  0x0,   0x0, 
  0x49a,   0x4000, 
  0x49a,   0x4000, 
  0xc49a,   0x4f00, 
  0xc49a,   0x4f00, 
  0xd6db,   0x6f00, 
  0xd6db,   0x6f00, 
  0x16db,   0x6000, 
  0x16db,   0x6000, 
  0xff,   0x0, 
  0xff,   0x0,
 /*------ plane # 2: --------*/

  0x0,   0x0, 
  0x3fff,   0xfc00, 
  0x249a,   0x4c00, 
  0x249a,   0x4c00, 
  0x249a,   0x4c00, 
  0x249a,   0x4f00, 
  0x36db,   0x6f00, 
  0x36db,   0x6c00, 
  0x36db,   0x6c00, 
  0x36db,   0x6c00, 
  0x3fff,   0xfc00, 
  0x3c,   0x0};
 struct  Image splitdownimage =
 {
 0,0,
 24 , 12 , 3 ,
 &splitdown[0],
 0x1f,0x00,
 NULL,
 };
 
 
struct SplitTool {
    struct Tool tool;
    short center;
};
 
extern struct Functions *functions;

extern printf();

static struct Event *processeventcode(event)

struct NoteEvent *event;

{
    struct SplitTool *tool = (struct SplitTool *) event->tool;
    event->tool = event->tool->next;
    if (!tool->tool.touched) {
	tool->center = 60;
	tool->tool.touched = TOUCH_INIT;
    }
    if ((event->status == MIDI_NOTEON) || 
	(event->status == MIDI_NOTEOFF) ||
	(event->status == MIDI_PTOUCH)) {
	if (event->value < tool->center) {
	    if (tool->tool.branch) {
		event->tool = tool->tool.branch;
	    }
	    else {
		(*functions->freeevent)(event);
		return(0);
	    }
	}
    }
    return((struct NoteEvet *)event);
}

void edittoolcode(tool)

struct SplitTool *tool;

{
    struct IntuiMessage *message;
    struct Window *window;
    long class, code;
    short octave, key;
    char string[10];
    static struct IntuiText itext = { 7,0,JAM2,0,0,NULL,0,0 };
    static char *convert[] = 
	{"C ","C#","D ","D#","E ","F ","F#","G ","G#","A ","A#","B "};
    struct Gadget *gadget;
    struct NewWindow *newwindow;
    splitNewWindowStructure1.Screen = functions->screen;
    if (tool->tool.touched & TOUCH_EDIT) {
	splitNewWindowStructure1.LeftEdge = tool->tool.left;
	splitNewWindowStructure1.TopEdge = tool->tool.top;
	splitNewWindowStructure1.Width = tool->tool.width;
	splitNewWindowStructure1.Height = tool->tool.height;
    }
    if (!(tool->tool.touched & TOUCH_INIT)) tool->center = 60;
    newwindow = (struct NewWindow *) 
	(*functions->DupeNewWindow)(&splitNewWindowStructure1);
    if (!newwindow) return;
    window = (struct Window *) (*functions->FlashyOpenWindow)(newwindow);
    if (!window) return;
    tool->tool.window = window;
    for (;;) {
	itext.IText = convert[tool->center % 12];
	PrintIText(window->RPort,&itext,82,21);
	(*functions->Itoa)(tool->center / 12,string,10);
	itext.IText = string;
	PrintIText(window->RPort,&itext,120,21);
	message = (struct IntuiMessage *) (*functions->GetIntuiMessage)(window);
	class = message->Class;
	code = message->Code;
	gadget = (struct Gadget *) message->IAddress;
	ReplyMsg((struct Message *)message);
	if (class == CLOSEWINDOW) break;
	else if (class == GADGETDOWN) {
	    class = gadget->GadgetID;
	    switch (class) {
		case 1 :
		    key = (*functions->popupkey)(window,tool->center % 12);
		    octave = tool->center / 12;
		    code = (octave * 12) + key;
		    tool->center = code;
		    break;
		case 2 :
		    octave = (*functions->popupoctave)(window,tool->center / 12);
		    key = tool->center % 12;
		    code = (octave * 12) + key;
		    tool->center = code;
		    break;
	    }
	}
    }
    tool->tool.window = 0;
    tool->tool.left = window->LeftEdge;
    tool->tool.top = window->TopEdge;
    tool->tool.width = window->Width;
    tool->tool.height = window->Height;
    tool->tool.touched = TOUCH_INIT | TOUCH_EDIT;
    (*functions->FlashyCloseWindow)(window);
    (*functions->DeleteNewWindow)(newwindow);
}

static struct ToolMaster master;

struct ToolMaster *inittoolmaster()

{
    memset((char *)&master,0,sizeof(struct ToolMaster));
    master.toolid = ID_SPLT;
    master.toolsize = sizeof(struct SplitTool);
    master.image = &splitdownimage;
    master.upimage = &splitupimage;
    strcpy(master.name,"Keyboard Split");
    master.edittool = edittoolcode;
    master.processevent = processeventcode;
    master.tooltype = TOOL_NORMAL | TOOL_BRANCHOUT;
    return(&master);
}
