/* $Id: Textview.c,v 1.1 1997/04/23 13:41:04 archer Exp $
 *	TEXTVIEW.C - Handle TextView objects
 */
#include <Widget/TextViewP.h>
#include <Widget/fontsP.h>


/**
 ** Private GC
 **/
static GC	text_gc;


/**
 ** Internal: TEXTVIEW_EXPOSE
 **	Refresh the widget window
 **/
static void textview_expose(X11_widget wdg, XEvent *expose)
{
char	**lines;
int	h_top, h_bot;
int	h_min, h_max;
int	h, y;
/*
 * Integral N
 */
	lines = W_TEXTVIEW->lines;
	if (!lines)
		return;
/*
 * Viewed window
 */
	h = W_TEXTVIEW->h;
	h_top = wdg->h - (wdg->margin_inside + wdg->border2);
	h_bot = h_top - h;
	y     = h_bot + W_TEXTVIEW->font->ascent;
	h_min = expose->xexpose.y;
	h_max = h_min + expose->xexpose.height;
/*
 * Go!
 */
	XSetForeground(x11__screen, text_gc, wdg->foreground);
	XSetFont(x11__screen, text_gc, W_TEXTVIEW->font->fid);
	while (*lines) {
		if (h_top <= h_min)
			break;
		if (h_bot < h_max) {
			XDrawString(x11__screen, wdg->id, text_gc,
						wdg->margin_inside, y,
						*lines, strlen(*lines));
		}
		lines++;
		h_top -= h;
		h_bot -= h;
		y     -= h;
	}
}


/**
 ** X11_TEXTVIEW_REFRESH
 **	Refresh a TextView
 **/
void X11_TextViewRefresh(X11_widget wdg, int line)
{
	if (wdg->id)
		XClearArea(x11__screen, wdg->id, 0, 0, 0, 0, True);
}


/**
 ** X11_TEXTVIEW
 **	Create a TextView window
 **/
X11_widget X11_TextView(X11_widget parent, char *name, char **field)
{
x11__textview	*object;
int		b, w, h;
/*
 * Get resource
 */
	object = allocator(x11__textview);
	x11__core_setup(&object->core, parent, "TextView", name);
/*
 * Initialize fields
 */
	object->core.event_mask = ExposureMask;
	object->core.on_expose  = textview_expose;
/*
 * Properties as specified by resources
 */
	object->core.margin_inside = 5;
	object->core.border2       = 2;
	object->core.background    = x11__white_pixel;
	x11__resource_basic(&object->core);
	object->font = x11__font_resource(x11__font_courrier12);
/*
 * Compute values based on resources field
 */
	h = object->font->ascent + object->font->descent;
	w = h * 20;
	b = object->core.margin_inside * 2 + object->core.border2;
	object->core.w_min = b + w;
	object->core.h_min = b + h * 5;
	object->core.w     = b + w * 2;
	object->core.h     = b + h * 24;
	object->h = h;
/*
 * Finished
 */
	if (!text_gc)
		text_gc = x11__widget_gc(OBJECT_P);
	object->lines = field;
	return OBJECT_P;
}
