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 "lwlib-Xaw.h"
29 #include <X11/StringDefs.h>
30 #include <X11/IntrinsicP.h>
31 #include <X11/CoreP.h>
32 #include <X11/Shell.h>
34 #include <X11/Xaw/Scrollbar.h>
35 #include <X11/Xaw/Paned.h>
36 #include <X11/Xaw/Dialog.h>
37 #include <X11/Xaw/Form.h>
38 #include <X11/Xaw/Command.h>
39 #include <X11/Xaw/Label.h>
41 #include <X11/Xatom.h>
43 static void xaw_generic_callback (/*Widget, XtPointer, XtPointer*/);
47 lw_xaw_widget_p (widget
)
50 return (XtIsSubclass (widget
, scrollbarWidgetClass
) ||
51 XtIsSubclass (widget
, dialogWidgetClass
));
55 xaw_update_scrollbar (instance
, widget
, val
)
56 widget_instance
*instance
;
61 if (val
->scrollbar_data
)
63 scrollbar_values
*data
= val
->scrollbar_data
;
64 Dimension height
, width
;
65 Dimension pos_x
, pos_y
;
66 int widget_shown
, widget_topOfThumb
;
67 float new_shown
, new_topOfThumb
;
69 XtVaGetValues (widget
,
74 XtNtopOfThumb
, &widget_topOfThumb
,
75 XtNshown
, &widget_shown
,
79 * First size and position the scrollbar widget.
80 * We need to position it to second-guess the Paned widget's notion
81 * of what should happen when the WMShell gets resized.
83 if (height
!= data
->scrollbar_height
|| pos_y
!= data
->scrollbar_pos
)
85 XtConfigureWidget (widget
, pos_x
, data
->scrollbar_pos
,
86 width
, data
->scrollbar_height
, 0);
88 XtVaSetValues (widget
,
89 XtNlength
, data
->scrollbar_height
,
95 * Now the size the scrollbar's slider.
97 new_shown
= (float) data
->slider_size
/
98 (float) (data
->maximum
- data
->minimum
);
100 new_topOfThumb
= (float) (data
->slider_position
- data
->minimum
) /
101 (float) (data
->maximum
- data
->minimum
);
108 if (new_topOfThumb
> 1.0)
109 new_topOfThumb
= 1.0;
110 if (new_topOfThumb
< 0)
113 if (new_shown
!= widget_shown
|| new_topOfThumb
!= widget_topOfThumb
)
114 XawScrollbarSetThumb (widget
, new_topOfThumb
, new_shown
);
120 xaw_update_one_widget (instance
, widget
, val
, deep_p
)
121 widget_instance
*instance
;
127 if (XtIsSubclass (widget
, scrollbarWidgetClass
))
129 xaw_update_scrollbar (instance
, widget
, val
);
132 if (XtIsSubclass (widget
, dialogWidgetClass
))
136 XtSetArg (al
[ac
], XtNlabel
, val
->contents
->value
); ac
++;
137 XtSetValues (widget
, al
, ac
);
139 else if (XtIsSubclass (widget
, commandWidgetClass
))
144 XtVaGetValues (widget
, XtNborderWidth
, &bw
, 0);
146 /* Don't let buttons end up with 0 borderwidth, that's ugly...
147 Yeah, all this should really be done through app-defaults files
148 or fallback resources, but that's a whole different can of worms
149 that I don't feel like opening right now. Making Athena widgets
150 not look like shit is just entirely too much work.
153 XtSetArg (al
[0], XtNborderWidth
, 1);
154 XtSetValues (widget
, al
, 1);
157 XtSetArg (al
[0], XtNlabel
, val
->value
);
158 XtSetArg (al
[1], XtNsensitive
, val
->enabled
);
159 /* Force centered button text. Se above. */
160 XtSetArg (al
[2], XtNjustify
, XtJustifyCenter
);
161 XtSetValues (widget
, al
, 3);
162 XtRemoveAllCallbacks (widget
, XtNcallback
);
163 XtAddCallback (widget
, XtNcallback
, xaw_generic_callback
, instance
);
168 xaw_update_one_value (instance
, widget
, val
)
169 widget_instance
*instance
;
173 /* This function is not used by the scrollbars and those are the only
174 Athena widget implemented at the moment so do nothing. */
179 xaw_destroy_instance (instance
)
180 widget_instance
*instance
;
182 if (XtIsSubclass (instance
->widget
, dialogWidgetClass
))
183 /* Need to destroy the Shell too. */
184 XtDestroyWidget (XtParent (instance
->widget
));
186 XtDestroyWidget (instance
->widget
);
190 xaw_popup_menu (widget
)
193 /* An Athena menubar has not been implemented. */
198 xaw_pop_instance (instance
, up
)
199 widget_instance
*instance
;
202 Widget widget
= instance
->widget
;
206 if (XtIsSubclass (widget
, dialogWidgetClass
))
208 /* For dialogs, we need to call XtPopup on the parent instead
209 of calling XtManageChild on the widget.
210 Also we need to hack the shell's WM_PROTOCOLS to get it to
211 understand what the close box is supposed to do!!
213 Display
*dpy
= XtDisplay (widget
);
214 Widget shell
= XtParent (widget
);
217 props
[i
++] = XInternAtom (dpy
, "WM_DELETE_WINDOW", False
);
218 XChangeProperty (dpy
, XtWindow (shell
),
219 XInternAtom (dpy
, "WM_PROTOCOLS", False
),
220 XA_ATOM
, 32, PropModeAppend
,
221 (unsigned char *) props
, i
);
223 /* Center the widget in its parent. Why isn't this kind of crap
224 done automatically? I thought toolkits were supposed to make
228 unsigned int x
, y
, w
, h
;
229 Widget topmost
= instance
->parent
;
232 w
= shell
->core
.width
;
233 h
= shell
->core
.height
;
234 while (topmost
->core
.parent
&& XtIsRealized (topmost
->core
.parent
))
235 topmost
= topmost
->core
.parent
;
236 if (topmost
->core
.width
< w
) x
= topmost
->core
.x
;
237 else x
= topmost
->core
.x
+ ((topmost
->core
.width
- w
) / 2);
238 if (topmost
->core
.height
< h
) y
= topmost
->core
.y
;
239 else y
= topmost
->core
.y
+ ((topmost
->core
.height
- h
) / 2);
240 /* Using XtMoveWidget caused the widget to come
241 out in the wrong place with vtwm.
242 Question of virtual vs real coords, perhaps. */
243 XtSetArg (args
[0], XtNx
, x
);
244 XtSetArg (args
[1], XtNy
, y
);
245 XtSetValues (shell
, args
, 2);
248 /* Finally, pop it up. */
249 XtPopup (shell
, XtGrabNonexclusive
);
252 XtManageChild (widget
);
256 if (XtIsSubclass (widget
, dialogWidgetClass
))
257 XtUnmanageChild (XtParent (widget
));
259 XtUnmanageChild (widget
);
266 static char overrideTrans
[] =
267 "<Message>WM_PROTOCOLS: lwlib_delete_dialog()";
268 static void wm_delete_window();
269 static XtActionsRec xaw_actions
[] = {
270 {"lwlib_delete_dialog", wm_delete_window
}
272 static Boolean actions_initted
= False
;
275 make_dialog (name
, parent
, pop_up_p
, shell_title
, icon_name
, text_input_slot
, radio_box
, list
, left_buttons
, right_buttons
)
281 Boolean text_input_slot
;
290 char button_name
[255];
294 XtTranslations override
;
296 if (! pop_up_p
) abort (); /* not implemented */
297 if (text_input_slot
) abort (); /* not implemented */
298 if (radio_box
) abort (); /* not implemented */
299 if (list
) abort (); /* not implemented */
301 if (! actions_initted
)
303 XtAppContext app
= XtWidgetToApplicationContext (parent
);
304 XtAppAddActions (app
, xaw_actions
,
305 sizeof (xaw_actions
) / sizeof (xaw_actions
[0]));
306 actions_initted
= True
;
309 override
= XtParseTranslationTable (overrideTrans
);
312 XtSetArg (av
[ac
], XtNtitle
, shell_title
); ac
++;
313 XtSetArg (av
[ac
], XtNallowShellResize
, True
); ac
++;
315 /* Don't allow any geometry request from the user. */
316 XtSetArg (av
[ac
], XtNgeometry
, 0); ac
++;
318 shell
= XtCreatePopupShell ("dialog", transientShellWidgetClass
,
320 XtOverrideTranslations (shell
, override
);
323 dialog
= XtCreateManagedWidget (name
, dialogWidgetClass
, shell
, av
, ac
);
327 for (i
= 0; i
< left_buttons
; i
++)
330 XtSetArg (av
[ac
], XtNfromHoriz
, button
); ac
++;
331 XtSetArg (av
[ac
], XtNleft
, XtChainLeft
); ac
++;
332 XtSetArg (av
[ac
], XtNright
, XtChainLeft
); ac
++;
333 XtSetArg (av
[ac
], XtNtop
, XtChainBottom
); ac
++;
334 XtSetArg (av
[ac
], XtNbottom
, XtChainBottom
); ac
++;
335 XtSetArg (av
[ac
], XtNresizable
, True
); ac
++;
336 sprintf (button_name
, "button%d", ++bc
);
337 button
= XtCreateManagedWidget (button_name
, commandWidgetClass
,
342 /* Create a separator
344 I want the separator to take up the slack between the buttons on
345 the right and the buttons on the left (that is I want the buttons
346 after the separator to be packed against the right edge of the
347 window) but I can't seem to make it do it.
350 XtSetArg (av
[ac
], XtNfromHoriz
, button
); ac
++;
351 /* XtSetArg (av [ac], XtNfromVert, XtNameToWidget (dialog, "label")); ac++; */
352 XtSetArg (av
[ac
], XtNleft
, XtChainLeft
); 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
], XtNlabel
, ""); ac
++;
357 XtSetArg (av
[ac
], XtNwidth
, 30); ac
++; /* #### aaack!! */
358 XtSetArg (av
[ac
], XtNborderWidth
, 0); ac
++;
359 XtSetArg (av
[ac
], XtNshapeStyle
, XmuShapeRectangle
); ac
++;
360 XtSetArg (av
[ac
], XtNresizable
, False
); ac
++;
361 XtSetArg (av
[ac
], XtNsensitive
, False
); ac
++;
362 button
= XtCreateManagedWidget ("separator",
363 /* labelWidgetClass, */
364 /* This has to be Command to fake out
365 the Dialog widget... */
369 for (i
= 0; i
< right_buttons
; i
++)
372 XtSetArg (av
[ac
], XtNfromHoriz
, button
); ac
++;
373 XtSetArg (av
[ac
], XtNleft
, XtChainRight
); ac
++;
374 XtSetArg (av
[ac
], XtNright
, XtChainRight
); ac
++;
375 XtSetArg (av
[ac
], XtNtop
, XtChainBottom
); ac
++;
376 XtSetArg (av
[ac
], XtNbottom
, XtChainBottom
); ac
++;
377 XtSetArg (av
[ac
], XtNresizable
, True
); ac
++;
378 sprintf (button_name
, "button%d", ++bc
);
379 button
= XtCreateManagedWidget (button_name
, commandWidgetClass
,
387 xaw_create_dialog (instance
)
388 widget_instance
* instance
;
390 char *name
= instance
->info
->type
;
391 Widget parent
= instance
->parent
;
393 Boolean pop_up_p
= instance
->pop_up_p
;
394 char *shell_name
= 0;
396 Boolean text_input_slot
= False
;
397 Boolean radio_box
= False
;
398 Boolean list
= False
;
400 int left_buttons
= 0;
401 int right_buttons
= 1;
405 icon_name
= "dbox-error";
406 shell_name
= "Error";
410 icon_name
= "dbox-info";
411 shell_name
= "Information";
416 icon_name
= "dbox-question";
417 shell_name
= "Prompt";
421 text_input_slot
= True
;
422 icon_name
= "dbox-question";
423 shell_name
= "Prompt";
427 icon_name
= "dbox-question";
428 shell_name
= "Question";
432 total_buttons
= name
[1] - '0';
434 if (name
[3] == 'T' || name
[3] == 't')
436 text_input_slot
= False
;
440 right_buttons
= name
[4] - '0';
442 left_buttons
= total_buttons
- right_buttons
;
444 widget
= make_dialog (name
, parent
, pop_up_p
,
445 shell_name
, icon_name
, text_input_slot
, radio_box
,
446 list
, left_buttons
, right_buttons
);
453 xaw_generic_callback (widget
, closure
, call_data
)
458 widget_instance
*instance
= (widget_instance
*) closure
;
459 Widget instance_widget
;
463 lw_internal_update_other_instances (widget
, closure
, call_data
);
467 if (widget
->core
.being_destroyed
)
470 instance_widget
= instance
->widget
;
471 if (!instance_widget
)
474 id
= instance
->info
->id
;
478 XtVaGetValues (widget
, XtNuserData
, &user_data
, 0);
480 /* Damn! Athena doesn't give us a way to hang our own data on the
481 buttons, so we have to go find it... I guess this assumes that
482 all instances of a button have the same call data. */
484 widget_value
*val
= instance
->info
->val
->contents
;
485 char *name
= XtName (widget
);
488 if (val
->name
&& !strcmp (val
->name
, name
))
493 user_data
= val
->call_data
;
497 if (instance
->info
->selection_cb
)
498 instance
->info
->selection_cb (widget
, id
, user_data
);
502 wm_delete_window (shell
, closure
, call_data
)
512 if (! XtIsSubclass (shell
, shellWidgetClass
))
514 XtVaGetValues (shell
, XtNnumChildren
, &nkids
, 0);
515 XtVaGetValues (shell
, XtNchildren
, &kids
, 0);
518 for (i
= 0; i
< nkids
; i
++)
521 if (XtIsSubclass (widget
, dialogWidgetClass
))
524 id
= lw_get_widget_id (widget
);
528 widget_info
*info
= lw_get_widget_info (id
);
529 if (! info
) abort ();
530 if (info
->selection_cb
)
531 info
->selection_cb (widget
, id
, (XtPointer
) -1);
534 lw_destroy_all_widgets (id
);
541 xaw_scrollbar_scroll (widget
, closure
, call_data
)
547 widget_instance
*instance
= (widget_instance
*) closure
;
549 scroll_event event_data
;
551 if (!instance
|| widget
->core
.being_destroyed
)
554 id
= instance
->info
->id
;
555 event_data
.slider_value
= 0;
558 if ((int) call_data
> 0)
559 event_data
.action
= SCROLLBAR_PAGE_DOWN
;
561 event_data
.action
= SCROLLBAR_PAGE_UP
;
563 if (instance
->info
->pre_activate_cb
)
564 instance
->info
->pre_activate_cb (widget
, id
, (XtPointer
) &event_data
);
569 xaw_scrollbar_jump (widget
, closure
, call_data
)
575 widget_instance
*instance
= (widget_instance
*) closure
;
577 scroll_event event_data
;
578 scrollbar_values
*val
=
579 (scrollbar_values
*) instance
->info
->val
->scrollbar_data
;
582 if (!instance
|| widget
->core
.being_destroyed
)
585 id
= instance
->info
->id
;
587 percent
= * (float *) call_data
;
588 event_data
.slider_value
=
589 (int) (percent
* (float) (val
->maximum
- val
->minimum
)) + val
->minimum
;
592 event_data
.action
= SCROLLBAR_DRAG
;
594 if (instance
->info
->pre_activate_cb
)
595 instance
->info
->pre_activate_cb (widget
, id
, (XtPointer
) &event_data
);
600 xaw_create_scrollbar (instance
)
601 widget_instance
*instance
;
609 XtVaGetValues (instance
->parent
, XtNwidth
, &width
, 0);
611 XtSetArg (av
[ac
], XtNshowGrip
, 0); ac
++;
612 XtSetArg (av
[ac
], XtNresizeToPreferred
, 1); ac
++;
613 XtSetArg (av
[ac
], XtNallowResize
, True
); ac
++;
614 XtSetArg (av
[ac
], XtNskipAdjust
, True
); ac
++;
615 XtSetArg (av
[ac
], XtNwidth
, width
); ac
++;
616 XtSetArg (av
[ac
], XtNmappedWhenManaged
, True
); ac
++;
619 XtCreateWidget (instance
->info
->name
, scrollbarWidgetClass
,
620 instance
->parent
, av
, ac
);
622 /* We have to force the border width to be 0 otherwise the
623 geometry manager likes to start looping for awhile... */
624 XtVaSetValues (scrollbar
, XtNborderWidth
, 0, 0);
626 XtRemoveAllCallbacks (scrollbar
, "jumpProc");
627 XtRemoveAllCallbacks (scrollbar
, "scrollProc");
629 XtAddCallback (scrollbar
, "jumpProc", xaw_scrollbar_jump
,
630 (XtPointer
) instance
);
631 XtAddCallback (scrollbar
, "scrollProc", xaw_scrollbar_scroll
,
632 (XtPointer
) instance
);
639 xaw_create_main (instance
)
640 widget_instance
*instance
;
645 /* Create a vertical Paned to hold menubar */
647 XtSetArg (al
[ac
], XtNborderWidth
, 0); ac
++;
648 return XtCreateWidget (instance
->info
->name
, panedWidgetClass
,
649 instance
->parent
, al
, ac
);
652 widget_creation_entry
653 xaw_creation_table
[] =
655 {"scrollbar", xaw_create_scrollbar
},
656 {"main", xaw_create_main
},