/*
(c) Copyright 1988-1994 Microsoft Corporation.

Microsoft Bars&Pipes Professional Source Code License

This License governs use of the accompanying Software.  
Microsoft hopes you find this Software useful.

You are licensed to do anything you want with the Software. 

In return, we simply require that you agree:

1.      Not to remove any copyright notices from the Software.

2.      That the Software comes "as is", with no warranties.  
        None whatsoever. This means no implied warranty of merchantability or 
        fitness for a particular purpose or any warranty of non-infringement.  
        Also, you must pass this disclaimer on whenever you distribute the Software.

3.      That we will not be liable for any of those types of damages known as indirect, 
        special, consequential, or incidental related to the Software or this License, 
        to the maximum extent the law permits. Also, you must pass this limitation of 
        liability on whenever you distribute the Software.

4.      That if you sue anyone over patents that you think may apply to the Software 
        for that person’s use of the Software, your license to the Software ends automatically.

5.      That the patent rights Microsoft is licensing only apply to the Software, 
        not to any derivatives you make.

6.      That your rights under the License end automatically if you breach this in any way.
*/

#include "/CompilerSpecific.h"
#include <libraries/dos.h>
#include <proto/exec.h>
#include <proto/graphics.h>
#include <proto/intuition.h>
#include <exec/types.h>
#include <exec/memory.h>
#include <string.h>
#include <intuition/intuition.h>
#include "/v.h"

#define ID_ECHO 0x4543484F

CHIP static UWORD Echo[]=
{
/*-------- plane # 0 --------*/

  0x0000,  0x0000,
  0x3fff,  0xfc00,
  0x3000,  0x0c00,
  0x3000,  0x0c00,
  0x3000,  0x0c00,
  0x3000,  0x0c00,
  0x3000,  0x0c00,
  0x3000,  0x0c00,
  0x3000,  0x0c00,
  0x3000,  0x0c00,
  0x3fff,  0xfc00,
  0x0000,  0x0000,

/*-------- plane # 1 --------*/

  0x0000,  0x0000,
  0x0000,  0x0400,
  0x0fff,  0xfc00,
  0x0fee,  0xec00,
  0xcfee,  0xef00,
  0xcfee,  0xef00,
  0xcf88,  0x8f00,
  0xcf88,  0x8f00,
  0x0fff,  0xfc00,
  0x0fff,  0xfc00,
  0x1fff,  0xfc00,
  0x0000,  0x0000,

/*-------- plane # 2 --------*/

  0x0000,  0x0000,
  0x0000,  0x0000,
  0x0000,  0x0000,
  0x0211,  0x1000,
  0x0211,  0x1000,
  0x0211,  0x1000,
  0x0e77,  0x7000,
  0x0e77,  0x7000,
  0x0000,  0x0000,
  0x0000,  0x0000,
  0x0000,  0x0000,
  0x0000,  0x0000
};

static struct Image Echoimage=
{
  0,0,24,12,3,
  Echo,
  0x7,0x0,NULL
};

struct EchoTool {
    struct Tool tool;
    short echocount;
    short echodistance;
    char echostyle;
};
 
extern struct Functions *functions;

#ifdef GERMAN
#include "echowg.c"
#endif 
#ifdef ENGLISH
#include "echow.c"
#endif
#ifdef FRENCH
#include "echowf.c"
#endif

SAVEDS void setgadgettext(window,id,text)

struct Window *window;
short id;
char *text;

{
    struct Gadget *gadget = (struct Gadget *) (*functions->GetGadget)(window,id);
    if (gadget) {
        gadget->GadgetText->IText = text;
        (functions->DrawEmbossed)(window,id);
    }
}

struct EchoTool *selectedtool = NULL;


SAVEDS static long lengthroutine(window,gadget,x)

struct Window *window;
struct Gadget *gadget;
unsigned long x;

{
    char text[30];
    struct RastPort *rp = window->RPort;
    Move(rp,216,24);
    SetAPen(rp,1);
    SetBPen(rp,0);
    SetDrMd(rp,JAM2);
    (*functions->lengthtostring)(selectedtool->tool.clip,0,x,text);
    strcat(text,"       ");
    Text(rp,text,10);
    return((long)x);
}


