home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / c / pcw_c.zip / PUSHPOP.C < prev    next >
C/C++ Source or Header  |  1990-12-17  |  4KB  |  116 lines

  1. /***********************************************************/
  2. /* File Id.                  PushPop.C                     */
  3. /* Author.                   Stan Milam.                   */
  4. /* Date Written.             11/06/88.                     */
  5. /* Date Last Modified.                                     */
  6. /*                                                         */
  7. /*         (c) Copyright 1989, 1990 by Stan Milam          */
  8. /*                                                         */
  9. /* Comments:  This file contains functions to save & re-   */
  10. /* store the contents of a screen text block.  These func- */
  11. /* tions return pointers to window structures containing   */
  12. /* the text buffers and other info.                        */
  13. /***********************************************************/
  14.  
  15. #if __TURBOC__ || __ZTC__
  16. #   define _fmalloc farmalloc
  17. #   define _ffree   farfree
  18. #   if __TURBOC__
  19. #      include <alloc.h>
  20. #   endif
  21. #else
  22. #   include <malloc.h>
  23. #endif
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <dos.h>
  28. #include "pcw.i"
  29. #include "pcwproto.h"
  30.  
  31. WNDPTR *wpush(int urow, int ucol, int lrow, int lcol) {
  32.  
  33.    int far  *scrnptr;
  34.    int      rows, cols, page, pagesize;
  35.    int      mx_rows, mx_cols;
  36.    unsigned offset, scrnseg;
  37.    WNDPTR   *wnd;
  38.  
  39.    if (!chk_video_state(&mx_rows,&mx_cols))
  40.       return(NULL);
  41.  
  42.    wnd = (WNDPTR *) malloc(sizeof(WNDPTR));
  43.    if (wnd == NULL) return(wnd);            /* Exit if error */
  44.  
  45.    memset(wnd, '\0', sizeof(WNDPTR));
  46.  
  47.    if (lcol > mx_cols) {                    /* Check to see if window */
  48.       mx_cols = lcol - mx_cols;             /* will fit on screen. Fix if */
  49.       lcol -= mx_cols;                      /* we have to */
  50.       ucol -= mx_cols;
  51.    }
  52.  
  53.    if (lrow > mx_rows) {                     /* Check for greater than 25 */
  54.       mx_rows = lrow - mx_rows;              /* rows and fix if needed */
  55.       lrow -= mx_rows;
  56.       urow -= mx_rows;
  57.    }
  58.  
  59.    rows = (lrow - urow) + 1;
  60.    cols = (lcol - ucol) + 1;
  61.  
  62.    wnd->wbuffer = (char far *) _fmalloc((rows * cols) * 2);
  63.    if (wnd->wbuffer == (char far *) NULL) { /* If error then */
  64.       free(wnd);                            /* Free memory */
  65.       return(NULL);                         /* Exit if no memory allocated */
  66.    }
  67.    wnd->wsave = (char far *) NULL;          /* Make NULL to be sure */
  68.  
  69.    page      = getpage();
  70.    scrnseg   = getscrnseg();
  71.    pagesize  = getpagesize();
  72.  
  73.    wnd->urow = urow;                        /* Save rows & cols in struct */
  74.    wnd->ucol = ucol;
  75.    wnd->lrow = lrow;
  76.    wnd->lcol = lcol;
  77.    wnd->page = page;
  78.    wnd->attr = (char) ((BLACK << 4) + LIGHTGRAY);
  79.    wnd->hideflag = 0;
  80.  
  81.    offset = MK_SCRNOFF(urow, ucol);               /* Create pointer to */
  82.    scrnptr = (int far *) MK_FP(scrnseg, offset);  /* Screen */
  83.  
  84.    SaveScrn(rows,cols,scrnptr,wnd->wbuffer);
  85.    re_order(wnd, PUSH);
  86.    return(wnd);
  87. }
  88.  
  89. /***********************************************************/
  90. /*                            Wpop                         */
  91. /*                                                         */
  92. /* Will restore the underlying screen image and free memory*/
  93. /* to the heap again.  Removes window from the internal    */
  94. /* list of windows.                                        */
  95. /***********************************************************/
  96.  
  97. WNDPTR *wpop(WNDPTR *wnd)  {
  98.  
  99.    int mx_rows, mx_cols;
  100.  
  101.    if (!chk_video_state(&mx_rows,&mx_cols)) return(0);
  102.    if (wnd == NULL) return(NULL);
  103.    if (wnd->wbuffer == (char far *) NULL)   /* If null pointer */
  104.       return(wnd);                          /* quit with bad return */
  105.    re_order(wnd, POP);                      /* Remove from internal list */
  106.    _ffree(wnd->wbuffer);
  107.    wnd->wbuffer = (char far *) NULL;
  108.    if (wnd->wsave != (char far *) NULL) {   /* Memory buffer */
  109.       _ffree(wnd->wsave);
  110.       wnd->wsave = (char far *) NULL;
  111.    }
  112.    free(wnd);                               /* Free allocated window */
  113.    wnd = (WNDPTR *) NULL;                   /* Make it NULL */
  114.    return(wnd);                             /* Return the NULL Pointer */
  115. }
  116.