home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / Misc / TRSICAT.LZX / CATS_CD2_TRSI / Reference_Library / lib_examples / iconexample.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-21  |  10.2 KB  |  299 lines

  1. ;/* iconexample.c - Execute me to compile me with SAS C 5.10
  2. LC -b1 -cfistq -v -y -j73 iconexample.c
  3. Blink FROM LIB:c.o,iconexample.o TO iconexample LIBRARY LIB:LC.lib,LIB:Amiga.lib
  4. quit
  5. **
  6. ** The following example demonstrates icon creation, icon reading and
  7. ** Tool Type parsing in the Workbench environment.  When called from the
  8. ** Shell, the example creates a small data file in RAM: and creates or
  9. ** updates a project icon for the data file.  The created project icon
  10. ** points to this example as its default tool.  When the new project
  11. ** icon is double-clicked, Workbench will invoke the default tool (this
  12. ** example) as a Workbench process, and pass it a description of the
  13. ** project data file as a Workbench argument (WBArg) in the WBStartup
  14. ** message.
  15. **
  16. ** iconexample.c - Workbench icon startup, creation, and parsing example
  17. */
  18.  
  19. #include <exec/types.h>
  20. #include <libraries/dos.h>
  21. #include <workbench/workbench.h>
  22. #include <workbench/startup.h>
  23.  
  24. #include <clib/alib_protos.h>
  25. #include <clib/exec_protos.h>
  26. #include <clib/dos_protos.h>
  27. #include <clib/icon_protos.h>
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #include <string.h>
  31.  
  32. #ifdef LATTICE
  33. int CXBRK(void) { return(0); }   /* Disable SAS Lattice CTRL/C handling */
  34. int chkabort(void) { return(0); }/* really */
  35. #endif
  36.  
  37. /* our functions */
  38. void cleanexit(UBYTE *,LONG);
  39. void cleanup(void);
  40. void message(UBYTE *);
  41. BOOL makeIcon(UBYTE *, char **, char *);
  42. BOOL showToolTypes(struct WBArg *);
  43.  
  44. UBYTE *projname     = "RAM:Example_Project";
  45. UBYTE *conwinname   = "CON:10/10/620/180/iconexample";
  46.  
  47. UBYTE deftoolname[] = {"iconexample"};
  48.  
  49. USHORT IconImageData1[] =  {
  50. /* Plane 0 */
  51.     0x0000,0x0000,0x0000,0x1000,0x0000,0x0000,0x0000,0x3000,
  52.     0x0FFF,0xFFFC,0x0000,0x3000,0x0800,0x0004,0x0000,0x3000,
  53.     0x0800,0x07FF,0xFFC0,0x3000,0x08A8,0xA400,0x00A0,0x3000,
  54.     0x0800,0x0400,0x0090,0x3000,0x08AA,0xA400,0x0088,0x3000,
  55.     0x0800,0x042A,0xA0FC,0x3000,0x082A,0xA400,0x0002,0x3000,
  56.     0x0800,0x0400,0x0002,0x3000,0x0800,0xA42A,0xA0A2,0x3000,
  57.     0x0800,0x0400,0x0002,0x3000,0x0950,0xA42A,0x8AA2,0x3000,
  58.     0x0800,0x0400,0x0002,0x3000,0x082A,0xA400,0x0002,0x3000,
  59.     0x0800,0x042A,0x2AA2,0x3000,0x0FFF,0xFC00,0x0002,0x3000,
  60.     0x0000,0x0400,0x0002,0x3000,0x0000,0x07FF,0xFFFE,0x3000,
  61.     0x0000,0x0000,0x0000,0x3000,0x7FFF,0xFFFF,0xFFFF,0xF000,
  62. /* Plane 1 */
  63.     0xFFFF,0xFFFF,0xFFFF,0xE000,0xD555,0x5555,0x5555,0x4000,
  64.     0xD000,0x0001,0x5555,0x4000,0xD7FF,0xFFF9,0x5555,0x4000,
  65.     0xD7FF,0xF800,0x0015,0x4000,0xD757,0x5BFF,0xFF55,0x4000,
  66.     0xD7FF,0xFBFF,0xFF65,0x4000,0xD755,0x5BFF,0xFF75,0x4000,
  67.     0xD7FF,0xFBD5,0x5F01,0x4000,0xD7D5,0x5BFF,0xFFFD,0x4000,
  68.     0xD7FF,0xFBFF,0xFFFD,0x4000,0xD7FF,0x5BD5,0x5F5D,0x4000,
  69.     0xD7FF,0xFBFF,0xFFFD,0x4000,0xD6AF,0x5BD5,0x755D,0x4000,
  70.     0xD7FF,0xFBFF,0xFFFD,0x4000,0xD7D5,0x5BFF,0xFFFD,0x4000,
  71.     0xD7FF,0xFBD5,0xD55D,0x4000,0xD000,0x03FF,0xFFFD,0x4000,
  72.     0xD555,0x53FF,0xFFFD,0x4000,0xD555,0x5000,0x0001,0x4000,
  73.     0xD555,0x5555,0x5555,0x4000,0x8000,0x0000,0x0000,0x0000,
  74. };
  75.  
  76. struct Image iconImage1 =
  77.     {
  78.     0, 0,                /* Top Corner */
  79.     52, 22, 2,           /* Width, Height, Depth */
  80.     &IconImageData1[0],  /* Image Data */
  81.     0x003, 0x000,        /* PlanePick,PlaneOnOff */
  82.     NULL                 /* Next Image */
  83.     };
  84.  
  85. UBYTE *toolTypes[] =
  86.     {
  87.     "FILETYPE=text",
  88.     "FLAGS=BOLD|ITALICS",
  89.     NULL
  90.     };
  91.  
  92. struct DiskObject projIcon =
  93.     {
  94.     WB_DISKMAGIC,                   /* Magic Number */
  95.     WB_DISKVERSION,                 /* Version */
  96.         {                           /* Embedded Gadget Structure */
  97.         NULL,                       /* Next Gadget Pointer */
  98.         97,12,52,23,                /* Left,Top,Width,Height */
  99.         GADGIMAGE|GADGHBOX,         /* Flags */
  100.         GADGIMMEDIATE|RELVERIFY,    /* Activation Flags */
  101.         BOOLGADGET,                 /* Gadget Type */
  102.         (APTR)&iconImage1,          /* Render Image */
  103.         NULL,                       /* Select Image */
  104.         NULL,                       /* Gadget Text */
  105.         NULL,                       /* Mutual Exclude */
  106.         NULL,                       /* Special Info */
  107.         0,                          /* Gadget ID */
  108.         NULL                        /* User Data */
  109.         },
  110.     WBPROJECT,                      /* Icon Type */
  111.     deftoolname,                    /* Default Tool */
  112.     toolTypes,                      /* Tool Type Array */
  113.     NO_ICON_POSITION,               /* Current X */
  114.     NO_ICON_POSITION,               /* Current Y */
  115.     NULL,                           /* Drawer Structure */
  116.     NULL,                           /* Tool Window */
  117.     4000                            /* Stack Size */
  118.     };
  119.  
  120. /* Opens and allocations we must clean up */
  121. struct Library *IconBase = NULL;
  122. FILE *conwin = NULL;
  123. LONG olddir = -1;
  124.  
  125. BOOL FromWb;
  126.  
  127. void main(int argc, char **argv)
  128.     {
  129.     struct WBStartup *WBenchMsg;
  130.     struct WBArg *wbarg;
  131.     FILE  *file;
  132.     LONG  wLen;
  133.     SHORT i;
  134.  
  135.     FromWb = (argc==0) ? TRUE : FALSE;
  136.  
  137.     /* Open icon.library */
  138.     if(!(IconBase = OpenLibrary("icon.library",33)))
  139.          cleanexit("Can't open icon.library\n",RETURN_FAIL);
  140.  
  141.     /* If started from CLI, this example will create a small text
  142.      * file RAM:Example_Project, and create an icon for the file
  143.      * which points to this program as its default tool.
  144.      */
  145.     if(!FromWb)
  146.         {
  147.         /* Make a sample project (data) file */
  148.         wLen = -1;
  149.         if(file=fopen(projname,"w"))
  150.             {
  151.             wLen = fprintf(file,"Have a nice day\n");
  152.             fclose(file);
  153.             }
  154.         if(wLen < 0) cleanexit("Error writing data file\n",RETURN_FAIL);
  155.  
  156.         /* Now save/update icon for this data file */
  157.         if(makeIcon(projname, toolTypes, deftoolname))
  158.              {
  159.              printf("%s data file and icon saved.\n",projname);
  160.              printf("Use Workbench menu Icon Information to examine the icon.\n");
  161.              printf("Then copy this example (iconexample) to RAM:\n");
  162.              printf("and double-click the %s project icon\n",projname);
  163.              }
  164.         else cleanexit("Error writing icon\n",RETURN_FAIL);
  165.         }
  166.  
  167.     else  /* Else we are FromWb - ie. we were either
  168.            * started by a tool icon, or as in this case,
  169.            * by being the default tool of a project icon.
  170.            */
  171.         {
  172.         if(!(conwin = fopen(conwinname,"r+")))
  173.              cleanexit("Can't open output window\n",RETURN_FAIL);
  174.  
  175.         WBenchMsg = (struct WBStartup *)argv;
  176.  
  177.         /* Note wbarg++ at end of FOR statement steps through wbargs.
  178.          * First arg is our executable (tool).  Any additional args
  179.          * are projects/icons passed to us via either extend select
  180.          * or default tool method.
  181.          */
  182.         for(i=0, wbarg=WBenchMsg->sm_ArgList;
  183.             i < WBenchMsg->sm_NumArgs;
  184.             i++, wbarg++)
  185.             {
  186.             /* if there's a directory lock for this wbarg, CD there */
  187.             olddir = -1;
  188.             if((wbarg->wa_Lock)&&(*wbarg->wa_Name))
  189.                 olddir = CurrentDir(wbarg->wa_Lock);
  190.  
  191.             showToolTypes(wbarg);
  192.  
  193.             if((i>0)&&(*wbarg->wa_Name))
  194.                 fprintf(conwin,"In Main. We could open the %s file here\n",
  195.                                  wbarg->wa_Name);
  196.             if(olddir != -1)  CurrentDir(olddir); /* CD back where we were */
  197.             }
  198.         Delay(500);
  199.         }
  200.     cleanup();
  201.     exit(RETURN_OK);
  202.     }
  203.  
  204. BOOL makeIcon(UBYTE *name, char **newtooltypes, char *newdeftool)
  205.     {
  206.     struct DiskObject *dobj;
  207.     char *olddeftool;
  208.     char **oldtooltypes;
  209.     BOOL success = FALSE;
  210.  
  211.     if(dobj=GetDiskObject(name))
  212.         {
  213.         /* If file already has an icon, we will save off any fields we
  214.          * need to update, update those fields, put the object, restore
  215.          * the old field pointers and then free the object.  This will
  216.          * preserve any custom imagery the user has, and the user's
  217.          * current placement of the icon.  If your application does
  218.          * not know where the user currently keeps your application,
  219.          * you should not update his dobj->do_DefaultTool.
  220.          */
  221.          oldtooltypes = dobj->do_ToolTypes;
  222.          olddeftool = dobj->do_DefaultTool;
  223.  
  224.          dobj->do_ToolTypes = newtooltypes;
  225.          dobj->do_DefaultTool = newdeftool;
  226.  
  227.          success = PutDiskObject(name,dobj);
  228.  
  229.          /* we must restore the original pointers before freeing */
  230.          dobj->do_ToolTypes = oldtooltypes;
  231.          dobj->do_DefaultTool = olddeftool;
  232.          FreeDiskObject(dobj);
  233.          }
  234.     /* Else, put our default icon */
  235.     if(!success)  success = PutDiskObject(name,&projIcon);
  236.     return(success);
  237.     }
  238.  
  239. BOOL showToolTypes(struct WBArg *wbarg)
  240.     {
  241.     struct DiskObject *dobj;
  242.     char **toolarray;
  243.     char *s;
  244.     BOOL success = FALSE;
  245.  
  246.     fprintf(conwin,"\nWBArg Lock=0x%lx, Name=%s\n",
  247.                            wbarg->wa_Lock,wbarg->wa_Name);
  248.  
  249.     if((*wbarg->wa_Name) && (dobj=GetDiskObject(wbarg->wa_Name)))
  250.         {
  251.         fprintf(conwin,"  We have read the DiskObject (icon) for this arg\n");
  252.         toolarray = (char **)dobj->do_ToolTypes;
  253.  
  254.         if(s=(char *)FindToolType(toolarray,"FILETYPE"))
  255.             {
  256.             fprintf(conwin,"    Found tooltype FILETYPE with value %s\n",s);
  257.             }
  258.         if(s=(char *)FindToolType(toolarray,"FLAGS"))
  259.             {
  260.             fprintf(conwin,"    Found tooltype FLAGS with value %s\n",s);
  261.             if(MatchToolValue(s,"BOLD"))
  262.                 fprintf(conwin,"      BOLD flag requested\n");
  263.             if(MatchToolValue(s,"ITALICS"))
  264.                 fprintf(conwin,"      ITALICS flag requested\n");
  265.             }
  266.         /* Free the diskobject we got */
  267.         FreeDiskObject(dobj);
  268.         success = TRUE;
  269.         }
  270.     else if(!(*wbarg->wa_Name))
  271.         fprintf(conwin,"  Must be a disk or drawer icon\n");
  272.     else
  273.         fprintf(conwin,"  Can't find any DiskObject (icon) for this WBArg\n");
  274.     return(success);
  275.     }
  276.  
  277.  
  278. /* Workbench-started programs with no output window may want to display
  279.  * messages in a different manner (requester, window title, etc)
  280.  */
  281. void message(UBYTE *s)
  282.     {
  283.     if(FromWb && conwin)  fprintf(conwin,s,strlen(s));
  284.     else if (!FromWb) printf(s);
  285.     }
  286.  
  287. void cleanexit(UBYTE *s, LONG n)
  288.     {
  289.     if(*s)  message(s);
  290.     cleanup();
  291.     exit(n);
  292.     }
  293.  
  294. void cleanup()
  295.    {
  296.    if(conwin)    fclose(conwin);
  297.    if(IconBase)  CloseLibrary(IconBase);
  298.    }
  299.