#include "includes.h"
#include "structs.h"
#include <datatypes/datatypesclass.h>
#include <datatypes/datatypes.h>
#include <intuition/classes.h>

extern struct Library       *ShutdownBase;
extern struct Library       *ScreenNotifyBase;
extern struct Library       *RetinaBase;
extern struct Library       *TimerBase;
extern struct Library       *DataTypesBase;
extern struct IntuitionBase *IntuitionBase;
extern struct GfxBase       *GfxBase;
extern struct ExecBase      *SysBase;

struct Gadget my_drag=
{
    NULL,
    0,1,
    0,0,
    GFLG_GADGHNONE,
    GACT_IMMEDIATE,
    GTYP_SYSGADGET|GTYP_WDRAGGING,
    0,
    0,
    0,

    0,
    0,
    2,
    0
};

struct Gadget my_close=
{
    NULL,
    0,0,
    16,16,
    NULL,
    GACT_IMMEDIATE,
    GTYP_SYSGADGET|GTYP_CLOSE,
    0,
    0,
    0,

    0,
    0,
    1,
    0
};

struct Gadget my_size=
{
    NULL,
    0,0,
    16,16,
    GFLG_GADGHNONE,
    GACT_IMMEDIATE,
    GTYP_SYSGADGET|GTYP_SIZING,
    0,
    0,
    0,

    0,
    0,
    3,
    0
};

extern struct tm_settings dftm;

extern UBYTE             numofvols;
extern char             *LoadedVolList[64],
                        *LoadedVolList_1[64];
extern struct DateTime   dt;

UBYTE                    numoflines;
struct Window           *my_window=0L;
struct TextFont         *my_text_font=0L,
                        *my_clock_font_1=0L,
                        *my_clock_font_2=0L;
struct RastPort         *my_rastport=0L,
                         my_rp,
                         my_rp_2;
struct BitMap           *my_bitmap=0L,
                        *my_b1=0L,
                        *my_b2=0L,
                        *my_b=0L;
struct BitMapHeader     *my_header=0L;
struct Screen           *my_pub_screen=0L;
Object                  *my_dtobject=0L;
UWORD                    yypos[32];
UWORD                    xxpos[32];
UBYTE                    were_on_public;
ULONG                    gauge_x_pos,
                         gauge_y_pos_2,
                         gauge_x_size,
                         gauge_y_size,
                         info_x_size,
                         texty,
                         textdelta,
                         space,
                         space_x=0L,
                         space_s=0L;
UBYTE                    were_on_public;
char                    *pubscreenadress=0L;
extern ULONG             my_window_mask;
BOOL                     InitIdle;


renew_temp_bitmaps(WORD width, WORD height)
{
    if(my_b1){FreeBitMap(my_b1);my_b1=0L;};
    if(my_b2){FreeBitMap(my_b2);my_b2=0L;};

    if(!(my_b1=(struct BitMap *)AllocBitMap(width,height+textdelta,my_pub_screen->RastPort.BitMap->Depth,BMF_CLEAR|BMF_DISPLAYABLE,0L)))cleanexit();
    if(!(my_b2=(struct BitMap *)AllocBitMap(width,height+textdelta,my_pub_screen->RastPort.BitMap->Depth,BMF_CLEAR|BMF_DISPLAYABLE,0L)))cleanexit();

    InitRastPort(&my_rp);   my_rp.BitMap  =(struct BitMap *)my_b1;
    InitRastPort(&my_rp_2); my_rp_2.BitMap=(struct BitMap *)my_b2;
}

dogetwidesttext(char *text)
{
    int dummy_1;
    if((dummy_1=TextLength(my_rastport,text,my_strlen(text))+4+BSIZX)>gauge_x_pos) gauge_x_pos=dummy_1;
}

