home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 October
/
usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso
/
x
/
volume10
/
xt-examples
/
part04
/
Confirm.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-11-04
|
9KB
|
279 lines
/***********************************************************
Copyright 1990 by Digital Equipment Corporation, Maynard, Massachusetts.
All Rights Reserved
Permission to use, copy, modify, and distribute these examples for any
purpose and without fee is hereby granted, provided that the above
copyright notice appear in all copies and that both that copyright
notice and this permission notice appear in supporting documentation,
and that the name of Digital not be used in advertising or publicity
pertaining to distribution of the software without specific, written
prior permission.
DIGITAL AND THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS, IN NO EVENT SHALL DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT
OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
OR PERFORMANCE OF THIS SOFTWARE.
******************************************************************/
#include <X11/IntrinsicP.h> /* Intrinsics header file */
#include <X11/StringDefs.h> /* Resource string definitions */
#include "ConfirmP.h" /* Confirm private header file */
#include "Label.h" /* Label public header file */
#include "Pushbutton.h" /* Pushbutton public header file */
#define Offset(field) XtOffsetOf(ConfirmRec, confirm.field)
static XtResource resources[] = {
{XtNlabel, XtCLabel, XtRString, sizeof(String),
Offset(label), XtRString, "Click to confirm"},
{XtNbuttonLabel, XtCButtonLabel, XtRString, sizeof(String),
Offset(button_label), XtRString, "OK"},
{XtNcallback, XtCCallback, XtRCallback, sizeof(XtCallbackList),
Offset(callback), XtRCallback, (XtPointer) NULL},
};
#undef Offset
#define LabelChild(w) (((CompositeWidget) w)->composite.children[0])
#define ButtonChild(w) (((CompositeWidget) w)->composite.children[1])
/* Forward declarations */
static void InsertChild(), Initialize(), PositionChildren(),
GetValuesHook();
static Boolean SetValues();
static XtGeometryResult GeometryManager();
ConfirmClassRec confirmClassRec = {
/* Core class part */
{
/* superclass */ (WidgetClass) &boxClassRec,
/* class_name */ "Confirm",
/* widget_size */ sizeof(ConfirmRec),
/* class_initialize */ NULL,
/* class_part_initialize */ NULL,
/* class_inited */ FALSE,
/* initialize */ Initialize,
/* initialize_hook */ NULL,
/* realize */ XtInheritRealize,
/* actions */ NULL,
/* num_actions */ 0,
/* resources */ resources,
/* num_resources */ XtNumber(resources),
/* xrm_class */ NULLQUARK,
/* compress_motion */ TRUE,
/* compress_exposure */ XtExposeCompressMultiple,
/* compress_enterleave */ TRUE,
/* visible_interest */ FALSE,
/* destroy */ NULL,
/* resize */ PositionChildren,
/* expose */ NULL,
/* set_values */ SetValues,
/* set_values_hook */ NULL,
/* set_values_almost */ XtInheritSetValuesAlmost,
/* get_values_hook */ GetValuesHook,
/* accept_focus */ NULL,
/* version */ XtVersion,
/* callback offsets */ NULL,
/* tm_table */ NULL,
/* query_geometry */ XtInheritQueryGeometry,
/* display_accelerator */ NULL,
/* extension */ NULL,
},
/* Composite class part */
{
/* geometry_manager */ GeometryManager,
/* change_managed */ XtInheritChangeManaged,
/* insert_child */ InsertChild,
/* delete_child */ XtInheritDeleteChild,
/* extension */ NULL,
},
/* Box class part */
{
/* extension */ NULL,
},
/* Confirm class part */
{
/* extension */ NULL,
}
};
WidgetClass confirmWidgetClass = (WidgetClass) &confirmClassRec;
static void InsertChild(w)
Widget w;
{
String params[1];
Cardinal num_params;
CompositeWidget parent = (CompositeWidget) XtParent(w);
if (parent->composite.num_children >= 2) {
params[0] = XtClass(parent)->core_class.class_name;
num_params = 1;
XtAppErrorMsg(XtWidgetToApplicationContext(w),
"childError", "number", "WidgetError",
"Applications cannot add children to %s widgets",
params, &num_params);
}
(*((CompositeWidgetClass)(confirmWidgetClass->
core_class.superclass))->composite_class.insert_child) (w);
}
static void Callback(w, call_data, client_data)
Widget w;
XtPointer call_data, client_data;
{
ConfirmWidget confirm = (ConfirmWidget) XtParent(w);
if (XtIsShell(XtParent(confirm))) XtPopdown(XtParent(confirm));
XtCallCallbackList(confirm, confirm->confirm.callback,
(XtPointer) NULL);
}
static void CalculateSize(cw, width, height)
ConfirmWidget cw;
Dimension *width, *height;
{
int label_width = LabelChild(cw)->core.width +
2 * LabelChild(cw)->core.border_width,
button_width = ButtonChild(cw)->core.width +
2 * ButtonChild(cw)->core.border_width,
max;
max = label_width > button_width ? label_width : button_width;
*width = max + 2 * cw->box.margin;
*height = LabelChild(cw)->core.height +
ButtonChild(cw)->core.height +
2 * (LabelChild(cw)->core.border_width +
ButtonChild(cw)->core.border_width) +
3 * cw->box.margin;
}
static void PositionChildren(cw)
ConfirmWidget cw;
{
int label_width = LabelChild(cw)->core.width +
2 * LabelChild(cw)->core.border_width,
button_width = ButtonChild(cw)->core.width +
2 * ButtonChild(cw)->core.border_width;
XtMoveWidget(LabelChild(cw), (cw->core.width - label_width) / 2,
cw->box.margin);
XtMoveWidget(ButtonChild(cw), (cw->core.width - button_width) / 2,
2 * cw->box.margin +
2 * LabelChild(cw)->core.border_width +
LabelChild(cw)->core.height);
}
static void Initialize(req, new, args, num_args)
Widget req, new;
ArgList args;
Cardinal *num_args;
{
ConfirmWidget cw = (ConfirmWidget) new;
Arg arg[1];
cw->confirm.label = XtNewString(cw->confirm.label);
XtSetArg(arg[0], XtNlabel, cw->confirm.label);
(void) XtCreateManagedWidget("label", labelWidgetClass,
new, arg, 1);
cw->confirm.button_label = XtNewString(cw->confirm.button_label);
XtSetArg(arg[0], XtNlabel, cw->confirm.button_label);
(void) XtCreateManagedWidget("button", pushbuttonWidgetClass,
new, arg, 1);
XtAddCallback(ButtonChild(cw), XtNcallback, Callback, NULL);
CalculateSize(cw, &cw->core.width, &cw->core.height);
PositionChildren(cw);
}
static void Destroy(w)
Widget w;
{
ConfirmWidget cw = (ConfirmWidget) w;
XtFree((char *) cw->confirm.label);
XtFree((char *) cw->confirm.button_label);
}
static Boolean SetValues(old, req, new, args, num_args)
Widget old, req, new;
ArgList args;
Cardinal *num_args;
{
register ConfirmWidget oldcw = (ConfirmWidget) old;
register ConfirmWidget newcw = (ConfirmWidget) new;
Arg arg[1];
Boolean resize = FALSE;
#define NE(field) (oldcw->field != newcw->field)
if (NE(confirm.label)) {
XtFree((char *) oldcw->confirm.label);
newcw->confirm.label = XtNewString(newcw->confirm.label);
XtSetArg(arg[0], XtNlabel, newcw->confirm.label);
XtSetValues(LabelChild(newcw), arg, 1);
resize = TRUE;
}
if (NE(confirm.button_label)) {
XtFree((char *) oldcw->confirm.button_label);
newcw->confirm.button_label =
XtNewString(newcw->confirm.button_label);
XtSetArg(arg[0], XtNlabel, newcw->confirm.button_label);
XtSetValues(ButtonChild(newcw), arg, 1);
resize = TRUE;
}
if (NE(box.margin) || resize) {
CalculateSize(newcw, &newcw->core.width, &newcw->core.height);
}
return FALSE;
#undef NE
}
static void GetValuesHook(w, args, num_args)
Widget w;
ArgList args;
Cardinal *num_args;
{
register int i;
for (i = 0; i < *num_args; i++) {
if (strcmp(args[i].name, XtNlabelWidget) == 0) {
*(Widget *) (args[i].value) = LabelChild(w);
} else if (strcmp(args[i].name, XtNbuttonWidget) == 0) {
*(Widget *) (args[i].value) = ButtonChild(w);
}
}
}
static XtGeometryResult GeometryManager(w, desired, allowed)
Widget w;
XtWidgetGeometry *desired, *allowed;
{
#define Wants(flag) (desired->request_mode & flag)
if (Wants(CWWidth)) w->core.width = desired->width;
if (Wants(CWHeight)) w->core.height = desired->height;
if (Wants(CWX)) w->core.x = desired->x;
if (Wants(CWY)) w->core.y = desired->y;
if (Wants(CWBorderWidth)) {
w->core.border_width = desired->border_width;
}
return XtGeometryYes;
#undef Wants
}