/**************************************************************************
 * OBJCLCAL.C - Calc clipping rectangle(s) for object in a tree.
 *************************************************************************/

#include "gemfintl.h"

short obj_clcalc(ptree, object, pgrect, pvrect)
	register OBJECT *ptree;
	short			   object;
	GRECT			*pgrect;
	VRECT			*pvrect;
{
	short			   adjust;
	_Ob_spec_t		 ospec;
	short			   ob_type;
	short			   ob_flags;
	GRECT			 workrect;

	obj_offxywh(ptree, object, &workrect);	/* get basic placement/sizes */

	ptree	 = &ptree[object];				/* registerize/precalc some  */
	ob_type  = ptree->ob_type & 0x00FF; 	/* miscellanious things we	 */
	ob_flags = ptree->ob_flags; 			/* us a lot below.			 */
	ospec	 = ptree->_Ob_spec;

	if (ob_flags & INDIRECT) {				/* if INDIRECT flag is set,  */
		ospec = *(_Ob_spec_t *)ospec;		  /* go get the real _Ob_spec.	*/
	}

	if (ob_type == G_USERDEF) {
		register XUSERBLK *pxub = (XUSERBLK *)ospec;
		if (pxub->ub_self == pxub) {
			ob_type = pxub->ob_type;
			ospec = pxub->ob_spec;
		}
	}

	/*
	 * deal with objects that can have graphics that extend outside
	 * their ob_width/ob_height values...
	 */

	switch (ob_type) {
	  case G_BOXTEXT:
	  case G_FBOXTEXT:
		adjust = ((TEDINFO*)ospec)->te_thickness;
		break;
	  case G_BOX:
	  case G_IBOX:
	  case G_BOXCHAR:
		adjust = (short)((char)((long)ospec >> 16));
		break;
	  case G_BUTTON:
		adjust = -1;
		if (ob_flags & EXIT)
			--adjust;
		if (ob_flags & DEFAULT)
			--adjust;
		break;
	  default:
		adjust = 0;
		break;
	}

	if (adjust > 0) {		/* if adjust value is positive, object has	  */
		adjust = 0; 		/* "inner width" and no adjustment is needed. */
	} else {				/* negative value implies outer width, invert */
		adjust = -adjust;	/* it to a positive number for rc_gadjust().  */
	}

	if (ptree->ob_state & OUTLINED) { 			 /* OUTLINED objects get  */
		adjust = 4;			  					 /* a fixed adjustment.   */
	}

	rc_gadjust(&workrect, adjust, adjust);		 /* apply adjustment	  */

	if (ptree->ob_state & SHADOWED) {			 /* apply special adjust  */
		workrect.g_w += gl_wbox / 2;			 /* for SHADOWED objects. */
		workrect.g_h += gl_hbox / 2;
	}

	if (pgrect != NULL) {						 /* if caller wants GRECT,*/
		*pgrect = workrect; 					 /* copy results to it.   */
	}

	if (pvrect != NULL) {						 /* if caller wants VRECT,*/
		rc_gtov(&workrect, pvrect); 			 /* convert results to it.*/
	}

	return adjust;
}


