/*
(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_SPLT 0x53504C54

#include "kbdsplitw.c"

CHIP static UWORD KeyboardSplitter[]=
{
/*-------- plane # 0 --------*/

  0x0000,  0x0000,
  0x3fff,  0xfc00,
  0x300d,  0x2c00,
  0x300d,  0x2c00,
  0x300d,  0x2c00,
  0x300d,  0x2c00,
  0x300d,  0xbc00,
  0x300d,  0xbc00,
  0x300d,  0xbc00,
  0x300d,  0xbc00,
  0x3fff,  0xfc00,
  0x0000,  0x0000,

/*-------- plane # 1 --------*/

  0x0000,  0x0000,
  0x0000,  0x0400,
  0x024d,  0x2c00,
  0x024d,  0x2c00,
  0xc24d,  0x2f00,
  0xc24d,  0x2f00,
  0xcb6d,  0xbf00,
  0xcb6d,  0xbf00,
  0x0b6d,  0xbc00,
  0x0b6d,  0xbc00,
  0x1fff,  0xfc00,
  0x00ff,  0x0000,

/*-------- plane # 2 --------*/

  0x0000,  0x0000,
  0x0000,  0x0000,
  0x024d,  0x2000,
  0x024d,  0x2000,
  0x024d,  0x2000,
  0x024d,  0x2000,
  0x0b6d,  0xb000,
  0x0b6d,  0xb000,
  0x0b6d,  0xb000,
  0x0b6d,  0xb000,
  0x0000,  0x0000,
  0x0000,  0x0000
};

static struct Image KeyboardSplitterimage=
{
  0,0,24,12,3,
  KeyboardSplitter,
  0x7,0x0,NULL
};

CHIP static UWORD KeyboardSplitter_up[]=
{
/*-------- plane # 0 --------*/

  0x0000,  0x0000,
  0x3fff,  0xfc00,
  0x300d,  0x2c00,
  0x300d,  0x2c00,
  0x300d,  0x2c00,
  0x300d,  0x2c00,
  0x300d,  0xbc00,
  0x300d,  0xbc00,
  0x300d,  0xbc00,
  0x300d,  0xbc00,
  0x3fff,  0xfc00,
  0x0000,  0x0000,

/*-------- plane # 1 --------*/

  0x00ff,  0x0000,
  0x0000,  0x0400,
  0x024d,  0x2c00,
  0x024d,  0x2c00,
  0xc24d,  0x2f00,
  0xc24d,  0x2f00,
  0xcb6d,  0xbf00,
  0xcb6d,  0xbf00,
  0x0b6d,  0xbc00,
  0x0b6d,  0xbc00,
  0x1fff,  0xfc00,
  0x0000,  0x0000,

/*-------- plane # 2 --------*/

  0x0000,  0x0000,
  0x0000,  0x0000,
  0x024d,  0x2000,
  0x024d,  0x2000,
  0x024d,  0x2000,
  0x024d,  0x2000,
  0x0b6d,  0xb000,
  0x0b6d,  0xb000,
  0x0b6d,  0xb000,
  0x0b6d,  0xb000,
  0x0000,  0x0000,
  0x0000,  0x0000
};

static struct Image KeyboardSplitter_upimage=
{
  0,0,24,12,3,
  KeyboardSplitter_up,
  0x7,0x0,NULL
};

struct SplitTool {
    struct Tool tool;
    short center;
};
 
extern struct Functions *functions;

extern printf();


SAVEDS static struct Event *processeventcode(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;
                event->type |= EVENT_BRANCH;
            }
            else {
                (*functions->freeevent)((struct Event *)event);
                return(struct Event *)(0);
            }
        }
    }
    return((struct Event *)event);
}


SAVEDS void setgadgettext(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);
    }
}


SAVEDS void edittoolcode(struct SplitTool *tool)
{
    struct IntuiMessage *message;
    struct Window *window;
    struct RastPort *rp;
    long class,code;
    short octave, key;
    //char string[10];
    long temp;
    short refresh = 1;
    static char *convert[] = 
        {"C","C#","D","D#","E","F","F#","G","G#","A","A#","B"};
    static char *octaves[] = 
        {"0","1","2","3","4","5","6","7","8","9","10"};
    struct Gadget *gadget;
    struct NewWindow *newwindow;
    static struct Menu TitleMenu = {    
        NULL,0,0,440,10,MENUENABLED,
        "Keyboard Split v2.1 ©1991 Blue Ribbon SoundWorks, Ltd.",0
    };
    splitNewWindowStructure1.Screen = functions->screen;
    if (tool->tool.touched & TOUCH_EDIT) {
        splitNewWindowStructure1.LeftEdge = tool->tool.left;
        splitNewWindowStructure1.TopEdge = tool->tool.top;
    }
    if (!tool->tool.touched) {
        tool->center = 60;
    }
    newwindow = (struct NewWindow *) 
        (*functions->DupeNewWindow)(&splitNewWindowStructure1);
    if (!newwindow) return;
    newwindow->Title = NULL;
    newwindow->Flags |= BORDERLESS;
    newwindow->Flags &= ~0xF;
    newwindow->BlockPen = 0;
    newwindow->DetailPen = 0;
    window = (struct Window *) (*functions->FlashyOpenWindow)(newwindow);
    if (!window) return;
    tool->tool.window = window;
    rp = window->RPort;
    SetMenuStrip(window,&TitleMenu);
    (*functions->EmbossWindowOn)(window,WINDOWCLOSE|WINDOWDEPTH|WINDOWDRAG,
        "Split",(short)-1,(short)-1,0,0);
    (*functions->EmbossOn)(window,2,0);
    (*functions->EmbossOn)(window,1,0);
    PrintIText(rp,&splitIText3,8,18);
    for (;;) {
        if (refresh) {
            setgadgettext(window,1,convert[tool->center % 12]);
            setgadgettext(window,2,octaves[tool->center / 12]);
        }
        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) {
            class = gadget->GadgetID;
            switch (class) {
                case 1 :
                    key = (*functions->popupkey)(window,tool->center % 12);
                    octave = tool->center / 12;
                    temp = (octave * 12) + key;
                    if (key == 1 || key == 3 || key == 6 || key == 8 || key == 10)
                        splitIText1.LeftEdge = 5;
                    else splitIText1.LeftEdge = 9;
                    tool->center = temp;
                    refresh = 1;
                    break;
                case 2 :
                    octave = (*functions->popupoctave)(window,tool->center / 12);
                    key = tool->center % 12;
                    temp = (octave * 12) + key;
                    tool->center = temp;
                    refresh = 1;
                    break;
            }
        }
    }
    tool->tool.window = NULL;
    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;
    ClearMenuStrip(window);
    (*functions->EmbossOff)(window,1);
    (*functions->EmbossOff)(window,2);
    (*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_SPLT;
    master.toolsize = sizeof(struct SplitTool);
    master.image = &KeyboardSplitterimage;
    master.upimage = &KeyboardSplitter_upimage;
    strcpy(master.name,"Keyboard Split");
    master.edittool = (void_cast)edittoolcode;
    master.processevent = (event_cast)processeventcode;
    master.tooltype = TOOL_NORMAL | TOOL_BRANCHOUT;
    return(&master);
}
