/*
** Generated by X-Designer
*/

/*
** X-Designer-generated prelude.
** Do not edit lines before "End of X-Designer generated prelude"
** Lines beginning ** X-Designer Stub indicate a stub
** which will not be output on re-generation
*/

/*
**LIBS: -lXm -lXt -lX11
*/

#include <stdlib.h>
#include <X11/Xatom.h>
#include <X11/Intrinsic.h>
#include <X11/Shell.h>

#include <Xm/Xm.h>
#include <Xm/DrawingA.h>

#include "resize.h"

/* End of X-Designer generated prelude */

#include <X11/keysym.h>

/* Action Procedure Functions */

/*
** X-Designer Stub HandleResize
*/

/*
** This is the engine for hiding/showing the drawing area, menubar, etc.
*/
#ifndef   _NO_PROTO
void HandleResize(Widget w, XEvent *event, String *params, Cardinal *num_params)
#else  /* _NO_PROTO */
void HandleResize(w, event, params, num_params)
	Widget    w ;
	XEvent   *event ;
	String   *params ;
	Cardinal *num_params ;
#endif /* _NO_PROTO */
{
	unsigned char attachment;

	XtVaGetValues (w, XmNtopAttachment, &attachment, NULL);

	if (attachment == XmATTACH_FORM) {
		/* Already in full-expanded mode */
		/* Re-display the menubar, etc   */

		XtManageChild (subform);
		XtVaSetValues (drawable, XmNtopAttachment, XmATTACH_WIDGET,
					 XmNtopWidget,     subform,
					 NULL);
	}
	else {
		/* Needs to be expanded  */
		/* Hide the menubar, etc */

		XtVaSetValues (drawable, XmNtopAttachment, XmATTACH_FORM, NULL);
		XtUnmanageChild (subform);
	}
}

/*
** The code is used to install a Grab/Accelerator on a shell
** on behalf of a widget down the hierarchy. This is so that
** we can issue the resize sequence irrespective of which widget
** currently has the input focus.
*/

/* Structure used to pass details of the grab to the event handler */
typedef struct 
{
	KeyCode       keycode ;     /* The key that we want to catch              */
	Modifiers     modifiers ;   /* Modified state: Control, Shift, etc        */
	Widget        target ;      /* Where we want the event to happen          */
	void        (*handler)() ;  /* What we want to happen. EG: HandleResize() */
} grab_record_t, *grab_record_p ;

/* This routine is called when the grab arrives                       */
/* It simply calls whatever routine we want associated with the event */
static void handle_grab(Widget widget, XtPointer client_data, XEvent *event, Boolean *cont)
{
	/* Check that we have the right key combination */
	grab_record_p grab = (grab_record_p) client_data ;
	
	if ((event->xkey.state == grab->modifiers)
	 && (event->xkey.keycode == grab->keycode)) {
		if (XtIsSensitive(grab->target)) {
			(*grab->handler)(grab->target, event, (String *) 0, (Cardinal) 0);
		}
	}
}

/* Routine to install the grab on the shell on behalf of a widget */
static Boolean install_grab(Widget shell, KeySym keysym, Modifiers modifiers, Widget widget, void (*handler)())
{
	KeyCode         keycode ;
	grab_record_p   grab ;

	/* Convert keysym to keycode and set up passive grab on shell */
	keycode = XKeysymToKeycode(XtDisplay(shell), keysym) ;

	if (keycode == 0) {
		return False ;	
	}

	XtGrabKey(shell, keycode, modifiers, False, GrabModeAsync, GrabModeAsync) ;

	/* Set up event handler on shell */
	grab = (grab_record_p) XtMalloc(sizeof(grab_record_t)) ;
	grab->keycode   = keycode ;
	grab->modifiers = modifiers ;
	grab->target    = widget ;
	grab->handler   = handler ;

	XtAddEventHandler(shell, KeyPressMask, False, handle_grab, (XtPointer) grab) ;

	return True ;
}

/*
** X-Designer Stub DrawableCreateCallback
*/

/*
** This is called when the drawing area is first created: it is set up
** by X-Designer. I just used this as a convenient place to install the
** grab on the drawing area, that is all.
*/

#ifndef   _NO_PROTO
void DrawableCreateCallback(Widget w, XtPointer client_data, XtPointer xt_call_data)
#else  /* _NO_PROTO */
void DrawableCreateCallback(w, client_data, call_data)
	Widget    w ;
	XtPointer client_data ;
	XmAnyCallbackStruct * call_data ;
#endif /* _NO_PROTO */
{
#ifndef   _NO_PROTO
	XmAnyCallbackStruct *call_data = (XmAnyCallbackStruct *) xt_call_data ;
#endif /* _NO_PROTO */

	/* Control-R does the business */

	install_grab (shell, XK_R, ControlMask, drawable, HandleResize) ;
}

