home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / alt / sources / 2575 / ebuttons.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-18  |  4.0 KB  |  170 lines

  1. /*
  2.  * X interface to Emacs.
  3.  * Copyright (C) 1992  Terry Jones
  4.  * (Based (heavily) on the taglist facility written by Brad Mears)
  5.  *
  6.  * This file is part of ebuttons
  7.  *
  8.  * Ebuttons is free software; you can redistribute it and/or modify
  9.  * it under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation; either version 1, or (at your option)
  11.  * any later version.
  12.  *
  13.  * The GNU General Public License can be obtained via anonymous ftp from
  14.  * prep.ai.mit.edu as pub/gnu/COPYING or pub/gnu/COPYING-2.
  15.  *
  16.  * Ebuttons is distributed in the hope that it will be useful,
  17.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.  * GNU General Public License for more details.
  20.  */
  21.  
  22. /*
  23.  * This provides a point-and-click command interface to an 
  24.  * emacs session.  This won't be much use without ebuttons.el.
  25.  * See the README for details on use.
  26.  */
  27.  
  28. #include <stdio.h>
  29.  
  30. #include <X11/Intrinsic.h>
  31. #include <X11/StringDefs.h>
  32. #include <X11/Shell.h>
  33.  
  34. #ifdef HAVE_XAW3D
  35. #include <X11/Xaw3d/Box.h>
  36. #include <X11/Xaw3d/Command.h>
  37. #else
  38. #include <X11/Xaw3d/Box.h>
  39. #include <X11/Xaw3d/Command.h>
  40. #endif
  41.  
  42. static void create_buttons();
  43. static void button_press();
  44. void toggle_visibility();
  45.  
  46. typedef struct {
  47.     String labels[MAX_BUTTONS];
  48.     String commands[MAX_BUTTONS];
  49. } AppResources;
  50.  
  51. static AppResources appResources;
  52. static Widget toplevel;            
  53.  
  54. #include "ebuttons.h"
  55.  
  56. int 
  57. main(argc, argv)
  58. int argc;
  59. char **argv;
  60. {
  61.    XtAppContext appContext;
  62.  
  63.    toplevel = XtAppInitialize(&appContext, "ebuttons", NULL, 0, &argc, argv, NULL, NULL, 0);
  64.    XtGetApplicationResources(toplevel, (XtPointer)&appResources, resources, XtNumber(resources), NULL, 0);
  65.    create_buttons(toplevel);
  66.    XtAppAddInput(appContext, fileno(stdin), XtInputReadMask, toggle_visibility, NULL);
  67.    XtRealizeWidget(toplevel);
  68.    XtAppMainLoop(appContext);
  69.    return 0;
  70. }
  71.  
  72.  
  73. /*
  74.  * Callback for the configurable command buttons.  The quit button
  75.  * has no associated command.
  76.  */
  77. static void 
  78. button_press(w, command, dummy)
  79. Widget  w;
  80. XtPointer command; 
  81. XtPointer dummy;
  82. {
  83.    if (!command){
  84.       exit(0);
  85.   }
  86.  
  87.    printf("%s", (String)command);
  88.    fflush(stdout);
  89.    return;
  90. }
  91.  
  92.  
  93. static void 
  94. create_buttons(parent)
  95. Widget parent;
  96. {
  97.     Widget cmds[MAX_BUTTONS];
  98.     Widget cmdbox;              
  99.     Widget quit;
  100.     int i;
  101.     Dimension width;
  102.     Dimension max_width = 0;
  103.     
  104.     cmdbox = XtCreateManagedWidget("cmdbox", boxWidgetClass, parent, NULL, 0);
  105.     
  106.     for (i = 0; i < MAX_BUTTONS; i++) {
  107.     if (appResources.labels[i] && appResources.commands[i]){
  108.         cmds[i] = XtCreateManagedWidget(appResources.labels[i], commandWidgetClass, cmdbox, NULL, 0);
  109.         XtAddCallback(cmds[i], XtNcallback, button_press, (XtPointer)appResources.commands[i]);
  110.     
  111.         XtVaGetValues(cmds[i], XtNwidth, &width, NULL);
  112.         
  113.         if (max_width < width){
  114.         max_width = width;
  115.         }
  116.     }
  117.     }
  118.     
  119.     quit = XtCreateManagedWidget("Quit", commandWidgetClass, cmdbox, NULL, 0);
  120.     XtAddCallback(quit, XtNcallback, button_press, NULL);
  121.     
  122.     XtVaGetValues(quit, XtNwidth, &width, NULL);
  123.     
  124.     if (max_width < width){
  125.     max_width = width;
  126.     }
  127.     
  128.     /* Set all the buttons to the same width */
  129.     for (i = 0; i < MAX_BUTTONS; i++) {
  130.     if (appResources.labels[i] && appResources.commands[i]){
  131.         XtVaSetValues(cmds[i], XtNwidth, max_width, NULL);
  132.     }
  133.     }
  134.     
  135.     XtVaSetValues(quit, XtNwidth, max_width, NULL);
  136.     
  137.     return;
  138. }
  139.  
  140.  
  141. void 
  142. toggle_visibility(client_data, dummy1, dummy2)
  143. XtPointer client_data;
  144. int *dummy1;
  145. XtInputId *dummy2;
  146. {
  147.    char buf[BUFSIZ];
  148.    static int visible = 1;
  149.  
  150.     /* Read input from stdin.  We know it is ready since we have been called. */
  151.    
  152.    if (!fgets(buf, BUFSIZ, stdin)){
  153.        /* This goes to stdout, not stderr. */
  154.        printf("could not read from stdin!\n");
  155.        exit(0);
  156.    }
  157.  
  158.    if (visible) {
  159.        XtUnmapWidget(toplevel);
  160.        visible = 0;
  161.    }
  162.    else {
  163.        XtMapWidget(toplevel);
  164.        visible = 1;
  165.    }
  166.    
  167.    return;
  168. }
  169.  
  170.