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)
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. */
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*/);
49 lw_xaw_widget_p (widget
)
52 return (XtIsSubclass (widget
, scrollbarWidgetClass
) ||
53 XtIsSubclass (widget
, dialogWidgetClass
));
58 xaw_update_scrollbar (instance
, widget
, val
)
59 widget_instance
*instance
;
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
,
76 XtNtopOfThumb
, &widget_topOfThumb
,
77 XtNshown
, &widget_shown
,
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
,
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
);
110 if (new_topOfThumb
> 1.0)
111 new_topOfThumb
= 1.0;
112 if (new_topOfThumb
< 0)
115 if (new_shown
!= widget_shown
|| new_topOfThumb
!= widget_topOfThumb
)
116 XawScrollbarSetThumb (widget
, new_topOfThumb
, new_shown
);
122 xaw_update_one_widget (instance
, widget
, val
, deep_p
)
123 widget_instance
*instance
;
129 if (XtIsSubclass (widget
, scrollbarWidgetClass
))
131 xaw_update_scrollbar (instance
, widget
, val
);
134 if (XtIsSubclass (widget
, dialogWidgetClass
))
138 XtSetArg (al
[ac
], XtNlabel
, val
->contents
->value
); ac
++;
139 XtSetValues (widget
, al
, ac
);
141 else if (XtIsSubclass (widget
, commandWidgetClass
))
146 XtVaGetValues (widget
, XtNborderWidth
, &bw
, NULL
);
148 /* Don't let buttons end up with 0 borderwidth, that's ugly...
149 Yeah, all this should really be done through app-defaults files
150 or fallback resources, but that's a whole different can of worms
151 that I don't feel like opening right now. Making Athena widgets
152 not look like shit is just entirely too much work.
155 XtSetArg (al
[0], XtNborderWidth
, 1);
156 XtSetValues (widget
, al
, 1);
159 XtSetSensitive (widget
, val
->enabled
);
160 XtSetArg (al
[0], XtNlabel
, val
->value
);
161 /* Force centered button text. Se above. */
162 XtSetArg (al
[1], XtNjustify
, XtJustifyCenter
);
163 XtSetValues (widget
, al
, 2);
164 XtRemoveAllCallbacks (widget
, XtNcallback
);
165 XtAddCallback (widget
, XtNcallback
, xaw_generic_callback
, instance
);
170 xaw_update_one_value (instance
, widget
, val
)
171 widget_instance
*instance
;
175 /* This function is not used by the scrollbars and those are the only
176 Athena widget implemented at the moment so do nothing. */
181 xaw_destroy_instance (instance
)
182 widget_instance
*instance
;
184 if (XtIsSubclass (instance
->widget
, dialogWidgetClass
))
185 /* Need to destroy the Shell too. */
186 XtDestroyWidget (XtParent (instance
->widget
));
188 XtDestroyWidget (instance
->widget
);
192 xaw_popup_menu (widget
, event
)
196 /* An Athena menubar has not been implemented. */
201 xaw_pop_instance (instance
, up
)
202 widget_instance
*instance
;
205 Widget widget
= instance
->widget
;
209 if (XtIsSubclass (widget
, dialogWidgetClass
))
211 /* For dialogs, we need to call XtPopup on the parent instead
212 of calling XtManageChild on the widget.
213 Also we need to hack the shell's WM_PROTOCOLS to get it to
214 understand what the close box is supposed to do!!
216 Display
*dpy
= XtDisplay (widget
);
217 Widget shell
= XtParent (widget
);
220 props
[i
++] = XInternAtom (dpy
, "WM_DELETE_WINDOW", False
);
221 XChangeProperty (dpy
, XtWindow (shell
),
222 XInternAtom (dpy
, "WM_PROTOCOLS", False
),
223 XA_ATOM
, 32, PropModeAppend
,
224 (unsigned char *) props
, i
);
226 /* Center the widget in its parent. Why isn't this kind of crap
227 done automatically? I thought toolkits were supposed to make
231 unsigned int x
, y
, w
, h
;
232 Widget topmost
= instance
->parent
;
235 w
= shell
->core
.width
;
236 h
= shell
->core
.height
;
237 while (topmost
->core
.parent
&& XtIsRealized (topmost
->core
.parent
))
238 topmost
= topmost
->core
.parent
;
239 if (topmost
->core
.width
< w
) x
= topmost
->core
.x
;
240 else x
= topmost
->core
.x
+ ((topmost
->core
.width
- w
) / 2);
241 if (topmost
->core
.height
< h
) y
= topmost
->core
.y
;
242 else y
= topmost
->core
.y
+ ((topmost
->core
.height
- h
) / 2);
243 /* Using XtMoveWidget caused the widget to come
244 out in the wrong place with vtwm.
245 Question of virtual vs real coords, perhaps. */
246 XtSetArg (args
[0], XtNx
, x
);
247 XtSetArg (args
[1], XtNy
, y
);
248 XtSetValues (shell
, args
, 2);
251 /* Finally, pop it up. */
252 XtPopup (shell
, XtGrabNonexclusive
);
255 XtManageChild (widget
);
259 if (XtIsSubclass (widget
, dialogWidgetClass
))
260 XtUnmanageChild (XtParent (widget
));
262 XtUnmanageChild (widget
);
269 static char overrideTrans
[] =
270 "<Message>WM_PROTOCOLS: lwlib_delete_dialog()";
271 static void wm_delete_window();
272 static XtActionsRec xaw_actions
[] = {
273 {"lwlib_delete_dialog", wm_delete_window
}
275 static Boolean actions_initted
= False
;
278 make_dialog (name
, parent
, pop_up_p
, shell_title
, icon_name
, text_input_slot
, radio_box
, list
, left_buttons
, right_buttons
)
284 Boolean text_input_slot
;
293 char button_name
[255];
297 XtTranslations override
;
299 if (! pop_up_p
) abort (); /* not implemented */
300 if (text_input_slot
) abort (); /* not implemented */
301 if (radio_box
) abort (); /* not implemented */
302 if (list
) abort (); /* not implemented */
304 if (! actions_initted
)
306 XtAppContext app
= XtWidgetToApplicationContext (parent
);
307 XtAppAddActions (app
, xaw_actions
,
308 sizeof (xaw_actions
) / sizeof (xaw_actions
[0]));
309 actions_initted
= True
;
312 override
= XtParseTranslationTable (overrideTrans
);
315 XtSetArg (av
[ac
], XtNtitle
, shell_title
); ac
++;
316 XtSetArg (av
[ac
], XtNallowShellResize
, True
); ac
++;
318 /* Don't allow any geometry request from the user. */
319 XtSetArg (av
[ac
], XtNgeometry
, 0); ac
++;
321 shell
= XtCreatePopupShell ("dialog", transientShellWidgetClass
,
323 XtOverrideTranslations (shell
, override
);
326 dialog
= XtCreateManagedWidget (name
, dialogWidgetClass
, shell
, av
, ac
);
330 for (i
= 0; i
< left_buttons
; i
++)
333 XtSetArg (av
[ac
], XtNfromHoriz
, button
); ac
++;
334 XtSetArg (av
[ac
], XtNleft
, XtChainLeft
); ac
++;
335 XtSetArg (av
[ac
], XtNright
, XtChainLeft
); ac
++;
336 XtSetArg (av
[ac
], XtNtop
, XtChainBottom
); ac
++;
337 XtSetArg (av
[ac
], XtNbottom
, XtChainBottom
); ac
++;
338 XtSetArg (av
[ac
], XtNresizable
, True
); ac
++;
339 sprintf (button_name
, "button%d", ++bc
);
340 button
= XtCreateManagedWidget (button_name
, commandWidgetClass
,
345 /* Create a separator
347 I want the separator to take up the slack between the buttons on
348 the right and the buttons on the left (that is I want the buttons
349 after the separator to be packed against the right edge of the
350 window) but I can't seem to make it do it.
353 XtSetArg (av
[ac
], XtNfromHoriz
, button
); ac
++;
354 /* XtSetArg (av [ac], XtNfromVert, XtNameToWidget (dialog, "label")); ac++; */
355 XtSetArg (av
[ac
], XtNleft
, XtChainLeft
); ac
++;
356 XtSetArg (av
[ac
], XtNright
, XtChainRight
); ac
++;
357 XtSetArg (av
[ac
], XtNtop
, XtChainBottom
); ac
++;
358 XtSetArg (av
[ac
], XtNbottom
, XtChainBottom
); ac
++;
359 XtSetArg (av
[ac
], XtNlabel
, ""); ac
++;
360 XtSetArg (av
[ac
], XtNwidth
, 30); ac
++; /* #### aaack!! */
361 XtSetArg (av
[ac
], XtNborderWidth
, 0); ac
++;
362 XtSetArg (av
[ac
], XtNshapeStyle
, XmuShapeRectangle
); ac
++;
363 XtSetArg (av
[ac
], XtNresizable
, False
); ac
++;
364 XtSetArg (av
[ac
], XtNsensitive
, False
); ac
++;
365 button
= XtCreateManagedWidget ("separator",
366 /* labelWidgetClass, */
367 /* This has to be Command to fake out
368 the Dialog widget... */
372 for (i
= 0; i
< right_buttons
; i
++)
375 XtSetArg (av
[ac
], XtNfromHoriz
, button
); ac
++;
376 XtSetArg (av
[ac
], XtNleft
, XtChainRight
); ac
++;
377 XtSetArg (av
[ac
], XtNright
, XtChainRight
); ac
++;
378 XtSetArg (av
[ac
], XtNtop
, XtChainBottom
); ac
++;
379 XtSetArg (av
[ac
], XtNbottom
, XtChainBottom
); ac
++;
380 XtSetArg (av
[ac
], XtNresizable
, True
); ac
++;
381 sprintf (button_name
, "button%d", ++bc
);
382 button
= XtCreateManagedWidget (button_name
, commandWidgetClass
,
390 xaw_create_dialog (instance
)
391 widget_instance
* instance
;
393 char *name
= instance
->info
->type
;
394 Widget parent
= instance
->parent
;
396 Boolean pop_up_p
= instance
->pop_up_p
;
397 char *shell_name
= 0;
399 Boolean text_input_slot
= False
;
400 Boolean radio_box
= False
;
401 Boolean list
= False
;
403 int left_buttons
= 0;
404 int right_buttons
= 1;
408 icon_name
= "dbox-error";
409 shell_name
= "Error";
413 icon_name
= "dbox-info";
414 shell_name
= "Information";
419 icon_name
= "dbox-question";
420 shell_name
= "Prompt";
424 text_input_slot
= True
;
425 icon_name
= "dbox-question";
426 shell_name
= "Prompt";
430 icon_name
= "dbox-question";
431 shell_name
= "Question";
435 total_buttons
= name
[1] - '0';
437 if (name
[3] == 'T' || name
[3] == 't')
439 text_input_slot
= False
;
443 right_buttons
= name
[4] - '0';
445 left_buttons
= total_buttons
- right_buttons
;
447 widget
= make_dialog (name
, parent
, pop_up_p
,
448 shell_name
, icon_name
, text_input_slot
, radio_box
,
449 list
, left_buttons
, right_buttons
);
456 xaw_generic_callback (widget
, closure
, call_data
)
461 widget_instance
*instance
= (widget_instance
*) closure
;
462 Widget instance_widget
;
466 lw_internal_update_other_instances (widget
, closure
, call_data
);
470 if (widget
->core
.being_destroyed
)
473 instance_widget
= instance
->widget
;
474 if (!instance_widget
)
477 id
= instance
->info
->id
;
481 XtVaGetValues (widget
, XtNuserData
, &user_data
, NULL
);
483 /* Damn! Athena doesn't give us a way to hang our own data on the
484 buttons, so we have to go find it... I guess this assumes that
485 all instances of a button have the same call data. */
487 widget_value
*val
= instance
->info
->val
->contents
;
488 char *name
= XtName (widget
);
491 if (val
->name
&& !strcmp (val
->name
, name
))
496 user_data
= val
->call_data
;
500 if (instance
->info
->selection_cb
)
501 instance
->info
->selection_cb (widget
, id
, user_data
);
505 wm_delete_window (shell
, closure
, call_data
)
515 if (! XtIsSubclass (shell
, shellWidgetClass
))
517 XtVaGetValues (shell
, XtNnumChildren
, &nkids
, NULL
);
518 XtVaGetValues (shell
, XtNchildren
, &kids
, NULL
);
521 for (i
= 0; i
< nkids
; i
++)
524 if (XtIsSubclass (widget
, dialogWidgetClass
))
527 id
= lw_get_widget_id (widget
);
531 widget_info
*info
= lw_get_widget_info (id
);
532 if (! info
) abort ();
533 if (info
->selection_cb
)
534 info
->selection_cb (widget
, id
, (XtPointer
) -1);
537 lw_destroy_all_widgets (id
);
545 xaw_scrollbar_scroll (widget
, closure
, call_data
)
550 widget_instance
*instance
= (widget_instance
*) closure
;
552 scroll_event event_data
;
554 if (!instance
|| widget
->core
.being_destroyed
)
557 id
= instance
->info
->id
;
558 event_data
.slider_value
= 0;
561 if ((int) call_data
> 0)
562 event_data
.action
= SCROLLBAR_PAGE_DOWN
;
564 event_data
.action
= SCROLLBAR_PAGE_UP
;
566 if (instance
->info
->pre_activate_cb
)
567 instance
->info
->pre_activate_cb (widget
, id
, (XtPointer
) &event_data
);
573 xaw_scrollbar_jump (widget
, closure
, call_data
)
578 widget_instance
*instance
= (widget_instance
*) closure
;
580 scroll_event event_data
;
581 scrollbar_values
*val
=
582 (scrollbar_values
*) instance
->info
->val
->scrollbar_data
;
585 if (!instance
|| widget
->core
.being_destroyed
)
588 id
= instance
->info
->id
;
590 percent
= * (float *) call_data
;
591 event_data
.slider_value
=
592 (int) (percent
* (float) (val
->maximum
- val
->minimum
)) + val
->minimum
;
595 event_data
.action
= SCROLLBAR_DRAG
;
597 if (instance
->info
->pre_activate_cb
)
598 instance
->info
->pre_activate_cb (widget
, id
, (XtPointer
) &event_data
);
603 xaw_create_scrollbar (instance
)
604 widget_instance
*instance
;
612 XtVaGetValues (instance
->parent
, XtNwidth
, &width
, NULL
);
614 XtSetArg (av
[ac
], XtNshowGrip
, 0); ac
++;
615 XtSetArg (av
[ac
], XtNresizeToPreferred
, 1); ac
++;
616 XtSetArg (av
[ac
], XtNallowResize
, True
); ac
++;
617 XtSetArg (av
[ac
], XtNskipAdjust
, True
); ac
++;
618 XtSetArg (av
[ac
], XtNwidth
, width
); ac
++;
619 XtSetArg (av
[ac
], XtNmappedWhenManaged
, True
); ac
++;
622 XtCreateWidget (instance
->info
->name
, scrollbarWidgetClass
,
623 instance
->parent
, av
, ac
);
625 /* We have to force the border width to be 0 otherwise the
626 geometry manager likes to start looping for awhile... */
627 XtVaSetValues (scrollbar
, XtNborderWidth
, 0, NULL
);
629 XtRemoveAllCallbacks (scrollbar
, "jumpProc");
630 XtRemoveAllCallbacks (scrollbar
, "scrollProc");
632 XtAddCallback (scrollbar
, "jumpProc", xaw_scrollbar_jump
,
633 (XtPointer
) instance
);
634 XtAddCallback (scrollbar
, "scrollProc", xaw_scrollbar_scroll
,
635 (XtPointer
) instance
);
644 xaw_create_main (instance
)
645 widget_instance
*instance
;
650 /* Create a vertical Paned to hold menubar */
652 XtSetArg (al
[ac
], XtNborderWidth
, 0); ac
++;
653 return XtCreateWidget (instance
->info
->name
, panedWidgetClass
,
654 instance
->parent
, al
, ac
);
657 widget_creation_entry
658 xaw_creation_table
[] =
660 {"scrollbar", xaw_create_scrollbar
},
661 {"main", xaw_create_main
},