/*
 * Copyright 1990 by Baylor College of Medicine ALL RIGHTS RESERVED. 
 *
 * This program is subject to a license agreement between 
 * Baylor College of Medicine and MIT. Any use inconsistent with
 * said license and any use by persons other than the faculty, 
 * students and staff at MIT or any use on a computer not operated 
 * as part of the Athena Computing Environment (ACE) is expressly 
 * prohibited.
 */
#include <stdio.h>

#include <X11/Intrinsic.h>
#include <X11/IntrinsicP.h>
#include <X11/ObjectP.h>
#include <X11/StringDefs.h>
#include <X11/CoreP.h>
#include <X11/Shell.h>
#include "TestP.h"

static ClassInitialize();
static Initialize();
static Destroy();
static Resize();
static ReDisplay();
static Boolean SetValues();

static ac_select() ;
static ac_focus_in() ;
static ac_focus_out() ;
static ac_key() ;

static char defaultTranslations[] = "\
	<Btn1Down>:	select()\n\
	<Key>:	key()\n\
	<FocusIn>:	focus-in()\n\
	<FocusOut>:	focus-out()\n\
" ;

static XtActionsRec actionsList[] =
{
	{"key",(XtActionProc)ac_key},
	{"focus-in",(XtActionProc)ac_focus_in},
	{"focus-out",(XtActionProc)ac_focus_out},
	{"select",(XtActionProc)ac_select},
} ;

static XtResource resources[] =
{
	{XtNnext,
		XtCNext,
		XtRPointer,
		sizeof(caddr_t),
		XtOffset(TestWidget,test.next),
		XtRPointer,
		NULL},
} ;

TestClassRec testWidgetClassRec =
{
	{
		(WidgetClass)&widgetClassRec,			/* superclass */
		"Test",									/* class name */
		sizeof(TestRec),						/* widget size */
		(XtWidgetClassProc)ClassInitialize,		/* class_initialize */
		NULL,									/* class_part_initialize */
		FALSE,									/* class inited */
		(XtInitProc)Initialize,					/* initialize */
		NULL,									/* initialize_hook */
		XtInheritRealize,						/* realize */
		actionsList,							/* actions */
		XtNumber(actionsList),					/* num_actions */
		resources,								/* resources */
		XtNumber(resources),					/* num_resources */
		NULLQUARK,								/* xrm_class */
		TRUE,									/* compress_motion */
		TRUE,									/* compress_exposure */
		FALSE,									/* compress_enterleave */
		FALSE,									/* visible_interest */
		(XtWidgetProc)Destroy,					/* destroy */
		(XtWidgetProc)Resize,					/* resize */
		(XtExposeProc)ReDisplay,				/* expose */
		SetValues,								/* set_values */
		NULL,									/* set_values_hook */
		XtInheritSetValuesAlmost,				/* set_values_almost */
		NULL,									/* get_values_hook */
		NULL,									/* accept_focus */
		XtVersion,								/* version */
		NULL,									/* callback private */
		defaultTranslations,					/* tm_table */
		NULL,									/* query_geometry */
		XtInheritDisplayAccelerator,			/* display_accelerator */
		NULL									/* extension */
	},
	{
		0,
	}
} ;

WidgetClass testWidgetClass = (WidgetClass)&testWidgetClassRec ;

static
ClassInitialize()
{
}

static
Initialize(request,new)
	Widget request,new ;
{
}

static
Widget
find_shell_parent(w)
	Widget w ;
{
	Widget p ;
	for(p = w; XtParent(p) != NULL && !XtIsShell(p); p = XtParent(p)) ;
	return p ;
}

static
Destroy(w)
	Widget w ;
{
}

static
Resize(w)
	Widget w ;
{
}

static
ReDisplay(w, event)
	Widget w ;
	XExposeEvent *event ;
{
}

static
Boolean
SetValues(old, request, new)
	Widget old ;
	Widget request ;
	Widget new ;
{
	return True ;
}

static
ac_select(w,event)
	Widget w ;
	XButtonEvent *event ;
{
	Widget shell = find_shell_parent(w) ;
	XtSetKeyboardFocus(shell,w) ;
}

static
ac_key(w,event)
	Widget w ;
	XButtonEvent *event ;
{
	TestPart *test = &(((TestWidget)w)->test) ;
	static char buf[80] ;
	KeySym key ;
	XComposeStatus compose ;
	int bc = XLookupString(event,buf,sizeof buf,&key,&compose) ;
	if (bc > 0)
	{
		switch(buf[0])
		{
			case '\t':
				if (test->next != NULL)
				{
					Widget shell = find_shell_parent(test->next) ;
					XtSetKeyboardFocus(shell,test->next) ;
				}
				break ;
			default :
				printf("%s Got key %c\n",w->core.name,buf[0]) ;
				break ;
		}
	}
}

static
ac_focus_in(w,event)
	Widget w ;
	XFocusChangeEvent *event ;
{
	if (event->detail == NotifyPointer)
	{
		return ;
	}
	printf("Got focus in on %s, detail = %d\n",w->core.name,event->detail) ;
	fflush(stdout) ;
}

static
ac_focus_out(w,event)
	Widget w ;
	XFocusChangeEvent *event ;
{
	if (event->detail == NotifyPointer)
	{
		return ;
	}
	printf("Got focus out on %s, detail =%d\n",w->core.name,event->detail) ;
	fflush(stdout) ;
}
