1 /* The lwlib interface to Athena widgets.
3 Copyright (C) 1993 Chuck Thompson <cthomp@cs.uiuc.edu>
4 Copyright (C) 1994, 2001-2016 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. If not, see <http://www.gnu.org/licenses/>. */
29 #include "lwlib-Xaw.h"
31 #include <X11/StringDefs.h>
32 #include <X11/IntrinsicP.h>
33 #include <X11/CoreP.h>
34 #include <X11/Shell.h>
37 #include <X11/Xaw3d/Scrollbar.h>
38 #include <X11/Xaw3d/Paned.h>
39 #include <X11/Xaw3d/Dialog.h>
40 #include <X11/Xaw3d/Form.h>
41 #include <X11/Xaw3d/Command.h>
42 #include <X11/Xaw3d/Label.h>
43 #else /* !HAVE_XAW3D */
44 #include <X11/Xaw/Scrollbar.h>
45 #include <X11/Xaw/Paned.h>
46 #include <X11/Xaw/Dialog.h>
47 #include <X11/Xaw/Form.h>
48 #include <X11/Xaw/Command.h>
49 #include <X11/Xaw/Label.h>
50 #endif /* HAVE_XAW3D */
52 #include <X11/Xatom.h>
55 #include <X11/Xft/Xft.h>
57 struct widget_xft_data
62 XftColor xft_fg
, xft_bg
;
63 int p_width
, p_height
;
70 static void xaw_generic_callback (Widget widget
,
76 lw_xaw_widget_p (Widget widget
)
78 return (XtIsSubclass (widget
, scrollbarWidgetClass
) ||
79 XtIsSubclass (widget
, dialogWidgetClass
));
85 fill_xft_data (struct widget_xft_data
*data
, Widget widget
, XftFont
*font
)
90 data
->widget
= widget
;
91 data
->xft_font
= font
;
92 XtVaGetValues (widget
,
97 colors
[0].pixel
= data
->xft_fg
.pixel
= fg
;
98 colors
[1].pixel
= data
->xft_bg
.pixel
= bg
;
99 XQueryColors (XtDisplay (widget
),
100 DefaultColormapOfScreen (XtScreen (widget
)),
103 data
->xft_fg
.color
.alpha
= 0xFFFF;
104 data
->xft_fg
.color
.red
= colors
[0].red
;
105 data
->xft_fg
.color
.green
= colors
[0].green
;
106 data
->xft_fg
.color
.blue
= colors
[0].blue
;
107 data
->xft_bg
.color
.alpha
= 0xFFFF;
108 data
->xft_bg
.color
.red
= colors
[1].red
;
109 data
->xft_bg
.color
.green
= colors
[1].green
;
110 data
->xft_bg
.color
.blue
= colors
[1].blue
;
114 data
->p_width
= data
->p_height
= 0;
118 openFont (Widget widget
, char *name
)
121 int screen
= XScreenNumberOfScreen (XtScreen (widget
));
122 int len
= strlen (fname
), i
= len
-1;
125 /* Try to convert Gtk-syntax (Sans 9) to Xft syntax Sans-9. */
126 while (i
> 0 && '0' <= fname
[i
] && fname
[i
] <= '9')
130 fname
= xstrdup (name
);
134 fn
= XftFontOpenName (XtDisplay (widget
), screen
, fname
);
135 if (fname
!= name
) xfree (fname
);
141 get_text_width_and_height (Widget widget
, char *text
,
148 while (bp
&& *bp
!= '\0')
151 char *cp
= strchr (bp
, '\n');
152 XftTextExtentsUtf8 (XtDisplay (widget
), xft_font
,
154 cp
? cp
- bp
: strlen (bp
),
156 bp
= cp
? cp
+ 1 : NULL
;
157 h
+= xft_font
->height
;
158 if (w
< gi
.width
) w
= gi
.width
;
166 draw_text (struct widget_xft_data
*data
, char *lbl
, int inverse
)
168 Screen
*sc
= XtScreen (data
->widget
);
169 int screen
= XScreenNumberOfScreen (sc
);
170 int y
= data
->xft_font
->ascent
;
171 int x
= inverse
? 0 : 2;
174 data
->xft_draw
= XftDrawCreate (XtDisplay (data
->widget
),
176 DefaultVisual (XtDisplay (data
->widget
),
178 DefaultColormapOfScreen (sc
));
179 XftDrawRect (data
->xft_draw
,
180 inverse
? &data
->xft_fg
: &data
->xft_bg
,
181 0, 0, data
->p_width
, data
->p_height
);
183 if (!inverse
) y
+= 2;
184 while (bp
&& *bp
!= '\0')
186 char *cp
= strchr (bp
, '\n');
187 XftDrawStringUtf8 (data
->xft_draw
,
188 inverse
? &data
->xft_bg
: &data
->xft_fg
,
189 data
->xft_font
, x
, y
,
191 cp
? cp
- bp
: strlen (bp
));
192 bp
= cp
? cp
+ 1 : NULL
;
193 /* 1.2 gives reasonable line spacing. */
194 y
+= data
->xft_font
->height
* 1.2;
201 set_text (struct widget_xft_data
*data
, Widget toplevel
, char *lbl
, int margin
)
205 width
= get_text_width_and_height (data
->widget
, lbl
, data
->xft_font
,
207 data
->p_width
= width
+ margin
;
208 data
->p_height
= height
+ margin
;
210 data
->p
= XCreatePixmap (XtDisplay (data
->widget
),
214 DefaultDepthOfScreen (XtScreen (data
->widget
)));
215 draw_text (data
, lbl
, 0);
216 XtVaSetValues (data
->widget
, XtNbitmap
, data
->p
, NULL
);
219 static struct widget_xft_data
*
220 find_xft_data (Widget widget
)
222 widget_instance
*inst
= NULL
;
223 Widget parent
= XtParent (widget
);
224 struct widget_xft_data
*data
= NULL
;
226 while (parent
&& !inst
)
228 inst
= lw_get_widget_instance (parent
);
229 parent
= XtParent (parent
);
231 if (!inst
|| !inst
->xft_data
|| !inst
->xft_data
[0].xft_font
) return 0;
233 for (nr
= 0; data
== NULL
&& nr
< inst
->nr_xft_data
; ++nr
)
235 if (inst
->xft_data
[nr
].widget
== widget
)
236 data
= &inst
->xft_data
[nr
];
243 command_press (Widget widget
,
246 Cardinal
*num_params
)
248 struct widget_xft_data
*data
= find_xft_data (widget
);
252 /* Since this isn't used for rectangle buttons, use it to for armed. */
253 XtVaSetValues (widget
, XtNcornerRoundPercent
, 1, NULL
);
255 XtVaGetValues (widget
, XtNlabel
, &lbl
, NULL
);
256 draw_text (data
, lbl
, 1);
261 command_reset (Widget widget
,
264 Cardinal
*num_params
)
266 struct widget_xft_data
*data
= find_xft_data (widget
);
270 XtVaGetValues (widget
, XtNcornerRoundPercent
, &cr
, NULL
);
274 XtVaSetValues (widget
, XtNcornerRoundPercent
, 0, NULL
);
275 XtVaGetValues (widget
, XtNlabel
, &lbl
, NULL
);
276 draw_text (data
, lbl
, 0);
285 xaw_update_one_widget (widget_instance
*instance
,
290 if (XtIsSubclass (widget
, dialogWidgetClass
))
294 if (instance
->xft_data
&& instance
->xft_data
[0].xft_font
)
296 set_text (&instance
->xft_data
[0], instance
->parent
,
297 val
->contents
->value
, 10);
300 XtVaSetValues (widget
, XtNlabel
, val
->contents
->value
, NULL
);
302 else if (XtIsSubclass (widget
, commandWidgetClass
))
308 XtVaGetValues (widget
, XtNborderWidth
, &bw
, NULL
);
310 /* Don't let buttons end up with 0 borderwidth, that's ugly...
311 Yeah, all this should really be done through app-defaults files
312 or fallback resources, but that's a whole different can of worms
313 that I don't feel like opening right now. Making Athena widgets
314 not look like shit is just entirely too much work.
317 XtSetArg (al
[0], XtNborderWidth
, 1);
318 XtSetValues (widget
, al
, 1);
321 XtSetSensitive (widget
, val
->enabled
);
322 XtSetArg (al
[ac
], XtNlabel
, val
->value
);ac
++;
323 /* Force centered button text. Se above. */
324 XtSetArg (al
[ac
], XtNjustify
, XtJustifyCenter
);ac
++;
326 if (instance
->xft_data
&& instance
->xft_data
[0].xft_font
)
330 for (nr
= 0; nr
< instance
->nr_xft_data
; ++nr
)
331 if (instance
->xft_data
[nr
].widget
== widget
)
333 if (nr
< instance
->nr_xft_data
)
335 set_text (&instance
->xft_data
[nr
], instance
->parent
,
338 /* Must set internalHeight to twice the highlight thickness,
339 or else it gets overwritten by our pixmap. Probably a bug. */
340 XtVaGetValues (widget
, XtNhighlightThickness
, &th
, NULL
);
341 XtSetArg (al
[ac
], XtNinternalHeight
, 2*th
);ac
++;
345 XtSetValues (widget
, al
, ac
);
346 XtRemoveAllCallbacks (widget
, XtNcallback
);
347 XtAddCallback (widget
, XtNcallback
, xaw_generic_callback
, instance
);
352 xaw_update_one_value (widget_instance
*instance
,
356 /* This function is not used by the scrollbars and those are the only
357 Athena widget implemented at the moment so do nothing. */
362 xaw_destroy_instance (widget_instance
*instance
)
365 if (instance
->xft_data
)
368 for (i
= 0; i
< instance
->nr_xft_data
; ++i
)
370 if (instance
->xft_data
[i
].xft_draw
)
371 XftDrawDestroy (instance
->xft_data
[i
].xft_draw
);
372 if (instance
->xft_data
[i
].p
!= None
)
374 XtVaSetValues (instance
->xft_data
[i
].widget
, XtNbitmap
, None
,
376 XFreePixmap (XtDisplay (instance
->widget
),
377 instance
->xft_data
[i
].p
);
380 if (instance
->xft_data
[0].xft_font
)
381 XftFontClose (XtDisplay (instance
->widget
),
382 instance
->xft_data
[0].xft_font
);
383 xfree (instance
->xft_data
);
386 if (XtIsSubclass (instance
->widget
, dialogWidgetClass
))
387 /* Need to destroy the Shell too. */
388 XtDestroyWidget (XtParent (instance
->widget
));
390 XtDestroyWidget (instance
->widget
);
394 xaw_popup_menu (Widget widget
, XEvent
*event
)
396 /* An Athena menubar has not been implemented. */
401 xaw_pop_instance (widget_instance
*instance
, Boolean up
)
403 Widget widget
= instance
->widget
;
407 if (XtIsSubclass (widget
, dialogWidgetClass
))
409 /* For dialogs, we need to call XtPopup on the parent instead
410 of calling XtManageChild on the widget.
411 Also we need to hack the shell's WM_PROTOCOLS to get it to
412 understand what the close box is supposed to do!!
414 Display
*dpy
= XtDisplay (widget
);
415 Widget shell
= XtParent (widget
);
418 props
[i
++] = XInternAtom (dpy
, "WM_DELETE_WINDOW", False
);
419 XChangeProperty (dpy
, XtWindow (shell
),
420 XInternAtom (dpy
, "WM_PROTOCOLS", False
),
421 XA_ATOM
, 32, PropModeAppend
,
422 (unsigned char *) props
, i
);
424 /* Center the widget in its parent. Why isn't this kind of crap
425 done automatically? I thought toolkits were supposed to make
429 unsigned int x
, y
, w
, h
;
430 Widget topmost
= instance
->parent
;
433 w
= shell
->core
.width
;
434 h
= shell
->core
.height
;
435 while (topmost
->core
.parent
&& XtIsRealized (topmost
->core
.parent
))
436 topmost
= topmost
->core
.parent
;
437 if (topmost
->core
.width
< w
) x
= topmost
->core
.x
;
438 else x
= topmost
->core
.x
+ ((topmost
->core
.width
- w
) / 2);
439 if (topmost
->core
.height
< h
) y
= topmost
->core
.y
;
440 else y
= topmost
->core
.y
+ ((topmost
->core
.height
- h
) / 2);
441 /* Using XtMoveWidget caused the widget to come
442 out in the wrong place with vtwm.
443 Question of virtual vs real coords, perhaps. */
444 XtSetArg (args
[0], XtNx
, x
);
445 XtSetArg (args
[1], XtNy
, y
);
446 XtSetValues (shell
, args
, 2);
449 /* Finally, pop it up. */
450 XtPopup (shell
, XtGrabNonexclusive
);
453 XtManageChild (widget
);
457 if (XtIsSubclass (widget
, dialogWidgetClass
))
458 XtUnmanageChild (XtParent (widget
));
460 XtUnmanageChild (widget
);
467 static char overrideTrans
[] =
468 "<Message>WM_PROTOCOLS: lwlib_delete_dialog()";
469 /* Dialogs pop down on any key press */
470 static char dialogOverride
[] =
471 "<KeyPress>Escape: lwlib_delete_dialog()";
472 static void wm_delete_window (Widget w
,
475 Cardinal
*num_params
);
476 static XtActionsRec xaw_actions
[] = {
477 {"lwlib_delete_dialog", wm_delete_window
}
479 static Boolean actions_initted
= False
;
482 static XtActionsRec button_actions
[] =
484 { "my_reset", command_reset
},
485 { "my_press", command_press
},
488 "<Leave>: reset() my_reset()\n"
489 "<Btn1Down>: set() my_press()\n"
490 "<Btn1Up>: my_reset() notify() unset()\n";
494 make_dialog (char* name
,
499 Boolean text_input_slot
,
504 widget_instance
*instance
)
509 char button_name
[255];
513 XtTranslations override
;
515 XftFont
*xft_font
= 0;
516 XtTranslations button_override
;
519 if (! pop_up_p
) abort (); /* not implemented */
520 if (text_input_slot
) abort (); /* not implemented */
521 if (radio_box
) abort (); /* not implemented */
522 if (list
) abort (); /* not implemented */
524 if (! actions_initted
)
526 XtAppContext app
= XtWidgetToApplicationContext (parent
);
527 XtAppAddActions (app
, xaw_actions
,
528 sizeof (xaw_actions
) / sizeof (xaw_actions
[0]));
530 XtAppAddActions (app
, button_actions
,
531 sizeof (button_actions
) / sizeof (button_actions
[0]));
533 actions_initted
= True
;
536 override
= XtParseTranslationTable (overrideTrans
);
539 XtSetArg (av
[ac
], XtNtitle
, shell_title
); ac
++;
540 XtSetArg (av
[ac
], XtNallowShellResize
, True
); ac
++;
542 /* Don't allow any geometry request from the user. */
543 XtSetArg (av
[ac
], XtNgeometry
, 0); ac
++;
545 shell
= XtCreatePopupShell ("dialog", transientShellWidgetClass
,
547 XtOverrideTranslations (shell
, override
);
550 dialog
= XtCreateManagedWidget (name
, dialogWidgetClass
, shell
, av
, ac
);
551 override
= XtParseTranslationTable (dialogOverride
);
552 XtOverrideTranslations (dialog
, override
);
559 XtVaGetValues (dialog
,
560 XtNnumChildren
, &num
,
561 XtNchildren
, &ch
, NULL
);
562 for (i
= 0; i
< num
; ++i
)
564 if (!XtIsSubclass (ch
[i
], commandWidgetClass
)
565 && XtIsSubclass (ch
[i
], labelWidgetClass
))
571 instance
->xft_data
= 0;
572 instance
->nr_xft_data
= 0;
576 { { "font", "Font", XtRString
, sizeof(String
), 0, XtRString
,
577 (XtPointer
)"Sans-10" }};
578 char *fontName
= NULL
;
579 XtVaGetSubresources (dialog
, &fontName
, "Dialog", "dialog",
580 rec
, 1, (String
)NULL
);
583 XFontStruct
*xfn
= XLoadQueryFont (XtDisplay (dialog
), fontName
);
585 xft_font
= openFont (dialog
, fontName
);
587 XFreeFont (XtDisplay (dialog
), xfn
);
592 instance
->nr_xft_data
= left_buttons
+ right_buttons
+ 1;
593 instance
->xft_data
= calloc (instance
->nr_xft_data
,
594 sizeof(*instance
->xft_data
));
596 fill_xft_data (&instance
->xft_data
[0], w
, xft_font
);
600 button_override
= XtParseTranslationTable (buttonTrans
);
606 for (i
= 0; i
< left_buttons
; i
++)
609 XtSetArg (av
[ac
], XtNfromHoriz
, button
); ac
++;
610 XtSetArg (av
[ac
], XtNleft
, XtChainLeft
); ac
++;
611 XtSetArg (av
[ac
], XtNright
, XtChainLeft
); ac
++;
612 XtSetArg (av
[ac
], XtNtop
, XtChainBottom
); ac
++;
613 XtSetArg (av
[ac
], XtNbottom
, XtChainBottom
); ac
++;
614 XtSetArg (av
[ac
], XtNresizable
, True
); ac
++;
616 if (DefaultDepthOfScreen (XtScreen (dialog
)) >= 16)
618 /* Turn of dithered shadow if we can. Looks bad */
619 XtSetArg (av
[ac
], "beNiceToColormap", False
); ac
++;
622 sprintf (button_name
, "button%d", ++bc
);
623 button
= XtCreateManagedWidget (button_name
, commandWidgetClass
,
628 fill_xft_data (&instance
->xft_data
[bc
], button
, xft_font
);
629 XtOverrideTranslations (button
, button_override
);
634 for (i
= 0; i
< right_buttons
; i
++)
637 XtSetArg (av
[ac
], XtNfromHoriz
, button
); ac
++;
640 /* Separator to the other buttons. */
641 XtSetArg (av
[ac
], XtNhorizDistance
, 30); ac
++;
643 XtSetArg (av
[ac
], XtNleft
, XtChainRight
); ac
++;
644 XtSetArg (av
[ac
], XtNright
, XtChainRight
); ac
++;
645 XtSetArg (av
[ac
], XtNtop
, XtChainBottom
); ac
++;
646 XtSetArg (av
[ac
], XtNbottom
, XtChainBottom
); ac
++;
647 XtSetArg (av
[ac
], XtNresizable
, True
); ac
++;
649 if (DefaultDepthOfScreen (XtScreen (dialog
)) >= 16)
651 /* Turn of dithered shadow if we can. Looks bad */
652 XtSetArg (av
[ac
], "beNiceToColormap", False
); ac
++;
655 sprintf (button_name
, "button%d", ++bc
);
656 button
= XtCreateManagedWidget (button_name
, commandWidgetClass
,
661 fill_xft_data (&instance
->xft_data
[bc
], button
, xft_font
);
662 XtOverrideTranslations (button
, button_override
);
671 xaw_create_dialog (widget_instance
*instance
)
673 char *name
= instance
->info
->type
;
674 Widget parent
= instance
->parent
;
676 Boolean pop_up_p
= instance
->pop_up_p
;
677 char *shell_name
= 0;
679 Boolean text_input_slot
= False
;
680 Boolean radio_box
= False
;
681 Boolean list
= False
;
683 int left_buttons
= 0;
684 int right_buttons
= 1;
688 icon_name
= "dbox-error";
689 shell_name
= "Error";
693 icon_name
= "dbox-info";
694 shell_name
= "Information";
699 icon_name
= "dbox-question";
700 shell_name
= "Prompt";
704 text_input_slot
= True
;
705 icon_name
= "dbox-question";
706 shell_name
= "Prompt";
710 icon_name
= "dbox-question";
711 shell_name
= "Question";
715 total_buttons
= name
[1] - '0';
717 if (name
[3] == 'T' || name
[3] == 't')
719 text_input_slot
= False
;
723 right_buttons
= name
[4] - '0';
725 left_buttons
= total_buttons
- right_buttons
;
727 widget
= make_dialog (name
, parent
, pop_up_p
,
728 shell_name
, icon_name
, text_input_slot
, radio_box
,
729 list
, left_buttons
, right_buttons
, instance
);
735 xaw_generic_callback (Widget widget
, XtPointer closure
, XtPointer call_data
)
737 widget_instance
*instance
= (widget_instance
*) closure
;
738 Widget instance_widget
;
742 lw_internal_update_other_instances (widget
, closure
, call_data
);
746 if (widget
->core
.being_destroyed
)
749 instance_widget
= instance
->widget
;
750 if (!instance_widget
)
753 id
= instance
->info
->id
;
755 /* Damn! Athena doesn't give us a way to hang our own data on the
756 buttons, so we have to go find it... I guess this assumes that
757 all instances of a button have the same call data. */
759 widget_value
*val
= instance
->info
->val
->contents
;
760 char *name
= XtName (widget
);
763 if (val
->name
&& !strcmp (val
->name
, name
))
768 user_data
= val
->call_data
;
771 if (instance
->info
->selection_cb
)
772 instance
->info
->selection_cb (widget
, id
, user_data
);
776 wm_delete_window (Widget w
,
779 Cardinal
*num_params
)
785 Widget widget
= 0, shell
;
787 if (XtIsSubclass (w
, dialogWidgetClass
))
788 shell
= XtParent (w
);
792 if (! XtIsSubclass (shell
, shellWidgetClass
))
794 XtVaGetValues (shell
, XtNnumChildren
, &nkids
, NULL
);
795 XtVaGetValues (shell
, XtNchildren
, &kids
, NULL
);
798 for (i
= 0; i
< nkids
; i
++)
801 if (XtIsSubclass (widget
, dialogWidgetClass
))
804 if (! widget
) return;
806 id
= lw_get_widget_id (widget
);
810 widget_info
*info
= lw_get_widget_info (id
);
811 if (! info
) abort ();
812 if (info
->selection_cb
)
813 info
->selection_cb (widget
, id
, (XtPointer
) -1);
816 lw_destroy_all_widgets (id
);
822 xaw_create_main (widget_instance
*instance
)
827 /* Create a vertical Paned to hold menubar */
829 XtSetArg (al
[ac
], XtNborderWidth
, 0); ac
++;
830 return XtCreateWidget (instance
->info
->name
, panedWidgetClass
,
831 instance
->parent
, al
, ac
);
834 widget_creation_entry
835 xaw_creation_table
[] =
837 {"main", xaw_create_main
},