/*
** 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 "halffont.h"

/* End of X-Designer generated prelude */

/* Motif 2.1 defines this, but 1.2 does not */
#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 XtPointer ConvertResource(char *resource, char *type)
{
	XrmValue     from ;
	XrmValue     to ;
		
	from.addr = resource ;
	from.size = strlen(resource) + 1 ;
	to.addr   = (XtPointer) 0 ;
		
	XtConvertAndStore(app_shell, XmRString, &from, type, &to) ;

	if (to.addr) {
		return (*(XtPointer *) to.addr) ;
	}

	return (XtPointer) 0 ;
}

static XFontStruct *the_font     = (XFontStruct *) 0 ;
static Pixel        top_color    = (Pixel) 0 ;
static Pixel        bottom_color = (Pixel) 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 ;
		char        *color ;

		font     = LookupResource(app_shell, "DrawingFont") ;
		the_font = (XFontStruct *) ConvertResource((font && *font) ? font : "fixed", XmRFontStruct) ;

		if (the_font != (XFontStruct *) 0) {
			values.font  = the_font->fid ;
			values_mask |= GCFont ;
		}
		
		color     = LookupResource(app_shell, "DrawingTopColor") ;
		top_color = (Pixel) ConvertResource((color && *color) ? color : "red", XmRPixel) ;
		
		color        = LookupResource(app_shell, "DrawingBottomColor") ;
		bottom_color = (Pixel) ConvertResource((color && *color) ? color : "blue", XmRPixel) ;

		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_twotone_string(Widget w, GC gc, char *string, XFontStruct *font, Pixel top, Pixel bottom, int x, int y)
{
	XRectangle  rectangle ;
	int         ascent ;
	int         descent ;
	int         direction ;
	XCharStruct overall ;
	XGCValues   values ;
	int         length = strlen(string) ;

	XTextExtents(font, string, length, &direction, &ascent, &descent, &overall) ;

	rectangle.x      = x ;
	rectangle.y      = y - overall.ascent ;
	rectangle.width  = (overall.rbearing - overall.lbearing) ;
	rectangle.height = (overall.ascent + overall.descent) / 2 ;

	XSetClipRectangles(XtDisplay(w), gc, 0, 0, &rectangle, 1, Unsorted) ;
	values.foreground = top ;
	XChangeGC(XtDisplay(w), gc, GCForeground, &values) ;
	XDrawImageString(XtDisplay(w), XtWindow(w), gc, x, y, string, length) ;
	
	rectangle.y      = y - overall.ascent + ((overall.ascent + overall.descent) / 2) ;
	rectangle.height = (overall.ascent + overall.descent) / 2 ;

	XSetClipRectangles(XtDisplay(w), gc, 0, 0, &rectangle, 1, Unsorted) ;
	values.foreground = bottom ;
	XChangeGC(XtDisplay(w), gc, GCForeground, &values) ;
	XDrawImageString(XtDisplay(w), XtWindow(w), gc, x, y, string, length) ;
}

/*
** 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_twotone_string(w, gc, "Two-tone Text",                 the_font, top_color, bottom_color, 50, 50) ;
		draw_twotone_string(w, gc, "Created using clip rectangles", the_font, top_color, bottom_color, 50, 100) ;
		draw_twotone_string(w, gc, "with graphic contexts",         the_font, top_color, bottom_color, 50, 150) ;
		draw_twotone_string(w, gc, "when drawing image strings",    the_font, top_color, bottom_color, 50, 200) ;
	}
}

