/* dialt.c -- test for dial gadget routines	*/

/*
Copyright (c) 1989 Commodore-Amiga, Inc.

Executables based on this information may be used in software
for Commodore Amiga computers. All other rights reserved.
This information is provided "as is"; no warranties are made.
All use is at your own risk, and no liability or responsibility
is assumed.
*/

#include "sysall.h"



struct  Window * getNewWind(SHORT left, SHORT top, SHORT width, SHORT height,struct Screen	*screen,ULONG   flg, ULONG iflg);
struct Gadget	*CreateDialGadget(int id,struct Image	*im);
void DeleteDialGadget(struct Gadget	*g);
BOOL SetDialCoords(struct Gadget	*g,int x, int y);
void GetDialCoords(struct Gadget	*g,WORD	*xp,WORD	*yp);
void drawNeedle( struct RastPort *rp, struct Gadget *g, int left, int top );
void normalizeDialCoords(struct Gadget	*g);
ULONG dial_Dispatch(struct Hook	*hook,struct Gadget	*g,ULONG		*msg);
ULONG dial_render(struct Gadget	*g,struct gpRender	*msg);
ULONG dial_realrender(struct Gadget		*g,struct GadgetInfo	*gi,struct RastPort		*rp,LONG			redraw);
ULONG dial_handleinput(struct Gadget	*g,struct gpInput	*msg);

struct  Library  *IntuitionBase = NULL;
struct  Library  *GfxBase = NULL;

ULONG   flg = WINDOWCLOSE | NOCAREREFRESH | WINDOWDRAG
    | WINDOWDEPTH | SIMPLE_REFRESH;

ULONG   iflg =	GADGETUP | GADGETDOWN |
		RAWKEY | MOUSEMOVE | MOUSEBUTTONS | CLOSEWINDOW;

#define DIALID	(0xdead)	/* gadgetid for dial gadget	*/

void main(void)
{
    struct  IntuiMessage	*msg;
    struct Window   	*window = NULL;
    WORD    		exitval = 0;
    struct Image	*CreateEllipseImage();
    struct Image	*ellipseimage = NULL;
    struct Gadget	*CreateDialGadget();
    struct Gadget	*dg;
    WORD		dialx, dialy;

    /* hold data from *msg  */
    ULONG   		class;
    USHORT  		code;
    APTR    		iaddr;
    UWORD		qualifier;
    Point		mouse;

    if ((IntuitionBase = OpenLibrary("intuition.library", 36L)
	) == NULL)
    {
	printf("NO INTUITION LIBRARY\n");
	exitval = 1;
	goto EXITING;
    }

    if ((GfxBase = OpenLibrary("graphics.library", 36L)
	) == NULL)
    {
	printf("NO GRAPHICS LIBRARY\n");
	exitval = 2;
	goto EXITING;
    }

    /* init libraries and window    */
    window = getNewWind(400, 120, 160, 90, NULL, flg, iflg);
    if (window == NULL)
    {
	exitval = 1;
	goto EXITING;
    }

    if ( ! (ellipseimage =  CreateEllipseImage( 100, 50, 2, 2, 3, 2, 4 )))
    {
    	exitval = 1;
	goto EXITING;
    }

    if ( dg = CreateDialGadget( DIALID, ellipseimage ) )
    {
	/* finish the setup of the gadget	*/
	dg->LeftEdge = 30;
	dg->TopEdge = 20;
	dg->Activation |= GADGIMMEDIATE | RELVERIFY;

	AddGadget( window, dg, (long) ~0 );
	RefreshGList( dg, window, NULL, 1L );
    }
    else
    {
    	printf( "create dialg failed\n");
    }

    printf("test program ok\n");

    FOREVER
    {
	if ((msg = (struct IntuiMessage *)GetMsg(window->UserPort)) == NULL)
	{
	    Wait(1L<<window->UserPort->mp_SigBit);
	    continue;
	}

	/* Stash message contents and reply,
	 * important when message triggers some
	 * lengthy processing
	 */

	class   = msg->Class;
	code    = msg->Code;
	iaddr   = msg->IAddress;
	qualifier = msg->Qualifier;
	mouse = *( (Point *) &msg->MouseX );

	ReplyMsg((struct Message *)msg);

	switch (class)
	{
	case GADGETUP:
	    printf(" gadgetup received\n");
	    if ( ((struct Gadget *) iaddr)->GadgetID == DIALID )
	    {
	    	printf(" dial gadget has been used.\n");
		GetDialCoords( iaddr, &dialx, &dialy );
		printf("coords are: %d/%d\n", dialx, dialy );
	    }
	    else printf( "unrecognized gadget message\n" );

	    break;

	case CLOSEWINDOW:
	    goto EXITING;
	}
    }

EXITING:
    if ( dg )
    {
	RemoveGadget( window, dg );
	DeleteDialGadget( dg );
    }
    DeleteImage( ellipseimage );

    if (window)
    {
    	CloseWindow(window);
    }

    if (GfxBase) CloseLibrary(GfxBase);
    if (IntuitionBase) CloseLibrary(IntuitionBase);

    exit (exitval);
}


struct  Window * getNewWind(SHORT left, SHORT top, SHORT width, SHORT height,struct Screen	*screen,ULONG   flg, ULONG iflg)
{
    struct  Window  *OpenWindow();
    struct  NewWindow   nw;

    nw.LeftEdge =   (SHORT) left;
    nw.TopEdge  =   (SHORT) top;
    nw.Width    =   (SHORT) width;
    nw.Height   =   (SHORT) height;
    nw.DetailPen    =   (UBYTE) -1;
    nw.BlockPen =   (UBYTE) -1;
    nw.IDCMPFlags   =   (ULONG) iflg;

    nw.Flags    =   (ULONG) flg;

    nw.FirstGadget  =   (struct Gadget *)   NULL;
    nw.CheckMark    =   (struct Image *)    NULL;
    nw.Title    =   (UBYTE *)   " Dial Test ";
    nw.Screen   =   screen;
    nw.BitMap   =   (struct BitMap *)   NULL;
    nw.MinWidth =   (SHORT) 50;
    nw.MinHeight=   (SHORT) 30;
    /* work around bug  */
    nw.MaxWidth =   (SHORT) nw.Width;
    nw.MaxHeight    =   (SHORT) nw.Height;
    nw.Type     =   (USHORT) (screen? CUSTOMSCREEN: WBENCHSCREEN);

    return ((struct Window *) OpenWindow(&nw));
}
