file: color_req.c
/*
* color_req.c - Functions needed to implement a color requester
*/
#include "XmdvTool.h"
#include "brush.h"
#include "color_req.h"
#include "brush_oper.h"
/* the brushes */
extern Brush brushes[MAXBRUSH]; /* the brushes */
/* the operations */
extern Operation operations[MAXOPER];
/* external colors */
extern XColor text_col, data_col, highlight_col, overlap_col;
extern XColor paint_col, grid1_col, grid2_col, grid3_col;
extern XColor stat1_col, stat2_col;
/* array of brush and brush overlap colors */
XColor brush_colors[NUM_BRUSH_CELLS];
/* color space #defines */
#define COLOR_RGB 1
#define COLOR_HSV 2
/* the maximum color value. All sliders have the range 0..MAX_COL_VALUE */
#define MAX_COL_VALUE 255
/* the gc */
extern GC gc;
/* external colors */
extern XColor outline_col;
/* the application shell */
extern Widget appShell;
/* backup copy of color for reset */
XColor backup_col;
/* a pointer to the color being manipulated */
XColor *manip_col = &brush_colors[BRUSH_COLOR(0)];
/* rgb or hsv color space */
int color_space = COLOR_RGB;
/* color requester initialization flag */
int creq_init = FALSE;
/* are we manipulating an operation color? */
int op_manip = FALSE;
/* the operation number being manipulated */
int op_num;
/*
* ColorReqInitCB() - Callback function when the color requester is
* created / popped up.
*
* PARAMETERS
* w Selected widget
* params User data passed to callback
* call_data Data about this callback
*
* RETURNS
* void
*/
void ColorReqInitCB(Widget w,
XtPointer params,
XtPointer call_data)
{
creq_init = TRUE;
/* update the sliders and value widgets */
ColorReqUpdateAll();
/* make a backup copy of the colors */
backup_col.red = manip_col->red;
backup_col.green = manip_col->green;
backup_col.blue = manip_col->blue;
}
/*
* ColorReqMenuInitCB() - Initialize the color requester menu
*
* PARAMETERS
* w Selected widget
* params User data passed to callback
* call_data Data about this callback
*
* RETURNS
* void
*/
void ColorReqMenuInitCB(Widget w,
XtPointer params,
XtPointer call_data)
{
BrushOperUpdateList();
}
/*
* ColorReqResetCB() - Callback function when the color requester reset
* button is pressed.
*
* PARAMETERS
* w Selected widget
* params User data passed to callback
* call_data Data about this callback
*
* RETURNS
* void
*/
void ColorReqResetCB(Widget w,
XtPointer params,
XtPointer call_data)
{
Colormap cmap; /* the colormap */
/* get the colormap */
cmap = DefaultColormap(XtDisplay(w), DefaultScreen(XtDisplay(w)));
/* check if manip_col is NULL */
if(!manip_col)
{
XBell(XtDisplay(w), 0);
return;
}
/* copy the brush color */
manip_col->red = backup_col.red;
manip_col->green = backup_col.green;
manip_col->blue = backup_col.blue;
XStoreColor(XtDisplay(w), cmap, manip_col);
/* reset the sliders and value widgets */
ColorReqUpdateAll();
}
/*
* ColorReqRedrawCanvasCB() - Callback function that performs the canvas
* redraw for the color requester.
*
* PARAMETERS
* w Selected widget
* params User data passed to callback
* call_data Data about this callback
*
* RETURNS
* void
*/
void ColorReqRedrawCanvasCB(Widget w,
XtPointer params,
XtPointer call_data)
{
Widget canvas_w; /* the drawing area */
Arg wargs[10]; /* for Xt get/set */
int n;
int width, height; /* size of drawing area widget */
/* get the canvas */
if(!(canvas_w = WcFullNameToWidget(appShell, "*color_req_canvas")))
{
fprintf(stderr, "Couldn't find *color_req_canvas\n");
return;
}
/* check if the color requster has been initialized */
if(!creq_init)
return;
/* set the appropriate color */
XSetForeground(XtDisplay(canvas_w), gc, manip_col->pixel);
/* query the size */
n = 0;
XtSetArg(wargs[n],XtNwidth,&width); n++;
XtSetArg(wargs[n],XtNheight,&height); n++;
XtGetValues(canvas_w,wargs,n);
/* draw the box to fill the entire area */
XFillRectangle(XtDisplay(canvas_w), XtWindow(canvas_w), gc,
0, 0, width, height);
}
/*
* ColorReqJumpCB() - Callback function when one of the color sliders
* is jumped to a new position.
*
* PARAMETERS
* w Selected widget
* params The name of the slider being manipulated
* call_data Position data about this callback
*
* RETURNS
* void
*/
void ColorReqJumpCB(Widget w, XtPointer params, XtPointer call_data)
{
float position; /* the new slider position 0..1 */
char *slider_name; /* the name of the slider */
int slider_num; /* the number of the slider */
Widget value_w; /* the value widget to update */
char buf[64]; /* temp buffer for value string */
Colormap cmap; /* the colormap */
int n;
Arg wargs[10]; /* for XGetValues() / XSetValues() */
/* extract passed information */
slider_name = (char *)params;
position = *(float *)call_data;
slider_num = *slider_name - '0';
/* get the colormap */
cmap = DefaultColormap(XtDisplay(w), DefaultScreen(XtDisplay(w)));
switch(slider_num)
{
case 1:
if(!(value_w = WcFullNameToWidget(w,"*color_req_val_1")))
{
fprintf(stderr, "Error finding *color_req_val_1\n");
return;
}
if(color_space == COLOR_RGB)
manip_col->red = (int)(position * MAX_XCOL_VALUE);
else
{
/* fix later */
}
break;
case 2:
if(!(value_w = WcFullNameToWidget(w,"*color_req_val_2")))
{
fprintf(stderr, "Error finding *color_req_val_2\n");
return;
}
if(color_space == COLOR_RGB)
manip_col->green = (int)(position * MAX_XCOL_VALUE);
else
{
/* fix later */
}
break;
case 3:
if(!(value_w = WcFullNameToWidget(w,"*color_req_val_3")))
{
fprintf(stderr, "Error finding *color_req_val_3\n");
return;
}
if(color_space == COLOR_RGB)
manip_col->blue = (int)(position * MAX_XCOL_VALUE);
else
{
/* fix later */
}
break;
default:
XBell(XtDisplay(w), 0);
fprintf(stderr, "Invalid slider number passed to ColorReqJumpCB()\n");
return;
}
/* update the value widget */
sprintf(buf, "%d", (int)(MAX_COL_VALUE * position));
n = 0;
XtSetArg(wargs[n], XtNlabel, buf); n++;
XtSetValues(value_w, wargs, n);
/* store the color */
XStoreColor(XtDisplay(w), cmap, manip_col);
/* if this is an operation color, update the interpolated colors */
if(op_manip)
BrushOperInterpolateColors(&operations[op_num]);
}
/*
* ColorReqScrollCB() - Callback function when one of the color sliders
* is scrolled left or right.
*
* PARAMETERS
* w Selected widget
* params The name of the slider being manipulated
* call_data # of pixels the scrollbar was moved
*
* RETURNS
* void
*/
void ColorReqScrollCB(Widget w, XtPointer params, XtPointer call_data)
{
Dimension length; /* The length of the scrollbar */
float position; /* the current slider position */
int direction; /* direction of change */
char *slider_name; /* the name of the slider */
int slider_num; /* the number of the slider */
Arg wargs[10]; /* for Xt get/set */
int n;
Widget value_w; /* the value widget to update */
char buf[80]; /* character buffer for updating labels */
Colormap cmap; /* the colormap */
/* extract passed information */
slider_name = (char *)params;
direction = (int)call_data;
slider_num = *slider_name - '0';
/* get length of scrollbar */
n = 0;
XtSetArg(wargs[n], XtNlength, &length); n++;
XtGetValues(w, wargs, n);
/* get the current position */
n = 0;
XtSetArg(wargs[n], XtNtopOfThumb, &position); n++;
XtGetValues(w, wargs, n);
/* get the colormap */
cmap = DefaultColormap(XtDisplay(w), DefaultScreen(XtDisplay(w)));
/* increment / decrement the position and bound */
if(direction < 0)
{
position += 1 / (float)MAX_COL_VALUE;
position = MIN(position, 1.0);
}
else
{
position -= 1 / (float)MAX_COL_VALUE;
position = MAX(position, 0.0);
}
/* determine slider */
switch(slider_num)
{
case 1:
if(!(value_w = WcFullNameToWidget(w,"*color_req_val_1")))
{
fprintf(stderr, "Error finding *color_req_val_1\n");
return;
}
if(color_space == COLOR_RGB)
manip_col->red = (int)(position * MAX_XCOL_VALUE);
else
{
/* fix later */
}
break;
case 2:
if(!(value_w = WcFullNameToWidget(w,"*color_req_val_2")))
{
fprintf(stderr, "Error finding *color_req_val_2\n");
return;
}
if(color_space == COLOR_RGB)
manip_col->green = (int)(position * MAX_XCOL_VALUE);
else
{
/* fix later */
}
break;
case 3:
if(!(value_w = WcFullNameToWidget(w,"*color_req_val_3")))
{
fprintf(stderr, "Error finding *color_req_val_3\n");
return;
}
if(color_space == COLOR_RGB)
manip_col->blue = (int)(position * MAX_XCOL_VALUE);
else
{
/* fix later */
}
break;
default:
XBell(XtDisplay(w), 0);
fprintf(stderr, "Invalid slider number passed to ColorReqScrollCB()\n");
return;
}
/* update the value widget */
sprintf(buf, "%d", (int)(MAX_COL_VALUE * position));
n = 0;
XtSetArg(wargs[n], XtNlabel, buf); n++;
XtSetValues(value_w, wargs, n);
/* update the slider */
XawScrollbarSetThumb(w, position, -1.0);
XStoreColor(XtDisplay(w), cmap, manip_col);
/* if this is an operation color, update the interpolated colors */
if(op_manip)
BrushOperInterpolateColors(&operations[op_num]);
}
/*
* ColorReqColorSpaceCB() - Callback function for changing the color space
* between RGB and HSV. Acts like a toggle,
* changing the global variable color_space.
* Updates the RGB/HSV button, and the slider
* labels to show the change.
*
* PARAMETERS
* w Selected widget
* params User data passed to callback
* call_data Data about this callback
*
* RETURNS
* void
*/
void ColorReqColorSpaceCB(Widget w,
XtPointer params,
XtPointer call_data)
{
int n;
Arg wargs[10]; /* for XGetValues() / XSetValues() */
Widget label1, label2, label3;
/* get the label widgets */
if(!(label1 = WcFullNameToWidget(appShell, "*color_req_label_1")))
{
fprintf(stderr, "Error finding *color_req_label_1\n");
return;
}
if(!(label2 = WcFullNameToWidget(appShell, "*color_req_label_2")))
{
fprintf(stderr, "Error finding *color_req_label_2\n");
return;
}
if(!(label3 = WcFullNameToWidget(appShell, "*color_req_label_3")))
{
fprintf(stderr, "Error finding *color_req_label_3\n");
return;
}
if(color_space == COLOR_RGB)
{
color_space = COLOR_HSV;
/* update the RGB/HSV button */
n = 0;
XtSetArg(wargs[n], XtNlabel, "HSV"); n++;
XtSetValues(w, wargs, n);
/* update the labels */
n = 0;
XtSetArg(wargs[n], XtNlabel, "H"); n++;
XtSetValues(label1, wargs, n);
n = 0;
XtSetArg(wargs[n], XtNlabel, "S"); n++;
XtSetValues(label2, wargs, n);
n = 0;
XtSetArg(wargs[n], XtNlabel, "V"); n++;
XtSetValues(label3, wargs, n);
}
else
{
color_space = COLOR_RGB;
/* update the RGB/HSV button */
n = 0;
XtSetArg(wargs[n], XtNlabel, "RGB"); n++;
XtSetValues(w, wargs, n);
/* update the labels */
n = 0;
XtSetArg(wargs[n], XtNlabel, "R"); n++;
XtSetValues(label1, wargs, n);
n = 0;
XtSetArg(wargs[n], XtNlabel, "G"); n++;
XtSetValues(label2, wargs, n);
n = 0;
XtSetArg(wargs[n], XtNlabel, "B"); n++;
XtSetValues(label3, wargs, n);
/* change the positions on the sliders */
}
}
/*
* ColorReqInvalidateColorCB() - Callback function that invalidates the
* currently selected color. For now
* we just reset to brush background 1.
*
* PARAMETERS
* w Selected widget
* params The color selected
* call_data Data about this callback
*
* RETURNS
* void
*/
void ColorReqInvalidateColorCB(Widget w,
XtPointer params,
XtPointer call_data)
{
/*
* "fake out" callbacks to ColorReqChooseColorCB() and
* ColorReqRedrawCanvas(). This is kinda cheesey, but it was
* simple.
*/
ColorReqChooseColorCB(w, "Brush Background 1", NULL);
ColorReqRedrawCanvasCB(w, NULL, NULL);
}
/*
* ColorReqChooseColorCB() - Callback function for choosing a color
* to manipulate from the menu. manip_col
* is updated to point to the correct color.
*
* PARAMETERS
* w Selected widget
* params The color selected
* call_data Data about this callback
*
* RETURNS
* void
*/
void ColorReqChooseColorCB(Widget w,
XtPointer params,
XtPointer call_data)
{
int n;
Arg wargs[10]; /* for XGetValues() / XSetValues() */
Widget cname_w; /* the color name widget */
char *color_name = params; /* convert to a char ptr */
/* not manipulating an operation */
op_manip = FALSE;
if(!strcmp(color_name, "Brush Background 1"))
manip_col = &brush_colors[BRUSH_COLOR(0)];
else if(!strcmp(color_name, "Brush Background 2"))
manip_col = &brush_colors[BRUSH_COLOR(1)];
else if(!strcmp(color_name, "Brush Background 3"))
manip_col = &brush_colors[BRUSH_COLOR(2)];
else if(!strcmp(color_name, "Brush Background 4"))
manip_col = &brush_colors[BRUSH_COLOR(3)];
else if(!strcmp(color_name, "Data"))
manip_col = &data_col;
else if(!strcmp(color_name, "Highlight"))
manip_col = &highlight_col;
else if(!strcmp(color_name, "Text"))
manip_col = &text_col;
else if(!strcmp(color_name, "Overlap"))
manip_col = &overlap_col;
else if(!strcmp(color_name, "Paint"))
manip_col = &paint_col;
else if(!strcmp(color_name, "Grid 1"))
manip_col = &grid1_col;
else if(!strcmp(color_name, "Grid 2"))
manip_col = &grid2_col;
else if(!strcmp(color_name, "Grid 3"))
manip_col = &grid3_col;
else if(!strcmp(color_name, "Stats 1"))
manip_col = &stat1_col;
else if(!strcmp(color_name, "Stats 2"))
manip_col = &stat2_col;
else if(!strncmp(color_name, "Operation", 9))
{
/* extract the operation number */
if(!(sscanf(color_name, "%*s%d", &op_num)))
{
fprintf(stderr,
"Invalid color name passed to ColorReqChooseColorCB()\n");
fprintf(stderr, "\"%s\"\n", color_name);
return;
}
op_num--;
manip_col = &operations[op_num].colors[0];
op_manip = TRUE;
}
else
{
fprintf(stderr,
"Invalid color name passed to ColorReqChooseColorCB()\n");
fprintf(stderr, "\"%s\"\n", color_name);
return;
}
/* update the sliders and value widgets */
ColorReqUpdateAll();
/* update the color name widget */
if(!(cname_w = WcFullNameToWidget(appShell, "*color_req_cname_label")))
{
fprintf(stderr, "Error getting *color_req_cname_label\n");
return;
}
n = 0;
XtSetArg(wargs[n], XtNlabel, color_name); n++;
XtSetValues(cname_w, wargs, n);
/* store a backup copy of the color values */
backup_col.red = manip_col->red;
backup_col.green = manip_col->green;
backup_col.blue = manip_col->blue;
}
/*
* ColorReqUpdateAll() - Updates all the sliders and value widgets
* to reflect the current values in manip_col
*
* PARAMETERS
* void
*
* RETURNS
* void
*/
void ColorReqUpdateAll(void)
{
Widget canvas_w; /* the canvas widget */
Widget widget; /* generic widget for manipulation */
char buf[64]; /* temp buffer for putting strings into */
int n;
Arg wargs[10]; /* for XGetValues() / XSetValues() */
/* get the canvas widget */
if(!(canvas_w = WcFullNameToWidget(appShell, "*color_req_canvas")))
{
fprintf(stderr, "Error getting *color_req_canvas\n");
return;
}
if(color_space == COLOR_RGB)
{
/* the red slider */
if(!(widget = WcFullNameToWidget(appShell, "*color_req_slider_1")))
{
fprintf(stderr, "Error getting *color_req_slider_1\n");
return;
}
XawScrollbarSetThumb(widget, manip_col->red/(float)MAX_XCOL_VALUE,
-1.0);
/* the red value widget */
if(!(widget = WcFullNameToWidget(appShell, "*color_req_val_1")))
{
fprintf(stderr, "Error getting *color_req_val_1\n");
return;
}
sprintf(buf, "%3d", (int)(manip_col->red/(float)MAX_XCOL_VALUE*
MAX_COL_VALUE));
n = 0;
XtSetArg(wargs[n], XtNlabel, buf); n++;
XtSetValues(widget, wargs, n);
/* the green slider */
if(!(widget = WcFullNameToWidget(appShell, "*color_req_slider_2")))
{
fprintf(stderr, "Error getting *color_req_slider_2\n");
return;
}
XawScrollbarSetThumb(widget, manip_col->green/(float)MAX_XCOL_VALUE,
-1.0);
/* the green value widget */
if(!(widget = WcFullNameToWidget(appShell, "*color_req_val_2")))
{
fprintf(stderr, "Error getting *color_req_val_2\n");
return;
}
sprintf(buf, "%3d", (int)(manip_col->green/(float)MAX_XCOL_VALUE*
MAX_COL_VALUE));
n = 0;
XtSetArg(wargs[n], XtNlabel, buf); n++;
XtSetValues(widget, wargs, n);
/* the blue slider */
if(!(widget = WcFullNameToWidget(appShell, "*color_req_slider_3")))
{
fprintf(stderr, "Error getting *color_req_slider_3\n");
return;
}
XawScrollbarSetThumb(widget, manip_col->blue/(float)MAX_XCOL_VALUE,
-1.0);
/* the blue value widget */
if(!(widget = WcFullNameToWidget(appShell, "*color_req_val_3")))
{
fprintf(stderr, "Error getting *color_req_val_3\n");
return;
}
sprintf(buf, "%3d", (int)(manip_col->blue/(float)MAX_XCOL_VALUE*
MAX_COL_VALUE));
n = 0;
XtSetArg(wargs[n], XtNlabel, buf); n++;
XtSetValues(widget, wargs, n);
}
else
{
}
}
C++ to HTML Conversion by ctoohtml