home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / XAP / XGAMES / XMENU.TAR / xmenu / xmenu.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-11-01  |  6.0 KB  |  219 lines

  1. #ifndef lint
  2. static char rcsid[] = "$Header: /usr/nfs/maple/d0/users/rlh2/toolkit/xmenu/RCS/xmenu.c,v 1.3 90/06/29 17:00:22 rlh2 Rel Locker: rlh2 $";
  3. #endif !lint
  4.  
  5. /* 
  6.  * Copyright 1990 Richard Hesketh / rlh2@ukc.ac.uk
  7.  *                Computing Lab. University of Kent at Canterbury, UK
  8.  *
  9.  * Permission to use, copy, modify and distribute this software and its
  10.  * documentation for any purpose is hereby granted without fee, provided that
  11.  * the above copyright notice appear in all copies and that both that
  12.  * copyright notice and this permission notice appear in supporting
  13.  * documentation, and that the names of Richard Hesketh and The University of
  14.  * Kent at Canterbury not be used in advertising or publicity pertaining to
  15.  * distribution of the software without specific, written prior permission.
  16.  * Richard Hesketh and The University of Kent at Canterbury make no
  17.  * representations about the suitability of this software for any purpose.
  18.  * It is provided "as is" without express or implied warranty.
  19.  *
  20.  * Richard Hesketh AND THE UNIVERSITY OF KENT AT CANTERBURY DISCLAIMS ALL
  21.  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
  22.  * OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL Richard Hesketh OR THE
  23.  * UNIVERSITY OF KENT AT CANTERBURY BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  24.  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  25.  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  26.  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  27.  * OF THIS SOFTWARE.
  28.  *
  29.  * Author:  Richard Hesketh / rlh2@ukc.ac.uk, 
  30.  *                Computing Lab. University of Kent at Canterbury, UK
  31.  */
  32.  
  33. /* XMenu - provide a popup menu from a shell script etc.
  34.  *
  35.  * This little toolkit program produces a popup menu on the screen taking
  36.  * the command line arguments as menu entries.  If an argument contains
  37.  * a non-escaped '=' (equals sign) the string to the left is the name
  38.  * displayed in the menu and the string to the right is the text output
  39.  * when this menu button is pressed.
  40.  *
  41.  * A special argument "-line" can be used to draw a dividing line between
  42.  * successive menu entries.
  43.  *
  44.  * A menu title can be given as an argument after a "-heading" flag.
  45.  *
  46.  * The menu is only popped down when a menu button is pressed.
  47.  * By default the menu is popped up below and to the right of the current
  48.  * pointer position, use "-geometry" to position the menu at a particular
  49.  * point (and in a particular size). 
  50.  *
  51.  * Examples:
  52.  *
  53.  *    xmenu -heading "Choose Files by Suffix" Compressed="*.Z" \
  54.  *            "C sources"="*.c" "Headers"="*.h" \
  55.  *            -line -line "        Cancel"
  56.  *
  57.  *    xmenu Hello="Hello World" -line "" "" -line Goodbye="Goodbye World"
  58.  */
  59.  
  60. #include <stdio.h>
  61. #include <X11/Intrinsic.h>
  62. #include <X11/StringDefs.h>
  63. #include <X11/Xaw/SimpleMenu.h>
  64. #include <X11/Xaw/SmeBSB.h>
  65. #include <X11/Xaw/SmeLine.h>
  66.  
  67. #define LINE (String)1
  68. #define LINE_TOK "-line"
  69. #define STATUS_START 101
  70.  
  71. struct ret {
  72.     String out_val;
  73.     Cardinal exit_status;
  74. };
  75.  
  76. static String MTrans = "<EnterWindow>: highlight()\n\
  77.             <LeaveWindow>: unhighlight()\n\
  78.             <MotionNotify>: highlight()\n\
  79.             <BtnUp>: notify() unhighlight()";
  80. static XtTranslations MPTrans = NULL;
  81.  
  82. static XrmOptionDescRec options[] = {
  83.     {"-heading", ".heading", XrmoptionSepArg, (XtPointer)NULL},
  84. };
  85.  
  86.  
  87. static void
  88. button_pressed(w, client_data, call_data)
  89. Widget w;
  90. XtPointer client_data, call_data;
  91. {
  92.     struct ret *values = (struct ret *)client_data;
  93.  
  94.     if (values->out_val != NULL)
  95.         fprintf(stdout, "%s\n", values->out_val);
  96.     exit(values->exit_status);
  97. }
  98.  
  99.  
  100. String
  101. get_prompt(str, values)
  102. String str;
  103. struct ret **values;
  104. {
  105.     static Cardinal count = STATUS_START;
  106.     int s, t;
  107.     char name[1000];
  108.     Boolean found_escape = FALSE;
  109.  
  110.     *values = NULL;
  111.  
  112.     if (strncmp(str, LINE_TOK, strlen(LINE_TOK)) == 0)
  113.         return (LINE);
  114.  
  115.     *values = XtNew(struct ret);
  116.     (*values)->out_val = NULL;
  117.  
  118.     for (t = s = 0; str[s] != '\0'; s++) {
  119.         if (str[s] == '\\') {
  120.             if (found_escape) {
  121.                 found_escape = FALSE;
  122.                 name[t++] = '\\';
  123.             } else
  124.                 found_escape = TRUE;
  125.         } else if (str[s] == '=') {
  126.             if (found_escape) {
  127.                 found_escape = FALSE;
  128.                 name[t++] = '=';
  129.             } else {
  130.                 (*values)->out_val = str + s + 1;
  131.                 break;
  132.             }
  133.         } else
  134.             name[t++] = str[s];
  135.     }
  136.     name[t] = '\0';
  137.     if ((*values)->out_val == NULL)
  138.         (*values)->out_val = str;
  139.  
  140.     (*values)->exit_status = count++;
  141.     return (XtNewString(name));
  142. }
  143.  
  144.  
  145. main(argc, argv)
  146. int argc;
  147. char *argv[];
  148. {
  149.     Widget shell, button;
  150.     Cardinal i;
  151.     Position x, y;
  152.     int int_x, int_y;
  153.     Window junk1;
  154.     int junk2;
  155.     unsigned int junk3;
  156.     Display *dpy;
  157.     struct ret *output_values;
  158.     String name, heading;
  159.     char *blank;
  160.     XrmValue value;
  161.     Boolean used_callback = FALSE;
  162.  
  163.     x = y = 400; /* just in case XQueryPointer() fails 8-) */
  164.  
  165.     XtToolkitInitialize();
  166.     dpy = XtOpenDisplay(XtCreateApplicationContext(), NULL,
  167.                     "xmenu", "XMenu",
  168.                     options, XtNumber(options),
  169.                     &argc, argv);
  170.  
  171.     if (XQueryPointer(dpy, DefaultRootWindow(dpy), &junk1, &junk1,
  172.         &int_x, &int_y, &junk2, &junk2, &junk3)) {
  173.         x = int_x;
  174.         y = int_y;
  175.     }
  176.  
  177.     if (XrmGetResource(dpy->db, "xmenu.heading", "", &blank, &value))
  178.         heading = (char *)value.addr;
  179.     else
  180.         heading = NULL;
  181.  
  182.     MPTrans = XtParseTranslationTable(MTrans);
  183.     shell = XtVaAppCreateShell("xmenu", "XMenu", simpleMenuWidgetClass,
  184.                     dpy, XtNx, x, XtNy, y,
  185.                     XtNlabel, heading,
  186.                     XtNtranslations, MPTrans,
  187.                     NULL);
  188.     if (heading != NULL)
  189.         button = XtVaCreateManagedWidget("menuLine", 
  190.                     smeLineObjectClass, shell,
  191.                     XtNlineWidth, 2,
  192.                     NULL);
  193.  
  194.     for (i = 1; i < argc; i++) {
  195.         name = get_prompt(argv[i], &output_values);
  196.  
  197.         if (name == LINE)
  198.             button = XtVaCreateManagedWidget("menuLine",
  199.                     smeLineObjectClass, shell,
  200.                     NULL);
  201.         else {
  202.             button = XtVaCreateManagedWidget("menuButton",
  203.                     smeBSBObjectClass, shell,
  204.                     XtNlabel, name,
  205.                     NULL);
  206.             XtAddCallback(button, XtNcallback, button_pressed,
  207.                     (XtPointer)output_values);
  208.             used_callback = TRUE;
  209.         }
  210.     }
  211.     if (!used_callback) {
  212.         fprintf(stderr, "xmenu: no menu items given.\n");
  213.         exit(1);
  214.     }
  215.  
  216.     XtRealizeWidget(shell);
  217.     XtAppMainLoop(XtWidgetToApplicationContext(shell));
  218. }
  219.