home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / vos2-121.zip / v / iconed / coldlg.cpp < prev    next >
C/C++ Source or Header  |  1998-07-03  |  12KB  |  420 lines

  1. //========================= coldlg.cxx ==================================
  2. // This the source for the color palette dialog class 
  3. //
  4. // Copyright (C) 1996 Philip Eckenroth, Mike Tipping, Marilee Padilla,
  5. //                    John Fredric Jr. Masciantoni, and Bruce E. Wampler.
  6. //
  7. // This file is part of the V Icon Editor, and is covered
  8. // under the terms of the GNU General Public License,
  9. // Version 2. This program has NO WARRANTY. See the source file
  10. // viedapp.cpp for more complete information about license terms.
  11. //
  12. // This file, while loosly tied to the iconed editor, serves as a pretty
  13. // good example of what all can be done with a dialog. Some of the
  14. // tricks are used here because each icon edit window needs its own
  15. // color palette and color picker. So we do some fancy stuff to support
  16. // that. First, the dialog itself is defined as an automatic variable
  17. // in the init routing. This places the definition on the stack instead
  18. // of in static data space. Then, space for a definition for each
  19. // instance of the color dialog object is allocated, and the definitions
  20. // of the dialog are copied. Finally, each color button in the dialog
  21. // is dynamically pointed to the appropriate cell of the local copy
  22. // of the Pal palette. Not all that hard once you see it done!
  23. //=======================================================================
  24.  
  25. #include <iostream.h>
  26. #include "coldlg.h"
  27. #include "viedcmdw.h"
  28. #include <v/vcolor.h>
  29. #include <v/vapp.h>
  30. #include <v/vwindow.h>
  31. #include <v/vicon.h>
  32. #include <v/vutil.h>
  33. #include <v/vreply.h>
  34.  
  35. // Set the pen default colors
  36.  
  37. // Set Item Values for RGB
  38. const ItemVal iRed = 1000;
  39. const ItemVal iGreen = 1001;
  40. const ItemVal iBlue = 1002;
  41.  
  42.  
  43. //==========================>>> SetCmdObjItemList <<<==============================
  44.   void vColorDialog::SetCmdObjItemList(CommandObject* cList, ItemVal id, void* ptr)
  45.   {
  46.     // This is called to change the attrs field of the item with given id
  47.  
  48.     // scan the entire list
  49.  
  50.     int ix;
  51.     for (ix = 0 ; cList && (cList[ix].cmdType != C_EndOfList) ; ++ix)
  52.       {
  53.     if (cList[ix].cmdId == id)        // the one we want!
  54.       {
  55.         cList[ix].itemList = ptr;        // change
  56.         break;
  57.       }
  58.       }
  59.    }
  60.  
  61. //========================>>> vColorDialog::vColorDialog <<<====================
  62.  vColorDialog::vColorDialog(vBaseWindow* bw, int All, myCanvasPane* cnv) :
  63.     vDialog(bw, 0, "Select Color")
  64.   {
  65.     // Creates a color palette dialog window
  66.  
  67.     UserDebug(Constructor,"vColorDialog::vColorDialog(basewindow)\n")
  68.     pWin = (vWindow*) bw;
  69.     myCanvas = (myCanvasPane*) cnv;
  70.     init();
  71.   }
  72.  
  73. //========================>>> vColorDialog::init <<<====================
  74.  void vColorDialog::init()
  75.   {
  76.     // Initializes the color palette dialog window
  77.  
  78.     static int sldR[2] = {0,255};    // Range for sliders
  79.  
  80.     // Define the color palette dialog window on the stack
  81.     static CommandObject DefaultCmds[] =
  82.     {
  83.      {C_Label, lbL1, 0,"             ",NoList,CA_MainMsg,isSens,NoFrame, 0, 0},
  84.     
  85.      //----------- The set color frame ---------------------------------
  86.  
  87.      {C_Frame, frWhich, 0, "", NoList, CA_NoBorder, isSens,
  88.     0, 0, lbL1},
  89.  
  90.      // this is used as a spacer to center the current foreground and backgroud
  91.      {C_ColorButton, cbtPen, 1," ",0,CA_None,isSens,  // set to clrPen
  92.     frWhich, 0, 0, 40, "Current Color"},
  93.      {C_Button, btSetFG, 0, "Apply: Foreground",NoList, CA_None, isSens, frWhich,
  94.     cbtPen, 0, 0, "Make Foreground Color"},
  95.      {C_Button, btSetBG, 0, "Apply: Background",NoList, CA_None, isSens, frWhich,
  96.         cbtPen, btSetFG, 0, "Make Background Color"},
  97.  
  98. // put in default color pallette
  99.      {C_Frame, frPalette, 0, "", NoList, CA_None, isSens, NoFrame, 0, frWhich},
  100.  
  101. #define frColors 1499    // must be this
  102.      {C_Frame,frColors,0,"",NoList,CA_NoBorder | CA_NoSpace,isSens,frPalette,0,0},
  103.  
  104. #define vC_Size 12
  105. #include  "palclrs.h"
  106.  
  107.  
  108.      // Add Slider frame
  109.      {C_Frame, frSliders, 0, "Modify", NoList, CA_None, isSens, 
  110.         NoFrame, 0, frPalette},
  111.  
  112.      // add red slider
  113.      {C_ColorButton,iRed,0," ",(void*)&vStdColors[vC_Red],CA_None,isSens,
  114.     frSliders,0,0, 0, "Click to enter Red value"},
  115.      {C_Slider, Rslider, 0, "", (void*)&sldR[0], CA_None, isSens, frSliders,
  116.         iRed, 0, 256, "Set Red Value"},
  117.      {C_Text, Rtext, 0, "   ", "0  ", CA_None, isSens, frSliders, 
  118.         Rslider, 0},
  119.  
  120.      // add green slider
  121.      {C_ColorButton,iGreen,0," ",(void*)&vStdColors[vC_Green],CA_None,isSens,
  122.     frSliders,0,Rslider, 0, "Click to enter Green value"},
  123.      {C_Slider, Gslider, 0, "", (void*)&sldR[0], CA_None, isSens, frSliders,
  124.         iGreen, Rslider, 256, "Set Green Value"},
  125.      {C_Text, Gtext, 0, "", "0  ", CA_None, isSens, frSliders, 
  126.         Gslider, Rslider},
  127.  
  128.      // add blue slider
  129.      {C_ColorButton,iBlue,0," ",(void*)&vStdColors[vC_Blue],CA_None,isSens,
  130.     frSliders,0,Gslider, 0, "Click to enter Blue value"},
  131.      {C_Slider, Bslider, 0, "", (void*)&sldR[0], CA_None, isSens, frSliders,
  132.         iBlue, Gslider, 256, "Set Blue Value"},
  133.      {C_Text, Btext, 0, "", "0  ", CA_None, isSens, frSliders, 
  134.         Bslider, Gslider},
  135.  
  136.      {C_Button, btReset, 0, "Reset",NoList, CA_None, isSens, NoFrame,
  137.     frSliders, frPalette, 0, "Reset selected color to original color"},
  138.  
  139.      {C_EndOfList,0,0,0,0,CA_None,0,0,0}
  140.   };
  141.  
  142.     vColor myPen;
  143.  
  144.     clrPen.Set(0,0,0);
  145.  
  146.     // Dynamically build the command list so that each dialog
  147.     // can have its own set of colors
  148.     int ix;
  149.     for (ix = 0 ; DefaultCmds[ix].cmdType != C_EndOfList ; ++ix)
  150.       ;
  151.     
  152.     myCmds = new CommandObject[ix+1];    // dynamic space
  153.  
  154.     for (int ij = 0 ; ij <= ix ; ++ij)
  155.     myCmds[ij] = DefaultCmds[ij];
  156.     
  157.  
  158.     for (int id =  0 ; id <= PalColors ; ++id)
  159.     SetCmdObjItemList(myCmds, id+1500, (void*)&Pal[id]);
  160.  
  161.     SetCmdObjItemList(myCmds, cbtPen, (void*)&clrPen);
  162.  
  163.     AddDialogCmds(myCmds);        // add the predefined commands
  164.  
  165.     // message id of current color
  166.     penColorMsg = 1500;
  167.  
  168.     // value id of current color
  169.     penColorVal = 0;
  170.  
  171.     // Set color buttons
  172.     saveColor = clrPen;
  173.     SetValue(cbtPen,0,ChangeColor);
  174.   }
  175.  
  176. //=======================>>> vColorDialog::~vColorDialog <<<====================
  177.   vColorDialog::~vColorDialog()
  178.   {
  179.     // Destroys the class vColorDialog
  180.     UserDebug(Destructor,"vColorDialog::~vColorDialog() destructor\n")
  181.     delete myCmds;
  182.   }
  183.  
  184. //===================>>> vColorDialog::UpdatePalette <<<=========================
  185.   void vColorDialog::UpdatePalette(vColor* theColors, int numColors)
  186.   {
  187.  
  188.     int lim = (numColors < PalColors) ? numColors : PalColors;
  189.  
  190.     // message id of current color
  191.     penColorMsg = 1500;
  192.  
  193.     // value id of current color
  194.     penColorVal = 0;
  195.  
  196.     // set color values
  197.     for (int i = 0 ; i < lim ; i++)    // Copy supplied colors
  198.       {
  199.     Pal[i].ResetColor(theColors[i]);
  200.     SetValue(1500+i,0,ChangeColor);
  201.       }
  202.  
  203.     for (int ix = lim ; ix < PalColors ; ++ix)
  204.       {
  205.     Pal[ix].ResetColor(0,0,0);
  206.     SetValue(1500+ix,0,ChangeColor);
  207.       }
  208.  
  209.     // Set color buttons
  210.     clrPen.ResetColor(Pal[0]);
  211.     saveColor = clrPen;
  212.     SetValue(cbtPen,0,ChangeColor);
  213.   }
  214.  
  215. //===================>>> vColorDialog::UpdateColors <<<=========================
  216.   void vColorDialog::UpdateColors(int mode, int value)
  217.   {
  218.     //  Update color buttons (current and color button index) after scroll
  219.     //  bar update
  220.  
  221.     if (mode == Rslider)
  222.       {
  223.     /* set value into palette */
  224.  
  225.     // Reset color - the R is new
  226.     Pal[penColorVal].ResetColor(value,
  227.         Pal[penColorVal].g(),
  228.         Pal[penColorVal].b());
  229.       } 
  230.     else if (mode == Gslider) 
  231.       {
  232.     // Reset color - the G is new
  233.     Pal[penColorVal].ResetColor(Pal[penColorVal].r(),
  234.         value,
  235.         Pal[penColorVal].b());
  236.       } 
  237.     else 
  238.       {
  239.     // Reset color - the B is new
  240.     Pal[penColorVal].ResetColor(Pal[penColorVal].r(),
  241.         Pal[penColorVal].g(),
  242.         value);
  243.       }
  244.  
  245.     // update pen
  246.     clrPen.ResetColor(Pal[penColorVal]);
  247.  
  248.     // update color buttons
  249.     SetValue(penColorMsg,0,ChangeColor);
  250.     SetValue(cbtPen, 0, ChangeColor);
  251.   }
  252.  
  253. //=====================>>> vColorDialog::UpdateSliders <<<======================
  254.   void vColorDialog::UpdateSliders(vColor CurColor)
  255.   {
  256.   //  Update Sliders with color
  257.  
  258.     char strbuff[20];
  259.     int index;
  260.  
  261.     // Set red value slider
  262.     index = CurColor.r();
  263.     index = (int) ((float)index / (float)255 * (float)100);
  264.     SetValue(Rslider, index, Value);
  265.     IntToStr(CurColor.r(),strbuff);
  266.     SetString(Rtext,strbuff);
  267.  
  268.     // Set green value slider
  269.     index = CurColor.g();
  270.     index = (int) ((float)index / (float)255 * (float)100);
  271.     SetValue(Gslider, index, Value);
  272.     IntToStr(CurColor.g(),strbuff);
  273.     SetString(Gtext,strbuff);
  274.  
  275.     // Set blue value slider
  276.     index = CurColor.b();
  277.     index = (int) ((float)index / (float)255 * (float)100);
  278.     SetValue(Bslider, index, Value);
  279.     IntToStr(CurColor.b(),strbuff);
  280.     SetString(Btext,strbuff);
  281.   }
  282.  
  283. //====================>>> vColorDialog::DialogCommand <<<=======================
  284.   void vColorDialog::DialogCommand(ItemVal id, ItemVal val, CmdType ctype)
  285.   {
  286.     // After the user has selected a command from the dialog, this routine is
  287.     // called with the value.  This code would be generated by Vigr.
  288.  
  289.     UserDebug2(CmdEvents,"vColorDialog::DialogCommand(id:%d, val:%d)\n",id, val)
  290.  
  291.     char buff[32];
  292.  
  293.     switch (id)        // We will do some things depending on value
  294.       {
  295.         case Rslider:    // set value of red scale
  296.           {
  297.             int index = (int) val;
  298.  
  299.         IntToStr(index,buff);
  300.             SetString(Rtext,buff);       // set string
  301.           
  302.             UpdateColors(Rslider, index);
  303.  
  304.             break;
  305.           }
  306.  
  307.         case Gslider:    // set value of green scale
  308.           {
  309.             int index = (int) val;
  310.  
  311.         IntToStr(index,buff);
  312.             SetString(Gtext,buff);       // set string
  313.  
  314.             UpdateColors(Gslider, index);
  315.  
  316.             break;
  317.           }
  318.  
  319.         case Bslider:    // set value of blue scale
  320.           {
  321.             int index = (int) val;
  322.  
  323.         IntToStr(index,buff);
  324.             SetString(Btext,buff);       // set string
  325.  
  326.             UpdateColors(Bslider, index);
  327.  
  328.             break;
  329.           }
  330.  
  331.     case iBlue:
  332.       {
  333.         int index;
  334.         vReplyDialog rp(pWin);
  335.         if (rp.Reply("Enter Blue value 0-255:",buff,30) == M_Cancel)
  336.         break;
  337.         index = (int) StrToLong(buff);
  338.         if (index < 0 || index > 255)
  339.         break;
  340.         IntToStr(index,buff);
  341.             SetString(Btext,buff);       // set string
  342.         SetValue(Bslider,(index*100)/255,Value);
  343.             UpdateColors(Bslider, index);
  344.             break;
  345.       }
  346.  
  347.     case iRed:
  348.       {
  349.         int index;
  350.         vReplyDialog rp(pWin);
  351.         if (rp.Reply("Enter Red value 0-255:",buff,30) == M_Cancel)
  352.         break;
  353.         index = (int) StrToLong(buff);
  354.         if (index < 0 || index > 255)
  355.         break;
  356.         IntToStr(index,buff);
  357.             SetString(Rtext,buff);       // set string
  358.         SetValue(Rslider,(index*100)/255,Value);
  359.             UpdateColors(Rslider, index);
  360.             break;
  361.       }
  362.  
  363.     case iGreen:
  364.       {
  365.         int index;
  366.         vReplyDialog rp(pWin);
  367.         if (rp.Reply("Enter Green value 0-255:",buff,30) == M_Cancel)
  368.         break;
  369.         index = (int) StrToLong(buff);
  370.         if (index < 0 || index > 255)
  371.         break;
  372.         IntToStr(index,buff);
  373.         SetValue(Gslider,(index*100)/255,Value);
  374.             SetString(Gtext,buff);       // set string
  375.             UpdateColors(Gslider, index);
  376.             break;
  377.       }
  378.  
  379.     case btSetBG:
  380.         myCanvas->SetBGColor(clrPen);
  381.         break;
  382.  
  383.     case btSetFG:
  384.         myCanvas->SetFGColor(clrPen);
  385.         break;
  386.  
  387.     case btReset:
  388.         Pal[penColorVal].ResetColor(saveColor);
  389.         // update color buttons
  390.         clrPen.ResetColor(saveColor);
  391.         SetValue(penColorMsg,0,ChangeColor);
  392.         SetValue(cbtPen, 0, ChangeColor);
  393.         UpdateSliders(clrPen);
  394.         break;
  395.  
  396.  
  397.     case btClose:        // Close the dialog
  398.       {
  399.         CloseDialog();
  400.         break;
  401.       }
  402.  
  403.     default:        // set a color...
  404.  
  405.         if (id >= 1500 && id <= 1499+PalColors)  // color button
  406.           {
  407.         penColorMsg = id;
  408.         penColorVal = val;
  409.         saveColor = Pal[val];
  410.         clrPen.ResetColor(Pal[val]);
  411.         SetValue(cbtPen,0,ChangeColor);
  412.         UpdateSliders(clrPen);
  413.         break;
  414.           }
  415.       }
  416.  
  417.     // All commands should also route through the parent handler
  418.     vDialog::DialogCommand(id,val,ctype);
  419.   }
  420.