home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / OS2 / EMXFIX04.ZIP / WINMGR1.C < prev    next >
C/C++ Source or Header  |  1994-01-16  |  12KB  |  535 lines

  1. /* winmgr1.c (emx+gcc) -- Copyright (c) 1987-1994 by Eberhard Mattes */
  2.  
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <dos.h>
  6. #include <sys/video.h>
  7. #include <sys/winmgr.h>
  8. #include "winmgr2.h"
  9.  
  10. wm_handle _wm_tab    = NULL;
  11. wm_handle *_wm_idx   = NULL;
  12.  
  13. char * _wm_screen    = NULL;
  14. char * _wm_line      = NULL;
  15.  
  16. int _wm_count        = 0;
  17. int _wm_max          = 0;
  18. int _wm_saved_cstart = 0;
  19. int _wm_saved_cend   = 0;
  20. int _wm_saved_x      = 0;
  21. int _wm_saved_y      = 0;
  22. int _wm_width        = 0;
  23. int _wm_height       = 0;
  24.  
  25. wm_handle _wm_cursor = NULL;
  26. int _wm_c_hide       = TRUE;
  27.  
  28.  
  29. void _wm_cursor1 (void)
  30. {
  31.   int ax, ay;
  32.  
  33.   if (_wm_cursor != NULL)
  34.     {
  35.       ax = _wm_cursor->x + _wm_cursor->x0;
  36.       ay = _wm_cursor->y + _wm_cursor->y0;
  37.       if (ax < 0 || ax >= _wm_width || ay < 0 || ay >= _wm_height)
  38.         v_hidecursor ();
  39.       else if (!_wm_cursor->c_vis)
  40.         v_hidecursor ();
  41.       else if (_wm_cursor->open && (_wm_cursor->visible ||
  42.                                     MASK (_wm_cursor,
  43.                                           _wm_cursor->x + _wm_cursor->ax,
  44.                                           _wm_cursor->y + _wm_cursor->ay)))
  45.         {
  46.           v_ctype (_wm_cursor->c_start, _wm_cursor->c_end);
  47.           v_gotoxy (ax, ay);
  48.         }
  49.       else if (!_wm_c_hide)
  50.         {
  51.           v_ctype (_wm_saved_cstart, _wm_saved_cend);
  52.           v_gotoxy (ax, ay);
  53.         }
  54.       else
  55.         v_hidecursor ();
  56.     }
  57.   else
  58.     v_hidecursor ();
  59. }
  60.  
  61.  
  62. void _wm_store1 (wm_handle wh, int x, int y, char c, int a)
  63. {
  64.   char *p;
  65.  
  66.   p = &DATA (wh, x, y);
  67.   *p++ = c;
  68.   *p = (char)a;
  69. }
  70.  
  71.  
  72.  
  73. void _wm_clrline1 (wm_handle wh, int y, int x0, int x1)
  74. {
  75.   _wm_clrline2 (&DATA (wh, x0, y), x1-x0+1, (char)wh->wattr);
  76. }
  77.  
  78.  
  79.  
  80. void _wm_line1 (wm_handle wh, int y)
  81. {
  82.   char *src;
  83.   int yoffs;
  84.  
  85.   if (wh->open)
  86.     {
  87.       yoffs = y * wh->bwidth;
  88.       src = &wh->data[2*(0+yoffs)];
  89.       if (wh->visible)
  90.         _wm_move2 (src, y+wh->by0, wh->bx0, wh->bx1, 1);
  91.       else
  92.         _wm_line2 (&wh->mask[0+yoffs], src, y+wh->by0, wh->bx0, wh->bx1);
  93.     }
  94. }
  95.  
  96.  
  97. void _wm_allvisible (wm_handle wh)
  98. {
  99.   memset (wh->mask, TRUE, wh->masksize);
  100.   wh->visible = TRUE;
  101. }
  102.  
  103.  
  104. void _wm_hide2 (wm_handle wh1, wm_handle wh2)
  105. {
  106.   int x0, xc, y, y0, y9;
  107.   char *mask;
  108.  
  109.   wh1->visible = FALSE;
  110.   x0 = MAX (wh1->bx0, wh2->bx0);
  111.   xc = MIN (wh1->bx1, wh2->bx1) - x0 + 1;
  112.   if (xc > 0)
  113.     {
  114.       y0 = MAX (wh1->by0, wh2->by0);
  115.       y9 = MIN (wh1->by1, wh2->by1);
  116.       mask = &MASK (wh1, x0 - wh1->bx0, y0 - wh1->by0);
  117.       for (y = y0; y <= y9; y++)
  118.         {
  119.           memset (mask, FALSE, xc);
  120.           mask += wh1->bwidth;
  121.         }
  122.     }
  123. }
  124.  
  125.  
  126. void _wm_hide1 (void)
  127. {
  128.   int i, j;
  129.   wm_handle a, b;
  130.  
  131.   for (i = _wm_count-1; i >= 0; --i)
  132.     _wm_allvisible (_wm_idx[i]);
  133.   for (i = _wm_count-1; i >= 0; --i)
  134.     {
  135.       a = _wm_idx[i];
  136.       if (a->open)
  137.         for (j = i-1; j >= 0; j--)
  138.           {
  139.             b = _wm_idx[j];
  140.             if (b->open && _wm_jam1 (a, b))
  141.               _wm_hide2 (b, a);
  142.           }
  143.     }
  144. }
  145.  
  146.  
  147. void _wm_unhide1 (wm_handle wh, wm_handle new_wh)
  148. {
  149.   int ay, ry0, ay0, ay9, x0, x9, mx;
  150.   int i;
  151.   wm_handle p;
  152.   char *src, *mask;
  153.  
  154.   _wm_allvisible (wh);
  155.   for (i = 0; i < _wm_count; ++i)
  156.     {
  157.       p = _wm_idx[i];
  158.       if (p != wh)
  159.         {
  160.           if (p->open && _wm_jam1 (wh, p))
  161.             _wm_hide2 (wh, p);
  162.         }
  163.     }
  164.   if (new_wh != NULL && _wm_jam1 (wh, new_wh))
  165.     _wm_hide2 (wh, new_wh);
  166.   x0 = wh->bx0; x9 = wh->bx1; mx = 0;
  167.   ay0 = wh->by0; ay9 = wh->by1; ry0 = 0;
  168.   if (x0 < 0)
  169.     {
  170.       mx = -x0;
  171.       x0 = 0;
  172.     }
  173.   if (x9 >= _wm_width)
  174.     x9 = _wm_width-1;
  175.   if (ay0 < 0)
  176.     {
  177.       ry0 = -ay0;
  178.       ay0 = 0;
  179.     }
  180.   if (ay9 >= _wm_height)
  181.     ay9 = _wm_height-1;
  182.   src = &_wm_screen[2*(x0+ay0*_wm_width)];
  183.   mask = &MASK (wh, mx, ry0);
  184.   for (ay = ay0; ay <= ay9; ++ay)
  185.     {
  186.       if (wh->visible)
  187.         _wm_move2 (src, ay, x0, x9, 1);
  188.       else
  189.         {
  190.           _wm_line2 (mask, src, ay, x0, x9);
  191.           mask += wh->bwidth;
  192.         }
  193.       src += 2*_wm_width;
  194.     }
  195. }
  196.  
  197.  
  198. void _wm_clr1 (wm_handle wh)
  199. {
  200.   int y;
  201.  
  202.   for (y = 0; y < wh->height; ++y)
  203.     _wm_clrline1 (wh, y+wh->ay, wh->ax, wh->width-1+wh->ax);
  204. }
  205.  
  206.  
  207. void _wm_border1 (wm_handle wh, int title_flag, int title_attr,
  208.                   const char *title)
  209. {
  210.   char ul, ur, ll, lr, ho, ve, tl, tr;
  211.   int x2, y2;
  212.   int i, attr;
  213.  
  214.   switch (wh->border)
  215.     {
  216.     case 0:
  217.       return;
  218.     case 1:
  219.       ul = (char)'┌'; ur = (char)'┐';
  220.       ll = (char)'└'; lr = (char)'┘';
  221.       ho = (char)'─'; ve = (char)'│';
  222.       tl = (char)'┤'; tr = (char)'├';
  223.       break;
  224.     case 2:
  225.       ul = (char)'╔'; ur = (char)'╗';
  226.       ll = (char)'╚'; lr = (char)'╝';
  227.       ho = (char)'═'; ve = (char)'║';
  228.       tl = (char)'╣'; tr = (char)'╠';
  229.       break;
  230.     case 3:
  231.       ul = (char)0xd5; ur = (char)0xb8;
  232.       ll = (char)0xd4; lr = (char)0xbe;
  233.       ho = (char)0xcd; ve = (char)0xb3;
  234.       tl = (char)0xb5; tr = (char)0xc6;
  235.       break;
  236.     case 4:
  237.       ul = (char)0xd6; ur = (char)0xb7;
  238.       ll = (char)0xd3; lr = (char)0xbd;
  239.       ho = (char)0xc4; ve = (char)0xba;
  240.       tl = (char)0xb6; tr = (char)0xc7;
  241.       break;
  242.     default:
  243.       ul = ur = ll = lr = ho = ve = tl = tr = (char)wh->border;
  244.     }
  245.   attr = wh->battr;
  246.   x2 = wh->x1+1 - wh->bx0;
  247.   y2 = wh->y1+1 - wh->by0;
  248.   _wm_store1 (wh, 0, 0, ul, attr);
  249.   _wm_store1 (wh, 0, y2, ll, attr);
  250.   for (i = wh->ay; i < y2; i++)
  251.     _wm_store1 (wh, 0, i, ve, attr);
  252.   _wm_store1 (wh, x2, 0, ur, attr);
  253.   _wm_store1 (wh, x2, y2, lr, attr);
  254.   for (i = wh->ay; i < y2; i++)
  255.     _wm_store1 (wh, x2, i, ve, attr);
  256.   for (i = wh->ax; i < x2; i++)
  257.     _wm_store1 (wh, i, 0, ho, attr);
  258.   for (i = wh->ax; i < x2; i++)
  259.     _wm_store1 (wh, i, y2, ho, attr);
  260.   if (title != NULL)
  261.     {
  262.       int tlen, twid, tx;
  263.       
  264.       tlen = strlen (title);
  265.       if (title_flag == 0)
  266.         twid = wh->bwidth-2;
  267.       else
  268.         {
  269.           twid = wh->bwidth-4;
  270.           if (tlen > twid)
  271.             {
  272.               twid += 2;
  273.               title_flag = 0;
  274.             }
  275.         }
  276.       if (tlen > twid)
  277.         tlen = twid;
  278.       tx = 1+(twid-tlen)/2;
  279.       if (title_flag)
  280.         {
  281.           _wm_store1 (wh, tx       , 0, tl, attr);
  282.           _wm_store1 (wh, tx+1+tlen, 0, tr, attr);
  283.           ++tx;
  284.         }
  285.       for (i = 0; i < tlen; ++i)
  286.         _wm_store1 (wh, tx++, 0, title[i], title_attr);
  287.     }
  288. }
  289.  
  290.  
  291. void _wm_put1 (wm_handle wh)
  292. {
  293.   int y;
  294.  
  295.   if (wh->open)
  296.     {
  297.       for (y = 0; y < wh->bheight; ++y)
  298.         _wm_line1 (wh, y);
  299.       _wm_cursor1 ();
  300.     }
  301.   wh->update_req = FALSE;
  302. }
  303.  
  304.  
  305. int _wm_jam1 (wm_handle wh1, wm_handle wh2)
  306. {
  307.   return (wh1->bx0 <= wh2->bx1 &&
  308.           wh2->bx0 <= wh1->bx1 &&
  309.           wh1->by0 <= wh2->by1 &&
  310.           wh2->by0 <= wh1->by1);
  311. }
  312.  
  313.  
  314. int _wm_idx1 (wm_handle wh)
  315. {
  316.   int i;
  317.  
  318.   for (i = 0; i < _wm_count; ++i)
  319.     if (_wm_idx[i] == wh)
  320.       return (i);
  321.   return (-1);
  322. }
  323.  
  324.  
  325. void _wm_copy1 (wm_handle wh, wm_handle old_wh)
  326. {
  327.   int ay, x0, x9, xc, i, ry0, ay0, ay9, mx;
  328.   wm_handle p;
  329.   char *dst, *src, *mask;
  330.  
  331.   if (wh->open)
  332.     {
  333.       _wm_allvisible (wh);
  334.       for (i = 0; i < _wm_count; ++i)
  335.         {
  336.           p = _wm_idx[i];
  337.           if (p != wh)
  338.             {
  339.               if (p->open && _wm_jam1 (wh, p))
  340.                 _wm_hide2 (wh, p);
  341.             }
  342.         }
  343.       if (old_wh != NULL && _wm_jam1 (wh, old_wh))
  344.         _wm_hide2 (wh, old_wh);
  345.       x0 = wh->bx0; x9 = wh->bx1; mx = 0;
  346.       ay0 = wh->by0; ay9 = wh->by1; ry0 = 0;
  347.       if (x0 < 0)
  348.         {
  349.           mx = -x0;
  350.           x0 = 0;
  351.         }
  352.       if (x9 >= _wm_width)
  353.         x9 = _wm_width-1;
  354.       if (ay0 < 0)
  355.         {
  356.           ry0 = -ay0;
  357.           ay0 = 0;
  358.         }
  359.       if (ay9 >= _wm_height)
  360.         ay9 = _wm_height-1;
  361.       xc = x9-x0+1;
  362.       if (xc > 0)
  363.         {
  364.           src = &_wm_line[2*x0];
  365.           dst = &_wm_screen[2*(x0+ay0*_wm_width)];
  366.           mask = &MASK (wh, mx, ry0);
  367.           for (ay = ay0; ay <= ay9; ay++)
  368.             {
  369.               _wm_move2 (_wm_line, ay, 0, _wm_width-1, 0);
  370.               if (wh->visible)
  371.                 memmove (dst, src, 2*xc);
  372.               else
  373.                 {
  374.                   _wm_copy2 (mask, dst, src, xc);
  375.                   mask += wh->bwidth;
  376.                 }
  377.               dst += 2*_wm_width;
  378.             }
  379.         }
  380.     }
  381. }
  382.  
  383.  
  384. int wm_init (int n)
  385. {
  386.   int i;
  387.  
  388.   if (n <= 0)
  389.     return (FALSE);
  390.   if (!v_init ())
  391.     return (FALSE);
  392.   v_dimen (&_wm_width, &_wm_height);
  393.   v_getctype (&_wm_saved_cstart, &_wm_saved_cend);
  394.   v_ctype (-1, -1);
  395.   v_getxy (&_wm_saved_x, &_wm_saved_y);
  396.   _wm_cursor = NULL;
  397.   _wm_c_hide = TRUE;
  398.   _wm_count = 0;
  399.   _wm_tab = calloc (n, sizeof (struct _wm_window));
  400.   _wm_idx = calloc (n, sizeof (wm_handle));
  401.   _wm_screen = _tmalloc (2 * _wm_width * _wm_height);
  402.   _wm_line = _tmalloc (2*_wm_width);
  403.   if (_wm_tab == NULL || _wm_idx == NULL || _wm_screen == NULL ||
  404.       _wm_line == NULL)
  405.     {
  406.       if (_wm_tab != NULL)
  407.         {
  408.           free (_wm_tab); _wm_tab = NULL;
  409.         }
  410.       if (_wm_idx != NULL)
  411.         {
  412.           free (_wm_idx); _wm_idx = NULL;
  413.         }
  414.       if (_wm_screen != NULL)
  415.         {
  416.           _tfree (_wm_screen); _wm_screen = NULL;
  417.         }
  418.       if (_wm_line != NULL)
  419.         {
  420.           _tfree (_wm_line); _wm_line = NULL;
  421.         }
  422.       _wm_max = 0;
  423.       return (FALSE);
  424.     }
  425.   else
  426.     {
  427.       _wm_max = n;
  428.       for (i = 0; i < _wm_max; ++i)
  429.         _wm_tab[i].used = 0;
  430.       return (TRUE);
  431.     }
  432. }
  433.  
  434.  
  435. wm_handle wm_create (int x0, int y0, int x1, int y1, int border, int battr,
  436.                      int wattr)
  437. {
  438.   int i, bx0, by0, bx1, by1;
  439.   long ldatasize;
  440.   unsigned datasize;
  441.   wm_handle wh;
  442.  
  443.   if (_wm_tab == NULL || x1 < x0 || y1 < y0 || x1-x0 > 1000 || y1-y0 > 1000)
  444.     return (NULL);
  445.   for (i = 0; i < _wm_max; ++i)
  446.     if (_wm_tab[i].used != WM_USED)
  447.       break;
  448.   if (i >= _wm_max)
  449.     return (NULL);                                     /* No slot found */
  450.   wh = &_wm_tab[i];
  451.   wh->x0 = x0; wh->y0 = y0;
  452.   wh->x1 = x1; wh->y1 = y1;
  453.   bx0 = x0; by0 = y0;
  454.   bx1 = x1; by1 = y1;
  455.   if (border != 0)
  456.     {
  457.       --bx0; --by0;
  458.       ++bx1; ++by1;
  459.     }
  460.   wh->bx0 = bx0; wh->by0 = by0;
  461.   wh->bx1 = bx1; wh->by1 = by1;
  462.   wh->width = x1-x0+1; wh->bwidth = bx1-bx0+1;
  463.   wh->height = y1-y0+1; wh->bheight = by1-by0+1;
  464.   wh->ax = x0-bx0; wh->ay = y0-by0;
  465.   ldatasize = (long)wh->bwidth * (long)wh->bheight;
  466. #if !defined (__EMX__)
  467.   if (ldatasize >= 32768L)
  468.     return (NULL);
  469. #endif
  470.   datasize = (unsigned)ldatasize;
  471.   wh->data = _tmalloc (2*datasize);
  472.   wh->mask = malloc (datasize);
  473.   if (wh->data == NULL || wh->mask == NULL)
  474.     {
  475.       if (wh->data != NULL) _tfree (wh->data);
  476.       if (wh->mask != NULL) free (wh->mask);
  477.       return (NULL);                                     /* Out of memory */
  478.     }
  479.   wh->masksize = datasize;
  480.   wh->border = border;
  481.   wh->battr = battr; wh->wattr = wattr;
  482.   wh->x = 0; wh->y = 0;
  483.   wh->used = WM_USED; wh->open = FALSE; wh->display = FALSE;
  484.   wh->update_flag = TRUE; wh->update_req = FALSE;
  485.   wh->wrap = TRUE;
  486.   wh->c_vis = TRUE;
  487.   wh->c_start = _wm_saved_cstart; wh->c_end = _wm_saved_cend;
  488.   _wm_idx[_wm_count++] = wh;
  489.   wh->visible = TRUE;
  490.   _wm_clr1 (wh);
  491.   _wm_border1 (wh, 0, 0, NULL);
  492.   _wm_hide1 ();
  493.   _wm_cursor1 ();
  494.   return (wh);
  495. }
  496.  
  497.  
  498. void wm_attrib (wm_handle wh, int a)
  499. {
  500.   if (wh->used != WM_USED)
  501.     return;
  502.   wh->wattr = a;
  503. }
  504.  
  505.  
  506. void wm_cursor (wm_handle wh)
  507. {
  508.   if (wh != NULL && wh->used != WM_USED)
  509.     return;
  510.   _wm_cursor = wh;
  511.   _wm_cursor1 ();
  512. }
  513.  
  514.  
  515. void wm_chide (int flag)
  516. {
  517.   _wm_c_hide = flag;
  518.   _wm_cursor1 ();
  519. }
  520.  
  521.  
  522. void wm_cvis (wm_handle wh, int flag)
  523. {
  524.   if (wh != NULL && wh->used != WM_USED)
  525.     return;
  526.   wh->c_vis = flag;
  527.   _wm_cursor1 ();
  528. }
  529.  
  530.  
  531. wm_handle wm_get_cursor (void)
  532. {
  533.   return (_wm_cursor);
  534. }
  535.