/*
 *  Source generated with GadToolsBox V1.3
 *  which is (c) Copyright 1991,92 Jaba Development
 */

#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>

#define FULL 65536
#define FRAC 8192

struct Screen        *Scr = NULL;
APTR                  VisualInfo = NULL;
struct Window        *Recusive_DemoWnd = NULL;
struct Menu          *Recusive_DemoMenus = NULL;
UWORD                 Recusive_DemoLeft = 10;
UWORD                 Recusive_DemoTop = 10;
UWORD                 Recusive_DemoWidth = 260;
UWORD                 Recusive_DemoHeight = 260;
UBYTE                *Recusive_DemoWdt = "Recursive Demo";
struct TextAttr      *Font;
UWORD                 FontSize;
UWORD                 OffX, OffY;
struct TextAttr       Topaz80 = {
    (STRPTR)"topaz.font", TOPAZ_EIGHTY, FS_NORMAL, FPF_ROMFONT };

struct NewMenu Recusive_DemoNewMenu[] = {
    NM_TITLE, (UBYTE *)"Image", NULL, 0, 0, NULL,
    NM_ITEM, (UBYTE *)"Box", NULL, 0, 0, NULL,
    NM_ITEM, (UBYTE *)"Border", NULL, 0, 0, NULL,
    NM_ITEM, (UBYTE *)"Box-Border", NULL, 0, 0, NULL,
    NM_ITEM, (UBYTE *)"Border-Box", NULL, 0, 0, NULL,
    NM_ITEM, (UBYTE *)NM_BARLABEL, NULL, 0, NULL, NULL,
    NM_ITEM, (UBYTE *)"Color", NULL, CHECKIT|MENUTOGGLE, 0, NULL,
    NM_END, NULL, NULL, 0, NULL, NULL };

static void ComputeFont( UWORD width, UWORD height )
{
    Font  = Scr->Font;
    FontSize = Font->ta_YSize;

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

    if ( width && height ) {
        if (( FontSize * ( width * FRAC ) / FULL + OffX + Scr->WBorRight ) > Scr->Width )
            goto UseTopaz;
        if (( FontSize * ( height * FRAC ) / FULL + OffY + Scr->WBorBottom ) > Scr->Height )
            goto UseTopaz;
    }
    return;

UseTopaz:
    Font = &Topaz80;
    FontSize = 8;
}

long SetupScreen( void )
{
    if ( NOT( Scr = LockPubScreen( NULL )))
        return( 1L );

    ComputeFont( NULL, NULL );

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

    return( NULL );
}

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

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

long OpenRecusive_DemoWindow( void )
{
    UWORD               wleft = Recusive_DemoLeft, wtop = Recusive_DemoTop, ww, wh;

    ComputeFont( Recusive_DemoWidth, Recusive_DemoHeight );

    ww = ( FontSize * ( Recusive_DemoWidth * FRAC )) /  FULL;
    wh = ( FontSize * ( Recusive_DemoHeight * FRAC )) / FULL;

    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 ( NOT( Recusive_DemoMenus = CreateMenus( Recusive_DemoNewMenu, GTMN_FrontPen, NULL, TAG_DONE )))
        return( 3L );

    LayoutMenus( Recusive_DemoMenus, VisualInfo, TAG_DONE );
    if ( NOT( Recusive_DemoWnd = OpenWindowTags( NULL,
                    WA_Left,          wleft,
                    WA_Top,           wtop,
                    WA_Width,         ( FontSize * ( Recusive_DemoWidth * FRAC )) / FULL + OffX + Scr->WBorRight,
                    WA_Height,        ( FontSize * ( Recusive_DemoHeight * FRAC )) / FULL + OffY + Scr->WBorBottom,
                    WA_IDCMP,         IDCMP_CLOSEWINDOW|IDCMP_REFRESHWINDOW|MENUPICK,
                    WA_Flags,         WFLG_DRAGBAR|WFLG_DEPTHGADGET|WFLG_CLOSEGADGET|WFLG_SMART_REFRESH|WFLG_GIMMEZEROZERO,
                    WA_Title,         Recusive_DemoWdt,
                    WA_ScreenTitle,   "© '92 by G.Klauberg@innoTEC1",
                    WA_PubScreen,     Scr,
                        TAG_DONE )))
        return( 4L );

    SetMenuStrip( Recusive_DemoWnd, Recusive_DemoMenus );
    GT_RefreshWindow( Recusive_DemoWnd, NULL );

    return( NULL );
}

void CloseRecusive_DemoWindow( void )
{
    if ( Recusive_DemoMenus      ) {
        ClearMenuStrip( Recusive_DemoWnd );
        FreeMenus( Recusive_DemoMenus );
        Recusive_DemoMenus = NULL;    }

    if ( Recusive_DemoWnd        ) {
        CloseWindow( Recusive_DemoWnd );
        Recusive_DemoWnd = NULL;
    }
}

