/* Generated by wbuild from "Label.w"
** (generator version $Revision: 2.0 $ of $Date: 93/07/06 16:08:04 $)
*/
#include <X11/IntrinsicP.h>
#include <X11/StringDefs.h>
#include "stip4.bm"
#include <stdio.h>
#include "TabString.h"
#include "LabelP.h"
static void _resolve_inheritance(
#if NeedFunctionPrototypes
WidgetClass
#endif
);
static void set_label(
#if NeedFunctionPrototypes
Widget,String 
#endif
);
static Boolean  set_values(
#if NeedFunctionPrototypes
Widget ,Widget ,Widget,ArgList ,Cardinal *
#endif
);
static void initialize(
#if NeedFunctionPrototypes
Widget ,Widget,ArgList ,Cardinal *
#endif
);
static void expose(
#if NeedFunctionPrototypes
Widget,XEvent *,Region 
#endif
);
static void make_gc(
#if NeedFunctionPrototypes
Widget
#endif
);
static void make_graygc(
#if NeedFunctionPrototypes
Widget
#endif
);
static void count_lines(
#if NeedFunctionPrototypes
Widget
#endif
);
/*ARGSUSED*/static void make_gc(self)Widget self;
{
    XtGCMask mask;
    XGCValues values;

    if (((XfwfLabelWidget)self)->xfwfLabel.gc != NULL) XtReleaseGC(self, ((XfwfLabelWidget)self)->xfwfLabel.gc);
    values.background = ((XfwfLabelWidget)self)->core.background_pixel;
    values.foreground = ((XfwfLabelWidget)self)->xfwfLabel.foreground;
    values.font = ((XfwfLabelWidget)self)->xfwfLabel.font->fid;
    mask = GCFont | GCBackground | GCForeground;
    ((XfwfLabelWidget)self)->xfwfLabel.gc = XtGetGC(self, mask, &values);

    if (((XfwfLabelWidget)self)->xfwfLabel.rv_gc != NULL) XtReleaseGC(self, ((XfwfLabelWidget)self)->xfwfLabel.rv_gc);
    values.foreground = ((XfwfLabelWidget)self)->core.background_pixel;
    values.background = ((XfwfLabelWidget)self)->xfwfLabel.foreground;
    values.font = ((XfwfLabelWidget)self)->xfwfLabel.font->fid;
    mask = GCFont | GCBackground | GCForeground;
    ((XfwfLabelWidget)self)->xfwfLabel.rv_gc = XtGetGC(self, mask, &values);
}
/*ARGSUSED*/static void make_graygc(self)Widget self;
{
    XtGCMask mask;
    XGCValues values;

    if (((XfwfLabelWidget)self)->xfwfLabel.graygc != NULL) XtReleaseGC(self, ((XfwfLabelWidget)self)->xfwfLabel.graygc);
    values.foreground = ((XfwfLabelWidget)self)->core.background_pixel;
    values.stipple =
	XCreateBitmapFromData(XtDisplay(self),
			      RootWindowOfScreen(XtScreen(self)),
			      stip4_bits, stip4_width, stip4_height);
    values.fill_style = FillStippled;
    mask = GCForeground | GCStipple | GCFillStyle;
    ((XfwfLabelWidget)self)->xfwfLabel.graygc = XtGetGC(self, mask, &values);
}
/*ARGSUSED*/static void count_lines(self)Widget self;
{
    String p, s;
    int w;

    ((XfwfLabelWidget)self)->xfwfLabel.nlines = 0;
    ((XfwfLabelWidget)self)->xfwfLabel.label_width = 0;
    if (((XfwfLabelWidget)self)->xfwfLabel.label) {
	for (p = ((XfwfLabelWidget)self)->xfwfLabel.label, ((XfwfLabelWidget)self)->xfwfLabel.nlines = 1, s = ((XfwfLabelWidget)self)->xfwfLabel.label; *s; s++) {
	    if (*s == '\n') {
		((XfwfLabelWidget)self)->xfwfLabel.nlines++;
		w = XfwfTextWidth(((XfwfLabelWidget)self)->xfwfLabel.font, p, s - p, ((XfwfLabelWidget)self)->xfwfLabel.tabs);
		p = s + 1;
		if (w > ((XfwfLabelWidget)self)->xfwfLabel.label_width) ((XfwfLabelWidget)self)->xfwfLabel.label_width = w;
	    }
	}
	w = XfwfTextWidth(((XfwfLabelWidget)self)->xfwfLabel.font, p, s - p, ((XfwfLabelWidget)self)->xfwfLabel.tabs);
	if (w > ((XfwfLabelWidget)self)->xfwfLabel.label_width) ((XfwfLabelWidget)self)->xfwfLabel.label_width = w;
    }
    ((XfwfLabelWidget)self)->xfwfLabel.label_height = ((XfwfLabelWidget)self)->xfwfLabel.nlines * (((XfwfLabelWidget)self)->xfwfLabel.font->ascent + ((XfwfLabelWidget)self)->xfwfLabel.font->descent);
    ((XfwfLabelWidget)self)->xfwfLabel.label_width += ((XfwfLabelWidget)self)->xfwfLabel.leftMargin + ((XfwfLabelWidget)self)->xfwfLabel.rightMargin;
    ((XfwfLabelWidget)self)->xfwfLabel.label_height += ((XfwfLabelWidget)self)->xfwfLabel.topMargin + ((XfwfLabelWidget)self)->xfwfLabel.bottomMargin;
}