/*

#define MAX_SERS 8

struct MsgPort  *SerialMP[MAX_SERS];         
struct IOExtSer *SerialIO[MAX_SERS];    
BOOL   device_present[MAX_SERS];

init_serial()
{
    int k;
    for(k=0;k<MAX_SERS;k++)
    {
        device_present[k]=FALSE;
        if(dftm.Devices[0][k]!=0)
        {
            if (SerialMP[k]= CreatePort(0,0) )
            {
                if (SerialIO[k]=(struct IOExtSer *) CreateExtIO(SerialMP[k],sizeof(struct IOExtSer)))
                {
                    SerialIO[k]->io_SerFlags=SERF_SHARED;
                    if (!OpenDevice(&dftm.Devices[k][0],0L,(struct IORequest *)SerialIO[k],0) )device_present[k]=TRUE;
                }
            }
        }
    }
}

free_serial()
{
    int k;
    for(k=0;k<MAX_SERS;k++)
    {
        if(SerialIO[k])
        {
            if (device_present[k])
            {
                CloseDevice((struct IORequest *)SerialIO[k]);
                SerialIO[k]=0L;
            }
            DeleteExtIO((struct IORequest *)SerialIO[k]);
        }
        if(SerialMP[k])
        {
            DeletePort(SerialMP[k]);
            SerialMP[k]=0L;
        }
    }
}

*/

open_new_window(WORD x, WORD y, WORD xsiz, WORD ysiz)
{
    if(!(my_window=(struct Window *) OpenWindowTags(0,
                                                    WA_Left,            x,
                                                    WA_Top,             y,
                                                    WA_Width,           xsiz,
                                                    WA_AutoAdjust,      TRUE,
                                                    WA_MaxWidth,        28800,
                                                    WA_MaxHeight,       ysiz,
                                                    WA_MinHeight,       ysiz,
                                                    WA_Height,          ysiz,
                                                    WA_MinWidth,        50,
                                                    WA_Flags,           WFLG_BORDERLESS|WFLG_SMART_REFRESH|WFLG_REPORTMOUSE|WFLG_RMBTRAP,
                                                    WA_PubScreenName,   pubscreenadress,
                                                    WA_Title,           0,
                                                    TAG_DONE
                                                   ))) {exit(1);};

    my_drag.Width=   my_window->Width;
    my_drag.Height=  my_window->Height-17;
    my_size.TopEdge= my_window->Height-16;
    my_size.LeftEdge=my_window->Width -16;
    if(dftm.WindowMove<2)
    {
        AddGadget(my_window,&my_drag, (UWORD)0);
        AddGadget(my_window,&my_size, (UWORD)0);
        AddGadget(my_window,&my_close,(UWORD)0);
    }
    ModifyIDCMP(my_window,IDCMP_NEWSIZE|IDCMP_CLOSEWINDOW|IDCMP_CHANGEWINDOW|IDCMP_VANILLAKEY|IDCMP_MOUSEBUTTONS|IDCMP_SIZEVERIFY);
    my_rastport=my_window->RPort;
    SetFont(my_rastport,my_text_font);
    SetDrMd(my_rastport,JAM1);
    my_window_mask  = 1L << my_window->UserPort->mp_SigBit;
    info_x_size     = ((my_window->Width)/dftm.colums)-(BSIZX<<1);
    if((dftm.back_pic==1)&&(DataTypesBase)) load_background();
}

get_min_size()
{
    struct IntuiMessage *m;
    if(dftm.DisplayDIGITAL | dftm.DisplayDATE)
    if(my_window->Width<MAX(my_clock_font_1->tf_XSize*8+my_clock_font_2->tf_XSize*9+32,gauge_x_pos+64))
    {
        ChangeWindowBox(my_window,my_window->LeftEdge,my_window->TopEdge,MAX(32+my_clock_font_1->tf_XSize*8+my_clock_font_2->tf_XSize*9,gauge_x_pos+64),my_window->Height);
        for(;;)
        {
            m=(struct IntuiMessage *)GetMsg( my_window->UserPort );
            if(m->Class==IDCMP_CHANGEWINDOW) break;
        }
        renew_temp_bitmaps(my_window->Width,my_window->Height);
        my_drag.Width   =my_window->Width;
        my_drag.Height  =my_window->Height-17;
        my_size.TopEdge =my_window->Height-16;
        my_size.LeftEdge=my_window->Width-16;
    }
    my_window->MinWidth=MAX(dftm.DisplayDIGITAL | dftm.DisplayDATE ? my_clock_font_1->tf_XSize*8+my_clock_font_2->tf_XSize*9+32 : 0,gauge_x_pos+64);
    my_window->MinWidth=MAX(((gauge_x_pos+BSIZX+32)*dftm.colums),my_window->MinWidth);
    if(my_window->Width<((gauge_x_pos+BSIZX+32)*dftm.colums))
    {
        ChangeWindowBox(my_window,my_window->LeftEdge,my_window->TopEdge,((gauge_x_pos+BSIZX+32)*dftm.colums),my_window->Height);
        for(;;)
        {
            m=(struct IntuiMessage *)GetMsg( my_window->UserPort );
            if(m->Class==IDCMP_CHANGEWINDOW) break;
        }
        renew_temp_bitmaps(my_window->Width,my_window->Height);
        my_drag.Width   =my_window->Width;
        my_drag.Height  =my_window->Height-17;
        my_size.TopEdge =my_window->Height-16;
        my_size.LeftEdge=my_window->Width-16;
    }
}

