v08i008: xmenu - popup menus from shell scripts, Part01/01

Richard Hesketh rlh2 at ukc.ac.uk
Wed Jun 27 16:51:38 AEST 1990


Submitted-by: Richard Hesketh <rlh2 at ukc.ac.uk>
Posting-number: Volume 8, Issue 8
Archive-name: xmenu/part01

Someone asked for this facility on comp.windows.x and as I could with one
as well .. here is one.  It is also an example use of the Athena
Simple Menu widget.

Richard Hesketh   :   @nsfnet-relay.ac.uk:rlh2 at ukc.ac.uk
		  :   rlh2 at ukc.ac.uk    ..!mcvax!ukc!rlh2
---                                               
Computing Lab., University of Kent at Canterbury,
Canterbury, Kent, CT2 7NF, United Kingdom.    Tel: +44 227 764000 ext 3682/7620

#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create the files:
#	README
#	Imakefile
#	patchlevel.h
#	xmenu.man
#	xmenu.c
# This archive created: Tue Jun 19 17:02:52 1990
# By:	Richard Hesketh ()
export PATH; PATH=/bin:$PATH
if test -f 'README'
then
	echo shar: will not over-write existing file "'README'"
else
cat << \SHAR_EOF > 'README'
XMenu - provide a popup menu from a shell script etc.

This little toolkit program produces a popup menu on the screen taking
the command line arguments as menu entries.  If an argument contains
a non-escaped '=' (equals sign) the string to the left is the name
displayed in the menu and the string to the right is the text output
when this menu button is pressed.

A special argument "line" can be used to draw a dividing line between
successive menu entries.

A menu title can be given as an argument after a "-heading" flag.

The menu is only popped down when a menu button is pressed.
By default the menu is popped up below and to the right of the current
pointer position, use "-geometry" to position the menu at a particular
point (and in a particular size). 

Examples:

xmenu -heading "Choose Files by Suffix" Compressed="*.Z" \
		"C sources"="*.c" "Headers"="*.h" \
		line line "        Cancel"
 
xmenu Hello="Hello World" line " " " " line Goodbye="Goodbye World"


Enjoy,

Richard Hesketh   :   @nsfnet-relay.ac.uk:rlh2 at ukc.ac.uk
		  :   rlh2 at ukc.ac.uk    ..!mcsun!ukc!rlh2
---                                               
Computing Lab., University of Kent at Canterbury,
Canterbury, Kent, CT2 7NF, United Kingdom.    Tel: +44 227 764000 ext 3682/7620
SHAR_EOF
fi # end of overwriting check
if test -f 'Imakefile'
then
	echo shar: will not over-write existing file "'Imakefile'"
else
cat << \SHAR_EOF > 'Imakefile'
       INCLUDES = -I$(TOP)
        DEPLIBS = XawClientDepLibs
LOCAL_LIBRARIES = XawClientLibs
           SRCS = xmenu.c
           OBJS = xmenu.o

SimpleProgramTarget(xmenu)
SHAR_EOF
fi # end of overwriting check
if test -f 'patchlevel.h'
then
	echo shar: will not over-write existing file "'patchlevel.h'"
else
cat << \SHAR_EOF > 'patchlevel.h'
#define patchlevel 0
SHAR_EOF
fi # end of overwriting check
if test -f 'xmenu.man'
then
	echo shar: will not over-write existing file "'xmenu.man'"
else
cat << \SHAR_EOF > 'xmenu.man'
.TH XMENU l "19 June 1990"
.SH NAME
xmenu \- provide simple popup menus to shell scripts
.SH SYNOPSIS
\fBxmenu\fP [-heading "menu title"] [menu_entry[=output_value] | line]...
.SH DESCRIPTION
\fIxmenu\fP provides a quick and easy way of producing a single popup
menu for use by shell scripts.  The \fI-heading\fP is used to set the menu's
title, all other arguments are used as items in the menu.  If an argument
contains an equals sign then the string to the left of the equals is used as
the displayed menu item and the the string to the right is used as the text
output when the item is pressed.  The argument "line" is recognised as
denoting a line separating menu items.

By default the menu is popped up to the right and below of the current pointer
position but this can be overridden using the standard toolkit "-geometry". 
.SH EXAMPLES
.DS
xmenu -heading "Choose Files by Suffix" Compressed="*.Z" \\
                       "C sources"="*.c" "Headers"="*.h" \\
                       line line "        Cancel"

xmenu Hello="Hello World" line " " " " line Goodbye="Goodbye World"
.DE
.SH AUTHOR
Richard Hesketh, University of Kent at Canterbury, June 1990
.br
@nsfnet-relay.ac.uk:rlh2 at ukc.ac.uk
SHAR_EOF
fi # end of overwriting check
if test -f 'xmenu.c'
then
	echo shar: will not over-write existing file "'xmenu.c'"
else
cat << \SHAR_EOF > 'xmenu.c'
#ifndef lint
static char rcsid[] = "$Header: xmenu.c,v 1.1 90/06/19 16:59:36 rlh2 Rel $";
#endif !lint

