                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                ;
	c2 = sel_start_col;
    }

    new_rgn = XCreateRegion();
    if (r1 == r2) {
	appendToRegion(new_rgn, r1, c1, r2, c2);
    } else {
	appendToRegion(new_rgn, r1, c1, r1, columns - 1);
	appendToRegion(new_rgn, r1 + 1, 0, r2 - 1, columns - 1);
	appendToRegion(new_rgn, r2, 0, r2, c2);
    }

    if (sel_rgn == None) sel_rgn = XCreateRegion();
    chg_rgn = XCreateRegion();
    XXorRegion(sel_rgn, new_rgn, chg_rgn);
    XDestroyRegion(sel_rgn);
    sel_rgn = None;

    /*
     * redraw any selected text
     */
    clp_rgn = XCreateRegion();
    XIntersectRegion(new_rgn, chg_rgn, clp_rgn);
    XSetRegion(display, gc, clp_rgn);
    XSetForeground(display, gc, selectBG);
    XFillRectangle(display, win, gc, 0, 0, (unsigned int)width,
	(unsigned int)height);
    XSetForeground(display, gc, selectFG);
    drawText();
    XDestroyRegion(clp_rgn);

    /*
     * redraw any normal text
     */
    clp_rgn = XCreateRegion();
    XSubtractRegion(chg_rgn, new_rgn, clp_rgn);
    XSetRegion(display, gc, clp_rgn);
    XSetForeground(display, gc, normalBG);
    XFillRectangle(display, win, gc, 0, 0, (unsigned int)width,
	(unsigned int)height);
    XSetForeground(display, gc, normalFG);
    drawText();

    XDestroyRegion(clp_rgn);
    XDestroyRegion(chg_rgn);

    sel_rgn = new_rgn;
}

/***********************************************************************
 *
 * FUNCTION:
 *	copySelect
 *
 * INPUTS:
 *	none
 *
 * OUTPUTS:
 *	none
 *
 * RETURNS:
 *	none
 *
 * EXTERNALLY READ:
 *	logstart	- (static int) current starting index in display area
 *	sel_start_row	- (static int) row of first selected character
 *	sel_start_col	- (static int) column of first selected character
 *	sel_cur_row	- (static int) row currently under the cursor
 *	sel_cur_col	- (static int) column currently under the cursor
 *
 * EXTERNALLY MODIFIED:
 *	sel_buf		- (static char *) text buffer for selection
 *
 * DESCRIPTION:
 *	Copy the text indicated by the selected area of the screen
 *	to the select text buffer.
 */

static void 
copySelect(void)
{
    int r1, c1;			/* first cell */
    int r2, c2;			/* last cell */
    char *msg;			/* text pointer */
    unsigned int len;		/* length of text string */
    char *ptr;			/* pointer into selection buffer */

    if ((sel_cur_row > sel_start_row) ||
	((sel_cur_row == sel_start_row) && (sel_cur_col >= sel_start_col))) {
	r1 = sel_start_row;
	c1 = sel_start_col;
	r2 = sel_cur_row;
	c2 = sel_cur_col;
    } else {
	r1 = sel_cur_row;
	c1 = sel_cur_col;
	r2 = sel_start_row;
	c2 = sel_start_col;
    }
    
    ptr = sel_buf;

    /*
     * get the first line
     */
    msg = getLogMessage(logstart + r1);
    len = strlen(msg);
    if (c1 > len) {
	msg = "";
	len = 0;
    } else {
	msg += c1;
	len -= c1;
	if (r1 == r2) c2 -= c1;
    }

    /*
     * copy each line in turn and get the last line
     */
    while (++r1 <= r2) {
	strncpy(ptr, msg, len);
	ptr += len;

	msg = getLogMessage(logstart + r1);
	len = strlen(msg);
    }

    /*
     * copy out the last line
     */
    if (c2 + 1 < len) {
	len = c2 + 1;
    }
    strncpy(ptr, msg, len);
    ptr += len;

    /*
     * terminate the buffer
     */
    *ptr = '\0';

    /*
     * cannot set a property to an empty string
     */
    if (*sel_buf == '\0') {
	strcpy(sel_buf, "\n");
    }
}

/***********************************************************************
 *
 * FUNCTION:
 *	appendToRegion
 *
 * INPUTS:
 *	rgn		- (Region) existing and modified region
 *	r1		- (int) row number of upper left corner
 *	c1		- (int) column number of upper left corner
 *	r2		- (int) row number of lower right corner
 *	c2		- (int) column number of lower right corner
 *
 * OUTPUTS:
 *	none
 *
 * RETURNS:
 *	none
 *
 * EXTERNALLY READ:
 *	height		- (static Dimension) current height of message area
 *	width		- (static Dimension) current width of message area
 *	cheight		- (static int) character height in pixels
 *	cwidth		- (static int) character width in pixels
 *	cell_x		- (static int) x offset of first cell in pixels
 *	cell_y		- (static int) y offset of first cell in pixels
 *
 * EXTERNALLY MODIFIED:
 *	none
 *
 * DESCRIPTION:
 *	Append a rectangle defined by row and column values to the
 *	select region.
 */

static void 
appendToRegion(Region rgn, int r1, int c1, int r2, int c2)
{
    XRectangle r;

    if ((c2 >= c1) && (r2 >= r1)) {
	r.x = cwidth * c1 + cell_x;
	r.y = cheight * r1 + cell_y;
	r.width = cwidth * (1 + c2 - c1);
	r.height = cheight * (1 + r2 - r1);
	XUnionRectWithRegion(&r, rgn, rgn);
    }
}
