/*
(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 <intuition/intuition.h>
#include "/v.h"

#define ID_TRAN 0x5452414E

CHIP static UWORD Transpose[]=
{
/*-------- 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,
  0x0f3f,  0xfc00,
  0xcf3f,  0xff00,
  0xcf3f,  0xff00,
  0xcf3f,  0xff00,
  0xcc3f,  0xff00,
  0x0c3f,  0xfc00,
  0x0fff,  0xfc00,
  0x1fff,  0xfc00,
  0x00ff,  0x0000,

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

  0x0000,  0x0000,
  0x0000,  0x0000,
  0x0000,  0x0000,
  0x0003,  0x0000,
  0x0007,  0x8000,
  0x000f,  0xc000,
  0x001b,  0x6000,
  0x0003,  0x0000,
  0x0003,  0x0000,
  0x0000,  0x0000,
  0x0000,  0x0000,
  0x0000,  0x0000
};

static struct Image Transposeimage=
{
  0,0,24,12,3,
  Transpose,
  0x7,0x0,NULL
};

CHIP static UWORD Transpose_up[]=
{
/*-------- 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 --------*/

  0x00ff,  0x0000,
  0x0000,  0x0400,
  0x0fff,  0xfc00,
  0x0f3f,  0xfc00,
  0xcf3f,  0xff00,
  0xcf3f,  0xff00,
  0xcf3f,  0xff00,
  0xcc3f,  0xff00,
  0x0c3f,  0xfc00,
  0x0fff,  0xfc00,
  0x1fff,  0xfc00,
  0x0000,  0x0000,

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

  0x0000,  0x0000,
  0x0000,  0x0000,
  0x0000,  0x0000,
  0x0003,  0x0000,
  0x0007,  0x8000,
  0x000f,  0xc000,
  0x001b,  0x6000,
  0x0003,  0x0000,
  0x0003,  0x0000,
  0x0000,  0x0000,
  0x0000,  0x0000,
  0x0000,  0x0000
};

static struct Image Transpose_upimage=
{
  0,0,24,12,3,
  Transpose_up,
  0x7,0x0,NULL
};

struct TransposeTool {
    struct Tool tool;
    short transpose;
    char octave;
    char interval;
};

#ifdef GERMAN
#include "transpwg.c"
#endif
#ifdef ENGLISH
#include "transpw.c"
#endif
#ifdef FRENCH
#include "transpwf.c"
#endif

extern struct Functions *functions;

extern printf();


SAVEDS static struct Event *processeventcode(struct NoteEvent *event)
{
    struct TransposeTool *tool = (struct TransposeTool *) event->tool;
    short value;
    char offset;
    struct NoteEvent *copy;
    event->tool = event->tool->next;
    if (!(event->type & EVENT_VOICE)) return((struct Event *)event);
    if ((event->status == MIDI_NOTEON) || (event->status == MIDI_NOTEOFF) ||
        (event->status == MIDI_PTOUCH)) {
        value = (*functions->twelvetoscale)
            (tool->tool.clip,event->time,event->value,&offset);
        value += tool->transpose;
        value = (*functions->scaletotwelve)(tool->tool.clip,event->time,value);
        value += offset;
        while (value < 0) value += 12;
        while (value > 127) value -= 12;
        if (tool->tool.branch) {
            copy = (struct NoteEvent *) (*functions->allocevent)();
            if (copy) {
                copy->value = value;
                copy->velocity = event->velocity;
                copy->duration = event->duration;
                copy->type = event->type;
                copy->status = event->status;
                event->next = copy;
                copy->time = event->time;
                copy->tool = tool->tool.branch;
                copy->type |= EVENT_BRANCH;
            }
        }
        else event->value = value;
    }
    return((struct Event *)event);
}


SAVEDS static long transposeroutine(struct Window *window,
                                    struct Gadget *gadget,
                                    long x)
{
    char text[20];
    struct RastPort *rp = window->RPort;
    Move(rp,270,38);
    SetAPen(rp,1);
    SetBPen(rp,0);
    SetDrMd(rp,JAM2);
    x -= 10;
    if (x <= 0) sprintf(text,"%ld  ",x);
    else sprintf(text,"+%ld  ",x);
    Text(rp,text,3);
    return((long)x);
}


SAVEDS static long intervalroutine(struct Window *window,
                                   struct Gadget *gadget,
                                   long x)
{
    static char *intervals[13] = {
         "<7th","<6th","<5th","<4th","<3rd","<2nd","U   ",
        ">2nd",">3rd",">4th",">5th",">6th",">7th"
    };
    struct RastPort *rp = window->RPort;
    Move(rp,270,23);
    SetAPen(rp,1);
    SetBPen(rp,0);
    SetDrMd(rp,JAM2);
    Text(rp,intervals[x],4);
    return((long)x - 6);
}


