/****************************************************************************
*
* $RCSfile: AboutReq.c $
* $Revision: 1.1 $
* $Date: 1997/08/19 15:10:23 $
* $Author: ssolie $
*
*****************************************************************************
*
* Copyright (c) 1997 Software Evolution.  All Rights Reserved.
*
*****************************************************************************
*
* AboutReq.c -- AboutReq class source file
*
* This file contains all the source code for AboutReq objects.
*/

#include <classact.h>
#include <classes/window.h>
#include <exec/memory.h>
#include <gadgets/button.h>
#include <gadgets/layout.h>
#include <images/label.h>
#include <intuition/icclass.h>
#include <intuition/intuition.h>
#include <intuition/screens.h>

#include <proto/button.h>
#include <proto/exec.h>
#include <proto/intuition.h>
#include <proto/label.h>
#include <proto/layout.h>
#include <proto/window.h>

#include "Debug.h"
#include "AboutReq.h"
#include "LocaleManager.h"

#define GetString(x)	(getString(locale_mgr, x, x##_STR))



/*** Global variables ***/
IMPORT LocaleManager		locale_mgr;
IMPORT struct ExecBase		*SysBase;
IMPORT struct IntuitionBase	*IntuitionBase;
IMPORT struct ClassLibrary	*WindowBase;
IMPORT struct ClassLibrary	*LayoutBase;
IMPORT struct ClassLibrary	*ButtonBase;
IMPORT struct ClassLibrary	*LabelBase;


/*** Local definitions ***/
#define GADGET_OK			1


/*** Local data types ***/
struct AboutReqClass {
	APTR about_window;				/* about window object */
	struct Window *window;			/* about window pointer */
};


/*
 * newAboutReq -- New AboutReq object
 *
 * Creates a new AboutReq object and opens the requester.  The requester
 * will use share the given message port so be sure to call the message
 * handling function after a signal.  Returns a reference to a new AboutReq
 * object if successful or NULL on error.
 */
AboutReq newAboutReq(struct Screen *screen, struct DrawInfo *draw_info,
	struct MsgPort *msg_port)
{
	STATIC UWORD fill_pattern[] = { 0x5555, 0xAAAA };
	AboutReq this;

	D(bug("newAboutReq(%08lx, %08lx, %08lx)\n", screen, draw_info, msg_port));

	if ( screen == NULL || draw_info == NULL || msg_port == NULL )
		return(NULL);

	this = AllocVec(sizeof(struct AboutReqClass), MEMF_CLEAR);
	if ( this == NULL )
		return(NULL);

	this->about_window = WindowObject,
		WA_Title, GetString(MSG_ABOUT_TITLE),
		WA_Flags, WFLG_DEPTHGADGET | WFLG_DRAGBAR | WFLG_ACTIVATE,
		WA_PubScreen, screen,
		WINDOW_Position, WPOS_CENTERSCREEN,
		WINDOW_SharedPort, msg_port,
		WINDOW_Layout, LayoutObject,
			GA_DrawInfo, draw_info,
			LAYOUT_DeferLayout, TRUE,
			LAYOUT_Orientation, LAYOUT_ORIENT_VERT,
			LAYOUT_HorizAlignment, LAYOUT_ALIGN_CENTER,
			LAYOUT_SpaceOuter, TRUE,
			LAYOUT_FillPen, draw_info->dri_Pens[SHINEPEN],
			LAYOUT_FillPattern, fill_pattern,
			LAYOUT_AddChild, LayoutObject,
				LAYOUT_Orientation, LAYOUT_ORIENT_VERT,
				LAYOUT_BevelStyle, BVS_THIN,
				LAYOUT_BevelState, IDS_SELECTED,
				LAYOUT_SpaceOuter, TRUE,
				LAYOUT_AddImage, LabelObject,
					LABEL_Text, GetString(MSG_ABOUT_MESSAGE),
				LabelEnd,
			LayoutEnd,
			LAYOUT_AddChild, ButtonObject,
				GA_ID, GADGET_OK,
				GA_Text, GetString(MSG_OK_BUTTON),
				GA_RelVerify, TRUE,
			ButtonEnd,
			CHILD_NominalSize, TRUE,
			CHILD_WeightedWidth, 0,
		LayoutEnd,
	WindowEnd;

	if ( this->about_window == NULL )  {
		D2(bug(" cannot create about window object\n"));
		deleteAboutReq(this);
		return(NULL);
	}

	/*** Open the completed window for the user ***/
	this->window = (struct Window*)DoMethod(this->about_window, WM_OPEN);
	if ( this->window == NULL )  {
		D2(bug(" cannot open about window\n"));
		deleteAboutReq(this);
		return(NULL);
	}

	return(this);
}


/*
 * deleteAboutReq -- Delete AboutReq object
 *
 * Deletes an AboutReq object.
 */
VOID deleteAboutReq(AboutReq this)
{
	D(bug("deleteAboutReq(%08lx)\n", this));

	if ( this == NULL )
		return;

	DisposeObject(this->about_window);
	FreeVec(this);
}


/*
 * handleAboutReqSignal -- Handle AboutReq signal
 *
 * Handles any messages waiting for the AboutReq object and takes the
 * appropriate action.  Returns TRUE if the requester has been confirmed
 * or FALSE if not.
 */
BOOL handleAboutReqSignal(AboutReq this)
{
	ULONG class, result;
	WORD code;
	BOOL done;

	D(bug("handleAboutReqSignal(%lx)\n", this));

	if ( this == NULL )
		return(FALSE);

	done = FALSE;
	result = DoMethod(this->about_window, WM_HANDLEINPUT, &code);
	while ( result != WMHI_LASTMSG && !done )  {
		class = result & WMHI_CLASSMASK;
		switch ( class )  {
			case WMHI_GADGETUP:
				done = TRUE;	/* there's only one button */
				break;
			default:
				D2(bug(" unknown class=%lx code=%lx\n", class, code));
		}

		result = DoMethod(this->about_window, WM_HANDLEINPUT, &code);
	}

	return(done);
}
