summaryrefslogtreecommitdiff
path: root/clients/xzwrite/GetString.c
blob: 3195c264430ec89044c7f264b9209ea6f9950028 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/Shell.h>
#include <X11/Xaw/Form.h>
#include <X11/Xaw/Label.h>
#include <X11/Xaw/AsciiText.h>
#include <X11/Xaw/Command.h>

#include "GetString.h"

#define XVCMW XtVaCreateManagedWidget

static int accepted, cancelled;
static void Accept(), Cancel(), Focus();
static int HierEvent();

extern void Popup();

static XtActionsRec actionTable[] = {
     {"Accept", (XtActionProc) Accept},
     {"Cancel", (XtActionProc) Cancel},
     {"Focus", (XtActionProc) Focus},
};
     
Widget InitGetString(parent, name)
   Widget parent;
   char *name;
{
     static int first_time = 1;
     Widget getStringWindow, form, title, edit, accept, cancel;

     if (first_time) {
	  XtAppAddActions(XtWidgetToApplicationContext(parent), actionTable,
			  XtNumber(actionTable));
	  first_time = 0;
     };

     getStringWindow = XtVaCreatePopupShell(name, transientShellWidgetClass,
					    parent,
					    XtNinput, True,
					    NULL);
     form = XVCMW("getStringForm", formWidgetClass, getStringWindow, NULL);
     title = XVCMW("getStringTitle", labelWidgetClass, form, NULL);
     edit = XVCMW("getStringEdit", asciiTextWidgetClass, form, NULL);
     accept = XVCMW("getStringAccept", commandWidgetClass, form, NULL);
     cancel = XVCMW("getStringCancel", commandWidgetClass, form, NULL);
     XtSetKeyboardFocus(form, edit);

     return getStringWindow;
}

int GetString(getStringWindow, label, value, pop_type, buf, len)
   Widget getStringWindow;
   String label, value;
   int pop_type;
   char *buf;
   int len;
{
     XtAppContext app_con;
     Widget title, edit;
     XEvent event;

     app_con = XtWidgetToApplicationContext(getStringWindow);
     title = XtNameToWidget(getStringWindow, "getStringForm.getStringTitle");
     edit = XtNameToWidget(getStringWindow, "getStringForm.getStringEdit");

     XtVaSetValues(title, XtNlabel, label, NULL);
     XtVaSetValues(edit, XtNstring, value, NULL);

     XtRealizeWidget(getStringWindow);
     Popup(getStringWindow, XtGrabExclusive, pop_type);

     accepted = cancelled = 0;
     while (! accepted && ! cancelled) {
	  XtAppNextEvent(app_con, &event);
	  XtDispatchEvent(&event);
     }

     XtPopdown(getStringWindow);

     if (accepted) {
	  char *s;
	  Widget text_source;

	  XtVaGetValues(edit, XtNstring, (XtArgVal) &s, XtNtextSource,
			(XtArgVal) &text_source, NULL);
	  strncpy(buf, s, len-2);
	  buf[len-1] = '\0';
	  XawAsciiSourceFreeString(text_source);
	  
	  return GETSTRING_ACCEPT;
     }
     else
	  return GETSTRING_CANCEL;
}

/*
 * I thought I needed this routine becaues XtAppNextEvent was
 * returning events for widgets that should have been blocked by the
 * XtGrabExclusive.. but it turns out that XtDispatch deals with that
 * (correctly) so this code is useless.
 */
static int HierEvent(w, event)
   Widget w;
   XAnyEvent *event;
{
     Widget event_w;

     event_w = XtWindowToWidget(XtDisplay(w), event->window);

     while (event_w = XtParent(event_w)) {
	  if (w == event_w)
	       return 1;
	  else if (XtIsShell(event_w))
	       break;
     }

     return 0;
}

/* ARGSUSED */
static void Accept(w, e, p, n)
   Widget w;
   XEvent *e;
   String *p;
   Cardinal *n;
{
     accepted = 1;
}

/* ARGSUSED */
static void Cancel(w, e, p, n)
   Widget w;
   XEvent *e;
   String *p;
   Cardinal *n;
{
     cancelled = 1;
}

/* ARGSUSED */
static void Focus(w, e, p, n)
   Widget w;
   XEvent *e;
   String *p;
   Cardinal *n;
{
     XSetInputFocus(XtDisplay(w), XtWindow(w), RevertToPointerRoot,
		    CurrentTime);
}