SAVEDS void edittoolcode(struct TransposeTool *tool)
{
    struct IntuiMessage *message;
    struct Window *window;
    long class,code;
    char refresh = 1;
    struct Gadget *gadget;
    struct NewWindow *newwindow;
    static struct Menu TitleMenu = {    
        NULL,0,0,460,10,MENUENABLED,
#ifdef GERMAN
        "Transponieren  v2.1 © 1991 The Blue Ribbon SoundWorks, Ltd.",0
#endif
#ifdef ENGLISH
        "Transpose  v2.1 © 1991 The Blue Ribbon SoundWorks, Ltd.",0
#endif
#ifdef FRENCH
        "Transposer  v2.1 © 1991 The Blue Ribbon SoundWorks, Ltd.",0
#endif
    };
    transposeNewWindowStructure1.Screen = functions->screen;
    if (tool->tool.touched & TOUCH_EDIT) {
        transposeNewWindowStructure1.LeftEdge = tool->tool.left;
        transposeNewWindowStructure1.TopEdge = tool->tool.top;
    }
    newwindow = (struct NewWindow *) 
        (*functions->DupeNewWindow)(&transposeNewWindowStructure1);
    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;
    SetMenuStrip(window,&TitleMenu);
    tool->tool.window = window;
    (*functions->EmbossWindowOn)(window,WINDOWCLOSE|WINDOWDEPTH|WINDOWDRAG,
#ifdef GERMAN
        "Transponieren",(short)-1,(short)-1,0,0);
#endif
#ifdef ENGLISH
        "Transpose",(short)-1,(short)-1,0,0);
#endif
#ifdef FRENCH
        "Transposer",(short)-1,(short)-1,0,0);
#endif
    (*functions->FatEmbossedPropOn)(window,1,3,4,(long_cast)transposeroutine,21,1);
    (*functions->FatEmbossedPropOn)(window,2,5,6,(long_cast)intervalroutine,13,1);
    for (;;) {
        if (refresh) {
            tool->transpose = (tool->octave * 7) + tool->interval;
            (*functions->ModifyEmbossedProp)(window,1,tool->octave+10,0,0,0,0,0);
            (*functions->DrawEmbossedProp)(window,1);
            (*functions->ModifyEmbossedProp)(window,2,tool->interval+6,0,0,0,0,0);
            (*functions->DrawEmbossedProp)(window,2);
        }
        refresh = 0;
        message = (struct IntuiMessage *) (*functions->GetIntuiMessage)(window);
        code = message->Code;
        class = message->Class;
        gadget = (struct Gadget *) message->IAddress;
        class = (*functions->SystemGadgets)(window,class,gadget,code);
        ReplyMsg((struct Message *)message);
        if (class == CLOSEWINDOW) break;
        else if (class == GADGETDOWN) {
            refresh = 1;
            switch (gadget->GadgetID) {
                case 1 :
                    tool->octave = (*functions->DragEmbossedProp)(window,1);
                    break;
                case 2 :
                    tool->interval = (*functions->DragEmbossedProp)(window,2);
                    break;
                case 3 :
                    tool->octave = (*functions->ShiftEmbossedProp)(window,1,-1,0);
                    break;
                case 4 :
                    tool->octave = (*functions->ShiftEmbossedProp)(window,1,1,0);
                    break;
                case 5 :
                    tool->interval = (*functions->ShiftEmbossedProp)(window,2,-1,0);
                    break;
                case 6 :
                    tool->interval = (*functions->ShiftEmbossedProp)(window,2,1,0);
                    break;
            }
        }
    }
    (*functions->FatEmbossedPropOff)(window,1,3,4);
    (*functions->FatEmbossedPropOff)(window,2,5,6);
    tool->tool.window = NULL;
    tool->tool.left = window->LeftEdge;
    tool->tool.top = window->TopEdge;
    tool->tool.touched = TOUCH_EDIT | TOUCH_INIT;
    ClearMenuStrip(window);
    (*functions->FlashyCloseWindow)(window);
    (*functions->DeleteNewWindow)(newwindow);
}

static struct ToolMaster master;

//extern long stdout;


SAVEDS struct ToolMaster *inittoolmaster()
{
    memset((char *)&master,0,sizeof(struct ToolMaster));
    master.toolid = ID_TRAN;
    master.image = &Transposeimage;
    master.upimage = &Transpose_upimage;
    master.toolsize = sizeof(struct TransposeTool);
#ifdef GERMAN
    strcpy(master.name,"Transponieren");
#endif
#ifdef ENGLISH
    strcpy(master.name,"Transpose");
#endif
#ifdef FRENCH
    strcpy(master.name,"Transposer");
#endif
    master.edittool = (void_cast)edittoolcode;
    master.processevent = (event_cast)processeventcode;
    master.tooltype = TOOL_NORMAL | TOOL_BRANCHOUT;
    return(&master);
}
