/*
 * Name:	MicroEmacs
 *		(Simple) Commodore Amiga mouse handling
 * Version:	31
 * Last edit:	21-Apr-86
 * Created:	21-Apr-86  ...!ihnp4!seismo!ut-sally!ut-ngp!mic
 */

#include <exec/types.h>
#include <intuition/intuition.h>
#undef	TRUE
#undef	FALSE
#include "def.h"

extern	int	ttmouse();		/* Defined by "ttyio.c"		*/
extern	int	forwline();		/* Defined by "line.c"		*/
extern	int	forwchar();		/* Defined by "basic.c"		*/

/*
 * Handle the mouse click that's been passed
 * by ttgetc() and position dot where the
 * user pointed at.
 */

amigamouse(f, n, k)
{
	register WINDOW *wp;
	register int	dot;
	register int	c;
	register int	col;
	register int	newrow, newcol;
	SHORT	x, y;
	USHORT	qualifier;

	/* figure out new screen position of dot */
	ttmouse(&x, &y, &qualifier);
	newcol = x / 8;
	newrow = y / 8;

	/* find out which window was clicked in	*/
	for (wp = wheadp; wp != NULL; wp = wp->w_wndp)
		if ((newrow >= wp->w_toprow) && 
			(newrow < (wp->w_toprow + wp->w_ntrows)))
			break;

	/* make sure click was in a bona fide window */
	if (wp == NULL)
		return (ABORT);

	/* move dot to top left of selected window */
	curwp = wp;
	curbp = wp->w_bufp;
	curwp->w_dotp = wp->w_linep;
	curwp->w_doto = 0;

	/* go forward the correct # of lines 		*/
	forwline(FALSE, newrow - curwp->w_toprow, KRANDOM);
	
	/* go forward the correct # of characters	*/
	/* need to count them out because of tabs	*/
	col = dot = 0;
	while ((col < newcol) && (dot < llength(curwp->w_dotp))) {
		c = lgetc(curwp->w_dotp, dot++);
		if (c == '\t')
			col |= 0x07;
		else if (ISCTRL(c) != FALSE)
			++col;
		++col;
	}
	forwchar(FALSE, dot, KRANDOM);

	return (TRUE);
}


