home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d5xx / d540 / setcolors.lha / SetColors / SetColors.c < prev    next >
C/C++ Source or Header  |  1991-08-27  |  6KB  |  244 lines

  1. /*
  2.  *    SetColors.c - Copyright © 1990 by Devil's child.
  3.  *
  4.  *    Created:    23 Nov 1990  14:15:11
  5.  *    Modified:    07 Feb 1991  21:42:33
  6.  *
  7.  *    Make>> cc -qf -ps -sb -so -wu -wd -wp -hi ram:small.sym <file>.c
  8.  *    Make>> ln -m <file>.o -larps -lreq
  9.  */
  10.  
  11. #include <dos_functions.h>
  12. #include <arpdos_pragmas.h>
  13.  
  14. #define NUMGADGETS 4
  15.  
  16. extern int _argc;
  17. extern char **_argv;
  18. extern struct WBStartup *WBenchMsg;
  19. extern struct ExecBase *SysBase;
  20. extern struct ArpBase *ArpBase;
  21. struct ReqLib *ReqBase;
  22. struct IntuitionBase *IntuitionBase;
  23. struct GfxBase *GfxBase;
  24. struct Window *Win;
  25. struct Screen *S;
  26. struct ColorMap *ColorMap;
  27. struct Preferences Prefs;
  28. struct FileReq FileReq;
  29. char Dir[REQ_DSIZE+1];
  30. char File[REQ_FCHARS+1];
  31. char Path[REQ_DSIZE+REQ_FCHARS+2];
  32. struct GadgetBlock GadgetArray[NUMGADGETS];
  33. BOOL WorkBench;
  34.  
  35. char *GadgetText[] = {
  36.     " Palette ",
  37.     "Load File",
  38.     "Save File",
  39.     "Save Pref"
  40. };
  41.  
  42. struct NewWindow NWS = {
  43.     0, 0,                    /* LeftEdge, TopEdge */
  44.     87, 69,                    /* Width, Height */
  45.     3, 2,                    /* DetailPen, BlockPen */
  46.     GADGETUP|CLOSEWINDOW,    /* IDCMPFlags */
  47.     SMART_REFRESH|NOCAREREFRESH|ACTIVATE|WINDOWCLOSE|WINDOWDRAG,        /* Flags */
  48.     NULL,                    /* FirstGadget */
  49.     NULL,                    /* CheckMark */
  50.     (UBYTE *)"Colors ",        /* Title */
  51.     NULL,                    /* Screen */
  52.     NULL,                    /* BitMap */
  53.     0, 0,                    /* MinWidth, MinHeight */
  54.     -1, -1,                    /* MaxWidth, MaxHeight */
  55.     CUSTOMSCREEN            /* Type (of screen) */
  56. };
  57.  
  58.  
  59. void SetPref(void)
  60. {
  61.     USHORT *ColorPtr;
  62.     short i;
  63.  
  64.     if (WorkBench) {
  65.         GetPrefs(&Prefs, 232);
  66.         ColorPtr = &Prefs.color0;
  67.         for( i=0 ; i<4 ; i++ )
  68.             *ColorPtr++ = GetRGB4(ColorMap, i);
  69.         SetPrefs(&Prefs, 232, TRUE);
  70.     }
  71. }
  72.  
  73.  
  74. void LoadColors(char *file)
  75. {
  76.     BPTR fh;
  77.     USHORT RGB[32];     /* 32 color registers max */
  78.     short nc, count;
  79.  
  80.     if (fh = Open(file, MODE_OLDFILE)) {
  81.         nc = Read(fh, (char *)RGB, 2 << S->BitMap.Depth);
  82.         Close(fh);
  83.     }
  84.     else {
  85.         SimpleRequest("Colors Request", "Couldn't open '%s'", file);
  86.         return;
  87.     }
  88.     if (nc < 0) {
  89.         SimpleRequest("Colors Request", "Error reading '%s'\nError code %ld", file, IoErr());
  90.         return;
  91.     }
  92.     if (nc > 0)    /* less than 2^depth colors have been read */
  93.         count = nc >> 1;
  94.     else
  95.         count = 1 << S->BitMap.Depth;
  96.     LoadRGB4(&S->ViewPort, RGB, count);
  97.     SetPref();
  98. }
  99.  
  100.  
  101. void SaveColors(char *file)
  102. {
  103.     BPTR fh;
  104.     USHORT RGB[32];     /* 32 color registers max */
  105.     short i;
  106.  
  107.     for( i=0 ; i < (1 << S->BitMap.Depth) ; i++ )
  108.         RGB[i] = GetRGB4(ColorMap, i);
  109.     if (fh = Open(file, MODE_NEWFILE)) {
  110.         Write(fh, (char *)RGB, 2 << S->BitMap.Depth);
  111.         Close(fh);
  112.     }
  113.     else
  114.         SimpleRequest("Colors Request", "Couldn't open '%s'", file);
  115. }
  116.  
  117.  
  118. void SavePref(void)
  119. {
  120.     BPTR fh;
  121.  
  122.     GetPrefs(&Prefs, 232);
  123.     if (fh = Open("devs:system-configuration", MODE_NEWFILE)) {
  124.         Write(fh, (char *)&Prefs, 232);
  125.         Close(fh);
  126.     }
  127.     else
  128.         SimpleRequest("Colors Request", "Couldn't open 'devs:system-configuration'");
  129. }
  130.  
  131.  
  132. void main(short argc, char *argv[])
  133. {
  134.     struct Process *MyProcess;
  135.     APTR OldWindowPtr;
  136.     struct IntuiMessage *Message;
  137.     ULONG Class;
  138.     SHORT ID, y, i;
  139.     BOOL quit = FALSE;
  140.     char file[256];
  141.  
  142.     if (!(ReqBase = (struct ReqLib *)ArpOpenLibrary("req.library", 1L))) {
  143.         if (!WBenchMsg) Puts("No req.library!");
  144.         exit(20);
  145.     }
  146.     GfxBase = (struct GfxBase *)ArpBase->GfxBase;
  147.     IntuitionBase = (struct IntuitionBase *)ArpBase->IntuiBase;
  148.     if (argc>0 && argv[1])
  149.         Delay(LMult(Atol(argv[1]),50));
  150.     NWS.Screen = S = IntuitionBase->ActiveScreen;
  151.     ColorMap = S->ViewPort.ColorMap;
  152.     WorkBench = (S->Flags & 0x000E) ? FALSE : TRUE;
  153.  
  154.     for( i=0, y=14 ; i < NUMGADGETS ; i++ ) {
  155.         LinkGadget(&GadgetArray[i], GadgetText[i], &NWS, 8, y);
  156.         GadgetArray[i].Gadget.GadgetID = i;
  157.         y += 14;
  158.     }
  159.     if (!WorkBench)
  160.         GadgetArray[3].Gadget.Flags |= GADGDISABLED;
  161.     Center(&NWS, 0, 0);
  162.  
  163.     if (argc>0 && argv[0]) {
  164.         if (BaseName(argv[0]) == argv[0])
  165.             SPrintf(file, "devs:%s.clr", argv[0]);
  166.         else
  167.             SPrintf(file, "%s.clr", argv[0]);
  168.         LoadColors(file);
  169.         return;
  170.     }
  171.  
  172.     if (!(Win = OpenWindow(&NWS)))
  173.         exit(20);
  174.     MyProcess = (struct Process *)SysBase->ThisTask;
  175.     OldWindowPtr = MyProcess->pr_WindowPtr;
  176.     MyProcess->pr_WindowPtr = (APTR)Win;
  177.  
  178.     FileReq.Dir = Dir;
  179.     FileReq.File = File;
  180.     FileReq.PathName = Path;
  181.     FileReq.dirnamescolor = 3;
  182.     FileReq.devicenamescolor = 3;
  183.     FileReq.stringnamecolor = 2;
  184.     FileReq.stringgadgetcolor = 2;
  185.     FileReq.boxbordercolor = 2;
  186.     FileReq.gadgetboxcolor = 2;
  187.     strcpy(FileReq.Show, "*.clr");
  188.     strcpy(Dir, "devs:");
  189.  
  190.     while (!quit) {
  191.         WaitPort(Win->UserPort);
  192.         while (Message = (struct IntuiMessage *)GetMsg(Win->UserPort)) {
  193.             Class = Message->Class;
  194.             if (Class == GADGETUP)    /* otherwise, address errors may occur */
  195.                 ID = ((struct Gadget *)Message->IAddress)->GadgetID;
  196.             ReplyMsg((struct Message *) Message);
  197.             switch (Class) {
  198.             case CLOSEWINDOW:
  199.                 quit = TRUE;
  200.                 break;
  201.             case GADGETUP:
  202.                 switch(ID) {
  203.                 case 0:
  204.                     if (ColorRequester(1) != 0xFFFF)
  205.                         SetPref();
  206.                     break;
  207.                 case 1:
  208.                     FileReq.Title = "Load Colors";
  209.                     FileReq.Flags = FRQCACHINGM|FRQLOADINGM;
  210.                     if (FileRequester(&FileReq))
  211.                         LoadColors(Path);
  212.                     break;
  213.                 case 2:
  214.                     FileReq.Title = "Save Colors";
  215.                     FileReq.Flags = FRQCACHINGM|FRQSAVINGM;
  216.                     if (FileRequester(&FileReq))
  217.                         SaveColors(Path);
  218.                     break;
  219.                 case 3:
  220.                     if (TwoGadRequest("Colors Request", "Really update system-configuration ?"))
  221.                         SavePref();
  222.                     break;
  223.                 }
  224.                 break;
  225.             }
  226.         }
  227.     }
  228.     PurgeFiles(&FileReq);
  229.     MyProcess->pr_WindowPtr = OldWindowPtr;
  230.     CloseWindowSafely(Win, NULL);
  231. }
  232.  
  233.  
  234. void _cli_parse(struct Process *pp, long alen, char *aptr)
  235. {
  236.     _argv = ArpAlloc(12);
  237.     _argc = (int)GADS(aptr, alen, "Usage: SetColors [ColorFile] [DELAY secs]", _argv, "ColorFile,DELAY/k");
  238.     if (_argc < 0) {
  239.         Puts(_argv[0]);
  240.         ArpExit(20L, ERROR_LINE_TOO_LONG);
  241.     }
  242. }
  243.  
  244.