home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / dirs / setcolors_419.lzh / SetColors / SetColors.c < prev    next >
C/C++ Source or Header  |  1990-12-18  |  6KB  |  243 lines

  1. /*
  2.  *    SetColors.c - Copyright © 1990 by Devil's child.
  3.  *
  4.  *    Created:    23 Nov 1990  14:15:11
  5.  *    Modified:    28 Nov 1990  17:57:19
  6.  *
  7.  *    Make>> cc -qf -ps -sb -so -wu -wd -wp -hi ram:small.pre <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.         ArpExit(20, 0);
  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.         ArpExit(20, 0);
  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.             ID = ((struct Gadget *)Message->IAddress)->GadgetID;
  195.             ReplyMsg((struct Message *) Message);
  196.             switch (Class) {
  197.             case CLOSEWINDOW:
  198.                 quit = TRUE;
  199.                 break;
  200.             case GADGETUP:
  201.                 switch(ID) {
  202.                 case 0:
  203.                     if (ColorRequester(1) != 0xFFFF)
  204.                         SetPref();
  205.                     break;
  206.                 case 1:
  207.                     FileReq.Title = "Load Colors";
  208.                     FileReq.Flags = FRQCACHINGM|FRQLOADINGM;
  209.                     if (FileRequester(&FileReq))
  210.                         LoadColors(Path);
  211.                     break;
  212.                 case 2:
  213.                     FileReq.Title = "Save Colors";
  214.                     FileReq.Flags = FRQCACHINGM|FRQSAVINGM;
  215.                     if (FileRequester(&FileReq))
  216.                         SaveColors(Path);
  217.                     break;
  218.                 case 3:
  219.                     if (TwoGadRequest("Colors Request", "Really update system-configuration ?"))
  220.                         SavePref();
  221.                     break;
  222.                 }
  223.                 break;
  224.             }
  225.         }
  226.     }
  227.     PurgeFiles(&FileReq);
  228.     MyProcess->pr_WindowPtr = OldWindowPtr;
  229.     CloseWindowSafely(Win, NULL);
  230. }
  231.  
  232.  
  233. void _cli_parse(struct Process *pp, long alen, char *aptr)
  234. {
  235.     _argv = ArpAlloc(12);
  236.     _argc = (int)GADS(aptr, alen, "Usage: SetColors [ColorFile] [DELAY secs]", _argv, "ColorFile,DELAY/k");
  237.     if (_argc < 0) {
  238.         Puts(_argv[0]);
  239.         ArpExit(20L, ERROR_LINE_TOO_LONG);
  240.     }
  241. }
  242.  
  243.