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
, NULL
);
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 XtSetSensitive (widget
, val
->enabled
);
158 XtSetArg (al
[0], XtNlabel
, val
->value
);
159 /* Force centered button text. Se above. */
160 XtSetArg (al
[1], XtNjustify
, XtJustifyCenter
);
161 XtSetValues (widget
, al
, 2);
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
, event
)
194 /* An Athena menubar has not been implemented. */
199 xaw_pop_instance (instance
, up
)
200 widget_instance
*instance
;
203 Widget widget
= instance
->widget
;
207 if (XtIsSubclass (widget
, dialogWidgetClass
))
209 /* For dialogs, we need to call XtPopup on the parent instead
210 of calling XtManageChild on the widget.
211 Also we need to hack the shell's WM_PROTOCOLS to get it to
212 understand what the close box is supposed to do!!
214 Display
*dpy
= XtDisplay (widget
);
215 Widget shell
= XtParent (widget
);
218 props
[i
++] = XInternAtom (dpy
, "WM_DELETE_WINDOW", False
);
219 XChangeProperty (dpy
, XtWindow (shell
),
220 XInternAtom (dpy
, "WM_PROTOCOLS", False
),
221 XA_ATOM
, 32, PropModeAppend
,
222 (unsigned char *) props
, i
);
224 /* Center the widget in its parent. Why isn't this kind of crap
225 done automatically? I thought toolkits were supposed to make
229 unsigned int x
, y
, w
, h
;
230 Widget topmost
= instance
->parent
;
233 w
= shell
->core
.width
;
234 h
= shell
->core
.height
;
235 while (topmost
->core
.parent
&& XtIsRealized (topmost
->core
.parent
))
236 topmost
= topmost
->core
.parent
;
237 if (topmost
->core
.width
< w
) x
= topmost
->core
.x
;
238 else x
= topmost
->core
.x
+ ((topmost
->core
.width
- w
) / 2);
239 if (topmost
->core
.height
< h
) y
= topmost
->core
.y
;
240 else y
= topmost
->core
.y
+ ((topmost
->core
.height
- h
) / 2);
241 /* Using XtMoveWidget caused the widget to come
242 out in the wrong place with vtwm.
243 Question of virtual vs real coords, perhaps. */
244 XtSetArg (args
[0], XtNx
, x
);
245 XtSetArg (args
[1], XtNy
, y
);
246 XtSetValues (shell
, args
, 2);
249 /* Finally, pop it up. */
250 XtPopup (shell
, XtGrabNonexclusive
);
253 XtManageChild (widget
);
257 if (XtIsSubclass (widget
, dialogWidgetClass
))
258 XtUnmanageChild (XtParent (widget
));
260 XtUnmanageChild (widget
);
267 static char overrideTrans
[] =
268 "<Message>WM_PROTOCOLS: lwlib_delete_dialog()";
269 static void wm_delete_window();
270 static XtActionsRec xaw_actions
[] = {
271 {"lwlib_delete_dialog", wm_delete_window
}
273 static Boolean actions_initted
= False
;
276 make_dialog (name
, parent
, pop_up_p
, shell_title
, icon_name
, text_input_slot
, radio_box
, list
, left_buttons
, right_buttons
)
282 Boolean text_input_slot
;
291 char button_name
[255];
295 XtTranslations override
;
297 if (! pop_up_p
) abort (); /* not implemented */
298 if (text_input_slot
) abort (); /* not implemented */
299 if (radio_box
) abort (); /* not implemented */
300 if (list
) abort (); /* not implemented */
302 if (! actions_initted
)
304 XtAppContext app
= XtWidgetToApplicationContext (parent
);
305 XtAppAddActions (app
, xaw_actions
,
306 sizeof (xaw_actions
) / sizeof (xaw_actions
[0]));
307 actions_initted
= True
;
310 override
= XtParseTranslationTable (overrideTrans
);
313 XtSetArg (av
[ac
], XtNtitle
, shell_title
); ac
++;
314 XtSetArg (av
[ac
], XtNallowShellResize
, True
); ac
++;
316 /* Don't allow any geometry request from the user. */
317 XtSetArg (av
[ac
], XtNgeometry
, 0); ac
++;
319 shell
= XtCreatePopupShell ("dialog", transientShellWidgetClass
,
321 XtOverrideTranslations (shell
, override
);
324 dialog
= XtCreateManagedWidget (name
, dialogWidgetClass
, shell
, av
, ac
);
328 for (i
= 0; i
< left_buttons
; i
++)
331 XtSetArg (av
[ac
], XtNfromHoriz
, button
); ac
++;
332 XtSetArg (av
[ac
], XtNleft
, XtChainLeft
); ac
++;
333 XtSetArg (av
[ac
], XtNright
, XtChainLeft
); ac
++;
334 XtSetArg (av
[ac
], XtNtop
, XtChainBottom
); ac
++;
335 XtSetArg (av
[ac
], XtNbottom
, XtChainBottom
); ac
++;
336 XtSetArg (av
[ac
], XtNresizable
, True
); ac
++;
337 sprintf (button_name
, "button%d", ++bc
);
338 button
= XtCreateManagedWidget (button_name
, commandWidgetClass
,
343 /* Create a separator
345 I want the separator to take up the slack between the buttons on
346 the right and the buttons on the left (that is I want the buttons
347 after the separator to be packed against the right edge of the
348 window) but I can't seem to make it do it.
351 XtSetArg (av
[ac
], XtNfromHoriz
, button
); ac
++;
352 /* XtSetArg (av [ac], XtNfromVert, XtNameToWidget (dialog, "label")); ac++; */
353 XtSetArg (av
[ac
], XtNleft
, XtChainLeft
); ac
++;
354 XtSetArg (av
[ac
], XtNright
, XtChainRight
); ac
++;
355 XtSetArg (av
[ac
], XtNtop
, XtChainBottom
); ac
++;
356 XtSetArg (av
[ac
], XtNbottom
, XtChainBottom
); ac
++;
357 XtSetArg (av
[ac
], XtNlabel
, ""); ac
++;
358 XtSetArg (av
[ac
], XtNwidth
, 30); ac
++; /* #### aaack!! */
359 XtSetArg (av
[ac
], XtNborderWidth
, 0); ac
++;
360 XtSetArg (av
[ac
], XtNshapeStyle
, XmuShapeRectangle
); ac
++;
361 XtSetArg (av
[ac
], XtNresizable
, False
); ac
++;
362 XtSetArg (av
[ac
], XtNsensitive
, False
); ac
++;
363 button
= XtCreateManagedWidget ("separator",
364 /* labelWidgetClass, */
365 /* This has to be Command to fake out
366 the Dialog widget... */
370 for (i
= 0; i
< right_buttons
; i
++)
373 XtSetArg (av
[ac
], XtNfromHoriz
, button
); ac
++;
374 XtSetArg (av
[ac
], XtNleft
, XtChainRight
); ac
++;
375 XtSetArg (av
[ac
], XtNright
, XtChainRight
); ac
++;
376 XtSetArg (av
[ac
], XtNtop
, XtChainBottom
); ac
++;
377 XtSetArg (av
[ac
], XtNbottom
, XtChainBottom
); ac
++;
378 XtSetArg (av
[ac
], XtNresizable
, True
); ac
++;
379 sprintf (button_name
, "button%d", ++bc
);
380 button
= XtCreateManagedWidget (button_name
, commandWidgetClass
,
388 xaw_create_dialog (instance
)
389 widget_instance
* instance
;
391 char *name
= instance
->info
->type
;
392 Widget parent
= instance
->parent
;
394 Boolean pop_up_p
= instance
->pop_up_p
;
395 char *shell_name
= 0;
397 Boolean text_input_slot
= False
;
398 Boolean radio_box
= False
;
399 Boolean list
= False
;
401 int left_buttons
= 0;
402 int right_buttons
= 1;
406 icon_name
= "dbox-error";
407 shell_name
= "Error";
411 icon_name
= "dbox-info";
412 shell_name
= "Information";
417 icon_name
= "dbox-question";
418 shell_name
= "Prompt";
422 text_input_slot
= True
;
423 icon_name
= "dbox-question";
424 shell_name
= "Prompt";
428 icon_name
= "dbox-question";
429 shell_name
= "Question";
433 total_buttons
= name
[1] - '0';
435 if (name
[3] == 'T' || name
[3] == 't')
437 text_input_slot
= False
;
441 right_buttons
= name
[4] - '0';
443 left_buttons
= total_buttons
- right_buttons
;
445 widget
= make_dialog (name
, parent
, pop_up_p
,
446 shell_name
, icon_name
, text_input_slot
, radio_box
,
447 list
, left_buttons
, right_buttons
);
454 xaw_generic_callback (widget
, closure
, call_data
)
459 widget_instance
*instance
= (widget_instance
*) closure
;
460 Widget instance_widget
;
464 lw_internal_update_other_instances (widget
, closure
, call_data
);
468 if (widget
->core
.being_destroyed
)
471 instance_widget
= instance
->widget
;
472 if (!instance_widget
)
475 id
= instance
->info
->id
;
479 XtVaGetValues (widget
, XtNuserData
, &user_data
, NULL
);
481 /* Damn! Athena doesn't give us a way to hang our own data on the
482 buttons, so we have to go find it... I guess this assumes that
483 all instances of a button have the same call data. */
485 widget_value
*val
= instance
->info
->val
->contents
;
486 char *name
= XtName (widget
);
489 if (val
->name
&& !strcmp (val
->name
, name
))
494 user_data
= val
->call_data
;
498 if (instance
->info
->selection_cb
)
499 instance
->info
->selection_cb (widget
, id
, user_data
);
503 wm_delete_window (shell
, closure
, call_data
)
513 if (! XtIsSubclass (shell
, shellWidgetClass
))
515 XtVaGetValues (shell
, XtNnumChildren
, &nkids
, NULL
);
516 XtVaGetValues (shell
, XtNchildren
, &kids
, NULL
);
519 for (i
= 0; i
< nkids
; i
++)
522 if (XtIsSubclass (widget
, dialogWidgetClass
))
525 id
= lw_get_widget_id (widget
);
529 widget_info
*info
= lw_get_widget_info (id
);
530 if (! info
) abort ();
531 if (info
->selection_cb
)
532 info
->selection_cb (widget
, id
, (XtPointer
) -1);
535 lw_destroy_all_widgets (id
);
542 xaw_scrollbar_scroll (widget
, closure
, call_data
)
548 widget_instance
*instance
= (widget_instance
*) closure
;
550 scroll_event event_data
;
552 if (!instance
|| widget
->core
.being_destroyed
)
555 id
= instance
->info
->id
;
556 event_data
.slider_value
= 0;
559 if ((int) call_data
> 0)
560 event_data
.action
= SCROLLBAR_PAGE_DOWN
;
562 event_data
.action
= SCROLLBAR_PAGE_UP
;
564 if (instance
->info
->pre_activate_cb
)
565 instance
->info
->pre_activate_cb (widget
, id
, (XtPointer
) &event_data
);
570 xaw_scrollbar_jump (widget
, closure
, call_data
)
576 widget_instance
*instance
= (widget_instance
*) closure
;
578 scroll_event event_data
;
579 scrollbar_values
*val
=
580 (scrollbar_values
*) instance
->info
->val
->scrollbar_data
;
583 if (!instance
|| widget
->core
.being_destroyed
)
586 id
= instance
->info
->id
;
588 percent
= * (float *) call_data
;
589 event_data
.slider_value
=
590 (int) (percent
* (float) (val
->maximum
- val
->minimum
)) + val
->minimum
;
593 event_data
.action
= SCROLLBAR_DRAG
;
595 if (instance
->info
->pre_activate_cb
)
596 instance
->info
->pre_activate_cb (widget
, id
, (XtPointer
) &event_data
);
601 xaw_create_scrollbar (instance
)
602 widget_instance
*instance
;
610 XtVaGetValues (instance
->parent
, XtNwidth
, &width
, NULL
);
612 XtSetArg (av
[ac
], XtNshowGrip
, 0); ac
++;
613 XtSetArg (av
[ac
], XtNresizeToPreferred
, 1); ac
++;
614 XtSetArg (av
[ac
], XtNallowResize
, True
); ac
++;
615 XtSetArg (av
[ac
], XtNskipAdjust
, True
); ac
++;
616 XtSetArg (av
[ac
], XtNwidth
, width
); ac
++;
617 XtSetArg (av
[ac
], XtNmappedWhenManaged
, True
); ac
++;
620 XtCreateWidget (instance
->info
->name
, scrollbarWidgetClass
,
621 instance
->parent
, av
, ac
);
623 /* We have to force the border width to be 0 otherwise the
624 geometry manager likes to start looping for awhile... */
625 XtVaSetValues (scrollbar
, XtNborderWidth
, 0, NULL
);
627 XtRemoveAllCallbacks (scrollbar
, "jumpProc");
628 XtRemoveAllCallbacks (scrollbar
, "scrollProc");
630 XtAddCallback (scrollbar
, "jumpProc", xaw_scrollbar_jump
,
631 (XtPointer
) instance
);
632 XtAddCallback (scrollbar
, "scrollProc", xaw_scrollbar_scroll
,
633 (XtPointer
) instance
);
640 xaw_create_main (instance
)
641 widget_instance
*instance
;
646 /* Create a vertical Paned to hold menubar */
648 XtSetArg (al
[ac
], XtNborderWidth
, 0); ac
++;
649 return XtCreateWidget (instance
->info
->name
, panedWidgetClass
,
650 instance
->parent
, al
, ac
);
653 widget_creation_entry
654 xaw_creation_table
[] =
656 {"scrollbar", xaw_create_scrollbar
},
657 {"main", xaw_create_main
},