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
Wrap
Text File
|
1995-11-14
|
15KB
|
369 lines
/**********************************************************************
*
* File : color.c
*
* Abstract : A simple color picker dialog. We could use the standard
* Windows Color Chooser dialog but I think it is a little
* fussy and elaborate for our purposes.
*
* This application had been written to be compatible with
* both the fixed and floating-point versions of the
* RenderWare library, i.e., it uses the macros CREAL,
* INT2REAL, RAdd, RDiv, RSub etc. If your application is
* intended for the floating-point version of the library
* only these macros are not necessary.
*
* Please note that this application is intended for
* demonstration purposes only. No support will be
* provided for this code and it comes with no warranty.
*
**********************************************************************
*
* This file is a product of Criterion Software Ltd.
*
* This file is provided as is with no warranties of any kind and is
* provided without any obligation on Criterion Software Ltd. or
* Canon Inc. to assist in its use or modification.
*
* Criterion Software Ltd. will not, under any
* circumstances, be liable for any lost revenue or other damages arising
* from the use of this file.
*
* Copyright (c) 1994, 1995 Criterion Software Ltd.
* All Rights Reserved.
*
* RenderWare is a trademark of Canon Inc.
*
**********************************************************************/
/**********************************************************************
*
* Header files.
*
**********************************************************************/
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <rwlib.h>
#include <rwwin.h>
#include "resource.h"
#include "common.h"
#include "color.h"
/**********************************************************************
*
* Constants.
*
**********************************************************************/
/**********************************************************************
*
* Functions.
*
**********************************************************************/
/**********************************************************************/
/*
* The color picker dialog message procedure. The color picker dialog
* box is invoked with the DialogBoxParam() Windows API call. The
* additional parameter being a pointer to an RwRGBColor containing
* the color to initialize the dialog to. This same color will hold the
* new color picked if the user clicks ok.
*/
BOOL CALLBACK
ColorDlgProc(HWND dialog, UINT message, WPARAM wParam, LPARAM lParam)
{
static RwRGBColor *RealColor;
static COLORREF Color;
static HPALETTE Palette;
RwBool isPaletteBased;
int r;
int g;
int b;
char buffer[10];
DRAWITEMSTRUCT FAR *drawItem;
HPALETTE oldPalette=NULL;
HPEN pen;
HPEN oldPen;
HBRUSH brush;
HBRUSH oldBrush;
HWND scrollBar=NULL;
int code;
int pos;
int newPos;
int channel;
int id;
switch (message)
{
case WM_INITDIALOG:
/*
* Stash away the supplied color so that we can store the
* picked color in it when the user clicks ok.
*/
RealColor = (RwRGBColor *)lParam;
/*
* If we are running on a palette based display we will
* select and realize RenderWare's own color palette when
* drawing the preview button to give a realistic picture
* of the available colors. So cache the RenderWare palette
* if we are using one.
*/
RwGetDeviceInfo(rwPALETTEBASED, &isPaletteBased, sizeof(isPaletteBased));
if (isPaletteBased)
RwGetDeviceInfo(rwPALETTE, &Palette, sizeof(Palette));
else
Palette = NULL;
/*
* Convert the RenderWare real color to a Windows COLORREF.
*/
Color = RGBToColorRef(RealColor);
r = GetRValue(Color);
g = GetGValue(Color);
b = GetBValue(Color);
/*
* Initialize the scrollbars.
*/
SetScrollRange(GetDlgItem(dialog, IDC_COLOR_RSLIDER), SB_CTL, 0, 255, FALSE);
SetScrollRange(GetDlgItem(dialog, IDC_COLOR_GSLIDER), SB_CTL, 0, 255, FALSE);
SetScrollRange(GetDlgItem(dialog, IDC_COLOR_BSLIDER), SB_CTL, 0, 255, FALSE);
SetScrollPos(GetDlgItem(dialog, IDC_COLOR_RSLIDER), SB_CTL, r, FALSE);
SetScrollPos(GetDlgItem(dialog, IDC_COLOR_GSLIDER), SB_CTL, g, FALSE);
SetScrollPos(GetDlgItem(dialog, IDC_COLOR_BSLIDER), SB_CTL, b, FALSE);
/*
* Initialize the edit controls.
*/
wsprintf(buffer, "%d", r);
SetDlgItemText(dialog, IDC_COLOR_REDIT, buffer);
wsprintf(buffer, "%d", g);
SetDlgItemText(dialog, IDC_COLOR_GEDIT, buffer);
wsprintf(buffer, "%d", b);
SetDlgItemText(dialog, IDC_COLOR_BEDIT, buffer);
return FALSE;
case WM_HSCROLL:
#if defined(WIN32)
code = (int)LOWORD(wParam);
newPos = (int)HIWORD(wParam);
scrollBar = (HWND)lParam;
id = (int)GetWindowLong(scrollBar, GWL_ID);
#else
code = (int)wParam;
newPos = (int)LOWORD(lParam);
scrollBar = (HWND)HIWORD(lParam);
id = (int)GetWindowWord(scrollBar, GWW_ID);
#endif
/*
* As we preview continuously SB_ENDSCROLL is not important to us.
*/
if (code == SB_ENDSCROLL)
return TRUE;
/*
* Update the scroll bar.
*/
pos = GetScrollPos(scrollBar, SB_CTL);
switch (code)
{
case SB_BOTTOM: pos = 255; break;
case SB_LINEDOWN: pos += 4; break;
case SB_LINEUP: pos -= 4; break;
case SB_PAGEDOWN: pos += 16; break;
case SB_PAGEUP: pos -= 16; break;
case SB_THUMBTRACK: pos = newPos; break;
case SB_THUMBPOSITION: pos = newPos; break;
case SB_TOP: pos = 0; break;
}
/*
* Clamp the position to the valid range.
*/
if (pos < 0)
pos = 0;
else if (pos > 255)
pos = 255;
SetScrollPos(scrollBar, SB_CTL, pos, TRUE);
/*
* Compute the new color (depending on which channel has changed)
* and update the appropriate edit control.
*/
switch (id)
{
case IDC_COLOR_RSLIDER:
wsprintf(buffer, "%d", pos);
SetDlgItemText(dialog, IDC_COLOR_REDIT, buffer);
Color = PALETTERGB(pos, GetGValue(Color), GetBValue(Color));
break;
case IDC_COLOR_GSLIDER:
wsprintf(buffer, "%d", pos);
SetDlgItemText(dialog, IDC_COLOR_GEDIT, buffer);
Color = PALETTERGB(GetRValue(Color), pos, GetBValue(Color));
break;
case IDC_COLOR_BSLIDER:
wsprintf(buffer, "%d", pos);
SetDlgItemText(dialog, IDC_COLOR_BEDIT, buffer);
Color = PALETTERGB(GetRValue(Color), GetGValue(Color), pos);
break;
}
/*
* Request a redraw of the owner-draw preview button.
*/
InvalidateRect(GetDlgItem(dialog, IDC_COLOR_PREVIEW), NULL, FALSE);
return TRUE;
case WM_DRAWITEM:
#if defined(__WINDOWS_386__)
drawItem = (DRAWITEMSTRUCT FAR *)MK_FP32((void*)lParam);
#else
drawItem = (DRAWITEMSTRUCT FAR *)lParam;
#endif
switch (wParam)
{
case IDC_COLOR_PREVIEW:
/*
* If we have a palette based output device then
* select and realize the palette (we realize
* it as a background palette as we don't want to
* be too aggresive about getting the colors we ask
* for - it is only for feedback after all).
*/
if (Palette != (HPALETTE)0)
{
oldPalette = SelectPalette(drawItem->hDC, Palette, TRUE);
RealizePalette(drawItem->hDC);
}
/*
* Draw a rectangle framed by a black line and filled
* with the current color.
*/
pen = GetStockObject(BLACK_PEN);
oldPen = SelectObject(drawItem->hDC, pen);
brush = CreateSolidBrush(Color);
oldBrush = SelectObject(drawItem->hDC, brush);
Rectangle(drawItem->hDC, drawItem->rcItem.left,
drawItem->rcItem.top,
drawItem->rcItem.right,
drawItem->rcItem.bottom);
SelectObject(drawItem->hDC, oldBrush);
DeleteObject(brush);
SelectObject(drawItem->hDC, oldPen);
if (Palette != (HPALETTE)0)
{
SelectPalette(drawItem->hDC, oldPalette, TRUE);
RealizePalette(drawItem->hDC);
}
break;
}
return TRUE;
case WM_COMMAND:
{
#ifdef WIN32
HWND hwndCtl = (HWND) lParam;
WORD wID = LOWORD(wParam);
WORD wNotifyCode = HIWORD(wParam);
#else
HWND hwndCtl = (HWND) LOWORD(lParam);
WORD wID = wParam;
WORD wNotifyCode = HIWORD(lParam);
#endif
switch (wID)
{
case IDC_COLOR_REDIT:
case IDC_COLOR_GEDIT:
case IDC_COLOR_BEDIT:
/*
* For the edit controls we only take any notice when the
* control loses the input focus.
*/
if (wNotifyCode == EN_KILLFOCUS)
{
/*
* Get the edit control's text.
*/
GetDlgItemText(dialog, wID, buffer, sizeof(buffer));
/*
* Ensure its a valid value.
*/
if ((sscanf(buffer, "%d", &channel) == 1) &&
(channel >= 0) && (channel <= 255))
{
/*
* Build the new current color and update the appropriate
* slider.
*/
switch (wID)
{
case IDC_COLOR_REDIT:
scrollBar = GetDlgItem(dialog, IDC_COLOR_RSLIDER);
Color = PALETTERGB(channel, GetGValue(Color), GetBValue(Color));
break;
case IDC_COLOR_GEDIT:
scrollBar = GetDlgItem(dialog, IDC_COLOR_GSLIDER);
Color = PALETTERGB(GetGValue(Color), channel, GetBValue(Color));
break;
case IDC_COLOR_BEDIT:
scrollBar = GetDlgItem(dialog, IDC_COLOR_BSLIDER);
Color = PALETTERGB(GetRValue(Color), GetGValue(Color), channel);
break;
}
SetScrollPos(scrollBar, SB_CTL, channel, TRUE);
}
else
{
/*
* Not valid, put the existing value back.
*/
switch (wID)
{
case IDC_COLOR_REDIT: scrollBar = GetDlgItem(dialog, IDC_COLOR_RSLIDER); break;
case IDC_COLOR_GEDIT: scrollBar = GetDlgItem(dialog, IDC_COLOR_GSLIDER); break;
case IDC_COLOR_BEDIT: scrollBar = GetDlgItem(dialog, IDC_COLOR_BSLIDER); break;
}
channel = GetScrollPos(scrollBar, SB_CTL);
sprintf(buffer, "%d", channel);
SetDlgItemText(dialog, wID, buffer);
}
}
break;
case IDOK:
/*
* Convert the Windows COLORREF back into the
* RenderWare color.
*/
ColorRefToRGB(Color, RealColor);
EndDialog(dialog, IDOK);
break;
case IDCANCEL:
EndDialog(dialog, IDCANCEL);
break;
}
return TRUE;
}
}
return FALSE;
}
/**********************************************************************/