
/*****************************************************************************/

/*  Module PCMU.C

    PANEL ColorMap Utility for Amiga

    Modification History (Push-Down List):  

            30.11.85        : Distribution Source V 6.20

    Copyright 1985 Roundhill Computer Systems Limited
    All rights reserved

    Use and disclosure of this code are prohibited unless in accordance
    with the terms of the PANEL software licence.

******************************************************************************/

#include <pnli.h>
#include <pnlc.h>
#include <exec/types.h>
#include <intuition/intuition.h>
#include <graphics/text.h>
#include <lattice/fcntl.h>

extern int PAMODE;                  /* panel mode flags */
extern int PAIATT[];                /* attribute array */
extern int (*PAIDCM)();             /* extra IDCMP function processor */
extern struct Screen *PASCRN;       /* Panel's standard screen */
extern struct Window *PAWIND;       /* Panel's standard window */

int local [8] [3];                  /* working copy of colormap */
int save  [8] [3];                  /* saved actual colormap */            
int back  [8] [3];                  /* previous pnlcolors contents */
int nread = 0;                      /* number of pnlcolors lines read */
char buffer[20];                    /* work buffer for formatted output */
int i, j, h, w;
int t, n;
int mx,my;
int quit = 0;                       /* switch to indicated finished */


/*****************************************************************************/

void disp(x,y,c) 

/*  print value local[y][x] in a suitable place.  inverse if c == 1 

******************************************************************************/

int x,y,c;

{
    if ( y == -1) return;
    if (c)
        {
        PAIATT[0] = 0;
        PAIATT[1] = 1;
        }
      else
        {
        PAIATT[0] = 1;
        PAIATT[1] = 0;
        }
    PAHION(0);
    PASBAD ( (x*3)+49, (y*2)+2 ), 
    sprintf(buffer,"%2d", local[y][x]);
    PASOUT(buffer);
}


/*****************************************************************************/

void doplus()

/*  increment RGB value and reset colors 

******************************************************************************/

{
    if ( j == -1) return;
    if (local[j][i] == 15) return;
    local[j][i] ++;
    disp(i,j,0);
    SetRGB4(&PASCRN->ViewPort,j,local[j][0],local[j][1],local[j][2]);
}


/*****************************************************************************/

void dominus()

/*  decrement RGB value and reset colors 

******************************************************************************/

{
    if ( j == -1) return;
    if (local[j][i] == 0) return;
    local[j][i] --;
    disp(i,j,0);
    SetRGB4(&PASCRN->ViewPort,j,local[j][0],local[j][1],local[j][2]);
}


/*****************************************************************************/

domouse(Message)

/*  process mousebuttons. called while waiting for any input 

******************************************************************************/

struct IntuiMessage *Message; 
           
{
    if (Message->Class == MOUSEBUTTONS)
        {
        mx = (Message->MouseX - 3)/w;
        my = (Message->MouseY - 11)/h;
        if (Message->Code == SELECTDOWN)
            {
            if ( (my == 17) && (mx >= 33) && (mx <= 36) )
                doplus();
            else if ( (my == 17) && (mx >= 38) && (mx <= 41) )
                dominus();
            else if ( (my & 1) && (my < 16) && (mx >= 49) && (mx <= 50) )
                { j = (my-1) / 2; i = 0; disp(i,j,1); }
            else if ( (my & 1) && (my < 16) && (mx >= 52) && (mx <= 53) )
                { j = (my-1) / 2; i = 1; disp(i,j,1); }
            else if ( (my & 1) && (my < 16) && (mx >= 55) && (mx <= 56) )
                { j = (my-1) / 2; i = 2; disp(i,j,1); }
            else if ( (my == 17) && (mx >= 43) && (mx <= 48) )
                { quit = 1; }
            else if ( (my == 17) && (mx >= 51) && (mx <= 56) )
                { quit = 2; }
            }
          else if (Message->Code == SELECTUP)
            {
            disp(i,j,0);
            }
        }
}


/*****************************************************************************/

void doread()

/*  read PANEL:pnlcolors file

******************************************************************************/

{
    j = 0;
    printf("Reading PANEL:pnlcolors . . .");
    t = open("PANEL:pnlcolors",O_RDONLY);
    if ( t > 0 )
        {
        while ( ( ( n = read(t,buffer,10) ) == 10) && ( j < 8 ) )
            {
            buffer[9] = '\0';   /* zap nl */ 
            sscanf(buffer,"%d %d %d",&back[j][0], &back[j][1], &back[j][2]);
            nread = ++j;
            }
        close (t);
        if (nread == 8) printf(" done\n");
        else printf(" incomplete\n");
        }
      else
        {
        printf(" (not found)\n");
        }
}


/*****************************************************************************/

void dowrite()

/*  write PANEL:pnlcolors file

******************************************************************************/

{
    printf("Writing PANEL:pnlcolors . . .");
    t = open("PANEL:pnlcolors",O_WRONLY|O_CREAT|O_TRUNC);
    if ( t > 0 )
        {
        for (j = 0; j < 8; j++)
            {
            sprintf(buffer,"%3d%3d%3d\n",
                 local[j][0],local[j][1],local[j][2]);
            n = write(t,buffer,10);
            if (n != 10) { printf(" (write error)\n"); break;}
            }
        printf(" done\n");
        close(t);
        }
}


