/*
(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 <intuition/intuition.h>
#include <graphics/gfxmacros.h>
#include <proto/graphics.h>
#include <proto/exec.h>
#include <proto/intuition.h>
#include <exec/memory.h>
#include <string.h>
#include "/v.h"
#include "windows.h"
#include "all.h"

extern struct Functions functions;
FARCALL extern struct Menu vMenu1;
#include "metrow.c"

void sendclick(on,time,velocity)

char on;
unsigned long time;
char velocity;

{
    struct Event *event;
    static struct Track track;
    if (functions.midiouttool) {
        event = (struct Event *) allocevent();
        if (event) {
            functions.midiouttool->track = &track;
            track.channelout = functions.clickchannel;
            event->tool = functions.midiouttool;
            if (on) {
                event->status = MIDI_NOTEON;
                event->byte2 = velocity;
            }
            else event->status = MIDI_NOTEOFF;
            event->byte1 = functions.midiclick & 0x7F;
            event->time = time;
            event->type = EVENT_VOICE;
            qevent(event);
        }
    }
}

static rootroutine(window,gadget,x,y)

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

{
    char text[20];
    struct RastPort *rp = window->RPort;
    functions.midiclick &= ~0x7F;
    functions.midiclick |= x;
    Move(rp,gadget->LeftEdge + gadget->Width + 54,gadget->TopEdge + 7);
    SetAPen(rp,1);
    SetBPen(rp,0);
    SetDrMd(rp,JAM2);
    notetostring(0,x,text);
    strcat(text,"  ");
    text[4] = 0;
    Text(rp,text,4);
    return((long)x);
}

static loudroutine(window,gadget,x,y)

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

{
    char text[20];
    struct RastPort *rp = window->RPort;
    if (x) functions.clickvolume = (char) x;
    else functions.clickvolume = 1;
    Move(rp,gadget->LeftEdge + gadget->Width + 54,gadget->TopEdge + 7);
    SetAPen(rp,1);
    SetBPen(rp,0);
    SetDrMd(rp,JAM2);
    sprintf(text,"%ld      ",x);
    text[4] = 0;
    Text(rp,text,4);
    return((long)x);
}

static lengthroutine(window,gadget,x)

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

{
    char text[20];
    struct RastPort *rp = window->RPort;
    functions.countdown = x | (functions.countdown & (METRO_LEADINONLY |
      METRO_COUNTDOWN));
    Move(rp,gadget->LeftEdge + gadget->Width + 52,gadget->TopEdge + 7);
//    Move(rp,254,77);
    SetAPen(rp,1);
    SetBPen(rp,0);
    SetDrMd(rp,JAM2);
    sprintf(text,"%ld",x);
    Text(rp,text,1);
    return((long)x);
}

void setgadgettext(window,id,text)

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

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

static void refreshmetro(window)

struct Window *window;

{
    static char channel[4];
    static char *subdivide[7] = {"1/1","1/2","1/4","1/8","1/16","1/32","1/64"};
    struct Gadget *gadget = GetGadget(window,5);
    SelectEmbossed(window,1,functions.clicking);
    SelectEmbossed(window,2,functions.seeclick);
    SelectEmbossed(window,3,functions.midiclick & 0x80);
    ModifyEmbossedProp(window,4,functions.midiclick & 0x7F,0,0,0,0,0);
    DrawEmbossedProp(window,4);
    DrawEmbossed(window,5);
    if (functions.midiouttool) {
        drawtool(functions.midiouttool,window->RPort,
        gadget->LeftEdge + 13,gadget->TopEdge,10);
    }
    Itoa(functions.clickchannel+1,channel,10);
    setgadgettext(window,6,channel);
    setgadgettext(window,7,subdivide[functions.subdivide & 7]);
    SelectEmbossed(window,8,functions.subdivide & 0x80);
    SelectEmbossed(window,10,functions.countdown & METRO_LEADINONLY);
    SelectEmbossed(window,20,!(functions.countdown&METRO_COUNTDOWN));
    ModifyEmbossedProp(window,11,functions.countdown & 0x3F,0,0,0,0,0);
    DrawEmbossedProp(window,11);
    ModifyEmbossedProp(window,9,functions.clickvolume,0,0,0,0,0);
    DrawEmbossedProp(window,9);
}

static editmetro(message)

struct IntuiMessage *message;

{
    struct Window *window = message->IDCMPWindow;
    long class,code;
    short refresh = 0;
    struct Gadget *gadget;
    short dclick = doubleclick(message);
    class = message->Class;
    code = message->Code;
    gadget = (struct Gadget *) message->IAddress;
    ReplyMsg((struct Message *)message);
    class = SystemGadgets(window,class,gadget,code);
    if (class == REFRESHWINDOW) {
        BeginRefresh(window);
        for (code=1;code<11;code++) DrawEmbossed(window,code);
        refreshmetro(window);
        EndRefresh(window,1);
    }
    else if (class == MENUPICK) {
        mainmenu(window,class,code);
    }
    if (class == CLOSEWINDOW) {
        return(0);
    }
    else if (class == GADGETDOWN) {
        switch (gadget->GadgetID) {
            case 1 :
                functions.clicking = !functions.clicking;
                SelectEmbossed(window,1,functions.clicking);
                setclicksound();
                break;
            case 2 :
                functions.seeclick = !functions.seeclick;
                SelectEmbossed(window,2,functions.seeclick);
                if (!functions.seeclick) displayclick((char)-1);
                break;
            case 3 :
                if (functions.midiclick & 0x80) functions.midiclick &= 0x7F;
                else functions.midiclick |= 0x80;
                SelectEmbossed(window,3,functions.midiclick & 0x80);
                break;
            case 4 :
                DragEmbossedProp(window,4);
                refresh = 2;
                break;
            case 5 :
                if (functions.midiouttool) {
                    if (!dragtool(window,0,functions.midiouttool)) {
                        if (dclick) calledit(functions.midiouttool);
                    }
                }
                break;
            case 6 :
                functions.clickchannel = popupchannel(window,functions.clickchannel);
                refresh = 2;
                break;
            case 7 :
                code = popupnote(window,functions.subdivide & 7);
                functions.subdivide &= 0xF8;
                functions.subdivide |= code;
                refresh = 1;
                break;
            case 8 :
                if (functions.subdivide & 0x80) functions.subdivide &= 0x7F;
                else functions.subdivide |= 0x80;
                SelectEmbossed(window,8,functions.subdivide & 0x80);
                break;
            case 9 :
                DragEmbossedProp(window,9);
                break;
            case 10 :
                if (functions.countdown & METRO_LEADINONLY)
                  functions.countdown &= ~METRO_LEADINONLY;
                else functions.countdown |= METRO_LEADINONLY;
                SelectEmbossed(window,10,functions.countdown & METRO_LEADINONLY);
                break;
            case 11 :
                DragEmbossedProp(window,11);
                break;
            case 12 :
                ShiftEmbossedProp(window,11,-1,0);
                break;
            case 13 :
                ShiftEmbossedProp(window,11,1,0);
                break;
            case 14 :
                ShiftEmbossedProp(window,4,-1,0);
                refresh = 2;
                break;
            case 15 :
                ShiftEmbossedProp(window,4,1,0);
                refresh = 2;
                break;
            case 16 :
                ShiftEmbossedProp(window,9,-1,0);
                break;
            case 17 :
                ShiftEmbossedProp(window,9,1,0);
                break;
            case 20 : /* countdown on and off */
                if (functions.countdown & METRO_COUNTDOWN)
                  functions.countdown &= ~METRO_COUNTDOWN;
                else functions.countdown |= METRO_COUNTDOWN;
                SelectEmbossed(window,20,!(functions.countdown & METRO_COUNTDOWN));
                break;
        }
    }
    if (refresh & 2) {
        sendclick(1,functions.timenow,80);
        sendclick(0,functions.timenow + 24,0);
    }
    if (refresh) refreshmetro(window);
    return(1);
}

