/**************************************************************************/
/*                                                                        */
/*  Multitool II   -   DOS-Hilfsprogramm                Version 1.4r000   */
/*                                                                        */
/*  ©1993 Boris Jakubaschk                                                */
/*                                                                        */
/*  Modul 3 - Requester: Alle Übrigen Requester (Stringeingabe...)        */
/*                                                                        */
/**************************************************************************/

#include "MToolDefs.h"
#include "BreadPic.h"

UWORD FillPattern[2] = { 0x5555, 0xAAAA };

struct TagItem    SreqWindowTags[] = {
    WA_Left,          142,
    WA_Top,           90,
    WA_Width,         357,
    WA_Height,        76,
    WA_IDCMP,         IDCMP_GADGETUP|IDCMP_CLOSEWINDOW|IDCMP_VANILLAKEY,
    WA_Flags,         WFLG_DRAGBAR|WFLG_DEPTHGADGET|WFLG_CLOSEGADGET|WFLG_SMART_REFRESH|WFLG_ACTIVATE,
    WA_Gadgets,       0l,
    WA_Title,         0l,
    WA_ScreenTitle,   0l,
    WA_PubScreen,     NULL,
    TAG_DONE };

/* Struktur für Image "Über Multitool" */

struct Image AboutImage = {
    0,0,262,143,2,
    NULL,
    0x3,0x0,NULL
    };

struct TagItem    AboutWindowTags[] = {
    WA_Left,          0,
    WA_Top,           0,
    WA_Width,         262,
    WA_Height,        143,
    WA_IDCMP,         IDCMP_VANILLAKEY|IDCMP_MOUSEMOVE,
    WA_Flags,         WFLG_BORDERLESS|WFLG_SMART_REFRESH|WFLG_ACTIVATE|WFLG_REPORTMOUSE,
    WA_Gadgets,       0l,
    WA_Title,         0l,
    WA_ScreenTitle,   0l,
    WA_PubScreen,     NULL,
    TAG_DONE };

/* Struktur für Easyrequester "Fehlermeldung" */

struct EasyStruct ErrorES = {
    sizeof (struct EasyStruct),
    0,
    0l,
    "%s",
    0l,
    };


/* Struktur für Easyrequester "Device-Info" */

struct EasyStruct DevInfoES = {
    sizeof (struct EasyStruct),
    0,
    0l,
    0l,
    0l,
    };

USHORT *MZeiger = NULL;
USHORT MZeigerDaten[]=
       { 0,0,
         0x0600, 0x0600,
         0x0f40, 0x0f40,
         0x3fe0, 0x3fe0,
         0x7fe0, 0x7fe0,
         0x61f0, 0x7ff0,
         0x7bf8, 0x7ff8,
         0xf7f8, 0xfff8,
         0x61fc, 0x7ffc,
         0x7f0c, 0x7ffc,
         0x3fde, 0x3ffe,
         0x7fbc, 0x7ffc,
         0x3f0c, 0x3ffc,
         0x1ff8, 0x1ff8,
         0x07f0, 0x07f0,
         0x01c0, 0x01c0,
         0x0700, 0x0700,
         0x0fc0, 0x0fc0,
         0x0780, 0x0780,
         0x0000, 0x0000,
         0x00c0, 0x00c0,
         0x00e0, 0x00e0,
         0x0040, 0x0040,
         0,0 };



/* ErrorReq()  : Wertet IoErr() aus und zeigt die zugehörige Fehlermeldung
                 in einem Requester an */

ErrorReq()
    {
    char IOError[60];

    ErrorES.es_Title = LS[MSG_3_T_ErrorES1];
    ErrorES.es_GadgetFormat = LS[MSG_3_T_ErrorES2];

    if (Fault(IoErr(), LS[MSG_3_T_Error], IOError, 60))
        EasyRequest( Wnd, &ErrorES, 0, IOError );
    }


/* ErrorMsg()  : Zeigt die übergebene Fehlermeldung in einem Requester an */

