home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume3 / awm2 / part09 / menu_sup.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-02-20  |  6.2 KB  |  272 lines

  1.  
  2.  
  3.  
  4. #ifndef lint
  5. static char *rcsid_menu_sup_c = "$Header: /usr/graph2/X11.3/contrib/windowmgrs/awm/RCS/menu_sup.c,v 1.3 89/02/07 22:40:04 jkh Exp $";
  6. #endif  lint
  7.  
  8. #include "X11/copyright.h"
  9. /*
  10.  *
  11.  * Copyright 1987, 1988 by Ardent Computer Corporation, Sunnyvale, Ca.
  12.  *
  13.  * Copyright 1987 by Jordan Hubbard.
  14.  *
  15.  *
  16.  *                         All Rights Reserved
  17.  *
  18.  * Permission to use, copy, modify, and distribute this software and its
  19.  * documentation for any purpose and without fee is hereby granted,
  20.  * provided that the above copyright notice appear in all copies and that
  21.  * both that copyright notice and this permission notice appear in
  22.  * supporting documentation, and that the name of Ardent Computer
  23.  * Corporation or Jordan Hubbard not be used in advertising or publicity
  24.  * pertaining to distribution of the software without specific, written
  25.  * prior permission.
  26.  *
  27.  */
  28.  
  29. #include "awm.h"
  30. #include <signal.h>
  31. #
  32.  
  33. /* interface functions for RTL menus */
  34.  
  35. do_nothing()    /* like it says... */
  36. {
  37. }
  38.  
  39. Boolean check_booleans(menu, item)
  40. RTLMenu menu;
  41. RTLMenuItem item;
  42. {
  43.      Boolean *foo;
  44.  
  45.      Entry("check_booleans")
  46.  
  47.      foo = (Boolean *)RTLMenu_Data(menu, item);
  48.      Leave(*foo)
  49. }
  50.  
  51. Boolean toggle_booleans(menu, item)
  52. RTLMenu menu;
  53. RTLMenuItem item;
  54. {
  55.      Boolean *foo;
  56.  
  57.      Entry("togglet_booleans")
  58.  
  59.      foo = (Boolean *)RTLMenu_Data(menu, item);
  60.      *foo = !(*foo);
  61.      Leave(*foo)
  62. }
  63.  
  64. /*ARGSUSED*/
  65. int do_shell(menu, item, window)    /* Do a shell command */
  66. RTLMenu menu;
  67. RTLMenuItem item;
  68. Window window;            /* not used */
  69. {
  70.      char *cmd;
  71.      int status, pid, w;
  72.      void (*istat)(), (*qstat)();
  73.  
  74.      Entry("do_shell")
  75.  
  76.      cmd = (char *)RTLMenu_Data(menu, item);
  77.      if ((pid = fork()) == 0) {
  78.       setpgrp(0, getpid());
  79.           signal(SIGHUP, SIG_DFL);
  80.           signal(SIGQUIT, SIG_DFL);
  81.           signal(SIGINT, SIG_DFL);
  82.       execl("/bin/sh", "sh", "-c", cmd, 0);
  83.       _exit(127);
  84.      }
  85.      istat = (int (*)())signal(SIGINT, SIG_IGN);
  86.      qstat = (int (*)())signal(SIGQUIT, SIG_IGN);
  87.      while ((w = wait(&status)) != pid && w != -1);
  88.      if (w == -1)
  89.       status = -1;
  90.      signal(SIGINT, istat);
  91.      signal(SIGQUIT, qstat);
  92.      Leave(status)
  93. }
  94.  
  95. /*ARGSUSED*/
  96. int do_text(menu, item, window)
  97. RTLMenu menu;
  98. RTLMenuItem item;
  99. Window window;
  100. {
  101.      char *buff;
  102.  
  103.      Entry("do_text")
  104.  
  105.      buff = (char *)RTLMenu_Data(menu, item);
  106.      XStoreBytes(dpy, buff, strlen(buff));
  107.      Leave_void
  108. }
  109.  
  110. /*ARGSUSED*/
  111. int do_text_nl(menu, item, window)
  112. RTLMenu menu;
  113. RTLMenuItem item;
  114. Window window;
  115. {
  116.      char *buff1, *buff2;
  117.  
  118.      Entry("do_text_nl")
  119.  
  120.      buff1 = (char *)RTLMenu_Data(menu, item);
  121.      buff2 = (char *)malloc(strlen(buff1) + 2);
  122.      strcpy(buff2, buff1);
  123.      strcat(buff2, "\n");
  124.      XStoreBytes(dpy, buff2, strlen(buff2));
  125.      free(buff2);
  126.      Leave_void
  127. }
  128.  
  129. int do_awm_func(menu, item, window)
  130. RTLMenu menu;
  131. RTLMenuItem item;
  132. Window window;
  133. {
  134.      int x, y, button;
  135.      Boolean (*func)();
  136.      extern Window Select_Window();
  137.  
  138.      Entry("do_awm_func")
  139.  
  140.      XSync(dpy, FALSE);
  141.      if (window == RootWindow(dpy, scr))
  142.           window = Select_Window(&x, &y, &button);
  143.      func = (Boolean(*)())RTLMenu_Data(menu, item);
  144.      Leave((*func)(window, 0, button, x, y))
  145. }
  146.  
  147. /*ARGSUSED*/
  148. int do_imm_func(menu, item, window)
  149. RTLMenu menu;
  150. RTLMenuItem item;
  151. Window window;
  152. {
  153.      Boolean (*func)();
  154.  
  155.      Entry("do_imm_func")
  156.  
  157.      func = (Boolean(*)())RTLMenu_Data(menu, item);
  158.      Leave((*func)(RootWindow(dpy, scr), 0, 0, 0, 0))
  159. }
  160.  
  161. MenuInfo *FindMenu(s)
  162. register char *s;
  163. {
  164.      MenuLink *ml;
  165.  
  166.      Entry("FindMenu")
  167.  
  168.      for (ml = Menus; ml; ml = ml->next)
  169.       if (!strcmp(s, ml->menu->name))
  170.            Leave(ml->menu)
  171.      Leave((MenuInfo *) 0)
  172. }
  173.  
  174. RTLMenu create_menu(m)
  175. MenuInfo *m;
  176. {
  177.      ActionLine *ln;
  178.      RTLMenuItem tmp;
  179.      MenuInfo *side_menu;
  180.  
  181.      Entry("create_menu")
  182.  
  183.      if (!m)
  184.       yyerror("Internal error, create_menu passed null pointer");
  185.      /*
  186.       * Were we already created? This is possible if we were referenced
  187.       * by somebody created before us.
  188.       */
  189.      if (m->menu)
  190.       Leave(m->menu)
  191.      m->menu = RTLMenu_Create();
  192.      /* make a name (or picture) label for this menu */
  193.      tmp = RTLMenu_Append_Call(m->menu, m->name, m->pixmapname, do_nothing, 0);
  194.      RTLMenu_Label_Entry(m->menu, tmp);
  195.      ln = m->line;
  196.      if (!ln) {
  197.       yyerror("Internal error in create_menu.");
  198.       fprintf(stderr, "Menu '%s' has no line list.\n", m->name);
  199.       exit(1);
  200.      }
  201.      if (!ln->name && !ln->pixmapname) {
  202.       fprintf(stderr, "awm: Action in menu '%s' has no name or backing pixmap\n",
  203.           m->name);
  204.       yyerror(".. aborting\n");
  205.       exit(1);
  206.      }
  207.      while (ln) {
  208.       switch (ln->type) {
  209.       case IsVar:
  210.            ln->item = RTLMenu_Append_Checkback(m->menu, ln->name,
  211.                            ln->pixmapname,
  212.                            check_booleans,
  213.                            toggle_booleans,
  214.                            ln->text);
  215.            break;
  216.            
  217.       case IsImmFunction:
  218.            ln->item = RTLMenu_Append_Call(m->menu, ln->name,
  219.                           ln->pixmapname,
  220.                           do_imm_func, ln->func);
  221.            break;
  222.  
  223.       case IsUwmFunction:
  224.            ln->item = RTLMenu_Append_Call(m->menu, ln->name,
  225.                           ln->pixmapname,
  226.                           do_awm_func, ln->func);
  227.            break;
  228.  
  229.       case IsMenuFunction:
  230.            if (!(side_menu = FindMenu(ln->text))) {
  231.             fprintf(stderr, "Unknown menu \"%s\" referenced in ",
  232.             ln->text);
  233.             yyerror(" ..");
  234.             exit(1);
  235.            }
  236.            /* If we haven't created the referenced menu yet, create it now */
  237.            if (!side_menu->menu)
  238.             side_menu->menu = create_menu(side_menu);
  239.            ln->item = RTLMenu_Append_Submenu(m->menu, ln->name,
  240.                          ln->pixmapname,
  241.                          side_menu->menu);
  242.            break;
  243.            
  244.       case IsText:
  245.            ln->item = RTLMenu_Append_Call(m->menu, ln->name,
  246.                           ln->pixmapname,
  247.                           do_text, ln->text);
  248.            break;
  249.  
  250.       case IsTextNL:
  251.            ln->item = RTLMenu_Append_Call(m->menu, ln->name,
  252.                           ln->pixmapname,
  253.                           do_text_nl, ln->text);
  254.            break;
  255.            
  256.       case IsShellCommand:
  257.            ln->item = RTLMenu_Append_Call(m->menu, ln->name,
  258.                           ln->pixmapname,
  259.                           do_shell, ln->text);
  260.            break;
  261.            
  262.       default:
  263.            fprintf(stderr, "create_menu, Unknown menu entry type %d\n",
  264.           ln->type);
  265.            break;
  266.       }
  267.       free(ln);
  268.       ln = ln->next;
  269.      }
  270.      Leave(m->menu)
  271. }
  272.