/*
 *		skew_iconify.c
 */

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

#include "skew.h"

extern 	struct Process	*ThisProcess;
extern 	struct Window	*Wnd;
extern 	struct Screen  	*Scr;
extern 	struct Gadget 	*GList;
extern 	APTR			VisualInfo;
extern 	APTR 			OldWindow;

extern VOID CleanUp( LONG );
extern LONG InitDisplay( VOID );

BOOL GoodDoubleClick( LONG, ULONG, ULONG );


VOID
Iconify( VOID )
{
	static LONG left	= 0;
	static LONG top 	= 0;

	struct NewWindow TinyWindow = {
		left, top,						/* left, top */
		100, 22, 						/* width,height */
		0, 1,							/* detailpen, blockpen */
		( MOUSEBUTTONS | GADGETDOWN | RAWKEY
								/* | INTUITICKS | INACTIVEWINDOW */ ),
		( WINDOWDRAG | RMBTRAP | NOCAREREFRESH | INACTIVEWINDOW),
		NULL,							/* no custom gadgets */
		NULL,							/* no custom menu images */
		(UBYTE *)"Skew v1.28",
		NULL,							/* no custom screen structure */
		NULL, 							/* no superbitmap */
		0, 0, 0, 0,
		PUBLICSCREEN
	};

	struct Window		*tinyWnd;
    struct IntuiMessage	*msg;

    ULONG  class;
    USHORT code;
	SHORT  old_x = 0, old_y = 0;
	SHORT  new_x, new_y;
    ULONG  seconds, micros;

    BOOL fDone = FALSE;

	/*
	 *	Shut down normal display
	 */
  	if ( Wnd ) {
		( (struct Process *)ThisProcess )->pr_WindowPtr = OldWindow;
		CloseWindow( Wnd );
		Wnd = NULL;
  	}

	if ( GList ) {
		FreeGadgets( GList );
		GList = NULL;
	}

	if ( VisualInfo ) {
		FreeVisualInfo( VisualInfo );
		VisualInfo = NULL;
	}

	if ( Scr ) {
		CloseScreen( Scr );
		Scr = NULL;
	}

	/* open our tiny window */
	tinyWnd = (struct Window *)OpenWindow( &TinyWindow );
	if ( ! tinyWnd ) {
		/* maybe the screen got too small...? */
		left	= 0;
		top		= 0;
		tinyWnd = (struct Window *)OpenWindow( &TinyWindow );
		if ( ! tinyWnd )
			CleanUp( 5L );
	}

    while ( ! fDone ) {
        WaitPort( tinyWnd->UserPort );
        while ( msg = (struct IntuiMessage *)GetMsg( tinyWnd->UserPort ) ) {

            class	=  	msg->Class;
			code	=	msg->Code;
			seconds	=	msg->Seconds;
			micros	=	msg->Micros;

			new_x	=	msg->MouseX;
			new_y	=	msg->MouseY;

            ReplyMsg( msg );

            switch ( class ) {
                case IDCMP_MOUSEBUTTONS:
					switch ( code ) {
						case SELECTDOWN:
							if ( GoodDoubleClick( 0L, seconds, micros ) )
								fDone = TRUE;
							break;
						case MENUDOWN:			/* some people use this */
								fDone = TRUE;
							break;
					}
					break;
            }
        }
    }

    while ( msg = (struct IntuiMessage *)GetMsg( tinyWnd->UserPort ) )
        ReplyMsg( msg );

	left	= tinyWnd->LeftEdge;
	top		= tinyWnd->TopEdge;

	CloseWindow( tinyWnd );
	if ( ! InitDisplay() )
		CleanUp( 5L );
}


/* -------------------------------------------------------------
 * returns TRUE if the time you give it was inside the double-click
 * distance from the last time it got
 *
 * ------------------------------------------------------------- */
#define left_button		0L
#define middle_button	1L
#define right_button	2L

	BOOL
GoodDoubleClick( LONG button, ULONG secs, ULONG mics )
{
	static ULONG OldLSeconds	= 0L;
	static ULONG OldLMicros		= 0L;

	static ULONG OldMSeconds	= 0L;
	static ULONG OldMMicros		= 0L;

	static ULONG OldRSeconds	= 0L;
	static ULONG OldRMicros		= 0L;

	BOOL ret_val = FALSE;

	switch ( button ) {
		case left_button:
			if ( DoubleClick( OldLSeconds, OldLMicros, secs, mics ) ) {
				OldLSeconds = 0L;
				OldLMicros	= 0L;
				ret_val = TRUE;
			} else {
				OldLSeconds = secs;
				OldLMicros	= mics;
			}
			OldMSeconds = 0L;
			OldMMicros	= 0L;
			OldRSeconds = 0L;
			OldRMicros	= 0L;
			break;
		case middle_button:
			if ( DoubleClick( OldMSeconds, OldMMicros, secs, mics ) ) {
				OldMSeconds = 0L;
				OldMMicros	= 0L;
				ret_val = TRUE;
			} else {
				OldMSeconds = secs;
				OldMMicros	= mics;
			}
			OldLSeconds = 0L;
			OldLMicros	= 0L;
			OldRSeconds = 0L;
			OldRMicros	= 0L;
			break;
		case right_button:
			if ( DoubleClick( OldRSeconds, OldRMicros, secs, mics ) ) {
				OldRSeconds = 0L;
				OldRMicros	= 0L;
				ret_val = TRUE;
			} else {
				OldRSeconds = secs;
				OldRMicros	= mics;
			}
			OldLSeconds = 0L;
			OldLMicros	= 0L;
			OldMSeconds = 0L;
			OldMMicros	= 0L;
			break;
	}
	return( ret_val );
}