ErrorMsg(char *Msg)
    {
    ErrorES.es_Title = LS[MSG_3_T_ErrorES1];
    ErrorES.es_GadgetFormat = LS[MSG_3_T_ErrorES2];

    EasyRequest( Wnd, &ErrorES, 0, Msg );
    }


/* StringRequest: Bringt einen Requester mit zwei Informationszeilen und
                  einer Stringeingabezeile auf den Bildschirm.
    Zeile1/2      Infozeilen
    Input         Eingabezeile */

AboutRequester( void )
    {
    struct Window *AboutWnd;
    UWORD *AI_Data;

    AboutWindowTags[ 9 ].ti_Data = Scr;
    AboutWindowTags[ 0 ].ti_Data = (Scr->Width-260)/2;
    AboutWindowTags[ 1 ].ti_Data = (Scr->Height-143)/2;
    
    if ( AI_Data = (UWORD *)AllocMem( 9724, MEMF_CHIP ) )
       {
       memcpy( AI_Data, BreadlessData, 9724 );
       if ( AboutWnd = OpenWindowTagList( 0l, AboutWindowTags ))
          {
          AboutImage.ImageData = AI_Data;
          DrawImage( AboutWnd->RPort,&AboutImage,0,0 );
          Wait(1 << AboutWnd->UserPort->mp_SigBit);
          CloseWindow( AboutWnd );
          };
       FreeMem( AI_Data, 9724 );
       };
    }


long StringRequest( char *Zeile1, char *Zeile2, char *Input)
    {
    char Exit = 0;
    struct StringInfo *sti;
    short yg, y1, x1;
    ULONG  GClass;
    USHORT GCode;
    struct Gadget *GList;
    struct TextFont *Font;

    OpenStringReq();

    GT_SetGadgetAttrs(SreqGadgets[0], SreqWnd, 0, GTST_String, Input, TAG_DONE);
    ActivateGadget( SreqGadgets[0], SreqWnd, 0 );

    SetAPen( SreqWnd->RPort, 1 );

    yg = LY_GFontH + 5;
    y1 = Scr->BarHeight+2;
    x1 = Wnd->RPort->TxWidth;

    if (Font=(struct TextFont *)OpenDiskFont( &GadgetFont ))
       SetFont( SreqWnd->RPort, Font );

    Move( SreqWnd->RPort, 11+21*x1-strlen(Zeile1)*(x1>>1), y1+SreqWnd->RPort->TxBaseline+6 );
    Text( SreqWnd->RPort, Zeile1, strlen(Zeile1) );

    Move( SreqWnd->RPort, 11+21*x1-strlen(Zeile2)*(x1>>1), y1+SreqWnd->RPort->TxBaseline+4+yg );
    Text( SreqWnd->RPort, Zeile2, strlen(Zeile2) );

    if ( Font ) CloseFont( Font );

    for(;Exit==0;)
	{
	Wait(1 << SreqWnd->UserPort->mp_SigBit);
	while ( msg = (struct IntuiMessage *) GT_GetIMsg(SreqWnd->UserPort))
	    {
	    GClass  = msg->Class;
	    GCode   = msg->Code;
	    if (GClass & GADGETUP)
                GList   = (struct Gadget *) msg->IAddress;
	    GT_ReplyIMsg(msg);
	    switch (GClass)
		{
		case CLOSEWINDOW :
                    Exit=1;
                    break;
                case GADGETUP    :
                    switch (GList->GadgetID)
                        {
                        case SGD_Input :
                        case SGD_OK    : Exit=2;
                                         break;
                        case SGD_Cancel: Exit=1;
                                         break;
                        };
                    break;
                case VANILLAKEY  :
                    if(GCode==LS[MSG_3_T_SRKeys][0])
                        {
                        Exit=2;
                        }
                    else if(GCode==LS[MSG_3_T_SRKeys][1])
                        {
                        Exit=1;
                        };
                    break;
		};
	    };
	};
    if (Exit==2)
        {
        sti=(struct StringInfo *)SreqGadgets[SGD_Input]->SpecialInfo;
        strcpy( Input, sti->Buffer );
        };
    CloseStringReq();
    return(Exit-1);
    }