/*****************************************************************************/

void doback()

/*  write PANEL:pnlcolors.bak file

******************************************************************************/

{
    printf("Writing PANEL:pnlcolors.bak .");
    t = open("PANEL:pnlcolors.bak",O_WRONLY|O_CREAT|O_TRUNC);
    if ( t > 0 )
        {
        for (j = 0; j < nread; j++)
            {
            sprintf(buffer,"%3d%3d%3d\n",
                 back[j][0],back[j][1],back[j][2]);
            n = write(t,buffer,10);
            if (n != 10) { printf(" (write error)\n"); break;}
            }
        close(t);
        printf(" done\n");
        }
}


/*****************************************************************************/

main()

/* main process

******************************************************************************/

{
    doread();                   /* read PANEL:pnlcolors file */

    PAMODE = PANELSIXTY;        /* force use of sixty-column width */

    PAOSINIT();                 /* do OS initialisation */

    PAINIT("");                 /* do screen / keyboard init */

    PAIATT[0] = 1;              /* force PAIATT in case user has paiatt file */
    PAIATT[1] = 0;
    PAHION(0);
    PAFUNC(1);                  /* init, cls */

    PAIDCM = domouse;           /* call above routine for all input */

    ModifyIDCMP(PAWIND,(PAWIND->IDCMPFlags) | MOUSEBUTTONS);  /* report mouse */

    h = PASCRN->Font->ta_YSize;     /* will always be sixty, so h = 9 */
    w = (h == 9) ? 10 : 8;          /* and w = 10 */

    for (j = 0; j < 8; j++)         /* save real amiga colors */
        {
        save[j][0] = (GetRGB4(PASCRN->ViewPort.ColorMap,j) >> 8) & 0xf;
        save[j][1] = (GetRGB4(PASCRN->ViewPort.ColorMap,j) >> 4) & 0xf;
        save[j][2] = (GetRGB4(PASCRN->ViewPort.ColorMap,j)     ) & 0xf;
        }

    for (j = 0; j < nread; j++)     /* override amiga colors with pnlcolors */
        {
        local[j][0] = back[j][0];
        local[j][1] = back[j][1];
        local[j][2] = back[j][2];
        }

    for (j = nread; j < 8; j++)     /* use amiga colors if no input */
        {
        local[j][0] = save[j][0];
        local[j][1] = save[j][1];
        local[j][2] = save[j][2];
        }

    for (j = 0; j < 8; j++)         /* set selected colors */
        {
        SetRGB4(&PASCRN->ViewPort,j,local[j][0],local[j][1],local[j][2]);
        }

    PAIATT[0] = 0;                  /* display headings */
    PAIATT[1] = 1;
    PAHION(0);
    PASBAD(  3, 0 );
    PASOUT("PANEL® ColorMap Utility V1.00               ");
    PAIATT[0] = 1;
    PAIATT[1] = 0;
    PAHION(0);
    PASBAD( 50, 0 );
    PASOUT("R");
    PASBAD( 53, 0 );
    PASOUT("G");
    PASBAD( 56, 0 );
    PASOUT("B");

    for (j = 0; j < 8; j++)         /* display screen for adjustment */
        {
        for (i = 0; i < 8; i++)
            {
            PAIATT[0] = i;
            PAIATT[1] = j;
            PAHION(0);
            PASBAD( (i*5)+3, (j*2)+2 );
            sprintf(buffer," %1d%1d ",i,j);
            PASOUT(buffer);
            }

        PAIATT[0] = 1;
        PAIATT[1] = j;
        PAHION(0);
        PASBAD( 43, (j*2)+2 );
        PASOUT("    ");

        disp(0,j,0);
        disp(1,j,0);
        disp(2,j,0);
        }

    PAIATT[0] = 0;                  /* display footings */
    PAIATT[1] = 1;
    PAHION(0);
    PASBAD( 3, 18 );
    PASOUT("Select RGB #, then ++ or --  ");
    PASBAD( 33, 18 );
    PASOUT(" ++ ");
    PASBAD( 38, 18 );
    PASOUT(" -- ");
    PASBAD( 43, 18 );
    PASOUT(" SAVE ");
    PASBAD( 51, 18 );
    PASOUT("CANCEL");

    i = j = -1;
    mx = my = -1;
    while (1)                       /* loop for ever */
        {
        if (PACSTA()) PACINP();     /* gobble any input */
        if (quit)                   /* set in domouse if finished */
            {
            PAFUNC(3);              /* cls */

            for (j = 0; j < 8; j++) /* reset real Amiga colors */
                SetRGB4(&PASCRN->ViewPort,j,save[j][0],save[j][1],save[j][2]);

            PAOSTERM();             /* close the window and screen */

            if (quit == 1)
                {
                dowrite();          /* write PANEL:pnlcolors file */
                doback();           /* write PANEL:pnlcolors.bak file */
                }
            exit(0);
            }
        }                            
}
