0
|
1 /* The lwlib interface to "xlwmenu" menus.
|
|
2 Copyright (C) 1992, 1994 Lucid, Inc.
|
|
3
|
|
4 This file is part of the Lucid Widget Library.
|
|
5
|
|
6 The Lucid Widget Library is free software; you can redistribute it and/or
|
|
7 modify it under the terms of the GNU General Public License as published by
|
|
8 the Free Software Foundation; either version 2, or (at your option)
|
|
9 any later version.
|
|
10
|
|
11 The Lucid Widget Library is distributed in the hope that it will be useful,
|
|
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14 GNU General Public License for more details.
|
|
15
|
|
16 You should have received a copy of the GNU General Public License
|
80
|
17 along with XEmacs; see the file COPYING. If not, write to
|
78
|
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
19 Boston, MA 02111-1307, USA. */
|
0
|
20
|
|
21 #include <stdlib.h> /* for abort () */
|
|
22 #include <limits.h>
|
|
23
|
|
24 #include "lwlib-Xlw.h"
|
|
25 #include <X11/StringDefs.h>
|
|
26 #include <X11/IntrinsicP.h>
|
|
27 #include <X11/ObjectP.h>
|
|
28 #include <X11/CompositeP.h>
|
|
29 #include <X11/Shell.h>
|
|
30 #ifdef MENUBARS_LUCID
|
|
31 #include "xlwmenu.h"
|
|
32 #endif
|
|
33 #ifdef SCROLLBARS_LUCID
|
|
34 #include "xlwscrollbar.h"
|
|
35 #endif
|
|
36
|
|
37
|
|
38
|
|
39 #ifdef MENUBARS_LUCID
|
|
40
|
|
41 /* Menu callbacks */
|
|
42
|
|
43 static void
|
|
44 pre_hook (Widget w, XtPointer client_data, XtPointer call_data)
|
|
45 {
|
|
46 widget_instance* instance = (widget_instance*)client_data;
|
|
47 widget_value* val;
|
|
48
|
|
49 if (w->core.being_destroyed)
|
|
50 return;
|
|
51
|
|
52 val = lw_get_widget_value_for_widget (instance, w);
|
|
53 #if 0
|
|
54 /* #### - this code used to (for some random back_asswards reason) pass
|
|
55 the expression below in the call_data slot. For incremental menu
|
|
56 construction, this needs to go. I can't even figure out why it was done
|
78
|
57 this way in the first place...it's just a historical weirdism. --Stig */
|
0
|
58 call_data = (val ? val->call_data : NULL);
|
|
59 #endif
|
|
60 if (val && val->call_data)
|
|
61 abort(); /* #### - the call_data for the top_level
|
|
62 "menubar" widget_value used to be passed
|
|
63 back to the pre_hook. */
|
|
64
|
|
65 if (instance->info->pre_activate_cb)
|
|
66 instance->info->pre_activate_cb (w, instance->info->id, call_data);
|
|
67 }
|
|
68
|
|
69 static void
|
|
70 pick_hook (Widget w, XtPointer client_data, XtPointer call_data)
|
|
71 {
|
|
72 widget_instance* instance = (widget_instance*)client_data;
|
|
73 widget_value* contents_val = (widget_value*)call_data;
|
|
74 widget_value* widget_val;
|
|
75 XtPointer widget_arg;
|
|
76 LWLIB_ID id;
|
|
77 lw_callback post_activate_cb;
|
|
78
|
|
79 if (w->core.being_destroyed)
|
|
80 return;
|
|
81
|
|
82 /* Grab these values before running any functions, in case running
|
|
83 the selection_cb causes the widget to be destroyed. */
|
|
84 id = instance->info->id;
|
|
85 post_activate_cb = instance->info->post_activate_cb;
|
|
86
|
|
87 widget_val = lw_get_widget_value_for_widget (instance, w);
|
|
88 widget_arg = widget_val ? widget_val->call_data : NULL;
|
|
89
|
|
90 if (instance->info->selection_cb &&
|
|
91 contents_val &&
|
|
92 contents_val->enabled &&
|
|
93 !contents_val->contents)
|
|
94 instance->info->selection_cb (w, id, contents_val->call_data);
|
|
95
|
|
96 if (post_activate_cb)
|
|
97 post_activate_cb (w, id, widget_arg);
|
|
98 }
|
|
99
|
|
100
|
|
101
|
|
102 /* creation functions */
|
|
103 static Widget
|
|
104 xlw_create_menubar (widget_instance* instance)
|
|
105 {
|
|
106 Widget widget =
|
|
107 XtVaCreateWidget (instance->info->name, xlwMenuWidgetClass,
|
|
108 instance->parent,
|
|
109 XtNmenu, instance->info->val,
|
|
110 0);
|
80
|
111 XtAddCallback (widget, XtNopen, pre_hook, (XtPointer)instance);
|
0
|
112 XtAddCallback (widget, XtNselect, pick_hook, (XtPointer)instance);
|
|
113 return widget;
|
|
114 }
|
|
115
|
|
116 static Widget
|
|
117 xlw_create_popup_menu (widget_instance* instance)
|
|
118 {
|
|
119 Widget popup_shell =
|
|
120 XtCreatePopupShell (instance->info->name, overrideShellWidgetClass,
|
|
121 instance->parent, NULL, 0);
|
|
122
|
|
123 Widget widget =
|
|
124 XtVaCreateManagedWidget ("popup", xlwMenuWidgetClass,
|
|
125 popup_shell,
|
|
126 XtNmenu, instance->info->val,
|
|
127 XtNhorizontal, False,
|
|
128 0);
|
|
129
|
|
130 XtAddCallback (widget, XtNselect, pick_hook, (XtPointer)instance);
|
|
131
|
|
132 return popup_shell;
|
|
133 }
|
|
134 #endif /* MENUBARS_LUCID */
|
|
135
|
|
136 #ifdef SCROLLBARS_LUCID
|
|
137 static void
|
|
138 xlw_scrollbar_callback (Widget widget, XtPointer closure, XtPointer call_data)
|
|
139 {
|
|
140 widget_instance *instance = (widget_instance *) closure;
|
|
141 LWLIB_ID id;
|
|
142 XlwScrollBarCallbackStruct *data =
|
|
143 (XlwScrollBarCallbackStruct *) call_data;
|
|
144 scroll_event event_data;
|
|
145 scrollbar_values *val =
|
|
146 (scrollbar_values *) instance->info->val->scrollbar_data;
|
|
147 double percent;
|
|
148
|
|
149 if (!instance || widget->core.being_destroyed)
|
|
150 return;
|
|
151
|
|
152 id = instance->info->id;
|
|
153
|
|
154 percent = (double) (data->value - 1) / (double) (INT_MAX - 1);
|
|
155 event_data.slider_value =
|
|
156 (int) (percent * (double) (val->maximum - val->minimum)) + val->minimum;
|
|
157
|
80
|
158 if (event_data.slider_value > val->maximum - val->slider_size)
|
|
159 event_data.slider_value = val->maximum - val->slider_size;
|
0
|
160 else if (event_data.slider_value < val->minimum)
|
80
|
161 event_data.slider_value = val->minimum;
|
0
|
162
|
|
163 if (data->event)
|
|
164 {
|
78
|
165 switch (data->event->type)
|
0
|
166 {
|
|
167 case KeyPress:
|
|
168 case KeyRelease:
|
|
169 event_data.time = data->event->xkey.time;
|
|
170 break;
|
|
171 case ButtonPress:
|
|
172 case ButtonRelease:
|
|
173 event_data.time = data->event->xbutton.time;
|
|
174 break;
|
|
175 case MotionNotify:
|
|
176 event_data.time = data->event->xmotion.time;
|
|
177 break;
|
|
178 case EnterNotify:
|
|
179 case LeaveNotify:
|
|
180 event_data.time = data->event->xcrossing.time;
|
|
181 break;
|
|
182 default:
|
|
183 event_data.time = 0;
|
|
184 break;
|
|
185 }
|
|
186 }
|
|
187 else
|
|
188 event_data.time = 0;
|
|
189
|
|
190 switch (data->reason)
|
|
191 {
|
80
|
192 case XmCR_DECREMENT: event_data.action = SCROLLBAR_LINE_UP; break;
|
|
193 case XmCR_INCREMENT: event_data.action = SCROLLBAR_LINE_DOWN; break;
|
|
194 case XmCR_PAGE_DECREMENT: event_data.action = SCROLLBAR_PAGE_UP; break;
|
|
195 case XmCR_PAGE_INCREMENT: event_data.action = SCROLLBAR_PAGE_DOWN; break;
|
|
196 case XmCR_TO_TOP: event_data.action = SCROLLBAR_TOP; break;
|
|
197 case XmCR_TO_BOTTOM: event_data.action = SCROLLBAR_BOTTOM; break;
|
|
198 case XmCR_DRAG: event_data.action = SCROLLBAR_DRAG; break;
|
|
199 case XmCR_VALUE_CHANGED: event_data.action = SCROLLBAR_CHANGE; break;
|
|
200 default: event_data.action = SCROLLBAR_CHANGE; break;
|
0
|
201 }
|
|
202
|
|
203 if (instance->info->pre_activate_cb)
|
|
204 instance->info->pre_activate_cb (widget, id, (XtPointer) &event_data);
|
|
205 }
|
|
206
|
78
|
207 #define add_scrollbar_callback(resource) \
|
|
208 XtAddCallback (scrollbar, resource, xlw_scrollbar_callback, (XtPointer) instance)
|
|
209
|
0
|
210 /* #### Does not yet support horizontal scrollbars. */
|
|
211 static Widget
|
|
212 xlw_create_scrollbar (widget_instance *instance, int vertical)
|
|
213 {
|
|
214 Arg al[20];
|
|
215 int ac = 0;
|
78
|
216 static XtCallbackRec callbacks[2] =
|
|
217 { {xlw_scrollbar_callback, NULL}, {NULL, NULL} };
|
|
218
|
|
219 callbacks[0].closure = (XtPointer) instance;
|
0
|
220
|
78
|
221 XtSetArg (al[ac], XmNminimum, 1); ac++;
|
|
222 XtSetArg (al[ac], XmNmaximum, INT_MAX); ac++;
|
|
223 XtSetArg (al[ac], XmNincrement, 1); ac++;
|
|
224 XtSetArg (al[ac], XmNpageIncrement, 1); ac++;
|
|
225 XtSetArg (al[ac], XmNorientation, (vertical ? XmVERTICAL : XmHORIZONTAL)); ac++;
|
0
|
226
|
78
|
227 XtSetArg (al[ac], XmNdecrementCallback, callbacks); ac++;
|
|
228 XtSetArg (al[ac], XmNdragCallback, callbacks); ac++;
|
|
229 XtSetArg (al[ac], XmNincrementCallback, callbacks); ac++;
|
|
230 XtSetArg (al[ac], XmNpageDecrementCallback, callbacks); ac++;
|
|
231 XtSetArg (al[ac], XmNpageIncrementCallback, callbacks); ac++;
|
|
232 XtSetArg (al[ac], XmNtoBottomCallback, callbacks); ac++;
|
|
233 XtSetArg (al[ac], XmNtoTopCallback, callbacks); ac++;
|
|
234 XtSetArg (al[ac], XmNvalueChangedCallback, callbacks); ac++;
|
0
|
235
|
78
|
236 return XtCreateWidget (instance->info->name, xlwScrollBarWidgetClass,
|
|
237 instance->parent, al, ac);
|
0
|
238 }
|
|
239
|
|
240 static Widget
|
|
241 xlw_create_vertical_scrollbar (widget_instance *instance)
|
|
242 {
|
|
243 return xlw_create_scrollbar (instance, 1);
|
|
244 }
|
|
245
|
|
246 static Widget
|
|
247 xlw_create_horizontal_scrollbar (widget_instance *instance)
|
|
248 {
|
|
249 return xlw_create_scrollbar (instance, 0);
|
|
250 }
|
|
251
|
|
252 static void
|
|
253 xlw_update_scrollbar (widget_instance *instance, Widget widget,
|
|
254 widget_value *val)
|
|
255 {
|
|
256 if (val->scrollbar_data)
|
|
257 {
|
|
258 scrollbar_values *data = val->scrollbar_data;
|
|
259 int widget_sliderSize, widget_val;
|
|
260 int new_sliderSize, new_value;
|
|
261 double percent;
|
|
262
|
78
|
263 /* First size and position the scrollbar widget. */
|
0
|
264 XtVaSetValues (widget,
|
|
265 XtNx, data->scrollbar_x,
|
|
266 XtNy, data->scrollbar_y,
|
|
267 XtNwidth, data->scrollbar_width,
|
|
268 XtNheight, data->scrollbar_height,
|
|
269 0);
|
|
270
|
80
|
271 /* Now size the scrollbar's slider. */
|
0
|
272 XtVaGetValues (widget,
|
|
273 XmNsliderSize, &widget_sliderSize,
|
80
|
274 XmNvalue, &widget_val,
|
0
|
275 0);
|
|
276
|
|
277 percent = (double) data->slider_size /
|
|
278 (double) (data->maximum - data->minimum);
|
|
279 percent = (percent > 1.0 ? 1.0 : percent);
|
|
280 new_sliderSize = (int) ((double) (INT_MAX - 1) * percent);
|
|
281
|
|
282 percent = (double) (data->slider_position - data->minimum) /
|
|
283 (double) (data->maximum - data->minimum);
|
|
284 percent = (percent > 1.0 ? 1.0 : percent);
|
|
285 new_value = (int) ((double) (INT_MAX - 1) * percent);
|
|
286
|
80
|
287 if (new_sliderSize > INT_MAX - 1)
|
|
288 new_sliderSize = INT_MAX - 1;
|
0
|
289 if (new_sliderSize < 1)
|
80
|
290 new_sliderSize = 1;
|
0
|
291
|
|
292 if (new_value > (INT_MAX - new_sliderSize))
|
80
|
293 new_value = INT_MAX - new_sliderSize;
|
0
|
294 else if (new_value < 1)
|
80
|
295 new_value = 1;
|
0
|
296
|
|
297 if (new_sliderSize != widget_sliderSize || new_value != widget_val)
|
|
298 XlwScrollBarSetValues (widget, new_value, new_sliderSize, 1, 1, False);
|
|
299 }
|
|
300 }
|
|
301
|
|
302 #endif /* SCROLLBARS_LUCID */
|
|
303
|
|
304 widget_creation_entry
|
|
305 xlw_creation_table [] =
|
|
306 {
|
|
307 #ifdef MENUBARS_LUCID
|
|
308 {"menubar", xlw_create_menubar},
|
|
309 {"popup", xlw_create_popup_menu},
|
|
310 #endif
|
|
311 #ifdef SCROLLBARS_LUCID
|
|
312 {"vertical-scrollbar", xlw_create_vertical_scrollbar},
|
|
313 {"horizontal-scrollbar", xlw_create_horizontal_scrollbar},
|
|
314 #endif
|
|
315 {NULL, NULL}
|
|
316 };
|
|
317
|
|
318 Boolean
|
|
319 lw_lucid_widget_p (Widget widget)
|
|
320 {
|
|
321 WidgetClass the_class = XtClass (widget);
|
|
322 #ifdef MENUBARS_LUCID
|
|
323 if (the_class == xlwMenuWidgetClass)
|
|
324 return True;
|
|
325 #endif
|
|
326 #ifdef SCROLLBARS_LUCID
|
|
327 if (the_class == xlwScrollBarWidgetClass)
|
|
328 return True;
|
|
329 #endif
|
|
330 #ifdef MENUBARS_LUCID
|
|
331 if (the_class == overrideShellWidgetClass)
|
|
332 return
|
|
333 XtClass (((CompositeWidget)widget)->composite.children [0])
|
|
334 == xlwMenuWidgetClass;
|
|
335 #endif
|
|
336 return False;
|
|
337 }
|
|
338
|
|
339 void
|
|
340 xlw_update_one_widget (widget_instance* instance, Widget widget,
|
|
341 widget_value* val, Boolean deep_p)
|
|
342 {
|
|
343 WidgetClass class;
|
|
344
|
|
345 class = XtClass (widget);
|
|
346
|
|
347 if (0)
|
|
348 ;
|
|
349 #ifdef MENUBARS_LUCID
|
|
350 else if (class == xlwMenuWidgetClass)
|
|
351 {
|
|
352 XlwMenuWidget mw;
|
|
353 if (XtIsShell (widget))
|
|
354 mw = (XlwMenuWidget)((CompositeWidget)widget)->composite.children [0];
|
|
355 else
|
|
356 mw = (XlwMenuWidget)widget;
|
|
357 XtVaSetValues (widget, XtNmenu, val, 0);
|
|
358 }
|
|
359 #endif
|
|
360 #ifdef SCROLLBARS_LUCID
|
|
361 else if (class == xlwScrollBarWidgetClass)
|
|
362 {
|
|
363 xlw_update_scrollbar (instance, widget, val);
|
|
364 }
|
|
365 #endif
|
|
366 }
|
|
367
|
|
368 void
|
|
369 xlw_update_one_value (widget_instance* instance, Widget widget,
|
|
370 widget_value* val)
|
|
371 {
|
|
372 return;
|
|
373 }
|
|
374
|
|
375 void
|
|
376 xlw_pop_instance (widget_instance* instance, Boolean up)
|
|
377 {
|
|
378 }
|
|
379
|
|
380 #ifdef MENUBARS_LUCID
|
|
381 void
|
|
382 xlw_popup_menu (Widget widget, XEvent *event)
|
|
383 {
|
|
384 XlwMenuWidget mw;
|
|
385
|
|
386 if (!XtIsShell (widget))
|
|
387 return;
|
|
388
|
|
389 if (event->type == ButtonPress || event->type == ButtonRelease)
|
|
390 {
|
|
391 mw = (XlwMenuWidget)((CompositeWidget)widget)->composite.children [0];
|
|
392 xlw_pop_up_menu (mw, (XButtonPressedEvent *)event);
|
|
393 }
|
|
394 else
|
|
395 abort ();
|
|
396 }
|
|
397 #endif
|
|
398
|
|
399 /* Destruction of instances */
|
|
400 void
|
|
401 xlw_destroy_instance (widget_instance* instance)
|
|
402 {
|
|
403 if (instance->widget)
|
|
404 XtDestroyWidget (instance->widget);
|
|
405 }
|