SAVEDS static long countroutine(window,gadget,x)

struct Window *window;
struct Gadget *gadget;
unsigned long x;

{
    char text[30];
    struct RastPort *rp = window->RPort;
    Move(rp,216,37);
    SetAPen(rp,1);
    SetBPen(rp,0);
    SetDrMd(rp,JAM2);
    sprintf(text,"%ld  ",x);
    Text(rp,text,2);
    return((long)x);
}


SAVEDS void edittoolcode(tool)

struct EchoTool *tool;

{
    struct IntuiMessage *message;
    struct Window *window;
    long class, code;
    short refresh = 1;
    struct Gadget *gadget;
    struct NewWindow *newwindow;
    static char *convert[] = 
        {" 1  ","1/2 ","1/4 ","1/8 ","1/16","1/32", "1/64", "????"};
    short type = 0, triplet = 0;
#ifdef GERMAN
    static char *style[3] = { "Fallend ","Bleibend","Steigend"};
#endif
#ifdef ENGLISH
    static char *style[3] = { "Falling"," Level  ","Rising "};
#endif
#ifdef FRENCH
    static char *style[3] = { "Décroit ","Constant"," Croît  "};
#endif
    echoNewWindowStructure1.Screen = functions->screen;
    if (tool->tool.touched & TOUCH_EDIT) {
        echoNewWindowStructure1.LeftEdge = tool->tool.left;
        echoNewWindowStructure1.TopEdge = tool->tool.top;
    }
    if (!tool->tool.touched) {
        tool->echodistance = 192;
        tool->echocount = 4;
        tool->tool.touched = TOUCH_INIT;
    }
    newwindow = (struct NewWindow *) 
        (*functions->DupeNewWindow)(&echoNewWindowStructure1);
    if (!newwindow) return;
    newwindow->Title = NULL;
    newwindow->Flags |= BORDERLESS;
    newwindow->Flags &= ~0xF;
    newwindow->DetailPen = 0;
    newwindow->BlockPen = 0;
    window = (struct Window *) 
        (*functions->FlashyOpenWindow)(newwindow);
    if (!window) return;
    tool->tool.window = window;
    (*functions->EmbossWindowOn)(window,WINDOWCLOSE|WINDOWDEPTH|WINDOWDRAG,
        "Echo",(short)-1,(short)-1,0,0);
    (*functions->EmbossOn)(window,5,0);
    (*functions->EmbossOn)(window,6,1);
    (*functions->EmbossOn)(window,10,1);
    selectedtool = tool;
    (*functions->FatEmbossedPropOn)(window,1,2,3,(long_cast)lengthroutine,1537,1);
    (*functions->FatEmbossedPropOn)(window,7,8,9,(long_cast)countroutine,20,1);
    for (;;) {
        if (refresh) {
            selectedtool = tool;
            (*functions->ModifyEmbossedProp)(window,1,tool->echodistance,0,0,0,0,0);
            (*functions->DrawEmbossedProp)(window,1);
            (*functions->ModifyEmbossedProp)(window,7,tool->echocount,0,0,0,0,0);
            (*functions->DrawEmbossedProp)(window,7);
            class = 768;
            triplet = 0;
            for (type = 0;type < 7; type++) {
                if (class == tool->echodistance) {
                    triplet = 0;
                    break;
                }
                class = class >> 1;
            }
            if (type > 6) {
                class = 512;
                for (type = 0;type < 7; type++) {
                    if (class == tool->echodistance) {
                        triplet = 1;
                        break;
                    }
                    class = class >> 1;
                }
            }
            (*functions->SelectEmbossed)(window,6,triplet);
            setgadgettext(window,5,convert[type]);
            setgadgettext(window,10,style[tool->echostyle]);
        }
        refresh = 0;
        message = (struct IntuiMessage *) (*functions->GetIntuiMessage)(window);
        class = message->Class;
        code = message->Code;
        gadget = (struct Gadget *) message->IAddress;
        class = (*functions->SystemGadgets)(window,class,gadget,code);
        ReplyMsg((struct Message *)message);
        if (class == CLOSEWINDOW) break;
        else if (class == GADGETDOWN) {
            selectedtool = tool;
            switch (gadget->GadgetID) {
                case 1 :
                    tool->echodistance = (*functions->DragEmbossedProp)(window,1);
                    break;
                case 2 :
                    tool->echodistance = 
                        (*functions->ShiftEmbossedProp)(window,1,-10,0);
                    break;
                case 3 :
                    tool->echodistance = 
                        (*functions->ShiftEmbossedProp)(window,1,10,0);
                    break;
                case 5 :
                    code = (*functions->popupnote)(window,-1);
                    if (code >= 0) {
                        code = 768 >> code;
                        if (triplet) code = (code << 1) / 3;
                        tool->echodistance = code;
                    }
                    refresh = 1;
                    break;
                case 6 :
                    triplet = !triplet;
                    code = 768 >> type;
                    if (triplet) code = (code << 1) / 3;
                    tool->echodistance = code;
                    refresh = 1;
                    break;
                case 7 :
                    tool->echocount = (*functions->DragEmbossedProp)(window,7);
                    break;
                case 8 :
                    tool->echocount = 
                        (*functions->ShiftEmbossedProp)(window,7,-1,0);
                    break;
                case 9 :
                    tool->echocount = 
                        (*functions->ShiftEmbossedProp)(window,7,1,0);
                    break;
                case 10 :
                    tool->echostyle = (tool->echostyle + 1) % 3;
                    refresh = 1;
                    break;
            }
        }
    }
    (*functions->EmbossOff)(window,5);
    (*functions->EmbossOff)(window,6);
    (*functions->EmbossOff)(window,10);
    (*functions->FatEmbossedPropOff)(window,1,2,3);
    (*functions->FatEmbossedPropOff)(window,7,8,9);
    tool->tool.window = NULL;
    tool->tool.left = window->LeftEdge;
    tool->tool.top = window->TopEdge;
    tool->tool.touched = TOUCH_INIT | TOUCH_EDIT;
    (*functions->FlashyCloseWindow)(window);
    (*functions->DeleteNewWindow)(newwindow);
}

