Remove un-logged bogus changes from 2000-12-20.
[emacs.git] / lwlib / lwlib-Xaw.c
blob5a1c9d2f268bc1719c22aad0b2f4421b8c95ba06
1 /* The lwlib interface to Athena widgets.
2 Copyright (C) 1993 Chuck Thompson <cthomp@cs.uiuc.edu>
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)
9 any later version.
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 GNU Emacs; 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. */
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
25 #include <stdio.h>
27 #include "lwlib-Xaw.h"
29 #include <X11/StringDefs.h>
30 #include <X11/IntrinsicP.h>
31 #include <X11/CoreP.h>
32 #include <X11/Shell.h>
34 #include <X11/Xaw/Scrollbar.h>
35 #include <X11/Xaw/Paned.h>
36 #include <X11/Xaw/Dialog.h>
37 #include <X11/Xaw/Form.h>
38 #include <X11/Xaw/Command.h>
39 #include <X11/Xaw/Label.h>
41 #include <X11/Xatom.h>
43 static void xaw_generic_callback (/*Widget, XtPointer, XtPointer*/);
46 Boolean
47 lw_xaw_widget_p (widget)
48 Widget widget;
50 return (XtIsSubclass (widget, scrollbarWidgetClass) ||
51 XtIsSubclass (widget, dialogWidgetClass));
54 static void
55 xaw_update_scrollbar (instance, widget, val)
56 widget_instance *instance;
57 Widget widget;
58 widget_value *val;
60 #if 0
61 if (val->scrollbar_data)
63 scrollbar_values *data = val->scrollbar_data;
64 Dimension height, width;
65 Dimension pos_x, pos_y;
66 int widget_shown, widget_topOfThumb;
67 float new_shown, new_topOfThumb;
69 XtVaGetValues (widget,
70 XtNheight, &height,
71 XtNwidth, &width,
72 XtNx, &pos_x,
73 XtNy, &pos_y,
74 XtNtopOfThumb, &widget_topOfThumb,
75 XtNshown, &widget_shown,
76 NULL);
79 * First size and position the scrollbar widget.
80 * We need to position it to second-guess the Paned widget's notion
81 * of what should happen when the WMShell gets resized.
83 if (height != data->scrollbar_height || pos_y != data->scrollbar_pos)
85 XtConfigureWidget (widget, pos_x, data->scrollbar_pos,
86 width, data->scrollbar_height, 0);
88 XtVaSetValues (widget,
89 XtNlength, data->scrollbar_height,
90 XtNthickness, width,
91 NULL);
95 * Now the size the scrollbar's slider.
97 new_shown = (float) data->slider_size /
98 (float) (data->maximum - data->minimum);
100 new_topOfThumb = (float) (data->slider_position - data->minimum) /
101 (float) (data->maximum - data->minimum);
103 if (new_shown > 1.0)
104 new_shown = 1.0;
105 if (new_shown < 0)
106 new_shown = 0;
108 if (new_topOfThumb > 1.0)
109 new_topOfThumb = 1.0;
110 if (new_topOfThumb < 0)
111 new_topOfThumb = 0;
113 if (new_shown != widget_shown || new_topOfThumb != widget_topOfThumb)
114 XawScrollbarSetThumb (widget, new_topOfThumb, new_shown);
116 #endif
119 void
120 xaw_update_one_widget (instance, widget, val, deep_p)
121 widget_instance *instance;
122 Widget widget;
123 widget_value *val;
124 Boolean deep_p;
126 #if 0
127 if (XtIsSubclass (widget, scrollbarWidgetClass))
129 xaw_update_scrollbar (instance, widget, val);
131 #endif
132 if (XtIsSubclass (widget, dialogWidgetClass))
134 Arg al[1];
135 int ac = 0;
136 XtSetArg (al[ac], XtNlabel, val->contents->value); ac++;
137 XtSetValues (widget, al, ac);
139 else if (XtIsSubclass (widget, commandWidgetClass))
141 Dimension bw = 0;
142 Arg al[3];
144 XtVaGetValues (widget, XtNborderWidth, &bw, NULL);
145 if (bw == 0)
146 /* Don't let buttons end up with 0 borderwidth, that's ugly...
147 Yeah, all this should really be done through app-defaults files
148 or fallback resources, but that's a whole different can of worms
149 that I don't feel like opening right now. Making Athena widgets
150 not look like shit is just entirely too much work.
153 XtSetArg (al[0], XtNborderWidth, 1);
154 XtSetValues (widget, al, 1);
157 XtSetArg (al[0], XtNlabel, val->value);
158 XtSetArg (al[1], XtNsensitive, val->enabled);
159 /* Force centered button text. Se above. */
160 XtSetArg (al[2], XtNjustify, XtJustifyCenter);
161 XtSetValues (widget, al, 3);
162 XtRemoveAllCallbacks (widget, XtNcallback);
163 XtAddCallback (widget, XtNcallback, xaw_generic_callback, instance);
167 void
168 xaw_update_one_value (instance, widget, val)
169 widget_instance *instance;
170 Widget widget;
171 widget_value *val;
173 /* This function is not used by the scrollbars and those are the only
174 Athena widget implemented at the moment so do nothing. */
175 return;
178 void
179 xaw_destroy_instance (instance)
180 widget_instance *instance;
182 if (XtIsSubclass (instance->widget, dialogWidgetClass))
183 /* Need to destroy the Shell too. */
184 XtDestroyWidget (XtParent (instance->widget));
185 else
186 XtDestroyWidget (instance->widget);
189 void
190 xaw_popup_menu (widget, event)
191 Widget widget;
192 XEvent *event;
194 /* An Athena menubar has not been implemented. */
195 return;
198 void
199 xaw_pop_instance (instance, up)
200 widget_instance *instance;
201 Boolean up;
203 Widget widget = instance->widget;
205 if (up)
207 if (XtIsSubclass (widget, dialogWidgetClass))
209 /* For dialogs, we need to call XtPopup on the parent instead
210 of calling XtManageChild on the widget.
211 Also we need to hack the shell's WM_PROTOCOLS to get it to
212 understand what the close box is supposed to do!!
214 Display *dpy = XtDisplay (widget);
215 Widget shell = XtParent (widget);
216 Atom props [2];
217 int i = 0;
218 props [i++] = XInternAtom (dpy, "WM_DELETE_WINDOW", False);
219 XChangeProperty (dpy, XtWindow (shell),
220 XInternAtom (dpy, "WM_PROTOCOLS", False),
221 XA_ATOM, 32, PropModeAppend,
222 (unsigned char *) props, i);
224 /* Center the widget in its parent. Why isn't this kind of crap
225 done automatically? I thought toolkits were supposed to make
226 life easier?
229 unsigned int x, y, w, h;
230 Widget topmost = instance->parent;
231 Arg args[2];
233 w = shell->core.width;
234 h = shell->core.height;
235 while (topmost->core.parent && XtIsRealized (topmost->core.parent))
236 topmost = topmost->core.parent;
237 if (topmost->core.width < w) x = topmost->core.x;
238 else x = topmost->core.x + ((topmost->core.width - w) / 2);
239 if (topmost->core.height < h) y = topmost->core.y;
240 else y = topmost->core.y + ((topmost->core.height - h) / 2);
241 /* Using XtMoveWidget caused the widget to come
242 out in the wrong place with vtwm.
243 Question of virtual vs real coords, perhaps. */
244 XtSetArg (args[0], XtNx, x);
245 XtSetArg (args[1], XtNy, y);
246 XtSetValues (shell, args, 2);
249 /* Finally, pop it up. */
250 XtPopup (shell, XtGrabNonexclusive);
252 else
253 XtManageChild (widget);
255 else
257 if (XtIsSubclass (widget, dialogWidgetClass))
258 XtUnmanageChild (XtParent (widget));
259 else
260 XtUnmanageChild (widget);
265 /* Dialog boxes */
267 static char overrideTrans[] =
268 "<Message>WM_PROTOCOLS: lwlib_delete_dialog()";
269 static void wm_delete_window();
270 static XtActionsRec xaw_actions [] = {
271 {"lwlib_delete_dialog", wm_delete_window}
273 static Boolean actions_initted = False;
275 static Widget
276 make_dialog (name, parent, pop_up_p, shell_title, icon_name, text_input_slot, radio_box, list, left_buttons, right_buttons)
277 char* name;
278 Widget parent;
279 Boolean pop_up_p;
280 char* shell_title;
281 char* icon_name;
282 Boolean text_input_slot;
283 Boolean radio_box;
284 Boolean list;
285 int left_buttons;
286 int right_buttons;
288 Arg av [20];
289 int ac = 0;
290 int i, bc;
291 char button_name [255];
292 Widget shell;
293 Widget dialog;
294 Widget button;
295 XtTranslations override;
297 if (! pop_up_p) abort (); /* not implemented */
298 if (text_input_slot) abort (); /* not implemented */
299 if (radio_box) abort (); /* not implemented */
300 if (list) abort (); /* not implemented */
302 if (! actions_initted)
304 XtAppContext app = XtWidgetToApplicationContext (parent);
305 XtAppAddActions (app, xaw_actions,
306 sizeof (xaw_actions) / sizeof (xaw_actions[0]));
307 actions_initted = True;
310 override = XtParseTranslationTable (overrideTrans);
312 ac = 0;
313 XtSetArg (av[ac], XtNtitle, shell_title); ac++;
314 XtSetArg (av[ac], XtNallowShellResize, True); ac++;
316 /* Don't allow any geometry request from the user. */
317 XtSetArg (av[ac], XtNgeometry, 0); ac++;
319 shell = XtCreatePopupShell ("dialog", transientShellWidgetClass,
320 parent, av, ac);
321 XtOverrideTranslations (shell, override);
323 ac = 0;
324 dialog = XtCreateManagedWidget (name, dialogWidgetClass, shell, av, ac);
326 bc = 0;
327 button = 0;
328 for (i = 0; i < left_buttons; i++)
330 ac = 0;
331 XtSetArg (av [ac], XtNfromHoriz, button); ac++;
332 XtSetArg (av [ac], XtNleft, XtChainLeft); ac++;
333 XtSetArg (av [ac], XtNright, XtChainLeft); ac++;
334 XtSetArg (av [ac], XtNtop, XtChainBottom); ac++;
335 XtSetArg (av [ac], XtNbottom, XtChainBottom); ac++;
336 XtSetArg (av [ac], XtNresizable, True); ac++;
337 sprintf (button_name, "button%d", ++bc);
338 button = XtCreateManagedWidget (button_name, commandWidgetClass,
339 dialog, av, ac);
341 if (right_buttons)
343 /* Create a separator
345 I want the separator to take up the slack between the buttons on
346 the right and the buttons on the left (that is I want the buttons
347 after the separator to be packed against the right edge of the
348 window) but I can't seem to make it do it.
350 ac = 0;
351 XtSetArg (av [ac], XtNfromHoriz, button); ac++;
352 /* XtSetArg (av [ac], XtNfromVert, XtNameToWidget (dialog, "label")); ac++; */
353 XtSetArg (av [ac], XtNleft, XtChainLeft); ac++;
354 XtSetArg (av [ac], XtNright, XtChainRight); ac++;
355 XtSetArg (av [ac], XtNtop, XtChainBottom); ac++;
356 XtSetArg (av [ac], XtNbottom, XtChainBottom); ac++;
357 XtSetArg (av [ac], XtNlabel, ""); ac++;
358 XtSetArg (av [ac], XtNwidth, 30); ac++; /* #### aaack!! */
359 XtSetArg (av [ac], XtNborderWidth, 0); ac++;
360 XtSetArg (av [ac], XtNshapeStyle, XmuShapeRectangle); ac++;
361 XtSetArg (av [ac], XtNresizable, False); ac++;
362 XtSetArg (av [ac], XtNsensitive, False); ac++;
363 button = XtCreateManagedWidget ("separator",
364 /* labelWidgetClass, */
365 /* This has to be Command to fake out
366 the Dialog widget... */
367 commandWidgetClass,
368 dialog, av, ac);
370 for (i = 0; i < right_buttons; i++)
372 ac = 0;
373 XtSetArg (av [ac], XtNfromHoriz, button); ac++;
374 XtSetArg (av [ac], XtNleft, XtChainRight); ac++;
375 XtSetArg (av [ac], XtNright, XtChainRight); ac++;
376 XtSetArg (av [ac], XtNtop, XtChainBottom); ac++;
377 XtSetArg (av [ac], XtNbottom, XtChainBottom); ac++;
378 XtSetArg (av [ac], XtNresizable, True); ac++;
379 sprintf (button_name, "button%d", ++bc);
380 button = XtCreateManagedWidget (button_name, commandWidgetClass,
381 dialog, av, ac);
384 return dialog;
387 Widget
388 xaw_create_dialog (instance)
389 widget_instance* instance;
391 char *name = instance->info->type;
392 Widget parent = instance->parent;
393 Widget widget;
394 Boolean pop_up_p = instance->pop_up_p;
395 char *shell_name = 0;
396 char *icon_name;
397 Boolean text_input_slot = False;
398 Boolean radio_box = False;
399 Boolean list = False;
400 int total_buttons;
401 int left_buttons = 0;
402 int right_buttons = 1;
404 switch (name [0]) {
405 case 'E': case 'e':
406 icon_name = "dbox-error";
407 shell_name = "Error";
408 break;
410 case 'I': case 'i':
411 icon_name = "dbox-info";
412 shell_name = "Information";
413 break;
415 case 'L': case 'l':
416 list = True;
417 icon_name = "dbox-question";
418 shell_name = "Prompt";
419 break;
421 case 'P': case 'p':
422 text_input_slot = True;
423 icon_name = "dbox-question";
424 shell_name = "Prompt";
425 break;
427 case 'Q': case 'q':
428 icon_name = "dbox-question";
429 shell_name = "Question";
430 break;
433 total_buttons = name [1] - '0';
435 if (name [3] == 'T' || name [3] == 't')
437 text_input_slot = False;
438 radio_box = True;
440 else if (name [3])
441 right_buttons = name [4] - '0';
443 left_buttons = total_buttons - right_buttons;
445 widget = make_dialog (name, parent, pop_up_p,
446 shell_name, icon_name, text_input_slot, radio_box,
447 list, left_buttons, right_buttons);
449 return widget;
453 static void
454 xaw_generic_callback (widget, closure, call_data)
455 Widget widget;
456 XtPointer closure;
457 XtPointer call_data;
459 widget_instance *instance = (widget_instance *) closure;
460 Widget instance_widget;
461 LWLIB_ID id;
462 XtPointer user_data;
464 lw_internal_update_other_instances (widget, closure, call_data);
466 if (! instance)
467 return;
468 if (widget->core.being_destroyed)
469 return;
471 instance_widget = instance->widget;
472 if (!instance_widget)
473 return;
475 id = instance->info->id;
477 #if 0
478 user_data = NULL;
479 XtVaGetValues (widget, XtNuserData, &user_data, NULL);
480 #else
481 /* Damn! Athena doesn't give us a way to hang our own data on the
482 buttons, so we have to go find it... I guess this assumes that
483 all instances of a button have the same call data. */
485 widget_value *val = instance->info->val->contents;
486 char *name = XtName (widget);
487 while (val)
489 if (val->name && !strcmp (val->name, name))
490 break;
491 val = val->next;
493 if (! val) abort ();
494 user_data = val->call_data;
496 #endif
498 if (instance->info->selection_cb)
499 instance->info->selection_cb (widget, id, user_data);
502 static void
503 wm_delete_window (shell, closure, call_data)
504 Widget shell;
505 XtPointer closure;
506 XtPointer call_data;
508 LWLIB_ID id;
509 Cardinal nkids;
510 int i;
511 Widget *kids = 0;
512 Widget widget;
513 if (! XtIsSubclass (shell, shellWidgetClass))
514 abort ();
515 XtVaGetValues (shell, XtNnumChildren, &nkids, NULL);
516 XtVaGetValues (shell, XtNchildren, &kids, NULL);
517 if (!kids || !*kids)
518 abort ();
519 for (i = 0; i < nkids; i++)
521 widget = kids[i];
522 if (XtIsSubclass (widget, dialogWidgetClass))
523 break;
525 id = lw_get_widget_id (widget);
526 if (! id) abort ();
529 widget_info *info = lw_get_widget_info (id);
530 if (! info) abort ();
531 if (info->selection_cb)
532 info->selection_cb (widget, id, (XtPointer) -1);
535 lw_destroy_all_widgets (id);
539 /* Scrollbars */
541 static void
542 xaw_scrollbar_scroll (widget, closure, call_data)
543 Widget widget;
544 XtPointer closure;
545 XtPointer call_data;
547 #if 0
548 widget_instance *instance = (widget_instance *) closure;
549 LWLIB_ID id;
550 scroll_event event_data;
552 if (!instance || widget->core.being_destroyed)
553 return;
555 id = instance->info->id;
556 event_data.slider_value = 0;
557 event_data.time = 0;
559 if ((int) call_data > 0)
560 event_data.action = SCROLLBAR_PAGE_DOWN;
561 else
562 event_data.action = SCROLLBAR_PAGE_UP;
564 if (instance->info->pre_activate_cb)
565 instance->info->pre_activate_cb (widget, id, (XtPointer) &event_data);
566 #endif
569 static void
570 xaw_scrollbar_jump (widget, closure, call_data)
571 Widget widget;
572 XtPointer closure;
573 XtPointer call_data;
575 #if 0
576 widget_instance *instance = (widget_instance *) closure;
577 LWLIB_ID id;
578 scroll_event event_data;
579 scrollbar_values *val =
580 (scrollbar_values *) instance->info->val->scrollbar_data;
581 float percent;
583 if (!instance || widget->core.being_destroyed)
584 return;
586 id = instance->info->id;
588 percent = * (float *) call_data;
589 event_data.slider_value =
590 (int) (percent * (float) (val->maximum - val->minimum)) + val->minimum;
592 event_data.time = 0;
593 event_data.action = SCROLLBAR_DRAG;
595 if (instance->info->pre_activate_cb)
596 instance->info->pre_activate_cb (widget, id, (XtPointer) &event_data);
597 #endif
600 static Widget
601 xaw_create_scrollbar (instance)
602 widget_instance *instance;
604 #if 0
605 Arg av[20];
606 int ac = 0;
607 Dimension width;
608 Widget scrollbar;
610 XtVaGetValues (instance->parent, XtNwidth, &width, NULL);
612 XtSetArg (av[ac], XtNshowGrip, 0); ac++;
613 XtSetArg (av[ac], XtNresizeToPreferred, 1); ac++;
614 XtSetArg (av[ac], XtNallowResize, True); ac++;
615 XtSetArg (av[ac], XtNskipAdjust, True); ac++;
616 XtSetArg (av[ac], XtNwidth, width); ac++;
617 XtSetArg (av[ac], XtNmappedWhenManaged, True); ac++;
619 scrollbar =
620 XtCreateWidget (instance->info->name, scrollbarWidgetClass,
621 instance->parent, av, ac);
623 /* We have to force the border width to be 0 otherwise the
624 geometry manager likes to start looping for awhile... */
625 XtVaSetValues (scrollbar, XtNborderWidth, 0, NULL);
627 XtRemoveAllCallbacks (scrollbar, "jumpProc");
628 XtRemoveAllCallbacks (scrollbar, "scrollProc");
630 XtAddCallback (scrollbar, "jumpProc", xaw_scrollbar_jump,
631 (XtPointer) instance);
632 XtAddCallback (scrollbar, "scrollProc", xaw_scrollbar_scroll,
633 (XtPointer) instance);
635 return scrollbar;
636 #endif
639 static Widget
640 xaw_create_main (instance)
641 widget_instance *instance;
643 Arg al[1];
644 int ac;
646 /* Create a vertical Paned to hold menubar */
647 ac = 0;
648 XtSetArg (al[ac], XtNborderWidth, 0); ac++;
649 return XtCreateWidget (instance->info->name, panedWidgetClass,
650 instance->parent, al, ac);
653 widget_creation_entry
654 xaw_creation_table [] =
656 {"scrollbar", xaw_create_scrollbar},
657 {"main", xaw_create_main},
658 {NULL, NULL}