/*  :ts=8 bk=0
 *
 * fixwb.c:	A program to fix the WorkBench backdrop window into place,
 *		by turning it into a SMART_REFRESH window.  Also will load
 *		an IFF image into the window, assuming I can figure out the
 *		IFF spec.
 *
 * Sigh.  As it turns out, the WorkBench program makes this effort
 * impossible since it clears the backdrop window everytime something new
 * happens with the disk icons.
 *
 * Leo L. Schwab		8703.10		(415)-456-6565
 */

#include <exec/types.h>
#include <intuition/intuition.h>

/*  Only the full-screen WorkBench window has these two flags set.  */
#define	WBACK		(WBENCHWINDOW | BACKDROP)

extern void	*OpenLibrary(), *OpenWindow(), *CreateBehindLayer();

struct NewWindow windef = {
	0, 30, 200, 10,
	-1, -1,
	NULL,
	WINDOWDRAG | WINDOWDEPTH | SMART_REFRESH | ACTIVATE,
	NULL, NULL,
	(UBYTE *) "Computing...",
	NULL, NULL,
	0, 0, 0, 0,
	WBENCHSCREEN
};

struct Window	*win;
void		*IntuitionBase, *GfxBase, *LayersBase;


main ()
{
	struct Screen *wbs;
	struct Layer_Info *li;
	struct Layer *wbl;
	struct Window *chk, *wbw = 0;
	long top, left, wide, high;
	long flags;

	openstuff ();
	wbs = win -> WScreen;	/*  Get pointer to WorkBench screen  */
	li = &wbs -> LayerInfo;

	for (chk = wbs -> FirstWindow; chk; chk = chk -> NextWindow) {
		flags = chk -> Flags;
		if ((flags & WBACK) == WBACK) {
			wbw = chk;
			break;
		}
	}
	if (!wbw)
		die ("WorkBench not loaded.");
	if ((flags & REFRESHBITS) == SMART_REFRESH)
		die ("WorkBench already fixed.");
	top	= wbw -> TopEdge;
	left	= wbw -> LeftEdge;
	wide	= wbw -> Width;
	high	= wbw -> Height;

	wbl = wbw -> RPort -> Layer;	/*  Get the layer pointer  */

	/*
	 * At this point, we are about to perform what is probably
	 * tantamount to rape to the system.  Therefore, I'm Forbid()ing
	 * anything else from running so as to prevent a major collapse.
	 *
	 * First, we stop everyone else.  Then, we un-backdrop the layer
	 * so we can depth-arrange it.  We then bring it to the front,
	 * forcing the layers library to save the state of everything else.
	 * Then we change the identity of the layer from SIMPLE to SMART.
	 * Then we depth-arrange it to the back again.  This will make all
	 * previous layers visible, and cause the library to create all the
	 * necssary ClipRects and backup bitmaps for us.  Then we re-set
	 * the backdrop bit, anchoring the window into place.
	 */

	Forbid ();
	wbl -> Flags &= ~LAYERBACKDROP;
	UpfrontLayer (li, wbl);
	wbl -> Flags = wbl -> Flags & ~LAYERSIMPLE | LAYERSMART;
	BehindLayer (li, wbl);
	wbl -> Flags |= LAYERBACKDROP;

	/*  Correct Intuition's ideas about the window.  */
	wbw -> Flags = flags & ~SIMPLE_REFRESH | SMART_REFRESH;

	Permit ();	/*  That should do it  */

	/*  Do things in the newly fixed WorkBench  */
	SetAPen (wbw -> RPort, 1L);
	Move (wbw -> RPort, 0L, 0L);
	Draw (wbw -> RPort, wide-1, high-1);
	Move (wbw -> RPort, 0L, high-1);
	Draw (wbw -> RPort, wide-1, 0L);
	/*
	 * If you want to watch the Mask "bug" at work, uncomment this next
	 * line of code.  Move window on and off disk icons and watch the
	 * results.
	 *
	 * wbw -> RPort -> Mask = 1;
	 */
	closestuff ();
}


openstuff ()
{
	if (!(IntuitionBase = OpenLibrary ("intuition.library", 0L)))
		die ("Intuition unavailable, use logic.");

	if (!(GfxBase = OpenLibrary ("graphics.library", 0L)))
		die ("Art shop closed.");

	if (!(LayersBase = OpenLibrary ("layers.library", 0L)))
		die ("Nothing laying around.");

	if (!(win = OpenWindow (&windef)))
		die ("Window painted shut.");
}

closestuff ()
{
	if (win)		CloseWindow (win);
	if (LayersBase)		CloseLibrary (LayersBase);
	if (GfxBase)		CloseLibrary (GfxBase);
	if (IntuitionBase)	CloseLibrary (IntuitionBase);
}

die (str)
char *str;
{
	puts (str);
	closestuff ();
	exit (10);
}


