home *** CD-ROM | disk | FTP | other *** search
/ 17 Bit Software 1: Collection A / 17Bit_Collection_A.iso / files / 1112.dms / 1112.adf / PasteGadgets / PasteGadgets.c < prev    next >
C/C++ Source or Header  |  1988-07-25  |  7KB  |  268 lines

  1. /*
  2.    PasteGadgets - code for a utility that loads or saves system-gadget
  3.                   images.
  4.    (c) 1990  Olaf Leimann
  5.              Dorpsstraat 77
  6.   NL-2211 GD Noordwijkerhout
  7. */
  8.  
  9. #include <exec/types.h>
  10. #include <exec/memory.h>
  11. #define  INTUITIONPRIVATE 1
  12. #include <intuition/intuitionbase.h>
  13. #include <workbench/startup.h>
  14. #include <proto/exec.h>
  15. #include <proto/dos.h>
  16. #include <proto/graphics.h>
  17. #include <proto/intuition.h>
  18. #include <string.h>
  19. #include <stdio.h>
  20.  
  21. struct IntuitionBase *IntuitionBase ;
  22.  
  23. extern void LoadImages(struct Window *,char *);
  24. extern void SaveImages(struct Window *,char *);
  25.  
  26. struct NewWindow GWindow = {
  27.    0,11,100,50,-1,-1,0L,
  28.    WINDOWCLOSE|WINDOWDRAG|WINDOWDEPTH|WINDOWSIZING|NOCAREREFRESH,
  29.    NULL,NULL,(UBYTE *)"HI!",NULL,NULL,
  30.    100,50,640,256,WBENCHSCREEN
  31. };
  32.  
  33. #define NUMGADGETS  6
  34.  
  35. char *ImageIDs[NUMGADGETS] = {
  36.    "CLSE","BACK","FRNT","SIZE","SBCK","SFNT"
  37. };
  38. USHORT ImageBits[NUMGADGETS] = {
  39.    CLOSE, WDOWNBACK, WUPFRONT, SIZING, SDOWNBACK, SUPFRONT
  40. };
  41.  
  42. void main(argc,argv)
  43.    int   argc ;
  44.    char *argv[] ;
  45. /*#FOLD:*/
  46. {
  47.    struct Window *win ;
  48.    int i,show=0 ;
  49.    char *s ;
  50.  
  51.    if(argc > 1){
  52.       if(strcmp(argv[1],"?")==0) show = 1 ;
  53.       IntuitionBase =(struct IntuitionBase *)OpenLibrary(
  54.          "intuition.library",0L);
  55.       if(IntuitionBase){
  56.          win = OpenWindow(&GWindow);
  57.          if(win){
  58.             for(i=1;i<argc;i++){
  59.                s = argv[i] ;
  60.                if(stricmp(s,"LOAD")==0 && i<argc-1)
  61.                   LoadImages(win,argv[++i]);
  62.                else if(stricmp(s,"SAVE")==0 && i<argc-1)
  63.                   SaveImages(win,argv[++i]);
  64.                else
  65.                   LoadImages(win,argv[i]);
  66.             }
  67.             CloseWindow(win);
  68.          }
  69.          CloseLibrary((struct Library *)IntuitionBase);
  70.       }
  71.    }
  72.    else show = 1 ;
  73.    if(show){
  74.       printf("PasteGadgets (c) 1990  Olaf Leimann\n");
  75.       printf("Loads or saves a set of systemgadgets.\n");
  76.       printf("Gadgets supported are:\n");
  77.       printf("Window DEPTH CLOSE DRAG and SIZE gadgets and\n");
  78.       printf("Screen DEPTH gadgets.\n");
  79.       printf("Usage: %s [LOAD] filename | [SAVE filename]\n",argv[0]);
  80.    }
  81. }
  82. /*#ENDFD*/
  83.  
  84. struct Gadget *FindGadget(win,bits)
  85.    struct Window *win ;
  86.    USHORT         bits ;
  87. /*#FOLD:*/
  88. {
  89.    struct Gadget *gad=NULL ;
  90.    struct Screen *scr ;
  91.    int            found = 0 ;
  92.  
  93.    if(win){
  94.       gad = win->FirstGadget ;
  95.       while(gad !=NULL && found==0){
  96.          if((gad->GadgetType & 0x00F0) == bits)
  97.             found = 1 ;
  98.          else
  99.             gad = gad->NextGadget ;
  100.       }
  101.       if(gad==NULL){
  102.          scr = win->WScreen ;
  103.          if(scr){
  104.             gad = scr->FirstGadget ;
  105.             while(gad !=NULL && found==0){
  106.                if((gad->GadgetType & 0x00F0) == bits)
  107.                   found = 1 ;
  108.                else
  109.                   gad = gad->NextGadget ;
  110.             }
  111.          }
  112.       }
  113.    }
  114.    return(gad);
  115. }
  116. /*#ENDFD*/
  117.  
  118. long CalcImageSize(im)
  119.    struct Image *im ;
  120. /*#FOLD:*/
  121. {
  122.    UBYTE planes=0,i;
  123.    UBYTE planepick;
  124.    long  size=0 ;
  125.  
  126.    if(im){
  127.       planepick = im->PlanePick ;
  128.       for(i=0;i<im->Depth;i++,planepick >>= 1){
  129.          if(planepick & 0x01) planes++ ;
  130.       }
  131.       size = planes * im->Height * ((im->Width + 0x000F) >> 4) ;
  132.    }
  133.    return(size);
  134. }
  135. /*#ENDFD*/
  136.  
  137. void TakeImage(from,to)
  138.    struct Image *from,*to ;
  139. /*#FOLD:*/
  140. {
  141.    long    size1,size2 ;
  142.    USHORT *Data ;
  143.  
  144.    size1 = CalcImageSize(from);
  145.    size2 = CalcImageSize(to);
  146.    if(size1 != size2)
  147.       Data = (USHORT *)AllocMem(size1 << 1,MEMF_CLEAR|MEMF_CHIP);
  148.    else
  149.       Data = to->ImageData ;
  150.  
  151.    Forbid();
  152.  
  153.    if(size1 != size2 && size2 > 0 && to->ImageData!=NULL)
  154.       FreeMem(to->ImageData,size2 << 1);
  155.  
  156.    memcpy((char *)to,(char *)from,sizeof(struct Image));
  157.    to->ImageData = Data ;
  158.    to->NextImage = NULL ;
  159.  
  160.    if(Data!=NULL && from->ImageData != NULL && size1 > 0){
  161.       memcpy((char *)to->ImageData,(char *)from->ImageData,size1 << 1);
  162.       Permit();
  163.    }
  164.    else{
  165.       Permit();
  166.       printf("--> err: No Data\n");
  167.    }
  168. }
  169. /*#ENDFD*/
  170.  
  171. char buffer[8] ;
  172.  
  173. void LoadImages(win,name)
  174.    struct Window *win ;
  175.    char          *name ;
  176. /*#FOLD:*/
  177. {
  178.    struct Image im ;
  179.    struct Gadget *gad ;
  180.    USHORT *Data ;
  181.    BPTR   file ;
  182.    short  l,ok=1 ;
  183.    long   size ;
  184.  
  185.    if(win!=NULL && name !=NULL){
  186.       file = Open(name,MODE_OLDFILE);
  187.       if(file){
  188.          do{
  189.             l = Read(file,(UBYTE *)&buffer[0],4L);
  190.             buffer[4] = 0 ;
  191.             if(l == 4){
  192.                ok=0;
  193.                while(ok < NUMGADGETS && strcmp(buffer,ImageIDs[ok])!=0)
  194.                    ok++ ;
  195.                if(ok < NUMGADGETS){
  196.  
  197.                   size = CalcImageSize(&im);
  198.                   Data = im.ImageData ;
  199.                   Read(file,(UBYTE *)&im,sizeof(struct Image));
  200.                   im.ImageData = Data ;
  201.  
  202.                   if(im.ImageData!=NULL && size>0)
  203.                      FreeMem((UBYTE *)im.ImageData,size << 1);
  204.  
  205.                   size = CalcImageSize(&im);
  206.                   if(size > 0){
  207.                      im.ImageData = (USHORT *)AllocMem(size << 1,
  208.                         MEMF_CLEAR|MEMF_CHIP);
  209.                      if(im.ImageData)
  210.                         Read(file,(UBYTE *)im.ImageData,size << 1);
  211.                   }
  212.                   else im.ImageData = NULL ;
  213.  
  214.                   gad=FindGadget(win,ImageBits[ok]);
  215.                   if(gad){
  216.                      if((gad->Flags & GADGIMAGE) &&
  217.                          gad->GadgetRender!=NULL){
  218.                         TakeImage(&im,(struct Image *)gad->GadgetRender);
  219.                      }
  220.                   }
  221.                }
  222.             }
  223.          }while(l==4 && ok<NUMGADGETS) ;
  224.          if(im.ImageData!=NULL && size>0)
  225.             FreeMem((UBYTE *)im.ImageData,size << 1);
  226.          Close(file);
  227.          RefreshGadgets(win->FirstGadget,win,NULL);
  228.          Delay(100);
  229.       }
  230.    }
  231. }
  232. /*#ENDFD*/
  233.  
  234. void SaveImages(win,name)
  235.    struct Window *win ;
  236.    char          *name ;
  237. /*#FOLD:*/
  238. {
  239.    struct Gadget *gad ;
  240.    BPTR           file ;
  241.    long           size ;
  242.    int            i ;
  243.  
  244.    if(win!=NULL && name !=NULL){
  245.       file = Open(name,MODE_NEWFILE);
  246.       if(file){
  247.          for(i=0;i<NUMGADGETS;i++){
  248.             gad = FindGadget(win,ImageBits[i]);
  249.             if(gad){
  250.                if((gad->Flags & GADGIMAGE) && gad->GadgetRender!=NULL){
  251.                   Write(file,(UBYTE *)ImageIDs[i],4L);
  252.                   Write(file,(UBYTE *)gad->GadgetRender,
  253.                      sizeof(struct Image));
  254.                   size = CalcImageSize((struct Image *)gad->GadgetRender);
  255.                   if(size > 0)
  256.                      Write(file,
  257.             (UBYTE *)((struct Image *)gad->GadgetRender)->ImageData,
  258.                      size << 1);
  259.                }
  260.             }
  261.          }
  262.          Close(file);
  263.       }
  264.    }
  265. }
  266. /*#ENDFD*/
  267.  
  268.