open_window()
{
    UBYTE k;
    ULONG dummy_1;
    WORD  win_height;

    pubscreenadress=(char *)&dftm.pubscreen_name[0];
    if( my_pub_screen=(struct Screen *)LockPubScreen(pubscreenadress))
    {
        were_on_public=TRUE;
    }
    else
    {
        pubscreenadress=0L;
        my_pub_screen=IntuitionBase->FirstScreen;
        were_on_public=FALSE;
    }

    if(dftm.DisplayDIGITAL|dftm.DisplayDATE)
    {
         dummy_1=MAX(my_clock_font_1->tf_YSize,my_clock_font_2->tf_YSize)+BSPC;
    }
    else dummy_1=0L;

    numoflines=0;
    if(dftm.DisplayAll)    numoflines++;
    if(dftm.DisplayChip)   numoflines++;
    if(dftm.DisplayFast)   numoflines++;
    if(dftm.DisplayRetina) numoflines++;
    if(dftm.DisplayInfo_1) numoflines++;
    if(dftm.DisplayInfo_2) numoflines++;
    if(dftm.DisplayIdle)   numoflines++;

/*    if(dftm.DisplayCarrier)numoflines++;*/
    numoflines+=numofvols;

    win_height=(((gauge_y_size))*(((numoflines-1)/dftm.colums)+1))+(BSIZY*2)+(BSPC*((numoflines-1)/dftm.colums))+2+dummy_1;

    renew_temp_bitmaps(dftm.x_siz,win_height);

    open_new_window(dftm.x_pos,dftm.y_pos,dftm.x_siz,win_height);

    gauge_x_pos=0;
    if(dftm.DisplayAll)    dogetwidesttext(&dftm.AllText[0]);
    if(dftm.DisplayChip)   dogetwidesttext(&dftm.ChipText[0]);
    if(dftm.DisplayFast)   dogetwidesttext(&dftm.FastText[0]);
    if(dftm.DisplayRetina) dogetwidesttext(&dftm.RetinaText[0]);
    for(k=0;k<numofvols;k++) dogetwidesttext(LoadedVolList[k]);

    get_min_size();
    gauge_x_size    = ((my_window->Width)/dftm.colums)-gauge_x_pos-BSIZX;
    for(k=0;k<dftm.colums;k++) xxpos[k]=(gauge_x_pos)*(k+1)+((BSIZX+gauge_x_size)*k)+1;
}

