home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / demos / xgc / getfile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-01-06  |  4.5 KB  |  159 lines

  1. /*
  2. ** getfilename.c
  3. **
  4. */
  5.  
  6. #include <X11/Intrinsic.h>
  7. #include <X11/StringDefs.h>
  8. #include <X11/Xaw/Label.h>
  9. #include <X11/Xaw/Command.h>
  10. #include <X11/Xaw/Form.h>
  11. #include <X11/Shell.h>
  12. #include <X11/Xaw/AsciiText.h>
  13. #include <stdio.h>
  14.  
  15. #include "xgc.h"
  16.  
  17. extern XStuff X;
  18. extern Widget topform;
  19.  
  20. static Widget popupshell = NULL;    /* popup dialog box */
  21. Widget filename_text_widget;    /* Widget containing the name of
  22.                    the file the user has selected */
  23. extern XtAppContext appcontext;
  24.  
  25. static void kill_popup_shell();
  26.  
  27. void
  28. get_filename(success,failure) 
  29.      void (*success)();        /* what function to call when a filename is
  30.                    chosen */
  31.      void (*failure)();        /* what function to call when the user
  32.                    cancels */
  33. {
  34.   static Widget popupform;    /* form inside shell */
  35.   static Widget label;        /* "Filename :" */
  36.   static Widget cancel;        /* command, select to cancel */
  37.  
  38.   Window dummy1, dummy2;
  39.   int x1,y1,x2,y2;
  40.   unsigned int mask;
  41.  
  42.   /* The translation table for the text widget.  Things such as <RETURN>
  43.   ** confirm the user's choice.  Other keys which would move out of
  44.   ** the range of a one-line window are disabled. */
  45.  
  46.   static char *translationtable = 
  47.     "Ctrl<Key>J:    KillPopup() Done()\n\
  48.      Ctrl<Key>M:    KillPopup() Done()\n\
  49.      <Key>Linefeed: KillPopup() Done()\n\
  50.      <Key>Return:   KillPopup() Done()\n\
  51.      Ctrl<Key>O:    Nothing()\n\
  52.      Meta<Key>I:    Nothing()\n\
  53.      Ctrl<Key>N:    Nothing()\n\
  54.      Ctrl<Key>P:    Nothing()\n\
  55.      Ctrl<Key>Z:    Nothing()\n\
  56.      Meta<Key>Z:    Nothing()\n\
  57.      Ctrl<Key>V:    Nothing()\n\
  58.      Meta<Key>V:    Nothing()";
  59.  
  60.   /* What the actions in the translation table correspond to. */
  61.  
  62.   static XtActionsRec actiontable[] = {
  63.     {"KillPopup", (XtActionProc) kill_popup_shell},
  64.     {"Done",      NULL},
  65.     {"Nothing",   NULL}
  66.   };
  67.  
  68.   static Arg popupshellargs[] = {     /* Where to put the popup shell. */
  69.     {XtNx,         (XtArgVal) NULL},
  70.     {XtNy,         (XtArgVal) NULL}
  71.   };
  72.  
  73.   static Arg labelargs[] = {    /* ArgList for the label */
  74.     {XtNborderWidth,    (XtArgVal) 0},
  75.     {XtNjustify,        (XtArgVal) XtJustifyRight}
  76.   };
  77.  
  78.   static Arg textargs[] = {    /* ArgList for the text widget */
  79.     {XtNeditType,       (XtArgVal) XawtextEdit},
  80.     {XtNwidth,          (XtArgVal) 200},
  81.     {XtNhorizDistance,  (XtArgVal) 10},
  82.     {XtNfromHoriz,      (XtArgVal) NULL},
  83.   };
  84.  
  85.   static Arg cancelargs[] = {    /* ArgList for the cancel button */
  86.     {XtNfromHoriz,      (XtArgVal) NULL},
  87.     {XtNcallback,       (XtArgVal) NULL}
  88.   };
  89.  
  90.   /* Procedures to call when the user selects 'cancel' */
  91.   static XtCallbackRec cancelcallbacklist[] = {
  92.     {(XtCallbackProc) kill_popup_shell, NULL},
  93.     {NULL, NULL},
  94.     {NULL, NULL}
  95.   };
  96.  
  97.   if (popupshell != NULL) {
  98.       XtPopup(popupshell,XtGrabExclusive);
  99.       return;
  100.   }
  101.  
  102.   /* Find out where the pointer is, so we can put the popup window there */
  103.  
  104.   (void) XQueryPointer(X.dpy,XtWindow(topform),&dummy1,&dummy2,&x1,&y1,
  105.                &x2,&y2,&mask);
  106.   
  107.   popupshellargs[0].value = (XtArgVal) x2;
  108.   popupshellargs[1].value = (XtArgVal) y2;
  109.   
  110.   popupshell = XtCreatePopupShell("popup",overrideShellWidgetClass,
  111.              topform,popupshellargs,XtNumber(popupshellargs));
  112.  
  113.   popupform = XtCreateManagedWidget("form",formWidgetClass,popupshell,
  114.                 NULL, 0);
  115.  
  116.   label = XtCreateManagedWidget("Filename: ",labelWidgetClass,popupform,
  117.                    labelargs,XtNumber(labelargs));
  118.  
  119.   textargs[3].value = (XtArgVal) label;
  120.  
  121.   filename_text_widget = XtCreateManagedWidget("text",asciiTextWidgetClass,
  122.                            popupform,
  123.                            textargs,XtNumber(textargs));
  124.  
  125.   /* Complete the action table.  We have to do it here because success
  126.   ** isn't known at compile time. */
  127.  
  128.   actiontable[1].proc = (XtActionProc) success;
  129.  
  130.   /* Register actions, translations, callbacks */
  131.  
  132.   XtAppAddActions(appcontext,actiontable,XtNumber(actiontable));
  133.   XtOverrideTranslations(filename_text_widget,
  134.              XtParseTranslationTable(translationtable));
  135.   cancelcallbacklist[1].callback = (XtCallbackProc) failure;
  136.  
  137.   cancelargs[0].value = (XtArgVal) filename_text_widget;
  138.   cancelargs[1].value = (XtArgVal) cancelcallbacklist;
  139.  
  140.   cancel = XtCreateManagedWidget("Cancel",commandWidgetClass,popupform,
  141.                  cancelargs,XtNumber(cancelargs));
  142.  
  143.   /* Bring up the popup.  When the user presses cancel or the return key,
  144.   ** the function kill_popup_shell (below) will be called to remove it. */
  145.  
  146.   XtPopup(popupshell,XtGrabExclusive);
  147. }
  148.  
  149. /* kill_popup_shell()
  150. ** ------------------
  151. ** Remove the popup window that get_filename popped up.
  152. */
  153.  
  154. static void
  155. kill_popup_shell()
  156. {
  157.   XtPopdown(popupshell);
  158. }
  159.