/*****$Def*GadToolsBox V1.3*Exp*0.01**************************************************
**	___000014
**
**	$Author$
**	$Locker : J_Santini $
**      $Date : 28/06/1992 22:29:32 $
**	$Source : Workbench:System/Sources/CData/CS/GaspIII/GaspWin.c $
**	$Revision : 0.01 $
**	$State : Exp $
**	$Translator : SAS/C V5.10a AmigaDOS $
**
**	$Log : GaspWin.c $
** Revision 0.01 28/06/1992 22:29:32 J_Santini ( SAS/C V5.10a AmigaDOS )
**	Source generated with GadToolsBox V1.3
**  which is (c) Copyright 1991,92 Jaba Development.
**  Puis modifié à la main.
** 
*******************************************************************************/

#include <exec/types.h>
#include <intuition/intuition.h>
#include <intuition/classes.h>
#include <intuition/classusr.h>
#include <intuition/imageclass.h>
#include <intuition/gadgetclass.h>
#include <libraries/gadtools.h>
#include <graphics/displayinfo.h>
#include <graphics/gfxbase.h>
#include <clib/exec_protos.h>
#include <clib/intuition_protos.h>
#include <clib/gadtools_protos.h>
#include <clib/graphics_protos.h>
#include <string.h>

#include <pragmas/exec_pragmas.h>
#include <pragmas/intuition_pragmas.h>
#include <pragmas/gadtools_pragmas.h>
#include <pragmas/graphics_pragmas.h>

#include "GaspWin.h"
#include "GaspIII.h"

struct Screen        *Scr = NULL;
APTR                  VisualInfo = NULL;
struct Window        *BrWnd = NULL;
struct Gadget        *BrGList = NULL;
struct Gadget        *BrGadgets[5];
UWORD                 BrLeft = 212;
UWORD                 BrTop = 82;
UWORD                 BrWidth = 91;
UWORD                 BrHeight = 84 ;
UBYTE                *BrWdt = (UBYTE *)"GaspIII";
struct TextAttr      *Font, Attr;
UWORD                 FontX, FontY;
UWORD                 OffX, OffY;

UBYTE         *X_MX0Labels[] = {
    (UBYTE *)"4",
    (UBYTE *)"6",
    (UBYTE *)"8",
    NULL };

static UWORD ComputeX( UWORD value )
{
    return(( UWORD )(( FontX * value ) / 8 ));
}

static UWORD ComputeY( UWORD value )
{
    return(( UWORD )(( FontY * value ) / 8 ));
}

static void ComputeFont( UWORD width, UWORD height )
{
    Font = &Attr;
    Font->ta_Name = Scr->RastPort.Font->tf_Message.mn_Node.ln_Name;
    Font->ta_YSize = FontY = Scr->RastPort.Font->tf_YSize;
    FontX = Scr->RastPort.Font->tf_XSize;

    OffY = FontY + Scr->WBorTop + 1;
    OffX = Scr->WBorLeft;

    if ( width && height ) {
        if (( ComputeX( width ) + OffX + Scr->WBorRight ) > Scr->Width )
            goto UseTopaz;
        if (( ComputeY( height ) + OffY + Scr->WBorBottom ) > Scr->Height )
            goto UseTopaz;
    }
    return;

UseTopaz:
    Font->ta_Name = (STRPTR)"topaz.font";
    FontX = FontY = Font->ta_YSize = 8;
}

int SetupScreen( void )
{
    if ( ! ( Scr = LockPubScreen(0L )))
        return( 1L );

    ComputeFont( 0L, 0L );

    if ( ! ( VisualInfo = GetVisualInfo( Scr, TAG_DONE )))
        return( 2L );

    return( 0L );
}

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

    if ( Scr        ) {
        UnlockPubScreen( NULL, Scr );
        Scr = NULL;
    }
}

