/***************************************************************************/
/*									   */
/* SmartIcon, release 1.0 - An Intuition object iconifier for the Amiga    */
/* Copyright (c) 1988 Gauthier H. Groult                                   */
/*									   */
/* Written in January 1988 by Gauthier H. Groult			   */
/*			      33, Boulevard Saint Denis,		   */
/*			      92400 Courbevoie				   */
/*			      France - Europe				   */
/*			      Tel: (16) 1 47 89 09 54                      */
/*			      email: mcvax!inria!litp!germinal!groult	   */
/*									   */
/* This source code is not in public domain, please do not distribute.	   */
/* The binary program is shareaware. Read docs files for details.	   */
/*									   */
/***************************************************************************/

#include <exec/types.h>
#include <exec/memory.h>
#include <libraries/dos.h>
#include <libraries/dosextens.h>
#include <workbench/workbench.h>
#include <workbench/startup.h>
#include <string.h>

VOID  main(), Cleanup(), RevealWindow();

LONG	 IconBase;
struct	 LayersBase	*LayersBase;
struct	 IntuitionBase	*IntuitionBase;
struct	 WBStartup	*wbStartup;
struct	 FileInfoBlock	*theIconInfo;
struct	 Lock		*theIconLock;

extern struct WBStartup *WBenchMsg;

/* This program is the one that un-iconifies a window when the user	   */
/* double-clicks on the icon. Its is actually written to the ram-disk	   */
/* by the iconofier program at iconification times. Thus it is		   */
/* contained in a binary format in the iconifier program itself.	   */
/* The program bin2txt was written to translate the executable form	   */
/* of this program to a C array.					   */
/*									   */
/* This code read a window pointer from the comment field of it's icon.    */
/* Then it calls reveal window to reset the window layer and leaves	   */
/* after having destroyed it's own ram-disk file.                          */
/*									   */
/* See icon.c for more details. 					   */

VOID
main(argc, argv)
UBYTE argc, **argv;
{
   UBYTE    filename[30];
   struct   WBArg	   *wbArgs;
   struct   Window	   *window;

   IntuitionBase = (struct IntuitionBase *)
		   OpenLibrary("intuition.library", 0);
   if (!LayersBase) Cleanup(10);

   LayersBase = (struct LayersBase *)OpenLibrary("layers.library", 0);
   if (!LayersBase) Cleanup(11);

   IconBase = OpenLibrary("icon.library",0);
   if (!IconBase) Cleanup(12);

   theIconInfo = (struct FileInfoBlock *)
		 AllocMem(sizeof(struct FileInfoBlock), MEMF_CHIP);
   if (!theIconInfo) Cleanup(20);

   if (!argc)
      {
      wbStartup = WBenchMsg;
      wbArgs = wbStartup->sm_ArgList;
      theIconLock = (struct Lock *)Lock(wbArgs[0].wa_Name, MODE_OLDFILE);
      if (!theIconLock) Cleanup(30);
      if (!Examine(theIconLock, theIconInfo))
	 {
	 UnLock(theIconLock);
	 Cleanup(31);
	 }

      Forbid();
      window = (struct Window *)atoi(theIconInfo->fib_Comment);
      RevealWindow(window->WScreen, window);
      Permit();

      UnLock(theIconLock);
      DeleteFile(wbArgs[0].wa_Name);
      sprintf(filename, "%s.info", wbArgs[0].wa_Name);
      DeleteFile(filename);
      }
   else printf("Must execute from WorkBench!!\n");

   Cleanup(0);
}

VOID
Cleanup(error)
USHORT error;
{
   if (error>10) DisplayBeep(NULL);

   if (theIconInfo)     FreeMem(theIconInfo, sizeof(struct FileInfoBlock));

   if (IconBase)        CloseLibrary(IconBase);
   if (LayersBase)      CloseLibrary(LayersBase);
   if (IntuitionBase)   CloseLibrary(IntuitionBase);
}

VOID
RevealWindow(screen, window)
struct	 Screen   *screen;
struct	 Window   *window;
{
   struct   Layer	*layer;
   struct   Layer_Info	*linfo;

   layer = window->RPort->Layer;
   linfo = layer->LayerInfo;

   MoveLayer(linfo, layer,
	     window->LeftEdge - screen->Width  + 1,
	     window->TopEdge  - screen->Height + 1);
   SizeLayer(linfo, layer, window->Width-1, window->Height-1);
   WindowToFront(window);
}