/* 
 * Copyright 1990 Richard Hesketh / rlh2 at ukc.ac.uk
 *                Computing Lab. University of Kent at Canterbury, UK
 *
 * Permission to use, copy, modify and distribute this software and its
 * documentation for any purpose is hereby granted without fee, 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 names of Richard Hesketh and The University of
 * Kent at Canterbury not be used in advertising or publicity pertaining to
 * distribution of the software without specific, written prior permission.
 * Richard Hesketh and The University of Kent at Canterbury make no
 * representations about the suitability of this software for any purpose.
 * It is provided "as is" without express or implied warranty.
 *
 * Richard Hesketh AND THE UNIVERSITY OF KENT AT CANTERBURY DISCLAIMS ALL
 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL Richard Hesketh OR THE
 * UNIVERSITY OF KENT AT CANTERBURY 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.
 *
 * Author:  Richard Hesketh / rlh2 at ukc.ac.uk, 
 *                Computing Lab. University of Kent at Canterbury, UK
 */

/* XMenu - provide a popup menu from a shell script etc.
 *
 * This little toolkit program produces a popup menu on the screen taking
 * the command line arguments as menu entries.  If an argument contains
 * a non-escaped '=' (equals sign) the string to the left is the name
 * displayed in the menu and the string to the right is the text output
 * when this menu button is pressed.
 *
 * A special argument "line" can be used to draw a dividing line between
 * successive menu entries.
 *
 * A menu title can be given as an argument after a "-heading" flag.
 *
 * The menu is only popped down when a menu button is pressed.
 * By default the menu is popped up below and to the right of the current
 * pointer position, use "-geometry" to position the menu at a particular
 * point (and in a particular size). 
 *
 * Examples:
 *
 *	xmenu -heading "Choose Files by Suffix" Compressed="*.Z" \
 *			"C sources"="*.c" "Headers"="*.h" \
 *			line line "        Cancel"
 *
 *	xmenu Hello="Hello World" line " " " " line Goodbye="Goodbye World"
 */

#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/Xaw/SimpleMenu.h>
#include <X11/Xaw/SmeBSB.h>
#include <X11/Xaw/SmeLine.h>

#define LINE (String)1
#define LINE_TOK "line"

static String MTrans = "<EnterWindow>: highlight()\n\
			<LeaveWindow>: unhighlight()\n\
			<MotionNotify>: highlight()\n\
			<BtnUp>: notify() unhighlight()";
static XtTranslations MPTrans = NULL;

static XrmOptionDescRec options[] = {
	{"-heading", ".heading", XrmoptionSepArg, (XtPointer)NULL},
};


static void
button_pressed(w, client_data, call_data)
Widget w;
XtPointer client_data, call_data;
{
	if (client_data != NULL)
		printf("%s\n", client_data);
	exit(0);
}

String
get_prompt(str, out_val)
String str;
String *out_val;
{
	int s, t;
	char name[1000];
	Boolean found_escape = FALSE;

	*out_val = NULL;

	if (strncmp(str, LINE_TOK, strlen(LINE_TOK)) == 0)
		return (LINE);

	for (t = s = 0; str[s] != '\0'; s++) {
		if (str[s] == '\\') {
			if (found_escape) {
				found_escape = FALSE;
				name[t++] = '\\';
			} else
				found_escape = TRUE;
		} else if (str[s] == '=') {
			if (found_escape) {
				found_escape = FALSE;
				name[t++] = '=';
			} else {
				*out_val = str + s + 1;
				break;
			}
		} else
			name[t++] = str[s];
	}
	name[t] = '\0';
	return (XtNewString(name));
}


main(argc, argv)
int argc;
char *argv[];
{
	Widget shell, button;
	Cardinal i;
	Position x, y;
	int int_x, int_y;
	Window junk1;
	int junk2;
	unsigned int junk3;
	Display *dpy;
	String out_val;
	String name, heading;
	char *blank;
	XrmValue value;

	x = y = 400; /* just in case XQueryPointer() fails 8-) */

	XtToolkitInitialize();
	dpy = XtOpenDisplay(XtCreateApplicationContext(), NULL,
					"xmenu", "XMenu",
					options, XtNumber(options),
					&argc, argv);

	if (XQueryPointer(dpy, DefaultRootWindow(dpy), &junk1, &junk1,
	    &int_x, &int_y, &junk2, &junk2, &junk3)) {
		x = int_x;
		y = int_y;
	}

	if (XrmGetResource(dpy->db, "xmenu.heading", "", &blank, &value))
		heading = (char *)value.addr;
	else
		heading = NULL;

	MPTrans = XtParseTranslationTable(MTrans);
	shell = XtVaAppCreateShell("xmenu", "XMenu", simpleMenuWidgetClass,
					dpy, XtNx, x, XtNy, y,
					XtNlabel, heading,
					XtNtranslations, MPTrans,
					NULL);
	if (heading != NULL)
		button = XtVaCreateManagedWidget("menuLine", 
					smeLineObjectClass, shell,
					XtNlineWidth, 3,
					NULL);

	for (i = 1; i < argc; i++) {
		name = get_prompt(argv[i], &out_val);

		if (name == LINE)
			button = XtVaCreateManagedWidget("menuLine",
					smeLineObjectClass, shell,
					NULL);
		else {
			button = XtVaCreateManagedWidget("menuButton",
					smeBSBObjectClass, shell,
					XtNlabel, name,
					NULL);
			XtAddCallback(button, XtNcallback, button_pressed,
					(XtPointer)out_val);
		}
	}

	XtRealizeWidget(shell);
	XtAppMainLoop(XtWidgetToApplicationContext(shell));
}
SHAR_EOF
fi # end of overwriting check
#	End of shell archive
exit 0

dan
----------------------------------------------------
O'Reilly && Associates   argv at sun.com / argv at ora.com
Opinions expressed reflect those of the author only.



More information about the Comp.sources.x mailing list