int OpenBrWindow( void )
{
    struct NewGadget     ng;
    struct Gadget       *g;
    UWORD               wleft =prefs.Left,wtop = prefs.Top , ww, wh;

    ComputeFont( BrWidth, BrHeight );

    ww = ComputeX( BrWidth );
    wh = ComputeY( BrHeight );

    if (( wleft + ww + OffX + Scr->WBorRight ) > Scr->Width ) wleft = Scr->Width - ww;
    if (( wtop + wh + OffY + Scr->WBorBottom ) > Scr->Height ) wtop = Scr->Height - wh;

    if ( ! ( g = CreateContext( &BrGList )))
        return( 1L );

    ng.ng_LeftEdge        =    OffX + ComputeX( 6 );
    ng.ng_TopEdge         =    OffY + ComputeY( 2 );
    ng.ng_Width           =    ComputeX( 79 );
    ng.ng_Height          =    ComputeY( 12 );
    ng.ng_GadgetText      =    (UBYTE *)"_Retry";
    ng.ng_TextAttr        =    Font;
    ng.ng_GadgetID        =    GD_RETRY;
    ng.ng_Flags           =    PLACETEXT_IN;
    ng.ng_VisualInfo      =    VisualInfo;

    g = CreateGadget( BUTTON_KIND, g, &ng, GT_Underscore, '_', TAG_DONE );

    BrGadgets[ 0 ] = g;

    ng.ng_TopEdge         =    OffY + ComputeY( 15 );
    ng.ng_GadgetText      =    (UBYTE *)"_New";
    ng.ng_GadgetID        =    GD_NEW;

    g = CreateGadget( BUTTON_KIND, g, &ng, GT_Underscore, '_', TAG_DONE );

    BrGadgets[ 1 ] = g;

    ng.ng_TopEdge         =    OffY + ComputeY( 28 );
    ng.ng_GadgetText      =    (UBYTE *)"_Clear";
    ng.ng_GadgetID        =    GD_CLEAR;

    g = CreateGadget( BUTTON_KIND, g, &ng, GT_Underscore, '_', TAG_DONE );

    BrGadgets[ 2 ] = g;

    ng.ng_TopEdge         =    OffY + ComputeY( 41 );
    ng.ng_GadgetText      =    (UBYTE *)"_About";
    ng.ng_GadgetID        =    GD_ABOUT;

    g = CreateGadget( BUTTON_KIND, g, &ng, GT_Underscore, '_', TAG_DONE );

    BrGadgets[ 3 ] = g;

    ng.ng_LeftEdge        =    OffX + ComputeX( 26 );
    ng.ng_TopEdge         =    OffY + ComputeY( 55 );
    ng.ng_GadgetText      =    NULL;
    ng.ng_GadgetID        =    GD_X_MX;
    ng.ng_Flags           =    PLACETEXT_RIGHT;

    g = CreateGadget( MX_KIND, g, &ng, GTMX_Labels, &X_MX0Labels[0],  GTMX_Active,prefs.X,TAG_DONE );

    BrGadgets[ 4 ] = g;

    if ( ! g )
        return( 2L );

    if ( ! ( BrWnd = OpenWindowTags( NULL,
                    WA_Left,          wleft,
                    WA_Top,           wtop,
                    WA_Width,         ww + OffX + Scr->WBorRight,
                    WA_Height,        wh + OffY + Scr->WBorBottom,
                    WA_IDCMP,         BUTTONIDCMP|MXIDCMP|IDCMP_MOUSEBUTTONS|IDCMP_CLOSEWINDOW|IDCMP_ACTIVEWINDOW|IDCMP_REFRESHWINDOW|IDCMP_VANILLAKEY|IDCMP_CHANGEWINDOW,
                    WA_Flags,         WFLG_DRAGBAR|WFLG_DEPTHGADGET|WFLG_CLOSEGADGET|WFLG_SMART_REFRESH|WFLG_REPORTMOUSE|WFLG_ACTIVATE|WFLG_RMBTRAP,
                    WA_Gadgets,       BrGList,
                    WA_Title,         BrWdt,
                    WA_ScreenTitle,   CopyRight ,
                    WA_Zoom,          &(prefs.Zoom),
                    TAG_DONE )))
        return( 4L );

    prefs.Zoom[0] = BrWnd->LeftEdge;
    prefs.Zoom[1] = BrWnd->TopEdge;
    prefs.Zoom[2] = BrWnd->Width;
    prefs.Zoom[3] = BrWnd->Height;

    GT_RefreshWindow( BrWnd, NULL );

    return( 0L );
}

void CloseBrWindow( void )
{
    if ( BrWnd        ) {
        CloseWindow( BrWnd );
        BrWnd = NULL;
    }

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