long OpenStringReq( void )
    {
    struct NewGadget     ng;
    struct Gadget       *g;
    short yg, y1, x1;
    struct TextFont     *TestFont;

    yg = LY_GFontH + 5;
    y1 = Scr->BarHeight+2;
    x1 = Wnd->RPort->TxWidth;

    if ( TestFont=(struct TextFont *)OpenDiskFont( &GadgetFont ) )
        {
        x1 = TestFont->tf_XSize;
        CloseFont( TestFont );
        };

    SreqWindowTags[ 9 ].ti_Data = Scr;
    SreqWindowTags[ 0 ].ti_Data = (Scr->Width-42*x1-22)/2;
    SreqWindowTags[ 1 ].ti_Data = (Scr->Height-yg*4-12-y1)/2;
    SreqWindowTags[ 2 ].ti_Data = 42*x1+22;
    SreqWindowTags[ 3 ].ti_Data = yg*4+12+y1;
    SreqWindowTags[ 7 ].ti_Data = LS[MSG_3_T_SreqTitle];
    SreqWindowTags[ 8 ].ti_Data = LS[MSG_3_T_SreqTitle];

    if ( NOT( g = CreateContext( &SreqGList )))
        return( 3l );

    ng.ng_LeftEdge        =    11;
    ng.ng_TopEdge         =    y1+2*yg+5;
    ng.ng_Width           =    42*x1;
    ng.ng_Height          =    yg;
    ng.ng_GadgetText      =    0l;
    ng.ng_TextAttr        =    &GadgetFont;
    ng.ng_GadgetID        =    SGD_Input;
    ng.ng_Flags           =    0;
    ng.ng_VisualInfo      =    VisualInfo;

    g = CreateGadget( STRING_KIND, g, &ng, GTST_MaxChars, 30, TAG_DONE );

    SreqGadgets[ 0 ] = g;

    ng.ng_TopEdge         =    y1+3*yg+7;
    ng.ng_Width           =    15*x1;
    ng.ng_GadgetText      =    LS[MSG_3_T_SROK];
    ng.ng_GadgetID        =    SGD_OK;

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

    SreqGadgets[ 1 ] = g;

    ng.ng_LeftEdge        =    11+27*x1;
    ng.ng_GadgetText      =    LS[MSG_3_T_SRCancel];
    ng.ng_GadgetID        =    SGD_Cancel;

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

    SreqGadgets[ 2 ] = g;

    if ( NOT g )
        return( 4l );

    if ( NOT( SreqWnd = OpenWindowTagList( 0l, SreqWindowTags )))
        return( 5l );

    SreqWnd->RPort->AreaPtrn = FillPattern;
    SreqWnd->RPort->AreaPtSz = 1;
    SetAPen( SreqWnd->RPort, 2 );
    RectFill( SreqWnd->RPort, 4, 11, 42*x1+17, yg*4+9+y1 );
    SreqWnd->RPort->AreaPtrn = NULL;
    SreqWnd->RPort->AreaPtSz = 0;

    SetAPen( SreqWnd->RPort, 0 );
    RectFill( SreqWnd->RPort, 12, y1+2, 42*x1+10, 2*yg+y1+2 );
    RectFill( SreqWnd->RPort, 12, y1+2*yg+5, 42*x1+10, 2*yg+y1+16 );

    DrawBevelBox( SreqWnd->RPort, 12, y1+2, 42*x1-1, 2*yg+1, GT_VisualInfo, VisualInfo, GTBB_Recessed, TRUE, TAG_DONE );

    AddGList( SreqWnd, SreqGList, 0, -1, NULL );
    RefreshGList( SreqGList, SreqWnd, NULL, -1 );
    GT_RefreshWindow( SreqWnd, 0l );

    return( 0l );
    }

