14 The Scale Widget
This chapter describes how to use the Scale widget to represent a range of values. The widget can be manipulated to
change the value.
The Scale widget displays a numeric value that falls within upper and lower bounds. The widget allows the user to
change that value interactively using a slider mechanism similar to that of a ScrollBar. This style of interface is useful
when it is inconvenient or inappropriate to have the user change a value using the keyboard. The widget is also
extremely intuitive to use; inexperienced users often understand how a Scale works when they first see one. the figure
shows how a Scale can be used with other widgets in an application.
A Scale widget in an application
A Scale can be oriented either horizontally or vertically. The values given to a Scale are stored as integers, but decimal
representation of values is possible through the use of a resource that allows you to place a decimal point in the value.
A Scale can be put in output−only mode, in which it is sometimes called a gauge. When a Scale is read−only, it
implies that the value is controlled by another widget or that it is being used to report status information specific to the
application. The standard way to create a read−only Scale is to specify that it is insensitive. Unfortunately, this
technique has the side−effect of graying out the widget. One workaround is to create a Scale widget that is sensitive,
but that has a null translation table.
14.1 Creating a Scale Widget
Applications that use the Scale widget must include the header file <Xm/Scale.h>. You can then create a Scale widget
as follows:
Widget scale;
367
scale = XtVaCreateManagedWidget ("name",
xmScaleWidgetClass, parent,
resource−value−list,
NULL);
Even though the Scale widget functions as a primitive widget, it is actually subclassed from the Manager widget. All
the parts of a Scale are really other primitive widgets, but these subwidgets are not accessible through the Motif
toolkit. The fact that the Scale is a Manager widget means that you can create widgets that are children of a Scale. The
children are arranged so that they are evenly distributed along the vertical or horizontal axis parallel to the slider,
depending on the orientation of the Scale. This technique is used primarily to provide "tick marks" for the Scale, as
we'll describe later. In all other respects, a Scale can be treated just like other primitive widgets. the source code shows
a program that creates some Scale widgets. XtSetLanguageProc() is only available in X11R5; there is no
corresponding function in X11R4.
/* simple_scale.c −− demonstrate a few scale widgets. */
#include <Xm/Scale.h>
#include <Xm/RowColumn.h>
main(argc, argv)
int argc;
char *argv[];
{
Widget toplevel, rowcol, scale;
XtAppContext app;
void new_value(); /* callback for Scale widgets */
XtSetLanguageProc (NULL, NULL, NULL);
toplevel = XtVaAppInitialize (&app, "Demos", NULL, 0,
&argc, argv, NULL, NULL);
rowcol = XtVaCreateWidget ("rowcol",
xmRowColumnWidgetClass, toplevel,
XmNorientation, XmHORIZONTAL,
NULL);
scale = XtVaCreateManagedWidget ("Days",
xmScaleWidgetClass, rowcol,
XtVaTypedArg, XmNtitleString, XmRString, "Days", 5,
XmNmaximum, 7,
XmNminimum, 1,
XmNvalue, 1,
XmNshowValue, True,
NULL);
XtAddCallback (scale, XmNvalueChangedCallback, new_value, NULL);
scale = XtVaCreateManagedWidget ("Weeks",
xmScaleWidgetClass, rowcol,
XtVaTypedArg, XmNtitleString, XmRString, "Weeks", 6,
XmNmaximum, 52,
XmNminimum, 1,
XmNvalue, 1,
XmNshowValue, True,
NULL);
XtAddCallback (scale, XmNvalueChangedCallback, new_value, NULL);
scale = XtVaCreateManagedWidget ("Months",
xmScaleWidgetClass, rowcol,
14 The Scale Widget 14 The Scale Widget
368
XtVaTypedArg, XmNtitleString, XmRString, "Months", 7,
XmNmaximum, 12,
XmNminimum, 1,
XmNvalue, 1,
XmNshowValue, True,
NULL);
XtAddCallback (scale, XmNvalueChangedCallback, new_value, NULL);
scale = XtVaCreateManagedWidget ("Years",
xmScaleWidgetClass, rowcol,
XtVaTypedArg, XmNtitleString, XmRString, "Years", 6,
XmNmaximum, 20,
XmNminimum, 1,
XmNvalue, 1,
XmNshowValue, True,
NULL);
XtAddCallback (scale, XmNvalueChangedCallback, new_value, NULL);
XtManageChild (rowcol);
XtRealizeWidget (toplevel);
XtAppMainLoop (app);
}
void
new_value(scale_w, client_data, call_data)
Widget scale_w;
XtPointer client_data;
XtPointer call_data;
{
XmScaleCallbackStruct *cbs = (XmScaleCallbackStruct *) call_data;
printf("%s: %d0, XtName(scale_w), cbs−>value);
}
The output of this program is shown in the figure.
Output of simple_scale.c
The four Scales represent the number of days, weeks, months, and years, respectively. Each Scale displays a title that
is specified by the XmNtitleString resource. Just as with other Motif widgets that display strings, the
XmNtitleString must be set as a compound string, not a normal C string. The easiest way to make the conversion
is to use the XtVaTypedArg feature, as we've done in this example. The use of this conversion method is described
in detail in Chapter 19, Compound Strings.
14 The Scale Widget 14 The Scale Widget
369
Get Volume 6A: Motif Programming Manual now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.