/*-- AutoRev header do NOT edit!
*
*   Program         :   Blocks.c
*   Copyright       :   Copyright © 1991-92 Jaba Development
*   Author          :   Jan van den Baard
*   Creation Date   :   04-Apr-92
*   Current version :   2.0
*   Translator      :   Dice v2.06.40
*
*   REVISION HISTORY
*
*   Date          Version         Comment
*   ---------     -------         ------------------------------------------
*   04-Apr-92     2.0             Text block routines. (rewrite)
*
*-- REV_END --*/

#include "View.h"

Prototype void SetMark( UWORD );
Prototype void UnSetMark( UWORD );
Prototype void JumpMark( UWORD );
Prototype void SaveBlock( WORD );
Prototype long CheckBlock( UWORD );

extern struct TextBlock          vwBlocks[ 10 ];
extern struct AsciiText         *vwText;
extern struct Window            *vwWindow;
extern struct Line              *vwFirst, *vwLast;
extern UBYTE                     ClearStr[], vwFile[], vwPath[], vwPatt[], vwName[];
#ifndef __DETACH
extern ULONG                     vwArgs[];
#else
extern ULONG                    *vwArgs;
#endif
extern ULONG                     vwShown;

extern __far struct Custom       custom;

long CheckBlock( UWORD mark )
{
    UBYTE       errl[ 31 ];

    if ( ! vwBlocks[ mark ].TopLine || ! vwBlocks[ mark].BottomLine ) {
        sprintf(( char * )&errl[ 0 ], "Block %-2.2ld not marked !", mark + 1 );
        Error( errl );
        return( FALSE );
    }
    return( TRUE );
}

void SetMark( UWORD mark )
{
    struct Line     *line, *lt;
    UBYTE            errl[ 31 ];

    if ( ! vwText )    return;

    if ( ! vwBlocks[ mark ].TopLine) {
        vwBlocks[ mark ].TopLine = vwFirst;
        return;
    } else if ( ! vwBlocks[ mark ].BottomLine ) {
        vwBlocks[ mark ].BottomLine = vwLast;

        for( line = vwText->First; line->Next; line = line->Next ) {
            if ( line == vwBlocks[ mark ].TopLine) break;
            else if ( line == vwBlocks[ mark ].BottomLine) {
                lt = vwBlocks[ mark ].TopLine;
                vwBlocks[ mark ].TopLine    = line->Prev;
                vwBlocks[ mark ].BottomLine = lt->Next;
                break;
            }
        }
        sprintf(( char * )&errl[ 0 ], "Block %-2.2ld marked !", mark + 1 );
        ErrorLine( errl );
        return;
    } else {
        sprintf(( char * )&errl[ 0 ], "Block %-2.2ld already marked !", mark + 1 );
        Error( errl );
        return;
    }
}

void UnSetMark( UWORD mark )
{
    UBYTE   errl[ 31 ];

    if ( ! vwText )    return;

    vwBlocks[ mark ].TopLine = vwBlocks[ mark ].BottomLine = NULL;

    sprintf(( char * )&errl[ 0 ], "Block %-2.2ld unmarked !", mark + 1 );
    ErrorLine( errl );
}

void JumpMark( UWORD mark )
{
    struct Line *line;

    if ( ! vwText )    return;

    if ( ! CheckBlock( mark ))
        return;

    vwFirst = vwBlocks[ mark ].TopLine;
    Inform( ClearStr );
    DisplayText();

    vwShown = 0L;

    for ( line = vwText->First; line != vwLast; line = line->Next )
        vwShown += line->Size;

    Percentage();
}

void SaveBlock( WORD mark )
{
    struct BuffIO       *ofile = NULL;
    struct Line         *f, *t;
    ULONG                ret;
    UBYTE               *title;

    if ( mark >= 0 ) {
        if ( ! CheckBlock( mark ))
            return;
    } else {
        if ( ! vwText )
            return;
    }

    if( mark >= 0 ) title = "Save textblock as...";
    else            title = "Save text as...";

    if( vwArgs[ 11 ] ) ON_SPRITE;

    ret = FileRequest( vwWindow, title, vwFile, vwPath, vwPatt, FR_SAVE );

    if ( vwArgs[ 11 ] ) OFF_SPRITE;

    if ( ! ret ) {
        InfoLine();
        return;
    }

    Busy();
    if ( ofile = BOpen( vwName, MODE_NEWFILE )) {
        ErrorLine( "Writing..." );
        if ( mark >= 0 ) {
            f = vwBlocks[ mark ].TopLine;
            t = vwBlocks[ mark ].BottomLine;
        } else {
            f = vwText->First;
            t = vwText->Last->Next;
        }

        for( ; f != t; f = f->Next )
            BPutS( ofile, f );

        if( BClose( ofile ) != ASE_OK)
            Error( "Write Error." );
    } else
        Error( "Can't open write file." );

    Ready();
    InfoLine();
}