CloseStringReq( void )
    {
    struct IntuiMessage *MyMsg;

    Forbid();
    while (MyMsg = GetMsg(SreqWnd->UserPort))
       {
       ReplyMsg(MyMsg);
       };

    ModifyIDCMP(SreqWnd,NULL);
    
    Permit();

    if ( SreqWnd    )
        RemoveGList( SreqWnd, SreqGList, -1 );
        CloseWindow( SreqWnd );

    if ( SreqGList  )
        FreeGadgets( SreqGList );
    }

long OpenConsole( void )
    {
    long FH;
    
    FH = Open( LS[MSG_3_T_OpenCON], MODE_READWRITE );
    return( FH );
    }


CloseConsole( struct FileHandle *FH )
    {
    Close( (BPTR)FH );
    SetPubScreenModes( OldPSM );
    }

ShowInfo( Act )
    {
    ULONG AllBl, AllBy, FreBl, FreBy, ByPBl;
    char DevName[40];
    char *DeNa;
    struct InfoData *IData;
    struct FileLock *FL;
    struct DeviceNode *DN;

    if (VL_Name[Act][0]!='\0' && (FL=(struct FileLock *)Lock(VL_Name[Act],ACCESS_READ))!=0)
        {
        if (IData = (struct InfoData *)AllocMem(sizeof(struct InfoData), 0))
            {
            if (Info((BPTR)FL,IData))
                {
                AllBl=IData->id_NumBlocks;
                FreBl=AllBl - IData->id_NumBlocksUsed;
                ByPBl=IData->id_BytesPerBlock;
                AllBy=AllBl * ByPBl;
                FreBy=FreBl * ByPBl;
                DN = (struct DeviceNode *)(IData->id_VolumeNode<<2);
                DeNa = (char *)(DN->dn_Name<<2);
                strncpy( DevName, DeNa+1, *DeNa );
                DevName[*DeNa] = '\0';
                DirAction( Act, 3, ByPBl );
                DevInfoES.es_Title = LS[MSG_3_T_DevInfoES1];
                DevInfoES.es_TextFormat = LS[MSG_3_T_DevInfoES2];
                DevInfoES.es_GadgetFormat = LS[MSG_3_T_DevInfoES3];

                if (NOT EasyRequest( Wnd, &DevInfoES, 0, DevName, AllBl, AllBy, FreBl, FreBy, ByPBl, VL_BytSel, VL_BlkSel*ByPBl ))
                    {
                    if (StringRequest(LS[MSG_2_T_ReName], DevName, DevName ))
                        {
                        if (Relabel(VL_Name[Act], DevName))
                            {
                            GT_SetGadgetAttrs(Gadgets[3], Wnd, 0, GTLV_Labels, 0, TAG_DONE);
                            GetDevs();
                            GT_SetGadgetAttrs(Gadgets[3], Wnd, 0, GTLV_Labels, &DL_List, TAG_DONE);
                            GetStat(0);
                            GetStat(1);
                            }
                         else
                            {
                            ErrorReq();
                            };
                        };
                    };
                };
            FreeMem( IData, sizeof(struct InfoData) );
            };
        }
    }

void WaitPointer( struct Window *wnd )
    {
    if (OS3 == 1)
        {
        SetWindowPointer( wnd, WA_BusyPointer, TRUE, WA_PointerDelay, TRUE, TAG_DONE );
        }
    else
        {
        if (MZeiger = AllocMem( sizeof( MZeigerDaten), MEMF_CHIP ))
            {
            CopyMem( MZeigerDaten, MZeiger, sizeof( MZeigerDaten ) );
            SetPointer( wnd, MZeiger, 22, 16, -7, -8 );
            }
        else MZeiger = NULL;
        };
    }

void StdPointer( struct Window *wnd )
    {
    if (OS3 == 1)
        {
        SetWindowPointer( wnd, TAG_DONE );
        }
    else
        {
        if (MZeiger)
            {
            ClearPointer( wnd );
            FreeMem( MZeiger, sizeof(MZeigerDaten) );
            MZeiger = NULL;
            };
        };
    }
