
/* SplitIcon.c */

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* (c) Copyright 1988 Charles D. Maier       All rights reserved         */
/*     338 E. 35th Street, Wilmington, Del 19802  (302) 764-6079         */
/*  This program may be used and modified for any purpose so long as     */
/*  this copyright notice remains with the code and the program is not   */
/*  sold and no charge is made for its use.                              */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*
   AltIcon is really nice program and Its tutorial led me to write this
very simple program. What it does is....
   Splits the icon that was combined as a dual-action icon with Alticon
into two separate icons that you can then edit separately and THEN
recombine if you wish.
   This program was brought about by a friend who wanted to improve on
someone elses imagary... Well Den.... Here it is.....

                      SPLITICON  !!!!

To use it, first compile the program (It works under Lattice, have no idea
about MANX) and link it.

*/

#include <stdio.h>
#include <exec/types.h>
#include <workbench/workbench.h>
#include <workbench/icon.h>
#include <workbench/startup.h>

int IconBase;
extern  struct WBStartup *WBenchMsg;

char front[] = "-F";
char back[] = "-B";
char name[80];

void main(argc,argv)
int argc;
char *argv[];
{

struct DiskObject *diskobj, *GetDiskObject();
APTR saveimage;
APTR savegad;
USHORT saveflags;

/* make sure we ran from CLI */
if (argc == 0)
   exit(1);

/* make sure they specified one icon */
if (argc != 2)
   {
   printf("Usage: %s <icon> \n", argv[0]);
   exit(2);
   }

/* make the icon library available to us */
if ( (IconBase = OpenLibrary( ICONNAME, 1)) == NULL)
   exit(2);

/* read the icon to modify from disk */
if ( (diskobj = GetDiskObject(argv[1])) == NULL)
   {
   printf("Cannot read icon for %s\n", argv[1]);
   CloseLibrary( IconBase );
   exit(3);
   }


/* remember what the original icon looked like */
/* we will restore it before we do a FreeDiskObject on it */
/* this way the system won't get confused as to which memory it */
/* allocated for the structure */

saveimage = diskobj->do_Gadget.SelectRender;
saveflags = diskobj->do_Gadget.Flags;
savegad   = diskobj->do_Gadget.GadgetRender;

/* modify the flags to get rid of the alternate image */
diskobj->do_Gadget.Flags = GADGHCOMP | GADGIMAGE;
diskobj->do_Gadget.SelectRender = NULL;

/* make the resultant images find thier own place in the window */
diskobj->do_CurrentX = NO_ICON_POSITION;
diskobj->do_CurrentY = NO_ICON_POSITION;

/* setup frontname */
strcpy(name,argv[1]);
strcat(name,front);


/* now write the front icon to disk */
if ( !PutDiskObject( name, diskobj) )
   printf("Cannot write new icon - code %d\n", IoErr() );


/* setup backname */
strcpy(name,argv[1]);
strcat(name,back);
diskobj->do_Gadget.GadgetRender = saveimage;

/* now write the back icon to disk */
if ( !PutDiskObject( name, diskobj) )
   printf("Cannot write new icon - code %d\n", IoErr() );

/* restore the original icon to what we read in */
diskobj->do_Gadget.SelectRender = saveimage;
diskobj->do_Gadget.Flags = saveflags;
diskobj->do_Gadget.GadgetRender = savegad;

/* and send it away */
if ( diskobj)
   FreeDiskObject( diskobj );
if ( IconBase)
   CloseLibrary( IconBase );
}

