Contents in 1999-06-04-13 of release-21-2.
[chise/xemacs-chise.git.1] / lwlib / lwlib-Xaw.c
1 /* The lwlib interface to Athena widgets.
2    Copyright (C) 1993, 1994 Free Software Foundation, 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 1, 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
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.  */
20
21 #include <config.h>
22 #include <stdio.h>
23
24 #ifdef STDC_HEADERS
25 #include <stdlib.h>
26 #endif
27
28 #include "lwlib-Xaw.h"
29
30 #include <X11/StringDefs.h>
31 #include <X11/IntrinsicP.h>
32 #include <X11/CoreP.h>
33 #include <X11/Shell.h>
34
35 #ifdef LWLIB_SCROLLBARS_ATHENA
36 #include <X11/Xaw/Scrollbar.h>
37 #endif
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>
43 #endif
44
45 #include <X11/Xatom.h>
46
47 static void xaw_generic_callback (Widget, XtPointer, XtPointer);
48
49
50 Boolean
51 lw_xaw_widget_p (Widget widget)
52 {
53   return (0
54 #ifdef LWLIB_SCROLLBARS_ATHENA
55           || XtIsSubclass (widget, scrollbarWidgetClass)
56 #endif
57 #ifdef LWLIB_DIALOGS_ATHENA
58           || XtIsSubclass (widget, dialogWidgetClass)
59 #endif
60           );
61 }
62
63 #ifdef LWLIB_SCROLLBARS_ATHENA
64 static void
65 xaw_update_scrollbar (widget_instance *instance, Widget widget,
66                       widget_value *val)
67 {
68   if (val->scrollbar_data)
69     {
70       scrollbar_values *data = val->scrollbar_data;
71       float widget_shown, widget_topOfThumb;
72       float new_shown, new_topOfThumb;
73       Arg al [10];
74
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);
81
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);
86
87       new_shown = (double) data->slider_size /
88         (double) (data->maximum - data->minimum);
89
90       new_topOfThumb = (double) (data->slider_position - data->minimum) /
91         (double) (data->maximum - data->minimum);
92
93       if (new_shown > 1.0)
94         new_shown = 1.0;
95       else if (new_shown < 0)
96         new_shown = 0;
97
98       if (new_topOfThumb > 1.0)
99         new_topOfThumb = 1.0;
100       else if (new_topOfThumb < 0)
101         new_topOfThumb = 0;
102
103       if (new_shown != widget_shown || new_topOfThumb != widget_topOfThumb)
104         XawScrollbarSetThumb (widget, new_topOfThumb, new_shown);
105     }
106 }
107 #endif /* LWLIB_SCROLLBARS_ATHENA */
108
109 void
110 xaw_update_one_widget (widget_instance *instance, Widget widget,
111                        widget_value *val, Boolean deep_p)
112 {
113   if (0)
114     ;
115 #ifdef LWLIB_SCROLLBARS_ATHENA
116   else if (XtIsSubclass (widget, scrollbarWidgetClass))
117     {
118       xaw_update_scrollbar (instance, widget, val);
119     }
120 #endif
121 #ifdef LWLIB_DIALOGS_ATHENA
122   else if (XtIsSubclass (widget, dialogWidgetClass))
123       {
124         Arg al [1];
125         XtSetArg (al [0], XtNlabel, val->contents->value);
126         XtSetValues (widget, al, 1);
127       }
128   else if (XtIsSubclass (widget, commandWidgetClass))
129     {
130       Dimension bw = 0;
131       Arg al [3];
132       XtSetArg (al [0], XtNborderWidth, &bw);
133       XtGetValues (widget, al, 1);
134
135 #ifndef LWLIB_DIALOGS_ATHENA3D
136       if (bw == 0)
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.
142          */
143         {
144           XtSetArg (al [0], XtNborderWidth, 1);
145           XtSetValues (widget, al, 1);
146         }
147 #endif /* ! LWLIB_DIALOGS_ATHENA3D */
148
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);
154
155       XtRemoveAllCallbacks (widget, XtNcallback);
156       XtAddCallback (widget, XtNcallback, xaw_generic_callback, instance);
157     }
158 #endif /* LWLIB_DIALOGS_ATHENA */
159 }
160
161 void
162 xaw_update_one_value (widget_instance *instance, Widget widget,
163                       widget_value *val)
164 {
165   /* This function is not used by the scrollbars and those are the only
166      Athena widget implemented at the moment so do nothing. */
167   return;
168 }
169
170 void
171 xaw_destroy_instance (widget_instance *instance)
172 {
173 #ifdef LWLIB_DIALOGS_ATHENA
174   if (XtIsSubclass (instance->widget, dialogWidgetClass))
175     /* Need to destroy the Shell too. */
176     XtDestroyWidget (XtParent (instance->widget));
177   else
178 #endif
179     XtDestroyWidget (instance->widget);
180 }
181
182 void
183 xaw_popup_menu (Widget widget, XEvent *event)
184 {
185   /* An Athena menubar has not been implemented. */
186   return;
187 }
188
189 void
190 xaw_pop_instance (widget_instance *instance, Boolean up)
191 {
192   Widget widget = instance->widget;
193
194   if (up)
195     {
196 #ifdef LWLIB_DIALOGS_ATHENA
197       if (XtIsSubclass (widget, dialogWidgetClass))
198         {
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!!
203            */
204           Display *dpy = XtDisplay (widget);
205           Widget shell = XtParent (widget);
206           Atom props [2];
207           int i = 0;
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);
213
214           /* Center the widget in its parent.  Why isn't this kind of crap
215              done automatically?  I thought toolkits were supposed to make
216              life easier?
217            */
218           {
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);
234           }
235
236           /* Finally, pop it up. */
237           XtPopup (shell, XtGrabNonexclusive);
238         }
239       else
240 #endif /* LWLIB_DIALOGS_ATHENA */
241         XtManageChild (widget);
242     }
243   else
244     {
245 #ifdef LWLIB_DIALOGS_ATHENA
246       if (XtIsSubclass (widget, dialogWidgetClass))
247         XtUnmanageChild (XtParent (widget));
248       else
249 #endif
250         XtUnmanageChild (widget);
251     }
252 }
253
254 \f
255 #ifdef LWLIB_DIALOGS_ATHENA
256 /* Dialog boxes */
257
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}
264 };
265 static Boolean actions_initted = False;
266
267 static Widget
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)
273 {
274   Arg av [20];
275   int ac = 0;
276   int i, bc;
277   char button_name [255];
278   Widget shell;
279   Widget dialog;
280   Widget button;
281   XtTranslations override;
282
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 */
287
288   if (! actions_initted)
289     {
290       XtAppContext app = XtWidgetToApplicationContext (parent);
291       XtAppAddActions (app, xaw_actions,
292                        sizeof (xaw_actions) / sizeof (xaw_actions[0]));
293       actions_initted = True;
294     }
295
296   override = XtParseTranslationTable (overrideTrans);
297
298   ac = 0;
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,
303                               parent, av, ac);
304   XtOverrideTranslations (shell, override);
305
306   ac = 0;
307   dialog = XtCreateManagedWidget (name, dialogWidgetClass, shell, av, ac);
308
309   bc = 0;
310   button = 0;
311   for (i = 0; i < left_buttons; i++)
312     {
313       ac = 0;
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,
322                                       dialog, av, ac);
323     }
324   if (right_buttons)
325     {
326       /* Create a separator
327
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.  
332        */
333       ac = 0;
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... */
350                                       commandWidgetClass,
351                                       dialog, av, ac);
352     }
353   for (i = 0; i < right_buttons; i++)
354     {
355       ac = 0;
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,
364                                       dialog, av, ac);
365     }
366
367   return dialog;
368 }
369
370 Widget
371 xaw_create_dialog (widget_instance* instance)
372 {
373   char *name = instance->info->type;
374   Widget parent = instance->parent;
375   Widget widget;
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;
382   int total_buttons;
383   int left_buttons = 0;
384   int right_buttons = 1;
385
386   switch (name [0]) {
387   case 'E': case 'e':
388     icon_name = "dbox-error";
389     shell_name = "Error";
390     break;
391
392   case 'I': case 'i':
393     icon_name = "dbox-info";
394     shell_name = "Information";
395     break;
396
397   case 'L': case 'l':
398     list = True;
399     icon_name = "dbox-question";
400     shell_name = "Prompt";
401     break;
402
403   case 'P': case 'p':
404     text_input_slot = True;
405     icon_name = "dbox-question";
406     shell_name = "Prompt";
407     break;
408
409   case 'Q': case 'q':
410     icon_name = "dbox-question";
411     shell_name = "Question";
412     break;
413   }
414   
415   total_buttons = name [1] - '0';
416
417   if (name [3] == 'T' || name [3] == 't')
418     {
419       text_input_slot = False;
420       radio_box = True;
421     }
422   else if (name [3])
423     right_buttons = name [4] - '0';
424   
425   left_buttons = total_buttons - right_buttons;
426   
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);
430
431   return widget;
432 }
433 #endif /* LWLIB_DIALOGS_ATHENA */
434
435
436 static void
437 xaw_generic_callback (Widget widget, XtPointer closure, XtPointer call_data)
438 {
439   widget_instance *instance = (widget_instance *) closure;
440   Widget instance_widget;
441   LWLIB_ID id;
442   XtPointer user_data;
443
444   lw_internal_update_other_instances (widget, closure, call_data);
445
446   if (! instance)
447     return;
448   if (widget->core.being_destroyed)
449     return;
450
451   instance_widget = instance->widget;
452   if (!instance_widget)
453     return;
454
455   id = instance->info->id;
456
457 #if 0
458   user_data = NULL;
459   {
460     Arg al [1];
461     XtSetArg (al [0], XtNuserData, &user_data);
462     XtGetValues (widget, al, 1);
463   }
464 #else
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. */
468   {
469     widget_value *val = instance->info->val->contents;
470     char *name = XtName (widget);
471     while (val)
472       {
473         if (val->name && !strcmp (val->name, name))
474           break;
475         val = val->next;
476       }
477     if (! val) abort ();
478     user_data = val->call_data;
479   }
480 #endif
481
482   if (instance->info->selection_cb)
483     instance->info->selection_cb (widget, id, user_data);
484 }
485
486 #ifdef LWLIB_DIALOGS_ATHENA
487
488 static XtActionProc
489 wm_delete_window (Widget shell, XtPointer closure, XtPointer call_data)
490 {
491   LWLIB_ID id;
492   Widget *kids = 0;
493   Widget widget;
494   Arg al [1];
495   if (! XtIsSubclass (shell, shellWidgetClass))
496     abort ();
497   XtSetArg (al [0], XtNchildren, &kids);
498   XtGetValues (shell, al, 1);
499   if (!kids || !*kids)
500     abort ();
501   widget = kids [0];
502   if (! XtIsSubclass (widget, dialogWidgetClass))
503     abort ();
504   id = lw_get_widget_id (widget);
505   if (! id) abort ();
506
507   {
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);
512   }
513
514   lw_destroy_all_widgets (id);
515   return NULL;
516 }
517
518 #endif /* LWLIB_DIALOGS_ATHENA */
519
520 \f
521 /* Scrollbars */
522
523 #ifdef LWLIB_SCROLLBARS_ATHENA
524 static void
525 xaw_scrollbar_scroll (Widget widget, XtPointer closure, XtPointer call_data)
526 {
527   widget_instance *instance = (widget_instance *) closure;
528   LWLIB_ID id;
529   scroll_event event_data;
530
531   if (!instance || widget->core.being_destroyed)
532     return;
533
534   id = instance->info->id;
535   event_data.slider_value = (int) call_data;
536   event_data.time = 0;
537
538   if ((int) call_data > 0)
539     /* event_data.action = SCROLLBAR_PAGE_DOWN;*/
540     event_data.action = SCROLLBAR_LINE_DOWN;
541   else
542     /* event_data.action = SCROLLBAR_PAGE_UP;*/
543     event_data.action = SCROLLBAR_LINE_UP;
544
545   if (instance->info->pre_activate_cb)
546     instance->info->pre_activate_cb (widget, id, (XtPointer) &event_data);
547 }
548
549 static void
550 xaw_scrollbar_jump (Widget widget, XtPointer closure, XtPointer call_data)
551 {
552   widget_instance *instance = (widget_instance *) closure;
553   LWLIB_ID id;
554   scroll_event event_data;
555   scrollbar_values *val =
556     (scrollbar_values *) instance->info->val->scrollbar_data;
557   float percent;
558
559   if (!instance || widget->core.being_destroyed)
560     return;
561
562   id = instance->info->id;
563
564   percent = * (float *) call_data;
565   event_data.slider_value =
566     (int) (percent * (float) (val->maximum - val->minimum)) + val->minimum;
567
568   event_data.time = 0;
569   event_data.action = SCROLLBAR_DRAG;
570
571   if (instance->info->pre_activate_cb)
572     instance->info->pre_activate_cb (widget, id, (XtPointer) &event_data);
573 }
574
575 static Widget
576 xaw_create_scrollbar (widget_instance *instance, int vertical)
577 {
578   Arg av[10];
579   int ac = 0;
580   
581   static XtCallbackRec jumpCallbacks[2] =
582   { {xaw_scrollbar_jump, NULL}, {NULL, NULL} };
583   
584   static XtCallbackRec scrollCallbacks[2] =
585   { {xaw_scrollbar_scroll, NULL}, {NULL, NULL} };
586
587   jumpCallbacks[0].closure = scrollCallbacks[0].closure = (XtPointer) instance;
588
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++;
599
600   return XtCreateWidget (instance->info->name, scrollbarWidgetClass,
601                          instance->parent, av, ac);
602 }
603
604 static Widget
605 xaw_create_vertical_scrollbar (widget_instance *instance)
606 {
607   return xaw_create_scrollbar (instance, 1);
608 }
609
610 static Widget
611 xaw_create_horizontal_scrollbar (widget_instance *instance)
612 {
613   return xaw_create_scrollbar (instance, 0);
614 }
615 #endif /* LWLIB_SCROLLBARS_ATHENA */
616
617 widget_creation_entry
618 xaw_creation_table [] =
619 {
620 #ifdef LWLIB_SCROLLBARS_ATHENA
621   {"vertical-scrollbar",        xaw_create_vertical_scrollbar},
622   {"horizontal-scrollbar",      xaw_create_horizontal_scrollbar},
623 #endif
624   {NULL, NULL}
625 };