Jspice3
x11util.c
Go to the documentation of this file.
1 /***************************************************************************
2 JSPICE3 adaptation of Spice3f2 - Copyright (c) Stephen R. Whiteley 1992
3 Copyright 1990 Regents of the University of California. All rights reserved.
4 Author: 1995 Stephen R. Whiteley
5 ****************************************************************************/
6 
7 #include "spice.h"
8 #include <stdio.h>
9 #include <ctype.h>
10 #include <unistd.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 
14 #ifdef HAVE_X11
15 #include <X11/Intrinsic.h>
16 #include <X11/StringDefs.h>
17 #include <X11/Xaw/AsciiText.h>
18 #include <X11/Xaw/Paned.h>
19 #include <X11/Xaw/Label.h>
20 #include <X11/Xaw/Viewport.h>
21 #include <X11/Xaw/Command.h>
22 #include <X11/Xaw/Box.h>
23 #include <X11/Shell.h>
24 
25 #include "x11util.h"
26 
27 #ifdef __STDC__
28 static void ToTop(Widget, XtPointer, XEvent*, Boolean*);
29 #else
30 static void ToTop();
31 #endif
32 
33 static char *oneLineTranslations =
34  "<Key>Return: cr_action()\n<Key>Delete: delete-next-character()";
35 static char *butTranslations = "<Btn1Up>: notify() reset()";
36 static char *wmTranslations = "<Message>WM_PROTOCOLS: quit_action()";
37 
38 
39 void
40 PopUpInput(initial_str, action_str, action_callback, w)
41 
42 String initial_str, action_str;
43 void (*action_callback)();
44 widget_bag *w;
45 {
46  Widget form;
47  Widget label;
48  Widget cancel;
49  Widget action;
50  Dimension width, b_width, d;
51  char **p;
52 
53  w->popup = XtVaCreatePopupShell("popup", transientShellWidgetClass,
54  w->shell, NULL);
55  XtOverrideTranslations(w->popup, XtParseTranslationTable(wmTranslations));
56  XtAddEventHandler(w->popup, VisibilityChangeMask,
57  False, ToTop, (XtPointer)w);
58 
59  form = XtVaCreateManagedWidget("form", formWidgetClass, w->popup,
60  NULL);
61 
62  label = XtVaCreateManagedWidget("label", labelWidgetClass,
63  form,
64  XtNlabel, "Enter filename: ",
65  XtNleft, XtChainLeft,
66  XtNright, XtChainLeft,
67  XtNresizable, TRUE,
68  XtNborderWidth, 0,
69  NULL);
70 
71  w->popup_text = XtVaCreateManagedWidget("text", asciiTextWidgetClass,
72  form,
73  XtNfromVert, label,
74  XtNleft, XtChainLeft,
75  XtNright, XtChainRight,
76  XtNeditType, XawtextEdit,
77  XtNresizable, TRUE,
78  XtNresize, XawtextResizeWidth,
79  XtNstring, initial_str,
80  NULL);
81  XtOverrideTranslations(w->popup_text,
82  XtParseTranslationTable(oneLineTranslations));
83 
84  action = XtVaCreateManagedWidget("action", commandWidgetClass,
85  form,
86  XtNlabel, action_str,
87  XtNfromVert, w->popup_text,
88  XtNleft, XtChainLeft,
89  XtNright, XtChainLeft,
90  NULL);
91  XtAddCallback(action, XtNcallback, action_callback, (XtPointer)w);
92  XtOverrideTranslations(action, XtParseTranslationTable(butTranslations));
93 
94  cancel = XtVaCreateManagedWidget("cancel", commandWidgetClass,
95  form,
96  XtNlabel, "Cancel",
97  XtNfromVert, w->popup_text,
98  XtNfromHoriz, action,
99  XtNleft, XtChainLeft,
100  XtNright, XtChainLeft,
101  NULL);
102  XtAddCallback(cancel, XtNcallback, PopDownInput, (XtPointer)w);
103 
104  XtRealizeWidget(w->popup);
105  XSetWMProtocols(Xdisplay, XtWindow(w->popup), &w->wm_delete, 1);
107 
108  XtVaGetValues(form,
109  XtNwidth, &width,
110  XtNborderWidth, &b_width,
111  XtNdefaultDistance, &d,
112  NULL);
113  width -= 2*(b_width + d + 1);
114  XtVaSetValues(w->popup_text, XtNwidth, width, NULL);
115 
116  XtPopup(w->popup, XtGrabNone);
117  XtSetKeyboardFocus(w->popup, w->popup_text);
118  XtSetKeyboardFocus(w->shell, w->popup);
119  for (p = w->popup_sens_list; p && *p; p++)
120  XtSetSensitive(XtNameToWidget(w->butbox, *p), False);
121 }
122 
123 
124 /* ARGSUSED */
125 void
126 PopDownInput(caller, client_data, call_data)
127 
128 Widget caller;
129 XtPointer client_data, call_data;
130 {
131  widget_bag *w = (widget_bag*)client_data;
132  char **p;
133 
134  XtSetKeyboardFocus(w->shell, None);
135  XtPopdown(w->popup);
136  for (p = w->popup_sens_list; p && *p; p++)
137  XtSetSensitive(XtNameToWidget(w->butbox, *p), True);
138  w->popup = NULL;
139 }
140 
141 
142 void
143 PopUpMessage(message_str, w)
144 
145 String message_str;
146 widget_bag *w;
147 {
148  Widget popup;
149  Widget form;
150  Widget label;
151  Widget cancel;
152  XColor visualcolor, exactcolor;
153 
154  popup = XtVaCreatePopupShell("popup_m", transientShellWidgetClass,
155  w->shell, NULL);
156  XtOverrideTranslations(popup, XtParseTranslationTable(wmTranslations));
157  XtAddEventHandler(popup, VisibilityChangeMask,
158  False, ToTop, (XtPointer)w);
159 
160  form = XtVaCreateManagedWidget("form_m", formWidgetClass, popup,
161  NULL);
162 
163  cancel = XtVaCreateManagedWidget("cancel_m", commandWidgetClass,
164  form,
165  XtNlabel, "OK",
166  XtNleft, XtChainLeft,
167  XtNright, XtChainLeft,
168  NULL);
169 
170  XAllocNamedColor(Xdisplay,
171  DefaultColormap(Xdisplay, DefaultScreen(Xdisplay)),
172  "red", &exactcolor, &visualcolor);
173  label = XtVaCreateManagedWidget("label_m", labelWidgetClass,
174  form,
175  XtNlabel, message_str,
176  XtNleft, XtChainLeft,
177  XtNright, XtChainLeft,
178  XtNfromHoriz, cancel,
179  XtNresizable, TRUE,
180  XtNborderWidth, 2,
181  XtNborderColor, visualcolor.pixel,
182  NULL);
183 
184  w->message = popup;
185  XtAddCallback(cancel, XtNcallback, PopDownMessage, (XtPointer)w);
186  XtAddEventHandler(form, KeyPressMask, False,
187  (XtEventHandler)PopDownMessage, (XtPointer)w);
188  XtRealizeWidget(popup);
189  XSetWMProtocols(Xdisplay, XtWindow(popup), &w->wm_delete, 1);
190  CenterWidgetOnWidget(popup, w->shell);
191  if (w->popup)
192  XtSetSensitive(w->popup, False);
193  XtSetSensitive(w->form, False);
194  XtSetKeyboardFocus(w->shell, form);
195  XtPopup(popup, XtGrabExclusive);
196 }
197 
198 
199 /* ARGSUSED */
200 void
201 PopDownMessage(caller, client_data, call_data)
202 
203 Widget caller;
204 XtPointer client_data, call_data;
205 {
206  widget_bag *w = (widget_bag*)client_data;
207 
208  if (w->popup) {
209  XtSetKeyboardFocus(w->shell, w->popup);
210  XtPopdown(w->message);
211  XtSetSensitive(w->popup, True);
212  }
213  else {
214  XtSetKeyboardFocus(w->shell, None);
215  XtPopdown(w->message);
216  }
217  XtSetSensitive(w->form, True);
218 }
219 
220 
221 void
222 PopUpErr(message_str, w)
223 
224 String message_str;
225 widget_bag *w;
226 {
227  Widget popup;
228  Widget form;
229  Widget label;
230  Widget cancel;
231  XColor visualcolor, exactcolor;
232 
233  popup = XtVaCreatePopupShell("popup_e", transientShellWidgetClass,
234  w->shell, NULL);
235  XtOverrideTranslations(popup, XtParseTranslationTable(wmTranslations));
236  XtAddEventHandler(popup, VisibilityChangeMask,
237  False, ToTop, (XtPointer)w);
238 
239  form = XtVaCreateManagedWidget("form_e", formWidgetClass, popup,
240  NULL);
241 
242  cancel = XtVaCreateManagedWidget("cancel_e", commandWidgetClass,
243  form,
244  XtNlabel, "OK",
245  XtNleft, XtChainLeft,
246  XtNright, XtChainLeft,
247  NULL);
248 
249  XAllocNamedColor(Xdisplay,
250  DefaultColormap(Xdisplay, DefaultScreen(Xdisplay)),
251  "red", &exactcolor, &visualcolor);
252  label = XtVaCreateManagedWidget("text_e", asciiTextWidgetClass,
253  form,
254  XtNstring, message_str,
255  XtNleft, XtChainLeft,
256  XtNright, XtChainRight,
257  XtNbottom, XtChainBottom,
258  XtNscrollHorizontal, XawtextScrollWhenNeeded,
259  XtNscrollVertical, XawtextScrollWhenNeeded,
260  XtNwidth, 500,
261  XtNheight, 200,
262  XtNfromHoriz, cancel,
263  XtNdisplayCaret, False,
264  XtNborderWidth, 2,
265  XtNborderColor, visualcolor.pixel,
266  NULL);
267 
268  XtAddCallback(cancel, XtNcallback, PopDownErr, (XtPointer)popup);
269  XtAddEventHandler(form, KeyPressMask, False, (XtEventHandler)PopDownErr,
270  (XtPointer)popup);
271  XtRealizeWidget(popup);
272  XSetWMProtocols(Xdisplay, XtWindow(popup), &w->wm_delete, 1);
273  CenterWidgetOnWidget(popup, w->shell);
274  XtPopup(popup, XtGrabNone);
275 }
276 
277 
278 /* ARGSUSED */
279 void
280 PopDownErr(caller, client_data, call_data)
281 
282 Widget caller;
283 XtPointer client_data, call_data;
284 {
285  XtPopdown((Widget)client_data);
286 }
287 
288 
289 void
290 CenterWidgetOnWidget(sub, master)
291 
292 Widget sub, master;
293 {
294  Dimension width, height, b_width;
295  Position x, y, max_x, max_y;
296 
297  XtVaGetValues(master,
298  XtNx, &x,
299  XtNy, &y,
300  XtNwidth, &width,
301  XtNheight, &height,
302  XtNborderWidth, &b_width,
303  NULL);
304 
305  width += 2*b_width;
306  height += 2*b_width;
307  height += 2*b_width;
308  x += (Position)width/2;
309  y += (Position)height/2;
310 
311  XtVaGetValues(sub,
312  XtNwidth, &width,
313  XtNheight, &height,
314  XtNborderWidth, &b_width,
315  NULL);
316 
317  width += 2*b_width;
318  height += 2*b_width;
319  x -= (Position)width/2;
320  if (x < 0) x = 0;
321  y -= (Position)height/2;
322  if (y < 0) y = 0;
323  if (y > (max_y = (Position) (XtScreen(sub)->height - height))) y = max_y;
324 
325  XtVaSetValues(sub, XtNx, x, XtNy, y, NULL);
326 }
327 
328 
330 CheckFile(fname, mode, w)
331 
332 char *fname;
333 int mode;
334 widget_bag *w;
335 {
336  char buf[512];
337  struct stat st;
338  FILE *fp;
339  char *msg = "Error: can't %s file %s";
340 
341  /* check filename */
342  if (!fname) return (NOGO);
343  while (isspace(*fname)) fname++;
344  if (!*fname) return (NOGO);
345 
346  if (!access(fname, F_OK)) {
347  /* named file exists */
348  if (stat(fname, &st))
349  return (NOGO); /* shouldn't happen */
350 
351  if ((st.st_mode&S_IFMT) != S_IFLNK &&
352  (st.st_mode&S_IFMT) != S_IFREG) goto bad;
353  /* not a simple file or symbolic link */
354  }
355 
356  switch (mode) {
357  case R_OK:
358  if (access(fname,R_OK)) {
359  /* can't open for reading */
360  if (!access(fname,F_OK)) goto bad; /* it exists, so error */
361  return (NO_EXIST);
362  }
363  return (READ_OK);
364  case W_OK:
365  if (access(fname,W_OK)) {
366  /* can't open for writing */
367  if (!access(fname,F_OK)) goto bad; /* it exists, so error */
368  fp = fopen(fname,"w");
369  if (!fp) goto bad;
370  fclose(fp);
371  }
372  return (WRITE_OK);
373  }
374 bad:
375  sprintf(buf, msg, (mode == R_OK) ? "read" : "write", fname);
376  PopUpMessage(buf, w);
377  return (NOGO);
378 }
379 
380 
381 /* ARGSUSED */
382 static void
383 ToTop(caller, clientdata, event, ctd)
384 
385 Widget caller;
386 XtPointer clientdata;
387 XEvent *event;
388 Boolean *ctd;
389 {
390  /* prevent windows from disappearing */
391  widget_bag *w = (widget_bag*)clientdata;
392 
393  XVisibilityEvent *vev = (XVisibilityEvent*)event;
394  XWindowChanges xv;
395 
396  if (vev->state == VisibilityFullyObscured) {
397  xv.sibling = XtWindow(w->shell);
398  xv.stack_mode = Above;
399  XReconfigureWMWindow(vev->display, vev->window,
400  DefaultScreen(vev->display), CWSibling|CWStackMode, &xv);
401  }
402 }
403 
404 #else
406 #endif
void PopUpErr()
static char buf[MAXPROMPT]
Definition: arg.c:18
Widget butbox
Definition: x11util.h:15
static char * butTranslations
Definition: xeditor.c:133
FILE * p
Definition: proc2mod.c:48
Definition: cddefs.h:169
#define TRUE
Definition: util.h:27
int x11_util_dummy
Definition: x11util.c:405
Definition: x11util.h:7
Definition: cddefs.h:237
#define NULL
Definition: spdefs.h:121
Widget message
Definition: x11util.h:18
check_type CheckFile()
#define True
Definition: scedstub.c:16
String * popup_sens_list
Definition: x11util.h:20
Widget popup_text
Definition: x11util.h:17
int access(char *pth, int m)
Definition: libfuncs.c:75
check_type
Definition: x11util.h:7
void PopUpMessage()
Widget shell
Definition: x11util.h:10
void PopDownErr()
Definition: x11util.h:7
void PopDownMessage()
static char * oneLineTranslations
Definition: xeditor.c:130
Widget popup
Definition: x11util.h:16
Widget form
Definition: x11util.h:11
static char * wmTranslations
Definition: xeditor.c:134
void CenterWidgetOnWidget()
#define False
Definition: scedstub.c:15
Atom wm_delete
Definition: x11util.h:19
void PopDownInput()
Display * Xdisplay
void PopUpInput()