static XtResource resources[] = {
{XtNlabel,XtCLabel,XtRString,sizeof(((XfwfLabelRec*)NULL)->xfwfLabel.label),XtOffsetOf(XfwfLabelRec,xfwfLabel.label),XtRImmediate,(XtPointer)NULL },
{XtNtablist,XtCTablist,XtRString,sizeof(((XfwfLabelRec*)NULL)->xfwfLabel.tablist),XtOffsetOf(XfwfLabelRec,xfwfLabel.tablist),XtRImmediate,(XtPointer)NULL },
{XtNfont,XtCFont,XtRFontStruct,sizeof(((XfwfLabelRec*)NULL)->xfwfLabel.font),XtOffsetOf(XfwfLabelRec,xfwfLabel.font),XtRString,(XtPointer)XtDefaultFont },
{XtNforeground,XtCForeground,XtRPixel,sizeof(((XfwfLabelRec*)NULL)->xfwfLabel.foreground),XtOffsetOf(XfwfLabelRec,xfwfLabel.foreground),XtRString,(XtPointer)XtDefaultForeground },
{XtNalignment,XtCAlignment,XtRAlignment,sizeof(((XfwfLabelRec*)NULL)->xfwfLabel.alignment),XtOffsetOf(XfwfLabelRec,xfwfLabel.alignment),XtRImmediate,(XtPointer)0 },
{XtNtopMargin,XtCTopMargin,XtRDimension,sizeof(((XfwfLabelRec*)NULL)->xfwfLabel.topMargin),XtOffsetOf(XfwfLabelRec,xfwfLabel.topMargin),XtRImmediate,(XtPointer)2 },
{XtNbottomMargin,XtCBottomMargin,XtRDimension,sizeof(((XfwfLabelRec*)NULL)->xfwfLabel.bottomMargin),XtOffsetOf(XfwfLabelRec,xfwfLabel.bottomMargin),XtRImmediate,(XtPointer)2 },
{XtNleftMargin,XtCLeftMargin,XtRDimension,sizeof(((XfwfLabelRec*)NULL)->xfwfLabel.leftMargin),XtOffsetOf(XfwfLabelRec,xfwfLabel.leftMargin),XtRImmediate,(XtPointer)2 },
{XtNrightMargin,XtCRightMargin,XtRDimension,sizeof(((XfwfLabelRec*)NULL)->xfwfLabel.rightMargin),XtOffsetOf(XfwfLabelRec,xfwfLabel.rightMargin),XtRImmediate,(XtPointer)2 },
{XtNshrinkToFit,XtCShrinkToFit,XtRBoolean,sizeof(((XfwfLabelRec*)NULL)->xfwfLabel.shrinkToFit),XtOffsetOf(XfwfLabelRec,xfwfLabel.shrinkToFit),XtRImmediate,(XtPointer)False },
{XtNrvStart,XtCRvStart,XtRInt,sizeof(((XfwfLabelRec*)NULL)->xfwfLabel.rvStart),XtOffsetOf(XfwfLabelRec,xfwfLabel.rvStart),XtRImmediate,(XtPointer)0 },
{XtNrvLength,XtCRvLength,XtRInt,sizeof(((XfwfLabelRec*)NULL)->xfwfLabel.rvLength),XtOffsetOf(XfwfLabelRec,xfwfLabel.rvLength),XtRImmediate,(XtPointer)0 },
{XtNtraversalOn,XtCTraversalOn,XtRBoolean,sizeof(((XfwfLabelRec*)NULL)->xfwfCommon.traversalOn),XtOffsetOf(XfwfLabelRec,xfwfCommon.traversalOn),XtRImmediate,(XtPointer)False },
};

