home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 428_02 / libsrc / wopen.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-13  |  4.3 KB  |  207 lines

  1. /*
  2. ** wopen.c
  3. **
  4. ** Pictor, Version 1.51, Copyright (c) 1992-94 SoftCircuits
  5. ** Redistributed by permission.
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include "pictor.h"
  11.  
  12.  
  13. WINDOW *_PL_winhead = NULL;
  14. int _PL_shadowcolor = foreback(WHITE,BLACK);
  15.  
  16.  
  17. #define STATIC_BUFSIZ   4096
  18. static char static_heap[STATIC_BUFSIZ];
  19. static char *heap_base = static_heap;
  20.  
  21. /*
  22. ** Maintains and allocates from the static heap.
  23. */
  24. static void *static_alloc(int size)
  25. {
  26.     if((heap_base + size) <= (static_heap + STATIC_BUFSIZ)) {
  27.         heap_base += size;
  28.         return(heap_base - size);
  29.     }
  30.     return(NULL);
  31.  
  32. } /* static_alloc */
  33.  
  34. /*
  35. ** Release static memory back to static heap.
  36. */
  37. static void static_free(char *ptr)
  38. {
  39.     if(ptr >= static_heap && ptr < (static_heap + STATIC_BUFSIZ))
  40.         heap_base = ptr;
  41.  
  42. } /* static_free */
  43.  
  44. /*
  45. ** Closes all open windows.
  46. */
  47. void wcloseall(void)
  48. {
  49.     while(_PL_winhead != NULL)
  50.         wclose();
  51.  
  52. } /* wcloseall */
  53.  
  54. /*
  55. ** Closes the current window.
  56. */
  57. void wclose(void)
  58. {
  59.     WINDOW *win;
  60.     int shadow;
  61.  
  62.     if(_PL_winhead != NULL) {
  63.  
  64.         win = _PL_winhead;
  65.         _PL_winhead = _PL_winhead->next;
  66.  
  67.         shadow = (win->style & WO_SHADOW) ? 1 : 0;
  68.  
  69.         /* restore screen under window */
  70.         if(win->buffer != NULL) {
  71.             putscrn(win->buffer,win->top,win->left,win->height + shadow,
  72.                 win->width + shadow);
  73.  
  74.             /* release buffer memory */
  75.             if(win->style & WO_STATICMEM)
  76.                 static_free((char *)win->buffer);
  77.             else
  78.                 free(win->buffer);
  79.         }
  80.         /* restore cursor to previous window or full screen */
  81.         setcurs(win->oldcurspos >> 8,win->oldcurspos & 0xFF);
  82.         setcurstype(win->oldcurstype);
  83.  
  84.         if(win->style & WO_STATICMEM)
  85.             static_free((char *)win);
  86.         else
  87.             free(win);
  88.     }
  89.  
  90. } /* wclose */
  91.  
  92. /*
  93. ** Clears the current window pane.
  94. */
  95. void wclear(void)
  96. {
  97.     if(_PL_winhead != NULL) {
  98.         vcolor(_CURR_WPANE.color);
  99.         scroll(0,_CURR_WPANE.top + 1,_CURR_WPANE.left + 1,getwrows(),getwcols());
  100.         _CURR_WPANE.row = 0;
  101.         _CURR_WPANE.column = 0;
  102.  
  103.         if(_PL_winhead->style & WO_TEXTCURSOR) {
  104.             wsynccurs();
  105.         }
  106.     }
  107.  
  108. } /* wclear */
  109.  
  110. /*
  111. ** Defines and displays a window.
  112. */
  113. int wopen(int top,int left,int height,int width,int color,int style)
  114. {
  115.     WINDOW *win;
  116.     int i,shadow;
  117.  
  118.     shadow = (style & WO_SHADOW) ? 1 : 0;
  119.  
  120.     /* validate window coordinates */
  121.     height = (height < 3) ? 3 : height;
  122.     width = (width < 3) ? 3 : width;
  123.     top = (top < 1) ? 1 : top;
  124.     left = (left < 1) ? 1 : left;
  125.  
  126.     if(((top + height + shadow) - 1) > _PL_rows)
  127.         height = (_PL_rows - (top + shadow)) + 1;
  128.     if(((left + width + shadow) - 1) > _PL_columns)
  129.         width = (_PL_columns - (left + shadow)) + 1;
  130.  
  131.     /* allocate memory for window */
  132.     if(style & WO_STATICMEM) {
  133.         /* allocate window structure */
  134.         win = static_alloc(sizeof(WINDOW));
  135.         if(win == NULL) {
  136.             /* fatal error */
  137.             _PL_color = 0x07;
  138.             cls();
  139.             vputs("Fatal error : wopen() out of static memory");
  140.             exit(-1);
  141.         }
  142.  
  143.         /* allocate window buffer */
  144.         win->buffer = static_alloc(((height + shadow) * (width + shadow)) * 2);
  145.     }
  146.     else {
  147.         /* allocate window structure */
  148.         win = malloc(sizeof(WINDOW));
  149.         if(win == NULL)
  150.             return(FALSE);
  151.  
  152.         /* allocate window buffer */
  153.         win->buffer = malloc(((height + shadow) * (width + shadow)) * 2);
  154.         if(win->buffer == NULL) {
  155.             free(win);
  156.             return(FALSE);
  157.         }
  158.     }
  159.  
  160.     win->next = _PL_winhead;
  161.     _PL_winhead = win;
  162.  
  163.     /* create the window */
  164.     if(win->buffer != NULL)
  165.         getscrn(win->buffer,top,left,height + shadow,width + shadow);
  166.     vcolor(color);
  167.  
  168.     frame(top,left,height,width);
  169.  
  170.     /* create shadow */
  171.     if(style & WO_SHADOW) {
  172.         setvpos(top + height,left + 1);
  173.         vrepa(_PL_shadowcolor,width);
  174.  
  175.         for(i = 0;i < height;i++) {
  176.             setvpos(top + i + 1,left + width);
  177.             vputa(_PL_shadowcolor);
  178.         }
  179.     }
  180.     win->oldcurspos = getcurs();
  181.     win->oldcurstype = getcurstype();
  182.     win->top = top;
  183.     win->left = left;
  184.     win->height = height;
  185.     win->width = width;
  186.     win->style = style;
  187.     win->color = color;
  188.  
  189.     win->numpanes = 1;
  190.     win->currpane = 0;
  191.     win->panes[win->currpane].top = top;
  192.     win->panes[win->currpane].left = left;
  193.     win->panes[win->currpane].height = height;
  194.     win->panes[win->currpane].width = width;
  195.     win->panes[win->currpane].color = color;
  196.  
  197.     if(win->style & WO_TEXTCURSOR)
  198.         showcurs(TRUE);
  199.     else
  200.         setcurs(_PL_rows + 1,1);
  201.  
  202.     wclear();
  203.  
  204.     return(TRUE);
  205.  
  206. } /* wopen */
  207.