1 /* The lwlib interface to Athena widgets.
3 Copyright (C) 1993 Chuck Thompson <cthomp@cs.uiuc.edu>
4 Copyright (C) 1994, 2001-2011 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. */
33 #include "lwlib-Xaw.h"
35 #include <X11/StringDefs.h>
36 #include <X11/IntrinsicP.h>
37 #include <X11/CoreP.h>
38 #include <X11/Shell.h>
41 #include <X11/Xaw3d/Scrollbar.h>
42 #include <X11/Xaw3d/Paned.h>
43 #include <X11/Xaw3d/Dialog.h>
44 #include <X11/Xaw3d/Form.h>
45 #include <X11/Xaw3d/Command.h>
46 #include <X11/Xaw3d/Label.h>
47 #else /* !HAVE_XAW3D */
48 #include <X11/Xaw/Scrollbar.h>
49 #include <X11/Xaw/Paned.h>
50 #include <X11/Xaw/Dialog.h>
51 #include <X11/Xaw/Form.h>
52 #include <X11/Xaw/Command.h>
53 #include <X11/Xaw/Label.h>
54 #endif /* HAVE_XAW3D */
56 #include <X11/Xatom.h>
59 #include <X11/Xft/Xft.h>
61 struct widget_xft_data
66 XftColor xft_fg
, xft_bg
;
67 int p_width
, p_height
;
74 static void xaw_generic_callback (Widget widget
,
80 lw_xaw_widget_p (Widget widget
)
82 return (XtIsSubclass (widget
, scrollbarWidgetClass
) ||
83 XtIsSubclass (widget
, dialogWidgetClass
));
89 fill_xft_data (struct widget_xft_data
*data
, Widget widget
, XftFont
*font
)
94 data
->widget
= widget
;
95 data
->xft_font
= font
;
96 XtVaGetValues (widget
,
101 colors
[0].pixel
= data
->xft_fg
.pixel
= fg
;
102 colors
[1].pixel
= data
->xft_bg
.pixel
= bg
;
103 XQueryColors (XtDisplay (widget
),
104 DefaultColormapOfScreen (XtScreen (widget
)),
107 data
->xft_fg
.color
.alpha
= 0xFFFF;
108 data
->xft_fg
.color
.red
= colors
[0].red
;
109 data
->xft_fg
.color
.green
= colors
[0].green
;
110 data
->xft_fg
.color
.blue
= colors
[0].blue
;
111 data
->xft_bg
.color
.alpha
= 0xFFFF;
112 data
->xft_bg
.color
.red
= colors
[1].red
;
113 data
->xft_bg
.color
.green
= colors
[1].green
;
114 data
->xft_bg
.color
.blue
= colors
[1].blue
;
118 data
->p_width
= data
->p_height
= 0;
122 openFont (Widget widget
, char *name
)
125 int screen
= XScreenNumberOfScreen (XtScreen (widget
));
126 int len
= strlen (fname
), i
= len
-1;
129 /* Try to convert Gtk-syntax (Sans 9) to Xft syntax Sans-9. */
130 while (i
> 0 && isdigit (fname
[i
]))
134 fname
= xstrdup (name
);
138 fn
= XftFontOpenName (XtDisplay (widget
), screen
, fname
);
139 if (fname
!= name
) xfree (fname
);
145 get_text_width_and_height (Widget widget
, char *text
,
152 while (bp
&& *bp
!= '\0')
155 char *cp
= strchr (bp
, '\n');
156 XftTextExtentsUtf8 (XtDisplay (widget
), xft_font
,
158 cp
? cp
- bp
: strlen (bp
),
160 bp
= cp
? cp
+ 1 : NULL
;
161 h
+= xft_font
->height
;
162 if (w
< gi
.width
) w
= gi
.width
;
170 draw_text (struct widget_xft_data
*data
, char *lbl
, int inverse
)
172 Screen
*sc
= XtScreen (data
->widget
);
173 int screen
= XScreenNumberOfScreen (sc
);
174 int y
= data
->xft_font
->ascent
;
175 int x
= inverse
? 0 : 2;
178 data
->xft_draw
= XftDrawCreate (XtDisplay (data
->widget
),
180 DefaultVisual (XtDisplay (data
->widget
),
182 DefaultColormapOfScreen (sc
));
183 XftDrawRect (data
->xft_draw
,
184 inverse
? &data
->xft_fg
: &data
->xft_bg
,
185 0, 0, data
->p_width
, data
->p_height
);
187 if (!inverse
) y
+= 2;
188 while (bp
&& *bp
!= '\0')
190 char *cp
= strchr (bp
, '\n');
191 XftDrawStringUtf8 (data
->xft_draw
,
192 inverse
? &data
->xft_bg
: &data
->xft_fg
,
193 data
->xft_font
, x
, y
,
195 cp
? cp
- bp
: strlen (bp
));
196 bp
= cp
? cp
+ 1 : NULL
;
197 /* 1.2 gives reasonable line spacing. */
198 y
+= data
->xft_font
->height
* 1.2;
205 set_text (struct widget_xft_data
*data
, Widget toplevel
, char *lbl
, int margin
)
209 width
= get_text_width_and_height (data
->widget
, lbl
, data
->xft_font
,
211 data
->p_width
= width
+ margin
;
212 data
->p_height
= height
+ margin
;
214 data
->p
= XCreatePixmap (XtDisplay (data
->widget
),
218 DefaultDepthOfScreen (XtScreen (data
->widget
)));
219 draw_text (data
, lbl
, 0);
220 XtVaSetValues (data
->widget
, XtNbitmap
, data
->p
, NULL
);
223 static struct widget_xft_data
*
224 find_xft_data (Widget widget
)
226 widget_instance
*inst
= NULL
;
227 Widget parent
= XtParent (widget
);
228 struct widget_xft_data
*data
= NULL
;
230 while (parent
&& !inst
)
232 inst
= lw_get_widget_instance (parent
);
233 parent
= XtParent (parent
);
235 if (!inst
|| !inst
->xft_data
|| !inst
->xft_data
[0].xft_font
) return 0;
237 for (nr
= 0; data
== NULL
&& nr
< inst
->nr_xft_data
; ++nr
)
239 if (inst
->xft_data
[nr
].widget
== widget
)
240 data
= &inst
->xft_data
[nr
];
247 command_press (Widget widget
,
250 Cardinal
*num_params
)
252 struct widget_xft_data
*data
= find_xft_data (widget
);
256 /* Since this isn't used for rectangle buttons, use it to for armed. */
257 XtVaSetValues (widget
, XtNcornerRoundPercent
, 1, NULL
);
259 XtVaGetValues (widget
, XtNlabel
, &lbl
, NULL
);
260 draw_text (data
, lbl
, 1);
265 command_reset (Widget widget
,
268 Cardinal
*num_params
)
270 struct widget_xft_data
*data
= find_xft_data (widget
);
274 XtVaGetValues (widget
, XtNcornerRoundPercent
, &cr
, NULL
);
278 XtVaSetValues (widget
, XtNcornerRoundPercent
, 0, NULL
);
279 XtVaGetValues (widget
, XtNlabel
, &lbl
, NULL
);
280 draw_text (data
, lbl
, 0);
289 xaw_update_one_widget (widget_instance
*instance
,
294 if (XtIsSubclass (widget
, dialogWidgetClass
))
298 if (instance
->xft_data
&& instance
->xft_data
[0].xft_font
)
300 set_text (&instance
->xft_data
[0], instance
->parent
,
301 val
->contents
->value
, 10);
304 XtVaSetValues (widget
, XtNlabel
, val
->contents
->value
, NULL
);
306 else if (XtIsSubclass (widget
, commandWidgetClass
))
312 XtVaGetValues (widget
, XtNborderWidth
, &bw
, NULL
);
314 /* Don't let buttons end up with 0 borderwidth, that's ugly...
315 Yeah, all this should really be done through app-defaults files
316 or fallback resources, but that's a whole different can of worms
317 that I don't feel like opening right now. Making Athena widgets
318 not look like shit is just entirely too much work.
321 XtSetArg (al
[0], XtNborderWidth
, 1);
322 XtSetValues (widget
, al
, 1);
325 XtSetSensitive (widget
, val
->enabled
);
326 XtSetArg (al
[ac
], XtNlabel
, val
->value
);ac
++;
327 /* Force centered button text. Se above. */
328 XtSetArg (al
[ac
], XtNjustify
, XtJustifyCenter
);ac
++;
330 if (instance
->xft_data
&& instance
->xft_data
[0].xft_font
)
334 for (nr
= 0; nr
< instance
->nr_xft_data
; ++nr
)
335 if (instance
->xft_data
[nr
].widget
== widget
)
337 if (nr
< instance
->nr_xft_data
)
339 set_text (&instance
->xft_data
[nr
], instance
->parent
,
342 /* Must set internalHeight to twice the highlight thickness,
343 or else it gets overwritten by our pixmap. Probably a bug. */
344 XtVaGetValues (widget
, XtNhighlightThickness
, &th
, NULL
);
345 XtSetArg (al
[ac
], XtNinternalHeight
, 2*th
);ac
++;
349 XtSetValues (widget
, al
, ac
);
350 XtRemoveAllCallbacks (widget
, XtNcallback
);
351 XtAddCallback (widget
, XtNcallback
, xaw_generic_callback
, instance
);
356 xaw_update_one_value (widget_instance
*instance
,
360 /* This function is not used by the scrollbars and those are the only
361 Athena widget implemented at the moment so do nothing. */
366 xaw_destroy_instance (widget_instance
*instance
)
369 if (instance
->xft_data
)
372 for (i
= 0; i
< instance
->nr_xft_data
; ++i
)
374 if (instance
->xft_data
[i
].xft_draw
)
375 XftDrawDestroy (instance
->xft_data
[i
].xft_draw
);
376 if (instance
->xft_data
[i
].p
!= None
)
378 XtVaSetValues (instance
->xft_data
[i
].widget
, XtNbitmap
, None
,
380 XFreePixmap (XtDisplay (instance
->widget
),
381 instance
->xft_data
[i
].p
);
384 if (instance
->xft_data
[0].xft_font
)
385 XftFontClose (XtDisplay (instance
->widget
),
386 instance
->xft_data
[0].xft_font
);
387 xfree (instance
->xft_data
);
390 if (XtIsSubclass (instance
->widget
, dialogWidgetClass
))
391 /* Need to destroy the Shell too. */
392 XtDestroyWidget (XtParent (instance
->widget
));
394 XtDestroyWidget (instance
->widget
);
398 xaw_popup_menu (Widget widget
, XEvent
*event
)
400 /* An Athena menubar has not been implemented. */
405 xaw_pop_instance (widget_instance
*instance
, Boolean up
)
407 Widget widget
= instance
->widget
;
411 if (XtIsSubclass (widget
, dialogWidgetClass
))
413 /* For dialogs, we need to call XtPopup on the parent instead
414 of calling XtManageChild on the widget.
415 Also we need to hack the shell's WM_PROTOCOLS to get it to
416 understand what the close box is supposed to do!!
418 Display
*dpy
= XtDisplay (widget
);
419 Widget shell
= XtParent (widget
);
422 props
[i
++] = XInternAtom (dpy
, "WM_DELETE_WINDOW", False
);
423 XChangeProperty (dpy
, XtWindow (shell
),
424 XInternAtom (dpy
, "WM_PROTOCOLS", False
),
425 XA_ATOM
, 32, PropModeAppend
,
426 (unsigned char *) props
, i
);
428 /* Center the widget in its parent. Why isn't this kind of crap
429 done automatically? I thought toolkits were supposed to make
433 unsigned int x
, y
, w
, h
;
434 Widget topmost
= instance
->parent
;
437 w
= shell
->core
.width
;
438 h
= shell
->core
.height
;
439 while (topmost
->core
.parent
&& XtIsRealized (topmost
->core
.parent
))
440 topmost
= topmost
->core
.parent
;
441 if (topmost
->core
.width
< w
) x
= topmost
->core
.x
;
442 else x
= topmost
->core
.x
+ ((topmost
->core
.width
- w
) / 2);
443 if (topmost
->core
.height
< h
) y
= topmost
->core
.y
;
444 else y
= topmost
->core
.y
+ ((topmost
->core
.height
- h
) / 2);
445 /* Using XtMoveWidget caused the widget to come
446 out in the wrong place with vtwm.
447 Question of virtual vs real coords, perhaps. */
448 XtSetArg (args
[0], XtNx
, x
);
449 XtSetArg (args
[1], XtNy
, y
);
450 XtSetValues (shell
, args
, 2);
453 /* Finally, pop it up. */
454 XtPopup (shell
, XtGrabNonexclusive
);
457 XtManageChild (widget
);
461 if (XtIsSubclass (widget
, dialogWidgetClass
))
462 XtUnmanageChild (XtParent (widget
));
464 XtUnmanageChild (widget
);
471 static char overrideTrans
[] =
472 "<Message>WM_PROTOCOLS: lwlib_delete_dialog()";
473 /* Dialogs pop down on any key press */
474 static char dialogOverride
[] =
475 "<KeyPress>Escape: lwlib_delete_dialog()";
476 static void wm_delete_window (Widget w
,
479 Cardinal
*num_params
);
480 static XtActionsRec xaw_actions
[] = {
481 {"lwlib_delete_dialog", wm_delete_window
}
483 static Boolean actions_initted
= False
;
486 static XtActionsRec button_actions
[] =
488 { "my_reset", command_reset
},
489 { "my_press", command_press
},
492 "<Leave>: reset() my_reset()\n"
493 "<Btn1Down>: set() my_press()\n"
494 "<Btn1Up>: my_reset() notify() unset()\n";
498 make_dialog (char* name
,
503 Boolean text_input_slot
,
508 widget_instance
*instance
)
513 char button_name
[255];
517 XtTranslations override
;
519 XftFont
*xft_font
= 0;
520 XtTranslations button_override
;
523 if (! pop_up_p
) abort (); /* not implemented */
524 if (text_input_slot
) abort (); /* not implemented */
525 if (radio_box
) abort (); /* not implemented */
526 if (list
) abort (); /* not implemented */
528 if (! actions_initted
)
530 XtAppContext app
= XtWidgetToApplicationContext (parent
);
531 XtAppAddActions (app
, xaw_actions
,
532 sizeof (xaw_actions
) / sizeof (xaw_actions
[0]));
534 XtAppAddActions (app
, button_actions
,
535 sizeof (button_actions
) / sizeof (button_actions
[0]));
537 actions_initted
= True
;
540 override
= XtParseTranslationTable (overrideTrans
);
543 XtSetArg (av
[ac
], XtNtitle
, shell_title
); ac
++;
544 XtSetArg (av
[ac
], XtNallowShellResize
, True
); ac
++;
546 /* Don't allow any geometry request from the user. */
547 XtSetArg (av
[ac
], XtNgeometry
, 0); ac
++;
549 shell
= XtCreatePopupShell ("dialog", transientShellWidgetClass
,
551 XtOverrideTranslations (shell
, override
);
554 dialog
= XtCreateManagedWidget (name
, dialogWidgetClass
, shell
, av
, ac
);
555 override
= XtParseTranslationTable (dialogOverride
);
556 XtOverrideTranslations (dialog
, override
);
563 XtVaGetValues (dialog
,
564 XtNnumChildren
, &num
,
565 XtNchildren
, &ch
, NULL
);
566 for (i
= 0; i
< num
; ++i
)
568 if (!XtIsSubclass (ch
[i
], commandWidgetClass
)
569 && XtIsSubclass (ch
[i
], labelWidgetClass
))
575 instance
->xft_data
= 0;
576 instance
->nr_xft_data
= 0;
580 { { "font", "Font", XtRString
, sizeof(String
), 0, XtRString
,
581 (XtPointer
)"Sans-10" }};
582 char *fontName
= NULL
;
583 XtVaGetSubresources (dialog
, &fontName
, "Dialog", "dialog",
584 rec
, 1, (String
)NULL
);
587 XFontStruct
*xfn
= XLoadQueryFont (XtDisplay (dialog
), fontName
);
589 xft_font
= openFont (dialog
, fontName
);
591 XFreeFont (XtDisplay (dialog
), xfn
);
596 instance
->nr_xft_data
= left_buttons
+ right_buttons
+ 1;
597 instance
->xft_data
= calloc (instance
->nr_xft_data
,
598 sizeof(*instance
->xft_data
));
600 fill_xft_data (&instance
->xft_data
[0], w
, xft_font
);
604 button_override
= XtParseTranslationTable (buttonTrans
);
610 for (i
= 0; i
< left_buttons
; i
++)
613 XtSetArg (av
[ac
], XtNfromHoriz
, button
); ac
++;
614 XtSetArg (av
[ac
], XtNleft
, XtChainLeft
); ac
++;
615 XtSetArg (av
[ac
], XtNright
, XtChainLeft
); ac
++;
616 XtSetArg (av
[ac
], XtNtop
, XtChainBottom
); ac
++;
617 XtSetArg (av
[ac
], XtNbottom
, XtChainBottom
); ac
++;
618 XtSetArg (av
[ac
], XtNresizable
, True
); ac
++;
620 if (DefaultDepthOfScreen (XtScreen (dialog
)) >= 16)
622 /* Turn of dithered shadow if we can. Looks bad */
623 XtSetArg (av
[ac
], "beNiceToColormap", False
); ac
++;
626 sprintf (button_name
, "button%d", ++bc
);
627 button
= XtCreateManagedWidget (button_name
, commandWidgetClass
,
632 fill_xft_data (&instance
->xft_data
[bc
], button
, xft_font
);
633 XtOverrideTranslations (button
, button_override
);
638 for (i
= 0; i
< right_buttons
; i
++)
641 XtSetArg (av
[ac
], XtNfromHoriz
, button
); ac
++;
644 /* Separator to the other buttons. */
645 XtSetArg (av
[ac
], XtNhorizDistance
, 30); ac
++;
647 XtSetArg (av
[ac
], XtNleft
, XtChainRight
); ac
++;
648 XtSetArg (av
[ac
], XtNright
, XtChainRight
); ac
++;
649 XtSetArg (av
[ac
], XtNtop
, XtChainBottom
); ac
++;
650 XtSetArg (av
[ac
], XtNbottom
, XtChainBottom
); ac
++;
651 XtSetArg (av
[ac
], XtNresizable
, True
); ac
++;
653 if (DefaultDepthOfScreen (XtScreen (dialog
)) >= 16)
655 /* Turn of dithered shadow if we can. Looks bad */
656 XtSetArg (av
[ac
], "beNiceToColormap", False
); ac
++;
659 sprintf (button_name
, "button%d", ++bc
);
660 button
= XtCreateManagedWidget (button_name
, commandWidgetClass
,
665 fill_xft_data (&instance
->xft_data
[bc
], button
, xft_font
);
666 XtOverrideTranslations (button
, button_override
);
675 xaw_create_dialog (widget_instance
*instance
)
677 char *name
= instance
->info
->type
;
678 Widget parent
= instance
->parent
;
680 Boolean pop_up_p
= instance
->pop_up_p
;
681 char *shell_name
= 0;
683 Boolean text_input_slot
= False
;
684 Boolean radio_box
= False
;
685 Boolean list
= False
;
687 int left_buttons
= 0;
688 int right_buttons
= 1;
692 icon_name
= "dbox-error";
693 shell_name
= "Error";
697 icon_name
= "dbox-info";
698 shell_name
= "Information";
703 icon_name
= "dbox-question";
704 shell_name
= "Prompt";
708 text_input_slot
= True
;
709 icon_name
= "dbox-question";
710 shell_name
= "Prompt";
714 icon_name
= "dbox-question";
715 shell_name
= "Question";
719 total_buttons
= name
[1] - '0';
721 if (name
[3] == 'T' || name
[3] == 't')
723 text_input_slot
= False
;
727 right_buttons
= name
[4] - '0';
729 left_buttons
= total_buttons
- right_buttons
;
731 widget
= make_dialog (name
, parent
, pop_up_p
,
732 shell_name
, icon_name
, text_input_slot
, radio_box
,
733 list
, left_buttons
, right_buttons
, instance
);
739 xaw_generic_callback (Widget widget
, XtPointer closure
, XtPointer call_data
)
741 widget_instance
*instance
= (widget_instance
*) closure
;
742 Widget instance_widget
;
746 lw_internal_update_other_instances (widget
, closure
, call_data
);
750 if (widget
->core
.being_destroyed
)
753 instance_widget
= instance
->widget
;
754 if (!instance_widget
)
757 id
= instance
->info
->id
;
759 /* Damn! Athena doesn't give us a way to hang our own data on the
760 buttons, so we have to go find it... I guess this assumes that
761 all instances of a button have the same call data. */
763 widget_value
*val
= instance
->info
->val
->contents
;
764 char *name
= XtName (widget
);
767 if (val
->name
&& !strcmp (val
->name
, name
))
772 user_data
= val
->call_data
;
775 if (instance
->info
->selection_cb
)
776 instance
->info
->selection_cb (widget
, id
, user_data
);
780 wm_delete_window (Widget w
,
783 Cardinal
*num_params
)
789 Widget widget
= 0, shell
;
791 if (XtIsSubclass (w
, dialogWidgetClass
))
792 shell
= XtParent (w
);
796 if (! XtIsSubclass (shell
, shellWidgetClass
))
798 XtVaGetValues (shell
, XtNnumChildren
, &nkids
, NULL
);
799 XtVaGetValues (shell
, XtNchildren
, &kids
, NULL
);
802 for (i
= 0; i
< nkids
; i
++)
805 if (XtIsSubclass (widget
, dialogWidgetClass
))
808 if (! widget
) return;
810 id
= lw_get_widget_id (widget
);
814 widget_info
*info
= lw_get_widget_info (id
);
815 if (! info
) abort ();
816 if (info
->selection_cb
)
817 info
->selection_cb (widget
, id
, (XtPointer
) -1);
820 lw_destroy_all_widgets (id
);
826 xaw_create_main (widget_instance
*instance
)
831 /* Create a vertical Paned to hold menubar */
833 XtSetArg (al
[ac
], XtNborderWidth
, 0); ac
++;
834 return XtCreateWidget (instance
->info
->name
, panedWidgetClass
,
835 instance
->parent
, al
, ac
);
838 widget_creation_entry
839 xaw_creation_table
[] =
841 {"main", xaw_create_main
},