1 /* The lwlib interface to Athena widgets.
2 Copyright (C) 1993 Chuck Thompson <cthomp@cs.uiuc.edu>
3 Copyright (C) 1994, 2001-2011 Free Software Foundation, Inc.
5 This file is part of the Lucid Widget Library.
7 The Lucid Widget Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 1, or (at your option)
12 The Lucid Widget Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
30 #include "../src/lisp.h"
32 #include "lwlib-Xaw.h"
34 #include <X11/StringDefs.h>
35 #include <X11/IntrinsicP.h>
36 #include <X11/CoreP.h>
37 #include <X11/Shell.h>
40 #include <X11/Xaw3d/Scrollbar.h>
41 #include <X11/Xaw3d/Paned.h>
42 #include <X11/Xaw3d/Dialog.h>
43 #include <X11/Xaw3d/Form.h>
44 #include <X11/Xaw3d/Command.h>
45 #include <X11/Xaw3d/Label.h>
46 #else /* !HAVE_XAW3D */
47 #include <X11/Xaw/Scrollbar.h>
48 #include <X11/Xaw/Paned.h>
49 #include <X11/Xaw/Dialog.h>
50 #include <X11/Xaw/Form.h>
51 #include <X11/Xaw/Command.h>
52 #include <X11/Xaw/Label.h>
53 #endif /* HAVE_XAW3D */
55 #include <X11/Xatom.h>
58 #include <X11/Xft/Xft.h>
60 struct widget_xft_data
65 XftColor xft_fg
, xft_bg
;
66 int p_width
, p_height
;
73 static void xaw_generic_callback (Widget widget
,
79 lw_xaw_widget_p (Widget widget
)
81 return (XtIsSubclass (widget
, scrollbarWidgetClass
) ||
82 XtIsSubclass (widget
, dialogWidgetClass
));
88 fill_xft_data (struct widget_xft_data
*data
, Widget widget
, XftFont
*font
)
93 data
->widget
= widget
;
94 data
->xft_font
= font
;
95 XtVaGetValues (widget
,
100 colors
[0].pixel
= data
->xft_fg
.pixel
= fg
;
101 colors
[1].pixel
= data
->xft_bg
.pixel
= bg
;
102 XQueryColors (XtDisplay (widget
),
103 DefaultColormapOfScreen (XtScreen (widget
)),
106 data
->xft_fg
.color
.alpha
= 0xFFFF;
107 data
->xft_fg
.color
.red
= colors
[0].red
;
108 data
->xft_fg
.color
.green
= colors
[0].green
;
109 data
->xft_fg
.color
.blue
= colors
[0].blue
;
110 data
->xft_bg
.color
.alpha
= 0xFFFF;
111 data
->xft_bg
.color
.red
= colors
[1].red
;
112 data
->xft_bg
.color
.green
= colors
[1].green
;
113 data
->xft_bg
.color
.blue
= colors
[1].blue
;
117 data
->p_width
= data
->p_height
= 0;
121 openFont (Widget widget
, char *name
)
124 int screen
= XScreenNumberOfScreen (XtScreen (widget
));
125 int len
= strlen (fname
), i
= len
-1;
128 /* Try to convert Gtk-syntax (Sans 9) to Xft syntax Sans-9. */
129 while (i
> 0 && isdigit (fname
[i
]))
133 fname
= xstrdup (name
);
137 fn
= XftFontOpenName (XtDisplay (widget
), screen
, fname
);
138 if (fname
!= name
) free (fname
);
144 get_text_width_and_height (Widget widget
, char *text
,
151 while (bp
&& *bp
!= '\0')
154 char *cp
= strchr (bp
, '\n');
155 XftTextExtentsUtf8 (XtDisplay (widget
), xft_font
,
157 cp
? cp
- bp
: strlen (bp
),
159 bp
= cp
? cp
+ 1 : NULL
;
160 h
+= xft_font
->height
;
161 if (w
< gi
.width
) w
= gi
.width
;
169 draw_text (struct widget_xft_data
*data
, char *lbl
, int inverse
)
171 Screen
*sc
= XtScreen (data
->widget
);
172 int screen
= XScreenNumberOfScreen (sc
);
173 int y
= data
->xft_font
->ascent
;
174 int x
= inverse
? 0 : 2;
177 data
->xft_draw
= XftDrawCreate (XtDisplay (data
->widget
),
179 DefaultVisual (XtDisplay (data
->widget
),
181 DefaultColormapOfScreen (sc
));
182 XftDrawRect (data
->xft_draw
,
183 inverse
? &data
->xft_fg
: &data
->xft_bg
,
184 0, 0, data
->p_width
, data
->p_height
);
186 if (!inverse
) y
+= 2;
187 while (bp
&& *bp
!= '\0')
189 char *cp
= strchr (bp
, '\n');
190 XftDrawStringUtf8 (data
->xft_draw
,
191 inverse
? &data
->xft_bg
: &data
->xft_fg
,
192 data
->xft_font
, x
, y
,
194 cp
? cp
- bp
: strlen (bp
));
195 bp
= cp
? cp
+ 1 : NULL
;
196 /* 1.2 gives reasonable line spacing. */
197 y
+= data
->xft_font
->height
* 1.2;
204 set_text (struct widget_xft_data
*data
, Widget toplevel
, char *lbl
, int margin
)
208 width
= get_text_width_and_height (data
->widget
, lbl
, data
->xft_font
,
210 data
->p_width
= width
+ margin
;
211 data
->p_height
= height
+ margin
;
213 data
->p
= XCreatePixmap (XtDisplay (data
->widget
),
217 DefaultDepthOfScreen (XtScreen (data
->widget
)));
218 draw_text (data
, lbl
, 0);
219 XtVaSetValues (data
->widget
, XtNbitmap
, data
->p
, NULL
);
222 static struct widget_xft_data
*
223 find_xft_data (Widget widget
)
225 widget_instance
*inst
= NULL
;
226 Widget parent
= XtParent (widget
);
227 struct widget_xft_data
*data
= NULL
;
229 while (parent
&& !inst
)
231 inst
= lw_get_widget_instance (parent
);
232 parent
= XtParent (parent
);
234 if (!inst
|| !inst
->xft_data
|| !inst
->xft_data
[0].xft_font
) return 0;
236 for (nr
= 0; data
== NULL
&& nr
< inst
->nr_xft_data
; ++nr
)
238 if (inst
->xft_data
[nr
].widget
== widget
)
239 data
= &inst
->xft_data
[nr
];
246 command_press (Widget widget
,
249 Cardinal
*num_params
)
251 struct widget_xft_data
*data
= find_xft_data (widget
);
255 /* Since this isn't used for rectangle buttons, use it to for armed. */
256 XtVaSetValues (widget
, XtNcornerRoundPercent
, 1, NULL
);
258 XtVaGetValues (widget
, XtNlabel
, &lbl
, NULL
);
259 draw_text (data
, lbl
, 1);
264 command_reset (Widget widget
,
267 Cardinal
*num_params
)
269 struct widget_xft_data
*data
= find_xft_data (widget
);
273 XtVaGetValues (widget
, XtNcornerRoundPercent
, &cr
, NULL
);
277 XtVaSetValues (widget
, XtNcornerRoundPercent
, 0, NULL
);
278 XtVaGetValues (widget
, XtNlabel
, &lbl
, NULL
);
279 draw_text (data
, lbl
, 0);
288 xaw_update_one_widget (widget_instance
*instance
,
293 if (XtIsSubclass (widget
, dialogWidgetClass
))
297 if (instance
->xft_data
&& instance
->xft_data
[0].xft_font
)
299 set_text (&instance
->xft_data
[0], instance
->parent
,
300 val
->contents
->value
, 10);
303 XtVaSetValues (widget
, XtNlabel
, val
->contents
->value
, NULL
);
305 else if (XtIsSubclass (widget
, commandWidgetClass
))
311 XtVaGetValues (widget
, XtNborderWidth
, &bw
, NULL
);
313 /* Don't let buttons end up with 0 borderwidth, that's ugly...
314 Yeah, all this should really be done through app-defaults files
315 or fallback resources, but that's a whole different can of worms
316 that I don't feel like opening right now. Making Athena widgets
317 not look like shit is just entirely too much work.
320 XtSetArg (al
[0], XtNborderWidth
, 1);
321 XtSetValues (widget
, al
, 1);
324 XtSetSensitive (widget
, val
->enabled
);
325 XtSetArg (al
[ac
], XtNlabel
, val
->value
);ac
++;
326 /* Force centered button text. Se above. */
327 XtSetArg (al
[ac
], XtNjustify
, XtJustifyCenter
);ac
++;
329 if (instance
->xft_data
&& instance
->xft_data
[0].xft_font
)
333 for (nr
= 0; nr
< instance
->nr_xft_data
; ++nr
)
334 if (instance
->xft_data
[nr
].widget
== widget
)
336 if (nr
< instance
->nr_xft_data
)
338 set_text (&instance
->xft_data
[nr
], instance
->parent
,
341 /* Must set internalHeight to twice the highlight thickness,
342 or else it gets overwritten by our pixmap. Probably a bug. */
343 XtVaGetValues (widget
, XtNhighlightThickness
, &th
, NULL
);
344 XtSetArg (al
[ac
], XtNinternalHeight
, 2*th
);ac
++;
348 XtSetValues (widget
, al
, ac
);
349 XtRemoveAllCallbacks (widget
, XtNcallback
);
350 XtAddCallback (widget
, XtNcallback
, xaw_generic_callback
, instance
);
355 xaw_update_one_value (widget_instance
*instance
,
359 /* This function is not used by the scrollbars and those are the only
360 Athena widget implemented at the moment so do nothing. */
365 xaw_destroy_instance (widget_instance
*instance
)
368 if (instance
->xft_data
)
371 for (i
= 0; i
< instance
->nr_xft_data
; ++i
)
373 if (instance
->xft_data
[i
].xft_draw
)
374 XftDrawDestroy (instance
->xft_data
[i
].xft_draw
);
375 if (instance
->xft_data
[i
].p
!= None
)
377 XtVaSetValues (instance
->xft_data
[i
].widget
, XtNbitmap
, None
,
379 XFreePixmap (XtDisplay (instance
->widget
),
380 instance
->xft_data
[i
].p
);
383 if (instance
->xft_data
[0].xft_font
)
384 XftFontClose (XtDisplay (instance
->widget
),
385 instance
->xft_data
[0].xft_font
);
386 free (instance
->xft_data
);
389 if (XtIsSubclass (instance
->widget
, dialogWidgetClass
))
390 /* Need to destroy the Shell too. */
391 XtDestroyWidget (XtParent (instance
->widget
));
393 XtDestroyWidget (instance
->widget
);
397 xaw_popup_menu (Widget widget
, XEvent
*event
)
399 /* An Athena menubar has not been implemented. */
404 xaw_pop_instance (widget_instance
*instance
, Boolean up
)
406 Widget widget
= instance
->widget
;
410 if (XtIsSubclass (widget
, dialogWidgetClass
))
412 /* For dialogs, we need to call XtPopup on the parent instead
413 of calling XtManageChild on the widget.
414 Also we need to hack the shell's WM_PROTOCOLS to get it to
415 understand what the close box is supposed to do!!
417 Display
*dpy
= XtDisplay (widget
);
418 Widget shell
= XtParent (widget
);
421 props
[i
++] = XInternAtom (dpy
, "WM_DELETE_WINDOW", False
);
422 XChangeProperty (dpy
, XtWindow (shell
),
423 XInternAtom (dpy
, "WM_PROTOCOLS", False
),
424 XA_ATOM
, 32, PropModeAppend
,
425 (unsigned char *) props
, i
);
427 /* Center the widget in its parent. Why isn't this kind of crap
428 done automatically? I thought toolkits were supposed to make
432 unsigned int x
, y
, w
, h
;
433 Widget topmost
= instance
->parent
;
436 w
= shell
->core
.width
;
437 h
= shell
->core
.height
;
438 while (topmost
->core
.parent
&& XtIsRealized (topmost
->core
.parent
))
439 topmost
= topmost
->core
.parent
;
440 if (topmost
->core
.width
< w
) x
= topmost
->core
.x
;
441 else x
= topmost
->core
.x
+ ((topmost
->core
.width
- w
) / 2);
442 if (topmost
->core
.height
< h
) y
= topmost
->core
.y
;
443 else y
= topmost
->core
.y
+ ((topmost
->core
.height
- h
) / 2);
444 /* Using XtMoveWidget caused the widget to come
445 out in the wrong place with vtwm.
446 Question of virtual vs real coords, perhaps. */
447 XtSetArg (args
[0], XtNx
, x
);
448 XtSetArg (args
[1], XtNy
, y
);
449 XtSetValues (shell
, args
, 2);
452 /* Finally, pop it up. */
453 XtPopup (shell
, XtGrabNonexclusive
);
456 XtManageChild (widget
);
460 if (XtIsSubclass (widget
, dialogWidgetClass
))
461 XtUnmanageChild (XtParent (widget
));
463 XtUnmanageChild (widget
);
470 static char overrideTrans
[] =
471 "<Message>WM_PROTOCOLS: lwlib_delete_dialog()";
472 /* Dialogs pop down on any key press */
473 static char dialogOverride
[] =
474 "<KeyPress>Escape: lwlib_delete_dialog()";
475 static void wm_delete_window (Widget w
,
478 Cardinal
*num_params
);
479 static XtActionsRec xaw_actions
[] = {
480 {"lwlib_delete_dialog", wm_delete_window
}
482 static Boolean actions_initted
= False
;
485 static XtActionsRec button_actions
[] =
487 { "my_reset", command_reset
},
488 { "my_press", command_press
},
491 "<Leave>: reset() my_reset()\n"
492 "<Btn1Down>: set() my_press()\n"
493 "<Btn1Up>: my_reset() notify() unset()\n";
497 make_dialog (char* name
,
502 Boolean text_input_slot
,
507 widget_instance
*instance
)
512 char button_name
[255];
516 XtTranslations override
;
518 XftFont
*xft_font
= 0;
519 XtTranslations button_override
;
522 if (! pop_up_p
) abort (); /* not implemented */
523 if (text_input_slot
) abort (); /* not implemented */
524 if (radio_box
) abort (); /* not implemented */
525 if (list
) abort (); /* not implemented */
527 if (! actions_initted
)
529 XtAppContext app
= XtWidgetToApplicationContext (parent
);
530 XtAppAddActions (app
, xaw_actions
,
531 sizeof (xaw_actions
) / sizeof (xaw_actions
[0]));
533 XtAppAddActions (app
, button_actions
,
534 sizeof (button_actions
) / sizeof (button_actions
[0]));
536 actions_initted
= True
;
539 override
= XtParseTranslationTable (overrideTrans
);
542 XtSetArg (av
[ac
], XtNtitle
, shell_title
); ac
++;
543 XtSetArg (av
[ac
], XtNallowShellResize
, True
); ac
++;
545 /* Don't allow any geometry request from the user. */
546 XtSetArg (av
[ac
], XtNgeometry
, 0); ac
++;
548 shell
= XtCreatePopupShell ("dialog", transientShellWidgetClass
,
550 XtOverrideTranslations (shell
, override
);
553 dialog
= XtCreateManagedWidget (name
, dialogWidgetClass
, shell
, av
, ac
);
554 override
= XtParseTranslationTable (dialogOverride
);
555 XtOverrideTranslations (dialog
, override
);
562 XtVaGetValues (dialog
,
563 XtNnumChildren
, &num
,
564 XtNchildren
, &ch
, NULL
);
565 for (i
= 0; i
< num
; ++i
)
567 if (!XtIsSubclass (ch
[i
], commandWidgetClass
)
568 && XtIsSubclass (ch
[i
], labelWidgetClass
))
574 instance
->xft_data
= 0;
575 instance
->nr_xft_data
= 0;
579 { { "faceName", "FaceName", XtRString
, sizeof(String
), 0, XtRString
,
580 (XtPointer
)"Sans-14" }};
582 XtVaGetSubresources (dialog
, &faceName
, "Dialog", "dialog",
583 rec
, 1, (String
)NULL
);
584 if (strcmp ("none", faceName
) != 0)
585 xft_font
= openFont (dialog
, faceName
);
588 instance
->nr_xft_data
= left_buttons
+ right_buttons
+ 1;
589 instance
->xft_data
= calloc (instance
->nr_xft_data
,
590 sizeof(*instance
->xft_data
));
592 fill_xft_data (&instance
->xft_data
[0], w
, xft_font
);
596 button_override
= XtParseTranslationTable (buttonTrans
);
602 for (i
= 0; i
< left_buttons
; i
++)
605 XtSetArg (av
[ac
], XtNfromHoriz
, button
); ac
++;
606 XtSetArg (av
[ac
], XtNleft
, XtChainLeft
); ac
++;
607 XtSetArg (av
[ac
], XtNright
, XtChainLeft
); ac
++;
608 XtSetArg (av
[ac
], XtNtop
, XtChainBottom
); ac
++;
609 XtSetArg (av
[ac
], XtNbottom
, XtChainBottom
); ac
++;
610 XtSetArg (av
[ac
], XtNresizable
, True
); ac
++;
612 if (DefaultDepthOfScreen (XtScreen (dialog
)) >= 16)
614 /* Turn of dithered shadow if we can. Looks bad */
615 XtSetArg (av
[ac
], "beNiceToColormap", False
); ac
++;
618 sprintf (button_name
, "button%d", ++bc
);
619 button
= XtCreateManagedWidget (button_name
, commandWidgetClass
,
624 fill_xft_data (&instance
->xft_data
[bc
], button
, xft_font
);
625 XtOverrideTranslations (button
, button_override
);
630 for (i
= 0; i
< right_buttons
; i
++)
633 XtSetArg (av
[ac
], XtNfromHoriz
, button
); ac
++;
636 /* Separator to the other buttons. */
637 XtSetArg (av
[ac
], XtNhorizDistance
, 30); ac
++;
639 XtSetArg (av
[ac
], XtNleft
, XtChainRight
); ac
++;
640 XtSetArg (av
[ac
], XtNright
, XtChainRight
); ac
++;
641 XtSetArg (av
[ac
], XtNtop
, XtChainBottom
); ac
++;
642 XtSetArg (av
[ac
], XtNbottom
, XtChainBottom
); ac
++;
643 XtSetArg (av
[ac
], XtNresizable
, True
); ac
++;
645 if (DefaultDepthOfScreen (XtScreen (dialog
)) >= 16)
647 /* Turn of dithered shadow if we can. Looks bad */
648 XtSetArg (av
[ac
], "beNiceToColormap", False
); ac
++;
651 sprintf (button_name
, "button%d", ++bc
);
652 button
= XtCreateManagedWidget (button_name
, commandWidgetClass
,
657 fill_xft_data (&instance
->xft_data
[bc
], button
, xft_font
);
658 XtOverrideTranslations (button
, button_override
);
667 xaw_create_dialog (widget_instance
*instance
)
669 char *name
= instance
->info
->type
;
670 Widget parent
= instance
->parent
;
672 Boolean pop_up_p
= instance
->pop_up_p
;
673 char *shell_name
= 0;
675 Boolean text_input_slot
= False
;
676 Boolean radio_box
= False
;
677 Boolean list
= False
;
679 int left_buttons
= 0;
680 int right_buttons
= 1;
684 icon_name
= "dbox-error";
685 shell_name
= "Error";
689 icon_name
= "dbox-info";
690 shell_name
= "Information";
695 icon_name
= "dbox-question";
696 shell_name
= "Prompt";
700 text_input_slot
= True
;
701 icon_name
= "dbox-question";
702 shell_name
= "Prompt";
706 icon_name
= "dbox-question";
707 shell_name
= "Question";
711 total_buttons
= name
[1] - '0';
713 if (name
[3] == 'T' || name
[3] == 't')
715 text_input_slot
= False
;
719 right_buttons
= name
[4] - '0';
721 left_buttons
= total_buttons
- right_buttons
;
723 widget
= make_dialog (name
, parent
, pop_up_p
,
724 shell_name
, icon_name
, text_input_slot
, radio_box
,
725 list
, left_buttons
, right_buttons
, instance
);
731 xaw_generic_callback (Widget widget
, XtPointer closure
, XtPointer call_data
)
733 widget_instance
*instance
= (widget_instance
*) closure
;
734 Widget instance_widget
;
738 lw_internal_update_other_instances (widget
, closure
, call_data
);
742 if (widget
->core
.being_destroyed
)
745 instance_widget
= instance
->widget
;
746 if (!instance_widget
)
749 id
= instance
->info
->id
;
751 /* Damn! Athena doesn't give us a way to hang our own data on the
752 buttons, so we have to go find it... I guess this assumes that
753 all instances of a button have the same call data. */
755 widget_value
*val
= instance
->info
->val
->contents
;
756 char *name
= XtName (widget
);
759 if (val
->name
&& !strcmp (val
->name
, name
))
764 user_data
= val
->call_data
;
767 if (instance
->info
->selection_cb
)
768 instance
->info
->selection_cb (widget
, id
, user_data
);
772 wm_delete_window (Widget w
,
775 Cardinal
*num_params
)
781 Widget widget
= 0, shell
;
783 if (XtIsSubclass (w
, dialogWidgetClass
))
784 shell
= XtParent (w
);
788 if (! XtIsSubclass (shell
, shellWidgetClass
))
790 XtVaGetValues (shell
, XtNnumChildren
, &nkids
, NULL
);
791 XtVaGetValues (shell
, XtNchildren
, &kids
, NULL
);
794 for (i
= 0; i
< nkids
; i
++)
797 if (XtIsSubclass (widget
, dialogWidgetClass
))
800 if (! widget
) return;
802 id
= lw_get_widget_id (widget
);
806 widget_info
*info
= lw_get_widget_info (id
);
807 if (! info
) abort ();
808 if (info
->selection_cb
)
809 info
->selection_cb (widget
, id
, (XtPointer
) -1);
812 lw_destroy_all_widgets (id
);
818 xaw_create_main (widget_instance
*instance
)
823 /* Create a vertical Paned to hold menubar */
825 XtSetArg (al
[ac
], XtNborderWidth
, 0); ac
++;
826 return XtCreateWidget (instance
->info
->name
, panedWidgetClass
,
827 instance
->parent
, al
, ac
);
830 widget_creation_entry
831 xaw_creation_table
[] =
833 {"main", xaw_create_main
},