1 /* The lwlib interface to "xlwmenu" menus.
2 Copyright (C) 1992, 1994 Lucid, Inc.
4 This file is part of the Lucid Widget Library.
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)
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.
16 You should have received a copy of the GNU General Public License
17 along with XEmacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 #include <stdlib.h> /* for abort () */
25 #include "lwlib-Xlw.h"
26 #include <X11/StringDefs.h>
27 #include <X11/IntrinsicP.h>
28 #include <X11/ObjectP.h>
29 #include <X11/CompositeP.h>
30 #include <X11/Shell.h>
31 #ifdef LWLIB_MENUBARS_LUCID
34 #ifdef LWLIB_SCROLLBARS_LUCID
35 #include "xlwscrollbar.h"
40 #ifdef LWLIB_MENUBARS_LUCID
45 pre_hook (Widget w, XtPointer client_data, XtPointer call_data)
47 widget_instance* instance = (widget_instance*)client_data;
50 if (w->core.being_destroyed)
53 val = lw_get_widget_value_for_widget (instance, w);
55 /* #### - this code used to (for some random back_asswards reason) pass
56 the expression below in the call_data slot. For incremental menu
57 construction, this needs to go. I can't even figure out why it was done
58 this way in the first place...it's just a historical weirdism. --Stig */
59 call_data = (val ? val->call_data : NULL);
61 if (val && val->call_data)
62 abort(); /* #### - the call_data for the top_level
63 "menubar" widget_value used to be passed
64 back to the pre_hook. */
66 if (instance->info->pre_activate_cb)
67 instance->info->pre_activate_cb (w, instance->info->id, call_data);
71 pick_hook (Widget w, XtPointer client_data, XtPointer call_data)
73 widget_instance* instance = (widget_instance*)client_data;
74 widget_value* contents_val = (widget_value*)call_data;
75 widget_value* widget_val;
78 lw_callback post_activate_cb;
80 if (w->core.being_destroyed)
83 /* Grab these values before running any functions, in case running
84 the selection_cb causes the widget to be destroyed. */
85 id = instance->info->id;
86 post_activate_cb = instance->info->post_activate_cb;
88 widget_val = lw_get_widget_value_for_widget (instance, w);
89 widget_arg = widget_val ? widget_val->call_data : NULL;
91 if (instance->info->selection_cb &&
93 contents_val->enabled &&
94 !contents_val->contents)
95 instance->info->selection_cb (w, id, contents_val->call_data);
98 post_activate_cb (w, id, widget_arg);
103 /* creation functions */
105 xlw_create_menubar (widget_instance* instance)
110 XtSetArg (al [0], XtNmenu, instance->info->val);
111 widget = XtCreateWidget (instance->info->name, xlwMenuWidgetClass,
112 instance->parent, al, 1);
113 XtAddCallback (widget, XtNopen, pre_hook, (XtPointer)instance);
114 XtAddCallback (widget, XtNselect, pick_hook, (XtPointer)instance);
119 xlw_create_popup_menu (widget_instance* instance)
122 Widget popup_shell, widget;
124 popup_shell = XtCreatePopupShell (instance->info->name,
125 overrideShellWidgetClass,
126 instance->parent, NULL, 0);
127 XtSetArg (al [0], XtNmenu, instance->info->val);
128 XtSetArg (al [1], XtNhorizontal, False);
129 widget = XtCreateManagedWidget ("popup", xlwMenuWidgetClass,
131 XtAddCallback (widget, XtNselect, pick_hook, (XtPointer)instance);
135 #endif /* LWLIB_MENUBARS_LUCID */
137 #ifdef LWLIB_SCROLLBARS_LUCID
139 xlw_scrollbar_callback (Widget widget, XtPointer closure, XtPointer call_data)
141 widget_instance *instance = (widget_instance *) closure;
143 XlwScrollBarCallbackStruct *data =
144 (XlwScrollBarCallbackStruct *) call_data;
145 scroll_event event_data;
146 scrollbar_values *val =
147 (scrollbar_values *) instance->info->val->scrollbar_data;
150 if (!instance || widget->core.being_destroyed)
153 id = instance->info->id;
155 percent = (double) (data->value - 1) / (double) (INT_MAX - 1);
156 event_data.slider_value =
157 (int) (percent * (double) (val->maximum - val->minimum)) + val->minimum;
159 if (event_data.slider_value > val->maximum - val->slider_size)
160 event_data.slider_value = val->maximum - val->slider_size;
161 else if (event_data.slider_value < val->minimum)
162 event_data.slider_value = val->minimum;
166 switch (data->event->type)
170 event_data.time = data->event->xkey.time;
174 event_data.time = data->event->xbutton.time;
177 event_data.time = data->event->xmotion.time;
181 event_data.time = data->event->xcrossing.time;
191 switch (data->reason)
193 case XmCR_DECREMENT: event_data.action = SCROLLBAR_LINE_UP; break;
194 case XmCR_INCREMENT: event_data.action = SCROLLBAR_LINE_DOWN; break;
195 case XmCR_PAGE_DECREMENT: event_data.action = SCROLLBAR_PAGE_UP; break;
196 case XmCR_PAGE_INCREMENT: event_data.action = SCROLLBAR_PAGE_DOWN; break;
197 case XmCR_TO_TOP: event_data.action = SCROLLBAR_TOP; break;
198 case XmCR_TO_BOTTOM: event_data.action = SCROLLBAR_BOTTOM; break;
199 case XmCR_DRAG: event_data.action = SCROLLBAR_DRAG; break;
200 case XmCR_VALUE_CHANGED: event_data.action = SCROLLBAR_CHANGE; break;
201 default: event_data.action = SCROLLBAR_CHANGE; break;
204 if (instance->info->pre_activate_cb)
205 instance->info->pre_activate_cb (widget, id, (XtPointer) &event_data);
208 #define add_scrollbar_callback(resource) \
209 XtAddCallback (scrollbar, resource, xlw_scrollbar_callback, (XtPointer) instance)
211 /* #### Does not yet support horizontal scrollbars. */
213 xlw_create_scrollbar (widget_instance *instance, int vertical)
217 static XtCallbackRec callbacks[2] =
218 { {xlw_scrollbar_callback, NULL}, {NULL, NULL} };
220 callbacks[0].closure = (XtPointer) instance;
222 XtSetArg (al[ac], XmNminimum, 1); ac++;
223 XtSetArg (al[ac], XmNmaximum, INT_MAX); ac++;
224 XtSetArg (al[ac], XmNincrement, 1); ac++;
225 XtSetArg (al[ac], XmNpageIncrement, 1); ac++;
226 XtSetArg (al[ac], XmNorientation, (vertical ? XmVERTICAL : XmHORIZONTAL)); ac++;
228 XtSetArg (al[ac], XmNdecrementCallback, callbacks); ac++;
229 XtSetArg (al[ac], XmNdragCallback, callbacks); ac++;
230 XtSetArg (al[ac], XmNincrementCallback, callbacks); ac++;
231 XtSetArg (al[ac], XmNpageDecrementCallback, callbacks); ac++;
232 XtSetArg (al[ac], XmNpageIncrementCallback, callbacks); ac++;
233 XtSetArg (al[ac], XmNtoBottomCallback, callbacks); ac++;
234 XtSetArg (al[ac], XmNtoTopCallback, callbacks); ac++;
235 XtSetArg (al[ac], XmNvalueChangedCallback, callbacks); ac++;
237 return XtCreateWidget (instance->info->name, xlwScrollBarWidgetClass,
238 instance->parent, al, ac);
242 xlw_create_vertical_scrollbar (widget_instance *instance)
244 return xlw_create_scrollbar (instance, 1);
248 xlw_create_horizontal_scrollbar (widget_instance *instance)
250 return xlw_create_scrollbar (instance, 0);
254 xlw_update_scrollbar (widget_instance *instance, Widget widget,
257 if (val->scrollbar_data)
259 scrollbar_values *data = val->scrollbar_data;
260 int widget_sliderSize, widget_val;
261 int new_sliderSize, new_value;
265 /* First size and position the scrollbar widget. */
266 XtSetArg (al [0], XtNx, data->scrollbar_x);
267 XtSetArg (al [1], XtNy, data->scrollbar_y);
268 XtSetArg (al [2], XtNwidth, data->scrollbar_width);
269 XtSetArg (al [3], XtNheight, data->scrollbar_height);
270 XtSetValues (widget, al, 4);
272 /* Now size the scrollbar's slider. */
273 XtSetArg (al [0], XmNsliderSize, &widget_sliderSize);
274 XtSetArg (al [1], XmNvalue, &widget_val);
275 XtGetValues (widget, al, 2);
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);
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);
287 if (new_sliderSize > INT_MAX - 1)
288 new_sliderSize = INT_MAX - 1;
289 else if (new_sliderSize < 1)
292 if (new_value > (INT_MAX - new_sliderSize))
293 new_value = INT_MAX - new_sliderSize;
294 else if (new_value < 1)
297 if (new_sliderSize != widget_sliderSize || new_value != widget_val)
298 XlwScrollBarSetValues (widget, new_value, new_sliderSize, 1, 1, False);
302 #endif /* LWLIB_SCROLLBARS_LUCID */
304 widget_creation_entry
305 xlw_creation_table [] =
307 #ifdef LWLIB_MENUBARS_LUCID
308 {"menubar", xlw_create_menubar},
309 {"popup", xlw_create_popup_menu},
311 #ifdef LWLIB_SCROLLBARS_LUCID
312 {"vertical-scrollbar", xlw_create_vertical_scrollbar},
313 {"horizontal-scrollbar", xlw_create_horizontal_scrollbar},
319 lw_lucid_widget_p (Widget widget)
321 WidgetClass the_class = XtClass (widget);
322 #ifdef LWLIB_MENUBARS_LUCID
323 if (the_class == xlwMenuWidgetClass)
326 #ifdef LWLIB_SCROLLBARS_LUCID
327 if (the_class == xlwScrollBarWidgetClass)
330 #ifdef LWLIB_MENUBARS_LUCID
331 if (the_class == overrideShellWidgetClass)
333 XtClass (((CompositeWidget)widget)->composite.children [0])
334 == xlwMenuWidgetClass;
340 xlw_update_one_widget (widget_instance* instance, Widget widget,
341 widget_value* val, Boolean deep_p)
345 class = XtClass (widget);
349 #ifdef LWLIB_MENUBARS_LUCID
350 else if (class == xlwMenuWidgetClass)
354 if (XtIsShell (widget))
355 mw = (XlwMenuWidget)((CompositeWidget)widget)->composite.children [0];
357 mw = (XlwMenuWidget)widget;
358 XtSetArg (al [0], XtNmenu, val);
359 XtSetValues (widget, al, 1);
362 #ifdef LWLIB_SCROLLBARS_LUCID
363 else if (class == xlwScrollBarWidgetClass)
365 xlw_update_scrollbar (instance, widget, val);
371 xlw_update_one_value (widget_instance* instance, Widget widget,
378 xlw_pop_instance (widget_instance* instance, Boolean up)
382 #ifdef LWLIB_MENUBARS_LUCID
384 xlw_popup_menu (Widget widget, XEvent *event)
388 if (!XtIsShell (widget))
391 if (event->type == ButtonPress || event->type == ButtonRelease)
393 mw = (XlwMenuWidget)((CompositeWidget)widget)->composite.children [0];
394 xlw_pop_up_menu (mw, (XButtonPressedEvent *)event);
399 #endif /* LWLIB_MENUBARS_LUCID */
401 \f/* Destruction of instances */
403 xlw_destroy_instance (widget_instance* instance)
405 if (instance->widget)
406 XtDestroyWidget (instance->widget);