home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / grafik / vifs / edcolors.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-08-13  |  7.5 KB  |  368 lines

  1. /*
  2.  *    edcolors.c  --  edit colors.
  3.  *
  4.  *    11 july 1989  Olle Olsson.
  5.  */
  6.  
  7. #include "edtrans.h"
  8.  
  9. /* minimum length of the color display box */
  10. #define CBOXL (MAX_COL / 20)
  11.  
  12. /* local things */
  13.  
  14. static void redisptr( ifsdes *dp, transform *tp, int newcolor );
  15.  
  16. static menudescr cmenu =        /* the menu */
  17.     {
  18.     "quit",
  19.     "im",
  20.     "Max_im+", "max_im-",
  21.     "_bg",
  22.     "prev",
  23.     "+index", "-index",
  24.     "R+", "G+", "B+",
  25.     "r-", "g-", "b-"
  26.     };
  27.  
  28.  
  29. void edcolors( ifsdes *dp )
  30. {
  31. int i;
  32. struct gm_status gs;        /* mouse status */
  33. transform *ctp;            /* current transform */
  34. rgb *cc;            /* current color */
  35. int vx;                /* x position for color box display */
  36. char *fim = "im:%d (%d) ";    /* format of the im level (max) */
  37. char *fclr = "color:%d ";    /* format of the color index */
  38. char buf[100];            /* tmp */
  39.  
  40. /* hide the mouse pointer */
  41. gm_hide();
  42.  
  43. /* show the menu (the caller has removed its menu) */
  44. setcolor( dp -> textcolor );
  45. showmenu( &cmenu );
  46.  
  47. /* assume that the menu doesn't cover all of the top line */
  48. vx = MAX_COL - CBOXL;
  49.  
  50. /* the color box is supposed to cover the im level display */
  51. /* (the color index string is shorter so don't bother with its length) */
  52. sprintf( buf, fim, dp -> clrsize, dp -> clrsize );
  53. i = MAX_COL - textwidth( buf );
  54. if (i < vx) vx = i;
  55.  
  56. gm_show();
  57.  
  58. /* if im mode: select the second level (not backgr.), else don't select */
  59. for (cc = dp -> im ? &dp -> colors[1] : 0, ctp = 0;;)
  60.     {
  61.     /* show the value */
  62.     if (ctp && !cc)
  63.         {
  64.         /* previous color! */
  65.  
  66.         gm_hide();
  67.  
  68.         /* draw the color display box in background color */
  69.         setfillstyle( SOLID_FILL, 0 );
  70.         bar( vx, 0, MAX_COL, CBOXL );
  71.  
  72.         setcolor( dp -> textcolor );
  73.         moveto( vx, 0 );
  74.         gputs( "prev" );
  75.  
  76.         gm_show();
  77.         }
  78.     else if (cc)
  79.         {
  80.         gm_hide();
  81.  
  82.         /* install the new palette value  */
  83.         setpalette( i = cc - dp -> colors, mkegacolor( cc ) );
  84.  
  85.         /* draw the color display box */
  86.         setfillstyle( SOLID_FILL, i );
  87.         bar( vx, 0, MAX_COL, CBOXL );
  88.  
  89.         /* im mode ? */
  90.         if (dp -> im && (i > 0))
  91.             {
  92.             /* display the im level */
  93.             sprintf( buf, fim, i, dp -> im );
  94.             }
  95.         else
  96.             {
  97.             /* display the current transforms color (or bg color) */
  98.             sprintf( buf, fclr, i );
  99.             }
  100.  
  101.         setcolor( dp -> textcolor );
  102.         moveto( vx, 0 );
  103.         gputs( buf );
  104.  
  105.         gm_show();
  106.         }
  107.  
  108.     /* read the keyboard & mouse */
  109.     switch (wbup(), getkey( &gs ))
  110.         {
  111.         case 'q':    /* quit */
  112.             /* remove the menu */
  113.             gm_hide();
  114.             nomenu();
  115.             gm_show();
  116.             return;
  117.  
  118.         /* Upper case: increase, lower: decrease color intensity */
  119.         case 'R': if (cc) r_incdec( cc, 1 ); break;
  120.         case 'G': if (cc) g_incdec( cc, 1 ); break;
  121.         case 'B': if (cc) b_incdec( cc, 1 ); break;
  122.         case 'r': if (cc) r_incdec( cc, 0 ); break;
  123.         case 'g': if (cc) g_incdec( cc, 0 ); break;
  124.         case 'b': if (cc) b_incdec( cc, 0 ); break;
  125.  
  126.         case '_':    /* background */
  127.             /* the first color is background */
  128.             cc = dp -> colors;
  129.             ctp = 0;
  130.  
  131.             break;
  132.  
  133.         case '+':    /* increase color or im level (index) */
  134.             if (dp -> im > 0)
  135.                 {
  136.                 /* im mode */
  137.                 /* increase or wrap around */
  138.                 if (cc < &dp -> colors[dp -> im])
  139.                     ++cc;
  140.                 else cc = &dp -> colors[1];
  141.                 }
  142.             else if (cc)
  143.                 {
  144.                 /* color index */
  145.                 /* increase or wrap */
  146.                 /* (also affects current transform) */
  147.                 if (cc < &dp -> colors[dp -> clrsize - 1])
  148.                     {
  149.                     ++cc;
  150.                     if (ctp)
  151.                         {
  152.                         /* redraw with the new color */
  153.                         redisptr( dp, ctp,
  154.                              ctp -> color + 1 );
  155.                         }
  156.                     }
  157.                 else
  158.                     {
  159.                     /* wrap to the second color (not bg) */
  160.                     cc = &dp -> colors[1];
  161.                     if (ctp)
  162.                         {
  163.                         /* redraw with the new color */
  164.                         redisptr( dp, ctp, 1 );
  165.                         }
  166.                     }
  167.                 }
  168.             else if (ctp)
  169.                 {
  170.                 /* previous color: select "real" color 1 */
  171.  
  172.                 /* redraw with the new color */
  173.                 redisptr( dp, ctp, 1 );
  174.  
  175.                 cc = &dp -> colors[ctp -> color];
  176.                 }
  177.  
  178.             break;
  179.  
  180.         case '-':    /* decrease color or im level (index) */
  181.             if (dp -> im > 0)
  182.                 {
  183.                 /* im mode */
  184.                 /* decrease or wrap */
  185.                 /* don't decrease to background */
  186.                 if (cc <= dp -> colors)
  187.                     {
  188.                     /* go from background */
  189.                     /* select the first level */
  190.                     cc = &dp -> colors[1];
  191.                     }
  192.                 else if (cc > &dp -> colors[1])
  193.                     --cc;
  194.                 else cc = &dp -> colors[dp -> im];
  195.                 }
  196.             else if (cc)
  197.                 {
  198.                 /* color index */
  199.                 /* decrease or wrap */
  200.                 /* (also affects current transform) */
  201.                 if (cc > &dp -> colors[1])
  202.                     {
  203.                     --cc;
  204.                     if (ctp)
  205.                         {
  206.                         /* redraw with the new color */
  207.                         redisptr( dp, ctp,
  208.                             ctp -> color - 1 );
  209.                         }
  210.                     }
  211.                 else
  212.                     {
  213.                     cc = &dp -> colors[dp -> clrsize - 1];
  214.                     if (ctp)
  215.                         {
  216.                         /* redraw with the new color */
  217.                         redisptr( dp, ctp,
  218.                              dp -> clrsize - 1 );
  219.                         }
  220.                     }
  221.                 }
  222.             else if (ctp)
  223.                 {
  224.                 /* previous color: select "real" color 1 */
  225.  
  226.                 /* redraw with the new color */
  227.                 redisptr( dp, ctp, 1 );
  228.  
  229.                 cc = &dp -> colors[ctp -> color];
  230.                 }
  231.  
  232.             break;
  233.  
  234.         case 'p':    /* previous color */
  235.             if (dp -> im)
  236.                 beep();
  237.             else if (ctp)
  238.                 {
  239.                 /* deselect color */
  240.                 cc = 0;
  241.  
  242.                 /* -1 is previous color */
  243.  
  244.                 /* redraw with the new color */
  245.                 redisptr( dp, ctp, -1 );
  246.                 }
  247.             else beep();
  248.             break;
  249.  
  250.         case 'i':    /* im mode flip */
  251.             if (dp -> im)
  252.                 {
  253.                 /* im mode was on, turn off */
  254.  
  255.                 /* keep the old value */
  256.                 dp -> im_prev = dp -> im;
  257.                 dp -> im = 0;
  258.  
  259.                 /* select the background
  260.                  * (to erase the im level display) */
  261.                 cc = dp -> colors;
  262.                             }
  263.             else
  264.                 {
  265.                 /* im mode was off, turn on */
  266.                 /* use previous value, but only if it is > 0 */
  267.                 if ((dp -> im = dp -> im_prev) <= 0)
  268.                     dp -> im = 1;
  269.  
  270.                 /* select the first im level (not backgr.) */
  271.                 cc = &dp -> colors[1];
  272.                 }
  273.  
  274.             /* no transform is selected */
  275.             ctp = 0;
  276.  
  277.             break;
  278.  
  279.         case 'M':    /* increase im max level */
  280.             if (dp -> im)
  281.                 {
  282.                 /* im mode */
  283.                 /* (dp -> im starts at 1) */
  284.                 if (dp -> im < dp -> clrsize - 2)
  285.                     ++dp -> im;
  286.                 else beep();
  287.                 }
  288.             else beep();
  289.  
  290.             break;
  291.  
  292.         case 'm':    /* decrease im max level */
  293.             if (dp -> im)
  294.                 {
  295.                 /* im mode */
  296.                 if (dp -> im > 1)
  297.                     {
  298.                     --dp -> im;
  299.  
  300.                     /* check current level */
  301.                     /* (dp -> im starts at 1) */
  302.                     if (cc > &dp -> colors[dp -> im])
  303.                         cc = &dp -> colors[dp -> im];
  304.                     }
  305.                 else beep();
  306.                 }
  307.             else beep();
  308.  
  309.             break;
  310.  
  311.         case 0:        /* a mouse button */
  312.             /* not valid if im mode */
  313.             if (dp -> im)
  314.                 {
  315.                 beep();
  316.                 break;
  317.                 }
  318.  
  319.             /* a transform is selected, find out which one! */
  320.             i = findtrans( dp -> tp, dp -> size, &dp -> area,
  321.                          gs.gm_xpos, gs.gm_ypos );
  322.  
  323.             /* found it? */
  324.             if (i < 0)
  325.                 break;    /* no */
  326.  
  327.             /* set the pointers for current color and transform */
  328.             ctp = &dp -> tp[i];
  329.             if (ctp -> color >= dp -> clrsize)
  330.                 {
  331.                 /* out of range */
  332.                 /* redraw with the new color */
  333.                 redisptr( dp, ctp, 1 );
  334.                 }
  335.  
  336.             if (ctp -> color < 0)
  337.                 {
  338.                 /* previous color */
  339.                 cc = 0;
  340.                 }
  341.             else cc = &dp -> colors[ctp -> color];
  342.  
  343.             break;
  344.  
  345.         default:    /* ?? */
  346.             break;
  347.         }
  348.     }
  349. }
  350.  
  351. /* local routines */
  352. static void redisptr( ifsdes *dp, transform *tp, int newcolor )
  353. {
  354. gm_hide();
  355.  
  356. /* erase */
  357. setcolor( (tp -> color > 0) ? tp -> color : dp -> textcolor );
  358. xdistrans( tp, &dp -> area );
  359.  
  360. /* change color and redisplay */
  361. tp -> color = newcolor;
  362. setcolor( (tp -> color > 0) ? tp -> color : dp -> textcolor );
  363. xdistrans( tp, &dp -> area );
  364.  
  365. setcolor( dp -> textcolor );
  366. gm_show();
  367. }
  368.