Reposition some code so defined before used.
[emacs.git] / lwlib / lwlib-Xaw.c
blob3ffc0d62f2b5c97aa300b81c7403586d0ea2fb92
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 "../src/lisp.h"
29 #include "lwlib-Xaw.h"
31 #include <X11/StringDefs.h>
32 #include <X11/IntrinsicP.h>
33 #include <X11/CoreP.h>
34 #include <X11/Shell.h>
36 #include <X11/Xaw/Scrollbar.h>
37 #include <X11/Xaw/Paned.h>
38 #include <X11/Xaw/Dialog.h>
39 #include <X11/Xaw/Form.h>
40 #include <X11/Xaw/Command.h>
41 #include <X11/Xaw/Label.h>
43 #include <X11/Xatom.h>
45 static void xaw_generic_callback (/*Widget, XtPointer, XtPointer*/);
48 Boolean
49 lw_xaw_widget_p (widget)
50 Widget widget;
52 return (XtIsSubclass (widget, scrollbarWidgetClass) ||
53 XtIsSubclass (widget, dialogWidgetClass));
56 #if 0
57 static void
58 xaw_update_scrollbar (instance, widget, val)
59 widget_instance *instance;
60 Widget widget;
61 widget_value *val;
63 if (val->scrollbar_data)
65 scrollbar_values *data = val->scrollbar_data;
66 Dimension height, width;
67 Dimension pos_x, pos_y;
68 int widget_shown, widget_topOfThumb;
69 float new_shown, new_topOfThumb;
71 XtVaGetValues (widget,
72 XtNheight, &height,
73 XtNwidth, &width,
74 XtNx, &pos_x,
75 XtNy, &pos_y,
76 XtNtopOfThumb, &widget_topOfThumb,
77 XtNshown, &widget_shown,
78 NULL);
81 * First size and position the scrollbar widget.
82 * We need to position it to second-guess the Paned widget's notion
83 * of what should happen when the WMShell gets resized.
85 if (height != data->scrollbar_height || pos_y != data->scrollbar_pos)
87 XtConfigureWidget (widget, pos_x, data->scrollbar_pos,
88 width, data->scrollbar_height, 0);
90 XtVaSetValues (widget,
91 XtNlength, data->scrollbar_height,
92 XtNthickness, width,
93 NULL);
97 * Now the size the scrollbar's slider.
99 new_shown = (float) data->slider_size /
100 (float) (data->maximum - data->minimum);
102 new_topOfThumb = (float) (data->slider_position - data->minimum) /
103 (float) (data->maximum - data->minimum);
105 if (new_shown > 1.0)
106 new_shown = 1.0;
107 if (new_shown < 0)
108 new_shown = 0;
110 if (new_topOfThumb > 1.0)
111 new_topOfThumb = 1.0;
112 if (new_topOfThumb < 0)
113 new_topOfThumb = 0;
115 if (new_shown != widget_shown || new_topOfThumb != widget_topOfThumb)
116 XawScrollbarSetThumb (widget, new_topOfThumb, new_shown);
119 #endif
121 void
122 #ifdef PROTOTYPES
123 xaw_update_one_widget (widget_instance *instance, Widget widget,
124 widget_value *val, Boolean deep_p)
125 #else
126 xaw_update_one_widget (instance, widget, val, deep_p)
127 widget_instance *instance;
128 Widget widget;
129 widget_value *val;
130 Boolean deep_p;
131 #endif
133 #if 0
134 if (XtIsSubclass (widget, scrollbarWidgetClass))
136 xaw_update_scrollbar (instance, widget, val);
138 #endif
139 if (XtIsSubclass (widget, dialogWidgetClass))
141 Arg al[1];
142 int ac = 0;
143 XtSetArg (al[ac], XtNlabel, val->contents->value); ac++;
144 XtSetValues (widget, al, ac);
146 else if (XtIsSubclass (widget, commandWidgetClass))
148 Dimension bw = 0;
149 Arg al[3];
151 XtVaGetValues (widget, XtNborderWidth, &bw, NULL);
152 if (bw == 0)
153 /* Don't let buttons end up with 0 borderwidth, that's ugly...
154 Yeah, all this should really be done through app-defaults files
155 or fallback resources, but that's a whole different can of worms
156 that I don't feel like opening right now. Making Athena widgets
157 not look like shit is just entirely too much work.
160 XtSetArg (al[0], XtNborderWidth, 1);
161 XtSetValues (widget, al, 1);
164 XtSetSensitive (widget, val->enabled);
165 XtSetArg (al[0], XtNlabel, val->value);
166 /* Force centered button text. Se above. */
167 XtSetArg (al[1], XtNjustify, XtJustifyCenter);
168 XtSetValues (widget, al, 2);
169 XtRemoveAllCallbacks (widget, XtNcallback);
170 XtAddCallback (widget, XtNcallback, xaw_generic_callback, instance);
174 void
175 xaw_update_one_value (instance, widget, val)
176 widget_instance *instance;
177 Widget widget;
178 widget_value *val;
180 /* This function is not used by the scrollbars and those are the only
181 Athena widget implemented at the moment so do nothing. */
182 return;
185 void
186 xaw_destroy_instance (instance)
187 widget_instance *instance;
189 if (XtIsSubclass (instance->widget, dialogWidgetClass))
190 /* Need to destroy the Shell too. */
191 XtDestroyWidget (XtParent (instance->widget));
192 else
193 XtDestroyWidget (instance->widget);
196 void
197 xaw_popup_menu (widget, event)
198 Widget widget;
199 XEvent *event;
201 /* An Athena menubar has not been implemented. */
202 return;
205 void
206 #ifdef PROTOTYPES
207 xaw_pop_instance (widget_instance *instance, Boolean up)
208 #else
209 xaw_pop_instance (instance, up)
210 widget_instance *instance;
211 Boolean up;
212 #endif
214 Widget widget = instance->widget;
216 if (up)
218 if (XtIsSubclass (widget, dialogWidgetClass))
220 /* For dialogs, we need to call XtPopup on the parent instead
221 of calling XtManageChild on the widget.
222 Also we need to hack the shell's WM_PROTOCOLS to get it to
223 understand what the close box is supposed to do!!
225 Display *dpy = XtDisplay (widget);
226 Widget shell = XtParent (widget);
227 Atom props [2];
228 int i = 0;
229 props [i++] = XInternAtom (dpy, "WM_DELETE_WINDOW", False);
230 XChangeProperty (dpy, XtWindow (shell),
231 XInternAtom (dpy, "WM_PROTOCOLS", False),
232 XA_ATOM, 32, PropModeAppend,
233 (unsigned char *) props, i);
235 /* Center the widget in its parent. Why isn't this kind of crap
236 done automatically? I thought toolkits were supposed to make
237 life easier?
240 unsigned int x, y, w, h;
241 Widget topmost = instance->parent;
242 Arg args[2];
244 w = shell->core.width;
245 h = shell->core.height;
246 while (topmost->core.parent && XtIsRealized (topmost->core.parent))
247 topmost = topmost->core.parent;
248 if (topmost->core.width < w) x = topmost->core.x;
249 else x = topmost->core.x + ((topmost->core.width - w) / 2);
250 if (topmost->core.height < h) y = topmost->core.y;
251 else y = topmost->core.y + ((topmost->core.height - h) / 2);
252 /* Using XtMoveWidget caused the widget to come
253 out in the wrong place with vtwm.
254 Question of virtual vs real coords, perhaps. */
255 XtSetArg (args[0], XtNx, x);
256 XtSetArg (args[1], XtNy, y);
257 XtSetValues (shell, args, 2);
260 /* Finally, pop it up. */
261 XtPopup (shell, XtGrabNonexclusive);
263 else
264 XtManageChild (widget);
266 else
268 if (XtIsSubclass (widget, dialogWidgetClass))
269 XtUnmanageChild (XtParent (widget));
270 else
271 XtUnmanageChild (widget);
276 /* Dialog boxes */
278 static char overrideTrans[] =
279 "<Message>WM_PROTOCOLS: lwlib_delete_dialog()";
280 static void wm_delete_window();
281 static XtActionsRec xaw_actions [] = {
282 {"lwlib_delete_dialog", wm_delete_window}
284 static Boolean actions_initted = False;
286 static Widget
287 make_dialog (name, parent, pop_up_p, shell_title, icon_name, text_input_slot, radio_box, list, left_buttons, right_buttons)
288 char* name;
289 Widget parent;
290 Boolean pop_up_p;
291 char* shell_title;
292 char* icon_name;
293 Boolean text_input_slot;
294 Boolean radio_box;
295 Boolean list;
296 int left_buttons;
297 int right_buttons;
299 Arg av [20];
300 int ac = 0;
301 int i, bc;
302 char button_name [255];
303 Widget shell;
304 Widget dialog;
305 Widget button;
306 XtTranslations override;
308 if (! pop_up_p) abort (); /* not implemented */
309 if (text_input_slot) abort (); /* not implemented */
310 if (radio_box) abort (); /* not implemented */
311 if (list) abort (); /* not implemented */
313 if (! actions_initted)
315 XtAppContext app = XtWidgetToApplicationContext (parent);
316 XtAppAddActions (app, xaw_actions,
317 sizeof (xaw_actions) / sizeof (xaw_actions[0]));
318 actions_initted = True;
321 override = XtParseTranslationTable (overrideTrans);
323 ac = 0;
324 XtSetArg (av[ac], XtNtitle, shell_title); ac++;
325 XtSetArg (av[ac], XtNallowShellResize, True); ac++;
327 /* Don't allow any geometry request from the user. */
328 XtSetArg (av[ac], XtNgeometry, 0); ac++;
330 shell = XtCreatePopupShell ("dialog", transientShellWidgetClass,
331 parent, av, ac);
332 XtOverrideTranslations (shell, override);
334 ac = 0;
335 dialog = XtCreateManagedWidget (name, dialogWidgetClass, shell, av, ac);
337 bc = 0;
338 button = 0;
339 for (i = 0; i < left_buttons; i++)
341 ac = 0;
342 XtSetArg (av [ac], XtNfromHoriz, button); ac++;
343 XtSetArg (av [ac], XtNleft, XtChainLeft); ac++;
344 XtSetArg (av [ac], XtNright, XtChainLeft); ac++;
345 XtSetArg (av [ac], XtNtop, XtChainBottom); ac++;
346 XtSetArg (av [ac], XtNbottom, XtChainBottom); ac++;
347 XtSetArg (av [ac], XtNresizable, True); ac++;
348 sprintf (button_name, "button%d", ++bc);
349 button = XtCreateManagedWidget (button_name, commandWidgetClass,
350 dialog, av, ac);
352 if (right_buttons)
354 /* Create a separator
356 I want the separator to take up the slack between the buttons on
357 the right and the buttons on the left (that is I want the buttons
358 after the separator to be packed against the right edge of the
359 window) but I can't seem to make it do it.
361 ac = 0;
362 XtSetArg (av [ac], XtNfromHoriz, button); ac++;
363 /* XtSetArg (av [ac], XtNfromVert, XtNameToWidget (dialog, "label")); ac++; */
364 XtSetArg (av [ac], XtNleft, XtChainLeft); ac++;
365 XtSetArg (av [ac], XtNright, XtChainRight); ac++;
366 XtSetArg (av [ac], XtNtop, XtChainBottom); ac++;
367 XtSetArg (av [ac], XtNbottom, XtChainBottom); ac++;
368 XtSetArg (av [ac], XtNlabel, ""); ac++;
369 XtSetArg (av [ac], XtNwidth, 30); ac++; /* #### aaack!! */
370 XtSetArg (av [ac], XtNborderWidth, 0); ac++;
371 XtSetArg (av [ac], XtNshapeStyle, XmuShapeRectangle); ac++;
372 XtSetArg (av [ac], XtNresizable, False); ac++;
373 XtSetArg (av [ac], XtNsensitive, False); ac++;
374 button = XtCreateManagedWidget ("separator",
375 /* labelWidgetClass, */
376 /* This has to be Command to fake out
377 the Dialog widget... */
378 commandWidgetClass,
379 dialog, av, ac);
381 for (i = 0; i < right_buttons; i++)
383 ac = 0;
384 XtSetArg (av [ac], XtNfromHoriz, button); ac++;
385 XtSetArg (av [ac], XtNleft, XtChainRight); ac++;
386 XtSetArg (av [ac], XtNright, XtChainRight); ac++;
387 XtSetArg (av [ac], XtNtop, XtChainBottom); ac++;
388 XtSetArg (av [ac], XtNbottom, XtChainBottom); ac++;
389 XtSetArg (av [ac], XtNresizable, True); ac++;
390 sprintf (button_name, "button%d", ++bc);
391 button = XtCreateManagedWidget (button_name, commandWidgetClass,
392 dialog, av, ac);
395 return dialog;
398 Widget
399 xaw_create_dialog (instance)
400 widget_instance* instance;
402 char *name = instance->info->type;
403 Widget parent = instance->parent;
404 Widget widget;
405 Boolean pop_up_p = instance->pop_up_p;
406 char *shell_name = 0;
407 char *icon_name = 0;
408 Boolean text_input_slot = False;
409 Boolean radio_box = False;
410 Boolean list = False;
411 int total_buttons;
412 int left_buttons = 0;
413 int right_buttons = 1;
415 switch (name [0]) {
416 case 'E': case 'e':
417 icon_name = "dbox-error";
418 shell_name = "Error";
419 break;
421 case 'I': case 'i':
422 icon_name = "dbox-info";
423 shell_name = "Information";
424 break;
426 case 'L': case 'l':
427 list = True;
428 icon_name = "dbox-question";
429 shell_name = "Prompt";
430 break;
432 case 'P': case 'p':
433 text_input_slot = True;
434 icon_name = "dbox-question";
435 shell_name = "Prompt";
436 break;
438 case 'Q': case 'q':
439 icon_name = "dbox-question";
440 shell_name = "Question";
441 break;
444 total_buttons = name [1] - '0';
446 if (name [3] == 'T' || name [3] == 't')
448 text_input_slot = False;
449 radio_box = True;
451 else if (name [3])
452 right_buttons = name [4] - '0';
454 left_buttons = total_buttons - right_buttons;
456 widget = make_dialog (name, parent, pop_up_p,
457 shell_name, icon_name, text_input_slot, radio_box,
458 list, left_buttons, right_buttons);
460 return widget;
464 static void
465 xaw_generic_callback (widget, closure, call_data)
466 Widget widget;
467 XtPointer closure;
468 XtPointer call_data;
470 widget_instance *instance = (widget_instance *) closure;
471 Widget instance_widget;
472 LWLIB_ID id;
473 XtPointer user_data;
475 lw_internal_update_other_instances (widget, closure, call_data);
477 if (! instance)
478 return;
479 if (widget->core.being_destroyed)
480 return;
482 instance_widget = instance->widget;
483 if (!instance_widget)
484 return;
486 id = instance->info->id;
488 #if 0
489 user_data = NULL;
490 XtVaGetValues (widget, XtNuserData, &user_data, NULL);
491 #else
492 /* Damn! Athena doesn't give us a way to hang our own data on the
493 buttons, so we have to go find it... I guess this assumes that
494 all instances of a button have the same call data. */
496 widget_value *val = instance->info->val->contents;
497 char *name = XtName (widget);
498 while (val)
500 if (val->name && !strcmp (val->name, name))
501 break;
502 val = val->next;
504 if (! val) abort ();
505 user_data = val->call_data;
507 #endif
509 if (instance->info->selection_cb)
510 instance->info->selection_cb (widget, id, user_data);
513 static void
514 wm_delete_window (shell, closure, call_data)
515 Widget shell;
516 XtPointer closure;
517 XtPointer call_data;
519 LWLIB_ID id;
520 Cardinal nkids;
521 int i;
522 Widget *kids = 0;
523 Widget widget;
524 if (! XtIsSubclass (shell, shellWidgetClass))
525 abort ();
526 XtVaGetValues (shell, XtNnumChildren, &nkids, NULL);
527 XtVaGetValues (shell, XtNchildren, &kids, NULL);
528 if (!kids || !*kids)
529 abort ();
530 for (i = 0; i < nkids; i++)
532 widget = kids[i];
533 if (XtIsSubclass (widget, dialogWidgetClass))
534 break;
536 id = lw_get_widget_id (widget);
537 if (! id) abort ();
540 widget_info *info = lw_get_widget_info (id);
541 if (! info) abort ();
542 if (info->selection_cb)
543 info->selection_cb (widget, id, (XtPointer) -1);
546 lw_destroy_all_widgets (id);
550 /* Scrollbars */
552 #if 0
553 static void
554 xaw_scrollbar_scroll (widget, closure, call_data)
555 Widget widget;
556 XtPointer closure;
557 XtPointer call_data;
559 widget_instance *instance = (widget_instance *) closure;
560 LWLIB_ID id;
561 scroll_event event_data;
563 if (!instance || widget->core.being_destroyed)
564 return;
566 id = instance->info->id;
567 event_data.slider_value = 0;
568 event_data.time = 0;
570 if ((int) call_data > 0)
571 event_data.action = SCROLLBAR_PAGE_DOWN;
572 else
573 event_data.action = SCROLLBAR_PAGE_UP;
575 if (instance->info->pre_activate_cb)
576 instance->info->pre_activate_cb (widget, id, (XtPointer) &event_data);
578 #endif
580 #if 0
581 static void
582 xaw_scrollbar_jump (widget, closure, call_data)
583 Widget widget;
584 XtPointer closure;
585 XtPointer call_data;
587 widget_instance *instance = (widget_instance *) closure;
588 LWLIB_ID id;
589 scroll_event event_data;
590 scrollbar_values *val =
591 (scrollbar_values *) instance->info->val->scrollbar_data;
592 float percent;
594 if (!instance || widget->core.being_destroyed)
595 return;
597 id = instance->info->id;
599 percent = * (float *) call_data;
600 event_data.slider_value =
601 (int) (percent * (float) (val->maximum - val->minimum)) + val->minimum;
603 event_data.time = 0;
604 event_data.action = SCROLLBAR_DRAG;
606 if (instance->info->pre_activate_cb)
607 instance->info->pre_activate_cb (widget, id, (XtPointer) &event_data);
609 #endif
611 static Widget
612 xaw_create_scrollbar (instance)
613 widget_instance *instance;
615 #if 0
616 Arg av[20];
617 int ac = 0;
618 Dimension width;
619 Widget scrollbar;
621 XtVaGetValues (instance->parent, XtNwidth, &width, NULL);
623 XtSetArg (av[ac], XtNshowGrip, 0); ac++;
624 XtSetArg (av[ac], XtNresizeToPreferred, 1); ac++;
625 XtSetArg (av[ac], XtNallowResize, True); ac++;
626 XtSetArg (av[ac], XtNskipAdjust, True); ac++;
627 XtSetArg (av[ac], XtNwidth, width); ac++;
628 XtSetArg (av[ac], XtNmappedWhenManaged, True); ac++;
630 scrollbar =
631 XtCreateWidget (instance->info->name, scrollbarWidgetClass,
632 instance->parent, av, ac);
634 /* We have to force the border width to be 0 otherwise the
635 geometry manager likes to start looping for awhile... */
636 XtVaSetValues (scrollbar, XtNborderWidth, 0, NULL);
638 XtRemoveAllCallbacks (scrollbar, "jumpProc");
639 XtRemoveAllCallbacks (scrollbar, "scrollProc");
641 XtAddCallback (scrollbar, "jumpProc", xaw_scrollbar_jump,
642 (XtPointer) instance);
643 XtAddCallback (scrollbar, "scrollProc", xaw_scrollbar_scroll,
644 (XtPointer) instance);
646 return scrollbar;
647 #else
648 return NULL;
649 #endif
652 static Widget
653 xaw_create_main (instance)
654 widget_instance *instance;
656 Arg al[1];
657 int ac;
659 /* Create a vertical Paned to hold menubar */
660 ac = 0;
661 XtSetArg (al[ac], XtNborderWidth, 0); ac++;
662 return XtCreateWidget (instance->info->name, panedWidgetClass,
663 instance->parent, al, ac);
666 widget_creation_entry
667 xaw_creation_table [] =
669 {"scrollbar", xaw_create_scrollbar},
670 {"main", xaw_create_main},
671 {NULL, NULL}