load_font()
{
    struct  TextAttr      my_text_attr;
    UBYTE   k;
    UWORD   dummy;
    struct  TextAttr      topaz_font =
    {
        "topaz.font",
        8,
        0,
        FPF_ROMFONT,
    };

    if(dftm.DisplayDIGITAL|dftm.DisplayDATE)
    {
        my_text_attr.ta_Name =(UBYTE *)&dftm.Clockfont_name[0];
        my_text_attr.ta_YSize=(UWORD)dftm.Clockfont_size;
        my_text_attr.ta_Flags=FPF_DISKFONT;
        my_clock_font_1=(struct TextFont *) OpenDiskFont( &my_text_attr );

        my_text_attr.ta_Name =(UBYTE *)&dftm.Clocksmallfont_name[0];
        my_text_attr.ta_YSize=(UWORD)dftm.Clocksmallfont_size;
        my_text_attr.ta_Flags=FPF_DISKFONT;
        my_clock_font_2=(struct TextFont *) OpenDiskFont( &my_text_attr );

        if(!my_clock_font_1)dftm.DisplayDIGITAL=FALSE;
        if(!my_clock_font_2)dftm.DisplayDATE=FALSE;
    }

    my_text_attr.ta_Name =(UBYTE *)&dftm.font_name[0];
    my_text_attr.ta_YSize=(UWORD)dftm.font_size;
    my_text_attr.ta_Flags=FPF_DISKFONT | FPB_PROPORTIONAL;

    my_text_font=(struct TextFont *)OpenDiskFont((struct TextAttr *)&my_text_attr );
    if(!my_text_font)
    {
        my_text_font=(struct TextFont *) OpenFont( &topaz_font );
        if(!my_text_font) { cleanexit();}
    }
    gauge_y_size    = ((my_text_font->tf_YSize)*(100+dftm.y_siz))/100;
    texty           = ((gauge_y_size-my_text_font->tf_YSize)>>1)+my_text_font->tf_Baseline;
    textdelta       = my_text_font->tf_YSize-my_text_font->tf_Baseline+1;
    dftm.BorderY    = dftm.BorderY < textdelta ? textdelta : dftm.BorderY;
    space           = BSPC+(gauge_y_size);
    for(k=0;k<32;k++)
        yypos[k]    = (UWORD)space*k + ( ( dftm.ClockIndent & 16 ) ? MAX( my_clock_font_1 ? my_clock_font_1->tf_YSize + BSPC : 0,my_clock_font_2 ? my_clock_font_2->tf_YSize + BSPC : 0) : 0 );
    gauge_y_pos_2   = BSIZY-1+(gauge_y_size);
}

load_background()
{
    if (my_dtobject = (Object *)NewDTObject(&dftm.background_picture[0],
                                            DTA_SourceType       ,DTST_FILE,
                                            DTA_GroupID          ,GID_PICTURE,
                                            PDTA_Remap           ,TRUE,
                                            PDTA_Screen          ,my_window->WScreen,
                                            PDTA_FreeSourceBitMap,TRUE,
                                            OBP_Precision        ,PRECISION_IMAGE,
                                            TAG_DONE))
    {
        if ( DoDTMethod(my_dtobject,NULL,NULL,DTM_PROCLAYOUT,NULL,1))
        {
            GetDTAttrs(my_dtobject,PDTA_BitMapHeader,&my_header,PDTA_DestBitMap,&my_bitmap,TAG_DONE);
            if (my_bitmap==FALSE) GetDTAttrs(my_dtobject,PDTA_BitMap,&my_bitmap,TAG_DONE);
        }
    }
}

free_background()
{
    if(my_dtobject) DisposeDTObject((Object *)my_dtobject);
    my_dtobject=0L; my_bitmap=0L;
}

new_window_size()
{
    renew_temp_bitmaps(my_window->Width,my_window->Height);
    my_drag.Width   =my_window->Width;
    my_drag.Height  =my_window->Height-17;
    my_size.TopEdge =my_window->Height-16;
    my_size.LeftEdge=my_window->Width -16;
}

free_all()
{
    if(were_on_public)  UnlockPubScreen (0,my_pub_screen);
    if(my_window)
    {
        RemoveGList(my_window,my_window->FirstGadget,-1);
        CloseWindow     (my_window);
    }
    if(my_text_font)    CloseFont       (my_text_font);
    if(my_clock_font_1) CloseFont       (my_clock_font_1);
    if(my_clock_font_2) CloseFont       (my_clock_font_2);
    if(my_b1)           FreeBitMap      (my_b1);
    if(my_b2)           FreeBitMap      (my_b2);
    if(my_bitmap)       free_background();
    my_clock_font_2 =0L;
    my_clock_font_1 =0L;
    my_text_font    =0L;
    my_window       =0L;
    my_b1           =0L;
    my_b2           =0L;
    freevols();
/*    free_serial();*/
}

cleanexit()
{
    free_all();
    exit(1);
}

makeall()
{
    if(!InitIdle & dftm.DisplayIdle)
    {
        init_idle();
        InitIdle=TRUE;
    }
    loadvols();
    getvolsizes(0);
    getvolsizes(1);
    getvolsizes(2);
    load_font();
    open_window();
    draw_borders_and_background();
    get_avail_ram_and_display(2,numofvols);
    DateStamp(&dt.dat_Stamp);
    DisplayCLOCK(&dt,((numoflines-1)/dftm.colums)+1);
}