extern printf();


SAVEDS static struct Event *processeventcode(event)

struct NoteEvent *event;

{
    struct EchoTool *tool = (struct EchoTool *) event->tool;
    struct NoteEvent *new;
    short count;
    short velocity, inc;
    long time;
    if (!tool->tool.touched) {
        tool->echocount = 4;
        tool->echodistance = 192;
        tool->tool.touched = TOUCH_INIT;
    }
    count = tool->echocount;
    event->tool = event->tool->next;
    if ((event->status == MIDI_NOTEON) || (event->status == MIDI_NOTEOFF) ||
        (event->status == MIDI_PTOUCH)) {
        if (tool->echostyle == 1) {
            inc = 0;
            velocity = event->velocity;
        }
        else if (tool->echostyle == 2) {
            inc = IDivS(event->velocity,count+1);
            velocity = event->velocity;
            event->velocity = inc;
            inc = -inc;
        }
        else {
            inc = IDivS(event->velocity,count+1);
            velocity = inc;
        }
        time = event->time + (tool->echodistance * count);
        for (;count;count--) {
            new = (struct NoteEvent *) (*functions->allocevent)();
            if (new) {
                new->tool = event->tool;
                new->status = event->status;
                new->value = event->value;
                new->type = event->type;
                new->duration = event->duration;
                new->time = time;
                time -= tool->echodistance;
                new->velocity = velocity;
                velocity += inc;
                new->next = event;
                event = new;
            }
        }
    }
    return((struct Event *)event);
}

static struct ToolMaster master;

//extern long stdout;


SAVEDS struct ToolMaster *inittoolmaster()

{
    memset((char *)&master,0,sizeof(struct ToolMaster));
    master.toolsize = sizeof(struct EchoTool);
    master.toolid = ID_ECHO;
    master.edittool = (void_cast)edittoolcode;
    master.image = &Echoimage;
    strcpy(master.name,"Echo");
    master.processevent = (event_cast)processeventcode;
    master.tooltype = TOOL_NORMAL;
    return(&master);
}
