home *** CD-ROM | disk | FTP | other *** search
/ Learn 3D Graphics Programming on the PC / Learn_3D_Graphics_Programming_on_the_PC_Ferraro.iso / rwwin / xolor.c_ / xolor.bin
Text File  |  1995-11-14  |  15KB  |  369 lines

  1. /**********************************************************************
  2.  *
  3.  * File :     color.c
  4.  *
  5.  * Abstract : A simple color picker dialog. We could use the standard
  6.  *            Windows Color Chooser dialog but I think it is a little
  7.  *            fussy and elaborate for our purposes.
  8.  *
  9.  *            This application had been written to be compatible with
  10.  *            both the fixed and floating-point versions of the
  11.  *            RenderWare library, i.e., it uses the macros CREAL,
  12.  *            INT2REAL, RAdd, RDiv, RSub etc. If your application is
  13.  *            intended for the floating-point version of the library
  14.  *            only these macros are not necessary.
  15.  *
  16.  *            Please note that this application is intended for
  17.  *            demonstration purposes only. No support will be
  18.  *            provided for this code and it comes with no warranty.
  19.  *
  20.  **********************************************************************
  21.  *
  22.  * This file is a product of Criterion Software Ltd.
  23.  *
  24.  * This file is provided as is with no warranties of any kind and is
  25.  * provided without any obligation on Criterion Software Ltd. or
  26.  * Canon Inc. to assist in its use or modification.
  27.  *
  28.  * Criterion Software Ltd. will not, under any
  29.  * circumstances, be liable for any lost revenue or other damages arising
  30.  * from the use of this file.
  31.  *
  32.  * Copyright (c) 1994, 1995 Criterion Software Ltd.
  33.  * All Rights Reserved.
  34.  *
  35.  * RenderWare is a trademark of Canon Inc.
  36.  *
  37.  **********************************************************************/
  38.  
  39. /**********************************************************************
  40.  *
  41.  * Header files.
  42.  *
  43.  **********************************************************************/
  44.  
  45. #include <windows.h>
  46. #include <stdlib.h>
  47. #include <stdio.h>
  48. #include <string.h>
  49.  
  50. #include <rwlib.h>
  51. #include <rwwin.h>
  52.  
  53. #include "resource.h"
  54.  
  55. #include "common.h"
  56. #include "color.h"
  57.  
  58. /**********************************************************************
  59.  *
  60.  * Constants.
  61.  *
  62.  **********************************************************************/
  63.  
  64. /**********************************************************************
  65.  *
  66.  * Functions.
  67.  *
  68.  **********************************************************************/
  69.  
  70. /**********************************************************************/
  71.  
  72. /*
  73.  * The color picker dialog message procedure. The color picker dialog
  74.  * box is invoked with the DialogBoxParam() Windows API call. The
  75.  * additional parameter being a pointer to an RwRGBColor containing
  76.  * the color to initialize the dialog to. This same color will hold the
  77.  * new color picked if the user clicks ok.
  78.  */
  79. BOOL CALLBACK
  80. ColorDlgProc(HWND dialog, UINT message, WPARAM wParam, LPARAM lParam)
  81. {
  82.     static RwRGBColor  *RealColor;
  83.     static COLORREF     Color;
  84.     static HPALETTE     Palette;
  85.     
  86.     RwBool              isPaletteBased;
  87.     int                 r;
  88.     int                 g;
  89.     int                 b;
  90.     char                buffer[10];
  91.     DRAWITEMSTRUCT FAR *drawItem;
  92.     HPALETTE            oldPalette=NULL;
  93.     HPEN                pen;
  94.     HPEN                oldPen;
  95.     HBRUSH              brush;
  96.     HBRUSH              oldBrush;
  97.     HWND                scrollBar=NULL;
  98.     int                 code;
  99.     int                 pos;
  100.     int                 newPos;
  101.     int                 channel;
  102.     int                 id;
  103.     
  104.     switch (message)
  105.     {
  106.         case WM_INITDIALOG:
  107.             /*
  108.              * Stash away the supplied color so that we can store the
  109.              * picked color in it when the user clicks ok.
  110.              */
  111.             RealColor = (RwRGBColor *)lParam;
  112.             
  113.             /*
  114.              * If we are running on a palette based display we will
  115.              * select and realize RenderWare's own color palette when
  116.              * drawing the preview button to give a realistic picture
  117.              * of the available colors. So cache the RenderWare palette
  118.              * if we are using one.
  119.              */
  120.             RwGetDeviceInfo(rwPALETTEBASED, &isPaletteBased, sizeof(isPaletteBased));
  121.             if (isPaletteBased)
  122.                 RwGetDeviceInfo(rwPALETTE, &Palette, sizeof(Palette));
  123.             else
  124.                 Palette = NULL;
  125.             
  126.             /*
  127.              * Convert the RenderWare real color to a Windows COLORREF.
  128.              */
  129.             Color = RGBToColorRef(RealColor);
  130.             r = GetRValue(Color);
  131.             g = GetGValue(Color);
  132.             b = GetBValue(Color);
  133.  
  134.             /*
  135.              * Initialize the scrollbars.
  136.              */
  137.             SetScrollRange(GetDlgItem(dialog, IDC_COLOR_RSLIDER), SB_CTL, 0, 255, FALSE);
  138.             SetScrollRange(GetDlgItem(dialog, IDC_COLOR_GSLIDER), SB_CTL, 0, 255, FALSE);
  139.             SetScrollRange(GetDlgItem(dialog, IDC_COLOR_BSLIDER), SB_CTL, 0, 255, FALSE);
  140.             SetScrollPos(GetDlgItem(dialog, IDC_COLOR_RSLIDER),   SB_CTL, r, FALSE);
  141.             SetScrollPos(GetDlgItem(dialog, IDC_COLOR_GSLIDER),   SB_CTL, g, FALSE);
  142.             SetScrollPos(GetDlgItem(dialog, IDC_COLOR_BSLIDER),   SB_CTL, b, FALSE);
  143.             
  144.             /*
  145.              * Initialize the edit controls.
  146.              */
  147.             wsprintf(buffer, "%d", r);
  148.             SetDlgItemText(dialog, IDC_COLOR_REDIT, buffer);
  149.             wsprintf(buffer, "%d", g);
  150.             SetDlgItemText(dialog, IDC_COLOR_GEDIT, buffer);
  151.             wsprintf(buffer, "%d", b);
  152.             SetDlgItemText(dialog, IDC_COLOR_BEDIT, buffer);
  153.             return FALSE;
  154.             
  155.         case WM_HSCROLL:
  156. #if       defined(WIN32)
  157.             code      = (int)LOWORD(wParam);
  158.             newPos    = (int)HIWORD(wParam);
  159.             scrollBar = (HWND)lParam;
  160.             id        = (int)GetWindowLong(scrollBar, GWL_ID);
  161. #else
  162.             code      = (int)wParam;
  163.             newPos    = (int)LOWORD(lParam);
  164.             scrollBar = (HWND)HIWORD(lParam);
  165.             id        = (int)GetWindowWord(scrollBar, GWW_ID);
  166. #endif            
  167.   
  168.             /* 
  169.              * As we preview continuously SB_ENDSCROLL is not important to us.
  170.              */
  171.             if (code == SB_ENDSCROLL)
  172.                 return TRUE;
  173.             
  174.             /*
  175.              * Update the scroll bar.
  176.              */
  177.             pos = GetScrollPos(scrollBar, SB_CTL);
  178.             switch (code)
  179.             {
  180.                 case SB_BOTTOM:        pos =  255;    break;
  181.                 case SB_LINEDOWN:      pos += 4;      break;
  182.                 case SB_LINEUP:        pos -= 4;      break;
  183.                 case SB_PAGEDOWN:      pos += 16;     break;
  184.                 case SB_PAGEUP:        pos -= 16;     break;
  185.                 case SB_THUMBTRACK:    pos =  newPos; break;
  186.                 case SB_THUMBPOSITION: pos =  newPos; break;
  187.                 case SB_TOP:           pos =  0;      break;
  188.             }
  189.             
  190.             /*
  191.              * Clamp the position to the valid range.
  192.              */
  193.             if (pos < 0)
  194.                 pos = 0;
  195.             else if (pos > 255)
  196.                 pos = 255;
  197.             SetScrollPos(scrollBar, SB_CTL, pos, TRUE);
  198.             
  199.             /*
  200.              * Compute the new color (depending on which channel has changed)
  201.              * and update the appropriate edit control.
  202.              */
  203.             switch (id)
  204.             {
  205.                 case IDC_COLOR_RSLIDER:
  206.                     wsprintf(buffer, "%d", pos);
  207.                     SetDlgItemText(dialog, IDC_COLOR_REDIT, buffer);
  208.                     Color = PALETTERGB(pos, GetGValue(Color), GetBValue(Color));
  209.                     break;
  210.                 case IDC_COLOR_GSLIDER:
  211.                     wsprintf(buffer, "%d", pos);
  212.                     SetDlgItemText(dialog, IDC_COLOR_GEDIT, buffer);
  213.                     Color = PALETTERGB(GetRValue(Color), pos, GetBValue(Color));
  214.                     break;
  215.                 case IDC_COLOR_BSLIDER:
  216.                     wsprintf(buffer, "%d", pos);
  217.                     SetDlgItemText(dialog, IDC_COLOR_BEDIT, buffer);
  218.                     Color = PALETTERGB(GetRValue(Color), GetGValue(Color), pos);
  219.                     break;
  220.             }
  221.             
  222.             /*
  223.              * Request a redraw of the owner-draw preview button.
  224.              */
  225.             InvalidateRect(GetDlgItem(dialog, IDC_COLOR_PREVIEW), NULL, FALSE);
  226.             return TRUE;
  227.             
  228.         case WM_DRAWITEM:
  229. #if defined(__WINDOWS_386__)
  230.             drawItem = (DRAWITEMSTRUCT FAR *)MK_FP32((void*)lParam);
  231. #else
  232.             drawItem = (DRAWITEMSTRUCT FAR *)lParam;
  233. #endif
  234.             
  235.             switch (wParam)
  236.             {
  237.                 case IDC_COLOR_PREVIEW:
  238.                     /*
  239.                      * If we have a palette based output device then
  240.                      * select and realize the palette (we realize
  241.                      * it as a background palette as we don't want to
  242.                      * be too aggresive about getting the colors we ask
  243.                      * for - it is only for feedback after all).
  244.                      */
  245.                     if (Palette != (HPALETTE)0)
  246.                     {
  247.                         oldPalette = SelectPalette(drawItem->hDC, Palette, TRUE);
  248.                         RealizePalette(drawItem->hDC);
  249.                     }
  250.                                          
  251.                     /*
  252.                      * Draw a rectangle framed by a black line and filled
  253.                      * with the current color.
  254.                      */
  255.                     pen      = GetStockObject(BLACK_PEN);
  256.                     oldPen   = SelectObject(drawItem->hDC, pen);
  257.                     brush    = CreateSolidBrush(Color);
  258.                     oldBrush = SelectObject(drawItem->hDC, brush);
  259.                     Rectangle(drawItem->hDC, drawItem->rcItem.left,
  260.                                              drawItem->rcItem.top,
  261.                                              drawItem->rcItem.right,
  262.                                              drawItem->rcItem.bottom);
  263.                     SelectObject(drawItem->hDC, oldBrush);
  264.                     DeleteObject(brush);
  265.                     SelectObject(drawItem->hDC, oldPen);
  266.  
  267.                     if (Palette != (HPALETTE)0)
  268.                     {
  269.                         SelectPalette(drawItem->hDC, oldPalette, TRUE);
  270.                         RealizePalette(drawItem->hDC);
  271.                     }
  272.  
  273.                     break;
  274.             }
  275.             return TRUE;
  276.         
  277.         case WM_COMMAND:
  278.         {
  279. #ifdef WIN32
  280.              HWND hwndCtl = (HWND) lParam;
  281.             WORD wID = LOWORD(wParam);
  282.             WORD wNotifyCode = HIWORD(wParam);
  283. #else
  284.             HWND hwndCtl = (HWND) LOWORD(lParam);
  285.             WORD wID = wParam;
  286.             WORD wNotifyCode = HIWORD(lParam);
  287. #endif
  288.             switch (wID)
  289.             {
  290.                 case IDC_COLOR_REDIT:
  291.                 case IDC_COLOR_GEDIT:
  292.                 case IDC_COLOR_BEDIT:
  293.                     /*
  294.                      * For the edit controls we only take any notice when the
  295.                      * control loses the input focus.
  296.                      */
  297.                     if (wNotifyCode == EN_KILLFOCUS)
  298.                     {
  299.                         /*
  300.                          * Get the edit control's text.
  301.                          */
  302.                         GetDlgItemText(dialog, wID, buffer, sizeof(buffer));
  303.                         
  304.                         /*
  305.                          * Ensure its a valid value.
  306.                          */
  307.                         if ((sscanf(buffer, "%d", &channel) == 1) &&
  308.                             (channel >= 0) && (channel <= 255))
  309.                         {
  310.                             /*
  311.                              * Build the new current color and update the appropriate
  312.                              * slider.
  313.                              */
  314.                             switch (wID)
  315.                             {
  316.                                 case IDC_COLOR_REDIT:
  317.                                     scrollBar = GetDlgItem(dialog, IDC_COLOR_RSLIDER);
  318.                                     Color = PALETTERGB(channel, GetGValue(Color), GetBValue(Color));
  319.                                     break;
  320.                                 case IDC_COLOR_GEDIT:
  321.                                     scrollBar = GetDlgItem(dialog, IDC_COLOR_GSLIDER);
  322.                                     Color = PALETTERGB(GetGValue(Color), channel, GetBValue(Color));
  323.                                     break;
  324.                                 case IDC_COLOR_BEDIT:
  325.                                     scrollBar = GetDlgItem(dialog, IDC_COLOR_BSLIDER);
  326.                                     Color = PALETTERGB(GetRValue(Color), GetGValue(Color), channel);
  327.                                     break;
  328.                             }
  329.                             SetScrollPos(scrollBar, SB_CTL, channel, TRUE);
  330.                         }
  331.                         else
  332.                         {
  333.                             /*
  334.                              * Not valid, put the existing value back.
  335.                              */
  336.                             switch (wID)
  337.                             {
  338.                                 case IDC_COLOR_REDIT: scrollBar = GetDlgItem(dialog, IDC_COLOR_RSLIDER); break;
  339.                                 case IDC_COLOR_GEDIT: scrollBar = GetDlgItem(dialog, IDC_COLOR_GSLIDER); break;
  340.                                 case IDC_COLOR_BEDIT: scrollBar = GetDlgItem(dialog, IDC_COLOR_BSLIDER); break;
  341.                             }
  342.                             channel = GetScrollPos(scrollBar, SB_CTL);
  343.                             sprintf(buffer, "%d", channel);
  344.                             SetDlgItemText(dialog, wID, buffer);
  345.                         }
  346.                     }
  347.                     break;
  348.                     
  349.                 case IDOK:
  350.                     /*
  351.                      * Convert the Windows COLORREF back into the
  352.                      * RenderWare color.
  353.                      */
  354.                     ColorRefToRGB(Color, RealColor);
  355.                     EndDialog(dialog, IDOK);
  356.                     break; 
  357.                     
  358.                 case IDCANCEL:
  359.                     EndDialog(dialog, IDCANCEL);
  360.                     break;
  361.             }
  362.             return TRUE;
  363.         }
  364.     }
  365.     return FALSE;
  366. }
  367.  
  368. /**********************************************************************/
  369.