v01i060: select: a selection widget, Part01/01
Mike Wexler
mikew at wyse.wyse.com
Thu Oct 13 03:32:55 AEST 1988
Submitted-by: gpasq at picuxa (Greg Pasquariello X1190)
Posting-number: Volume 1, Issue 60
Archive-name: select/part01
#! /bin/sh
# This is a shell archive. Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file". To overwrite existing
# files, type "sh file -c". You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g.. If this archive is complete, you
# will see the following message at the end:
# "End of archive 1 (of 1)."
# Contents: README Select.c Select.h SelectP.h makefile patchlevel.h
# select.man.tbl xtest.c
# Wrapped by mikew at wyse on Wed Oct 12 10:31:40 1988
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'README' -a "${1}" != "-c" ; then
echo shar: Will not clobber existing file \"'README'\"
else
echo shar: Extracting \"'README'\" \(1631 characters\)
sed "s/^X//" >'README' <<'END_OF_FILE'
X
X
XSelect Widget
X
XEnclosed is a copy of my select widget. Currently it works, although
Xit still lacks some functionality, mainly because I haven't had time to
Ximplement it. You cannot yet change the pixmap for example. But these
Xthings are minor, and either you can add them yourself, or you will have to
Xwait for an updated version.
X
XThe Select widget is created as any other widget, and is similar to a command
Xbutton. The difference lies in the fact that it has no border, and you can
Xprovide a bitmap for the widget to use as an icon. As it is now, only the
Xicon portion is sensitive, but that will be changed soon.
X
XThe translation tables call the callback routine(s) when button 1 is pressed,
Xand keep track of where the pointer is with any pointer motion.
X
XResources for the widget are:
X
X Name Type Default
X=====================================================================
X XtNcallback Callback NULL
X XtNmapData String NULL
X XtNmapHeight int 0
X XtNmapWidth int 0
X XtNlabel String NULL
X XtNfont XFontStruct fixed
X XtNforeground Pixel white
X
X
XIn order to use the widget, you must first load a bitmap, as created by the
Xbitmap editor. Pass the bitmap data, height, and width, an appropriate label,
Xand any fonts or foreground, and create the widget.
X
XYou may ask yourself "Why is this an entirely new widget, not a subclass of
Xa command widget?". Then again you may not. It was written this way, because
XI wanted to write a widget from scratch. Simple as that.
X
XAnyway, I don't think I left anything out. Enjoy. If you have any questions,
Xsend me mail at att!picuxa!gpasq.
X
XGreg Pasquariello
END_OF_FILE
if test 1631 -ne `wc -c <'README'`; then
echo shar: \"'README'\" unpacked with wrong size!
fi
# end of 'README'
fi
if test -f 'Select.c' -a "${1}" != "-c" ; then
echo shar: Will not clobber existing file \"'Select.c'\"
else
echo shar: Extracting \"'Select.c'\" \(5770 characters\)
sed "s/^X//" >'Select.c' <<'END_OF_FILE'
X#include <X11/Xlib.h>
X#include <X11/Intrinsic.h>
X#include <X11/IntrinsicP.h>
X#include <X11/CoreP.h>
X#include <X11/Xatom.h>
X#include <X11/StringDefs.h>
X#include <X11/cursorfont.h>
X#include "SelectP.h"
X
X#define PAD 4
X
Xstatic XtResource resources[] = {
X { XtNcallback, XtCCallback, XtRCallback, sizeof(caddr_t),
X XtOffset(SelectWidget, select.callbacks), XtRCallback,
X (caddr_t)NULL },
X { XtNmapData, XtCString, XtRString, sizeof(String),
X XtOffset(SelectWidget, select.map_data), XtRString, NULL },
X { XtNmapHeight, XtCInt, XtRInt, sizeof(int),
X XtOffset(SelectWidget, select.map_height), XtRInt, 0 },
X { XtNmapWidth, XtCInt, XtRInt, sizeof(int),
X XtOffset(SelectWidget, select.map_width), XtRInt, 0 },
X { XtNlabel, XtCLabel, XtRString, sizeof(String),
X XtOffset(SelectWidget, select.label), XtRString, NULL },
X { XtNforeground, XtCForeground, XtRPixel, sizeof(Pixel),
X XtOffset(SelectWidget, select.foreground), XtRString, "Black" },
X { XtNfont, XtCFont, XtRFontStruct, sizeof(XFontStruct *),
X XtOffset(SelectWidget, select.font), XtRString, "fixed" }
X };
X
X
Xstatic Boolean SetValues();
Xstatic void Destroy(),
X Motion(),
X Notify(),
X Redisplay(),
X Realize(),
X Initialize(),
X InitializeGC();
X
Xstatic char selectTrans[] =
X "<Motion>: motion()\n\
X <Btn1Up>: notify()";
X
Xstatic XtActionsRec selectActs[] = {
X {"motion", Motion},
X {"notify", Notify},
X };
X
XSelectClassRec selectClassRec = {
X {
X /* superclass */ (WidgetClass) &widgetClassRec,
X /* class name */ "Select",
X /* widget size */ sizeof(SelectRec),
X /* class initialize */ NULL,
X /* class part initialize */ NULL,
X /* class inited */ FALSE,
X /* initialize */ Initialize,
X /* initialize hook */ NULL,
X /* realize */ Realize,
X /* actions */ selectActs,
X /* number actions */ XtNumber(selectActs),
X /* resources */ resources,
X /* number resources */ XtNumber(resources),
X /* xrm class */ NULLQUARK,
X /* input flags */ TRUE, TRUE, TRUE, FALSE,
X /* destroy */ Destroy,
X /* resize */ XtInheritResize,
X /* expose */ Redisplay,
X /* set values */ SetValues,
X /* set values hook */ NULL,
X /* set values almost */ XtInheritSetValuesAlmost,
X /* get values hook */ NULL,
X /* accept focus */ NULL,
X /* version */ XtVersion,
X /* callback offsets */ NULL,
X /* tm table */ selectTrans
X }
X};
X
XWidgetClass selectWidgetClass = (WidgetClass) &selectClassRec;
X
Xstatic void Initialize(req, new)
XWidget req, new;
X {
X SelectWidget gw;
X
X gw = (SelectWidget) new;
X
X if(!(gw->select.label)) {
X gw->select.label = XtMalloc(strlen(gw->core.name) + 1);
X strcpy(gw->select.label, gw->core.name);
X }
X
X InitializeGC(gw);
X SetTextParms(gw);
X
X if(!gw->core.width)
X gw->core.width = (gw->select.map_width +
X gw->select.label_width) + PAD;
X if(!gw->core.height)
X gw->core.height = gw->select.label_height + PAD * 2;
X
X }
X
X
XSetTextParms(w)
XSelectWidget w;
X {
X w->select.label_len = strlen(w->select.label);
X w->select.label_width = XTextWidth(w->select.font, w->select.label,
X w->select.label_len);
X w->select.label_height = w->select.font->max_bounds.ascent +
X w->select.font->max_bounds.descent;
X }
X
X
X
Xstatic void InitializeGC(w)
XSelectWidget w;
X {
X XGCValues val;
X
X val.font = w->select.font->fid;
X val.foreground = w->select.foreground;
X
X w->select.normGC = XtGetGC(w, (GCForeground | GCFont), &val);
X }
X
X
X
Xstatic void Redisplay(w, event)
XSelectWidget w;
XXEvent *event;
X {
X Display *dy;
X XGCValues val;
X
X dy = XtDisplay(w);
X
X XCopyArea(dy, w->select.pixmap, XtWindow(w), w->select.normGC,
X 0, 0, w->select.map_width, w->select.map_height,
X w->select.begx - (w->select.map_width + PAD),
X w->select.begy - w->select.label_height);
X
X XDrawString(dy, XtWindow(w), w->select.normGC, w->select.begx,
X w->select.begy, w->select.label, w->select.label_len);
X
X }
X
X
X
X
Xstatic Boolean SetValues(cur, req, new)
XSelectWidget cur, req, new;
X {
X if(cur->select.font != new->select.font)
X SetTextParms(new);
X
X return TRUE;
X }
X
X
Xstatic void Realize(w, mask, attr)
XSelectWidget w;
XMask *mask;
XXSetWindowAttributes *attr;
X {
X Display *dy;
X Pixmap stipple;
X
X dy = XtDisplay(w);
X
X w->core.window = XCreateWindow(dy, XtWindow(w->core.parent),
X w->core.x, w->core.y, w->core.width, w->core.height, 0,
X CopyFromParent, CopyFromParent, CopyFromParent, *mask, attr);
X
X w->select.pixmap = XCreatePixmapFromBitmapData(dy, w->core.window,
X w->select.map_data, w->select.map_width,
X w->select.map_height, w->select.foreground,
X w->core.background_pixel,
X DefaultDepth(dy, DefaultScreen(dy)));
X
X XMapWindow(dy, w->core.window);
X
X w->select.begx = w->core.width - w->select.label_width;
X w->select.begy = w->core.height - w->select.label_height / 2;
X
X w->select.cursor = XCreateFontCursor(dy, CURSOR);
X }
X
Xstatic void Notify(w, event, parms, nparms)
XSelectWidget w;
XXEvent *event;
XString *parms;
XCardinal nparms;
X {
X if(event->type != ButtonRelease)
X return;
X
X if(event->xbutton.button != Button1)
X return;
X
X if((event->xbutton.x <= w->select.map_width)
X && (event->xbutton.y <= w->select.map_height))
X XtCallCallbacks(w, XtNcallback, NULL);
X }
X
X
Xstatic void Motion(w, event, parms, nparms)
XSelectWidget w;
XXEvent *event;
XString *parms;
XCardinal nparms;
X {
X if((event->xmotion.x <= w->select.map_width)
X && (event->xmotion.y <= w->select.map_height))
X XDefineCursor(XtDisplay(w), XtWindow(w), w->select.cursor);
X else
X XUndefineCursor(XtDisplay(w), XtWindow(w));
X }
X
Xstatic void Destroy(w)
XSelectWidget w;
X {
X XtRemoveAllCallbacks(w, XtNcallback);
X XtFree(w->select.label);
X XtDestroyGC(w, w->select.normGC);
X XFreePixmap(XtDisplay(w), w->select.pixmap);
X }
END_OF_FILE
if test 5770 -ne `wc -c <'Select.c'`; then
echo shar: \"'Select.c'\" unpacked with wrong size!
fi
# end of 'Select.c'
fi
if test -f 'Select.h' -a "${1}" != "-c" ; then
echo shar: Will not clobber existing file \"'Select.h'\"
else
echo shar: Extracting \"'Select.h'\" \(343 characters\)
sed "s/^X//" >'Select.h' <<'END_OF_FILE'
X#include <X11/Core.h>
X/*
X Select widget include file.
X 1988 Greg Pasquariello
X*/
X
X#define XtNmapData "mapData"
X#define XtNmapHeight "mapHeight"
X#define XtNmapWidth "mapWidth"
X#define XtNdistance "distance"
X
X#define XtCLabel "Label"
X#define XtCInt "Int"
X
Xtypedef struct _SelectRec *SelectWidget;
Xextern WidgetClass selectWidgetClass;
END_OF_FILE
if test 343 -ne `wc -c <'Select.h'`; then
echo shar: \"'Select.h'\" unpacked with wrong size!
fi
# end of 'Select.h'
fi
if test -f 'SelectP.h' -a "${1}" != "-c" ; then
echo shar: Will not clobber existing file \"'SelectP.h'\"
else
echo shar: Extracting \"'SelectP.h'\" \(655 characters\)
sed "s/^X//" >'SelectP.h' <<'END_OF_FILE'
X#include <X11/ShellP.h>
X#include <X11/LabelP.h>
X#include "Select.h"
X
X#define CURSOR XC_hand2
X
Xtypedef struct {
X Cursor cursor;
X XFontStruct *font;
X Pixel foreground;
X GC normGC;
X XtCallbackList callbacks;
X Dimension begx,
X begy;
X String label;
X String map_data;
X int map_width,
X map_height;
X Pixmap pixmap;
X int distance;
X int label_len;
X Dimension label_width,
X label_height;
X } SelectPart;
X
X
Xtypedef struct _SelectRec {
X CorePart core;
X SelectPart select;
X } SelectRec;
X
X
Xtypedef struct {
X int filler;
X } SelectClassPart;
X
Xtypedef struct _SelectClassRec {
X CoreClassPart core_class;
X SelectClassPart select_class;
X } SelectClassRec;
X
X
END_OF_FILE
if test 655 -ne `wc -c <'SelectP.h'`; then
echo shar: \"'SelectP.h'\" unpacked with wrong size!
fi
# end of 'SelectP.h'
fi
if test -f 'makefile' -a "${1}" != "-c" ; then
echo shar: Will not clobber existing file \"'makefile'\"
else
echo shar: Extracting \"'makefile'\" \(525 characters\)
sed "s/^X//" >'makefile' <<'END_OF_FILE'
X#
X# Not really much to this, other than the simple compile.
X# You must edit this file to archive in whatever libraries you
X# want, etc.
X#
XLDFLAGS=-L/usr/new/usr/lib
XLIBS=-lXaw -lXt -lX11
XCFLAGS=-O -I/usr/new/usr/include
Xall: xtest select.man
Xxtest: xtest.o Select.o
X cc $(LDFLAGS) -o xtest xtest.o Select.o $(LIBS)
XSelect.o: Select.c Select.h SelectP.h
X cc $(CFLAGS) -c Select.c
Xselect.man: select.man.tbl
X tbl select.man.tbl > select.man
Xclean:
X rm xtest *.o select.man
Xkit:
X makekit README *.c *.h makefile select.man.tbl
END_OF_FILE
if test 525 -ne `wc -c <'makefile'`; then
echo shar: \"'makefile'\" unpacked with wrong size!
fi
# end of 'makefile'
fi
if test -f 'patchlevel.h' -a "${1}" != "-c" ; then
echo shar: Will not clobber existing file \"'patchlevel.h'\"
else
echo shar: Extracting \"'patchlevel.h'\" \(21 characters\)
sed "s/^X//" >'patchlevel.h' <<'END_OF_FILE'
X#define PATCHLEVEL 0
END_OF_FILE
if test 21 -ne `wc -c <'patchlevel.h'`; then
echo shar: \"'patchlevel.h'\" unpacked with wrong size!
fi
# end of 'patchlevel.h'
fi
if test -f 'select.man.tbl' -a "${1}" != "-c" ; then
echo shar: Will not clobber existing file \"'select.man.tbl'\"
else
echo shar: Extracting \"'select.man.tbl'\" \(1340 characters\)
sed "s/^X//" >'select.man.tbl' <<'END_OF_FILE'
X.TH Select 3W "" "12 October 1988" "X Version 11"
X.SH NAME
XSelect \- Selection widget
X.SH DESCRIPTION
X.PP
XThe Select widget is created as any other widget, and is similar to a command
Xbutton. The difference lies in the fact that it has no border, and you can
Xprovide a bitmap for the widget to use as an icon. As it is now, only the
Xicon portion is sensitive, but that will be changed soon.
X.PP
XThe translation tables call the callback routine(s) when button 1 is pressed,
Xand keep track of where the pointer is with any pointer motion.
X.PP
XIn order to use the widget, you must first load a bitmap, as created by the
Xbitmap editor. Pass the bitmap data, height, and width, an appropriate label,
Xand any fonts or foreground, and create the widget.
X.PP
XYou may ask yourself "Why is this an entirely new widget, not a subclass of
Xa command widget?". Then again you may not. It was written this way, because
XI wanted to write a widget from scratch. Simple as that.
X.PP
XAnyway, I don't think I left anything out. Enjoy. If you have any questions,
Xsend me mail at att!picuxa!gpasq.
X.SH RESOURCES
X.TS
Xallbox expand ;
Xl l l .
XName Type Default
X
XXtNcallback Callback NULL
XXtNmapData String NULL
XXtNmapHeight int 0
XXtNmapWidth int 0
XXtNlabel String NULL
XXtNfont XFontStruct fixed
XXtNforeground Pixel white
X.TE
X.SH AUTHOR
XGreg Pasquariello
END_OF_FILE
if test 1340 -ne `wc -c <'select.man.tbl'`; then
echo shar: \"'select.man.tbl'\" unpacked with wrong size!
fi
# end of 'select.man.tbl'
fi
if test -f 'xtest.c' -a "${1}" != "-c" ; then
echo shar: Will not clobber existing file \"'xtest.c'\"
else
echo shar: Extracting \"'xtest.c'\" \(1326 characters\)
sed "s/^X//" >'xtest.c' <<'END_OF_FILE'
X#include <X11/Intrinsic.h>
X#include <X11/StringDefs.h>
X#include <X11/Box.h>
X#include "Select.h"
X
XWidget toplevel,
X box,
X button;
X
XArg wargs[10];
Xstatic void Routine();
X
X#define light_width 16
X#define light_height 16
Xstatic char light_bits[] = {
X 0xc0, 0x07, 0x20, 0x08, 0x10, 0x10, 0x08, 0x20, 0x08, 0x20, 0x08, 0x20,
X 0x88, 0x22, 0x88, 0x22, 0x90, 0x12, 0x20, 0x09, 0xc0, 0x07, 0x40, 0x04,
X 0xc0, 0x07, 0x40, 0x04, 0xc0, 0x07, 0x00, 0x01};
X
Xmain(argc, argv)
Xint argc;
Xchar **argv;
X {
X register int n;
X
X toplevel = XtInitialize("main", "XFm", NULL, 0, &argc, argv);
X
X n = 0;
X XtSetArg(wargs[n], XtNx, 100); n++;
X XtSetArg(wargs[n], XtNy, 100); n++;
X box = XtCreateManagedWidget("box", boxWidgetClass,
X toplevel, wargs, n);
X
X n = 0;
X XtSetArg(wargs[n], XtNx, 12); n++;
X XtSetArg(wargs[n], XtNy, 10); n++;
X XtSetArg(wargs[n], XtNlabel, "Light Bulb Label"); n++;
X XtSetArg(wargs[n], XtNmapData, light_bits); n++;
X XtSetArg(wargs[n], XtNmapHeight, light_height); n++;
X XtSetArg(wargs[n], XtNmapWidth, light_width); n++;
X button = XtCreateManagedWidget("lightbutton", selectWidgetClass,
X box, wargs, n);
X
X XtAddCallback(button, XtNcallback, Routine, NULL);
X
X XtRealizeWidget(toplevel);
X XtMainLoop();
X }
X
Xstatic void Routine(w, client, call)
XWidget w;
Xcaddr_t client,
X call;
X {
X printf("The lightbulb was selected\n");
X }
END_OF_FILE
if test 1326 -ne `wc -c <'xtest.c'`; then
echo shar: \"'xtest.c'\" unpacked with wrong size!
fi
# end of 'xtest.c'
fi
echo shar: End of archive 1 \(of 1\).
cp /dev/null ark1isdone
MISSING=""
for I in 1 ; do
if test ! -f ark${I}isdone ; then
MISSING="${MISSING} ${I}"
fi
done
if test "${MISSING}" = "" ; then
echo You have the archive.
rm -f ark[1-9]isdone
else
echo You still need to unpack the following archives:
echo " " ${MISSING}
fi
## End of shell archive.
exit 0
--
Mike Wexler(wyse!mikew) Phone: (408)433-1000 x1330
Moderator of comp.sources.x
More information about the Comp.sources.x
mailing list