/*
** Generated by X-Designer
*/

/*
** X-Designer-generated prelude.
** Do not edit lines before "End of X-Designer generated prelude"
** Lines beginning ** X-Designer Stub indicate a stub
** which will not be output on re-generation
*/

/*
**LIBS: -lXm -lXt -lX11
*/

#include <stdlib.h>
#include <X11/Xatom.h>
#include <X11/Intrinsic.h>
#include <X11/Shell.h>

#include <Xm/Xm.h>
#include <Xm/DrawingA.h>

#include "cliptext.h"



/* End of X-Designer generated prelude */


/* Defined in Motif 2.1 but not 1.2 */

#ifndef   XmUNSPECIFIED_PIXEL
#define   XmUNSPECIFIED_PIXEL    ((Pixel) (~0))
#endif /* XmUNSPECIFIED_PIXEL */

typedef struct Options_s
{
	char *option ;
} Options_t ;

static char *LookupResource(Widget app_shell, char *key)
{
	Options_t  options ;
	XtResource string_resource[1] ;
	 
	string_resource[0].resource_name   = key ;
        string_resource[0].resource_class  = XtCString ;
        string_resource[0].resource_type   = XtRString ;
        string_resource[0].resource_size   = sizeof (String) ;
        string_resource[0].resource_offset = 0 ;
        string_resource[0].default_type    = XtRString ;
        string_resource[0].default_addr    = "" ;

        XtGetApplicationResources(app_shell, &options, string_resource, 1, (ArgList) 0, 0) ;

        return options.option ;
}

static XFontStruct *the_font = (XFontStruct *) 0 ;
		
static GC create_gc(Widget app_shell, Widget drawable)
{
	static GC gc = (GC) 0 ;
	
	if (gc == (GC) 0) {
		XGCValues    values ;
		unsigned int values_mask = (unsigned int) 0 ;
		Pixel        bg          = (Pixel) 0 ;
		Pixel        fg          = (Pixel) 0 ;
		XrmValue     from ;
		XrmValue     to ;
		char        *font ;

		font = LookupResource(app_shell, "DrawingFont") ;

		from.addr = ((font && *font) ? font : "fixed") ;
		from.size = strlen(from.addr) + 1 ;
		to.addr   = (XtPointer) 0 ;

		XtConvertAndStore(app_shell, XmRString, &from, XmRFontStruct, &to) ;

		if (to.addr) {
			the_font = (*(XFontStruct **) to.addr) ;
		}

		if (the_font != (XFontStruct *) 0) {
			values.font  = the_font->fid ;
			values_mask |= GCFont ;
		}
		
		XtVaGetValues(drawable, XmNforeground, &fg, XmNbackground, &bg, NULL) ;

		if (fg != XmUNSPECIFIED_PIXEL) {
			values.foreground = fg ;
			values_mask      |= GCForeground ;
		}

		if (bg != XmUNSPECIFIED_PIXEL) {
			values.background = bg ;
			values_mask      |= GCBackground ;
		}

		values.fill_style = FillSolid ;
		values_mask      |= GCFillStyle ;

		gc = XCreateGC(XtDisplay(drawable), XtWindow(drawable), values_mask, &values) ;
	}

	return gc ;
}

/*
** X-Designer Stub OnExpose
*/

static void draw_clipped_string(Widget w, GC gc, char *string, XFontStruct *font, int x, int y, int clip_size)
{
	XRectangle  rectangle ;
	int         ascent ;
	int         descent ;
	int         direction ;
	XCharStruct overall ;

	XTextExtents(font, string, strlen(string), &direction, &ascent, &descent, &overall) ;

	rectangle.x      = x ;
	rectangle.y      = y - overall.ascent + clip_size ;
	rectangle.width  = (overall.rbearing - overall.lbearing) ;
	rectangle.height = (overall.ascent + overall.descent - clip_size) ;

	XSetClipRectangles(XtDisplay(w), gc, 0, 0, &rectangle, 1, Unsorted) ;

	XDrawImageString(XtDisplay(w), XtWindow(w), gc, x, y, string, strlen(string)) ;
}

/*
** X-Designer Stub OnExpose
*/

#ifndef   _NO_PROTO
void OnExpose(Widget w, XtPointer client_data, XtPointer xt_call_data)
#else  /* _NO_PROTO */
void OnExpose(w, client_data, call_data)
	Widget    w ;
	XtPointer client_data ;
	XmDrawingAreaCallbackStruct * call_data ;
#endif /* _NO_PROTO */
{
#ifndef   _NO_PROTO
	XmDrawingAreaCallbackStruct *call_data = (XmDrawingAreaCallbackStruct *) xt_call_data ;
#endif /* _NO_PROTO */

	GC gc = (GC) 0 ;
	
	if (XtIsRealized(w)) {
		gc = create_gc(app_shell, w) ;

		draw_clipped_string(w, gc, "Clipped text",                    the_font, 50, 50, 5) ;
		draw_clipped_string(w, gc, "created through clip rectangles", the_font, 50, 100, 5) ;
		draw_clipped_string(w, gc, "when drawing image text",         the_font, 50, 150, 5) ;
	}
}

