/* Generated by wbuild from "Mlabel.w"
** (generator version $Revision: 3.0 $ of $Date: 95/08/30 17:50:38 $)
*/
#include <X11/IntrinsicP.h>
#include <X11/StringDefs.h>
#line 369 "Mlabel.w"
#include <stdio.h>
#include <./MlabelP.h>
static void _resolve_inheritance(
#if NeedFunctionPrototypes
WidgetClass
#endif
);
#line 149 "Mlabel.w"
static Boolean  set_values(
#if NeedFunctionPrototypes
Widget ,Widget ,Widget,ArgList ,Cardinal *
#endif
);
#line 202 "Mlabel.w"
static void initialize(
#if NeedFunctionPrototypes
Widget ,Widget,ArgList ,Cardinal *
#endif
);
#line 264 "Mlabel.w"
static void expose(
#if NeedFunctionPrototypes
Widget,XEvent *,Region 
#endif
);
#line 313 "Mlabel.w"
static void make_gc(
#if NeedFunctionPrototypes
Widget
#endif
);
#line 343 "Mlabel.w"
static void count_lines(
#if NeedFunctionPrototypes
Widget
#endif
);
#line 313 "Mlabel.w"
/*ARGSUSED*/
#if NeedFunctionPrototypes
#line 313 "Mlabel.w"
static void make_gc(Widget self)
#else
#line 313 "Mlabel.w"
static void make_gc(self)Widget self;
#endif
#line 314 "Mlabel.w"
{
    XtGCMask mask;
    XGCValues values;

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

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

    if (((MlabelWidget)self)->mlabel.hl_gc != NULL) XtReleaseGC(self, ((MlabelWidget)self)->mlabel.hl_gc);
    values.background = ((MlabelWidget)self)->core.background_pixel;
    values.foreground = ((MlabelWidget)self)->mlabel.hlForeground;
    values.font = ((MlabelWidget)self)->mlabel.font->fid;
    values.function = GXcopy;
    ((MlabelWidget)self)->mlabel.hl_gc = XtGetGC(self, mask, &values);
}
#line 343 "Mlabel.w"
/*ARGSUSED*/
#if NeedFunctionPrototypes
#line 343 "Mlabel.w"
static void count_lines(Widget self)
#else
#line 343 "Mlabel.w"
static void count_lines(self)Widget self;
#endif
#line 344 "Mlabel.w"
{
    String p, s;
    int w;

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

static XtResource resources[] = {
#line 25 "Mlabel.w"
{XtNlabel,XtCLabel,XtRString,sizeof(((MlabelRec*)NULL)->mlabel.label),XtOffsetOf(MlabelRec,mlabel.label),XtRImmediate,(XtPointer)NULL },
#line 29 "Mlabel.w"
{XtNfont,XtCFont,XtRFontStruct,sizeof(((MlabelRec*)NULL)->mlabel.font),XtOffsetOf(MlabelRec,mlabel.font),XtRString,(XtPointer)XtDefaultFont },
#line 34 "Mlabel.w"
{XtNforeground,XtCForeground,XtRPixel,sizeof(((MlabelRec*)NULL)->mlabel.foreground),XtOffsetOf(MlabelRec,mlabel.foreground),XtRString,(XtPointer)XtDefaultForeground },
#line 35 "Mlabel.w"
{XtNhlForeground,XtCHlForeground,XtRPixel,sizeof(((MlabelRec*)NULL)->mlabel.hlForeground),XtOffsetOf(MlabelRec,mlabel.hlForeground),XtRString,(XtPointer)XtDefaultForeground },
#line 47 "Mlabel.w"
{XtNalignment,XtCAlignment,XtRAlignment,sizeof(((MlabelRec*)NULL)->mlabel.alignment),XtOffsetOf(MlabelRec,mlabel.alignment),XtRImmediate,(XtPointer)0 },
#line 52 "Mlabel.w"
{XtNtopMargin,XtCTopMargin,XtRDimension,sizeof(((MlabelRec*)NULL)->mlabel.topMargin),XtOffsetOf(MlabelRec,mlabel.topMargin),XtRImmediate,(XtPointer)2 },
#line 57 "Mlabel.w"
{XtNbottomMargin,XtCBottomMargin,XtRDimension,sizeof(((MlabelRec*)NULL)->mlabel.bottomMargin),XtOffsetOf(MlabelRec,mlabel.bottomMargin),XtRImmediate,(XtPointer)2 },
#line 64 "Mlabel.w"
{XtNleftMargin,XtCLeftMargin,XtRDimension,sizeof(((MlabelRec*)NULL)->mlabel.leftMargin),XtOffsetOf(MlabelRec,mlabel.leftMargin),XtRImmediate,(XtPointer)2 },
#line 69 "Mlabel.w"
{XtNrightMargin,XtCRightMargin,XtRDimension,sizeof(((MlabelRec*)NULL)->mlabel.rightMargin),XtOffsetOf(MlabelRec,mlabel.rightMargin),XtRImmediate,(XtPointer)2 },
#line 76 "Mlabel.w"
{XtNshrinkToFit,XtCShrinkToFit,XtRBoolean,sizeof(((MlabelRec*)NULL)->mlabel.shrinkToFit),XtOffsetOf(MlabelRec,mlabel.shrinkToFit),XtRImmediate,(XtPointer)False },
#line 82 "Mlabel.w"
{XtNrvStart,XtCRvStart,XtRInt,sizeof(((MlabelRec*)NULL)->mlabel.rvStart),XtOffsetOf(MlabelRec,mlabel.rvStart),XtRImmediate,(XtPointer)0 },
#line 87 "Mlabel.w"
{XtNrvLength,XtCRvLength,XtRInt,sizeof(((MlabelRec*)NULL)->mlabel.rvLength),XtOffsetOf(MlabelRec,mlabel.rvLength),XtRImmediate,(XtPointer)0 },
#line 92 "Mlabel.w"
{XtNhlStart,XtCHlStart,XtRInt,sizeof(((MlabelRec*)NULL)->mlabel.hlStart),XtOffsetOf(MlabelRec,mlabel.hlStart),XtRImmediate,(XtPointer)0 },
#line 93 "Mlabel.w"
{XtNhlLength,XtCHlLength,XtRInt,sizeof(((MlabelRec*)NULL)->mlabel.hlLength),XtOffsetOf(MlabelRec,mlabel.hlLength),XtRImmediate,(XtPointer)0 },
};

MlabelClassRec mlabelClassRec = {
{ /* core_class part */
/* superclass   	*/  (WidgetClass) &compositeClassRec,
/* class_name   	*/  "Mlabel",
/* widget_size  	*/  sizeof(MlabelRec),
/* class_initialize 	*/  NULL,
/* class_part_initialize*/  _resolve_inheritance,
/* class_inited 	*/  FALSE,
/* initialize   	*/  initialize,
/* initialize_hook 	*/  NULL,
/* realize      	*/  XtInheritRealize,
/* actions      	*/  NULL,
/* num_actions  	*/  0,
/* resources    	*/  resources,
/* num_resources 	*/  14,
/* xrm_class    	*/  NULLQUARK,
/* compres_motion 	*/  False ,
/* compress_exposure 	*/  FALSE ,
/* compress_enterleave 	*/  False ,
/* visible_interest 	*/  False ,
/* destroy      	*/  NULL,
/* resize       	*/  XtInheritResize,
/* expose       	*/  expose,
/* set_values   	*/  set_values,
/* set_values_hook 	*/  NULL,
/* set_values_almost 	*/  XtInheritSetValuesAlmost,
/* get_values+hook 	*/  NULL,
/* accept_focus 	*/  XtInheritAcceptFocus,
/* version      	*/  XtVersion,
/* callback_private 	*/  NULL,
/* tm_table      	*/  NULL,
/* query_geometry 	*/  XtInheritQueryGeometry,
/* display_acceleator 	*/  XtInheritDisplayAccelerator,
/* extension    	*/  NULL 
},
{ /* composite_class part */
XtInheritGeometryManager,
XtInheritChangeManaged,
XtInheritInsertChild,
XtInheritDeleteChild,
NULL
},
{ /* Mlabel_class part */
 /* dummy */  0
},
};
WidgetClass mlabelWidgetClass = (WidgetClass) &mlabelClassRec;
static void _resolve_inheritance(class)
WidgetClass class;
{
  MlabelWidgetClass c = (MlabelWidgetClass) class;
  MlabelWidgetClass 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 == mlabelWidgetClass) return;
  super = (MlabelWidgetClass)class->core_class.superclass;
}
#line 149 "Mlabel.w"
/*ARGSUSED*/
#if NeedFunctionPrototypes
#line 149 "Mlabel.w"
static Boolean  set_values(Widget  old,Widget  request,Widget self,ArgList  args,Cardinal * num_args)
#else
#line 149 "Mlabel.w"
static Boolean  set_values(old,request,self,args,num_args)Widget  old;Widget  request;Widget self;ArgList  args;Cardinal * num_args;
#endif
#line 150 "Mlabel.w"
{
    Boolean need_redisplay = False, need_count = False;

    if (((MlabelWidget)self)->mlabel.font != ((MlabelWidget)old)->mlabel.font) {
		make_gc(self);
		if (((MlabelWidget)self)->mlabel.label != NULL) need_count = True;
	    }

    if (((MlabelWidget)self)->mlabel.foreground != ((MlabelWidget)old)->mlabel.foreground
	 	|| ((MlabelWidget)self)->mlabel.hlForeground != ((MlabelWidget)self)->mlabel.hlForeground
		|| ((MlabelWidget)self)->core.background_pixel != ((MlabelWidget)old)->core.background_pixel) {
		make_gc(self);
		if (((MlabelWidget)self)->mlabel.label != NULL) need_redisplay = True;
	    }

    if (((MlabelWidget)self)->mlabel.topMargin != ((MlabelWidget)old)->mlabel.topMargin
		|| ((MlabelWidget)self)->mlabel.bottomMargin != ((MlabelWidget)old)->mlabel.bottomMargin
		|| ((MlabelWidget)self)->mlabel.leftMargin != ((MlabelWidget)old)->mlabel.leftMargin
		|| ((MlabelWidget)self)->mlabel.rightMargin != ((MlabelWidget)old)->mlabel.rightMargin)
		need_count = True;

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

    if (((MlabelWidget)self)->mlabel.rvStart != ((MlabelWidget)old)->mlabel.rvStart || ((MlabelWidget)self)->mlabel.rvLength != ((MlabelWidget)old)->mlabel.rvLength
	 	|| ((MlabelWidget)self)->mlabel.hlStart != ((MlabelWidget)old)->mlabel.hlStart || ((MlabelWidget)self)->mlabel.hlLength != ((MlabelWidget)old)->mlabel.hlLength)
			if (((MlabelWidget)self)->mlabel.label != NULL) need_redisplay = True;

    if (((MlabelWidget)self)->mlabel.label != ((MlabelWidget)old)->mlabel.label) {
		XtFree(((MlabelWidget)old)->mlabel.label);
		((MlabelWidget)self)->mlabel.label = XtNewString(((MlabelWidget)self)->mlabel.label);
		need_count = True;
    	}

    if (need_count) {
		count_lines(self);
		need_redisplay = True;
	    }

    if (need_count && ((MlabelWidget)self)->mlabel.shrinkToFit) {
		((MlabelWidget)self)->core.width = ((MlabelWidget)self)->mlabel.label_width;
		((MlabelWidget)self)->core.height = ((MlabelWidget)self)->mlabel.label_height;
	    need_redisplay = True;
		}

    return need_redisplay;
}
#line 202 "Mlabel.w"
/*ARGSUSED*/
#if NeedFunctionPrototypes
#line 202 "Mlabel.w"
static void initialize(Widget  request,Widget self,ArgList  args,Cardinal * num_args)
#else
#line 202 "Mlabel.w"
static void initialize(request,self,args,num_args)Widget  request;Widget self;ArgList  args;Cardinal * num_args;
#endif
#line 203 "Mlabel.w"
{
    if (((MlabelWidget)self)->mlabel.label) ((MlabelWidget)self)->mlabel.label = XtNewString(((MlabelWidget)self)->mlabel.label);
    count_lines(self);
    ((MlabelWidget)self)->mlabel.gc = NULL;
    ((MlabelWidget)self)->mlabel.rv_gc = NULL;
    ((MlabelWidget)self)->mlabel.hl_gc = NULL;
    make_gc(self);
    if (((MlabelWidget)self)->mlabel.shrinkToFit) {
		((MlabelWidget)self)->core.width = ((MlabelWidget)self)->mlabel.label_width;
		((MlabelWidget)self)->core.height = ((MlabelWidget)self)->mlabel.label_height;
	    }
}
#define max(a, b) ((a )>(b )?(a ):(b ))


