/*
* tkScale.c --
*
* This module implements a scale widgets for the Tk toolkit.
* A scale displays a slider that can be adjusted to change a
* value; it also displays numeric labels and a textual label,
* if desired.
*
* The modifications to use floating-point values are based on
* an implementation by Paul Mackerras. The -variable option
* is due to Henning Schulzrinne. All of these are used with
* permission.
*
* Copyright (c) 1990-1994 The Regents of the University of California.
* Copyright (c) 1994-1995 Sun Microsystems, Inc.
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
* SCCS: @(#) tkScale.c 1.80 96/03/21 13:11:55
*/
#include "tkInt.h"
#include "tkDefault.h"
#include <math.h>
/*
* A data structure of the following type is kept for each scale
* widget managed by this file:
*/
typedef struct {
* means that the window has been destroyed
* but the data structures haven't yet been
* cleaned up.*/
* other things, so that resources can be
* freed even after tkwin has gone away. */
* "horizontal"). */
* zero means horizontal. */
* in pixels. */
* in pixels. */
* If non-NULL, scale's value tracks
* the contents of this variable and
* vice versa. */
* scale. */
* of scale. */
* don't display any tick marks. */
* even multiple of this value. */
* in values. 0 means we get to choose the
* range of the scale. */
* digits and other information. */
* scale value. (0 means we pick a value). */
* commands because the scale value changed.
* NULL means don't invoke commands.
* Malloc'ed. */
* on scrolling actions (in ms). */
* scale; NULL means don't display a
* label. Malloc'ed. */
* changed when scale is disabled. */
/*
* Information used when displaying widget:
*/
* background areas. */
* raised, sunken, or flat. */
* around widget when it has the focus.
* <= 0 means don't draw a highlight. */
/* Color for drawing traversal highlight
* area when highlight is off. */
* traversal highlight and 3-D border.
* Indicates how much interior stuff must
* be offset from outside edges to leave
* room for borders. */
* long dimension of scale. */
* below or to the left of the slider; zero
* means don't display the value. */
/*
* Layout information for horizontal scales, assuming that window
* gets the size it requested:
*/
/*
* Layout information for vertical scales, assuming that window
* gets the size it requested:
*/
/*
* Miscellaneous information:
*/
* the C code, but used by keyboard traversal
* scripts. Malloc'ed, but may be NULL. */
* definitions. */
} Scale;
/*
* Flag bits for scales:
*
* REDRAW_SLIDER - 1 means slider (and numerical readout) need
* to be redrawn.
* REDRAW_OTHER - 1 means other stuff besides slider and value
* need to be redrawn.
* REDRAW_ALL - 1 means the entire widget needs to be redrawn.
* ACTIVE - 1 means the widget is active (the mouse is
* in its window).
* INVOKE_COMMAND - 1 means the scale's command needs to be
* invoked during the next redisplay (the
* value of the scale has changed since the
* last time the command was invoked).
* SETTING_VAR - 1 means that the associated variable is
* being set by us, so there's no need for
* ScaleVarProc to do anything.
* NEVER_SET - 1 means that the scale's value has never
* been set before (so must invoke -command and
* set associated variable even if the value
* doesn't appear to have changed).
* GOT_FOCUS - 1 means that the focus is currently in
* this widget.
*/
/*
* Symbolic values for the active parts of a slider. These are
* the values that may be returned by the ScaleElement procedure.
*/
#define OTHER 0
/*
* Space to leave between scale area and text, and between text and
* edge of window.
*/
/*
* How many characters of space to provide when formatting the
* scale's value:
*/
/*
* Information used for argv parsing.
*/
(char *) NULL, 0, 0},
(char *) NULL, 0, 0},
(char *) NULL, 0, 0},
0},
"HighlightBackground", DEF_SCALE_HIGHLIGHT_BG,
"HighlightThickness",
(char *) NULL, 0, 0}
};
/*
* Forward declarations for procedures defined later in this file:
*/
int flags));
int what));
int y));
double value));
static void ScaleCmdDeletedProc _ANSI_ARGS_((
int y));
int flags));
/*
*--------------------------------------------------------------
*
* Tk_ScaleCmd --
*
* This procedure is invoked to process the "scale" Tcl
* command. See the user documentation for details on what
* it does.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* See the user documentation.
*
*--------------------------------------------------------------
*/
int
* interpreter. */
int argc; /* Number of arguments. */
char **argv; /* Argument strings. */
{
if (argc < 2) {
return TCL_ERROR;
}
return TCL_ERROR;
}
/*
* Initialize fields that won't be initialized by ConfigureScale,
* or which ConfigureScale expects to have reasonable values
* (e.g. resource pointers).
*/
scalePtr->tickInterval = 0;
scalePtr->repeatDelay = 0;
scalePtr->repeatInterval = 0;
scalePtr->labelLength = 0;
scalePtr->borderWidth = 0;
scalePtr->highlightWidth = 0;
scalePtr->sliderLength = 0;
scalePtr->horizLabelY = 0;
scalePtr->horizValueY = 0;
scalePtr->horizTroughY = 0;
scalePtr->horizTickY = 0;
scalePtr->vertTickRightX = 0;
scalePtr->vertValueRightX = 0;
scalePtr->vertTroughX = 0;
scalePtr->vertLabelX = 0;
goto error;
}
return TCL_OK;
return TCL_ERROR;
}
/*
*--------------------------------------------------------------
*
* ScaleWidgetCmd --
*
* This procedure is invoked to process the Tcl command
* that corresponds to a widget managed by this module.
* See the user documentation for details on what it does.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* See the user documentation.
*
*--------------------------------------------------------------
*/
static int
* widget. */
int argc; /* Number of arguments. */
char **argv; /* Argument strings. */
{
int c;
if (argc < 2) {
return TCL_ERROR;
}
c = argv[1][0];
&& (length >= 2)) {
if (argc != 3) {
argv[0], " cget option\"",
(char *) NULL);
goto error;
}
&& (length >= 3)) {
if (argc == 2) {
} else if (argc == 3) {
} else {
}
&& (length >= 3)) {
int x, y ;
double value;
goto error;
}
if (argc == 3) {
goto error;
}
} else {
}
+ scalePtr->borderWidth;
} else {
+ scalePtr->borderWidth;
}
double value;
int x, y;
goto error;
}
if (argc == 2) {
} else {
goto error;
}
}
int x, y, thing;
if (argc != 4) {
goto error;
}
goto error;
}
switch (thing) {
}
double value;
if (argc != 3) {
goto error;
}
goto error;
}
}
} else {
"\": must be cget, configure, coords, get, identify, or set",
(char *) NULL);
goto error;
}
return result;
return TCL_ERROR;
}
/*
*----------------------------------------------------------------------
*
* DestroyScale --
*
* This procedure is invoked by Tcl_EventuallyFree or Tcl_Release
* to clean up the internal structure of a button at a safe time
* (when no-one is using it anymore).
*
* Results:
* None.
*
* Side effects:
* Everything associated with the scale is freed up.
*
*----------------------------------------------------------------------
*/
static void
char *memPtr; /* Info about scale widget. */
{
/*
* Free up all the stuff that requires special handling, then
* let Tk_FreeOptions handle all the standard option-related
* stuff.
*/
}
}
}
}
}
/*
*----------------------------------------------------------------------
*
* ConfigureScale --
*
* the Tk option database, in order to configure (or
* reconfigure) a scale widget.
*
* Results:
* The return value is a standard Tcl result. If TCL_ERROR is
* returned, then interp->result contains an error message.
*
* Side effects:
* Configuration information, such as colors, border width,
* etc. get set for scalePtr; old resources get freed,
* if there were any.
*
*----------------------------------------------------------------------
*/
static int
* not already have values for some fields. */
int argc; /* Number of valid entries in argv. */
char **argv; /* Arguments. */
int flags; /* Flags to pass to Tk_ConfigureWidget. */
{
/*
* Eliminate any existing trace on a variable monitored by the scale.
*/
}
return TCL_ERROR;
}
/*
* If the scale is tied to the value of a variable, then set up
* a trace on the variable's value and set the scale's value from
* the value of the variable, if it exists.
*/
double value;
if (stringValue != NULL) {
}
}
}
/*
* Several options need special processing, such as parsing the
* orientation and creating GCs.
*/
} else {
"\": must be vertical or horizontal", (char *) NULL);
return TCL_ERROR;
}
/*
* Make sure that the tick interval has the right sign so that
* addition moves from fromValue to toValue.
*/
if ((scalePtr->tickInterval < 0)
}
/*
* Set the scale value to itself; all this does is to make sure
* that the scale's value is within the new acceptable range for
* the scale and reflect the value in the associated variable,
* if any.
*/
} else {
scalePtr->labelLength = 0;
}
"\": must be normal, active, or disabled", (char *) NULL);
return TCL_ERROR;
}
}
&gcValues);
}
if (scalePtr->highlightWidth < 0) {
scalePtr->highlightWidth = 0;
}
}
/*
* Recompute display-related information, and let the geometry
* manager know how much space is needed now.
*/
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* ComputeFormat --
*
* This procedure is invoked to recompute the "format" field
* of a scale's widget record, which determines how the value
* of the scale is converted to a string.
*
* Results:
* None.
*
* Side effects:
* The format field of scalePtr is modified.
*
*----------------------------------------------------------------------
*/
static void
{
double maxValue, x;
/*
* Compute the displacement from the decimal of the most significant
* digit required for any number in the scale's range.
*/
if (x > maxValue) {
maxValue = x;
}
if (maxValue == 0) {
maxValue = 1;
}
/*
* If the number of significant digits wasn't specified explicitly,
* compute it. It's the difference between the most significant
* digit needed to represent any number on the scale and the
* most significant digit of the smallest difference between
* numbers on the scale. In other words, display enough digits so
* that at least one digit will be different between any two adjacent
* positions of the scale.
*/
if (numDigits <= 0) {
if (scalePtr->resolution > 0) {
/*
* A resolution was specified for the scale, so just use it.
*/
} else {
/*
* No resolution was specified, so compute the difference
* in value between adjacent pixels and use it for the least
* significant digit.
*/
}
if (x > 0){
} else {
leastSigDigit = 0;
}
}
if (numDigits < 1) {
numDigits = 1;
}
}
/*
* Compute the number of characters required using "e" format and
* "f" format, and then choose whichever one takes fewer characters.
*/
if (numDigits > 1) {
eDigits++; /* Decimal point. */
}
if (afterDecimal < 0) {
afterDecimal = 0;
}
if (afterDecimal > 0) {
fDigits++; /* Decimal point. */
}
if (mostSigDigit < 0) {
fDigits++; /* Zero to left of decimal point. */
}
} else {
}
}
/*
*----------------------------------------------------------------------
*
* ComputeScaleGeometry --
*
* This procedure is called to compute various geometrical
* information for a scale, such as where various things get
* displayed. It's called when the window is reconfigured.
*
* Results:
* None.
*
* Side effects:
* Display-related numbers get changed in *scalePtr. The
* geometry manager gets told about the window's preferred size.
*
*----------------------------------------------------------------------
*/
static void
{
/*
* Horizontal scales are simpler than vertical ones because
* all sizes are the same (the height of a line of text);
* handle them first and then quit.
*/
extraSpace = 0;
if (scalePtr->labelLength != 0) {
y += lineHeight + SPACING;
}
y += lineHeight + SPACING;
} else {
scalePtr->horizValueY = y;
}
y += extraSpace;
scalePtr->horizTroughY = y;
if (scalePtr->tickInterval != 0) {
}
return;
}
/*
* Vertical scale: compute the amount of space needed to display
* the scales value by formatting strings for the two end points;
* use whichever length is longer.
*/
}
/*
* Assign x-locations to the elements of the scale, working from
* left to right.
*/
} else if (scalePtr->tickInterval != 0) {
scalePtr->vertTickRightX = x;
} else {
scalePtr->vertTickRightX = x;
scalePtr->vertValueRightX = x;
}
scalePtr->vertTroughX = x;
if (scalePtr->labelLength == 0) {
scalePtr->vertLabelX = 0;
} else {
}
}
/*
*--------------------------------------------------------------
*
* DisplayVerticalScale --
*
* This procedure redraws the contents of a vertical scale
* window. It is invoked as a do-when-idle handler, so it only
* runs when there's nothing else for the application to do.
*
* Results:
* There is no return value. If only a part of the scale needs
* to be redrawn, then drawnAreaPtr is modified to reflect the
* area that was actually modified.
*
* Side effects:
* Information appears on the screen.
*
*--------------------------------------------------------------
*/
static void
* or pixmap). */
* if only a part of the scale is
* redrawn, gets modified to reflect
* the part of the window that was
* redrawn. */
{
double tickValue;
/*
* Display the information from left to right across the window.
*/
}
/*
* Display the tick marks.
*/
if (scalePtr->tickInterval != 0) {
/*
* The RoundToResolution call gets rid of accumulated
* round-off errors, if any.
*/
break;
}
} else {
break;
}
}
}
}
}
/*
* Display the value, if it is desired.
*/
}
/*
* Display the trough and the slider.
*/
} else {
}
if (shadowWidth == 0) {
shadowWidth = 1;
}
x += shadowWidth;
y += shadowWidth;
height -= shadowWidth;
/*
* Draw the label to the right of the scale.
*/
}
}
/*
*----------------------------------------------------------------------
*
* DisplayVerticalValue --
*
* This procedure is called to display values (scale readings)
* for vertically-oriented scales.
*
* Results:
* None.
*
* Side effects:
* The numerical value corresponding to value is displayed with
* its right edge at "rightEdge", and at a vertical position in
* the scale that corresponds to "value".
*
*----------------------------------------------------------------------
*/
static void
* display value. */
* the value. */
double value; /* Y-coordinate of number to display,
* specified in application coords, not
* in pixels (we'll compute pixels). */
int rightEdge; /* X-coordinate of right edge of text,
* specified in pixels. */
{
/*
* Adjust the y-coordinate if necessary to keep the text entirely
* inside the window.
*/
}
}
}
/*
*--------------------------------------------------------------
*
* DisplayHorizontalScale --
*
* This procedure redraws the contents of a horizontal scale
* window. It is invoked as a do-when-idle handler, so it only
* runs when there's nothing else for the application to do.
*
* Results:
* There is no return value. If only a part of the scale needs
* to be redrawn, then drawnAreaPtr is modified to reflect the
* area that was actually modified.
*
* Side effects:
* Information appears on the screen.
*
*--------------------------------------------------------------
*/
static void
* or pixmap). */
* if only a part of the scale is
* redrawn, gets modified to reflect
* the part of the window that was
* redrawn. */
{
double tickValue;
/*
* Display the information from bottom to top across the window.
*/
}
/*
* Display the tick marks.
*/
if (scalePtr->tickInterval != 0) {
/*
* The RoundToResolution call gets rid of accumulated
* round-off errors, if any.
*/
break;
}
} else {
break;
}
}
}
}
}
/*
* Display the value, if it is desired.
*/
}
/*
* Display the trough and the slider.
*/
y = scalePtr->horizTroughY;
y + scalePtr->borderWidth,
} else {
}
y += scalePtr->borderWidth;
if (shadowWidth == 0) {
shadowWidth = 1;
}
x += shadowWidth;
y += shadowWidth;
width -= shadowWidth;
/*
* Draw the label at the top of the scale.
*/
}
}
/*
*----------------------------------------------------------------------
*
* DisplayHorizontalValue --
*
* This procedure is called to display values (scale readings)
* for horizontally-oriented scales.
*
* Results:
* None.
*
* Side effects:
* The numerical value corresponding to value is displayed with
* its bottom edge at "bottom", and at a horizontal position in
* the scale that corresponds to "value".
*
*----------------------------------------------------------------------
*/
static void
* display value. */
* the value. */
double value; /* X-coordinate of number to display,
* specified in application coords, not
* in pixels (we'll compute pixels). */
int top; /* Y-coordinate of top edge of text,
* specified in pixels. */
{
/*
* Adjust the x-coordinate if necessary to keep the text entirely
* inside the window.
*/
}
}
}
/*
*----------------------------------------------------------------------
*
* DisplayScale --
*
* This procedure is invoked as an idle handler to redisplay
* the contents of a scale widget.
*
* Results:
* None.
*
* Side effects:
* The scale gets redisplayed.
*
*----------------------------------------------------------------------
*/
static void
{
int result;
goto done;
}
/*
* Invoke the scale's command if needed.
*/
(char *) NULL);
}
}
return;
}
/*
* In order to avoid screen flashes, this procedure redraws
* the scale in a pixmap, then copies the pixmap to the
* screen in a single operation. This means that there's no
* point in time where the on-sreen image has been cleared.
*/
drawnArea.x = 0;
drawnArea.y = 0;
/*
* Much of the redisplay is done totally differently for
* horizontal and vertical scales. Handle the part that's
* different.
*/
} else {
}
/*
* Now handle the part of redisplay that is the same for
* horizontal and vertical scales: border and traversal
* highlight.
*/
}
if (scalePtr->highlightWidth != 0) {
} else {
}
}
}
/*
* Copy the information from the off-screen pixmap onto the screen,
* then delete the pixmap.
*/
done:
}
/*
*----------------------------------------------------------------------
*
* ScaleElement --
*
* Determine which part of a scale widget lies under a given
* point.
*
* Results:
* The return value is either TROUGH1, SLIDER, TROUGH2, or
* OTHER, depending on which of the scale's active elements
* (if any) is under the point at (x,y).
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int
int x, y; /* Coordinates within scalePtr's window. */
{
int sliderFirst;
if ((x < scalePtr->vertTroughX)
return OTHER;
}
return OTHER;
}
if (y < sliderFirst) {
return TROUGH1;
}
return SLIDER;
}
return TROUGH2;
}
if ((y < scalePtr->horizTroughY)
return OTHER;
}
return OTHER;
}
if (x < sliderFirst) {
return TROUGH1;
}
return SLIDER;
}
return TROUGH2;
}
/*
*----------------------------------------------------------------------
*
* PixelToValue --
*
* Given a pixel within a scale window, return the scale
* reading corresponding to that pixel.
*
* Results:
* A double-precision scale reading. If the value is outside
* the legal range for the scale then it's rounded to the nearest
* end of the scale.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static double
int x, y; /* Coordinates of point within
* window. */
{
value = y;
} else {
value = x;
}
if (pixelRange <= 0) {
/*
* Not enough room for the slider to actually slide: just return
* the scale's current value.
*/
}
+ scalePtr->borderWidth;
value /= pixelRange;
if (value < 0) {
value = 0;
}
if (value > 1) {
value = 1;
}
}
/*
*----------------------------------------------------------------------
*
* ValueToPixel --
*
* Given a reading of the scale, return the x-coordinate or
* y-coordinate corresponding to that reading, depending on
* whether the scale is vertical or horizontal, respectively.
*
* Results:
* An integer value giving the pixel location corresponding
* to reading. The value is restricted to lie within the
* defined range for the scale.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int
double value; /* Reading of the widget. */
{
int y, pixelRange;
double valueRange;
if (valueRange == 0) {
y = 0;
} else {
/ valueRange + 0.5);
if (y < 0) {
y = 0;
} else if (y > pixelRange) {
y = pixelRange;
}
}
return y;
}
/*
*--------------------------------------------------------------
*
* ScaleEventProc --
*
* This procedure is invoked by the Tk dispatcher for various
* events on scales.
*
* Results:
* None.
*
* Side effects:
* When the window gets deleted, internal structures get
* cleaned up. When it gets exposed, it is redisplayed.
*
*--------------------------------------------------------------
*/
static void
{
}
}
if (scalePtr->highlightWidth > 0) {
}
}
if (scalePtr->highlightWidth > 0) {
}
}
}
}
/*
*----------------------------------------------------------------------
*
* ScaleCmdDeletedProc --
*
* This procedure is invoked when a widget command is deleted. If
* the widget isn't already in the process of being destroyed,
* this command destroys it.
*
* Results:
* None.
*
* Side effects:
* The widget is destroyed.
*
*----------------------------------------------------------------------
*/
static void
{
/*
* This procedure could be invoked either because the window was
* destroyed and the command was then deleted (in which case tkwin
* is NULL) or because the command was deleted, and then this procedure
* destroys the widget.
*/
}
}
/*
*--------------------------------------------------------------
*
* SetScaleValue --
*
* This procedure changes the value of a scale and invokes
* a Tcl command to reflect the current position of a scale
*
* Results:
* None.
*
* Side effects:
* A Tcl command is invoked, and an additional error-processing
* command may also be invoked. The scale's slider is redrawn.
*
*--------------------------------------------------------------
*/
static void
double value; /* New value for scale. Gets adjusted
* if it's off the scale. */
int setVar; /* Non-zero means reflect new value through
* to associated variable, if any. */
int invokeCommand; /* Non-zero means invoked -command option
* to notify of new value, 0 means don't. */
{
}
}
return;
}
if (invokeCommand) {
}
}
}
/*
*--------------------------------------------------------------
*
* EventuallyRedrawScale --
*
* Arrange for part or all of a scale widget to redrawn at
* the next convenient time in the future.
*
* Results:
* None.
*
* Side effects:
* If "what" is REDRAW_SLIDER then just the slider and the
* value readout will be redrawn; if "what" is REDRAW_ALL
* then the entire widget will be redrawn.
*
*--------------------------------------------------------------
*/
static void
int what; /* What to redraw: REDRAW_SLIDER
* or REDRAW_ALL. */
{
return;
}
}
}
/*
*--------------------------------------------------------------
*
* RoundToResolution --
*
* Round a given floating-point value to the nearest multiple
* of the scale's resolution.
*
* Results:
* The return value is the rounded result.
*
* Side effects:
* None.
*
*--------------------------------------------------------------
*/
static double
double value; /* Value to round. */
{
if (scalePtr->resolution <= 0) {
return value;
}
if (rem < 0) {
}
} else {
}
}
return new;
}
/*
*----------------------------------------------------------------------
*
* ScaleVarProc --
*
* This procedure is invoked by Tcl whenever someone modifies a
* variable associated with a scale widget.
*
* Results:
* NULL is always returned.
*
* Side effects:
* The value displayed in the scale will change to match the
* variable's new value. If the variable has a bogus value then
* it is reset to the value of the scale.
*
*----------------------------------------------------------------------
*/
/* ARGSUSED */
static char *
char *name1; /* Name of variable. */
char *name2; /* Second part of variable name. */
int flags; /* Information about what happened. */
{
double value;
/*
* If the variable is unset, then immediately recreate it unless
* the whole interpreter is going away.
*/
if (flags & TCL_TRACE_UNSETS) {
}
return (char *) NULL;
}
/*
* If we came here because we updated the variable (in SetScaleValue),
* then ignore the trace. Otherwise update the scale with the value
* of the variable.
*/
return (char *) NULL;
}
if (stringValue != NULL) {
result = "can't assign non-numeric value to scale variable";
} else {
}
/*
* This code is a bit tricky because it sets the scale's value before
* calling SetScaleValue. This way, SetScaleValue won't bother to
* set the variable again or to invoke the -command. However, it
* also won't redisplay the scale, so we have to ask for that
* explicitly.
*/
}
return result;
}