home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 377b.lha / libraries / workbench / rkm_icon_example.c < prev    next >
Encoding:
C/C++ Source or Header  |  1980-02-03  |  10.4 KB  |  304 lines

  1. ;/* rkm_icon_example.c - Execute me to compile me with Lattice 5.04
  2. LC -b1 -cfistq -v -y -j73 rkm_icon_example.c
  3. Blink FROM LIB:c.o,rkm_icon_example.o TO rkm_icon_example LIBRARY LIB:LC.lib,LIB:Amiga.lib
  4. quit
  5. */
  6.  
  7. /* RKM_Icon_Example.c - Workbench icon startup, creation, and parsing example
  8.  *
  9.  * Copyright (c) 1990 Commodore-Amiga, Inc.
  10.  *
  11.  * This example is provided in electronic form by Commodore-Amiga, Inc. for
  12.  * use with the 1.3 revisions of the Addison-Wesley Amiga reference manuals. 
  13.  * The 1.3 Addison-Wesley Amiga Reference Manual series contains additional
  14.  * information on the correct usage of the techniques and operating system
  15.  * functions presented in this example.  The source and executable code of
  16.  * this example may only be distributed in free electronic form, via bulletin
  17.  * board or as part of a fully non-commercial and freely redistributable
  18.  * diskette.  Both the source and executable code (including comments) must
  19.  * be included, without modification, in any copy.  This example may not be
  20.  * published in printed form or distributed with any commercial product.
  21.  * However, the programming techniques and support routines set forth in
  22.  * this example may be used in the development of original executable
  23.  * software products for Commodore Amiga computers.
  24.  * All other rights reserved.
  25.  * This example is provided "as-is" and is subject to change; no warranties
  26.  * are made.  All use is at your own risk.  No liability or responsibility
  27.  * is assumed.
  28.  */
  29.  
  30. #include <exec/types.h>
  31. #include <libraries/dos.h>
  32. #include <workbench/workbench.h>
  33. #include <workbench/startup.h>
  34. #ifdef LATTICE
  35. #include <proto/all.h>
  36. #include <stdlib.h>
  37. #include <stdio.h>
  38. #include <string.h>
  39. int CXBRK(void) { return(0); }        /* Disable Lattice CTRL/C handling */
  40. int chkabort(void) { return(0); }    /* really */
  41. #endif
  42.  
  43. /* our functions */
  44. void cleanexit(UBYTE *,LONG);
  45. void cleanup(void);
  46. void message(UBYTE *);
  47. BOOL makeIcon(UBYTE *, char **, char *);
  48. BOOL showToolTypes(struct WBArg *);
  49.  
  50.  
  51. UBYTE *projname = "RAM:Example_Project";
  52. UBYTE *conwinname = "CON:10/10/620/180/RKM_Icon_Example";
  53.  
  54. char deftoolname[] = "RKM_Icon_Example";
  55.  
  56. USHORT IconImageData1[] = {
  57. /* Plane 0 */
  58.     0x0000,0x0000,0x0000,0x3fff,0xffcc,0x0000,0x3fff,0xffcf,
  59.     0x0000,0x3fff,0xffcf,0xc000,0x3803,0xffcf,0xf000,0x3fff,
  60.     0xffc0,0x0000,0x3803,0xffff,0xfc00,0x3fff,0xffff,0xfc00,
  61.     0x3fff,0xffff,0xfc00,0x3f84,0x00c0,0x7c00,0x3fff,0xffff,
  62.     0xfc00,0x3900,0x8000,0x7c00,0x3fff,0xffff,0xfc00,0x3800,
  63.     0x0040,0x7c00,0x3fff,0xffff,0xfc00,0x3fff,0xffff,0xfc00,
  64.     0x3fff,0xfe00,0x7c00,0x3fff,0xffff,0xfc00,0x3fff,0xffff,
  65.     0xfc00,0x0000,0x0000,0x0000,
  66. /* Plane 1 */
  67.     0xffff,0xfffc,0x0000,0xc000,0x0033,0x0000,0xc000,0x0030,
  68.     0xc000,0xc000,0x0030,0x3000,0xc7fc,0x0030,0x0c00,0xc000,
  69.     0x003f,0xff00,0xc7fc,0x0000,0x0300,0xc000,0x0000,0x0300,
  70.     0xc000,0x0000,0x0300,0xc07b,0xff3f,0x8300,0xc000,0x0000,
  71.     0x0300,0xc6ff,0x7fff,0x8300,0xc000,0x0000,0x0300,0xc7ff,
  72.     0xffbf,0x8300,0xc000,0x0000,0x0300,0xc000,0x0000,0x0300,
  73.     0xc000,0x01ff,0x8300,0xc000,0x0000,0x0300,0xc000,0x0000,
  74.     0x0300,0xffff,0xffff,0xff00,
  75.     };
  76.  
  77. struct Image iconImage1 = 
  78.     {
  79.     0, 0,                /* Top Corner */
  80.     40, 20, 2,           /* Width, Height, Depth */
  81.     &IconImageData1[0],  /* Image Data */
  82.     0x003, 0x000,        /* PlanePick,PlaneOnOff */
  83.     NULL                 /* Next Image */
  84.     };
  85.  
  86. UBYTE *toolTypes[] = 
  87.     {
  88.     "FILETYPE=text",
  89.     "FLAGS=BOLD|ITALICS",
  90.     NULL
  91.     };
  92.  
  93. struct DiskObject projIcon = 
  94.     {
  95.     WB_DISKMAGIC,                   /* Magic Number */
  96.     WB_DISKVERSION,                 /* Version */
  97.         {                           /* Embedded Gadget Structure */
  98.         NULL,                       /* Next Gadget Pointer */
  99.         97,12,40,21,                /* Left,Top,Width,Height */
  100.         GADGIMAGE|GADGHBOX,         /* Flags */
  101.         GADGIMMEDIATE|RELVERIFY,    /* Activation Flags */
  102.         BOOLGADGET,                 /* Gadget Type */
  103.         (APTR)&iconImage1,          /* Render Image */
  104.         NULL,                       /* Select Image */
  105.         NULL,                       /* Gadget Text */
  106.         NULL,                       /* Mutual Exclude */
  107.         NULL,                       /* Special Info */
  108.         0,                          /* Gadget ID */
  109.         NULL                        /* User Data */
  110.         },
  111.     WBPROJECT,                      /* Icon Type */
  112.     deftoolname,                    /* Default Tool */
  113.     toolTypes,                      /* Tool Type Array */
  114.     NO_ICON_POSITION,               /* Current X */
  115.     NO_ICON_POSITION,               /* Current Y */
  116.     NULL,                           /* Drawer Structure */
  117.     NULL,                           /* Tool Window */
  118.     4000                            /* Stack Size */
  119.     };
  120.  
  121.  
  122. /* Opens and allocations we must clean up */
  123. struct Library *IconBase = NULL;
  124. FILE *conwin = NULL;
  125. LONG olddir = -1;
  126.  
  127. BOOL FromWb;
  128.  
  129. void main(int argc, char **argv)
  130.     {
  131.     struct WBStartup *WBenchMsg;
  132.     struct WBArg *wbarg;
  133.     FILE  *file;
  134.     LONG  wLen;
  135.     SHORT i;
  136.  
  137.     FromWb = (argc==0) ? TRUE : FALSE;
  138.     
  139.     /* Open icon.library */
  140.     if(!(IconBase = OpenLibrary("icon.library",33)))
  141.          cleanexit("Can't open icon.library\n",RETURN_FAIL);
  142.  
  143.     /* If started from cli, this example will create
  144.      * a small text file RAM:Example_Project, and
  145.      * create an icon for the file which points
  146.      * to this program as its default tool.
  147.      */
  148.     if(!FromWb)
  149.         {
  150.         /* Make a sample project (data) file */
  151.         wLen = -1;
  152.         if(file=fopen(projname,"w"))
  153.             {
  154.             wLen = fprintf(file,"Have a nice day\n");
  155.             fclose(file);
  156.             }
  157.         if(wLen < 0) cleanexit("Error writing data file\n",RETURN_FAIL);
  158.        
  159.         /* Now save/update icon for this data file */
  160.         if(makeIcon(projname, toolTypes, deftoolname))
  161.              {
  162.              printf("%s data file and icon saved.\n",projname); 
  163.              printf("Use Workbench Info to examine the icon.\n");
  164.              printf("Then copy this example (RKM_Icon_Example) to RAM:\n");
  165.              printf("and double-click the %s project icon\n",projname);
  166.              }
  167.         else cleanexit("Error writing icon\n",RETURN_FAIL);
  168.         }
  169.  
  170.     else  /* Else we are FromWb - ie. we were either
  171.            * started by a tool icon, or as in this case,
  172.            * by being the default tool of a project icon.
  173.            */
  174.         {
  175.         if(!(conwin = fopen(conwinname,"r+")))
  176.              cleanexit("Can't open output window\n",RETURN_FAIL);
  177.  
  178.         WBenchMsg = (struct WBStartup *)argv;
  179.  
  180.         /* Note wbarg++ at end of FOR statement steps through wbargs.
  181.          * First arg is our executable (tool).  Any additional args
  182.          * are projects/icons passed to us via either extend select
  183.          * or default tool method.
  184.          */
  185.         for(i=0, wbarg=WBenchMsg->sm_ArgList; 
  186.             i < WBenchMsg->sm_NumArgs; 
  187.             i++, wbarg++)
  188.             {
  189.             /* if there's a directory lock for this wbarg, CD there */
  190.             olddir = -1;
  191.             if((wbarg->wa_Lock)&&(*wbarg->wa_Name)) 
  192.                 olddir = CurrentDir(wbarg->wa_Lock);
  193.  
  194.             showToolTypes(wbarg);
  195.                 
  196.             if((i>0)&&(*wbarg->wa_Name)) 
  197.                 fprintf(conwin,"In Main. We could open the %s file here\n",
  198.                                  wbarg->wa_Name);
  199.             if(olddir != -1)  CurrentDir(olddir); /* CD back where we were */
  200.             }
  201.         Delay(500);
  202.         }
  203.     cleanup();
  204.     exit(RETURN_OK);
  205.     }
  206.  
  207. BOOL makeIcon(UBYTE *name, char **newtooltypes, char *newdeftool)
  208.     {
  209.     struct DiskObject *dobj;
  210.     char *olddeftool;
  211.     char **oldtooltypes;
  212.     BOOL success = FALSE;
  213.  
  214.     if(dobj=GetDiskObject(name))
  215.         {
  216.         /* If file already has an icon, we will save off any fields we
  217.          * need to update, update those fields, put the object, restore
  218.          * the old field pointers and then free the object.  This will 
  219.          * preserve any custom imagery the user has, and the user's
  220.          * current placement of the icon.  If your application does
  221.          * not know where the user currently keeps your application,
  222.          * you should not update his dobj->do_DefaultTool.
  223.          */
  224.          oldtooltypes = dobj->do_ToolTypes;
  225.          olddeftool = dobj->do_DefaultTool;
  226.  
  227.          dobj->do_ToolTypes = newtooltypes;
  228.          dobj->do_DefaultTool = newdeftool;
  229.  
  230.          success = PutDiskObject(name,dobj);
  231.  
  232.          /* we must restore the original pointers before freeing */
  233.          dobj->do_ToolTypes = oldtooltypes;
  234.          dobj->do_DefaultTool = olddeftool;
  235.          FreeDiskObject(dobj);
  236.          }
  237.     /* Else, put our default icon */
  238.     if(!success)  success = PutDiskObject(name,&projIcon);
  239.     return(success);
  240.     }
  241.  
  242. BOOL showToolTypes(struct WBArg *wbarg)
  243.     {
  244.     struct DiskObject *dobj;
  245.     char **toolarray;
  246.     char *s;
  247.     BOOL success = FALSE;
  248.  
  249.     fprintf(conwin,"\nWBArg Lock=0x%lx, Name=%s\n",
  250.                            wbarg->wa_Lock,wbarg->wa_Name);
  251.  
  252.     if((*wbarg->wa_Name) && (dobj=GetDiskObject(wbarg->wa_Name)))
  253.         {
  254.         fprintf(conwin,"  We have read the DiskObject (icon) for this arg\n");
  255.         toolarray = (char **)dobj->do_ToolTypes;
  256.  
  257.         if(s=(char *)FindToolType(toolarray,"FILETYPE"))
  258.             {
  259.             fprintf(conwin,"    Found tooltype FILETYPE with value %s\n",s);
  260.             }
  261.         if(s=(char *)FindToolType(toolarray,"FLAGS"))
  262.             {
  263.             fprintf(conwin,"    Found tooltype FLAGS with value %s\n",s);
  264.             if(MatchToolValue(s,"BOLD")) 
  265.                 fprintf(conwin,"      BOLD flag requested\n");
  266.             if(MatchToolValue(s,"ITALICS")) 
  267.                 fprintf(conwin,"      ITALICS flag requested\n");
  268.             }
  269.         /* Free the diskobject we got */
  270.         FreeDiskObject(dobj);
  271.         success = TRUE;
  272.         }
  273.     else if(!(*wbarg->wa_Name))
  274.         fprintf(conwin,"  Must be a disk or drawer icon\n");
  275.     else 
  276.         fprintf(conwin,"  Can't find DiskObject (icon) for this WBArg\n");
  277.     return(success);
  278.     }
  279.  
  280.  
  281. /* Workbench-started programs with no output window
  282.  * will want to display messages in a different manner
  283.  * (requester, window title, etc) if FromWb is TRUE.
  284.  */
  285. void message(UBYTE *s)
  286.     {
  287.     if(FromWb && conwin)  fprintf(conwin,s,strlen(s));
  288.     else if (!FromWb) printf(s);
  289.     }
  290.  
  291. void cleanexit(UBYTE *s, LONG n)
  292.     {
  293.     if(*s)  message(s);
  294.     cleanup();
  295.     exit(n);
  296.     }
  297.  
  298. void cleanup()
  299.    {
  300.    if(conwin)     fclose(conwin);
  301.    if(IconBase)  CloseLibrary(IconBase);
  302.    }
  303.  
  304.