#define min(a, b) ((a )<(b )?(a ):(b ))


#define draw_line(dpy, win, from, to) do {\
	if (((MlabelWidget)self)->mlabel.hlStart >= to) hstart = to;\
 	else hstart = max(((MlabelWidget)self)->mlabel.hlStart, from);\
	if (((MlabelWidget)self)->mlabel.hlStart + ((MlabelWidget)self)->mlabel.hlLength <= from) hend = hstart;\
 	else hend = min(((MlabelWidget)self)->mlabel.hlStart + ((MlabelWidget)self)->mlabel.hlLength, to);\
        if (((MlabelWidget)self)->mlabel.rvStart >= to) rstart = to;\
	else rstart = max(((MlabelWidget)self)->mlabel.rvStart, from);\
	if (((MlabelWidget)self)->mlabel.rvStart + ((MlabelWidget)self)->mlabel.rvLength <= from) rend = rstart;\
	else rend = min(((MlabelWidget)self)->mlabel.rvStart + ((MlabelWidget)self)->mlabel.rvLength, to);\
	w1 = XTextWidth(((MlabelWidget)self)->mlabel.font, ((MlabelWidget)self)->mlabel.label + from, rstart - from);\
	w2 = XTextWidth(((MlabelWidget)self)->mlabel.font, ((MlabelWidget)self)->mlabel.label + rstart, rend - rstart);\
	w3 = XTextWidth(((MlabelWidget)self)->mlabel.font, ((MlabelWidget)self)->mlabel.label + rend, to - rend);\
 	w4 = XTextWidth(((MlabelWidget)self)->mlabel.font, ((MlabelWidget)self)->mlabel.label + hstart, hend - hstart);\
 	w5 = XTextWidth(((MlabelWidget)self)->mlabel.font, ((MlabelWidget)self)->mlabel.label + from, hstart - from);\
	if (((MlabelWidget)self)->mlabel.alignment & XfwfLeft)\
	    x = rect.x;\
	else if (((MlabelWidget)self)->mlabel.alignment & XfwfRight)\
	    x = rect.x + rect.width - w1 - w2 - w3;\
	else\
	    x = rect.x + (rect.width - w1 - w2 - w3)/2;\
	if (w1)\
	    XDrawString(dpy, win, ((MlabelWidget)self)->mlabel.gc, x, y, ((MlabelWidget)self)->mlabel.label + from,\
			     rstart - from);\
	if (w2)\
	    XDrawImageString(dpy, win, ((MlabelWidget)self)->mlabel.rv_gc, x + w1, y, ((MlabelWidget)self)->mlabel.label\
			     + rstart, rend - rstart);\
	if (w3)\
	    XDrawString(dpy, win, ((MlabelWidget)self)->mlabel.gc, x + w1 + w2, y, ((MlabelWidget)self)->mlabel.label +\
			     rend, to - rend);\
 	if (w4)\
 	    XDrawString(dpy, win, ((MlabelWidget)self)->mlabel.hl_gc, x + w5, y, ((MlabelWidget)self)->mlabel.label\
 			     + hstart, hend - hstart);\
    }while (0 )


#line 264 "Mlabel.w"
/*ARGSUSED*/
#if NeedFunctionPrototypes
#line 264 "Mlabel.w"
static void expose(Widget self,XEvent * event,Region  region)
#else
#line 264 "Mlabel.w"
static void expose(self,event,region)Widget self;XEvent * event;Region  region;
#endif
#line 265 "Mlabel.w"
{
    Region reg;
    XRectangle rect;
    int baseline;
    int w1, w2, w3, w4, w5;
    int x, y, i, j, rstart, rend, hstart, hend;

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

	XSetClipMask(XtDisplay(self), ((MlabelWidget)self)->mlabel.gc, None);
	XSetClipMask(XtDisplay(self), ((MlabelWidget)self)->mlabel.rv_gc, None);
 	XSetClipMask(XtDisplay(self), ((MlabelWidget)self)->mlabel.hl_gc, None);
    }
}
#line 127 "Mlabel.w"
#line 132 "Mlabel.w"
#line 133 "Mlabel.w"
#line 134 "Mlabel.w"
#line 135 "Mlabel.w"
#line 136 "Mlabel.w"
