]> asedeno.scripts.mit.edu Git - 1ts-debian.git/blob - zephyr/clients/xzwrite/GetString.c
finalize -3
[1ts-debian.git] / zephyr / clients / xzwrite / GetString.c
1 #include <X11/Intrinsic.h>
2 #include <X11/StringDefs.h>
3 #include <X11/Shell.h>
4 #include <X11/Xaw/Form.h>
5 #include <X11/Xaw/Label.h>
6 #include <X11/Xaw/AsciiText.h>
7 #include <X11/Xaw/Command.h>
8
9 #include "GetString.h"
10
11 #define XVCMW XtVaCreateManagedWidget
12
13 static int accepted, cancelled;
14 static void Accept(), Cancel(), Focus();
15
16 extern void Popup();
17
18 static XtActionsRec actionTable[] = {
19      {"Accept", (XtActionProc) Accept},
20      {"Cancel", (XtActionProc) Cancel},
21      {"Focus", (XtActionProc) Focus},
22 };
23      
24 Widget InitGetString(parent, name)
25    Widget parent;
26    char *name;
27 {
28      static int first_time = 1;
29      Widget getStringWindow, form, title, edit, accept, cancel;
30
31      if (first_time) {
32           XtAppAddActions(XtWidgetToApplicationContext(parent), actionTable,
33                           XtNumber(actionTable));
34           first_time = 0;
35      };
36
37      getStringWindow = XtVaCreatePopupShell(name, transientShellWidgetClass,
38                                             parent,
39                                             XtNinput, True,
40                                             NULL);
41      form = XVCMW("getStringForm", formWidgetClass, getStringWindow, NULL);
42      title = XVCMW("getStringTitle", labelWidgetClass, form, NULL);
43      edit = XVCMW("getStringEdit", asciiTextWidgetClass, form, NULL);
44      accept = XVCMW("getStringAccept", commandWidgetClass, form, NULL);
45      cancel = XVCMW("getStringCancel", commandWidgetClass, form, NULL);
46      XtSetKeyboardFocus(form, edit);
47
48      return getStringWindow;
49 }
50
51 int GetString(getStringWindow, label, value, pop_type, buf, len)
52    Widget getStringWindow;
53    String label, value;
54    int pop_type;
55    char *buf;
56    int len;
57 {
58      XtAppContext app_con;
59      Widget title, edit;
60      XEvent event;
61
62      app_con = XtWidgetToApplicationContext(getStringWindow);
63      title = XtNameToWidget(getStringWindow, "getStringForm.getStringTitle");
64      edit = XtNameToWidget(getStringWindow, "getStringForm.getStringEdit");
65
66      XtVaSetValues(title, XtNlabel, label, NULL);
67      XtVaSetValues(edit, XtNstring, value, NULL);
68
69      XtRealizeWidget(getStringWindow);
70      Popup(getStringWindow, XtGrabExclusive, pop_type);
71
72      accepted = cancelled = 0;
73      while (! accepted && ! cancelled) {
74           XtAppNextEvent(app_con, &event);
75           XtDispatchEvent(&event);
76      }
77
78      XtPopdown(getStringWindow);
79
80      if (accepted) {
81           char *s;
82           Widget text_source;
83
84           XtVaGetValues(edit, XtNstring, (XtArgVal) &s, XtNtextSource,
85                         (XtArgVal) &text_source, NULL);
86           strncpy(buf, s, len-2);
87           buf[len-1] = '\0';
88           XawAsciiSourceFreeString(text_source);
89           
90           return GETSTRING_ACCEPT;
91      }
92      else
93           return GETSTRING_CANCEL;
94 }
95
96 /* ARGSUSED */
97 static void Accept(w, e, p, n)
98    Widget w;
99    XEvent *e;
100    String *p;
101    Cardinal *n;
102 {
103      accepted = 1;
104 }
105
106 /* ARGSUSED */
107 static void Cancel(w, e, p, n)
108    Widget w;
109    XEvent *e;
110    String *p;
111    Cardinal *n;
112 {
113      cancelled = 1;
114 }
115
116 /* ARGSUSED */
117 static void Focus(w, e, p, n)
118    Widget w;
119    XEvent *e;
120    String *p;
121    Cardinal *n;
122 {
123      XSetInputFocus(XtDisplay(w), XtWindow(w), RevertToPointerRoot,
124                     CurrentTime);
125 }