Use defcustom for user variables.
[emacs.git] / lwlib / lwlib-Xaw.c
blob21685659ae565accfc538bc1582de9d8a776d216
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 #include <stdio.h>
23 #include "lwlib-Xaw.h"
25 #include <X11/StringDefs.h>
26 #include <X11/IntrinsicP.h>
27 #include <X11/CoreP.h>
28 #include <X11/Shell.h>
30 #include <X11/Xaw/Scrollbar.h>
31 #include <X11/Xaw/Paned.h>
32 #include <X11/Xaw/Dialog.h>
33 #include <X11/Xaw/Form.h>
34 #include <X11/Xaw/Command.h>
35 #include <X11/Xaw/Label.h>
37 #include <X11/Xatom.h>
39 static void xaw_generic_callback (/*Widget, XtPointer, XtPointer*/);
42 Boolean
43 lw_xaw_widget_p (widget)
44 Widget widget;
46 return (XtIsSubclass (widget, scrollbarWidgetClass) ||
47 XtIsSubclass (widget, dialogWidgetClass));
50 static void
51 xaw_update_scrollbar (instance, widget, val)
52 widget_instance *instance;
53 Widget widget;
54 widget_value *val;
56 #if 0
57 if (val->scrollbar_data)
59 scrollbar_values *data = val->scrollbar_data;
60 Dimension height, width;
61 Dimension pos_x, pos_y;
62 int widget_shown, widget_topOfThumb;
63 float new_shown, new_topOfThumb;
65 XtVaGetValues (widget,
66 XtNheight, &height,
67 XtNwidth, &width,
68 XtNx, &pos_x,
69 XtNy, &pos_y,
70 XtNtopOfThumb, &widget_topOfThumb,
71 XtNshown, &widget_shown,
72 0);
75 * First size and position the scrollbar widget.
76 * We need to position it to second-guess the Paned widget's notion
77 * of what should happen when the WMShell gets resized.
79 if (height != data->scrollbar_height || pos_y != data->scrollbar_pos)
81 XtConfigureWidget (widget, pos_x, data->scrollbar_pos,
82 width, data->scrollbar_height, 0);
84 XtVaSetValues (widget,
85 XtNlength, data->scrollbar_height,
86 XtNthickness, width,
87 0);
91 * Now the size the scrollbar's slider.
93 new_shown = (float) data->slider_size /
94 (float) (data->maximum - data->minimum);
96 new_topOfThumb = (float) (data->slider_position - data->minimum) /
97 (float) (data->maximum - data->minimum);
99 if (new_shown > 1.0)
100 new_shown = 1.0;
101 if (new_shown < 0)
102 new_shown = 0;
104 if (new_topOfThumb > 1.0)
105 new_topOfThumb = 1.0;
106 if (new_topOfThumb < 0)
107 new_topOfThumb = 0;
109 if (new_shown != widget_shown || new_topOfThumb != widget_topOfThumb)
110 XawScrollbarSetThumb (widget, new_topOfThumb, new_shown);
112 #endif
115 void
116 xaw_update_one_widget (instance, widget, val, deep_p)
117 widget_instance *instance;
118 Widget widget;
119 widget_value *val;
120 Boolean deep_p;
122 #if 0
123 if (XtIsSubclass (widget, scrollbarWidgetClass))
125 xaw_update_scrollbar (instance, widget, val);
127 #endif
128 if (XtIsSubclass (widget, dialogWidgetClass))
130 Arg al[1];
131 int ac = 0;
132 XtSetArg (al[ac], XtNlabel, val->contents->value); ac++;
133 XtSetValues (widget, al, ac);
135 else if (XtIsSubclass (widget, commandWidgetClass))
137 Dimension bw = 0;
138 Arg al[3];
140 XtVaGetValues (widget, XtNborderWidth, &bw, 0);
141 if (bw == 0)
142 /* Don't let buttons end up with 0 borderwidth, that's ugly...
143 Yeah, all this should really be done through app-defaults files
144 or fallback resources, but that's a whole different can of worms
145 that I don't feel like opening right now. Making Athena widgets
146 not look like shit is just entirely too much work.
149 XtSetArg (al[0], XtNborderWidth, 1);
150 XtSetValues (widget, al, 1);
153 XtSetArg (al[0], XtNlabel, val->value);
154 XtSetArg (al[1], XtNsensitive, val->enabled);
155 /* Force centered button text. Se above. */
156 XtSetArg (al[2], XtNjustify, XtJustifyCenter);
157 XtSetValues (widget, al, 3);
158 XtRemoveAllCallbacks (widget, XtNcallback);
159 XtAddCallback (widget, XtNcallback, xaw_generic_callback, instance);
163 void
164 xaw_update_one_value (instance, widget, val)
165 widget_instance *instance;
166 Widget widget;
167 widget_value *val;
169 /* This function is not used by the scrollbars and those are the only
170 Athena widget implemented at the moment so do nothing. */
171 return;
174 void
175 xaw_destroy_instance (instance)
176 widget_instance *instance;
178 if (XtIsSubclass (instance->widget, dialogWidgetClass))
179 /* Need to destroy the Shell too. */
180 XtDestroyWidget (XtParent (instance->widget));
181 else
182 XtDestroyWidget (instance->widget);
185 void
186 xaw_popup_menu (widget)
187 Widget widget;
189 /* An Athena menubar has not been implemented. */
190 return;
193 void
194 xaw_pop_instance (instance, up)
195 widget_instance *instance;
196 Boolean up;
198 Widget widget = instance->widget;
200 if (up)
202 if (XtIsSubclass (widget, dialogWidgetClass))
204 /* For dialogs, we need to call XtPopup on the parent instead
205 of calling XtManageChild on the widget.
206 Also we need to hack the shell's WM_PROTOCOLS to get it to
207 understand what the close box is supposed to do!!
209 Display *dpy = XtDisplay (widget);
210 Widget shell = XtParent (widget);
211 Atom props [2];
212 int i = 0;
213 props [i++] = XInternAtom (dpy, "WM_DELETE_WINDOW", False);
214 XChangeProperty (dpy, XtWindow (shell),
215 XInternAtom (dpy, "WM_PROTOCOLS", False),
216 XA_ATOM, 32, PropModeAppend,
217 (unsigned char *) props, i);
219 /* Center the widget in its parent. Why isn't this kind of crap
220 done automatically? I thought toolkits were supposed to make
221 life easier?
224 unsigned int x, y, w, h;
225 Widget topmost = instance->parent;
226 Arg args[2];
228 w = shell->core.width;
229 h = shell->core.height;
230 while (topmost->core.parent && XtIsRealized (topmost->core.parent))
231 topmost = topmost->core.parent;
232 if (topmost->core.width < w) x = topmost->core.x;
233 else x = topmost->core.x + ((topmost->core.width - w) / 2);
234 if (topmost->core.height < h) y = topmost->core.y;
235 else y = topmost->core.y + ((topmost->core.height - h) / 2);
236 /* Using XtMoveWidget caused the widget to come
237 out in the wrong place with vtwm.
238 Question of virtual vs real coords, perhaps. */
239 XtSetArg (args[0], XtNx, x);
240 XtSetArg (args[1], XtNy, y);
241 XtSetValues (shell, args, 2);
244 /* Finally, pop it up. */
245 XtPopup (shell, XtGrabNonexclusive);
247 else
248 XtManageChild (widget);
250 else
252 if (XtIsSubclass (widget, dialogWidgetClass))
253 XtUnmanageChild (XtParent (widget));
254 else
255 XtUnmanageChild (widget);
260 /* Dialog boxes */
262 static char overrideTrans[] =
263 "<Message>WM_PROTOCOLS: lwlib_delete_dialog()";
264 static void wm_delete_window();
265 static XtActionsRec xaw_actions [] = {
266 {"lwlib_delete_dialog", wm_delete_window}
268 static Boolean actions_initted = False;
270 static Widget
271 make_dialog (name, parent, pop_up_p, shell_title, icon_name, text_input_slot, radio_box, list, left_buttons, right_buttons)
272 char* name;
273 Widget parent;
274 Boolean pop_up_p;
275 char* shell_title;
276 char* icon_name;
277 Boolean text_input_slot;
278 Boolean radio_box;
279 Boolean list;
280 int left_buttons;
281 int right_buttons;
283 Arg av [20];
284 int ac = 0;
285 int i, bc;
286 char button_name [255];
287 Widget shell;
288 Widget dialog;
289 Widget button;
290 XtTranslations override;
292 if (! pop_up_p) abort (); /* not implemented */
293 if (text_input_slot) abort (); /* not implemented */
294 if (radio_box) abort (); /* not implemented */
295 if (list) abort (); /* not implemented */
297 if (! actions_initted)
299 XtAppContext app = XtWidgetToApplicationContext (parent);
300 XtAppAddActions (app, xaw_actions,
301 sizeof (xaw_actions) / sizeof (xaw_actions[0]));
302 actions_initted = True;
305 override = XtParseTranslationTable (overrideTrans);
307 ac = 0;
308 XtSetArg (av[ac], XtNtitle, shell_title); ac++;
309 XtSetArg (av[ac], XtNallowShellResize, True); ac++;
311 /* Don't allow any geometry request from the user. */
312 XtSetArg (av[ac], XtNgeometry, 0); ac++;
314 shell = XtCreatePopupShell ("dialog", transientShellWidgetClass,
315 parent, av, ac);
316 XtOverrideTranslations (shell, override);
318 ac = 0;
319 dialog = XtCreateManagedWidget (name, dialogWidgetClass, shell, av, ac);
321 bc = 0;
322 button = 0;
323 for (i = 0; i < left_buttons; i++)
325 ac = 0;
326 XtSetArg (av [ac], XtNfromHoriz, button); ac++;
327 XtSetArg (av [ac], XtNleft, XtChainLeft); ac++;
328 XtSetArg (av [ac], XtNright, XtChainLeft); ac++;
329 XtSetArg (av [ac], XtNtop, XtChainBottom); ac++;
330 XtSetArg (av [ac], XtNbottom, XtChainBottom); ac++;
331 XtSetArg (av [ac], XtNresizable, True); ac++;
332 sprintf (button_name, "button%d", ++bc);
333 button = XtCreateManagedWidget (button_name, commandWidgetClass,
334 dialog, av, ac);
336 if (right_buttons)
338 /* Create a separator
340 I want the separator to take up the slack between the buttons on
341 the right and the buttons on the left (that is I want the buttons
342 after the separator to be packed against the right edge of the
343 window) but I can't seem to make it do it.
345 ac = 0;
346 XtSetArg (av [ac], XtNfromHoriz, button); ac++;
347 /* XtSetArg (av [ac], XtNfromVert, XtNameToWidget (dialog, "label")); ac++; */
348 XtSetArg (av [ac], XtNleft, XtChainLeft); ac++;
349 XtSetArg (av [ac], XtNright, XtChainRight); ac++;
350 XtSetArg (av [ac], XtNtop, XtChainBottom); ac++;
351 XtSetArg (av [ac], XtNbottom, XtChainBottom); ac++;
352 XtSetArg (av [ac], XtNlabel, ""); ac++;
353 XtSetArg (av [ac], XtNwidth, 30); ac++; /* #### aaack!! */
354 XtSetArg (av [ac], XtNborderWidth, 0); ac++;
355 XtSetArg (av [ac], XtNshapeStyle, XmuShapeRectangle); ac++;
356 XtSetArg (av [ac], XtNresizable, False); ac++;
357 XtSetArg (av [ac], XtNsensitive, False); ac++;
358 button = XtCreateManagedWidget ("separator",
359 /* labelWidgetClass, */
360 /* This has to be Command to fake out
361 the Dialog widget... */
362 commandWidgetClass,
363 dialog, av, ac);
365 for (i = 0; i < right_buttons; i++)
367 ac = 0;
368 XtSetArg (av [ac], XtNfromHoriz, button); ac++;
369 XtSetArg (av [ac], XtNleft, XtChainRight); ac++;
370 XtSetArg (av [ac], XtNright, XtChainRight); ac++;
371 XtSetArg (av [ac], XtNtop, XtChainBottom); ac++;
372 XtSetArg (av [ac], XtNbottom, XtChainBottom); ac++;
373 XtSetArg (av [ac], XtNresizable, True); ac++;
374 sprintf (button_name, "button%d", ++bc);
375 button = XtCreateManagedWidget (button_name, commandWidgetClass,
376 dialog, av, ac);
379 return dialog;
382 Widget
383 xaw_create_dialog (instance)
384 widget_instance* instance;
386 char *name = instance->info->type;
387 Widget parent = instance->parent;
388 Widget widget;
389 Boolean pop_up_p = instance->pop_up_p;
390 char *shell_name = 0;
391 char *icon_name;
392 Boolean text_input_slot = False;
393 Boolean radio_box = False;
394 Boolean list = False;
395 int total_buttons;
396 int left_buttons = 0;
397 int right_buttons = 1;
399 switch (name [0]) {
400 case 'E': case 'e':
401 icon_name = "dbox-error";
402 shell_name = "Error";
403 break;
405 case 'I': case 'i':
406 icon_name = "dbox-info";
407 shell_name = "Information";
408 break;
410 case 'L': case 'l':
411 list = True;
412 icon_name = "dbox-question";
413 shell_name = "Prompt";
414 break;
416 case 'P': case 'p':
417 text_input_slot = True;
418 icon_name = "dbox-question";
419 shell_name = "Prompt";
420 break;
422 case 'Q': case 'q':
423 icon_name = "dbox-question";
424 shell_name = "Question";
425 break;
428 total_buttons = name [1] - '0';
430 if (name [3] == 'T' || name [3] == 't')
432 text_input_slot = False;
433 radio_box = True;
435 else if (name [3])
436 right_buttons = name [4] - '0';
438 left_buttons = total_buttons - right_buttons;
440 widget = make_dialog (name, parent, pop_up_p,
441 shell_name, icon_name, text_input_slot, radio_box,
442 list, left_buttons, right_buttons);
444 return widget;
448 static void
449 xaw_generic_callback (widget, closure, call_data)
450 Widget widget;
451 XtPointer closure;
452 XtPointer call_data;
454 widget_instance *instance = (widget_instance *) closure;
455 Widget instance_widget;
456 LWLIB_ID id;
457 XtPointer user_data;
459 lw_internal_update_other_instances (widget, closure, call_data);
461 if (! instance)
462 return;
463 if (widget->core.being_destroyed)
464 return;
466 instance_widget = instance->widget;
467 if (!instance_widget)
468 return;
470 id = instance->info->id;
472 #if 0
473 user_data = NULL;
474 XtVaGetValues (widget, XtNuserData, &user_data, 0);
475 #else
476 /* Damn! Athena doesn't give us a way to hang our own data on the
477 buttons, so we have to go find it... I guess this assumes that
478 all instances of a button have the same call data. */
480 widget_value *val = instance->info->val->contents;
481 char *name = XtName (widget);
482 while (val)
484 if (val->name && !strcmp (val->name, name))
485 break;
486 val = val->next;
488 if (! val) abort ();
489 user_data = val->call_data;
491 #endif
493 if (instance->info->selection_cb)
494 instance->info->selection_cb (widget, id, user_data);
497 static void
498 wm_delete_window (shell, closure, call_data)
499 Widget shell;
500 XtPointer closure;
501 XtPointer call_data;
503 LWLIB_ID id;
504 Widget *kids = 0;
505 Widget widget;
506 if (! XtIsSubclass (shell, shellWidgetClass))
507 abort ();
508 XtVaGetValues (shell, XtNchildren, &kids, 0);
509 if (!kids || !*kids)
510 abort ();
511 widget = kids [0];
512 if (! XtIsSubclass (widget, dialogWidgetClass))
513 abort ();
514 id = lw_get_widget_id (widget);
515 if (! id) abort ();
518 widget_info *info = lw_get_widget_info (id);
519 if (! info) abort ();
520 if (info->selection_cb)
521 info->selection_cb (widget, id, (XtPointer) -1);
524 lw_destroy_all_widgets (id);
528 /* Scrollbars */
530 static void
531 xaw_scrollbar_scroll (widget, closure, call_data)
532 Widget widget;
533 XtPointer closure;
534 XtPointer call_data;
536 #if 0
537 widget_instance *instance = (widget_instance *) closure;
538 LWLIB_ID id;
539 scroll_event event_data;
541 if (!instance || widget->core.being_destroyed)
542 return;
544 id = instance->info->id;
545 event_data.slider_value = 0;
546 event_data.time = 0;
548 if ((int) call_data > 0)
549 event_data.action = SCROLLBAR_PAGE_DOWN;
550 else
551 event_data.action = SCROLLBAR_PAGE_UP;
553 if (instance->info->pre_activate_cb)
554 instance->info->pre_activate_cb (widget, id, (XtPointer) &event_data);
555 #endif
558 static void
559 xaw_scrollbar_jump (widget, closure, call_data)
560 Widget widget;
561 XtPointer closure;
562 XtPointer call_data;
564 #if 0
565 widget_instance *instance = (widget_instance *) closure;
566 LWLIB_ID id;
567 scroll_event event_data;
568 scrollbar_values *val =
569 (scrollbar_values *) instance->info->val->scrollbar_data;
570 float percent;
572 if (!instance || widget->core.being_destroyed)
573 return;
575 id = instance->info->id;
577 percent = * (float *) call_data;
578 event_data.slider_value =
579 (int) (percent * (float) (val->maximum - val->minimum)) + val->minimum;
581 event_data.time = 0;
582 event_data.action = SCROLLBAR_DRAG;
584 if (instance->info->pre_activate_cb)
585 instance->info->pre_activate_cb (widget, id, (XtPointer) &event_data);
586 #endif
589 static Widget
590 xaw_create_scrollbar (instance)
591 widget_instance *instance;
593 #if 0
594 Arg av[20];
595 int ac = 0;
596 Dimension width;
597 Widget scrollbar;
599 XtVaGetValues (instance->parent, XtNwidth, &width, 0);
601 XtSetArg (av[ac], XtNshowGrip, 0); ac++;
602 XtSetArg (av[ac], XtNresizeToPreferred, 1); ac++;
603 XtSetArg (av[ac], XtNallowResize, True); ac++;
604 XtSetArg (av[ac], XtNskipAdjust, True); ac++;
605 XtSetArg (av[ac], XtNwidth, width); ac++;
606 XtSetArg (av[ac], XtNmappedWhenManaged, True); ac++;
608 scrollbar =
609 XtCreateWidget (instance->info->name, scrollbarWidgetClass,
610 instance->parent, av, ac);
612 /* We have to force the border width to be 0 otherwise the
613 geometry manager likes to start looping for awhile... */
614 XtVaSetValues (scrollbar, XtNborderWidth, 0, 0);
616 XtRemoveAllCallbacks (scrollbar, "jumpProc");
617 XtRemoveAllCallbacks (scrollbar, "scrollProc");
619 XtAddCallback (scrollbar, "jumpProc", xaw_scrollbar_jump,
620 (XtPointer) instance);
621 XtAddCallback (scrollbar, "scrollProc", xaw_scrollbar_scroll,
622 (XtPointer) instance);
624 return scrollbar;
625 #endif
628 static Widget
629 xaw_create_main (instance)
630 widget_instance *instance;
632 Arg al[1];
633 int ac;
635 /* Create a vertical Paned to hold menubar */
636 ac = 0;
637 XtSetArg (al[ac], XtNborderWidth, 0); ac++;
638 return XtCreateWidget (instance->info->name, panedWidgetClass,
639 instance->parent, al, ac);
642 widget_creation_entry
643 xaw_creation_table [] =
645 {"scrollbar", xaw_create_scrollbar},
646 {"main", xaw_create_main},
647 {NULL, NULL}