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
;
121 if (XtIsSubclass (widget
, scrollbarWidgetClass
))
123 xaw_update_scrollbar (instance
, widget
, val
);
125 else if (XtIsSubclass (widget
, dialogWidgetClass
))
127 XtVaSetValues (widget
, XtNlabel
, val
->contents
->value
, 0);
129 else if (XtIsSubclass (widget
, commandWidgetClass
))
132 XtVaGetValues (widget
, XtNborderWidth
, &bw
, 0);
134 /* Don't let buttons end up with 0 borderwidth, that's ugly...
135 Yeah, all this should really be done through app-defaults files
136 or fallback resources, but that's a whole different can of worms
137 that I don't feel like opening right now. Making Athena widgets
138 not look like shit is just entirely too much work.
140 XtVaSetValues (widget
, XtNborderWidth
, 1, 0);
142 XtVaSetValues (widget
,
143 XtNlabel
, val
->value
,
144 XtNsensitive
, val
->enabled
,
145 /* Force centered button text. Se above. */
146 XtNjustify
, XtJustifyCenter
,
149 XtRemoveAllCallbacks (widget
, XtNcallback
);
150 XtAddCallback (widget
, XtNcallback
, xaw_generic_callback
, instance
);
155 xaw_update_one_value (instance
, widget
, val
)
156 widget_instance
*instance
;
160 /* This function is not used by the scrollbars and those are the only
161 Athena widget implemented at the moment so do nothing. */
166 xaw_destroy_instance (instance
)
167 widget_instance
*instance
;
169 if (XtIsSubclass (instance
->widget
, dialogWidgetClass
))
170 /* Need to destroy the Shell too. */
171 XtDestroyWidget (XtParent (instance
->widget
));
173 XtDestroyWidget (instance
->widget
);
177 xaw_popup_menu (widget
)
180 /* An Athena menubar has not been implemented. */
185 xaw_pop_instance (instance
, up
)
186 widget_instance
*instance
;
189 Widget widget
= instance
->widget
;
193 if (XtIsSubclass (widget
, dialogWidgetClass
))
195 /* For dialogs, we need to call XtPopup on the parent instead
196 of calling XtManageChild on the widget.
197 Also we need to hack the shell's WM_PROTOCOLS to get it to
198 understand what the close box is supposed to do!!
200 Display
*dpy
= XtDisplay (widget
);
201 Widget shell
= XtParent (widget
);
204 props
[i
++] = XInternAtom (dpy
, "WM_DELETE_WINDOW", False
);
205 XChangeProperty (dpy
, XtWindow (shell
),
206 XInternAtom (dpy
, "WM_PROTOCOLS", False
),
207 XA_ATOM
, 32, PropModeAppend
,
208 (unsigned char *) props
, i
);
210 /* Center the widget in its parent. Why isn't this kind of crap
211 done automatically? I thought toolkits were supposed to make
216 Widget topmost
= instance
->parent
;
217 w
= shell
->core
.width
;
218 h
= shell
->core
.height
;
219 while (topmost
->core
.parent
&& XtIsRealized (topmost
->core
.parent
))
220 topmost
= topmost
->core
.parent
;
221 if (topmost
->core
.width
< w
) x
= topmost
->core
.x
;
222 else x
= topmost
->core
.x
+ ((topmost
->core
.width
- w
) / 2);
223 if (topmost
->core
.height
< h
) y
= topmost
->core
.y
;
224 else y
= topmost
->core
.y
+ ((topmost
->core
.height
- h
) / 2);
225 XtMoveWidget (shell
, x
, y
);
228 /* Finally, pop it up. */
229 XtPopup (shell
, XtGrabNonexclusive
);
232 XtManageChild (widget
);
236 if (XtIsSubclass (widget
, dialogWidgetClass
))
237 XtUnmanageChild (XtParent (widget
));
239 XtUnmanageChild (widget
);
246 static char overrideTrans
[] =
247 "<Message>WM_PROTOCOLS: lwlib_delete_dialog()";
248 static void wm_delete_window();
249 static XtActionsRec xaw_actions
[] = {
250 {"lwlib_delete_dialog", wm_delete_window
}
252 static Boolean actions_initted
= False
;
255 make_dialog (name
, parent
, pop_up_p
, shell_title
, icon_name
, text_input_slot
, radio_box
, list
, left_buttons
, right_buttons
)
261 Boolean text_input_slot
;
270 char button_name
[255];
274 XtTranslations override
;
276 if (! pop_up_p
) abort (); /* not implemented */
277 if (text_input_slot
) abort (); /* not implemented */
278 if (radio_box
) abort (); /* not implemented */
279 if (list
) abort (); /* not implemented */
281 if (! actions_initted
)
283 XtAppContext app
= XtWidgetToApplicationContext (parent
);
284 XtAppAddActions (app
, xaw_actions
,
285 sizeof (xaw_actions
) / sizeof (xaw_actions
[0]));
286 actions_initted
= True
;
289 override
= XtParseTranslationTable (overrideTrans
);
292 XtSetArg (av
[ac
], XtNtitle
, shell_title
); ac
++;
293 XtSetArg (av
[ac
], XtNallowShellResize
, True
); ac
++;
294 shell
= XtCreatePopupShell ("dialog", transientShellWidgetClass
,
296 XtOverrideTranslations (shell
, override
);
299 dialog
= XtCreateManagedWidget (name
, dialogWidgetClass
, shell
, av
, ac
);
303 for (i
= 0; i
< left_buttons
; i
++)
306 XtSetArg (av
[ac
], XtNfromHoriz
, button
); ac
++;
307 XtSetArg (av
[ac
], XtNleft
, XtChainLeft
); ac
++;
308 XtSetArg (av
[ac
], XtNright
, XtChainLeft
); ac
++;
309 XtSetArg (av
[ac
], XtNtop
, XtChainBottom
); ac
++;
310 XtSetArg (av
[ac
], XtNbottom
, XtChainBottom
); ac
++;
311 XtSetArg (av
[ac
], XtNresizable
, True
); ac
++;
312 sprintf (button_name
, "button%d", ++bc
);
313 button
= XtCreateManagedWidget (button_name
, commandWidgetClass
,
318 /* Create a separator
320 I want the separator to take up the slack between the buttons on
321 the right and the buttons on the left (that is I want the buttons
322 after the separator to be packed against the right edge of the
323 window) but I can't seem to make it do it.
326 XtSetArg (av
[ac
], XtNfromHoriz
, button
); ac
++;
327 /* XtSetArg (av [ac], XtNfromVert, XtNameToWidget (dialog, "label")); ac++; */
328 XtSetArg (av
[ac
], XtNleft
, XtChainLeft
); ac
++;
329 XtSetArg (av
[ac
], XtNright
, XtChainRight
); ac
++;
330 XtSetArg (av
[ac
], XtNtop
, XtChainBottom
); ac
++;
331 XtSetArg (av
[ac
], XtNbottom
, XtChainBottom
); ac
++;
332 XtSetArg (av
[ac
], XtNlabel
, ""); ac
++;
333 XtSetArg (av
[ac
], XtNwidth
, 30); ac
++; /* #### aaack!! */
334 XtSetArg (av
[ac
], XtNborderWidth
, 0); ac
++;
335 XtSetArg (av
[ac
], XtNshapeStyle
, XmuShapeRectangle
); ac
++;
336 XtSetArg (av
[ac
], XtNresizable
, False
); ac
++;
337 XtSetArg (av
[ac
], XtNsensitive
, False
); ac
++;
338 button
= XtCreateManagedWidget ("separator",
339 /* labelWidgetClass, */
340 /* This has to be Command to fake out
341 the Dialog widget... */
345 for (i
= 0; i
< right_buttons
; i
++)
348 XtSetArg (av
[ac
], XtNfromHoriz
, button
); ac
++;
349 XtSetArg (av
[ac
], XtNleft
, XtChainRight
); ac
++;
350 XtSetArg (av
[ac
], XtNright
, XtChainRight
); ac
++;
351 XtSetArg (av
[ac
], XtNtop
, XtChainBottom
); ac
++;
352 XtSetArg (av
[ac
], XtNbottom
, XtChainBottom
); ac
++;
353 XtSetArg (av
[ac
], XtNresizable
, True
); ac
++;
354 sprintf (button_name
, "button%d", ++bc
);
355 button
= XtCreateManagedWidget (button_name
, commandWidgetClass
,
363 xaw_create_dialog (instance
)
364 widget_instance
* instance
;
366 char *name
= instance
->info
->type
;
367 Widget parent
= instance
->parent
;
369 Boolean pop_up_p
= instance
->pop_up_p
;
370 char *shell_name
= 0;
372 Boolean text_input_slot
= False
;
373 Boolean radio_box
= False
;
374 Boolean list
= False
;
376 int left_buttons
= 0;
377 int right_buttons
= 1;
381 icon_name
= "dbox-error";
382 shell_name
= "Error";
386 icon_name
= "dbox-info";
387 shell_name
= "Information";
392 icon_name
= "dbox-question";
393 shell_name
= "Prompt";
397 text_input_slot
= True
;
398 icon_name
= "dbox-question";
399 shell_name
= "Prompt";
403 icon_name
= "dbox-question";
404 shell_name
= "Question";
408 total_buttons
= name
[1] - '0';
410 if (name
[3] == 'T' || name
[3] == 't')
412 text_input_slot
= False
;
416 right_buttons
= name
[4] - '0';
418 left_buttons
= total_buttons
- right_buttons
;
420 widget
= make_dialog (name
, parent
, pop_up_p
,
421 shell_name
, icon_name
, text_input_slot
, radio_box
,
422 list
, left_buttons
, right_buttons
);
429 xaw_generic_callback (widget
, closure
, call_data
)
434 widget_instance
*instance
= (widget_instance
*) closure
;
435 Widget instance_widget
;
439 lw_internal_update_other_instances (widget
, closure
, call_data
);
443 if (widget
->core
.being_destroyed
)
446 instance_widget
= instance
->widget
;
447 if (!instance_widget
)
450 id
= instance
->info
->id
;
454 XtVaGetValues (widget
, XtNuserData
, &user_data
, 0);
456 /* Damn! Athena doesn't give us a way to hang our own data on the
457 buttons, so we have to go find it... I guess this assumes that
458 all instances of a button have the same call data. */
460 widget_value
*val
= instance
->info
->val
->contents
;
461 char *name
= XtName (widget
);
464 if (val
->name
&& !strcmp (val
->name
, name
))
469 user_data
= val
->call_data
;
473 if (instance
->info
->selection_cb
)
474 instance
->info
->selection_cb (widget
, id
, user_data
);
478 wm_delete_window (shell
, closure
, call_data
)
486 if (! XtIsSubclass (shell
, shellWidgetClass
))
488 XtVaGetValues (shell
, XtNchildren
, &kids
, 0);
492 if (! XtIsSubclass (widget
, dialogWidgetClass
))
494 id
= lw_get_widget_id (widget
);
498 widget_info
*info
= lw_get_widget_info (id
);
499 if (! info
) abort ();
500 if (info
->selection_cb
)
501 info
->selection_cb (widget
, id
, (XtPointer
) -1);
504 lw_destroy_all_widgets (id
);
511 xaw_scrollbar_scroll (widget
, closure
, call_data
)
517 widget_instance
*instance
= (widget_instance
*) closure
;
519 scroll_event event_data
;
521 if (!instance
|| widget
->core
.being_destroyed
)
524 id
= instance
->info
->id
;
525 event_data
.slider_value
= 0;
528 if ((int) call_data
> 0)
529 event_data
.action
= SCROLLBAR_PAGE_DOWN
;
531 event_data
.action
= SCROLLBAR_PAGE_UP
;
533 if (instance
->info
->pre_activate_cb
)
534 instance
->info
->pre_activate_cb (widget
, id
, (XtPointer
) &event_data
);
539 xaw_scrollbar_jump (widget
, closure
, call_data
)
545 widget_instance
*instance
= (widget_instance
*) closure
;
547 scroll_event event_data
;
548 scrollbar_values
*val
=
549 (scrollbar_values
*) instance
->info
->val
->scrollbar_data
;
552 if (!instance
|| widget
->core
.being_destroyed
)
555 id
= instance
->info
->id
;
557 percent
= * (float *) call_data
;
558 event_data
.slider_value
=
559 (int) (percent
* (float) (val
->maximum
- val
->minimum
)) + val
->minimum
;
562 event_data
.action
= SCROLLBAR_DRAG
;
564 if (instance
->info
->pre_activate_cb
)
565 instance
->info
->pre_activate_cb (widget
, id
, (XtPointer
) &event_data
);
570 xaw_create_scrollbar (instance
)
571 widget_instance
*instance
;
579 XtVaGetValues (instance
->parent
, XtNwidth
, &width
, 0);
581 XtSetArg (av
[ac
], XtNshowGrip
, 0); ac
++;
582 XtSetArg (av
[ac
], XtNresizeToPreferred
, 1); ac
++;
583 XtSetArg (av
[ac
], XtNallowResize
, True
); ac
++;
584 XtSetArg (av
[ac
], XtNskipAdjust
, True
); ac
++;
585 XtSetArg (av
[ac
], XtNwidth
, width
); ac
++;
586 XtSetArg (av
[ac
], XtNmappedWhenManaged
, True
); ac
++;
589 XtCreateWidget (instance
->info
->name
, scrollbarWidgetClass
,
590 instance
->parent
, av
, ac
);
592 /* We have to force the border width to be 0 otherwise the
593 geometry manager likes to start looping for awhile... */
594 XtVaSetValues (scrollbar
, XtNborderWidth
, 0, 0);
596 XtRemoveAllCallbacks (scrollbar
, "jumpProc");
597 XtRemoveAllCallbacks (scrollbar
, "scrollProc");
599 XtAddCallback (scrollbar
, "jumpProc", xaw_scrollbar_jump
,
600 (XtPointer
) instance
);
601 XtAddCallback (scrollbar
, "scrollProc", xaw_scrollbar_scroll
,
602 (XtPointer
) instance
);
608 widget_creation_entry
609 xaw_creation_table
[] =
611 {"scrollbar", xaw_create_scrollbar
},