*** empty log message ***
[bpt/emacs.git] / lwlib / lwlib-Xaw.c
1 /* The lwlib interface to Athena widgets.
2 Copyright (C) 1993 Chuck Thompson <cthomp@cs.uiuc.edu>
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 GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 #include <stdio.h>
21
22 #include "lwlib-Xaw.h"
23
24 #include <X11/StringDefs.h>
25 #include <X11/IntrinsicP.h>
26 #include <X11/CoreP.h>
27 #include <X11/Shell.h>
28
29 #include <X11/Xaw/Scrollbar.h>
30 #include <X11/Xaw/Paned.h>
31 #include <X11/Xaw/Dialog.h>
32 #include <X11/Xaw/Form.h>
33 #include <X11/Xaw/Command.h>
34 #include <X11/Xaw/Label.h>
35
36 #include <X11/Xatom.h>
37
38 static void xaw_generic_callback (Widget, XtPointer, XtPointer);
39
40
41 Boolean
42 lw_xaw_widget_p (widget)
43 Widget widget;
44 {
45 return (XtIsSubclass (widget, scrollbarWidgetClass) ||
46 XtIsSubclass (widget, dialogWidgetClass));
47 }
48
49 static void
50 xaw_update_scrollbar (instance, widget, val)
51 widget_instance *instance;
52 Widget widget;
53 widget_value *val;
54 {
55 #if 0
56 if (val->scrollbar_data)
57 {
58 scrollbar_values *data = val->scrollbar_data;
59 Dimension height, width;
60 Dimension pos_x, pos_y;
61 int widget_shown, widget_topOfThumb;
62 float new_shown, new_topOfThumb;
63
64 XtVaGetValues (widget,
65 XtNheight, &height,
66 XtNwidth, &width,
67 XtNx, &pos_x,
68 XtNy, &pos_y,
69 XtNtopOfThumb, &widget_topOfThumb,
70 XtNshown, &widget_shown,
71 0);
72
73 /*
74 * First size and position the scrollbar widget.
75 * We need to position it to second-guess the Paned widget's notion
76 * of what should happen when the WMShell gets resized.
77 */
78 if (height != data->scrollbar_height || pos_y != data->scrollbar_pos)
79 {
80 XtConfigureWidget (widget, pos_x, data->scrollbar_pos,
81 width, data->scrollbar_height, 0);
82
83 XtVaSetValues (widget,
84 XtNlength, data->scrollbar_height,
85 XtNthickness, width,
86 0);
87 }
88
89 /*
90 * Now the size the scrollbar's slider.
91 */
92 new_shown = (float) data->slider_size /
93 (float) (data->maximum - data->minimum);
94
95 new_topOfThumb = (float) (data->slider_position - data->minimum) /
96 (float) (data->maximum - data->minimum);
97
98 if (new_shown > 1.0)
99 new_shown = 1.0;
100 if (new_shown < 0)
101 new_shown = 0;
102
103 if (new_topOfThumb > 1.0)
104 new_topOfThumb = 1.0;
105 if (new_topOfThumb < 0)
106 new_topOfThumb = 0;
107
108 if (new_shown != widget_shown || new_topOfThumb != widget_topOfThumb)
109 XawScrollbarSetThumb (widget, new_topOfThumb, new_shown);
110 }
111 #endif
112 }
113
114 void
115 xaw_update_one_widget (instance, widget, val, deep_p)
116 widget_instance *instance;
117 Widget widget;
118 widget_value *val;
119 Boolean deep_p;
120 {
121 if (XtIsSubclass (widget, scrollbarWidgetClass))
122 {
123 xaw_update_scrollbar (instance, widget, val);
124 }
125 else if (XtIsSubclass (widget, dialogWidgetClass))
126 {
127 XtVaSetValues (widget, XtNlabel, val->contents->value, 0);
128 }
129 else if (XtIsSubclass (widget, commandWidgetClass))
130 {
131 Dimension bw = 0;
132 XtVaGetValues (widget, XtNborderWidth, &bw, 0);
133 if (bw == 0)
134 /* Don't let buttons end up with 0 borderwidth, that's ugly...
135 Yeah, all this should really be done through app-defaults files
136 or fallback resources, but that's a whole different can of worms
137 that I don't feel like opening right now. Making Athena widgets
138 not look like shit is just entirely too much work.
139 */
140 XtVaSetValues (widget, XtNborderWidth, 1, 0);
141
142 XtVaSetValues (widget,
143 XtNlabel, val->value,
144 XtNsensitive, val->enabled,
145 /* Force centered button text. Se above. */
146 XtNjustify, XtJustifyCenter,
147 0);
148
149 XtRemoveAllCallbacks (widget, XtNcallback);
150 XtAddCallback (widget, XtNcallback, xaw_generic_callback, instance);
151 }
152 }
153
154 void
155 xaw_update_one_value (instance, widget, val)
156 widget_instance *instance;
157 Widget widget;
158 widget_value *val;
159 {
160 /* This function is not used by the scrollbars and those are the only
161 Athena widget implemented at the moment so do nothing. */
162 return;
163 }
164
165 void
166 xaw_destroy_instance (instance)
167 widget_instance *instance;
168 {
169 if (XtIsSubclass (instance->widget, dialogWidgetClass))
170 /* Need to destroy the Shell too. */
171 XtDestroyWidget (XtParent (instance->widget));
172 else
173 XtDestroyWidget (instance->widget);
174 }
175
176 void
177 xaw_popup_menu (widget)
178 Widget widget;
179 {
180 /* An Athena menubar has not been implemented. */
181 return;
182 }
183
184 void
185 xaw_pop_instance (instance, up)
186 widget_instance *instance;
187 Boolean up;
188 {
189 Widget widget = instance->widget;
190
191 if (up)
192 {
193 if (XtIsSubclass (widget, dialogWidgetClass))
194 {
195 /* For dialogs, we need to call XtPopup on the parent instead
196 of calling XtManageChild on the widget.
197 Also we need to hack the shell's WM_PROTOCOLS to get it to
198 understand what the close box is supposed to do!!
199 */
200 Display *dpy = XtDisplay (widget);
201 Widget shell = XtParent (widget);
202 Atom props [2];
203 int i = 0;
204 props [i++] = XInternAtom (dpy, "WM_DELETE_WINDOW", False);
205 XChangeProperty (dpy, XtWindow (shell),
206 XInternAtom (dpy, "WM_PROTOCOLS", False),
207 XA_ATOM, 32, PropModeAppend,
208 (unsigned char *) props, i);
209
210 /* Center the widget in its parent. Why isn't this kind of crap
211 done automatically? I thought toolkits were supposed to make
212 life easier?
213 */
214 {
215 int x, y, w, h;
216 Widget topmost = instance->parent;
217 w = shell->core.width;
218 h = shell->core.height;
219 while (topmost->core.parent && XtIsRealized (topmost->core.parent))
220 topmost = topmost->core.parent;
221 if (topmost->core.width < w) x = topmost->core.x;
222 else x = topmost->core.x + ((topmost->core.width - w) / 2);
223 if (topmost->core.height < h) y = topmost->core.y;
224 else y = topmost->core.y + ((topmost->core.height - h) / 2);
225 XtMoveWidget (shell, x, y);
226 }
227
228 /* Finally, pop it up. */
229 XtPopup (shell, XtGrabNonexclusive);
230 }
231 else
232 XtManageChild (widget);
233 }
234 else
235 {
236 if (XtIsSubclass (widget, dialogWidgetClass))
237 XtUnmanageChild (XtParent (widget));
238 else
239 XtUnmanageChild (widget);
240 }
241 }
242
243 \f
244 /* Dialog boxes */
245
246 static char overrideTrans[] =
247 "<Message>WM_PROTOCOLS: lwlib_delete_dialog()";
248 static void wm_delete_window();
249 static XtActionsRec xaw_actions [] = {
250 {"lwlib_delete_dialog", wm_delete_window}
251 };
252 static Boolean actions_initted = False;
253
254 static Widget
255 make_dialog (name, parent, pop_up_p, shell_title, icon_name, text_input_slot, radio_box, list, left_buttons, right_buttons)
256 char* name;
257 Widget parent;
258 Boolean pop_up_p;
259 char* shell_title;
260 char* icon_name;
261 Boolean text_input_slot;
262 Boolean radio_box;
263 Boolean list;
264 int left_buttons;
265 int right_buttons;
266 {
267 Arg av [20];
268 int ac = 0;
269 int i, bc;
270 char button_name [255];
271 Widget shell;
272 Widget dialog;
273 Widget button;
274 XtTranslations override;
275
276 if (! pop_up_p) abort (); /* not implemented */
277 if (text_input_slot) abort (); /* not implemented */
278 if (radio_box) abort (); /* not implemented */
279 if (list) abort (); /* not implemented */
280
281 if (! actions_initted)
282 {
283 XtAppContext app = XtWidgetToApplicationContext (parent);
284 XtAppAddActions (app, xaw_actions,
285 sizeof (xaw_actions) / sizeof (xaw_actions[0]));
286 actions_initted = True;
287 }
288
289 override = XtParseTranslationTable (overrideTrans);
290
291 ac = 0;
292 XtSetArg (av[ac], XtNtitle, shell_title); ac++;
293 XtSetArg (av[ac], XtNallowShellResize, True); ac++;
294 shell = XtCreatePopupShell ("dialog", transientShellWidgetClass,
295 parent, av, ac);
296 XtOverrideTranslations (shell, override);
297
298 ac = 0;
299 dialog = XtCreateManagedWidget (name, dialogWidgetClass, shell, av, ac);
300
301 bc = 0;
302 button = 0;
303 for (i = 0; i < left_buttons; i++)
304 {
305 ac = 0;
306 XtSetArg (av [ac], XtNfromHoriz, button); ac++;
307 XtSetArg (av [ac], XtNleft, XtChainLeft); ac++;
308 XtSetArg (av [ac], XtNright, XtChainLeft); ac++;
309 XtSetArg (av [ac], XtNtop, XtChainBottom); ac++;
310 XtSetArg (av [ac], XtNbottom, XtChainBottom); ac++;
311 XtSetArg (av [ac], XtNresizable, True); ac++;
312 sprintf (button_name, "button%d", ++bc);
313 button = XtCreateManagedWidget (button_name, commandWidgetClass,
314 dialog, av, ac);
315 }
316 if (right_buttons)
317 {
318 /* Create a separator
319
320 I want the separator to take up the slack between the buttons on
321 the right and the buttons on the left (that is I want the buttons
322 after the separator to be packed against the right edge of the
323 window) but I can't seem to make it do it.
324 */
325 ac = 0;
326 XtSetArg (av [ac], XtNfromHoriz, button); ac++;
327 /* XtSetArg (av [ac], XtNfromVert, XtNameToWidget (dialog, "label")); ac++; */
328 XtSetArg (av [ac], XtNleft, XtChainLeft); ac++;
329 XtSetArg (av [ac], XtNright, XtChainRight); ac++;
330 XtSetArg (av [ac], XtNtop, XtChainBottom); ac++;
331 XtSetArg (av [ac], XtNbottom, XtChainBottom); ac++;
332 XtSetArg (av [ac], XtNlabel, ""); ac++;
333 XtSetArg (av [ac], XtNwidth, 30); ac++; /* #### aaack!! */
334 XtSetArg (av [ac], XtNborderWidth, 0); ac++;
335 XtSetArg (av [ac], XtNshapeStyle, XmuShapeRectangle); ac++;
336 XtSetArg (av [ac], XtNresizable, False); ac++;
337 XtSetArg (av [ac], XtNsensitive, False); ac++;
338 button = XtCreateManagedWidget ("separator",
339 /* labelWidgetClass, */
340 /* This has to be Command to fake out
341 the Dialog widget... */
342 commandWidgetClass,
343 dialog, av, ac);
344 }
345 for (i = 0; i < right_buttons; i++)
346 {
347 ac = 0;
348 XtSetArg (av [ac], XtNfromHoriz, button); ac++;
349 XtSetArg (av [ac], XtNleft, XtChainRight); ac++;
350 XtSetArg (av [ac], XtNright, XtChainRight); ac++;
351 XtSetArg (av [ac], XtNtop, XtChainBottom); ac++;
352 XtSetArg (av [ac], XtNbottom, XtChainBottom); ac++;
353 XtSetArg (av [ac], XtNresizable, True); ac++;
354 sprintf (button_name, "button%d", ++bc);
355 button = XtCreateManagedWidget (button_name, commandWidgetClass,
356 dialog, av, ac);
357 }
358
359 return dialog;
360 }
361
362 Widget
363 xaw_create_dialog (instance)
364 widget_instance* instance;
365 {
366 char *name = instance->info->type;
367 Widget parent = instance->parent;
368 Widget widget;
369 Boolean pop_up_p = instance->pop_up_p;
370 char *shell_name = 0;
371 char *icon_name;
372 Boolean text_input_slot = False;
373 Boolean radio_box = False;
374 Boolean list = False;
375 int total_buttons;
376 int left_buttons = 0;
377 int right_buttons = 1;
378
379 switch (name [0]) {
380 case 'E': case 'e':
381 icon_name = "dbox-error";
382 shell_name = "Error";
383 break;
384
385 case 'I': case 'i':
386 icon_name = "dbox-info";
387 shell_name = "Information";
388 break;
389
390 case 'L': case 'l':
391 list = True;
392 icon_name = "dbox-question";
393 shell_name = "Prompt";
394 break;
395
396 case 'P': case 'p':
397 text_input_slot = True;
398 icon_name = "dbox-question";
399 shell_name = "Prompt";
400 break;
401
402 case 'Q': case 'q':
403 icon_name = "dbox-question";
404 shell_name = "Question";
405 break;
406 }
407
408 total_buttons = name [1] - '0';
409
410 if (name [3] == 'T' || name [3] == 't')
411 {
412 text_input_slot = False;
413 radio_box = True;
414 }
415 else if (name [3])
416 right_buttons = name [4] - '0';
417
418 left_buttons = total_buttons - right_buttons;
419
420 widget = make_dialog (name, parent, pop_up_p,
421 shell_name, icon_name, text_input_slot, radio_box,
422 list, left_buttons, right_buttons);
423
424 return widget;
425 }
426
427
428 static void
429 xaw_generic_callback (widget, closure, call_data)
430 Widget widget;
431 XtPointer closure;
432 XtPointer call_data;
433 {
434 widget_instance *instance = (widget_instance *) closure;
435 Widget instance_widget;
436 LWLIB_ID id;
437 XtPointer user_data;
438
439 lw_internal_update_other_instances (widget, closure, call_data);
440
441 if (! instance)
442 return;
443 if (widget->core.being_destroyed)
444 return;
445
446 instance_widget = instance->widget;
447 if (!instance_widget)
448 return;
449
450 id = instance->info->id;
451
452 #if 0
453 user_data = NULL;
454 XtVaGetValues (widget, XtNuserData, &user_data, 0);
455 #else
456 /* Damn! Athena doesn't give us a way to hang our own data on the
457 buttons, so we have to go find it... I guess this assumes that
458 all instances of a button have the same call data. */
459 {
460 widget_value *val = instance->info->val->contents;
461 char *name = XtName (widget);
462 while (val)
463 {
464 if (val->name && !strcmp (val->name, name))
465 break;
466 val = val->next;
467 }
468 if (! val) abort ();
469 user_data = val->call_data;
470 }
471 #endif
472
473 if (instance->info->selection_cb)
474 instance->info->selection_cb (widget, id, user_data);
475 }
476
477 static void
478 wm_delete_window (shell, closure, call_data)
479 Widget shell;
480 XtPointer closure;
481 XtPointer call_data;
482 {
483 LWLIB_ID id;
484 Widget *kids = 0;
485 Widget widget;
486 if (! XtIsSubclass (shell, shellWidgetClass))
487 abort ();
488 XtVaGetValues (shell, XtNchildren, &kids, 0);
489 if (!kids || !*kids)
490 abort ();
491 widget = kids [0];
492 if (! XtIsSubclass (widget, dialogWidgetClass))
493 abort ();
494 id = lw_get_widget_id (widget);
495 if (! id) abort ();
496
497 {
498 widget_info *info = lw_get_widget_info (id);
499 if (! info) abort ();
500 if (info->selection_cb)
501 info->selection_cb (widget, id, (XtPointer) -1);
502 }
503
504 lw_destroy_all_widgets (id);
505 }
506
507 \f
508 /* Scrollbars */
509
510 static void
511 xaw_scrollbar_scroll (widget, closure, call_data)
512 Widget widget;
513 XtPointer closure;
514 XtPointer call_data;
515 {
516 #if 0
517 widget_instance *instance = (widget_instance *) closure;
518 LWLIB_ID id;
519 scroll_event event_data;
520
521 if (!instance || widget->core.being_destroyed)
522 return;
523
524 id = instance->info->id;
525 event_data.slider_value = 0;
526 event_data.time = 0;
527
528 if ((int) call_data > 0)
529 event_data.action = SCROLLBAR_PAGE_DOWN;
530 else
531 event_data.action = SCROLLBAR_PAGE_UP;
532
533 if (instance->info->pre_activate_cb)
534 instance->info->pre_activate_cb (widget, id, (XtPointer) &event_data);
535 #endif
536 }
537
538 static void
539 xaw_scrollbar_jump (widget, closure, call_data)
540 Widget widget;
541 XtPointer closure;
542 XtPointer call_data;
543 {
544 #if 0
545 widget_instance *instance = (widget_instance *) closure;
546 LWLIB_ID id;
547 scroll_event event_data;
548 scrollbar_values *val =
549 (scrollbar_values *) instance->info->val->scrollbar_data;
550 float percent;
551
552 if (!instance || widget->core.being_destroyed)
553 return;
554
555 id = instance->info->id;
556
557 percent = * (float *) call_data;
558 event_data.slider_value =
559 (int) (percent * (float) (val->maximum - val->minimum)) + val->minimum;
560
561 event_data.time = 0;
562 event_data.action = SCROLLBAR_DRAG;
563
564 if (instance->info->pre_activate_cb)
565 instance->info->pre_activate_cb (widget, id, (XtPointer) &event_data);
566 #endif
567 }
568
569 static Widget
570 xaw_create_scrollbar (instance)
571 widget_instance *instance;
572 {
573 #if 0
574 Arg av[20];
575 int ac = 0;
576 Dimension width;
577 Widget scrollbar;
578
579 XtVaGetValues (instance->parent, XtNwidth, &width, 0);
580
581 XtSetArg (av[ac], XtNshowGrip, 0); ac++;
582 XtSetArg (av[ac], XtNresizeToPreferred, 1); ac++;
583 XtSetArg (av[ac], XtNallowResize, True); ac++;
584 XtSetArg (av[ac], XtNskipAdjust, True); ac++;
585 XtSetArg (av[ac], XtNwidth, width); ac++;
586 XtSetArg (av[ac], XtNmappedWhenManaged, True); ac++;
587
588 scrollbar =
589 XtCreateWidget (instance->info->name, scrollbarWidgetClass,
590 instance->parent, av, ac);
591
592 /* We have to force the border width to be 0 otherwise the
593 geometry manager likes to start looping for awhile... */
594 XtVaSetValues (scrollbar, XtNborderWidth, 0, 0);
595
596 XtRemoveAllCallbacks (scrollbar, "jumpProc");
597 XtRemoveAllCallbacks (scrollbar, "scrollProc");
598
599 XtAddCallback (scrollbar, "jumpProc", xaw_scrollbar_jump,
600 (XtPointer) instance);
601 XtAddCallback (scrollbar, "scrollProc", xaw_scrollbar_scroll,
602 (XtPointer) instance);
603
604 return scrollbar;
605 #endif
606 }
607
608 widget_creation_entry
609 xaw_creation_table [] =
610 {
611 {"scrollbar", xaw_create_scrollbar},
612 {NULL, NULL}
613 };