1 /*
2  * chglbl.c
3  *
4  * Copyright (C) 1997 Rasca, Berlin
5  * EMail: thron@gmx.de
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21 
22 #include <stdio.h>
23 #include <X11/Intrinsic.h>
24 #include <X11/StringDefs.h>
25 #include <X11/Shell.h>
26 #include <X11/Xaw/Form.h>
27 #include <Xw/Field.h>
28 #include <Xw/Button.h>
29 #include <Xw/Label.h>
30 
31 void WmDelEH (Widget, XtPointer, XEvent*, Boolean*);
32 static Widget shell = None;
33 static Widget frame, field, ok;
34 
35 /*
36  * find toplevel widget
37  */
38 static Widget
Toplevel(Widget w)39 Toplevel (Widget w)
40 {
41     while ((!XtIsTopLevelShell(w)) && XtParent(w))
42         w = XtParent(w);
43     return (w);
44 }
45 
46 /*
47  */
48 static void
CbDone(Widget w,XtPointer client_data,XtPointer call_data)49 CbDone (Widget w, XtPointer client_data, XtPointer call_data)
50 {
51 #ifdef DEBUG
52 	printf ("CbDone()\n");
53 #endif
54 	if ((strcmp ("ok", XtName(w)) == 0) || (strcmp("field", XtName(w)) ==0)) {
55 		char *val;
56 
57 		val = XwFieldGetString (field);
58 		if (val)
59 			XtVaSetValues ((Widget)client_data, XtNlabel, val, NULL);
60 	}
61 	XtDestroyWidget (shell);
62 }
63 
64 /*
65  */
66 static void
CbTab(Widget w,XtPointer client_data,XtPointer call_data)67 CbTab (Widget w, XtPointer client_data, XtPointer call_data)
68 {
69 	Widget *pn = (Widget*)client_data;
70 	XEvent *ev = (XEvent*) call_data;
71 
72 #ifdef DEBUG
73 	printf ("CbTab()\n");
74 #endif
75 	if (ev->type != KeyPress)
76 		return;
77 	if (ev->xkey.state == ShiftMask) {
78 		XtSetKeyboardFocus (XtParent(pn[0]), pn[0]);
79 	} else {
80 		XtSetKeyboardFocus (XtParent(pn[1]), pn[1]);
81 	}
82 }
83 
84 /*
85  * change the label of a device
86  */
87 void
CbChangeLabel(Widget w,XtPointer client_data,XtPointer call_data)88 CbChangeLabel (Widget w, XtPointer client_data, XtPointer call_data)
89 {
90 	Widget top, label, cancel;
91 	static Widget prev_next[3][2];
92 	Position x, y;
93 	char *name = NULL;
94 	static Atom wm_delete;
95 
96 	wm_delete = (Atom) client_data;
97 	top = Toplevel(w);
98 	XtVaGetValues (w, XtNlabel, &name, NULL);
99 	shell = XtVaCreatePopupShell ("new_label", transientShellWidgetClass, top,
100 				NULL);
101 
102 	frame = XtVaCreateManagedWidget ("label_box", formWidgetClass, shell,
103 				NULL);
104 
105 	label = XtVaCreateManagedWidget ("label", xwLabelWidgetClass, frame,
106 				XtNlabel, "Label:",
107 				NULL);
108 
109 	field = XtVaCreateManagedWidget ("field", xwFieldWidgetClass, frame,
110 				XtNstring, name,
111 				XtNfromHoriz, label,
112 				NULL);
113 
114 	ok     = XtVaCreateManagedWidget ("ok", xwButtonWidgetClass, frame,
115 				XtNfromVert, label,
116 				NULL);
117 
118 	cancel = XtVaCreateManagedWidget ("cancel", xwButtonWidgetClass, frame,
119 				XtNfromVert, label,
120 				XtNfromHoriz, ok,
121 				NULL);
122 
123 	XtAddCallback (field, XtNactivateCallback, CbDone, w);
124 	XtAddCallback (ok, XtNcallback, CbDone, w);
125 	XtAddCallback (cancel, XtNcallback, CbDone, w);
126 
127 	prev_next[0][0] = cancel;
128 	prev_next[0][1] = ok;
129 	XtAddCallback (field, XtNtabCallback, CbTab, prev_next[0]);
130 	prev_next[1][0] = field;
131 	prev_next[1][1] = cancel;
132 	XtAddCallback (ok, XtNtabCallback, CbTab, prev_next[1]);
133 	prev_next[2][0] = ok;
134 	prev_next[2][1] = field;
135 	XtAddCallback (cancel, XtNtabCallback, CbTab, prev_next[2]);
136 
137 	XtTranslateCoords (w, 0, 0, &x, &y);
138 	XtVaSetValues (shell, XtNx, x, XtNy, y, NULL);
139 	XtPopup (shell, XtGrabNonexclusive);
140 
141 	XSetWMProtocols (XtDisplay(shell), XtWindow(shell), &wm_delete, 1);
142 	XtAddEventHandler (shell, NoEventMask, True, WmDelEH , &wm_delete);
143 	XtInstallAccelerators (field, ok);
144 	XtInstallAccelerators (field, cancel);
145 	XtSetKeyboardFocus (frame, field);
146 }
147 
148