Label.c revision 7a84e134
17a84e134Smrg/* $Xorg: Label.c,v 1.5 2001/02/09 02:03:43 xorgcvs Exp $ */
27a84e134Smrg
37a84e134Smrg/***********************************************************
47a84e134Smrg
57a84e134SmrgCopyright 1987, 1988, 1994, 1998  The Open Group
67a84e134Smrg
77a84e134SmrgPermission to use, copy, modify, distribute, and sell this software and its
87a84e134Smrgdocumentation for any purpose is hereby granted without fee, provided that
97a84e134Smrgthe above copyright notice appear in all copies and that both that
107a84e134Smrgcopyright notice and this permission notice appear in supporting
117a84e134Smrgdocumentation.
127a84e134Smrg
137a84e134SmrgThe above copyright notice and this permission notice shall be included in
147a84e134Smrgall copies or substantial portions of the Software.
157a84e134Smrg
167a84e134SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
177a84e134SmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
187a84e134SmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
197a84e134SmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
207a84e134SmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
217a84e134SmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
227a84e134Smrg
237a84e134SmrgExcept as contained in this notice, the name of The Open Group shall not be
247a84e134Smrgused in advertising or otherwise to promote the sale, use or other dealings
257a84e134Smrgin this Software without prior written authorization from The Open Group.
267a84e134Smrg
277a84e134Smrg
287a84e134SmrgCopyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts.
297a84e134Smrg
307a84e134Smrg                        All Rights Reserved
317a84e134Smrg
327a84e134SmrgPermission to use, copy, modify, and distribute this software and its
337a84e134Smrgdocumentation for any purpose and without fee is hereby granted,
347a84e134Smrgprovided that the above copyright notice appear in all copies and that
357a84e134Smrgboth that copyright notice and this permission notice appear in
367a84e134Smrgsupporting documentation, and that the name of Digital not be
377a84e134Smrgused in advertising or publicity pertaining to distribution of the
387a84e134Smrgsoftware without specific, written prior permission.
397a84e134Smrg
407a84e134SmrgDIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
417a84e134SmrgALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
427a84e134SmrgDIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
437a84e134SmrgANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
447a84e134SmrgWHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
457a84e134SmrgARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
467a84e134SmrgSOFTWARE.
477a84e134Smrg
487a84e134Smrg******************************************************************/
497a84e134Smrg/* $XFree86: xc/lib/Xaw/Label.c,v 1.12 2001/01/17 19:42:27 dawes Exp $ */
507a84e134Smrg
517a84e134Smrg#ifdef HAVE_CONFIG_H
527a84e134Smrg#include <config.h>
537a84e134Smrg#endif
547a84e134Smrg#include <stdio.h>
557a84e134Smrg#include <ctype.h>
567a84e134Smrg#include <X11/IntrinsicP.h>
577a84e134Smrg#include <X11/StringDefs.h>
587a84e134Smrg#include <X11/Xos.h>
597a84e134Smrg#include <X11/Xmu/Converters.h>
607a84e134Smrg#include <X11/Xmu/Drawing.h>
617a84e134Smrg#include <X11/Xaw/LabelP.h>
627a84e134Smrg#include <X11/Xaw/XawInit.h>
637a84e134Smrg#include "Private.h"
647a84e134Smrg
657a84e134Smrg#define streq(a, b)		(strcmp((a), (b)) == 0)
667a84e134Smrg
677a84e134Smrg#define MULTI_LINE_LABEL 32767
687a84e134Smrg
697a84e134Smrg#ifdef CRAY
707a84e134Smrg#define WORD64
717a84e134Smrg#endif
727a84e134Smrg
737a84e134Smrg/*
747a84e134Smrg * Class Methods
757a84e134Smrg */
767a84e134Smrgstatic void XawLabelClassInitialize(void);
777a84e134Smrgstatic void XawLabelDestroy(Widget);
787a84e134Smrgstatic void XawLabelInitialize(Widget, Widget, ArgList, Cardinal*);
797a84e134Smrgstatic XtGeometryResult XawLabelQueryGeometry(Widget, XtWidgetGeometry*,
807a84e134Smrg					      XtWidgetGeometry*);
817a84e134Smrgstatic void XawLabelRedisplay(Widget, XEvent*, Region);
827a84e134Smrgstatic void XawLabelResize(Widget);
837a84e134Smrgstatic Boolean XawLabelSetValues(Widget, Widget, Widget,
847a84e134Smrg				 ArgList, Cardinal*);
857a84e134Smrg
867a84e134Smrg/*
877a84e134Smrg * Prototypes
887a84e134Smrg */
897a84e134Smrg#ifdef WORD64
907a84e134Smrgstatic int _XawLabelWidth16(XFontStruct*, char*, int);
917a84e134Smrgstatic void _XawLabelDraw16(Display*, Drawable, GC, int, int, char*, int);
927a84e134Smrg#endif
937a84e134Smrgstatic void compute_bitmap_offsets(LabelWidget);
947a84e134Smrgstatic void GetGrayGC(LabelWidget);
957a84e134Smrgstatic void GetNormalGC(LabelWidget);
967a84e134Smrgstatic void _Reposition(LabelWidget, unsigned int, unsigned int,
977a84e134Smrg			Position*, Position*);
987a84e134Smrgstatic void set_bitmap_info(LabelWidget);
997a84e134Smrgstatic void SetTextWidthAndHeight(LabelWidget);
1007a84e134Smrg
1017a84e134Smrg/*
1027a84e134Smrg * Initialization
1037a84e134Smrg */
1047a84e134Smrg#define offset(field) XtOffsetOf(LabelRec, field)
1057a84e134Smrgstatic XtResource resources[] = {
1067a84e134Smrg  {
1077a84e134Smrg    XtNforeground,
1087a84e134Smrg    XtCForeground,
1097a84e134Smrg    XtRPixel,
1107a84e134Smrg    sizeof(Pixel),
1117a84e134Smrg    offset(label.foreground),
1127a84e134Smrg    XtRString,
1137a84e134Smrg    XtDefaultForeground
1147a84e134Smrg  },
1157a84e134Smrg  {
1167a84e134Smrg    XtNfont,
1177a84e134Smrg    XtCFont,
1187a84e134Smrg    XtRFontStruct,
1197a84e134Smrg    sizeof(XFontStruct*),
1207a84e134Smrg    offset(label.font),
1217a84e134Smrg    XtRString,
1227a84e134Smrg    XtDefaultFont
1237a84e134Smrg  },
1247a84e134Smrg  {
1257a84e134Smrg    XtNfontSet,
1267a84e134Smrg    XtCFontSet,
1277a84e134Smrg    XtRFontSet,
1287a84e134Smrg    sizeof(XFontSet),
1297a84e134Smrg    offset(label.fontset),
1307a84e134Smrg    XtRString,
1317a84e134Smrg    XtDefaultFontSet
1327a84e134Smrg  },
1337a84e134Smrg  {
1347a84e134Smrg    XtNlabel,
1357a84e134Smrg    XtCLabel,
1367a84e134Smrg    XtRString,
1377a84e134Smrg    sizeof(String),
1387a84e134Smrg    offset(label.label),
1397a84e134Smrg    XtRString,
1407a84e134Smrg    NULL
1417a84e134Smrg  },
1427a84e134Smrg  {
1437a84e134Smrg    XtNencoding,
1447a84e134Smrg    XtCEncoding,
1457a84e134Smrg    XtRUnsignedChar,
1467a84e134Smrg    sizeof(unsigned char),
1477a84e134Smrg    offset(label.encoding),
1487a84e134Smrg    XtRImmediate,
1497a84e134Smrg    (XtPointer)XawTextEncoding8bit
1507a84e134Smrg  },
1517a84e134Smrg  {
1527a84e134Smrg    XtNjustify,
1537a84e134Smrg    XtCJustify,
1547a84e134Smrg    XtRJustify,
1557a84e134Smrg    sizeof(XtJustify),
1567a84e134Smrg    offset(label.justify),
1577a84e134Smrg    XtRImmediate,
1587a84e134Smrg    (XtPointer)XtJustifyCenter
1597a84e134Smrg  },
1607a84e134Smrg  {
1617a84e134Smrg    XtNinternalWidth,
1627a84e134Smrg    XtCWidth,
1637a84e134Smrg    XtRDimension,
1647a84e134Smrg    sizeof(Dimension),
1657a84e134Smrg    offset(label.internal_width),
1667a84e134Smrg    XtRImmediate,
1677a84e134Smrg    (XtPointer)4
1687a84e134Smrg  },
1697a84e134Smrg  {
1707a84e134Smrg    XtNinternalHeight,
1717a84e134Smrg    XtCHeight,
1727a84e134Smrg    XtRDimension,
1737a84e134Smrg    sizeof(Dimension),
1747a84e134Smrg    offset(label.internal_height),
1757a84e134Smrg    XtRImmediate,
1767a84e134Smrg    (XtPointer)2
1777a84e134Smrg  },
1787a84e134Smrg  {
1797a84e134Smrg    XtNleftBitmap,
1807a84e134Smrg    XtCLeftBitmap,
1817a84e134Smrg    XtRBitmap,
1827a84e134Smrg    sizeof(Pixmap),
1837a84e134Smrg    offset(label.left_bitmap),
1847a84e134Smrg    XtRImmediate,
1857a84e134Smrg    (XtPointer)None
1867a84e134Smrg  },
1877a84e134Smrg  {
1887a84e134Smrg    XtNbitmap,
1897a84e134Smrg    XtCPixmap,
1907a84e134Smrg    XtRBitmap,
1917a84e134Smrg    sizeof(Pixmap),
1927a84e134Smrg    offset(label.pixmap),
1937a84e134Smrg    XtRImmediate,
1947a84e134Smrg    (XtPointer)None
1957a84e134Smrg  },
1967a84e134Smrg  {
1977a84e134Smrg    XtNresize,
1987a84e134Smrg    XtCResize,
1997a84e134Smrg    XtRBoolean,
2007a84e134Smrg    sizeof(Boolean),
2017a84e134Smrg    offset(label.resize),
2027a84e134Smrg    XtRImmediate,
2037a84e134Smrg    (XtPointer)True
2047a84e134Smrg  },
2057a84e134Smrg  {
2067a84e134Smrg    XtNlabelX,
2077a84e134Smrg    XtCPosition,
2087a84e134Smrg    XtRPosition,
2097a84e134Smrg    sizeof(Position),
2107a84e134Smrg    offset(label.label_x),
2117a84e134Smrg    XtRImmediate,
2127a84e134Smrg    (XtPointer)0
2137a84e134Smrg  },
2147a84e134Smrg  {
2157a84e134Smrg    XtNlabelY,
2167a84e134Smrg    XtCPosition,
2177a84e134Smrg    XtRPosition,
2187a84e134Smrg    sizeof(Position),
2197a84e134Smrg    offset(label.label_y),
2207a84e134Smrg    XtRImmediate,
2217a84e134Smrg    (XtPointer)0
2227a84e134Smrg  },
2237a84e134Smrg};
2247a84e134Smrg#undef offset
2257a84e134Smrg
2267a84e134Smrg#define Superclass (&simpleClassRec)
2277a84e134SmrgLabelClassRec labelClassRec = {
2287a84e134Smrg  /* core */
2297a84e134Smrg  {
2307a84e134Smrg    (WidgetClass)&simpleClassRec,	/* superclass */
2317a84e134Smrg    "Label",				/* class_name */
2327a84e134Smrg    sizeof(LabelRec),			/* widget_size */
2337a84e134Smrg    XawLabelClassInitialize,		/* class_initialize */
2347a84e134Smrg    NULL,				/* class_part_initialize */
2357a84e134Smrg    False,				/* class_inited */
2367a84e134Smrg    XawLabelInitialize,			/* initialize */
2377a84e134Smrg    NULL,				/* initialize_hook */
2387a84e134Smrg    XtInheritRealize,			/* realize */
2397a84e134Smrg    NULL,				/* actions */
2407a84e134Smrg    0,					/* num_actions */
2417a84e134Smrg    resources,				/* resources */
2427a84e134Smrg    XtNumber(resources),		/* num_resources */
2437a84e134Smrg    NULLQUARK,				/* xrm_class */
2447a84e134Smrg    True,				/* compress_motion */
2457a84e134Smrg    True,				/* compress_exposure */
2467a84e134Smrg    True,				/* compress_enterleave */
2477a84e134Smrg    False,				/* visible_interest */
2487a84e134Smrg    XawLabelDestroy,			/* destroy */
2497a84e134Smrg    XawLabelResize,			/* resize */
2507a84e134Smrg    XawLabelRedisplay,			/* expose */
2517a84e134Smrg    XawLabelSetValues,			/* set_values */
2527a84e134Smrg    NULL,				/* set_values_hook */
2537a84e134Smrg    XtInheritSetValuesAlmost,		/* set_values_almost */
2547a84e134Smrg    NULL,				/* get_values_hook */
2557a84e134Smrg    NULL,				/* accept_focus */
2567a84e134Smrg    XtVersion,				/* version */
2577a84e134Smrg    NULL,				/* callback_private */
2587a84e134Smrg    NULL,				/* tm_table */
2597a84e134Smrg    XawLabelQueryGeometry,		/* query_geometry */
2607a84e134Smrg    XtInheritDisplayAccelerator,	/* display_accelerator */
2617a84e134Smrg    NULL,				/* extension */
2627a84e134Smrg  },
2637a84e134Smrg  /* simple */
2647a84e134Smrg  {
2657a84e134Smrg    XtInheritChangeSensitive,		/* change_sensitive */
2667a84e134Smrg  },
2677a84e134Smrg  /* label */
2687a84e134Smrg  {
2697a84e134Smrg    NULL,				/* extension */
2707a84e134Smrg  }
2717a84e134Smrg};
2727a84e134Smrg
2737a84e134SmrgWidgetClass labelWidgetClass = (WidgetClass)&labelClassRec;
2747a84e134Smrg
2757a84e134Smrg/*
2767a84e134Smrg * Implementation
2777a84e134Smrg */
2787a84e134Smrgstatic void
2797a84e134SmrgXawLabelClassInitialize(void)
2807a84e134Smrg{
2817a84e134Smrg    XawInitializeWidgetSet();
2827a84e134Smrg    XtAddConverter(XtRString, XtRJustify, XmuCvtStringToJustify, NULL, 0);
2837a84e134Smrg    XtSetTypeConverter(XtRJustify, XtRString, XmuCvtJustifyToString,
2847a84e134Smrg		       NULL, 0, XtCacheNone, NULL);
2857a84e134Smrg}
2867a84e134Smrg
2877a84e134Smrg#ifndef WORD64
2887a84e134Smrg#define TXT16 XChar2b
2897a84e134Smrg#else
2907a84e134Smrg#define TXT16 char
2917a84e134Smrg
2927a84e134Smrgstatic XChar2b *buf2b;
2937a84e134Smrgstatic int buf2blen = 0;
2947a84e134Smrg
2957a84e134Smrgstatic int
2967a84e134Smrg_XawLabelWidth16(XFontStruct *fs, char *str, int n)
2977a84e134Smrg{
2987a84e134Smrg    int i;
2997a84e134Smrg    XChar2b *ptr;
3007a84e134Smrg
3017a84e134Smrg    if (n > buf2blen) {
3027a84e134Smrg	buf2b = (XChar2b *)XtRealloc((char *)buf2b, n * sizeof(XChar2b));
3037a84e134Smrg	buf2blen = n;
3047a84e134Smrg    }
3057a84e134Smrg    for (ptr = buf2b, i = n; --i >= 0; ptr++) {
3067a84e134Smrg	ptr->byte1 = *str++;
3077a84e134Smrg	ptr->byte2 = *str++;
3087a84e134Smrg    }
3097a84e134Smrg
3107a84e134Smrg    return (XTextWidth16(fs, buf2b, n));
3117a84e134Smrg}
3127a84e134Smrg
3137a84e134Smrgstatic void
3147a84e134Smrg_XawLabelDraw16(Display *dpy, Drawable d, GC gc, int x, int y,
3157a84e134Smrg		char *str, int n)
3167a84e134Smrg{
3177a84e134Smrg    int i;
3187a84e134Smrg    XChar2b *ptr;
3197a84e134Smrg
3207a84e134Smrg    if (n > buf2blen) {
3217a84e134Smrg	buf2b = (XChar2b *)XtRealloc((char *)buf2b, n * sizeof(XChar2b));
3227a84e134Smrg	buf2blen = n;
3237a84e134Smrg    }
3247a84e134Smrg    for (ptr = buf2b, i = n; --i >= 0; ptr++) {
3257a84e134Smrg	ptr->byte1 = *str++;
3267a84e134Smrg	ptr->byte2 = *str++;
3277a84e134Smrg    }
3287a84e134Smrg    XDrawString16(dpy, d, gc, x, y, buf2b, n);
3297a84e134Smrg}
3307a84e134Smrg
3317a84e134Smrg#define XTextWidth16 _XawLabelWidth16
3327a84e134Smrg#define XDrawString16 _XawLabelDraw16
3337a84e134Smrg#endif /* WORD64 */
3347a84e134Smrg
3357a84e134Smrg/*
3367a84e134Smrg * Calculate width and height of displayed text in pixels
3377a84e134Smrg */
3387a84e134Smrgstatic void
3397a84e134SmrgSetTextWidthAndHeight(LabelWidget lw)
3407a84e134Smrg{
3417a84e134Smrg    XFontStruct	*fs = lw->label.font;
3427a84e134Smrg    char *nl;
3437a84e134Smrg
3447a84e134Smrg    if (lw->label.pixmap != None) {
3457a84e134Smrg	Window root;
3467a84e134Smrg	int x, y;
3477a84e134Smrg	unsigned int width, height, bw, depth;
3487a84e134Smrg
3497a84e134Smrg	if (XGetGeometry(XtDisplay(lw), lw->label.pixmap, &root, &x, &y,
3507a84e134Smrg		       &width, &height, &bw, &depth)) {
3517a84e134Smrg	    lw->label.label_height = height;
3527a84e134Smrg	    lw->label.label_width = width;
3537a84e134Smrg	    lw->label.label_len = depth;
3547a84e134Smrg	    return;
3557a84e134Smrg	}
3567a84e134Smrg    }
3577a84e134Smrg    if (lw->simple.international == True) {
3587a84e134Smrg	XFontSet	fset = lw->label.fontset;
3597a84e134Smrg	XFontSetExtents *ext = XExtentsOfFontSet(fset);
3607a84e134Smrg
3617a84e134Smrg	lw->label.label_height = ext->max_ink_extent.height;
3627a84e134Smrg	if (lw->label.label == NULL) {
3637a84e134Smrg	    lw->label.label_len = 0;
3647a84e134Smrg	    lw->label.label_width = 0;
3657a84e134Smrg	}
3667a84e134Smrg	else if ((nl = index(lw->label.label, '\n')) != NULL) {
3677a84e134Smrg	    char *label;
3687a84e134Smrg
3697a84e134Smrg	    lw->label.label_len = MULTI_LINE_LABEL;
3707a84e134Smrg	    lw->label.label_width = 0;
3717a84e134Smrg	    for (label = lw->label.label; nl != NULL; nl = index(label, '\n')) {
3727a84e134Smrg		int width = XmbTextEscapement(fset, label, (int)(nl - label));
3737a84e134Smrg
3747a84e134Smrg		if (width > (int)lw->label.label_width)
3757a84e134Smrg		    lw->label.label_width = width;
3767a84e134Smrg		label = nl + 1;
3777a84e134Smrg		if (*label)
3787a84e134Smrg		    lw->label.label_height += ext->max_ink_extent.height;
3797a84e134Smrg	    }
3807a84e134Smrg	    if (*label) {
3817a84e134Smrg		int width = XmbTextEscapement(fset, label, strlen(label));
3827a84e134Smrg
3837a84e134Smrg		if (width > (int)lw->label.label_width)
3847a84e134Smrg		    lw->label.label_width = width;
3857a84e134Smrg	    }
3867a84e134Smrg	}
3877a84e134Smrg	else {
3887a84e134Smrg	    lw->label.label_len = strlen(lw->label.label);
3897a84e134Smrg	    lw->label.label_width =
3907a84e134Smrg		XmbTextEscapement(fset, lw->label.label, lw->label.label_len);
3917a84e134Smrg	}
3927a84e134Smrg    }
3937a84e134Smrg    else {
3947a84e134Smrg	lw->label.label_height = fs->max_bounds.ascent + fs->max_bounds.descent;
3957a84e134Smrg	if (lw->label.label == NULL) {
3967a84e134Smrg	    lw->label.label_len = 0;
3977a84e134Smrg	    lw->label.label_width = 0;
3987a84e134Smrg	}
3997a84e134Smrg	else if ((nl = index(lw->label.label, '\n')) != NULL) {
4007a84e134Smrg	    char *label;
4017a84e134Smrg
4027a84e134Smrg	    lw->label.label_len = MULTI_LINE_LABEL;
4037a84e134Smrg	    lw->label.label_width = 0;
4047a84e134Smrg	    for (label = lw->label.label; nl != NULL; nl = index(label, '\n')) {
4057a84e134Smrg		int width;
4067a84e134Smrg
4077a84e134Smrg		if (lw->label.encoding)
4087a84e134Smrg		    width = XTextWidth16(fs, (TXT16*)label, (int)(nl - label) / 2);
4097a84e134Smrg		else
4107a84e134Smrg		    width = XTextWidth(fs, label, (int)(nl - label));
4117a84e134Smrg		if (width > (int)lw->label.label_width)
4127a84e134Smrg		    lw->label.label_width = width;
4137a84e134Smrg		label = nl + 1;
4147a84e134Smrg		if (*label)
4157a84e134Smrg		    lw->label.label_height +=
4167a84e134Smrg			fs->max_bounds.ascent + fs->max_bounds.descent;
4177a84e134Smrg	    }
4187a84e134Smrg	    if (*label) {
4197a84e134Smrg		int width = XTextWidth(fs, label, strlen(label));
4207a84e134Smrg
4217a84e134Smrg		if (lw->label.encoding)
4227a84e134Smrg		    width = XTextWidth16(fs, (TXT16*)label, strlen(label) / 2);
4237a84e134Smrg		else
4247a84e134Smrg		    width = XTextWidth(fs, label, strlen(label));
4257a84e134Smrg		if (width > (int) lw->label.label_width)
4267a84e134Smrg		    lw->label.label_width = width;
4277a84e134Smrg	    }
4287a84e134Smrg	}
4297a84e134Smrg	else {
4307a84e134Smrg	    lw->label.label_len = strlen(lw->label.label);
4317a84e134Smrg	    if (lw->label.encoding)
4327a84e134Smrg		lw->label.label_width =
4337a84e134Smrg		    XTextWidth16(fs, (TXT16*)lw->label.label,
4347a84e134Smrg			   (int)lw->label.label_len / 2);
4357a84e134Smrg	    else
4367a84e134Smrg		lw->label.label_width =
4377a84e134Smrg	      XTextWidth(fs, lw->label.label, (int)lw->label.label_len);
4387a84e134Smrg	}
4397a84e134Smrg    }
4407a84e134Smrg}
4417a84e134Smrg
4427a84e134Smrgstatic void
4437a84e134SmrgGetNormalGC(LabelWidget lw)
4447a84e134Smrg{
4457a84e134Smrg    XGCValues	values;
4467a84e134Smrg
4477a84e134Smrg    values.foreground	= lw->label.foreground;
4487a84e134Smrg    values.background	= lw->core.background_pixel;
4497a84e134Smrg    values.font		= lw->label.font->fid;
4507a84e134Smrg    values.graphics_exposures = False;
4517a84e134Smrg
4527a84e134Smrg    if (lw->simple.international == True)
4537a84e134Smrg    /* Since Xmb/wcDrawString eats the font, I must use XtAllocateGC */
4547a84e134Smrg	lw->label.normal_GC = XtAllocateGC((Widget)lw, 0,
4557a84e134Smrg					   GCForeground | GCBackground |
4567a84e134Smrg					   GCGraphicsExposures,
4577a84e134Smrg					   &values, GCFont, 0);
4587a84e134Smrg    else
4597a84e134Smrg	lw->label.normal_GC = XtGetGC((Widget)lw,
4607a84e134Smrg				      GCForeground | GCBackground | GCFont |
4617a84e134Smrg				      GCGraphicsExposures, &values);
4627a84e134Smrg}
4637a84e134Smrg
4647a84e134Smrgstatic void
4657a84e134SmrgGetGrayGC(LabelWidget lw)
4667a84e134Smrg{
4677a84e134Smrg    XGCValues	values;
4687a84e134Smrg
4697a84e134Smrg    values.foreground = lw->label.foreground;
4707a84e134Smrg    values.background = lw->core.background_pixel;
4717a84e134Smrg    values.font	      = lw->label.font->fid;
4727a84e134Smrg    values.fill_style = FillTiled;
4737a84e134Smrg    values.tile       = XmuCreateStippledPixmap(XtScreen((Widget)lw),
4747a84e134Smrg						lw->label.foreground,
4757a84e134Smrg						lw->core.background_pixel,
4767a84e134Smrg						lw->core.depth);
4777a84e134Smrg    values.graphics_exposures = False;
4787a84e134Smrg
4797a84e134Smrg    lw->label.stipple = values.tile;
4807a84e134Smrg    if (lw->simple.international == True)
4817a84e134Smrg	/* Since Xmb/wcDrawString eats the font, I must use XtAllocateGC */
4827a84e134Smrg	lw->label.gray_GC = XtAllocateGC((Widget)lw, 0,
4837a84e134Smrg					 GCForeground | GCBackground |
4847a84e134Smrg					 GCTile | GCFillStyle |
4857a84e134Smrg					 GCGraphicsExposures,
4867a84e134Smrg					 &values, GCFont, 0);
4877a84e134Smrg	else
4887a84e134Smrg	    lw->label.gray_GC = XtGetGC((Widget)lw,
4897a84e134Smrg					GCForeground | GCBackground |
4907a84e134Smrg					GCFont | GCTile | GCFillStyle |
4917a84e134Smrg					GCGraphicsExposures,
4927a84e134Smrg					&values);
4937a84e134Smrg}
4947a84e134Smrg
4957a84e134Smrgstatic void
4967a84e134Smrgcompute_bitmap_offsets(LabelWidget lw)
4977a84e134Smrg{
4987a84e134Smrg    /*
4997a84e134Smrg     * bitmap will be eventually be displayed at
5007a84e134Smrg     * (internal_width, internal_height + lbm_y)
5017a84e134Smrg     */
5027a84e134Smrg    if (lw->label.lbm_height != 0)
5037a84e134Smrg	lw->label.lbm_y = (XtHeight(lw) - (lw->label.internal_height * 2 +
5047a84e134Smrg			   lw->label.lbm_height)) / 2;
5057a84e134Smrg    else
5067a84e134Smrg	lw->label.lbm_y = 0;
5077a84e134Smrg}
5087a84e134Smrg
5097a84e134Smrgstatic void
5107a84e134Smrgset_bitmap_info(LabelWidget lw)
5117a84e134Smrg{
5127a84e134Smrg    Window root;
5137a84e134Smrg    int x, y;
5147a84e134Smrg    unsigned int bw, depth;
5157a84e134Smrg
5167a84e134Smrg    if (!(lw->label.left_bitmap
5177a84e134Smrg	&& XGetGeometry(XtDisplay(lw), lw->label.left_bitmap, &root, &x, &y,
5187a84e134Smrg			&lw->label.lbm_width, &lw->label.lbm_height,
5197a84e134Smrg			&bw, &depth)))
5207a84e134Smrg	lw->label.lbm_width = lw->label.lbm_height = 0;
5217a84e134Smrg
5227a84e134Smrg    compute_bitmap_offsets(lw);
5237a84e134Smrg}
5247a84e134Smrg
5257a84e134Smrg/*ARGSUSED*/
5267a84e134Smrgstatic void
5277a84e134SmrgXawLabelInitialize(Widget request, Widget cnew,
5287a84e134Smrg		   ArgList args, Cardinal *num_args)
5297a84e134Smrg{
5307a84e134Smrg    LabelWidget lw = (LabelWidget)cnew;
5317a84e134Smrg
5327a84e134Smrg    if (!lw->label.font) XtError("Aborting: no font found\n");
5337a84e134Smrg    if (lw->simple.international && !lw->label.fontset)
5347a84e134Smrg	XtError("Aborting: no fontset found\n");
5357a84e134Smrg
5367a84e134Smrg    if (lw->label.label == NULL)
5377a84e134Smrg	lw->label.label = XtNewString(lw->core.name);
5387a84e134Smrg    else
5397a84e134Smrg	lw->label.label = XtNewString(lw->label.label);
5407a84e134Smrg
5417a84e134Smrg    GetNormalGC(lw);
5427a84e134Smrg    GetGrayGC(lw);
5437a84e134Smrg
5447a84e134Smrg    SetTextWidthAndHeight(lw);
5457a84e134Smrg
5467a84e134Smrg    if (XtHeight(lw) == 0)
5477a84e134Smrg	XtHeight(lw) = lw->label.label_height + 2 * lw->label.internal_height;
5487a84e134Smrg
5497a84e134Smrg    set_bitmap_info(lw);		/* need core.height */
5507a84e134Smrg
5517a84e134Smrg    if (XtWidth(lw) == 0)		/* need label.lbm_width */
5527a84e134Smrg	XtWidth(lw) = lw->label.label_width + 2 * lw->label.internal_width +
5537a84e134Smrg		      LEFT_OFFSET(lw);
5547a84e134Smrg
5557a84e134Smrg    lw->label.label_x = lw->label.label_y = 0;
5567a84e134Smrg    (*XtClass(cnew)->core_class.resize)((Widget)lw);
5577a84e134Smrg}
5587a84e134Smrg
5597a84e134Smrg/*ARGSUSED*/
5607a84e134Smrgstatic void
5617a84e134SmrgXawLabelRedisplay(Widget gw, XEvent *event, Region region)
5627a84e134Smrg{
5637a84e134Smrg    LabelWidget w = (LabelWidget)gw;
5647a84e134Smrg    GC gc;
5657a84e134Smrg
5667a84e134Smrg    if (*Superclass->core_class.expose != NULL)
5677a84e134Smrg	(*Superclass->core_class.expose)(gw, event, region);
5687a84e134Smrg
5697a84e134Smrg    gc = XtIsSensitive(gw) ? w->label.normal_GC : w->label.gray_GC;
5707a84e134Smrg#ifdef notdef
5717a84e134Smrg    if (region != NULL)
5727a84e134Smrg	XSetRegion(XtDisplay(gw), gc, region);
5737a84e134Smrg#endif /*notdef*/
5747a84e134Smrg
5757a84e134Smrg    if (w->label.pixmap == None) {
5767a84e134Smrg	int len = w->label.label_len;
5777a84e134Smrg	char *label = w->label.label;
5787a84e134Smrg	Position y = w->label.label_y + w->label.font->max_bounds.ascent;
5797a84e134Smrg	Position ksy = w->label.label_y;
5807a84e134Smrg
5817a84e134Smrg	/* display left bitmap */
5827a84e134Smrg	if (w->label.left_bitmap && w->label.lbm_width != 0)
5837a84e134Smrg	    XCopyPlane (XtDisplay(gw), w->label.left_bitmap, XtWindow(gw), gc,
5847a84e134Smrg			0, 0, w->label.lbm_width, w->label.lbm_height,
5857a84e134Smrg			w->label.internal_width,
5867a84e134Smrg			w->label.internal_height + w->label.lbm_y, 1L);
5877a84e134Smrg
5887a84e134Smrg	if (w->simple.international == True) {
5897a84e134Smrg	    XFontSetExtents *ext = XExtentsOfFontSet(w->label.fontset);
5907a84e134Smrg
5917a84e134Smrg	    ksy += XawAbs(ext->max_ink_extent.y);
5927a84e134Smrg
5937a84e134Smrg	    if (len == MULTI_LINE_LABEL) {
5947a84e134Smrg		char *nl;
5957a84e134Smrg
5967a84e134Smrg		while ((nl = index(label, '\n')) != NULL) {
5977a84e134Smrg		    XmbDrawString(XtDisplay(w), XtWindow(w), w->label.fontset,
5987a84e134Smrg				  gc, w->label.label_x, ksy, label,
5997a84e134Smrg				  (int)(nl - label));
6007a84e134Smrg		    ksy += ext->max_ink_extent.height;
6017a84e134Smrg		    label = nl + 1;
6027a84e134Smrg		}
6037a84e134Smrg		len = strlen(label);
6047a84e134Smrg	    }
6057a84e134Smrg	    if (len)
6067a84e134Smrg		XmbDrawString(XtDisplay(w), XtWindow(w), w->label.fontset, gc,
6077a84e134Smrg			      w->label.label_x, ksy, label, len);
6087a84e134Smrg	}
6097a84e134Smrg	else {
6107a84e134Smrg	    if (len == MULTI_LINE_LABEL) {
6117a84e134Smrg		char *nl;
6127a84e134Smrg
6137a84e134Smrg		while ((nl = index(label, '\n')) != NULL) {
6147a84e134Smrg		    if (w->label.encoding)
6157a84e134Smrg			XDrawString16(XtDisplay(gw), XtWindow(gw), gc,
6167a84e134Smrg				      w->label.label_x, y,
6177a84e134Smrg				      (TXT16*)label, (int)(nl - label) / 2);
6187a84e134Smrg		    else
6197a84e134Smrg			XDrawString(XtDisplay(gw), XtWindow(gw), gc,
6207a84e134Smrg				    w->label.label_x, y, label, (int)(nl - label));
6217a84e134Smrg		    y += w->label.font->max_bounds.ascent +
6227a84e134Smrg			 w->label.font->max_bounds.descent;
6237a84e134Smrg		    label = nl + 1;
6247a84e134Smrg		}
6257a84e134Smrg		len = strlen(label);
6267a84e134Smrg	    }
6277a84e134Smrg	    if (len) {
6287a84e134Smrg		if (w->label.encoding)
6297a84e134Smrg		    XDrawString16(XtDisplay(gw), XtWindow(gw), gc,
6307a84e134Smrg				  w->label.label_x, y, (TXT16*)label, len / 2);
6317a84e134Smrg		else
6327a84e134Smrg		    XDrawString(XtDisplay(gw), XtWindow(gw), gc,
6337a84e134Smrg				w->label.label_x, y, label, len);
6347a84e134Smrg	    }
6357a84e134Smrg	}
6367a84e134Smrg    }
6377a84e134Smrg    else if (w->label.label_len == 1)
6387a84e134Smrg	XCopyPlane(XtDisplay(gw), w->label.pixmap, XtWindow(gw), gc,
6397a84e134Smrg		   0, 0, w->label.label_width, w->label.label_height,
6407a84e134Smrg		   w->label.label_x, w->label.label_y, 1L);
6417a84e134Smrg    else
6427a84e134Smrg	XCopyArea(XtDisplay(gw), w->label.pixmap, XtWindow(gw), gc,
6437a84e134Smrg		  0, 0, w->label.label_width, w->label.label_height,
6447a84e134Smrg		  w->label.label_x, w->label.label_y);
6457a84e134Smrg
6467a84e134Smrg#ifdef notdef
6477a84e134Smrg    if (region != NULL)
6487a84e134Smrg	XSetClipMask(XtDisplay(gw), gc, (Pixmap)None);
6497a84e134Smrg#endif /* notdef */
6507a84e134Smrg}
6517a84e134Smrg
6527a84e134Smrgstatic void
6537a84e134Smrg_Reposition(LabelWidget lw, unsigned int width, unsigned int height,
6547a84e134Smrg	    Position *dx, Position *dy)
6557a84e134Smrg{
6567a84e134Smrg    Position newPos;
6577a84e134Smrg    Position leftedge = lw->label.internal_width + LEFT_OFFSET(lw);
6587a84e134Smrg
6597a84e134Smrg    switch (lw->label.justify) {
6607a84e134Smrg	case XtJustifyLeft:
6617a84e134Smrg	    newPos = leftedge;
6627a84e134Smrg	    break;
6637a84e134Smrg	case XtJustifyRight:
6647a84e134Smrg	    newPos = width - (lw->label.label_width + lw->label.internal_width);
6657a84e134Smrg	    break;
6667a84e134Smrg	case XtJustifyCenter:
6677a84e134Smrg        /*FALLTRHOUGH*/
6687a84e134Smrg	default:
6697a84e134Smrg	    newPos = (int)(width - lw->label.label_width) >> 1;
6707a84e134Smrg	    break;
6717a84e134Smrg    }
6727a84e134Smrg    if (newPos < (Position)leftedge)
6737a84e134Smrg	newPos = leftedge;
6747a84e134Smrg    *dx = newPos - lw->label.label_x;
6757a84e134Smrg    lw->label.label_x = newPos;
6767a84e134Smrg
6777a84e134Smrg    newPos = (height - lw->label.label_height) >> 1;
6787a84e134Smrg    *dy = newPos - lw->label.label_y;
6797a84e134Smrg    lw->label.label_y = newPos;
6807a84e134Smrg}
6817a84e134Smrg
6827a84e134Smrgstatic void
6837a84e134SmrgXawLabelResize(Widget w)
6847a84e134Smrg{
6857a84e134Smrg    LabelWidget lw = (LabelWidget)w;
6867a84e134Smrg    Position dx, dy;
6877a84e134Smrg
6887a84e134Smrg    _Reposition(lw, XtWidth(w), XtHeight(w), &dx, &dy);
6897a84e134Smrg    compute_bitmap_offsets(lw);
6907a84e134Smrg}
6917a84e134Smrg
6927a84e134Smrg#define PIXMAP 0
6937a84e134Smrg#define WIDTH 1
6947a84e134Smrg#define HEIGHT 2
6957a84e134Smrg#define NUM_CHECKS 3
6967a84e134Smrgstatic Boolean
6977a84e134SmrgXawLabelSetValues(Widget current, Widget request, Widget cnew,
6987a84e134Smrg		  ArgList args, Cardinal *num_args)
6997a84e134Smrg{
7007a84e134Smrg    LabelWidget curlw = (LabelWidget)current;
7017a84e134Smrg    LabelWidget reqlw = (LabelWidget)request;
7027a84e134Smrg    LabelWidget newlw = (LabelWidget)cnew;
7037a84e134Smrg    unsigned int i;
7047a84e134Smrg    Boolean was_resized = False, redisplay = False, checks[NUM_CHECKS];
7057a84e134Smrg
7067a84e134Smrg    for (i = 0; i < NUM_CHECKS; i++)
7077a84e134Smrg	checks[i] = False;
7087a84e134Smrg
7097a84e134Smrg    for (i = 0; i < *num_args; i++) {
7107a84e134Smrg	if (streq(XtNbitmap, args[i].name))
7117a84e134Smrg	    checks[PIXMAP] = True;
7127a84e134Smrg	else if (streq(XtNwidth, args[i].name))
7137a84e134Smrg	    checks[WIDTH] = True;
7147a84e134Smrg	else if (streq(XtNheight, args[i].name))
7157a84e134Smrg	    checks[HEIGHT] = True;
7167a84e134Smrg    }
7177a84e134Smrg
7187a84e134Smrg    if (newlw->label.label == NULL)
7197a84e134Smrg	newlw->label.label = newlw->core.name;
7207a84e134Smrg
7217a84e134Smrg    /*
7227a84e134Smrg     * resize on bitmap change
7237a84e134Smrg     */
7247a84e134Smrg    if (curlw->label.left_bitmap != newlw->label.left_bitmap)
7257a84e134Smrg	was_resized = True;
7267a84e134Smrg
7277a84e134Smrg    if (curlw->label.encoding != newlw->label.encoding)
7287a84e134Smrg	was_resized = True;
7297a84e134Smrg
7307a84e134Smrg    if (curlw->simple.international
7317a84e134Smrg	&& curlw->label.fontset != newlw->label.fontset)
7327a84e134Smrg	was_resized = True;
7337a84e134Smrg
7347a84e134Smrg    if (curlw->label.label != newlw->label.label) {
7357a84e134Smrg	if (curlw->label.label != curlw->core.name)
7367a84e134Smrg	    XtFree((char *)curlw->label.label);
7377a84e134Smrg
7387a84e134Smrg	if (newlw->label.label != newlw->core.name)
7397a84e134Smrg	    newlw->label.label = XtNewString(newlw->label.label);
7407a84e134Smrg
7417a84e134Smrg	was_resized = True;
7427a84e134Smrg    }
7437a84e134Smrg
7447a84e134Smrg    if (was_resized || (curlw->label.font != newlw->label.font) ||
7457a84e134Smrg        curlw->label.justify != newlw->label.justify || checks[PIXMAP]) {
7467a84e134Smrg	SetTextWidthAndHeight(newlw);
7477a84e134Smrg	was_resized = True;
7487a84e134Smrg    }
7497a84e134Smrg
7507a84e134Smrg    /* recalculate the window size if something has changed */
7517a84e134Smrg    if (newlw->label.resize && was_resized) {
7527a84e134Smrg	if (XtHeight(curlw) == XtHeight(reqlw) && !checks[HEIGHT])
7537a84e134Smrg	    XtHeight(newlw) = newlw->label.label_height +
7547a84e134Smrg			      (newlw->label.internal_height << 1);
7557a84e134Smrg
7567a84e134Smrg	set_bitmap_info(newlw);
7577a84e134Smrg
7587a84e134Smrg	if (XtWidth(curlw) == XtWidth(reqlw) && !checks[WIDTH])
7597a84e134Smrg	    XtWidth(newlw) = newlw->label.label_width + LEFT_OFFSET(newlw) +
7607a84e134Smrg			     (newlw->label.internal_width << 1);
7617a84e134Smrg    }
7627a84e134Smrg
7637a84e134Smrg    if (curlw->label.foreground		!= newlw->label.foreground
7647a84e134Smrg	|| curlw->core.background_pixel != newlw->core.background_pixel
7657a84e134Smrg	|| curlw->label.font->fid	!= newlw->label.font->fid) {
7667a84e134Smrg	/* The Fontset is not in the GC - don't make a new GC if FS changes! */
7677a84e134Smrg	XtReleaseGC(cnew, curlw->label.normal_GC);
7687a84e134Smrg	XtReleaseGC(cnew, curlw->label.gray_GC);
7697a84e134Smrg	XmuReleaseStippledPixmap(XtScreen(current), curlw->label.stipple);
7707a84e134Smrg	GetNormalGC(newlw);
7717a84e134Smrg	GetGrayGC(newlw);
7727a84e134Smrg	redisplay = True;
7737a84e134Smrg    }
7747a84e134Smrg
7757a84e134Smrg    if (curlw->label.label_x != newlw->label.label_x ||
7767a84e134Smrg        curlw->label.label_y != newlw->label.label_y)
7777a84e134Smrg	redisplay = True;
7787a84e134Smrg
7797a84e134Smrg    if (curlw->label.internal_width != newlw->label.internal_width
7807a84e134Smrg	|| curlw->label.internal_height != newlw->label.internal_height
7817a84e134Smrg	|| was_resized) {
7827a84e134Smrg	/* Resize() will be called if geometry changes succeed */
7837a84e134Smrg	Position dx, dy;
7847a84e134Smrg
7857a84e134Smrg	_Reposition(newlw, XtWidth(curlw), XtHeight(curlw), &dx, &dy);
7867a84e134Smrg    }
7877a84e134Smrg
7887a84e134Smrg      return (was_resized || redisplay ||
7897a84e134Smrg	      XtIsSensitive(current) != XtIsSensitive(cnew));
7907a84e134Smrg}
7917a84e134Smrg
7927a84e134Smrgstatic void
7937a84e134SmrgXawLabelDestroy(Widget w)
7947a84e134Smrg{
7957a84e134Smrg    LabelWidget lw = (LabelWidget)w;
7967a84e134Smrg
7977a84e134Smrg    if (lw->label.label != lw->core.name)
7987a84e134Smrg	XtFree(lw->label.label);
7997a84e134Smrg    XtReleaseGC(w, lw->label.normal_GC);
8007a84e134Smrg    XtReleaseGC(w, lw->label.gray_GC);
8017a84e134Smrg    XmuReleaseStippledPixmap(XtScreen(w), lw->label.stipple);
8027a84e134Smrg}
8037a84e134Smrg
8047a84e134Smrgstatic XtGeometryResult
8057a84e134SmrgXawLabelQueryGeometry(Widget w, XtWidgetGeometry *intended,
8067a84e134Smrg		      XtWidgetGeometry *preferred)
8077a84e134Smrg{
8087a84e134Smrg    LabelWidget lw = (LabelWidget)w;
8097a84e134Smrg
8107a84e134Smrg    preferred->request_mode = CWWidth | CWHeight;
8117a84e134Smrg    preferred->width = lw->label.label_width +
8127a84e134Smrg		       (lw->label.internal_width << 1) + LEFT_OFFSET(lw);
8137a84e134Smrg    preferred->height = lw->label.label_height +
8147a84e134Smrg			(lw->label.internal_height << 1);
8157a84e134Smrg
8167a84e134Smrg    if (((intended->request_mode & (CWWidth | CWHeight)) == (CWWidth | CWHeight))
8177a84e134Smrg	&& intended->width == preferred->width
8187a84e134Smrg	&& intended->height == preferred->height)
8197a84e134Smrg	return (XtGeometryYes);
8207a84e134Smrg      else if (preferred->width == XtWidth(w) && preferred->height == XtHeight(w))
8217a84e134Smrg	return (XtGeometryNo);
8227a84e134Smrg
8237a84e134Smrg    return (XtGeometryAlmost);
8247a84e134Smrg}
825