XfwfLabelClassRec xfwfLabelClassRec = {
{ /* core_class part */
(WidgetClass) &xfwfBoardClassRec,
"TextBox",
sizeof(XfwfLabelRec),
NULL,
_resolve_inheritance,
FALSE,
initialize,
NULL,
XtInheritRealize,
NULL,
0,
resources,
13,
NULLQUARK,
False ,
FALSE ,
False ,
False ,
NULL,
XtInheritResize,
expose,
set_values,
NULL,
XtInheritSetValuesAlmost,
NULL,
XtInheritAcceptFocus,
XtVersion,
NULL,
NULL,
XtInheritQueryGeometry,
XtInheritDisplayAccelerator,
NULL 
},
{ /* composite_class part */
XtInheritGeometryManager,
XtInheritChangeManaged,
XtInheritInsertChild,
XtInheritDeleteChild,
NULL
},
{ /* XfwfCommon_class part */
XtInherit_compute_inside,
XtInherit_highlight_border,
XtInherit_unhighlight_border,
XtInherit_would_accept_focus,
XtInherit_traverse,
XtInherit_choose_color,
XtInherit_lighter_color,
XtInherit_darker_color,
NULL ,
},
{ /* XfwfFrame_class part */
0
},
{ /* XfwfBoard_class part */
XtInherit_set_abs_location,
},
{ /* XfwfLabel_class part */
set_label,
},
};
WidgetClass xfwfLabelWidgetClass = (WidgetClass) &xfwfLabelClassRec;
static void _resolve_inheritance(class)
WidgetClass class;
{
  XfwfLabelWidgetClass c = (XfwfLabelWidgetClass) class;
  XfwfLabelWidgetClass super;
  static CompositeClassExtensionRec extension_rec = {
    NULL, NULLQUARK, XtCompositeExtensionVersion,
    sizeof(CompositeClassExtensionRec), True};
  CompositeClassExtensionRec *ext;
  ext = (XtPointer)XtMalloc(sizeof(*ext));
  *ext = extension_rec;
  ext->next_extension = c->composite_class.extension;
  c->composite_class.extension = ext;
  if (class == xfwfLabelWidgetClass) return;
  super = (XfwfLabelWidgetClass)class->core_class.superclass;
  if (c->xfwfLabel_class.set_label == XtInherit_set_label)
    c->xfwfLabel_class.set_label = super->xfwfLabel_class.set_label;
}
/*ARGSUSED*/static void set_label(self,newlabel)Widget self;String  newlabel;
{
    Position x, y;
    Dimension w, h;

    XtFree(((XfwfLabelWidget)self)->xfwfLabel.label);
    ((XfwfLabelWidget)self)->xfwfLabel.label = XtNewString(newlabel);
    count_lines(self);
    if (XtIsRealized(self)) {
	((XfwfLabelWidgetClass)self->core.widget_class)->xfwfCommon_class.compute_inside(self, &x, &y, &w, &h);
	XClearArea(XtDisplay(self), XtWindow(self), x, y, w, h, True);
	/* $expose($, NULL, NULL); */
    }
}
/*ARGSUSED*/static Boolean  set_values(old,request,self,args,num_args)Widget  old;Widget  request;Widget self;ArgList  args;Cardinal * num_args;
{
    Boolean need_redisplay = False, need_count = False;
    Position x, y;
    Dimension w, h, wd, ht;

    if (((XfwfLabelWidget)self)->core.background_pixel != ((XfwfLabelWidget)old)->core.background_pixel)
	make_graygc(self);

    if (((XfwfLabelWidget)self)->xfwfLabel.tablist != ((XfwfLabelWidget)old)->xfwfLabel.tablist) {
	XtFree((String) ((XfwfLabelWidget)old)->xfwfLabel.tabs);
	((XfwfLabelWidget)self)->xfwfLabel.tabs = XfwfTablist2Tabs(((XfwfLabelWidget)self)->xfwfLabel.tablist);
	if (((XfwfLabelWidget)self)->xfwfLabel.label != NULL) need_count = True;
    }

    if (((XfwfLabelWidget)self)->xfwfLabel.font != ((XfwfLabelWidget)old)->xfwfLabel.font) {
	make_gc(self);
	if (((XfwfLabelWidget)self)->xfwfLabel.label != NULL) need_count = True;
    }
    if (((XfwfLabelWidget)self)->xfwfLabel.foreground != ((XfwfLabelWidget)old)->xfwfLabel.foreground
	|| ((XfwfLabelWidget)self)->core.background_pixel != ((XfwfLabelWidget)old)->core.background_pixel) {
	make_gc(self);
	if (((XfwfLabelWidget)self)->xfwfLabel.label != NULL) need_redisplay = True;
    }
    if (((XfwfLabelWidget)self)->xfwfLabel.topMargin != ((XfwfLabelWidget)old)->xfwfLabel.topMargin
	|| ((XfwfLabelWidget)self)->xfwfLabel.bottomMargin != ((XfwfLabelWidget)old)->xfwfLabel.bottomMargin
	|| ((XfwfLabelWidget)self)->xfwfLabel.leftMargin != ((XfwfLabelWidget)old)->xfwfLabel.leftMargin
	|| ((XfwfLabelWidget)self)->xfwfLabel.rightMargin != ((XfwfLabelWidget)old)->xfwfLabel.rightMargin)
	need_count = True;

    if (((XfwfLabelWidget)self)->core.sensitive != ((XfwfLabelWidget)old)->core.sensitive)
	if (((XfwfLabelWidget)self)->xfwfLabel.label != NULL) need_redisplay = True;

    if (((XfwfLabelWidget)self)->xfwfLabel.rvStart != ((XfwfLabelWidget)old)->xfwfLabel.rvStart || ((XfwfLabelWidget)self)->xfwfLabel.rvLength != ((XfwfLabelWidget)old)->xfwfLabel.rvLength)
	if (((XfwfLabelWidget)self)->xfwfLabel.label != NULL) need_redisplay = True;

    if (((XfwfLabelWidget)self)->xfwfLabel.label != ((XfwfLabelWidget)old)->xfwfLabel.label) {
	XtFree(((XfwfLabelWidget)old)->xfwfLabel.label);
	((XfwfLabelWidget)self)->xfwfLabel.label = XtNewString(((XfwfLabelWidget)self)->xfwfLabel.label);
	need_count = True;
    }
    if (need_count) {
	count_lines(self);
	need_redisplay = True;
    }
    if (need_count && ((XfwfLabelWidget)self)->xfwfLabel.shrinkToFit) {
	((XfwfLabelWidgetClass)self->core.widget_class)->xfwfCommon_class.compute_inside(self, &x, &y, &w, &h);
	wd = ((XfwfLabelWidget)self)->xfwfLabel.label_width + ((XfwfLabelWidget)self)->core.width - w;
	ht = ((XfwfLabelWidget)self)->xfwfLabel.label_height + ((XfwfLabelWidget)self)->core.height - h;
	if (wd != ((XfwfLabelWidget)self)->core.width || ht != ((XfwfLabelWidget)self)->core.height) {
	    ((XfwfLabelWidgetClass)self->core.widget_class)->xfwfBoard_class.set_abs_location(self, CWWidth | CWHeight, 0, 0, wd, ht);
	    need_redisplay = False;
	}
    }
    return need_redisplay;
}
/*ARGSUSED*/static void initialize(request,self,args,num_args)Widget  request;Widget self;ArgList  args;Cardinal * num_args;
{
    char *s;
    Position x, y;
    Dimension w, h, wd, ht;

    if (((XfwfLabelWidget)self)->xfwfLabel.label) ((XfwfLabelWidget)self)->xfwfLabel.label = XtNewString(((XfwfLabelWidget)self)->xfwfLabel.label);
    count_lines(self);
    ((XfwfLabelWidget)self)->xfwfLabel.gc = NULL;
    ((XfwfLabelWidget)self)->xfwfLabel.rv_gc = NULL;
    ((XfwfLabelWidget)self)->xfwfLabel.graygc = NULL;
    make_gc(self);
    make_graygc(self);
    ((XfwfLabelWidget)self)->xfwfLabel.tabs = XfwfTablist2Tabs(((XfwfLabelWidget)self)->xfwfLabel.tablist);
    if (((XfwfLabelWidget)self)->xfwfLabel.shrinkToFit) {
	((XfwfLabelWidgetClass)self->core.widget_class)->xfwfCommon_class.compute_inside(self, &x, &y, &w, &h);
	wd = ((XfwfLabelWidget)self)->xfwfLabel.label_width + ((XfwfLabelWidget)self)->core.width - w;
	ht = ((XfwfLabelWidget)self)->xfwfLabel.label_height + ((XfwfLabelWidget)self)->core.height - h;
	((XfwfLabelWidgetClass)self->core.widget_class)->xfwfBoard_class.set_abs_location(self, CWWidth | CWHeight, 0, 0, wd, ht);
    }
}
/*ARGSUSED*/static void expose(self,event,region)Widget self;XEvent * event;Region  region;
{
    Region reg;
    XRectangle rect;
    int baseline;
    int w1, w2, w3;
    char *s, *t;
    int x, y, i, j, rstart, rend;

    if (! XtIsRealized(self)) return;
    xfwfBoardClassRec.core_class.expose(self, event, region);
    if (((XfwfLabelWidget)self)->xfwfLabel.label != NULL) {
	baseline = ((XfwfLabelWidget)self)->xfwfLabel.font->ascent + ((XfwfLabelWidget)self)->xfwfLabel.font->descent;
	((XfwfLabelWidgetClass)self->core.widget_class)->xfwfCommon_class.compute_inside(self, &rect.x, &rect.y, &rect.width, &rect.height);
	rect.x += ((XfwfLabelWidget)self)->xfwfLabel.leftMargin;  rect.width -= ((XfwfLabelWidget)self)->xfwfLabel.leftMargin + ((XfwfLabelWidget)self)->xfwfLabel.rightMargin;
	rect.y += ((XfwfLabelWidget)self)->xfwfLabel.topMargin;  rect.height -= ((XfwfLabelWidget)self)->xfwfLabel.topMargin + ((XfwfLabelWidget)self)->xfwfLabel.bottomMargin;
	reg = XCreateRegion();
	XUnionRectWithRegion(&rect, reg, reg);
	if (region != NULL) XIntersectRegion(region, reg, reg);
	XSetRegion(XtDisplay(self), ((XfwfLabelWidget)self)->xfwfLabel.gc, reg);
	XSetRegion(XtDisplay(self), ((XfwfLabelWidget)self)->xfwfLabel.rv_gc, reg);
	if (((XfwfLabelWidget)self)->xfwfLabel.alignment & XfwfTop)
	    y = rect.y + ((XfwfLabelWidget)self)->xfwfLabel.font->ascent;
	else if (((XfwfLabelWidget)self)->xfwfLabel.alignment & XfwfBottom)
	    y = rect.y + rect.height - ((XfwfLabelWidget)self)->xfwfLabel.nlines * baseline + ((XfwfLabelWidget)self)->xfwfLabel.font->ascent;
	else
	    y = rect.y + (rect.height - ((XfwfLabelWidget)self)->xfwfLabel.nlines * baseline)/2 + ((XfwfLabelWidget)self)->xfwfLabel.font->ascent;
	for (i = 0, j = 0; ((XfwfLabelWidget)self)->xfwfLabel.label[i]; i++) {
	    if (((XfwfLabelWidget)self)->xfwfLabel.label[i] == '\n') {
		draw_line(XtDisplay(self), XtWindow(self), j, i);
		j = i + 1;
		y += baseline;
	    }
	}
	draw_line(XtDisplay(self), XtWindow(self), j, i);

	/* Gray out if not sensitive */
	if (! ((XfwfLabelWidget)self)->core.sensitive) {
	    XSetRegion(XtDisplay(self), ((XfwfLabelWidget)self)->xfwfLabel.graygc, reg);
	    XFillRectangle(XtDisplay(self), XtWindow(self), ((XfwfLabelWidget)self)->xfwfLabel.graygc, rect.x,
			   rect.y, rect.width, rect.height);
	    XSetClipMask(XtDisplay(self), ((XfwfLabelWidget)self)->xfwfLabel.graygc, None);
	}
	XSetClipMask(XtDisplay(self), ((XfwfLabelWidget)self)->xfwfLabel.gc, None);
	XSetClipMask(XtDisplay(self), ((XfwfLabelWidget)self)->xfwfLabel.rv_gc, None);

	XDestroyRegion (reg);					/* MF038 */
    }
}
