(Fx_popup_dialog, Fx_popup_menu): Require 2 args.
[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 Arg al[1];
128 int ac = 0;
129 XtSetArg (al[ac], XtNlabel, val->contents->value); ac++;
130 XtSetValues (widget, al, ac);
131 }
132 else if (XtIsSubclass (widget, commandWidgetClass))
133 {
134 Dimension bw = 0;
135 XtVaGetValues (widget, XtNborderWidth, &bw, 0);
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 XtVaSetValues (widget, XtNborderWidth, 1, 0);
144
145 XtVaSetValues (widget,
146 XtNlabel, val->value,
147 XtNsensitive, val->enabled,
148 /* Force centered button text. Se above. */
149 XtNjustify, XtJustifyCenter,
150 0);
151
152 XtRemoveAllCallbacks (widget, XtNcallback);
153 XtAddCallback (widget, XtNcallback, xaw_generic_callback, instance);
154 }
155 }
156
157 void
158 xaw_update_one_value (instance, widget, val)
159 widget_instance *instance;
160 Widget widget;
161 widget_value *val;
162 {
163 /* This function is not used by the scrollbars and those are the only
164 Athena widget implemented at the moment so do nothing. */
165 return;
166 }
167
168 void
169 xaw_destroy_instance (instance)
170 widget_instance *instance;
171 {
172 if (XtIsSubclass (instance->widget, dialogWidgetClass))
173 /* Need to destroy the Shell too. */
174 XtDestroyWidget (XtParent (instance->widget));
175 else
176 XtDestroyWidget (instance->widget);
177 }
178
179 void
180 xaw_popup_menu (widget)
181 Widget widget;
182 {
183 /* An Athena menubar has not been implemented. */
184 return;
185 }
186
187 void
188 xaw_pop_instance (instance, up)
189 widget_instance *instance;
190 Boolean up;
191 {
192 Widget widget = instance->widget;
193
194 if (up)
195 {
196 if (XtIsSubclass (widget, dialogWidgetClass))
197 {
198 /* For dialogs, we need to call XtPopup on the parent instead
199 of calling XtManageChild on the widget.
200 Also we need to hack the shell's WM_PROTOCOLS to get it to
201 understand what the close box is supposed to do!!
202 */
203 Display *dpy = XtDisplay (widget);
204 Widget shell = XtParent (widget);
205 Atom props [2];
206 int i = 0;
207 props [i++] = XInternAtom (dpy, "WM_DELETE_WINDOW", False);
208 XChangeProperty (dpy, XtWindow (shell),
209 XInternAtom (dpy, "WM_PROTOCOLS", False),
210 XA_ATOM, 32, PropModeAppend,
211 (unsigned char *) props, i);
212
213 /* Center the widget in its parent. Why isn't this kind of crap
214 done automatically? I thought toolkits were supposed to make
215 life easier?
216 */
217 {
218 int x, y, w, h;
219 Widget topmost = instance->parent;
220 w = shell->core.width;
221 h = shell->core.height;
222 while (topmost->core.parent && XtIsRealized (topmost->core.parent))
223 topmost = topmost->core.parent;
224 if (topmost->core.width < w) x = topmost->core.x;
225 else x = topmost->core.x + ((topmost->core.width - w) / 2);
226 if (topmost->core.height < h) y = topmost->core.y;
227 else y = topmost->core.y + ((topmost->core.height - h) / 2);
228 XtMoveWidget (shell, x, y);
229 }
230
231 /* Finally, pop it up. */
232 XtPopup (shell, XtGrabNonexclusive);
233 }
234 else
235 XtManageChild (widget);
236 }
237 else
238 {
239 if (XtIsSubclass (widget, dialogWidgetClass))
240 XtUnmanageChild (XtParent (widget));
241 else
242 XtUnmanageChild (widget);
243 }
244 }
245
246 \f
247 /* Dialog boxes */
248
249 static char overrideTrans[] =
250 "<Message>WM_PROTOCOLS: lwlib_delete_dialog()";
251 static void wm_delete_window();
252 static XtActionsRec xaw_actions [] = {
253 {"lwlib_delete_dialog", wm_delete_window}
254 };
255 static Boolean actions_initted = False;
256
257 static Widget
258 make_dialog (name, parent, pop_up_p, shell_title, icon_name, text_input_slot, radio_box, list, left_buttons, right_buttons)
259 char* name;
260 Widget parent;
261 Boolean pop_up_p;
262 char* shell_title;
263 char* icon_name;
264 Boolean text_input_slot;
265 Boolean radio_box;
266 Boolean list;
267 int left_buttons;
268 int right_buttons;
269 {
270 Arg av [20];
271 int ac = 0;
272 int i, bc;
273 char button_name [255];
274 Widget shell;
275 Widget dialog;
276 Widget button;
277 XtTranslations override;
278
279 if (! pop_up_p) abort (); /* not implemented */
280 if (text_input_slot) abort (); /* not implemented */
281 if (radio_box) abort (); /* not implemented */
282 if (list) abort (); /* not implemented */
283
284 if (! actions_initted)
285 {
286 XtAppContext app = XtWidgetToApplicationContext (parent);
287 XtAppAddActions (app, xaw_actions,
288 sizeof (xaw_actions) / sizeof (xaw_actions[0]));
289 actions_initted = True;
290 }
291
292 override = XtParseTranslationTable (overrideTrans);
293
294 ac = 0;
295 XtSetArg (av[ac], XtNtitle, shell_title); ac++;
296 XtSetArg (av[ac], XtNallowShellResize, True); ac++;
297 shell = XtCreatePopupShell ("dialog", transientShellWidgetClass,
298 parent, av, ac);
299 XtOverrideTranslations (shell, override);
300
301 ac = 0;
302 dialog = XtCreateManagedWidget (name, dialogWidgetClass, shell, av, ac);
303
304 bc = 0;
305 button = 0;
306 for (i = 0; i < left_buttons; i++)
307 {
308 ac = 0;
309 XtSetArg (av [ac], XtNfromHoriz, button); ac++;
310 XtSetArg (av [ac], XtNleft, XtChainLeft); ac++;
311 XtSetArg (av [ac], XtNright, XtChainLeft); ac++;
312 XtSetArg (av [ac], XtNtop, XtChainBottom); ac++;
313 XtSetArg (av [ac], XtNbottom, XtChainBottom); ac++;
314 XtSetArg (av [ac], XtNresizable, True); ac++;
315 sprintf (button_name, "button%d", ++bc);
316 button = XtCreateManagedWidget (button_name, commandWidgetClass,
317 dialog, av, ac);
318 }
319 if (right_buttons)
320 {
321 /* Create a separator
322
323 I want the separator to take up the slack between the buttons on
324 the right and the buttons on the left (that is I want the buttons
325 after the separator to be packed against the right edge of the
326 window) but I can't seem to make it do it.
327 */
328 ac = 0;
329 XtSetArg (av [ac], XtNfromHoriz, button); ac++;
330 /* XtSetArg (av [ac], XtNfromVert, XtNameToWidget (dialog, "label")); ac++; */
331 XtSetArg (av [ac], XtNleft, XtChainLeft); ac++;
332 XtSetArg (av [ac], XtNright, XtChainRight); ac++;
333 XtSetArg (av [ac], XtNtop, XtChainBottom); ac++;
334 XtSetArg (av [ac], XtNbottom, XtChainBottom); ac++;
335 XtSetArg (av [ac], XtNlabel, ""); ac++;
336 XtSetArg (av [ac], XtNwidth, 30); ac++; /* #### aaack!! */
337 XtSetArg (av [ac], XtNborderWidth, 0); ac++;
338 XtSetArg (av [ac], XtNshapeStyle, XmuShapeRectangle); ac++;
339 XtSetArg (av [ac], XtNresizable, False); ac++;
340 XtSetArg (av [ac], XtNsensitive, False); ac++;
341 button = XtCreateManagedWidget ("separator",
342 /* labelWidgetClass, */
343 /* This has to be Command to fake out
344 the Dialog widget... */
345 commandWidgetClass,
346 dialog, av, ac);
347 }
348 for (i = 0; i < right_buttons; i++)
349 {
350 ac = 0;
351 XtSetArg (av [ac], XtNfromHoriz, button); ac++;
352 XtSetArg (av [ac], XtNleft, XtChainRight); ac++;
353 XtSetArg (av [ac], XtNright, XtChainRight); ac++;
354 XtSetArg (av [ac], XtNtop, XtChainBottom); ac++;
355 XtSetArg (av [ac], XtNbottom, XtChainBottom); ac++;
356 XtSetArg (av [ac], XtNresizable, True); ac++;
357 sprintf (button_name, "button%d", ++bc);
358 button = XtCreateManagedWidget (button_name, commandWidgetClass,
359 dialog, av, ac);
360 }
361
362 return dialog;
363 }
364
365 Widget
366 xaw_create_dialog (instance)
367 widget_instance* instance;
368 {
369 char *name = instance->info->type;
370 Widget parent = instance->parent;
371 Widget widget;
372 Boolean pop_up_p = instance->pop_up_p;
373 char *shell_name = 0;
374 char *icon_name;
375 Boolean text_input_slot = False;
376 Boolean radio_box = False;
377 Boolean list = False;
378 int total_buttons;
379 int left_buttons = 0;
380 int right_buttons = 1;
381
382 switch (name [0]) {
383 case 'E': case 'e':
384 icon_name = "dbox-error";
385 shell_name = "Error";
386 break;
387
388 case 'I': case 'i':
389 icon_name = "dbox-info";
390 shell_name = "Information";
391 break;
392
393 case 'L': case 'l':
394 list = True;
395 icon_name = "dbox-question";
396 shell_name = "Prompt";
397 break;
398
399 case 'P': case 'p':
400 text_input_slot = True;
401 icon_name = "dbox-question";
402 shell_name = "Prompt";
403 break;
404
405 case 'Q': case 'q':
406 icon_name = "dbox-question";
407 shell_name = "Question";
408 break;
409 }
410
411 total_buttons = name [1] - '0';
412
413 if (name [3] == 'T' || name [3] == 't')
414 {
415 text_input_slot = False;
416 radio_box = True;
417 }
418 else if (name [3])
419 right_buttons = name [4] - '0';
420
421 left_buttons = total_buttons - right_buttons;
422
423 widget = make_dialog (name, parent, pop_up_p,
424 shell_name, icon_name, text_input_slot, radio_box,
425 list, left_buttons, right_buttons);
426
427 return widget;
428 }
429
430
431 static void
432 xaw_generic_callback (widget, closure, call_data)
433 Widget widget;
434 XtPointer closure;
435 XtPointer call_data;
436 {
437 widget_instance *instance = (widget_instance *) closure;
438 Widget instance_widget;
439 LWLIB_ID id;
440 XtPointer user_data;
441
442 lw_internal_update_other_instances (widget, closure, call_data);
443
444 if (! instance)
445 return;
446 if (widget->core.being_destroyed)
447 return;
448
449 instance_widget = instance->widget;
450 if (!instance_widget)
451 return;
452
453 id = instance->info->id;
454
455 #if 0
456 user_data = NULL;
457 XtVaGetValues (widget, XtNuserData, &user_data, 0);
458 #else
459 /* Damn! Athena doesn't give us a way to hang our own data on the
460 buttons, so we have to go find it... I guess this assumes that
461 all instances of a button have the same call data. */
462 {
463 widget_value *val = instance->info->val->contents;
464 char *name = XtName (widget);
465 while (val)
466 {
467 if (val->name && !strcmp (val->name, name))
468 break;
469 val = val->next;
470 }
471 if (! val) abort ();
472 user_data = val->call_data;
473 }
474 #endif
475
476 if (instance->info->selection_cb)
477 instance->info->selection_cb (widget, id, user_data);
478 }
479
480 static void
481 wm_delete_window (shell, closure, call_data)
482 Widget shell;
483 XtPointer closure;
484 XtPointer call_data;
485 {
486 LWLIB_ID id;
487 Widget *kids = 0;
488 Widget widget;
489 if (! XtIsSubclass (shell, shellWidgetClass))
490 abort ();
491 XtVaGetValues (shell, XtNchildren, &kids, 0);
492 if (!kids || !*kids)
493 abort ();
494 widget = kids [0];
495 if (! XtIsSubclass (widget, dialogWidgetClass))
496 abort ();
497 id = lw_get_widget_id (widget);
498 if (! id) abort ();
499
500 {
501 widget_info *info = lw_get_widget_info (id);
502 if (! info) abort ();
503 if (info->selection_cb)
504 info->selection_cb (widget, id, (XtPointer) -1);
505 }
506
507 lw_destroy_all_widgets (id);
508 }
509
510 \f
511 /* Scrollbars */
512
513 static void
514 xaw_scrollbar_scroll (widget, closure, call_data)
515 Widget widget;
516 XtPointer closure;
517 XtPointer call_data;
518 {
519 #if 0
520 widget_instance *instance = (widget_instance *) closure;
521 LWLIB_ID id;
522 scroll_event event_data;
523
524 if (!instance || widget->core.being_destroyed)
525 return;
526
527 id = instance->info->id;
528 event_data.slider_value = 0;
529 event_data.time = 0;
530
531 if ((int) call_data > 0)
532 event_data.action = SCROLLBAR_PAGE_DOWN;
533 else
534 event_data.action = SCROLLBAR_PAGE_UP;
535
536 if (instance->info->pre_activate_cb)
537 instance->info->pre_activate_cb (widget, id, (XtPointer) &event_data);
538 #endif
539 }
540
541 static void
542 xaw_scrollbar_jump (widget, closure, call_data)
543 Widget widget;
544 XtPointer closure;
545 XtPointer call_data;
546 {
547 #if 0
548 widget_instance *instance = (widget_instance *) closure;
549 LWLIB_ID id;
550 scroll_event event_data;
551 scrollbar_values *val =
552 (scrollbar_values *) instance->info->val->scrollbar_data;
553 float percent;
554
555 if (!instance || widget->core.being_destroyed)
556 return;
557
558 id = instance->info->id;
559
560 percent = * (float *) call_data;
561 event_data.slider_value =
562 (int) (percent * (float) (val->maximum - val->minimum)) + val->minimum;
563
564 event_data.time = 0;
565 event_data.action = SCROLLBAR_DRAG;
566
567 if (instance->info->pre_activate_cb)
568 instance->info->pre_activate_cb (widget, id, (XtPointer) &event_data);
569 #endif
570 }
571
572 static Widget
573 xaw_create_scrollbar (instance)
574 widget_instance *instance;
575 {
576 #if 0
577 Arg av[20];
578 int ac = 0;
579 Dimension width;
580 Widget scrollbar;
581
582 XtVaGetValues (instance->parent, XtNwidth, &width, 0);
583
584 XtSetArg (av[ac], XtNshowGrip, 0); ac++;
585 XtSetArg (av[ac], XtNresizeToPreferred, 1); ac++;
586 XtSetArg (av[ac], XtNallowResize, True); ac++;
587 XtSetArg (av[ac], XtNskipAdjust, True); ac++;
588 XtSetArg (av[ac], XtNwidth, width); ac++;
589 XtSetArg (av[ac], XtNmappedWhenManaged, True); ac++;
590
591 scrollbar =
592 XtCreateWidget (instance->info->name, scrollbarWidgetClass,
593 instance->parent, av, ac);
594
595 /* We have to force the border width to be 0 otherwise the
596 geometry manager likes to start looping for awhile... */
597 XtVaSetValues (scrollbar, XtNborderWidth, 0, 0);
598
599 XtRemoveAllCallbacks (scrollbar, "jumpProc");
600 XtRemoveAllCallbacks (scrollbar, "scrollProc");
601
602 XtAddCallback (scrollbar, "jumpProc", xaw_scrollbar_jump,
603 (XtPointer) instance);
604 XtAddCallback (scrollbar, "scrollProc", xaw_scrollbar_scroll,
605 (XtPointer) instance);
606
607 return scrollbar;
608 #endif
609 }
610
611 widget_creation_entry
612 xaw_creation_table [] =
613 {
614 {"scrollbar", xaw_create_scrollbar},
615 {NULL, NULL}
616 };