1 /*
2    XMascot Ver 2.6
3    Copyright(c) 1996,1997 Go Watanabe     go@cclub.tutcc.tut.ac.jp
4                           Tsuyoshi IIda   iida@cclub.tutcc.tut.ac.jp
5 */
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <math.h>
9 
10 #include <X11/Xlib.h>
11 #include <X11/Intrinsic.h>
12 #include <X11/StringDefs.h>
13 
14 #ifndef XAW3D
15 #include <X11/Xaw/SimpleMenu.h>
16 #include <X11/Xaw/SmeBSB.h>
17 #include <X11/Xaw/SmeLine.h>
18 #include <X11/Xaw/Label.h>
19 #include <X11/Xaw/Command.h>
20 #include <X11/Xaw/AsciiText.h>
21 #include <X11/Xaw/Box.h>
22 #include <X11/Xaw/Form.h>
23 #include <X11/Xaw/Scrollbar.h>
24 #include <X11/Xaw/Toggle.h>
25 #else
26 #include <X11/Xaw3d/SimpleMenu.h>
27 #include <X11/Xaw3d/SmeBSB.h>
28 #include <X11/Xaw3d/SmeLine.h>
29 #include <X11/Xaw3d/Label.h>
30 #include <X11/Xaw3d/Command.h>
31 #include <X11/Xaw3d/AsciiText.h>
32 #include <X11/Xaw3d/Box.h>
33 #include <X11/Xaw3d/Form.h>
34 #include <X11/Xaw3d/Scrollbar.h>
35 #include <X11/Xaw3d/Toggle.h>
36 #endif
37 
38 #include "CascadeMenu.h"
39 #include "xmascot.h"
40 
41 extern AppData  adat;		/* ���ץꥱ�������꥽���� */
42 extern Widget   top;
43 
44 #ifdef BIFF
45 extern Widget   biff;
46 #endif
47 
48 typedef struct {
49 	char           *name;
50 	char           *format;
51 	float           factor;
52 	float           data;
53 	float           offset;
54 	float           diff;
55 	Widget          text, bar;
56 	int             changed;
57 }               PrefDialog;
58 
59 PrefDialog      param[] = {
60 	{"grav", "%4.2fG", 1, 0, 0, 0.01, NULL, NULL, 0},
61 	{"damp", "%4.2f ", 1, 0, 0, 0.01, NULL, NULL, 0},
62 #ifdef SHADOW
63 	{"shadow", "%4.0f ", 20, 0, 0, 1, NULL, NULL, 0},
64 #endif
65 	{"mag", "%4.1f ", 2.9, 0, 0.1, 0.1, NULL, NULL, 0},
66 	{"chain", "%4.0f ", 19, 0, 1, 1, NULL, NULL, 0},
67 };
68 
69 static PrefDialog *pref;
70 static void
set_pref(float data)71 set_pref(float data)
72 {
73 	char            str[20];
74 	sprintf(str, pref->format, data);
75 #if 0
76 	XtVaSetValues(pref->text, XtNlabel, XtNewString(str), NULL);
77 #endif
78 	XtVaSetValues(pref->text, XtNlabel, str, NULL);
79 
80 	pref->data = data;
81 	XawScrollbarSetThumb(pref->bar, ((data - pref->offset) / pref->factor), 0);
82 	pref->changed = 0;
83 	pref++;
84 }
85 
86 static int
changed(void)87 changed(void)
88 {
89 	if (pref->changed)
90 		return 1;
91 	pref++;
92 	return 0;
93 }
94 
95 static float
get_pref(void)96 get_pref(void)
97 {
98 	float           ret = pref->data;
99 	pref++;
100 	return ret;
101 }
102 
103 /* �������ޥ������������Υ��åȥ��å� */
104 static void
set_preference_dialog(Widget w,XtPointer dat,XtPointer call)105 set_preference_dialog(Widget w, XtPointer dat, XtPointer call)
106 {
107 	pref = param;
108 	set_pref(adat.grav / 980.0);
109 	set_pref(adat.damping);
110 #ifdef SHADOW
111 	set_pref(adat.shadow);
112 #endif
113 	set_pref(adat.magnify);
114 	set_pref(adat.chain_num);
115 }
116 
117 /* ������� */
118 static void
popdown(Widget w,XtPointer dat,XtPointer call)119 popdown(Widget w, XtPointer dat, XtPointer call)
120 {
121 	XtPopdown(dat);
122 }
123 
124 /* OK */
125 static void
ok_preference(Widget w,XtPointer dat,XtPointer cal)126 ok_preference(Widget w, XtPointer dat, XtPointer cal)
127 {
128 	int             reload_flag = 0;
129 	int             reload_chain = 0;
130 
131 	XtPopdown(dat);
132 
133 	pref = param;
134 	if (changed())
135 		adat.grav = get_pref() * 980;
136 	if (changed())
137 		adat.damping = get_pref();
138 
139 #ifdef SHADOW
140 	if (changed()) {
141 		reload_flag = 1;
142 		adat.shadow = get_pref();
143 	}
144 #endif
145 	if (changed()) {
146 		reload_flag = 1;
147 		adat.magnify = get_pref();
148 	}
149 	if (changed()) {
150 		reload_chain = 1;
151 		create_chains(get_pref());
152 		RaiseAll();
153 	}
154 	if (reload_flag) {
155 		set_widget_pattern(top, adat.pin_pat, adat.pcol0, adat.prgb0);
156 #ifdef BIFF
157 		set_widget_pattern(biff, adat.biff_pat, adat.bcol0, adat.brgb0);
158 #endif
159 		set_mas(&adat);
160 	}
161 #ifdef USE_CHAINPAT
162 	if (reload_chain || reload_flag)
163 		set_chain_pat(adat.chain_pat, adat.ccol0, adat.crgb0);
164 #endif
165 	set_sim_param();
166 	reset_pos();
167 	set_pos();
168 }
169 
170 /* ��������С������ѤΥ�����Хå� */
171 static void
bar_jump(Widget w,XtPointer dat,XtPointer a)172 bar_jump(Widget w, XtPointer dat, XtPointer a)
173 {
174 	char            str[20];
175 	float           data = *((float *) a);
176 	PrefDialog     *p = &param[(long) dat];
177 	sprintf(str, p->format, (p->data = data * p->factor + p->offset));
178 #if 0
179 	XtVaSetValues(p->text, XtNlabel, XtNewString(str), NULL);
180 #endif
181 	XtVaSetValues(p->text, XtNlabel, str, NULL);
182 	p->changed = 1;
183 }
184 
185 static void
bar_scroll(Widget w,XtPointer dat,XtPointer a)186 bar_scroll(Widget w, XtPointer dat, XtPointer a)
187 {
188 	char            str[20];
189 	int             dir = (int) a;
190 	PrefDialog     *p = &param[(long) dat];
191 	if (dir < 0) {
192 		p->data -= p->diff;
193 		if (p->data < 0)
194 			p->data = 0;
195 	} else if (dir > 0) {
196 		p->data += p->diff;
197 		if (p->data > p->factor + p->offset)
198 			p->data = p->factor + p->offset;
199 	}
200 	sprintf(str, p->format, p->data);
201 #if 0
202 	XtVaSetValues(p->text, XtNlabel, XtNewString(str), NULL);
203 #endif
204 	XtVaSetValues(p->text, XtNlabel, str, NULL);
205 	XawScrollbarSetThumb(w, (p->data - p->offset) / p->factor, 0);
206 	p->changed = 1;
207 }
208 
209 /* �������ޥ����ѥ��������ν���� */
210 Widget
preference_dialog(Widget top)211 preference_dialog(Widget top)
212 {
213 	int             i;
214 	char            name[20];
215 	PrefDialog     *p;
216 
217 	Widget          base, form, box, ok, cancel;
218 	Widget          up;
219 	Widget          box_label, box_text, box_bar;
220 
221 	base = XtVaCreatePopupShell("preferenceBase",
222 				    transientShellWidgetClass, top, NULL);
223 	XtAddCallback(base, XtNpopupCallback, set_preference_dialog, NULL);
224 	form = XtVaCreateManagedWidget("preference", formWidgetClass, base, NULL);
225 	up = XtVaCreateManagedWidget("title", labelWidgetClass, form, NULL);
226 	box_label = XtVaCreateManagedWidget("box", boxWidgetClass, form,
227 					    XtNfromVert, up,
228 				    XtNorientation, XtorientVertical, NULL);
229 	box_text = XtVaCreateManagedWidget("box", boxWidgetClass, form,
230 					   XtNfromVert, up,
231 					   XtNfromHoriz, box_label,
232 				    XtNorientation, XtorientVertical, NULL);
233 	box_bar = XtVaCreateManagedWidget("box", boxWidgetClass, form,
234 					  XtNfromVert, up,
235 					  XtNfromHoriz, box_text,
236 				    XtNorientation, XtorientVertical, NULL);
237 	up = box_label;
238 	for (i = 0; i < XtNumber(param); i++) {
239 		p = &param[i];
240 		sprintf(name, "%s%s", p->name, "_label");
241 		XtVaCreateManagedWidget(name, labelWidgetClass, box_label, NULL);
242 		sprintf(name, "%s%s", p->name, "_num");
243 		p->text = XtVaCreateManagedWidget(name, labelWidgetClass, box_text, NULL);
244 		sprintf(name, "%s%s", p->name, "_bar");
245 		p->bar = XtVaCreateManagedWidget(name, scrollbarWidgetClass, box_bar,
246 					 XtNorientation, XtorientHorizontal,
247 						 NULL);
248 		XtAddCallback(p->bar, XtNjumpProc, bar_jump, I2P(i));
249 		XtAddCallback(p->bar, XtNscrollProc, bar_scroll, I2P(i));
250 	}
251 	box = XtVaCreateManagedWidget("box", boxWidgetClass, form,
252 				      XtNfromVert, up,
253 				  XtNorientation, XtorientHorizontal, NULL);
254 	ok = XtVaCreateManagedWidget("ok", commandWidgetClass, box, NULL);
255 	XtAddCallback(ok, XtNcallback, ok_preference, base);
256 	cancel = XtVaCreateManagedWidget("cancel", commandWidgetClass, box, NULL);
257 	XtAddCallback(cancel, XtNcallback, popdown, base);
258 	return base;
259 }
260