1 /* The lwlib interface to Athena widgets.
2 Copyright (C) 1993, 1994 Free Software Foundation, 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 1, 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. */
28 #include "lwlib-Xaw.h"
30 #include <X11/StringDefs.h>
31 #include <X11/IntrinsicP.h>
32 #include <X11/CoreP.h>
33 #include <X11/Shell.h>
35 #ifdef LWLIB_SCROLLBARS_ATHENA
36 #include <X11/Xaw/Scrollbar.h>
38 #ifdef LWLIB_DIALOGS_ATHENA
39 #include <X11/Xaw/Dialog.h>
40 #include <X11/Xaw/Form.h>
41 #include <X11/Xaw/Command.h>
42 #include <X11/Xaw/Label.h>
45 #include <X11/Xatom.h>
47 static void xaw_generic_callback (Widget, XtPointer, XtPointer);
51 lw_xaw_widget_p (Widget widget)
54 #ifdef LWLIB_SCROLLBARS_ATHENA
55 || XtIsSubclass (widget, scrollbarWidgetClass)
57 #ifdef LWLIB_DIALOGS_ATHENA
58 || XtIsSubclass (widget, dialogWidgetClass)
63 #ifdef LWLIB_SCROLLBARS_ATHENA
65 xaw_update_scrollbar (widget_instance *instance, Widget widget,
68 if (val->scrollbar_data)
70 scrollbar_values *data = val->scrollbar_data;
71 float widget_shown, widget_topOfThumb;
72 float new_shown, new_topOfThumb;
75 /* First size and position the scrollbar widget. */
76 XtSetArg (al [0], XtNx, data->scrollbar_x);
77 XtSetArg (al [1], XtNy, data->scrollbar_y);
78 XtSetArg (al [2], XtNwidth, data->scrollbar_width);
79 XtSetArg (al [3], XtNheight, data->scrollbar_height);
80 XtSetValues (widget, al, 4);
82 /* Now size the scrollbar's slider. */
83 XtSetArg (al [0], XtNtopOfThumb, &widget_topOfThumb);
84 XtSetArg (al [1], XtNshown, &widget_shown);
85 XtGetValues (widget, al, 2);
87 new_shown = (double) data->slider_size /
88 (double) (data->maximum - data->minimum);
90 new_topOfThumb = (double) (data->slider_position - data->minimum) /
91 (double) (data->maximum - data->minimum);
95 else if (new_shown < 0)
98 if (new_topOfThumb > 1.0)
100 else if (new_topOfThumb < 0)
103 if (new_shown != widget_shown || new_topOfThumb != widget_topOfThumb)
104 XawScrollbarSetThumb (widget, new_topOfThumb, new_shown);
107 #endif /* LWLIB_SCROLLBARS_ATHENA */
110 xaw_update_one_widget (widget_instance *instance, Widget widget,
111 widget_value *val, Boolean deep_p)
115 #ifdef LWLIB_SCROLLBARS_ATHENA
116 else if (XtIsSubclass (widget, scrollbarWidgetClass))
118 xaw_update_scrollbar (instance, widget, val);
121 #ifdef LWLIB_DIALOGS_ATHENA
122 else if (XtIsSubclass (widget, dialogWidgetClass))
125 XtSetArg (al [0], XtNlabel, val->contents->value);
126 XtSetValues (widget, al, 1);
128 else if (XtIsSubclass (widget, commandWidgetClass))
132 XtSetArg (al [0], XtNborderWidth, &bw);
133 XtGetValues (widget, al, 1);
135 #ifndef LWLIB_DIALOGS_ATHENA3D
137 /* Don't let buttons end up with 0 borderwidth, that's ugly...
138 Yeah, all this should really be done through app-defaults files
139 or fallback resources, but that's a whole different can of worms
140 that I don't feel like opening right now. Making Athena widgets
141 not look like shit is just entirely too much work.
144 XtSetArg (al [0], XtNborderWidth, 1);
145 XtSetValues (widget, al, 1);
147 #endif /* ! LWLIB_DIALOGS_ATHENA3D */
149 XtSetArg (al [0], XtNlabel, val->value);
150 XtSetArg (al [1], XtNsensitive, val->enabled);
151 /* Force centered button text. See above. */
152 XtSetArg (al [2], XtNjustify, XtJustifyCenter);
153 XtSetValues (widget, al, 3);
155 XtRemoveAllCallbacks (widget, XtNcallback);
156 XtAddCallback (widget, XtNcallback, xaw_generic_callback, instance);
158 #endif /* LWLIB_DIALOGS_ATHENA */
162 xaw_update_one_value (widget_instance *instance, Widget widget,
165 /* This function is not used by the scrollbars and those are the only
166 Athena widget implemented at the moment so do nothing. */
171 xaw_destroy_instance (widget_instance *instance)
173 #ifdef LWLIB_DIALOGS_ATHENA
174 if (XtIsSubclass (instance->widget, dialogWidgetClass))
175 /* Need to destroy the Shell too. */
176 XtDestroyWidget (XtParent (instance->widget));
179 XtDestroyWidget (instance->widget);
183 xaw_popup_menu (Widget widget, XEvent *event)
185 /* An Athena menubar has not been implemented. */
190 xaw_pop_instance (widget_instance *instance, Boolean up)
192 Widget widget = instance->widget;
196 #ifdef LWLIB_DIALOGS_ATHENA
197 if (XtIsSubclass (widget, dialogWidgetClass))
199 /* For dialogs, we need to call XtPopup on the parent instead
200 of calling XtManageChild on the widget.
201 Also we need to hack the shell's WM_PROTOCOLS to get it to
202 understand what the close box is supposed to do!!
204 Display *dpy = XtDisplay (widget);
205 Widget shell = XtParent (widget);
208 props [i++] = XInternAtom (dpy, "WM_DELETE_WINDOW", False);
209 XChangeProperty (dpy, XtWindow (shell),
210 XInternAtom (dpy, "WM_PROTOCOLS", False),
211 XA_ATOM, 32, PropModeAppend,
212 (unsigned char *) props, i);
214 /* Center the widget in its parent. Why isn't this kind of crap
215 done automatically? I thought toolkits were supposed to make
219 unsigned int x, y, w, h;
220 Widget topmost = instance->parent;
221 w = shell->core.width;
222 h = shell->core.height;
223 while (topmost->core.parent &&
224 XtIsRealized (topmost->core.parent) &&
225 /* HAVE_SESSION adds an unmapped parent widget that
226 we should ignore here. */
227 topmost->core.parent->core.mapped_when_managed)
228 topmost = topmost->core.parent;
229 if (topmost->core.width < w) x = topmost->core.x;
230 else x = topmost->core.x + ((topmost->core.width - w) / 2);
231 if (topmost->core.height < h) y = topmost->core.y;
232 else y = topmost->core.y + ((topmost->core.height - h) / 2);
233 XtMoveWidget (shell, x, y);
236 /* Finally, pop it up. */
237 XtPopup (shell, XtGrabNonexclusive);
240 #endif /* LWLIB_DIALOGS_ATHENA */
241 XtManageChild (widget);
245 #ifdef LWLIB_DIALOGS_ATHENA
246 if (XtIsSubclass (widget, dialogWidgetClass))
247 XtUnmanageChild (XtParent (widget));
250 XtUnmanageChild (widget);
255 #ifdef LWLIB_DIALOGS_ATHENA
258 static char overrideTrans[] =
259 "<Message>WM_PROTOCOLS: lwlib_delete_dialog()";
260 static XtActionProc wm_delete_window (Widget shell, XtPointer closure,
261 XtPointer call_data);
262 static XtActionsRec xaw_actions [] = {
263 {"lwlib_delete_dialog", (XtActionProc) wm_delete_window}
265 static Boolean actions_initted = False;
268 make_dialog (CONST char* name, Widget parent, Boolean pop_up_p,
269 CONST char* shell_title, CONST char* icon_name,
270 Boolean text_input_slot,
271 Boolean radio_box, Boolean list,
272 int left_buttons, int right_buttons)
277 char button_name [255];
281 XtTranslations override;
283 if (! pop_up_p) abort (); /* not implemented */
284 if (text_input_slot) abort (); /* not implemented */
285 if (radio_box) abort (); /* not implemented */
286 if (list) abort (); /* not implemented */
288 if (! actions_initted)
290 XtAppContext app = XtWidgetToApplicationContext (parent);
291 XtAppAddActions (app, xaw_actions,
292 sizeof (xaw_actions) / sizeof (xaw_actions[0]));
293 actions_initted = True;
296 override = XtParseTranslationTable (overrideTrans);
299 XtSetArg (av[ac], XtNtitle, shell_title); ac++;
300 XtSetArg (av[ac], XtNallowShellResize, True); ac++;
301 XtSetArg (av[ac], XtNtransientFor, parent); ac++;
302 shell = XtCreatePopupShell ("dialog", transientShellWidgetClass,
304 XtOverrideTranslations (shell, override);
307 dialog = XtCreateManagedWidget (name, dialogWidgetClass, shell, av, ac);
311 for (i = 0; i < left_buttons; i++)
314 XtSetArg (av [ac], XtNfromHoriz, button); ac++;
315 XtSetArg (av [ac], XtNleft, XtChainLeft); ac++;
316 XtSetArg (av [ac], XtNright, XtChainLeft); ac++;
317 XtSetArg (av [ac], XtNtop, XtChainBottom); ac++;
318 XtSetArg (av [ac], XtNbottom, XtChainBottom); ac++;
319 XtSetArg (av [ac], XtNresizable, True); ac++;
320 sprintf (button_name, "button%d", ++bc);
321 button = XtCreateManagedWidget (button_name, commandWidgetClass,
326 /* Create a separator
328 I want the separator to take up the slack between the buttons on
329 the right and the buttons on the left (that is I want the buttons
330 after the separator to be packed against the right edge of the
331 window) but I can't seem to make it do it.
334 XtSetArg (av [ac], XtNfromHoriz, button); ac++;
335 /* XtSetArg (av [ac], XtNfromVert, XtNameToWidget (dialog, "label")); ac++; */
336 XtSetArg (av [ac], XtNleft, XtChainLeft); ac++;
337 XtSetArg (av [ac], XtNright, XtChainRight); ac++;
338 XtSetArg (av [ac], XtNtop, XtChainBottom); ac++;
339 XtSetArg (av [ac], XtNbottom, XtChainBottom); ac++;
340 XtSetArg (av [ac], XtNlabel, ""); ac++;
341 XtSetArg (av [ac], XtNwidth, 30); ac++; /* #### aaack!! */
342 XtSetArg (av [ac], XtNborderWidth, 0); ac++;
343 XtSetArg (av [ac], XtNshapeStyle, XmuShapeRectangle); ac++;
344 XtSetArg (av [ac], XtNresizable, False); ac++;
345 XtSetArg (av [ac], XtNsensitive, False); ac++;
346 button = XtCreateManagedWidget ("separator",
347 /* labelWidgetClass, */
348 /* This has to be Command to fake out
349 the Dialog widget... */
353 for (i = 0; i < right_buttons; i++)
356 XtSetArg (av [ac], XtNfromHoriz, button); ac++;
357 XtSetArg (av [ac], XtNleft, XtChainRight); ac++;
358 XtSetArg (av [ac], XtNright, XtChainRight); ac++;
359 XtSetArg (av [ac], XtNtop, XtChainBottom); ac++;
360 XtSetArg (av [ac], XtNbottom, XtChainBottom); ac++;
361 XtSetArg (av [ac], XtNresizable, True); ac++;
362 sprintf (button_name, "button%d", ++bc);
363 button = XtCreateManagedWidget (button_name, commandWidgetClass,
371 xaw_create_dialog (widget_instance* instance)
373 char *name = instance->info->type;
374 Widget parent = instance->parent;
376 Boolean pop_up_p = instance->pop_up_p;
377 CONST char *shell_name = 0;
378 CONST char *icon_name = 0;
379 Boolean text_input_slot = False;
380 Boolean radio_box = False;
381 Boolean list = False;
383 int left_buttons = 0;
384 int right_buttons = 1;
388 icon_name = "dbox-error";
389 shell_name = "Error";
393 icon_name = "dbox-info";
394 shell_name = "Information";
399 icon_name = "dbox-question";
400 shell_name = "Prompt";
404 text_input_slot = True;
405 icon_name = "dbox-question";
406 shell_name = "Prompt";
410 icon_name = "dbox-question";
411 shell_name = "Question";
415 total_buttons = name [1] - '0';
417 if (name [3] == 'T' || name [3] == 't')
419 text_input_slot = False;
423 right_buttons = name [4] - '0';
425 left_buttons = total_buttons - right_buttons;
427 widget = make_dialog (name, parent, pop_up_p,
428 shell_name, icon_name, text_input_slot, radio_box,
429 list, left_buttons, right_buttons);
433 #endif /* LWLIB_DIALOGS_ATHENA */
437 xaw_generic_callback (Widget widget, XtPointer closure, XtPointer call_data)
439 widget_instance *instance = (widget_instance *) closure;
440 Widget instance_widget;
444 lw_internal_update_other_instances (widget, closure, call_data);
448 if (widget->core.being_destroyed)
451 instance_widget = instance->widget;
452 if (!instance_widget)
455 id = instance->info->id;
461 XtSetArg (al [0], XtNuserData, &user_data);
462 XtGetValues (widget, al, 1);
465 /* Damn! Athena doesn't give us a way to hang our own data on the
466 buttons, so we have to go find it... I guess this assumes that
467 all instances of a button have the same call data. */
469 widget_value *val = instance->info->val->contents;
470 char *name = XtName (widget);
473 if (val->name && !strcmp (val->name, name))
478 user_data = val->call_data;
482 if (instance->info->selection_cb)
483 instance->info->selection_cb (widget, id, user_data);
486 #ifdef LWLIB_DIALOGS_ATHENA
489 wm_delete_window (Widget shell, XtPointer closure, XtPointer call_data)
495 if (! XtIsSubclass (shell, shellWidgetClass))
497 XtSetArg (al [0], XtNchildren, &kids);
498 XtGetValues (shell, al, 1);
502 if (! XtIsSubclass (widget, dialogWidgetClass))
504 id = lw_get_widget_id (widget);
508 widget_info *info = lw_get_widget_info (id);
509 if (! info) abort ();
510 if (info->selection_cb)
511 info->selection_cb (widget, id, (XtPointer) -1);
514 lw_destroy_all_widgets (id);
518 #endif /* LWLIB_DIALOGS_ATHENA */
523 #ifdef LWLIB_SCROLLBARS_ATHENA
525 xaw_scrollbar_scroll (Widget widget, XtPointer closure, XtPointer call_data)
527 widget_instance *instance = (widget_instance *) closure;
529 scroll_event event_data;
531 if (!instance || widget->core.being_destroyed)
534 id = instance->info->id;
535 event_data.slider_value = (int) call_data;
538 if ((int) call_data > 0)
539 /* event_data.action = SCROLLBAR_PAGE_DOWN;*/
540 event_data.action = SCROLLBAR_LINE_DOWN;
542 /* event_data.action = SCROLLBAR_PAGE_UP;*/
543 event_data.action = SCROLLBAR_LINE_UP;
545 if (instance->info->pre_activate_cb)
546 instance->info->pre_activate_cb (widget, id, (XtPointer) &event_data);
550 xaw_scrollbar_jump (Widget widget, XtPointer closure, XtPointer call_data)
552 widget_instance *instance = (widget_instance *) closure;
554 scroll_event event_data;
555 scrollbar_values *val =
556 (scrollbar_values *) instance->info->val->scrollbar_data;
559 if (!instance || widget->core.being_destroyed)
562 id = instance->info->id;
564 percent = * (float *) call_data;
565 event_data.slider_value =
566 (int) (percent * (float) (val->maximum - val->minimum)) + val->minimum;
569 event_data.action = SCROLLBAR_DRAG;
571 if (instance->info->pre_activate_cb)
572 instance->info->pre_activate_cb (widget, id, (XtPointer) &event_data);
576 xaw_create_scrollbar (widget_instance *instance, int vertical)
581 static XtCallbackRec jumpCallbacks[2] =
582 { {xaw_scrollbar_jump, NULL}, {NULL, NULL} };
584 static XtCallbackRec scrollCallbacks[2] =
585 { {xaw_scrollbar_scroll, NULL}, {NULL, NULL} };
587 jumpCallbacks[0].closure = scrollCallbacks[0].closure = (XtPointer) instance;
589 /* #### This is tacked onto the with and height and completely
590 screws our geometry management. We should probably make the
591 top-level aware of this so that people could have a border but so
592 few people use the Athena scrollbar now that it really isn't
593 worth the effort, at least not at the moment. */
594 XtSetArg (av [ac], XtNborderWidth, 0); ac++;
595 XtSetArg (av [ac], XtNorientation,
596 vertical ? XtorientVertical : XtorientHorizontal); ac++;
597 XtSetArg (av [ac], "jumpProc", jumpCallbacks); ac++;
598 XtSetArg (av [ac], "scrollProc", scrollCallbacks); ac++;
600 return XtCreateWidget (instance->info->name, scrollbarWidgetClass,
601 instance->parent, av, ac);
605 xaw_create_vertical_scrollbar (widget_instance *instance)
607 return xaw_create_scrollbar (instance, 1);
611 xaw_create_horizontal_scrollbar (widget_instance *instance)
613 return xaw_create_scrollbar (instance, 0);
615 #endif /* LWLIB_SCROLLBARS_ATHENA */
617 widget_creation_entry
618 xaw_creation_table [] =
620 #ifdef LWLIB_SCROLLBARS_ATHENA
621 {"vertical-scrollbar", xaw_create_vertical_scrollbar},
622 {"horizontal-scrollbar", xaw_create_horizontal_scrollbar},