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

extern struct Functions functions;
FARCALL
extern struct Menu vMenu1;

#include "tpw.c"

static void refreshtp(window)

struct Window *window;

{
    unsigned short index;
    for (index=0;index<4;index++) 
        SetStringInfoNumber(window,index+5,functions.tempos[index],10);
}

static void fillstring(window,id)

struct Window *window;
unsigned short id;

{
    unsigned short tempo;
    struct StringInfo *stringinfo = 
        (struct StringInfo *) GetStringInfo(window,id);
    tempo = stringinfo->LongInt;
    if (tempo < MINTEMPO) tempo = MINTEMPO;
    if (tempo > MAXTEMPO) tempo = MAXTEMPO;
    functions.tempos[id-5] = tempo;
}

static edittp(message)

struct IntuiMessage *message;

{
    struct Window *window = message->IDCMPWindow;
    long class,code;
    long change;
    struct Gadget *gadget;
    static unsigned short stringid = 0;
    class = message->Class;
    code = message->Code;
    gadget = (struct Gadget *) message->IAddress;
    ReplyMsg((struct Message *)message);
    class = SystemGadgets(window,class,gadget,code);
    if (class == MENUPICK) {
        mainmenu(window,class,code);
    }
    else if (class == REFRESHWINDOW) {
        BeginRefresh(window);
        EndRefresh(window,1);
    }
    if (stringid) {
        if ((class == GADGETDOWN) || (class == CLOSEWINDOW) || 
                (class == INACTIVEWINDOW)) {
            fillstring(window,stringid);
            stringid = 0;
        }
    }
    if (class == CLOSEWINDOW) {
        stringid = 0;
        return(0);      
    }
    else if (class == GADGETDOWN) {
        stringid = gadget->GadgetID;
        if (stringid < 5) {
            change = functions.tempos[stringid - 1];
            if (change < functions.tempo) {
                change = functions.tempo - change;
                change = -change;
            }
            else change = change - functions.tempo;
            changetempo(change);
            stringid = 0;
        }
    }
    else if (class == GADGETUP) {
        if (gadget->GadgetID > 4) {
            fillstring(window,gadget->GadgetID);
            refreshtp(window);
        }
    }
    return(1);
}

static void opentp(window)

struct Window *window;

{
    static char modes[] = { 1,1,1,1,3,3,3,3 };
    short i;
    for (i=0;i<8;i++) EmbossOn(window,i+1,modes[i]);
    refreshtp(window);
}

static void closetp(window)

struct Window *window;

{
    short i;
    for (i=1;i<9;i++) EmbossOff(window,i);
}

void installtpwindow()

{
#ifdef FRENCH
    tpNewWindowStructure1.Title = "Palette Tempo";
#endif
    installwindow(WINDOW_TP,&tpNewWindowStructure1,1,&vMenu1,
        (void_cast)opentp,(long_cast)edittp,(void_cast)closetp,0,-1,-1,0,0);
}