static struct Tool *toolroutine(window,newtool,mousex,mousey)

struct Window *window;
struct Tool *newtool;
short mousex,mousey;

{
    if (newtool->tooltype & TOOL_OUTPUT) {
        ActivateWindow(window);
        if (functions.midiouttool) {
            deletetool(functions.midiouttool);
        }
        functions.midiouttool = newtool;
        refreshmetro(window);
        return(newtool);
    }
    deletetool(newtool);
    return(0);
}

static void openmetro(window)

struct Window *window;

{
    static char modes[] = {1,1,1,0,0,0,0,1,0,1};
    short i;
    FatEmbossedPropOn(window,11,12,13,(long_cast)lengthroutine,10,1);
    FatEmbossedPropOn(window,4,14,15,(long_cast)rootroutine,128,1);
    FatEmbossedPropOn(window,9,16,17,(long_cast)loudroutine,128,1);
    for (i=0;i<10;i++) if (i != 3) EmbossOn(window,i+1,modes[i]);
    EmbossOn(window,20,1);
    refreshmetro(window);
    addtoolserver(toolroutine,window);
    linkobject(window,&functions.countdown,refreshmetro);
}

static void closemetro(window)

struct Window *window;

{
    short i;
    for (i=1;i<11;i++) if (i != 3) EmbossOff(window,i);
    EmbossOff(window,20);
    FatEmbossedPropOff(window,11,12,13);
    FatEmbossedPropOff(window,4,14,15);
    FatEmbossedPropOff(window,9,16,17);
    removetoolserver(window);
    unlinkwindow(window);
}

static toolremoveroutine(struct ToolMaster *toolmaster,char remove)

{
    struct Tool *tool;
    tool = functions.midiouttool;
    if (tool && (tool->toolid == toolmaster->toolid)) {
        if (remove) {
            functions.midiouttool = 0;
            deletetool(tool);
        }
        else return(1);
    }
    return(0);
}

void startmetro()

{
    addtoolremover((long_cast)toolremoveroutine);
}

void installmetrowindow()

{
    installwindow(WINDOW_METRO,&metroNewWindowStructure1,1,
        &vMenu1,(void_cast)openmetro,(long_cast)editmetro,
        (void_cast)closemetro,0,30000,30000,0,0);
}

