Annotation of pmsdk/samples/browse/browse.c, revision 1.1.1.1

1.1       root        1: /* -----------------------------------------------------------------*\
                      2: /*     AVIO (windowed) File Browser Demo
                      3: /*     Version 1.0
                      4: /*     Created by Microsoft Corp. 1988
                      5: /* -----------------------------------------------------------------*/
                      6: 
                      7: /* ------------------------------------ */
                      8: /* library includes                    */
                      9: /* ------------------------------------ */
                     10: #define INCL_BASE
                     11: #define INCL_AVIO
                     12: #include <stdlib.h>
                     13: #include <stdio.h>
                     14: #include <os2.h>
                     15: #include <string.h>
                     16: #include "browse.h"
                     17: 
                     18: /* ------------------------------------ */
                     19: /* global variables                    */
                     20: /* ------------------------------------ */
                     21: FILE *pFile_In;
                     22: char *apchDataLines[ NUM_DATA_LINES ];
                     23: SHORT HighestLine= -1;
                     24: SHORT HorScrollPos=0;
                     25: extern HVPS hVPS;
                     26: extern HWND hwndSkel;
                     27: extern HDC hDC;
                     28: extern USHORT Vid_Cols, Vid_Rows;
                     29: 
                     30: /* ================================================================ */
                     31: /*         Functions                                               */
                     32: /* ================================================================ */
                     33: 
                     34: SHORT Initialize(argc,argv)
                     35: int argc;
                     36: char *argv[];
                     37: 
                     38: /* tries to open input file and initialize global variables */
                     39: /* plus any other misc. functions                          */
                     40: {
                     41:     char *achInFile;
                     42:     VIOMODEINFO viomiMode;
                     43: 
                     44:     /* -------------------------- */
                     45:     /* get input file ready      */
                     46:     /* -------------------------- */
                     47:     if (argc==1)
                     48:        pFile_In=stdin;
                     49:     else {
                     50:        achInFile=argv[1];
                     51:        if ((pFile_In=fopen(achInFile,"r"))==NULL) {
                     52:            fprintf(stderr,"Error--could not open %s",achInFile);
                     53:            return(-1);
                     54:        }
                     55:     }
                     56: 
                     57:     /* -------------------------- */
                     58:     /* read input file           */
                     59:     /* -------------------------- */
                     60:     if (ReadFile())
                     61:        return(-1);
                     62: 
                     63:     /* -------------------------- */
                     64:     /* get video parameters      */
                     65:     /* -------------------------- */
                     66:     viomiMode.cb = sizeof(viomiMode);
                     67: 
                     68:     GetWinCharSize();
                     69: 
                     70:     SetScrollPosVert( 0, HighestLine );
                     71:     SetScrollPosHorz( 0, HIGHEST_HSCROLL );
                     72:     DisplayScreen(0, TRUE);
                     73: }
                     74: 
                     75: /* ================================================================ */
                     76: 
                     77: SHORT ReadFile( void )
                     78: 
                     79: /* reads data lines from the file to memory.       */
                     80: /* if an error occurs, it will abort the program    */
                     81: /* stores the lines as null-terminated ascii, with  */
                     82: /* no /n's in the strings.                          */
                     83: {   char achLine[256];
                     84: 
                     85:     while (fgets(achLine,128,pFile_In)) {
                     86: 
                     87:        /* strip LF character from end of string before storing */
                     88:        /* by converting LF to /0                               */
                     89:        if (achLine[strlen(achLine)-1]=='\n')
                     90:            achLine[strlen(achLine)-1]=0;
                     91:        else {
                     92:            fprintf(stderr,"Internal error in ReadFile.\n");
                     93:            return(-1);
                     94:        }
                     95:        if (StoreLine(achLine)) {
                     96:            fprintf(stderr,"Error while storing line in ReadFile.\n");
                     97:            return(-1);
                     98:        }
                     99:     }
                    100: 
                    101:     fclose(pFile_In);
                    102:     return(0);
                    103: }
                    104: 
                    105: /* ================================================================ */
                    106: /*     Line-handling subsystem                                     */
                    107: /* ================================================================ */
                    108: 
                    109: SHORT StoreLine( pchLine )
                    110: char *pchLine;
                    111: 
                    112: /* store a line in memory at end of list.                      */
                    113: /* creates the line number automatically                       */
                    114: /* extensions to allow >64K data go here and in RetrieveLine   */
                    115: {
                    116:     /* don't forget to malloc space for \0 */
                    117:     if (HighestLine==NUM_DATA_LINES ||
                    118:       (apchDataLines[++HighestLine]=malloc(strlen(pchLine)+1))==NULL )
                    119:        return(-1);
                    120:     /* copy (to, from) */
                    121:     strcpy(apchDataLines[HighestLine],pchLine);
                    122:     return(0);
                    123: }
                    124: 
                    125: /* ================================================================ */
                    126: 
                    127: SHORT RetrieveLine( ppchLine , LineNum )
                    128: char **ppchLine;
                    129: USHORT LineNum;
                    130: 
                    131: /* take line number and convert it to a string of data from memory  */
                    132: /* extensions to allow >64K data also incorporated here            */
                    133: {
                    134:     if (LineNum > HighestLine)
                    135:        return(-1);
                    136:     *ppchLine=apchDataLines[LineNum];
                    137:     return(0);
                    138: }
                    139: 
                    140: /* ================================================================ */
                    141: /*             Video-handling subsystem                            */
                    142: /* ================================================================ */
                    143: 
                    144: void ClearScreen( void )
                    145: 
                    146: /* self-explanitory I hope         */
                    147: /* uses technique from qh examples  */
                    148: /* clearing=scrolldown(maxlines)    */
                    149: {
                    150:     BYTE bBlank[2];
                    151:     bBlank[0] = 0x20;                      /* space character       */
                    152:     bBlank[1] = 0x07;                      /* white attribute (EGA) */
                    153:     VioScrollUp(0,                        /* top row               */
                    154:         0,                                 /* left column           */
                    155:         -1,                                /* bottom row            */
                    156:         -1,                                /* right column          */
                    157:         -1,                                /* number of lines       */
                    158:         bBlank,                            /* cell to write         */
                    159:        WINDOW);                                /* video handle          */
                    160: }
                    161: 
                    162: /* ================================================================ */
                    163: 
                    164: void DisplayScreen( USHORT TopLine, BOOL force)
                    165: 
                    166: /* display lines from memory to video, starting at line number TopLine */
                    167: /* has 'smarts' to scroll+paint remainder.                             */
                    168: {
                    169:     SHORT LineDiff;
                    170:     static USHORT OldTopLine;
                    171: 
                    172:     VioSetOrg(0, HorScrollPos, WINDOW);   /* effects horizontal shift */
                    173:     LineDiff=TopLine-OldTopLine;
                    174:     if ( (abs(LineDiff) < Vid_Rows ) && !force ) {
                    175:        /* is legal to apply scroll+paint remainder algorithm */
                    176:        /* (usually applied when user wants line up/down) */
                    177:        if (LineDiff<0) {
                    178:            /* user scrolls back; repaint top part of screen */
                    179:            ScrollDown( -LineDiff );
                    180:            VidRefresh(TopLine, -LineDiff, 0);
                    181:        }
                    182:        else {
                    183:            /* user scrolls forward; repaint bottom part */
                    184:            ScrollUp( LineDiff );
                    185:            VidRefresh( TopLine+Vid_Rows-LineDiff, LineDiff, Vid_Rows-LineDiff);
                    186:        }
                    187:     }
                    188:     else {
                    189:        /* repaint entire screen */
                    190:        ClearScreen();
                    191:        VidRefresh(TopLine, Vid_Rows, 0);
                    192:     }
                    193:     OldTopLine=TopLine;
                    194: }
                    195: 
                    196: /* ================================================================ */
                    197: 
                    198: void VidRefresh ( USHORT LineFrom, USHORT NumLines, USHORT VidRowStart)
                    199: /* Redisplay lines LineFrom to LineTo starting at video VidRowStart */
                    200: /* Will NOT check that you haven't given it stupid arguments        */
                    201: {
                    202:     USHORT LineOffset;
                    203:     char *pchLine;
                    204: 
                    205:     for (LineOffset=0; LineOffset < NumLines ; LineOffset++) {
                    206:        if (LineOffset + LineFrom > HighestLine)
                    207:            break;
                    208:        RetrieveLine(&pchLine, LineOffset + LineFrom);
                    209:        VioSetCurPos(
                    210:            VidRowStart+LineOffset,     /* vertical pos   */
                    211:            0                    ,      /* horizontal pos */
                    212:            WINDOW                      /* hvio */
                    213:        );
                    214:        VioWrtTTY(
                    215:            pchLine,            /* string to print */
                    216:            strlen(pchLine),    /* string length */
                    217:            WINDOW              /* hvio */
                    218:        );
                    219:     }
                    220: }
                    221: 
                    222: void ScrollUp(int Nlines)
                    223: {
                    224:     BYTE bBlank[2];
                    225:     bBlank[0] = 0x20;                      /* space character       */
                    226:     bBlank[1] = 0x07;                      /* white attribute (EGA) */
                    227:     VioScrollUp(0,                        /* top row               */
                    228:         0,                                 /* left column           */
                    229:         -1,                                /* bottom row            */
                    230:         -1,                                /* right column          */
                    231:        Nlines,                            /* number of lines       */
                    232:         bBlank,                            /* cell to write         */
                    233:        WINDOW);                                /* video handle          */
                    234: }
                    235: 
                    236: void ScrollDown(int Nlines)
                    237: {
                    238:     BYTE bBlank[2];
                    239:     bBlank[0] = 0x20;                      /* space character       */
                    240:     bBlank[1] = 0x07;                      /* white attribute (EGA) */
                    241:     VioScrollDn(0,                         /* top row               */
                    242:         0,                                 /* left column           */
                    243:         -1,                                /* bottom row            */
                    244:         -1,                                /* right column          */
                    245:        Nlines,                            /* number of lines       */
                    246:         bBlank,                            /* cell to write         */
                    247:        WINDOW);                                /* video handle          */
                    248: }
                    249: 
                    250: /* ================================================================ */
                    251: /*     User input subsystem                                        */
                    252: /* ================================================================ */
                    253: 
                    254: VOID ExecuteAction( CHAR ch , USHORT usSliderPos )
                    255: /* should be: Execute_User_Action()                            */
                    256: /* determine the new line number                               */
                    257: /* at top of screen                                            */
                    258: /* given the movement code in ch                               */
                    259: /* returns 1 if successful                                     */
                    260: /* returns 0 if unsuccessful (user wants to quit)              */
                    261: {
                    262:     static USHORT OldLine ;
                    263:     BOOL bForce=FALSE;
                    264: 
                    265: #define downbound( X, A, B) ( (X) > ((B)-(A)) )? (B) : (X)+(A)
                    266: #define upbound( X, A, B) ( (X) < ((B)+(A)) )? (B) : (X)-(A)
                    267: 
                    268: 
                    269:     switch (ch) {
                    270:        case LINE_UP:
                    271:            OldLine=upbound( OldLine, 1, 0);
                    272:            break;
                    273:        case LINE_DOWN:
                    274:            OldLine=downbound( OldLine, 1, MAX_START_LINE);
                    275:            break;
                    276:        case PAGE_UP:
                    277:            OldLine=upbound( OldLine, Vid_Rows, 0);
                    278:            break;
                    279:        case PAGE_DOWN:
                    280:            OldLine=downbound( OldLine, Vid_Rows, MAX_START_LINE);
                    281:            break;
                    282:        case GOTO_TOP:
                    283:            OldLine= 0;
                    284:            break;
                    285:        case GOTO_BOTTOM:
                    286:            OldLine= MAX_START_LINE;
                    287:            break;
                    288:        case GOTO_LINE_TRACK:
                    289:            OldLine= usSliderPos;
                    290:            break;
                    291:        case CHAR_RIGHT:
                    292:            HorScrollPos= downbound ( HorScrollPos, 1, HIGHEST_HSCROLL);
                    293:            break;
                    294:        case CHAR_LEFT:
                    295:            HorScrollPos = upbound( HorScrollPos, 1, 0);
                    296:            break;
                    297:        case PAGE_RIGHT:
                    298:            HorScrollPos = downbound( HorScrollPos, HSCROLL_PAGESIZE, HIGHEST_HSCROLL);
                    299:            break;
                    300:        case PAGE_LEFT:
                    301:            HorScrollPos = upbound( HorScrollPos, HSCROLL_PAGESIZE, 0);
                    302:            break;
                    303:        case GOTO_HSCROLL_TRACK:
                    304:            HorScrollPos = (SHORT) usSliderPos;
                    305:            break;
                    306:        case NULL:
                    307:            bForce=TRUE;
                    308:            break;
                    309:        case GOTO_LINE, GOTO_HSCROLL:
                    310:            break;
                    311:     }
                    312:     if ( (ch != GOTO_HSCROLL_TRACK) && (ch != GOTO_LINE_TRACK) ) {
                    313:        SetScrollPosVert ( OldLine, MAX_START_LINE );
                    314:        SetScrollPosHorz ( HorScrollPos, HIGHEST_HSCROLL );
                    315:     }
                    316:     DisplayScreen( OldLine, bForce );
                    317: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.