1 /* The lwlib interface to Athena widgets.
3 Copyright (C) 1993 Chuck Thompson <cthomp@cs.uiuc.edu>
4 Copyright (C) 1994, 2001-2012 Free Software Foundation, Inc.
6 This file is part of the Lucid Widget Library.
8 The Lucid Widget Library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 1, or (at your option)
13 The Lucid Widget Library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
31 #include "lwlib-Xaw.h"
33 #include <X11/StringDefs.h>
34 #include <X11/IntrinsicP.h>
35 #include <X11/CoreP.h>
36 #include <X11/Shell.h>
39 #include <X11/Xaw3d/Scrollbar.h>
40 #include <X11/Xaw3d/Paned.h>
41 #include <X11/Xaw3d/Dialog.h>
42 #include <X11/Xaw3d/Form.h>
43 #include <X11/Xaw3d/Command.h>
44 #include <X11/Xaw3d/Label.h>
45 #else /* !HAVE_XAW3D */
46 #include <X11/Xaw/Scrollbar.h>
47 #include <X11/Xaw/Paned.h>
48 #include <X11/Xaw/Dialog.h>
49 #include <X11/Xaw/Form.h>
50 #include <X11/Xaw/Command.h>
51 #include <X11/Xaw/Label.h>
52 #endif /* HAVE_XAW3D */
54 #include <X11/Xatom.h>
57 #include <X11/Xft/Xft.h>
59 struct widget_xft_data
64 XftColor xft_fg
, xft_bg
;
65 int p_width
, p_height
;
72 static void xaw_generic_callback (Widget widget
,
78 lw_xaw_widget_p (Widget widget
)
80 return (XtIsSubclass (widget
, scrollbarWidgetClass
) ||
81 XtIsSubclass (widget
, dialogWidgetClass
));
87 fill_xft_data (struct widget_xft_data
*data
, Widget widget
, XftFont
*font
)
92 data
->widget
= widget
;
93 data
->xft_font
= font
;
94 XtVaGetValues (widget
,
99 colors
[0].pixel
= data
->xft_fg
.pixel
= fg
;
100 colors
[1].pixel
= data
->xft_bg
.pixel
= bg
;
101 XQueryColors (XtDisplay (widget
),
102 DefaultColormapOfScreen (XtScreen (widget
)),
105 data
->xft_fg
.color
.alpha
= 0xFFFF;
106 data
->xft_fg
.color
.red
= colors
[0].red
;
107 data
->xft_fg
.color
.green
= colors
[0].green
;
108 data
->xft_fg
.color
.blue
= colors
[0].blue
;
109 data
->xft_bg
.color
.alpha
= 0xFFFF;
110 data
->xft_bg
.color
.red
= colors
[1].red
;
111 data
->xft_bg
.color
.green
= colors
[1].green
;
112 data
->xft_bg
.color
.blue
= colors
[1].blue
;
116 data
->p_width
= data
->p_height
= 0;
120 openFont (Widget widget
, char *name
)
123 int screen
= XScreenNumberOfScreen (XtScreen (widget
));
124 int len
= strlen (fname
), i
= len
-1;
127 /* Try to convert Gtk-syntax (Sans 9) to Xft syntax Sans-9. */
128 while (i
> 0 && isdigit (fname
[i
]))
132 fname
= xstrdup (name
);
136 fn
= XftFontOpenName (XtDisplay (widget
), screen
, fname
);
137 if (fname
!= name
) xfree (fname
);
143 get_text_width_and_height (Widget widget
, char *text
,
150 while (bp
&& *bp
!= '\0')
153 char *cp
= strchr (bp
, '\n');
154 XftTextExtentsUtf8 (XtDisplay (widget
), xft_font
,
156 cp
? cp
- bp
: strlen (bp
),
158 bp
= cp
? cp
+ 1 : NULL
;
159 h
+= xft_font
->height
;
160 if (w
< gi
.width
) w
= gi
.width
;
168 draw_text (struct widget_xft_data
*data
, char *lbl
, int inverse
)
170 Screen
*sc
= XtScreen (data
->widget
);
171 int screen
= XScreenNumberOfScreen (sc
);
172 int y
= data
->xft_font
->ascent
;
173 int x
= inverse
? 0 : 2;
176 data
->xft_draw
= XftDrawCreate (XtDisplay (data
->widget
),
178 DefaultVisual (XtDisplay (data
->widget
),
180 DefaultColormapOfScreen (sc
));
181 XftDrawRect (data
->xft_draw
,
182 inverse
? &data
->xft_fg
: &data
->xft_bg
,
183 0, 0, data
->p_width
, data
->p_height
);
185 if (!inverse
) y
+= 2;
186 while (bp
&& *bp
!= '\0')
188 char *cp
= strchr (bp
, '\n');
189 XftDrawStringUtf8 (data
->xft_draw
,
190 inverse
? &data
->xft_bg
: &data
->xft_fg
,
191 data
->xft_font
, x
, y
,
193 cp
? cp
- bp
: strlen (bp
));
194 bp
= cp
? cp
+ 1 : NULL
;
195 /* 1.2 gives reasonable line spacing. */
196 y
+= data
->xft_font
->height
* 1.2;
203 set_text (struct widget_xft_data
*data
, Widget toplevel
, char *lbl
, int margin
)
207 width
= get_text_width_and_height (data
->widget
, lbl
, data
->xft_font
,
209 data
->p_width
= width
+ margin
;
210 data
->p_height
= height
+ margin
;
212 data
->p
= XCreatePixmap (XtDisplay (data
->widget
),
216 DefaultDepthOfScreen (XtScreen (data
->widget
)));
217 draw_text (data
, lbl
, 0);
218 XtVaSetValues (data
->widget
, XtNbitmap
, data
->p
, NULL
);
221 static struct widget_xft_data
*
222 find_xft_data (Widget widget
)
224 widget_instance
*inst
= NULL
;
225 Widget parent
= XtParent (widget
);
226 struct widget_xft_data
*data
= NULL
;
228 while (parent
&& !inst
)
230 inst
= lw_get_widget_instance (parent
);
231 parent
= XtParent (parent
);
233 if (!inst
|| !inst
->xft_data
|| !inst
->xft_data
[0].xft_font
) return 0;
235 for (nr
= 0; data
== NULL
&& nr
< inst
->nr_xft_data
; ++nr
)
237 if (inst
->xft_data
[nr
].widget
== widget
)
238 data
= &inst
->xft_data
[nr
];
245 command_press (Widget widget
,
248 Cardinal
*num_params
)
250 struct widget_xft_data
*data
= find_xft_data (widget
);
254 /* Since this isn't used for rectangle buttons, use it to for armed. */
255 XtVaSetValues (widget
, XtNcornerRoundPercent
, 1, NULL
);
257 XtVaGetValues (widget
, XtNlabel
, &lbl
, NULL
);
258 draw_text (data
, lbl
, 1);
263 command_reset (Widget widget
,
266 Cardinal
*num_params
)
268 struct widget_xft_data
*data
= find_xft_data (widget
);
272 XtVaGetValues (widget
, XtNcornerRoundPercent
, &cr
, NULL
);
276 XtVaSetValues (widget
, XtNcornerRoundPercent
, 0, NULL
);
277 XtVaGetValues (widget
, XtNlabel
, &lbl
, NULL
);
278 draw_text (data
, lbl
, 0);
287 xaw_update_one_widget (widget_instance
*instance
,
292 if (XtIsSubclass (widget
, dialogWidgetClass
))
296 if (instance
->xft_data
&& instance
->xft_data
[0].xft_font
)
298 set_text (&instance
->xft_data
[0], instance
->parent
,
299 val
->contents
->value
, 10);
302 XtVaSetValues (widget
, XtNlabel
, val
->contents
->value
, NULL
);
304 else if (XtIsSubclass (widget
, commandWidgetClass
))
310 XtVaGetValues (widget
, XtNborderWidth
, &bw
, NULL
);
312 /* Don't let buttons end up with 0 borderwidth, that's ugly...
313 Yeah, all this should really be done through app-defaults files
314 or fallback resources, but that's a whole different can of worms
315 that I don't feel like opening right now. Making Athena widgets
316 not look like shit is just entirely too much work.
319 XtSetArg (al
[0], XtNborderWidth
, 1);
320 XtSetValues (widget
, al
, 1);
323 XtSetSensitive (widget
, val
->enabled
);
324 XtSetArg (al
[ac
], XtNlabel
, val
->value
);ac
++;
325 /* Force centered button text. Se above. */
326 XtSetArg (al
[ac
], XtNjustify
, XtJustifyCenter
);ac
++;
328 if (instance
->xft_data
&& instance
->xft_data
[0].xft_font
)
332 for (nr
= 0; nr
< instance
->nr_xft_data
; ++nr
)
333 if (instance
->xft_data
[nr
].widget
== widget
)
335 if (nr
< instance
->nr_xft_data
)
337 set_text (&instance
->xft_data
[nr
], instance
->parent
,
340 /* Must set internalHeight to twice the highlight thickness,
341 or else it gets overwritten by our pixmap. Probably a bug. */
342 XtVaGetValues (widget
, XtNhighlightThickness
, &th
, NULL
);
343 XtSetArg (al
[ac
], XtNinternalHeight
, 2*th
);ac
++;
347 XtSetValues (widget
, al
, ac
);
348 XtRemoveAllCallbacks (widget
, XtNcallback
);
349 XtAddCallback (widget
, XtNcallback
, xaw_generic_callback
, instance
);
354 xaw_update_one_value (widget_instance
*instance
,
358 /* This function is not used by the scrollbars and those are the only
359 Athena widget implemented at the moment so do nothing. */
364 xaw_destroy_instance (widget_instance
*instance
)
367 if (instance
->xft_data
)
370 for (i
= 0; i
< instance
->nr_xft_data
; ++i
)
372 if (instance
->xft_data
[i
].xft_draw
)
373 XftDrawDestroy (instance
->xft_data
[i
].xft_draw
);
374 if (instance
->xft_data
[i
].p
!= None
)
376 XtVaSetValues (instance
->xft_data
[i
].widget
, XtNbitmap
, None
,
378 XFreePixmap (XtDisplay (instance
->widget
),
379 instance
->xft_data
[i
].p
);
382 if (instance
->xft_data
[0].xft_font
)
383 XftFontClose (XtDisplay (instance
->widget
),
384 instance
->xft_data
[0].xft_font
);
385 xfree (instance
->xft_data
);
388 if (XtIsSubclass (instance
->widget
, dialogWidgetClass
))
389 /* Need to destroy the Shell too. */
390 XtDestroyWidget (XtParent (instance
->widget
));
392 XtDestroyWidget (instance
->widget
);
396 xaw_popup_menu (Widget widget
, XEvent
*event
)
398 /* An Athena menubar has not been implemented. */
403 xaw_pop_instance (widget_instance
*instance
, Boolean up
)
405 Widget widget
= instance
->widget
;
409 if (XtIsSubclass (widget
, dialogWidgetClass
))
411 /* For dialogs, we need to call XtPopup on the parent instead
412 of calling XtManageChild on the widget.
413 Also we need to hack the shell's WM_PROTOCOLS to get it to
414 understand what the close box is supposed to do!!
416 Display
*dpy
= XtDisplay (widget
);
417 Widget shell
= XtParent (widget
);
420 props
[i
++] = XInternAtom (dpy
, "WM_DELETE_WINDOW", False
);
421 XChangeProperty (dpy
, XtWindow (shell
),
422 XInternAtom (dpy
, "WM_PROTOCOLS", False
),
423 XA_ATOM
, 32, PropModeAppend
,
424 (unsigned char *) props
, i
);
426 /* Center the widget in its parent. Why isn't this kind of crap
427 done automatically? I thought toolkits were supposed to make
431 unsigned int x
, y
, w
, h
;
432 Widget topmost
= instance
->parent
;
435 w
= shell
->core
.width
;
436 h
= shell
->core
.height
;
437 while (topmost
->core
.parent
&& XtIsRealized (topmost
->core
.parent
))
438 topmost
= topmost
->core
.parent
;
439 if (topmost
->core
.width
< w
) x
= topmost
->core
.x
;
440 else x
= topmost
->core
.x
+ ((topmost
->core
.width
- w
) / 2);
441 if (topmost
->core
.height
< h
) y
= topmost
->core
.y
;
442 else y
= topmost
->core
.y
+ ((topmost
->core
.height
- h
) / 2);
443 /* Using XtMoveWidget caused the widget to come
444 out in the wrong place with vtwm.
445 Question of virtual vs real coords, perhaps. */
446 XtSetArg (args
[0], XtNx
, x
);
447 XtSetArg (args
[1], XtNy
, y
);
448 XtSetValues (shell
, args
, 2);
451 /* Finally, pop it up. */
452 XtPopup (shell
, XtGrabNonexclusive
);
455 XtManageChild (widget
);
459 if (XtIsSubclass (widget
, dialogWidgetClass
))
460 XtUnmanageChild (XtParent (widget
));
462 XtUnmanageChild (widget
);
469 static char overrideTrans
[] =
470 "<Message>WM_PROTOCOLS: lwlib_delete_dialog()";
471 /* Dialogs pop down on any key press */
472 static char dialogOverride
[] =
473 "<KeyPress>Escape: lwlib_delete_dialog()";
474 static void wm_delete_window (Widget w
,
477 Cardinal
*num_params
);
478 static XtActionsRec xaw_actions
[] = {
479 {"lwlib_delete_dialog", wm_delete_window
}
481 static Boolean actions_initted
= False
;
484 static XtActionsRec button_actions
[] =
486 { "my_reset", command_reset
},
487 { "my_press", command_press
},
490 "<Leave>: reset() my_reset()\n"
491 "<Btn1Down>: set() my_press()\n"
492 "<Btn1Up>: my_reset() notify() unset()\n";
496 make_dialog (char* name
,
501 Boolean text_input_slot
,
506 widget_instance
*instance
)
511 char button_name
[255];
515 XtTranslations override
;
517 XftFont
*xft_font
= 0;
518 XtTranslations button_override
;
521 if (! pop_up_p
) abort (); /* not implemented */
522 if (text_input_slot
) abort (); /* not implemented */
523 if (radio_box
) abort (); /* not implemented */
524 if (list
) abort (); /* not implemented */
526 if (! actions_initted
)
528 XtAppContext app
= XtWidgetToApplicationContext (parent
);
529 XtAppAddActions (app
, xaw_actions
,
530 sizeof (xaw_actions
) / sizeof (xaw_actions
[0]));
532 XtAppAddActions (app
, button_actions
,
533 sizeof (button_actions
) / sizeof (button_actions
[0]));
535 actions_initted
= True
;
538 override
= XtParseTranslationTable (overrideTrans
);
541 XtSetArg (av
[ac
], XtNtitle
, shell_title
); ac
++;
542 XtSetArg (av
[ac
], XtNallowShellResize
, True
); ac
++;
544 /* Don't allow any geometry request from the user. */
545 XtSetArg (av
[ac
], XtNgeometry
, 0); ac
++;
547 shell
= XtCreatePopupShell ("dialog", transientShellWidgetClass
,
549 XtOverrideTranslations (shell
, override
);
552 dialog
= XtCreateManagedWidget (name
, dialogWidgetClass
, shell
, av
, ac
);
553 override
= XtParseTranslationTable (dialogOverride
);
554 XtOverrideTranslations (dialog
, override
);
561 XtVaGetValues (dialog
,
562 XtNnumChildren
, &num
,
563 XtNchildren
, &ch
, NULL
);
564 for (i
= 0; i
< num
; ++i
)
566 if (!XtIsSubclass (ch
[i
], commandWidgetClass
)
567 && XtIsSubclass (ch
[i
], labelWidgetClass
))
573 instance
->xft_data
= 0;
574 instance
->nr_xft_data
= 0;
578 { { "font", "Font", XtRString
, sizeof(String
), 0, XtRString
,
579 (XtPointer
)"Sans-10" }};
580 char *fontName
= NULL
;
581 XtVaGetSubresources (dialog
, &fontName
, "Dialog", "dialog",
582 rec
, 1, (String
)NULL
);
585 XFontStruct
*xfn
= XLoadQueryFont (XtDisplay (dialog
), fontName
);
587 xft_font
= openFont (dialog
, fontName
);
589 XFreeFont (XtDisplay (dialog
), xfn
);
594 instance
->nr_xft_data
= left_buttons
+ right_buttons
+ 1;
595 instance
->xft_data
= calloc (instance
->nr_xft_data
,
596 sizeof(*instance
->xft_data
));
598 fill_xft_data (&instance
->xft_data
[0], w
, xft_font
);
602 button_override
= XtParseTranslationTable (buttonTrans
);
608 for (i
= 0; i
< left_buttons
; i
++)
611 XtSetArg (av
[ac
], XtNfromHoriz
, button
); ac
++;
612 XtSetArg (av
[ac
], XtNleft
, XtChainLeft
); ac
++;
613 XtSetArg (av
[ac
], XtNright
, XtChainLeft
); ac
++;
614 XtSetArg (av
[ac
], XtNtop
, XtChainBottom
); ac
++;
615 XtSetArg (av
[ac
], XtNbottom
, XtChainBottom
); ac
++;
616 XtSetArg (av
[ac
], XtNresizable
, True
); ac
++;
618 if (DefaultDepthOfScreen (XtScreen (dialog
)) >= 16)
620 /* Turn of dithered shadow if we can. Looks bad */
621 XtSetArg (av
[ac
], "beNiceToColormap", False
); ac
++;
624 sprintf (button_name
, "button%d", ++bc
);
625 button
= XtCreateManagedWidget (button_name
, commandWidgetClass
,
630 fill_xft_data (&instance
->xft_data
[bc
], button
, xft_font
);
631 XtOverrideTranslations (button
, button_override
);
636 for (i
= 0; i
< right_buttons
; i
++)
639 XtSetArg (av
[ac
], XtNfromHoriz
, button
); ac
++;
642 /* Separator to the other buttons. */
643 XtSetArg (av
[ac
], XtNhorizDistance
, 30); ac
++;
645 XtSetArg (av
[ac
], XtNleft
, XtChainRight
); ac
++;
646 XtSetArg (av
[ac
], XtNright
, XtChainRight
); ac
++;
647 XtSetArg (av
[ac
], XtNtop
, XtChainBottom
); ac
++;
648 XtSetArg (av
[ac
], XtNbottom
, XtChainBottom
); ac
++;
649 XtSetArg (av
[ac
], XtNresizable
, True
); ac
++;
651 if (DefaultDepthOfScreen (XtScreen (dialog
)) >= 16)
653 /* Turn of dithered shadow if we can. Looks bad */
654 XtSetArg (av
[ac
], "beNiceToColormap", False
); ac
++;
657 sprintf (button_name
, "button%d", ++bc
);
658 button
= XtCreateManagedWidget (button_name
, commandWidgetClass
,
663 fill_xft_data (&instance
->xft_data
[bc
], button
, xft_font
);
664 XtOverrideTranslations (button
, button_override
);
673 xaw_create_dialog (widget_instance
*instance
)
675 char *name
= instance
->info
->type
;
676 Widget parent
= instance
->parent
;
678 Boolean pop_up_p
= instance
->pop_up_p
;
679 char *shell_name
= 0;
681 Boolean text_input_slot
= False
;
682 Boolean radio_box
= False
;
683 Boolean list
= False
;
685 int left_buttons
= 0;
686 int right_buttons
= 1;
690 icon_name
= "dbox-error";
691 shell_name
= "Error";
695 icon_name
= "dbox-info";
696 shell_name
= "Information";
701 icon_name
= "dbox-question";
702 shell_name
= "Prompt";
706 text_input_slot
= True
;
707 icon_name
= "dbox-question";
708 shell_name
= "Prompt";
712 icon_name
= "dbox-question";
713 shell_name
= "Question";
717 total_buttons
= name
[1] - '0';
719 if (name
[3] == 'T' || name
[3] == 't')
721 text_input_slot
= False
;
725 right_buttons
= name
[4] - '0';
727 left_buttons
= total_buttons
- right_buttons
;
729 widget
= make_dialog (name
, parent
, pop_up_p
,
730 shell_name
, icon_name
, text_input_slot
, radio_box
,
731 list
, left_buttons
, right_buttons
, instance
);
737 xaw_generic_callback (Widget widget
, XtPointer closure
, XtPointer call_data
)
739 widget_instance
*instance
= (widget_instance
*) closure
;
740 Widget instance_widget
;
744 lw_internal_update_other_instances (widget
, closure
, call_data
);
748 if (widget
->core
.being_destroyed
)
751 instance_widget
= instance
->widget
;
752 if (!instance_widget
)
755 id
= instance
->info
->id
;
757 /* Damn! Athena doesn't give us a way to hang our own data on the
758 buttons, so we have to go find it... I guess this assumes that
759 all instances of a button have the same call data. */
761 widget_value
*val
= instance
->info
->val
->contents
;
762 char *name
= XtName (widget
);
765 if (val
->name
&& !strcmp (val
->name
, name
))
770 user_data
= val
->call_data
;
773 if (instance
->info
->selection_cb
)
774 instance
->info
->selection_cb (widget
, id
, user_data
);
778 wm_delete_window (Widget w
,
781 Cardinal
*num_params
)
787 Widget widget
= 0, shell
;
789 if (XtIsSubclass (w
, dialogWidgetClass
))
790 shell
= XtParent (w
);
794 if (! XtIsSubclass (shell
, shellWidgetClass
))
796 XtVaGetValues (shell
, XtNnumChildren
, &nkids
, NULL
);
797 XtVaGetValues (shell
, XtNchildren
, &kids
, NULL
);
800 for (i
= 0; i
< nkids
; i
++)
803 if (XtIsSubclass (widget
, dialogWidgetClass
))
806 if (! widget
) return;
808 id
= lw_get_widget_id (widget
);
812 widget_info
*info
= lw_get_widget_info (id
);
813 if (! info
) abort ();
814 if (info
->selection_cb
)
815 info
->selection_cb (widget
, id
, (XtPointer
) -1);
818 lw_destroy_all_widgets (id
);
824 xaw_create_main (widget_instance
*instance
)
829 /* Create a vertical Paned to hold menubar */
831 XtSetArg (al
[ac
], XtNborderWidth
, 0); ac
++;
832 return XtCreateWidget (instance
->info
->name
, panedWidgetClass
,
833 instance
->parent
, al
, ac
);
836 widget_creation_entry
837 xaw_creation_table
[] =
839 {"main", xaw_create_main
},