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, 675 Mass Ave, Cambridge, MA 02139, USA. */
22 #include "lwlib-Xaw.h"
24 #include <X11/StringDefs.h>
25 #include <X11/IntrinsicP.h>
26 #include <X11/CoreP.h>
27 #include <X11/Shell.h>
29 #include <X11/Xaw/Scrollbar.h>
30 #include <X11/Xaw/Paned.h>
31 #include <X11/Xaw/Dialog.h>
32 #include <X11/Xaw/Form.h>
33 #include <X11/Xaw/Command.h>
34 #include <X11/Xaw/Label.h>
36 #include <X11/Xatom.h>
38 static void xaw_generic_callback (Widget
, XtPointer
, XtPointer
);
42 lw_xaw_widget_p (widget
)
45 return (XtIsSubclass (widget
, scrollbarWidgetClass
) ||
46 XtIsSubclass (widget
, dialogWidgetClass
));
50 xaw_update_scrollbar (instance
, widget
, val
)
51 widget_instance
*instance
;
56 if (val
->scrollbar_data
)
58 scrollbar_values
*data
= val
->scrollbar_data
;
59 Dimension height
, width
;
60 Dimension pos_x
, pos_y
;
61 int widget_shown
, widget_topOfThumb
;
62 float new_shown
, new_topOfThumb
;
64 XtVaGetValues (widget
,
69 XtNtopOfThumb
, &widget_topOfThumb
,
70 XtNshown
, &widget_shown
,
74 * First size and position the scrollbar widget.
75 * We need to position it to second-guess the Paned widget's notion
76 * of what should happen when the WMShell gets resized.
78 if (height
!= data
->scrollbar_height
|| pos_y
!= data
->scrollbar_pos
)
80 XtConfigureWidget (widget
, pos_x
, data
->scrollbar_pos
,
81 width
, data
->scrollbar_height
, 0);
83 XtVaSetValues (widget
,
84 XtNlength
, data
->scrollbar_height
,
90 * Now the size the scrollbar's slider.
92 new_shown
= (float) data
->slider_size
/
93 (float) (data
->maximum
- data
->minimum
);
95 new_topOfThumb
= (float) (data
->slider_position
- data
->minimum
) /
96 (float) (data
->maximum
- data
->minimum
);
103 if (new_topOfThumb
> 1.0)
104 new_topOfThumb
= 1.0;
105 if (new_topOfThumb
< 0)
108 if (new_shown
!= widget_shown
|| new_topOfThumb
!= widget_topOfThumb
)
109 XawScrollbarSetThumb (widget
, new_topOfThumb
, new_shown
);
115 xaw_update_one_widget (instance
, widget
, val
, deep_p
)
116 widget_instance
*instance
;
122 if (XtIsSubclass (widget
, scrollbarWidgetClass
))
124 xaw_update_scrollbar (instance
, widget
, val
);
127 if (XtIsSubclass (widget
, dialogWidgetClass
))
131 XtSetArg (al
[ac
], XtNlabel
, val
->contents
->value
); ac
++;
132 XtSetValues (widget
, al
, ac
);
134 else if (XtIsSubclass (widget
, commandWidgetClass
))
139 XtVaGetValues (widget
, XtNborderWidth
, &bw
, 0);
141 /* Don't let buttons end up with 0 borderwidth, that's ugly...
142 Yeah, all this should really be done through app-defaults files
143 or fallback resources, but that's a whole different can of worms
144 that I don't feel like opening right now. Making Athena widgets
145 not look like shit is just entirely too much work.
148 XtSetArg (al
[0], XtNborderWidth
, 1);
149 XtSetValues (widget
, al
, 1);
152 XtSetArg (al
[0], XtNlabel
, val
->value
);
153 XtSetArg (al
[1], XtNsensitive
, val
->enabled
);
154 /* Force centered button text. Se above. */
155 XtSetArg (al
[2], XtNjustify
, XtJustifyCenter
);
156 XtSetValues (widget
, al
, 3);
157 XtRemoveAllCallbacks (widget
, XtNcallback
);
158 XtAddCallback (widget
, XtNcallback
, xaw_generic_callback
, instance
);
163 xaw_update_one_value (instance
, widget
, val
)
164 widget_instance
*instance
;
168 /* This function is not used by the scrollbars and those are the only
169 Athena widget implemented at the moment so do nothing. */
174 xaw_destroy_instance (instance
)
175 widget_instance
*instance
;
177 if (XtIsSubclass (instance
->widget
, dialogWidgetClass
))
178 /* Need to destroy the Shell too. */
179 XtDestroyWidget (XtParent (instance
->widget
));
181 XtDestroyWidget (instance
->widget
);
185 xaw_popup_menu (widget
)
188 /* An Athena menubar has not been implemented. */
193 xaw_pop_instance (instance
, up
)
194 widget_instance
*instance
;
197 Widget widget
= instance
->widget
;
201 if (XtIsSubclass (widget
, dialogWidgetClass
))
203 /* For dialogs, we need to call XtPopup on the parent instead
204 of calling XtManageChild on the widget.
205 Also we need to hack the shell's WM_PROTOCOLS to get it to
206 understand what the close box is supposed to do!!
208 Display
*dpy
= XtDisplay (widget
);
209 Widget shell
= XtParent (widget
);
212 props
[i
++] = XInternAtom (dpy
, "WM_DELETE_WINDOW", False
);
213 XChangeProperty (dpy
, XtWindow (shell
),
214 XInternAtom (dpy
, "WM_PROTOCOLS", False
),
215 XA_ATOM
, 32, PropModeAppend
,
216 (unsigned char *) props
, i
);
218 /* Center the widget in its parent. Why isn't this kind of crap
219 done automatically? I thought toolkits were supposed to make
224 Widget topmost
= instance
->parent
;
225 w
= shell
->core
.width
;
226 h
= shell
->core
.height
;
227 while (topmost
->core
.parent
&& XtIsRealized (topmost
->core
.parent
))
228 topmost
= topmost
->core
.parent
;
229 if (topmost
->core
.width
< w
) x
= topmost
->core
.x
;
230 else x
= topmost
->core
.x
+ ((topmost
->core
.width
- w
) / 2);
231 if (topmost
->core
.height
< h
) y
= topmost
->core
.y
;
232 else y
= topmost
->core
.y
+ ((topmost
->core
.height
- h
) / 2);
233 XtMoveWidget (shell
, x
, y
);
236 /* Finally, pop it up. */
237 XtPopup (shell
, XtGrabNonexclusive
);
240 XtManageChild (widget
);
244 if (XtIsSubclass (widget
, dialogWidgetClass
))
245 XtUnmanageChild (XtParent (widget
));
247 XtUnmanageChild (widget
);
254 static char overrideTrans
[] =
255 "<Message>WM_PROTOCOLS: lwlib_delete_dialog()";
256 static void wm_delete_window();
257 static XtActionsRec xaw_actions
[] = {
258 {"lwlib_delete_dialog", wm_delete_window
}
260 static Boolean actions_initted
= False
;
263 make_dialog (name
, parent
, pop_up_p
, shell_title
, icon_name
, text_input_slot
, radio_box
, list
, left_buttons
, right_buttons
)
269 Boolean text_input_slot
;
278 char button_name
[255];
282 XtTranslations override
;
284 if (! pop_up_p
) abort (); /* not implemented */
285 if (text_input_slot
) abort (); /* not implemented */
286 if (radio_box
) abort (); /* not implemented */
287 if (list
) abort (); /* not implemented */
289 if (! actions_initted
)
291 XtAppContext app
= XtWidgetToApplicationContext (parent
);
292 XtAppAddActions (app
, xaw_actions
,
293 sizeof (xaw_actions
) / sizeof (xaw_actions
[0]));
294 actions_initted
= True
;
297 override
= XtParseTranslationTable (overrideTrans
);
300 XtSetArg (av
[ac
], XtNtitle
, shell_title
); ac
++;
301 XtSetArg (av
[ac
], XtNallowShellResize
, True
); ac
++;
302 shell
= XtCreatePopupShell ("dialog", transientShellWidgetClass
,
304 XtOverrideTranslations (shell
, override
);
307 dialog
= XtCreateManagedWidget (name
, dialogWidgetClass
, shell
, av
, ac
);
311 for (i
= 0; i
< left_buttons
; i
++)
314 XtSetArg (av
[ac
], XtNfromHoriz
, button
); ac
++;
315 XtSetArg (av
[ac
], XtNleft
, XtChainLeft
); ac
++;
316 XtSetArg (av
[ac
], XtNright
, XtChainLeft
); ac
++;
317 XtSetArg (av
[ac
], XtNtop
, XtChainBottom
); ac
++;
318 XtSetArg (av
[ac
], XtNbottom
, XtChainBottom
); ac
++;
319 XtSetArg (av
[ac
], XtNresizable
, True
); ac
++;
320 sprintf (button_name
, "button%d", ++bc
);
321 button
= XtCreateManagedWidget (button_name
, commandWidgetClass
,
326 /* Create a separator
328 I want the separator to take up the slack between the buttons on
329 the right and the buttons on the left (that is I want the buttons
330 after the separator to be packed against the right edge of the
331 window) but I can't seem to make it do it.
334 XtSetArg (av
[ac
], XtNfromHoriz
, button
); ac
++;
335 /* XtSetArg (av [ac], XtNfromVert, XtNameToWidget (dialog, "label")); ac++; */
336 XtSetArg (av
[ac
], XtNleft
, XtChainLeft
); ac
++;
337 XtSetArg (av
[ac
], XtNright
, XtChainRight
); ac
++;
338 XtSetArg (av
[ac
], XtNtop
, XtChainBottom
); ac
++;
339 XtSetArg (av
[ac
], XtNbottom
, XtChainBottom
); ac
++;
340 XtSetArg (av
[ac
], XtNlabel
, ""); ac
++;
341 XtSetArg (av
[ac
], XtNwidth
, 30); ac
++; /* #### aaack!! */
342 XtSetArg (av
[ac
], XtNborderWidth
, 0); ac
++;
343 XtSetArg (av
[ac
], XtNshapeStyle
, XmuShapeRectangle
); ac
++;
344 XtSetArg (av
[ac
], XtNresizable
, False
); ac
++;
345 XtSetArg (av
[ac
], XtNsensitive
, False
); ac
++;
346 button
= XtCreateManagedWidget ("separator",
347 /* labelWidgetClass, */
348 /* This has to be Command to fake out
349 the Dialog widget... */
353 for (i
= 0; i
< right_buttons
; i
++)
356 XtSetArg (av
[ac
], XtNfromHoriz
, button
); ac
++;
357 XtSetArg (av
[ac
], XtNleft
, XtChainRight
); ac
++;
358 XtSetArg (av
[ac
], XtNright
, XtChainRight
); ac
++;
359 XtSetArg (av
[ac
], XtNtop
, XtChainBottom
); ac
++;
360 XtSetArg (av
[ac
], XtNbottom
, XtChainBottom
); ac
++;
361 XtSetArg (av
[ac
], XtNresizable
, True
); ac
++;
362 sprintf (button_name
, "button%d", ++bc
);
363 button
= XtCreateManagedWidget (button_name
, commandWidgetClass
,
371 xaw_create_dialog (instance
)
372 widget_instance
* instance
;
374 char *name
= instance
->info
->type
;
375 Widget parent
= instance
->parent
;
377 Boolean pop_up_p
= instance
->pop_up_p
;
378 char *shell_name
= 0;
380 Boolean text_input_slot
= False
;
381 Boolean radio_box
= False
;
382 Boolean list
= False
;
384 int left_buttons
= 0;
385 int right_buttons
= 1;
389 icon_name
= "dbox-error";
390 shell_name
= "Error";
394 icon_name
= "dbox-info";
395 shell_name
= "Information";
400 icon_name
= "dbox-question";
401 shell_name
= "Prompt";
405 text_input_slot
= True
;
406 icon_name
= "dbox-question";
407 shell_name
= "Prompt";
411 icon_name
= "dbox-question";
412 shell_name
= "Question";
416 total_buttons
= name
[1] - '0';
418 if (name
[3] == 'T' || name
[3] == 't')
420 text_input_slot
= False
;
424 right_buttons
= name
[4] - '0';
426 left_buttons
= total_buttons
- right_buttons
;
428 widget
= make_dialog (name
, parent
, pop_up_p
,
429 shell_name
, icon_name
, text_input_slot
, radio_box
,
430 list
, left_buttons
, right_buttons
);
437 xaw_generic_callback (widget
, closure
, call_data
)
442 widget_instance
*instance
= (widget_instance
*) closure
;
443 Widget instance_widget
;
447 lw_internal_update_other_instances (widget
, closure
, call_data
);
451 if (widget
->core
.being_destroyed
)
454 instance_widget
= instance
->widget
;
455 if (!instance_widget
)
458 id
= instance
->info
->id
;
462 XtVaGetValues (widget
, XtNuserData
, &user_data
, 0);
464 /* Damn! Athena doesn't give us a way to hang our own data on the
465 buttons, so we have to go find it... I guess this assumes that
466 all instances of a button have the same call data. */
468 widget_value
*val
= instance
->info
->val
->contents
;
469 char *name
= XtName (widget
);
472 if (val
->name
&& !strcmp (val
->name
, name
))
477 user_data
= val
->call_data
;
481 if (instance
->info
->selection_cb
)
482 instance
->info
->selection_cb (widget
, id
, user_data
);
486 wm_delete_window (shell
, closure
, call_data
)
494 if (! XtIsSubclass (shell
, shellWidgetClass
))
496 XtVaGetValues (shell
, XtNchildren
, &kids
, 0);
500 if (! XtIsSubclass (widget
, dialogWidgetClass
))
502 id
= lw_get_widget_id (widget
);
506 widget_info
*info
= lw_get_widget_info (id
);
507 if (! info
) abort ();
508 if (info
->selection_cb
)
509 info
->selection_cb (widget
, id
, (XtPointer
) -1);
512 lw_destroy_all_widgets (id
);
519 xaw_scrollbar_scroll (widget
, closure
, call_data
)
525 widget_instance
*instance
= (widget_instance
*) closure
;
527 scroll_event event_data
;
529 if (!instance
|| widget
->core
.being_destroyed
)
532 id
= instance
->info
->id
;
533 event_data
.slider_value
= 0;
536 if ((int) call_data
> 0)
537 event_data
.action
= SCROLLBAR_PAGE_DOWN
;
539 event_data
.action
= SCROLLBAR_PAGE_UP
;
541 if (instance
->info
->pre_activate_cb
)
542 instance
->info
->pre_activate_cb (widget
, id
, (XtPointer
) &event_data
);
547 xaw_scrollbar_jump (widget
, closure
, call_data
)
553 widget_instance
*instance
= (widget_instance
*) closure
;
555 scroll_event event_data
;
556 scrollbar_values
*val
=
557 (scrollbar_values
*) instance
->info
->val
->scrollbar_data
;
560 if (!instance
|| widget
->core
.being_destroyed
)
563 id
= instance
->info
->id
;
565 percent
= * (float *) call_data
;
566 event_data
.slider_value
=
567 (int) (percent
* (float) (val
->maximum
- val
->minimum
)) + val
->minimum
;
570 event_data
.action
= SCROLLBAR_DRAG
;
572 if (instance
->info
->pre_activate_cb
)
573 instance
->info
->pre_activate_cb (widget
, id
, (XtPointer
) &event_data
);
578 xaw_create_scrollbar (instance
)
579 widget_instance
*instance
;
587 XtVaGetValues (instance
->parent
, XtNwidth
, &width
, 0);
589 XtSetArg (av
[ac
], XtNshowGrip
, 0); ac
++;
590 XtSetArg (av
[ac
], XtNresizeToPreferred
, 1); ac
++;
591 XtSetArg (av
[ac
], XtNallowResize
, True
); ac
++;
592 XtSetArg (av
[ac
], XtNskipAdjust
, True
); ac
++;
593 XtSetArg (av
[ac
], XtNwidth
, width
); ac
++;
594 XtSetArg (av
[ac
], XtNmappedWhenManaged
, True
); ac
++;
597 XtCreateWidget (instance
->info
->name
, scrollbarWidgetClass
,
598 instance
->parent
, av
, ac
);
600 /* We have to force the border width to be 0 otherwise the
601 geometry manager likes to start looping for awhile... */
602 XtVaSetValues (scrollbar
, XtNborderWidth
, 0, 0);
604 XtRemoveAllCallbacks (scrollbar
, "jumpProc");
605 XtRemoveAllCallbacks (scrollbar
, "scrollProc");
607 XtAddCallback (scrollbar
, "jumpProc", xaw_scrollbar_jump
,
608 (XtPointer
) instance
);
609 XtAddCallback (scrollbar
, "scrollProc", xaw_scrollbar_scroll
,
610 (XtPointer
) instance
);
616 widget_creation_entry
617 xaw_creation_table
[] =
619 {"scrollbar", xaw_create_scrollbar
},