/* Switch Colors V2.0
   by
°ª°Guido Wegener
   Mar. 1992
*/

#include <functions.h>
#include <exec/types.h>
#include <pragmas.h>
#include <intuition/intuition.h>
#include <graphics/gfxbase.h>
#include <graphics/view.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <intuition/preferences.h>

struct IntuitionBase *IntuitionBase=NULL;
struct GfxBase *GfxBase=NULL;
struct Preferences *prefs=NULL;

struct IntuiText itexts[3]=
 {
  {1,0,JAM1,9,2,NULL,(UBYTE *)"WB 1.x",NULL},
  {1,0,JAM1,9,2,NULL,(UBYTE *)"WB 2.0",NULL},
  {1,0,JAM1,20,2,NULL,(UBYTE *)"OLD",NULL}
 };

struct IntuiText xtex={1,0,JAM2,2,2,NULL,(UBYTE *)"Select Palette            ",NULL};

char texte[][27]=
 {
  "This is no gadget, sucker!",
  "The gadgets are down there",
  "V V V V V V V V V V V V V ",
  "How did you find this     ",
  "hidden gadget?            ",
  "I thought that nobody will",
  "ever be so stupid to click",
  "a line of text!!!         ",
  "Since there was no room   ",
  "for the credits in the    ",
  "titlebar, I will have to  ",
  "place them here:          ",
  "This is Version 2.0 of    ",
  "SwitchColors.             ",
  "Everything was done by    ",
  "Guido Wegener.            ",
  "Martin Hammers gave me the",
  "RGBs for WB2.0 and helped ",
  "me to get the glorious    ",
  "idea to write this tool by",
  "talking about something   ",
  "completely different.     ",
  "Alexander Carbin's        ",
  "problems with the colors  ",
  "of ZeigE showed me a clean",
  "way to get and change the ",
  "colors of the WorkBench   ",
  "and related screens.      ",
  "This little helper is     ",
  "Freeware. It may be copied",
  "as long as no profit is   ",
  "made.                     ",
  "If you want to contact me ",
  "(legal), then write to    ",
  "Guido Wegener             ",
  "Eisenacher Str. 2         ",
  "W-5300 Bonn 1             ",
  "West-Germany              ",
  "I really hope that you    ",
  "like this piece of art!   ",
  "I think that it is quite  ",
  "usefull when rummaging    ",
  "old and new Fish-Discs or ",
  "to have a brief glimpse of",
  "the real look of a given  ",
  "icon.                     ",
  "Now I will have to restart",
  "the text.                 ",
  "BYE!                      "
 };
int act=-1;

SHORT cors[]=
 {
  0,0,
  66,0,
  66,12,
  0,12,
  0,0
 };

struct Border mybord=
 {
  -1,-1,
  1,0,JAM1,
  5,
  cors,
  NULL
 };

struct Gadget xgad=
 {
  NULL,
  7,11,
  213,11,
  GADGHNONE,
  RELVERIFY,
  BOOLGADGET,
  NULL,
  NULL,
  NULL,
  0,
  NULL,
  10,
  NULL
 };

struct Gadget gad3=
 {
  &xgad,
  154,24,
  65,11,
  GADGHCOMP,
  RELVERIFY,
  BOOLGADGET,
  (APTR)&mybord,
  NULL,
  &(itexts[2]),
  0,
  NULL,
  3,
  NULL
 };

struct Gadget gad2=
 {
  &gad3,
  81,24,
  65,11,
  GADGHCOMP,
  RELVERIFY,
  BOOLGADGET,
  (APTR)&mybord,
  NULL,
  &(itexts[1]),
  0,
  NULL,
  2,
  NULL
 };

struct Gadget gad1=
 {
  &gad2,
  8,24,
  65,11,
  GADGHCOMP,
  RELVERIFY,
  BOOLGADGET,
  (APTR)&mybord,
  NULL,
  &(itexts[0]),
  0,
  NULL,
  1,
  NULL
 };

struct Window *MyWindow=NULL;
struct NewWindow MyNewWindow=
 {
  402,10,
  228,40,
  -1,-1,
  GADGETUP | CLOSEWINDOW,
  WINDOWDRAG | WINDOWCLOSE | WINDOWDEPTH,
  &gad1,
  NULL,
  (UBYTE *)"SwitchColors by GW",
  NULL,
  NULL,
  0,0,0,0,
  WBENCHSCREEN
 };

UBYTE pal[3*4*3]=
 {
  0,5,10,
  15,15,15,
  0,0,2,
  15,8,0,
  10,10,10,
  0,0,1,
  15,15,15,
  5,7,10
 };
struct ViewPort *vp;

void ope(void);
void clo(char *text);
void gadup(USHORT id);
void cpal(int n);

void main()
 {
  struct IntuiMessage *msg;
  BOOL cont;

  ope();
  
  cont=TRUE;
  while(cont)
   {
    Wait(1L<<MyWindow->UserPort->mp_SigBit);
    while(msg=(struct IntuiMessage *)GetMsg(MyWindow->UserPort))
     {
      switch(msg->Class)
       {
        case GADGETUP: gadup(((struct Gadget *)(msg->IAddress))->GadgetID);break;
        case CLOSEWINDOW: cont=FALSE;break;
        default: puts("Unknown Message type!");break;
       }
      ReplyMsg((struct Message *)msg);
     }
   }
  
  clo(NULL);
 }

void gadup(USHORT id)
 {
  switch(id)
   {
    case 1: cpal(1);break;
    case 2: cpal(2);break;
    case 3: cpal(3);break;
    case 10: act++;
             if(act>=49) act=8;
             strcpy((char *)xtex.IText,texte[act]);
             PrintIText(MyWindow->RPort,&xtex,7L,11L);
             break;
   }
 }

void cpal(int n)
 {
  int i,pn;
  USHORT *co;

  GetPrefs(prefs,sizeof(struct Preferences));
  co=&(prefs->color0);
  for(i=0;i<4;i++)
   {
    pn=(n-1)*12+i*3;
    co[i]=pal[pn]*256+pal[pn+1]*16+pal[pn+2];
   }
  SetPrefs(prefs,sizeof(struct Preferences),TRUE);
 }

void ope()
 {
  UWORD coded;
  int i;
  USHORT *co;

  IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",0L);
  if(!IntuitionBase) clo("Can't open Intuition!");
  GfxBase=(struct GfxBase *)OpenLibrary("graphics.library",0L);
  if(!GfxBase) clo("Can't open Graphics!");
  MyWindow=OpenWindow(&MyNewWindow);
  if(!MyWindow) clo("Can't open Window!");
  PrintIText(MyWindow->RPort,&xtex,7L,11L);
  prefs=AllocMem(sizeof(struct Preferences),0L);
  if(!prefs) clo("No Memory for Preferences!");

  GetPrefs(prefs,sizeof(struct Preferences));
  co=&(prefs->color0);
  for(i=0;i<4;i++)
   {
    coded=co[i];
    pal[24+i*3]=(coded& 0x0f00 )/ 0x0100;
    pal[25+i*3]=(coded& 0x00f0 )/ 0x0010;
    pal[26+i*3]=(coded& 0x000f );
   }
 }

void clo(char *text)
 {
  if(text) puts(text);
  if(prefs) FreeMem(prefs,sizeof(struct Preferences));
  if(MyWindow) CloseWindow(MyWindow);
  if(GfxBase) CloseLibrary(GfxBase);
  if(IntuitionBase) CloseLibrary(IntuitionBase);
  exit(text?20:0);
 }

