/*
* tkButton.c --
*
* This module implements a collection of button-like
* widgets for the Tk toolkit. The widgets implemented
* include labels, buttons, check buttons, and radio
* buttons.
*
* 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: @(#) tkButton.c 1.128 96/03/01 17:34:49
*/
#include "tkInt.h"
#include "tkDefault.h"
/*
* A data structure of the following type is kept for each
* widget managed by this file:
*/
typedef struct {
* means that the window has been destroyed. */
* free up resources after tkwin is gone. */
* that may be performed on widget. See
* below for possible values. */
/*
* Information about what's in the button.
*/
* or NULL. */
* don't underline anything. */
* If non-NULL, button displays the contents
* of this variable. */
* then text and textVar are ignored. */
* NULL. If non-NULL, bitmap, text, and
* textVarName are ignored. */
* none. */
* (malloc'ed), or NULL. */
* or NULL if none. Ignored if image is
* NULL. */
/*
* Information used when displaying widget:
*/
* normal, active, or disabled. */
* border and background when window
* isn't active. NULL means no such
* border exists. */
* border and background when window
* is active. NULL means no such
* border exists. */
* 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. */
* means use normalFg instead. */
* means use normalFg with a 50% stipple
* instead. */
* used to copy from off-screen pixmap onto
* screen. */
* means use normalTextGC). */
* disabledFg is NULL. */
* disabledFg isn't NULL, this GC is used to
* draw button text or icon. Otherwise
* text or icon is drawn with normalGC and
* this GC is used to stipple background
* across it. For labels this is None. */
* off-screen pixmap to the screen. */
* for window, in characters for text and in
* pixels for bitmaps. In this case the actual
* size of the text string or bitmap is
* ignored in computing desired window size. */
* onto next line. <= 0 means don't wrap
* except at newlines. */
* on each side). Ignored for bitmaps and
* images. */
* inside button region. */
* don't draw it. */
* widget background, when selected. */
* in pixels. */
* in pixels. */
* display of indicator. */
/*
* For check and radio buttons, the fields below are used
* to manage the variable indicating the button's state.
*/
* state of button. Malloc'ed (if
* not NULL). */
* this button is selected. Malloc'ed (if
* not NULL). */
* button isn't selected. Malloc'ed
* (if not NULL). Valid only for check
* buttons. */
/*
* Miscellaneous information:
*/
* the C code, but used by keyboard traversal
* scripts. Malloc'ed, but may be NULL. */
* invoked; valid for buttons only.
* If not NULL, it's malloc-ed. */
* definitions. */
} Button;
/*
* Possible "type" values for buttons. These are the kinds of
* widgets supported by this file. The ordering of the type
* numbers is significant: greater means more features and is
* used in the code.
*/
#define TYPE_LABEL 0
/*
* Class names for buttons, indexed by one of the type values above.
*/
/*
* Flag bits for buttons:
*
* REDRAW_PENDING: Non-zero means a DoWhenIdle handler
* has already been queued to redraw
* this window.
* SELECTED: Non-zero means this button is selected,
* so special highlight should be drawn.
* GOT_FOCUS: Non-zero means this button currently
* has the input focus.
*/
/*
* Mask values used to selectively enable entries in the
* configuration specs:
*/
/*
* Information used for parsing configuration specs:
*/
"DisabledForeground", DEF_BUTTON_DISABLED_FG_COLOR,
"DisabledForeground", DEF_BUTTON_DISABLED_FG_MONO,
ALL_MASK},
"HighlightBackground", DEF_BUTTON_HIGHLIGHT_BG,
ALL_MASK},
"HighlightThickness",
"HighlightThickness",
(char *) NULL, 0, 0}
};
/*
* String to print out in error messages, identifying options for
* widget commands for different types of labels or buttons:
*/
static char *optionStrings[] = {
"cget or configure",
"cget, configure, flash, or invoke",
"cget, configure, deselect, flash, invoke, select, or toggle",
"cget, configure, deselect, flash, invoke, or select"
};
/*
* Forward declarations for procedures defined later in this file:
*/
static void ButtonCmdDeletedProc _ANSI_ARGS_((
int type));
static void ButtonSelectImageProc _ANSI_ARGS_((
int flags));
int flags));
int flags));
/*
*--------------------------------------------------------------
*
* Tk_ButtonCmd, Tk_CheckbuttonCmd, Tk_LabelCmd, Tk_RadiobuttonCmd --
*
* These procedures are invoked to process the "button", "label",
* "radiobutton", and "checkbutton" Tcl commands. See the
* user documentation for details on what they do.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* See the user documentation. These procedures are just wrappers;
* they call ButtonCreate to do all of the real work.
*
*--------------------------------------------------------------
*/
int
* interpreter. */
int argc; /* Number of arguments. */
char **argv; /* Argument strings. */
{
}
int
* interpreter. */
int argc; /* Number of arguments. */
char **argv; /* Argument strings. */
{
}
int
* interpreter. */
int argc; /* Number of arguments. */
char **argv; /* Argument strings. */
{
}
int
* interpreter. */
int argc; /* Number of arguments. */
char **argv; /* Argument strings. */
{
}
/*
*--------------------------------------------------------------
*
* ButtonCreate --
*
* This procedure does all the real work of implementing the
* "button", "label", "radiobutton", and "checkbutton" Tcl
* commands. See the user documentation for details on what it does.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* See the user documentation.
*
*--------------------------------------------------------------
*/
static int
* interpreter. */
int argc; /* Number of arguments. */
char **argv; /* Argument strings. */
int type; /* Type of button to create: TYPE_LABEL,
* TYPE_BUTTON, TYPE_CHECK_BUTTON, or
* TYPE_RADIO_BUTTON. */
{
if (argc < 2) {
return TCL_ERROR;
}
/*
* Create the new window.
*/
return TCL_ERROR;
}
/*
* Initialize the data structure for the button.
*/
butPtr->borderWidth = 0;
butPtr->highlightWidth = 0;
butPtr->wrapLength = 0;
butPtr->indicatorOn = 0;
butPtr->indicatorSpace = 0;
butPtr->indicatorDiameter = 0;
return TCL_ERROR;
}
return TCL_OK;
}
/*
*--------------------------------------------------------------
*
* ButtonWidgetCmd --
*
* 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
int argc; /* Number of arguments. */
char **argv; /* Argument strings. */
{
int c;
if (argc < 2) {
"wrong # args: should be \"%.50s option ?arg arg ...?\"",
argv[0]);
return TCL_ERROR;
}
c = argv[1][0];
&& (length >= 2)) {
if (argc != 3) {
argv[0], " cget option\"",
(char *) NULL);
goto error;
}
&& (length >= 2)) {
if (argc == 2) {
} else if (argc == 3) {
} else {
}
if (argc > 2) {
"wrong # args: should be \"%.50s deselect\"",
argv[0]);
goto error;
}
}
};
}
int i;
if (argc > 2) {
"wrong # args: should be \"%.50s flash\"",
argv[0]);
goto error;
}
for (i = 0; i < 4; i++) {
? tkActiveUid : tkNormalUid;
: butPtr->normalBorder);
/*
* Special note: must cancel any existing idle handler
* for DisplayButton; it's no longer needed, and DisplayButton
* cleared the REDRAW_PENDING flag.
*/
Tcl_Sleep(50);
}
}
if (argc > 2) {
"wrong # args: should be \"%.50s invoke\"",
argv[0]);
goto error;
}
}
if (argc > 2) {
"wrong # args: should be \"%.50s select\"",
argv[0]);
goto error;
}
}
if (argc > 2) {
"wrong # args: should be \"%.50s toggle\"",
argv[0]);
goto error;
}
TCL_GLOBAL_ONLY) == NULL) {
}
} else {
TCL_GLOBAL_ONLY) == NULL) {
}
}
} else {
goto error;
}
return result;
return TCL_ERROR;
}
/*
*----------------------------------------------------------------------
*
* DestroyButton --
*
* 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 widget is freed up.
*
*----------------------------------------------------------------------
*/
static void
{
/*
* Free up all the stuff that requires special handling, then
* let Tk_FreeOptions handle all the standard option-related
* stuff.
*/
}
}
}
}
}
}
}
}
}
}
/*
*----------------------------------------------------------------------
*
* ConfigureButton --
*
* the Tk option database, in order to configure (or
* reconfigure) a button 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 text string, colors, font,
* etc. get set for butPtr; old resources get freed, if there
* were any. The button is redisplayed.
*
*----------------------------------------------------------------------
*/
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. */
{
unsigned long mask;
/*
* Eliminate any existing trace on variables monitored by the button.
*/
}
}
return TCL_ERROR;
}
/*
* A few options need special processing, such as setting the
* background from a 3-D border, or filling in complicated
* defaults that couldn't be specified to Tk_ConfigureWidget.
*/
} else {
"\": must be normal, active, or disabled", (char *) NULL);
return TCL_ERROR;
}
}
if (butPtr->highlightWidth < 0) {
butPtr->highlightWidth = 0;
}
/*
* Note: GraphicsExpose events are disabled in normalTextGC because it's
* used to copy stuff from an off-screen pixmap onto the screen (we know
* that there's no problem with obscured areas).
*/
&gcValues);
}
}
}
} else {
Tk_GetUid("gray50"));
return TCL_ERROR;
}
}
}
}
}
}
}
}
char *value;
}
/*
* Select the button if the associated variable has the
* appropriate value, initialize the variable if it doesn't
* exist, then set a trace on the variable to monitor future
* changes to its value.
*/
}
} else {
return TCL_ERROR;
}
}
}
/*
* Get the images for the widget, if there are any. Allocate the
* new images before freeing the old ones, so that the reference
* counts don't go to zero and cause image data to be discarded.
*/
return TCL_ERROR;
}
} else {
}
}
(ClientData) butPtr);
return TCL_ERROR;
}
} else {
}
}
/*
* The button must display the value of a variable: set up a trace
* on the variable's value, create the variable if it doesn't
* exist, and fetch its current value.
*/
char *value;
return TCL_ERROR;
}
} else {
}
}
}
return TCL_ERROR;
}
return TCL_ERROR;
}
} else {
!= TCL_OK) {
goto widthError;
}
!= TCL_OK) {
goto heightError;
}
}
/*
* Lastly, arrange for the button to be redisplayed.
*/
}
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* DisplayButton --
*
* This procedure is invoked to display a button widget. It is
* normally invoked as an idle handler.
*
* Results:
* None.
*
* Side effects:
* Commands are output to X to display the button in its
* current mode. The REDRAW_PENDING flag is cleared.
*
*----------------------------------------------------------------------
*/
static void
{
int x = 0; /* Initialization only needed to stop
* compiler warning. */
int y, relief;
* it is a flavor of button, so we offset
* the text to make the button appear to
* move up and down as the relief changes. */
return;
}
} else {
}
}
/*
* Override the relief specified for the button if this is a
* checkbutton or radiobutton and there's no indicator.
*/
}
/*
* In order to avoid screen flashes, this procedure redraws
* the button 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.
*/
/*
* Display image or bitmap or text for button.
*/
break;
- width))/2;
break;
default:
break;
}
break;
break;
default:
break;
}
if (relief == TK_RELIEF_RAISED) {
x -= offset;
y -= offset;
} else if (relief == TK_RELIEF_SUNKEN) {
x += offset;
y += offset;
}
x, y);
} else {
x, y);
}
} else {
}
y += height/2;
goto imageOrBitmap;
} else {
+ offset;
break;
break;
default:
break;
}
break;
break;
default:
break;
}
if (relief == TK_RELIEF_RAISED) {
x -= offset;
y -= offset;
} else if (relief == TK_RELIEF_SUNKEN) {
x += offset;
y += offset;
}
}
/*
* Draw the indicator for check buttons and radio buttons. At this
* point x and y refer to the top-left corner of the text or image
* or bitmap.
*/
int dim;
x -= butPtr->indicatorSpace;
y -= dim/2;
x += butPtr->borderWidth;
y += butPtr->borderWidth;
} else {
}
}
int radius;
points[0].y = y;
} else {
}
}
/*
* If the button is disabled with a stipple rather than a special
* foreground color, generate the stippled effect. If the widget
* is selected and we use a different background color when selected,
* must temporarily modify the GC.
*/
}
}
}
/*
* Draw the border and traversal highlight last. This way, if the
* button's contents overflow they'll be covered up by the border.
*/
if (relief != TK_RELIEF_FLAT) {
}
if (butPtr->highlightWidth != 0) {
} else {
}
}
/*
* Copy the information from the off-screen pixmap onto the screen,
* then delete the pixmap.
*/
}
/*
*--------------------------------------------------------------
*
* ButtonEventProc --
*
* This procedure is invoked by the Tk dispatcher for various
* events on buttons.
*
* Results:
* None.
*
* Side effects:
* When the window gets deleted, internal structures get
* cleaned up. When it gets exposed, it is redisplayed.
*
*--------------------------------------------------------------
*/
static void
{
goto redraw;
/*
* Must redraw after size changes, since layout could have changed
* and borders will need to be redrawn.
*/
goto redraw;
}
}
if (butPtr->highlightWidth > 0) {
goto redraw;
}
}
if (butPtr->highlightWidth > 0) {
goto redraw;
}
}
}
return;
}
}
/*
*----------------------------------------------------------------------
*
* ButtonCmdDeletedProc --
*
* 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.
*/
}
}
/*
*----------------------------------------------------------------------
*
* ComputeButtonGeometry --
*
* After changes in a button's text or bitmap, this procedure
* recomputes the button's geometry and passes this information
* along to the geometry manager for the window.
*
* Results:
* None.
*
* Side effects:
* The button's window may change size.
*
*----------------------------------------------------------------------
*/
static void
{
if (butPtr->highlightWidth < 0) {
butPtr->highlightWidth = 0;
}
butPtr->indicatorSpace = 0;
}
}
} else {
}
}
goto imageOrBitmap;
} else {
&butPtr->textHeight);
}
}
}
}
}
/*
* When issuing the geometry request, add extra space for the indicator,
* if any, and for the border and padding, plus two extra pixels so the
* display can be offset by 1 pixel in either direction for the raised
* or lowered effect.
*/
}
width += 2;
height += 2;
}
}
/*
*----------------------------------------------------------------------
*
* InvokeButton --
*
* This procedure is called to carry out the actions associated
* with a button, such as invoking a Tcl command or setting a
* variable. This procedure is invoked, for example, when the
* button is invoked via the mouse.
*
* Results:
* A standard Tcl return value. Information is also left in
* interp->result.
*
* Side effects:
* Depends on the button and its associated command.
*
*----------------------------------------------------------------------
*/
static int
{
return TCL_ERROR;
}
} else {
return TCL_ERROR;
}
}
return TCL_ERROR;
}
}
}
return TCL_OK;
}
/*
*--------------------------------------------------------------
*
* ButtonVarProc --
*
* This procedure is invoked when someone changes the
* state variable associated with a radio button. Depending
* on the new value of the button's variable, the button
* may be selected or deselected.
*
* Results:
* NULL is always returned.
*
* Side effects:
* The button may become selected or deselected.
*
*--------------------------------------------------------------
*/
/* ARGSUSED */
static char *
char *name1; /* Name of variable. */
char *name2; /* Second part of variable name. */
int flags; /* Information about what happened. */
{
char *value;
/*
* If the variable is being unset, then just re-establish the
* trace unless the whole interpreter is going away.
*/
if (flags & TCL_TRACE_UNSETS) {
}
goto redisplay;
}
/*
* Use the value of the variable to update the selected status of
* the button.
*/
value = "";
}
return (char *) NULL;
}
} else {
return (char *) NULL;
}
}
return (char *) NULL;
}
/*
*--------------------------------------------------------------
*
* ButtonTextVarProc --
*
* This procedure is invoked when someone changes the variable
* whose contents are to be displayed in a button.
*
* Results:
* NULL is always returned.
*
* Side effects:
* The text displayed in the button will change to match the
* variable.
*
*--------------------------------------------------------------
*/
/* ARGSUSED */
static char *
char *name1; /* Not used. */
char *name2; /* Not used. */
int flags; /* Information about what happened. */
{
char *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;
}
value = "";
}
}
}
return (char *) NULL;
}
/*
*----------------------------------------------------------------------
*
* ButtonImageProc --
*
* This procedure is invoked by the image code whenever the manager
* for an image does something that affects the size of contents
* of an image displayed in a button.
*
* Results:
* None.
*
* Side effects:
* Arranges for the button to get redisplayed.
*
*----------------------------------------------------------------------
*/
static void
int x, y; /* Upper left pixel (within image)
* that must be redisplayed. */
* (may be <= 0). */
{
}
}
}
/*
*----------------------------------------------------------------------
*
* ButtonSelectImageProc --
*
* This procedure is invoked by the image code whenever the manager
* for an image does something that affects the size of contents
* of the image displayed in a button when it is selected.
*
* Results:
* None.
*
* Side effects:
* May arrange for the button to get redisplayed.
*
*----------------------------------------------------------------------
*/
static void
int x, y; /* Upper left pixel (within image)
* that must be redisplayed. */
* (may be <= 0). */
{
/*
* Don't recompute geometry: it's controlled by the primary image.
*/
}
}