home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The World of Computer Software
/
World_Of_Computer_Software-02-385-Vol-1of3.iso
/
m
/
master12.zip
/
mastering
/
scalecolors.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-08-25
|
19KB
|
554 lines
/* Xm headers */
#include <Xm/DialogS.h>
#include <Xm/Form.h>
#include <Xm/Frame.h>
#include <Xm/LabelG.h>
#include <Xm/PushBG.h>
#include <Xm/CascadeB.h>
#include <Xm/MessageB.h>
#include <Xm/RowColumn.h>
#include <Xm/MainW.h>
#include <Xm/Scale.h>
/* global defines */
#define CMPSTR(str) XmStringCreateLtoR (str, XmFONTLIST_DEFAULT_TAG)
/* geometry */
#define HORIZONTAL_SPACING 8
#define VERTICAL_SPACING 8
#define TOP_OFFSET (Dimension) 30
#define SCALE_HIGHLIGHT_THICKNESS (Dimension) 2
#define SCALE_WIDTH 400
#define SCALE_HEIGHT 25
#define RED 1
#define GREEN 2
#define BLUE 3
#define BUFFERSZ 1000
Widget appshell;
Widget frame;
Widget form;
Widget redLabel;
Widget greenLabel;
Widget blueLabel;
Widget redScale;
Widget greenScale;
Widget blueScale;
XColor bg_color; /* color structure for the background color */
XColor fg_color; /* color structure for the foreground color */
XColor ts_color; /* color structure for the top shadow color */
XColor bs_color; /* color structure for the bottom shadow color */
XColor sc_color; /* color structure for the select color (arm) color */
XmColorProc calcRGB;
Display *display;
Colormap colormap;
/* Forward Declarations */
static Widget CreateHelp();
static void HelpCB();
static void QuitCB();
static void CreateColorEditor();
static void changRGB_CB();
static void SetScales();
static void GenerateColors();
Pixel GetPixel();
char *tmpbuf = "#78a0d5";
/****************************************/
/* main */
/****************************************/
void main(argc, argv)
int argc;
char **argv;
{
register int n;
Arg args[4];
Widget main_window, menu_bar, menu_pane, cascade;
Widget button;
Boolean status;
unsigned long plane_mask;
unsigned long *color_cells;
XtAppContext app_context;
/* Initialize the toolkit and open the display */
appshell = XtAppInitialize(&app_context, "Scalecolors", NULL, 0, &argc,
argv, NULL, args, 0);
/* Get the display */
display = XtDisplay(appshell);
/* Get the default colormap for this display */
colormap = XDefaultColormap(display, XDefaultScreen(display));
/* Allocate 4 color cells for use with this program: background,
foreground, top shadow, and bottom shadow. The select color
won't be used so we don't need to allocate for it */
color_cells = (unsigned long *)XtMalloc (4 * sizeof (unsigned long));
status = XAllocColorCells (display, colormap,
0, &plane_mask, 0, color_cells, 4);
if(status == False)
{
printf(" couldn't allocate enough color cells\n");
exit(1);
}
else
{
/* Assign the allocated color cells to the correct structure */
n = 0;
bg_color.pixel = color_cells[n++];
bg_color.flags = DoRed | DoGreen | DoBlue;
fg_color.pixel = color_cells[n++];
fg_color.flags = DoRed | DoGreen | DoBlue;
bs_color.pixel = color_cells[n++];
bs_color.flags = DoRed | DoGreen | DoBlue;
ts_color.pixel = color_cells[n];
ts_color.flags = DoRed | DoGreen | DoBlue;
}
/* Get the RGB value (XColor) of the background */
XParseColor(display, colormap, tmpbuf, &bg_color);
/* Create main window. */
main_window = XmCreateMainWindow (appshell, "main1", args, 0);
XtManageChild (main_window);
/* Create menu bar in main window. */
menu_bar = XmCreateMenuBar (main_window, "menu_bar", args, 0);
XtManageChild (menu_bar);
/* Create "File" pulldown menu. */
menu_pane = XmCreatePulldownMenu (menu_bar, "menu_pane", args, 0);
button = XmCreatePushButtonGadget (menu_pane, "Exit", args, 0);
XtManageChild (button);
XtAddCallback (button, XmNactivateCallback, QuitCB, NULL);
n = 0;
XtSetArg (args[n], XmNsubMenuId, menu_pane); n++;
cascade = XmCreateCascadeButton (menu_bar, "File", args, n);
XtManageChild (cascade);
/* Create "Help" button. */
cascade = XmCreateCascadeButton (menu_bar, "Help", args, 0);
XtManageChild (cascade);
XtAddCallback (cascade, XmNactivateCallback, HelpCB, NULL);
n = 0;
XtSetArg (args[n], XmNmenuHelpWidget, cascade); n++;
XtSetValues (menu_bar, args, n);
/*
* Create a frame widget
*/
n = 0;
XtSetArg(args[n], XmNmarginWidth, 15); n++;
XtSetArg(args[n], XmNmarginHeight, 15); n++;
frame = XmCreateFrame (main_window, "frame", args, n);
XtManageChild(frame);
/* Set main window areas */
XmMainWindowSetAreas (main_window, menu_bar, NULL, NULL, NULL, frame);
/* Go generate the foreground, top shadow, and bottom shadow colors
based on the background color which was set by XParseColor above */
GenerateColors();
/* Create the sliders with labels using the generated colors */
CreateColorEditor();
XtRealizeWidget (appshell);
XtAppMainLoop (app_context);
}
/*-------------------------------------------------------------
** HelpCB - callback for help button
*/
static
void HelpCB (w, client_data, call_data)
Widget w; /* widget id */
XtPointer client_data; /* data from application */
XtPointer call_data; /* data from widget class */
{
Widget message_box; /* message box */
/* Create help window. */
message_box = CreateHelp (w);
/* Display help window. */
XtManageChild (message_box);
}
/*-------------------------------------------------------------
** CreateHelp - create help window
*/
static
Widget CreateHelp (parent)
Widget parent; /* parent widget */
{
Widget button;
Widget message_box; /* Message Dialog */
Arg args[4]; /* arg list */
register int n; /* arg count */
static char message[BUFFERSZ]; /* help text */
XmString title_string = NULL;
XmString message_string = NULL;
XmString button_string = NULL;
/* Generate message to display. */
sprintf (message, "\
This program uses three scale widgets to balance colors. The three scales\n\
are used to adjust the primary colors: red, blue, and green. As the\n\
scales are adjusted, the background color changes accordingly. To\n\
terminate the program, press the 'File' button and then the 'Exit'\n\
button.\0");
/* Create the compound strings */
message_string = CMPSTR (message);
button_string = CMPSTR ("Close");
title_string = CMPSTR ("scalecolors help");
/* Create message box dialog. */
n = 0;
XtSetArg (args[n], XmNdialogTitle, title_string); n++;
XtSetArg (args[n], XmNokLabelString, button_string); n++;
XtSetArg (args[n], XmNmessageString, message_string); n++;
message_box = XmCreateMessageDialog (parent, "helpbox", args, n);
button = XmMessageBoxGetChild (message_box, XmDIALOG_CANCEL_BUTTON);
XtUnmanageChild (button);
button = XmMessageBoxGetChild (message_box, XmDIALOG_HELP_BUTTON);
XtUnmanageChild (button);
/* Free strings and return message box. */
if (title_string) XmStringFree (title_string);
if (message_string) XmStringFree (message_string);
if (button_string) XmStringFree (button_string);
return (message_box);
}
/*-------------------------------------------------------------
** QuitCB - callback for quit button
*/
static
void QuitCB (w, client_data, call_data)
Widget w; /* widget id */
XtPointer client_data; /* data from application */
XtPointer call_data; /* data from widget class */
{
/* Terminate the application. */
exit (0);
}
/*--------------------------------------------------------------
* CreateColorEditor - Create the form and scale widgets
*/
static void
CreateColorEditor()
{
register int n;
Arg args[15];
XmString string; /* temp Xm string */
WidgetList children;
/* Create form widget */
n=0;
XtSetArg(args[n], XmNhorizontalSpacing, HORIZONTAL_SPACING); n++;
XtSetArg(args[n], XmNverticalSpacing, VERTICAL_SPACING); n++;
XtSetArg(args[n], XmNbackground, bg_color.pixel); n++;
XtSetArg(args[n], XmNforeground, fg_color.pixel); n++;
XtSetArg(args[n], XmNtopShadowColor, ts_color.pixel); n++;
XtSetArg(args[n], XmNbottomShadowColor, bs_color.pixel); n++;
form = XmCreateForm(frame,"form", args, n);
XtManageChild (form);
/*
* Create RGB label gadgets for R, G, B, labels
*/
string = CMPSTR("R");
n = 0;
XtSetArg(args[n], XmNalignment, XmALIGNMENT_END); n++;
XtSetArg(args[n], XmNlabelString, string); n++;
XtSetArg(args[n], XmNmarginHeight, 0); n++;
XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
XtSetArg(args[n], XmNtopOffset, TOP_OFFSET); n++;
XtSetArg(args[n], XmNbottomAttachment, XmATTACH_NONE); n++;
XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
XtSetArg(args[n], XmNrightAttachment, XmATTACH_NONE); n++;
XtSetArg(args[n], XmNhorizontalSpacing, 30); n++;
redLabel = XmCreateLabelGadget(form, "redLabel", args, n);
XtManageChild(redLabel);
XmStringFree(string);
string = CMPSTR("G");
n = 0;
XtSetArg(args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
XtSetArg(args[n], XmNtopWidget, redLabel); n++;
XtSetArg(args[n], XmNtopOffset, TOP_OFFSET); n++;
XtSetArg(args[n], XmNbottomAttachment, XmATTACH_NONE); n++;
XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
XtSetArg(args[n], XmNrightAttachment, XmATTACH_NONE); n++;
XtSetArg(args[n], XmNhorizontalSpacing, 30); n++;
XtSetArg(args[n], XmNalignment, XmALIGNMENT_END); n++;
XtSetArg(args[n], XmNlabelString, string); n++;
XtSetArg(args[n], XmNmarginHeight, 0); n++;
greenLabel = XmCreateLabelGadget(form, "greenLabel", args, n);
XtManageChild(greenLabel);
XmStringFree(string);
string = CMPSTR("B");
n = 0;
XtSetArg(args[n], XmNhorizontalSpacing, 30); n++;
XtSetArg(args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
XtSetArg(args[n], XmNtopWidget, greenLabel); n++;
XtSetArg(args[n], XmNtopOffset, TOP_OFFSET); n++;
XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
XtSetArg(args[n], XmNrightAttachment, XmATTACH_NONE); n++;
XtSetArg(args[n], XmNalignment, XmALIGNMENT_END); n++;
XtSetArg(args[n], XmNlabelString, string); n++;
XtSetArg(args[n], XmNmarginHeight, 0); n++;
blueLabel = XmCreateLabelGadget(form, "blueLabel", args, n);
XtManageChild(blueLabel);
XmStringFree(string);
/*
* Create the three scale widgets, one each for red, green, and blue
*/
n = 0;
XtSetArg(args[n], XmNbackground, bg_color.pixel); n++;
XtSetArg(args[n], XmNforeground, fg_color.pixel); n++;
XtSetArg(args[n], XmNtopShadowColor, ts_color.pixel); n++;
XtSetArg(args[n], XmNbottomShadowColor, bs_color.pixel); n++;
XtSetArg(args[n], XmNshowValue, True); n++;
XtSetArg(args[n], XmNorientation, XmHORIZONTAL); n++;
XtSetArg(args[n], XmNprocessingDirection, XmMAX_ON_RIGHT); n++;
XtSetArg(args[n], XmNhighlightThickness, SCALE_HIGHLIGHT_THICKNESS); n++;
XtSetArg(args[n], XmNmaximum, 0xff); n++;
XtSetArg(args[n], XmNminimum, 0x0); n++;
XtSetArg(args[n], XmNincrement, 1); n++;
XtSetArg(args[n], XmNscaleWidth, SCALE_WIDTH); n++;
XtSetArg(args[n], XmNscaleHeight, SCALE_HEIGHT); n++;
redScale = XmCreateScale(form, "redScale", args, n);
XtManageChild(redScale);
XtAddCallback(redScale, XmNvalueChangedCallback, changRGB_CB,
(XtPointer) RED);
XtAddCallback(redScale, XmNdragCallback, changRGB_CB, (XtPointer) RED );
greenScale = XmCreateScale(form, "greenScale", args, n);
XtManageChild(greenScale);
XtAddCallback(greenScale, XmNvalueChangedCallback, changRGB_CB,
(XtPointer) GREEN );
XtAddCallback(greenScale, XmNdragCallback, changRGB_CB, (XtPointer) GREEN );
blueScale = XmCreateScale(form, "blueScale", args, n);
XtManageChild(blueScale);
XtAddCallback(blueScale, XmNvalueChangedCallback, changRGB_CB,
(XtPointer) BLUE );
XtAddCallback(blueScale, XmNdragCallback, changRGB_CB, (XtPointer) BLUE);
/* Add to Red Scale */
n=0;
XtSetArg(args[n], XmNhorizontalSpacing, 30); n++;
XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
XtSetArg(args[n], XmNbottomAttachment, XmATTACH_OPPOSITE_WIDGET); n++;
XtSetArg(args[n], XmNbottomWidget, redLabel); n++;
XtSetArg(args[n], XmNbottomOffset, 0); n++;
XtSetArg(args[n], XmNleftAttachment, XmATTACH_WIDGET); n++;
XtSetArg(args[n], XmNleftWidget, redLabel); n++;
XtSetArg(args[n], XmNleftOffset, HORIZONTAL_SPACING); n++;
XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
XtSetValues (redScale, args, n);
/* Add to Green Scale */
n=0;
XtSetArg(args[n], XmNhorizontalSpacing, 30); n++;
XtSetArg(args[n], XmNtopAttachment, XmATTACH_NONE); n++;
XtSetArg(args[n], XmNbottomAttachment, XmATTACH_OPPOSITE_WIDGET); n++;
XtSetArg(args[n], XmNbottomWidget, greenLabel); n++;
XtSetArg(args[n], XmNbottomOffset, 0); n++;
XtSetArg(args[n], XmNleftAttachment, XmATTACH_WIDGET); n++;
XtSetArg(args[n], XmNleftWidget, redLabel); n++;
XtSetArg(args[n], XmNleftOffset, HORIZONTAL_SPACING); n++;
XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
XtSetValues (greenScale, args, n);
/* Add to Blue Scale */
n=0;
XtSetArg(args[n], XmNhorizontalSpacing, 30); n++;
XtSetArg(args[n], XmNtopAttachment, XmATTACH_NONE); n++;
XtSetArg(args[n], XmNbottomAttachment, XmATTACH_OPPOSITE_WIDGET); n++;
XtSetArg(args[n], XmNbottomWidget, blueLabel); n++;
XtSetArg(args[n], XmNbottomOffset, 0); n++;
XtSetArg(args[n], XmNleftAttachment, XmATTACH_WIDGET); n++;
XtSetArg(args[n], XmNleftWidget, redLabel); n++;
XtSetArg(args[n], XmNleftOffset, HORIZONTAL_SPACING); n++;
XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
XtSetValues (blueScale, args, n);
/* Set the scales to the bg component of the selected color */
SetScales(&bg_color);
/* Set the trough colors of the RGB scales -
get the composite children of the scales -
child[1] is the scroll bar */
n=0;
XtSetArg(args[n], XmNchildren, &children); n++;
XtGetValues(redScale, args, n);
n=0;
XtSetArg(args[n], XmNtroughColor, GetPixel(form,"red")); n++;
XtSetValues (children[1], args, n);
n=0;
XtSetArg(args[n], XmNchildren, &children); n++;
XtGetValues(greenScale, args, n);
n=0;
XtSetArg(args[n], XmNtroughColor, GetPixel(form,"green")); n++;
XtSetValues (children[1], args, n);
n=0;
XtSetArg(args[n], XmNchildren, &children); n++;
XtGetValues(blueScale, args, n);
n=0;
XtSetArg(args[n], XmNtroughColor, GetPixel(form,"blue")); n++;
XtSetValues (children[1], args, n);
}
/************************************************************************
* GenerateColors()
* Generates new RGB values for foreground, top shadow,
* bottom shadow, and select color based on the background.
* Select color is not used in our program but the Motif color
* generation routine expects it and calculates it.
* The Colors generated are stored in the red, green, and blue
* fields of the XColor structure passed in.
* The generated colors are then used to update the color cells
* of the ColorSet being edited.
************************************************************************/
static void
GenerateColors()
{
XColor colors[4];
int j=0;
/* Get the color calculation procedure */
if (calcRGB == NULL)
calcRGB = XmGetColorCalculation();
/* Given the background color, calculate the foreground, select color,
top shadow, and bottom shadow colors */
(*calcRGB)(&bg_color, &fg_color, &sc_color, &ts_color, &bs_color);
/* Put those calculated colors in an array */
colors[j++] = bg_color;
colors[j++] = fg_color;
colors[j++] = ts_color;
colors[j++] = bs_color;
/* Go change the colors dynamically */
XStoreColors(display, colormap, colors, j);
}
/************************************************************************
* changeRGB_CB()
* Called when one of the RGB scales is moved
************************************************************************/
static void
changRGB_CB( w, client_data, call_data )
Widget w;
XtPointer client_data;
XtPointer call_data;
{
int reason_code;
int value;
int color;
reason_code = ((XmAnyCallbackStruct *)call_data)->reason;
if ( reason_code == XmCR_VALUE_CHANGED || reason_code == XmCR_DRAG )
{
color = (int) client_data;
value = ((XmScaleCallbackStruct *)call_data)->value;
/*
* Shift value -- to make up for scale max of only 0xff
*/
value <<= 8;
switch (color)
{
case RED:
bg_color.red = value;
break;
case GREEN:
bg_color.green = value;
break;
case BLUE:
bg_color.blue = value;
break;
default:
return;
}
SetScales(&bg_color);
GenerateColors();
}
}
/************************************************************************
* SetScales()
* passed an XColor, updates all scales.
************************************************************************/
static void
SetScales( rgb )
XColor *rgb;
{
XmScaleSetValue(redScale,rgb->red >> 8);
XmScaleSetValue(greenScale,rgb->green >> 8);
XmScaleSetValue(blueScale,rgb->blue >> 8);
}
/************************************************************************
* GetPixel()
* passed in a color string, calls XtConvert which is an X
* toolkit converter which converts a string into a pixel value.
************************************************************************/
Pixel
GetPixel( widget, color_string )
Widget widget;
char *color_string;
{
XrmValue from, to;
/* Determine the size of the string passed in */
from.size = strlen(color_string) + 1;
if (from.size < sizeof(String))
from.size = sizeof(String);
from.addr = color_string;
/* Go convert the string into a pixel (i.e. color cell) */
XtConvert(widget, XmRString, &from, XmRPixel, &to);
/* Return that pixel */
return ((Pixel) *((Pixel *) to.addr));
}