/*
**	actions.c
**
*/

#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include "defs.h"
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/Xaw/Command.h>
#include <X11/Xaw/Box.h>
#include <X11/Xaw/Dialog.h>
#include <X11/Xaw/Toggle.h>
#include <X11/Xaw/List.h>
#include <X11/Xaw/Form.h>
#include <X11/Xaw/Cardinals.h>
#include "xmagic.h"


#define ILLEGAL	3

static Boolean	order_flag = False;
static char	order_prefix;
static char	logic_string [10] = "FD";

void quit_action (quit_button, client_data, call_data)

Widget		quit_button;
caddr_t		client_data, call_data;

{
	send_to_magic ("e\n");
	XtDestroyApplicationContext(XtWidgetToApplicationContext(quit_button));
	exit (0);
}

void go_action (go_button, client_data, call_data)

Widget		go_button;
caddr_t		client_data, call_data;

{
	char           s [256];
	extern Boolean verbose;

	if (!verbose) {
		verbose = True;
		send_to_magic ("g\n");
		get_from_magic (s, 256);
/*		verbose = True; */
		receive_all ();
		verbose = False;
		send_to_magic ("\n");
		receive_all ();
	}
	else {
		send_to_magic ("g\n");
		receive_all ();
		send_to_magic ("\n");
		receive_all ();
	}
}

void logic_action (logic_list, client_data, the_selected_logic)

Widget			logic_list;
caddr_t			client_data; 
XtListReturnStruct	*the_selected_logic;

{
	char	the_line [256];

/*	sprintf (the_line, "logic = %s\n", the_selected_logic->string); */
	strcpy (logic_string, the_selected_logic->string);
	send_to_magic ("l\n");
	if (order_flag) 
		sprintf (the_line, "%c%s\n", order_prefix, 
			the_selected_logic->string);
	else
		sprintf (the_line, "%s\n", the_selected_logic->string);
	send_to_magic (the_line);
	receive_all ();
}

void verbose_action (toggle, client_data, call_data)

Widget	toggle;
caddr_t	client_data, call_data; /* unused */

{
	extern Boolean	verbose;
	Arg		args[ONE];

	XtSetArg (args[ZERO], XtNstate, &verbose); 
	XtGetValues (toggle, args, ONE);
}

void order_action (order_list, client_data, the_selected_order)

Widget			order_list;
caddr_t			client_data;
XtListReturnStruct	*the_selected_order;

{
	char	the_line [256];

	if (the_selected_order->list_index == 1) {
		order_flag = True;
		order_prefix = 'L';
	}
	else 
	   if (the_selected_order->list_index == 2) {
		order_flag = True;
		order_prefix = 'T';
	}
	else
		order_flag = False;
	if (order_flag)
		sprintf (the_line, "%c%s\n", order_prefix, logic_string);
	else 
		sprintf (the_line, "%s\n", logic_string);
	send_to_magic ("l\n"); send_to_magic (the_line);
	receive_all ();
}

static char 	tty_output [10] = "none", file_output [10] = "none",
		file_name [30] = "xmagic.save";
Boolean		already_open = False;

void tty_action (tty_list, client_data, the_selected_output)

Widget			tty_list;
caddr_t			client_data;
XtListReturnStruct	*the_selected_output;

{
	char	the_line [256];

	strcpy (tty_output, the_selected_output->string);
	sprintf (the_line, "%s\n", the_selected_output->string);
	send_to_magic ("I\n");
	send_to_magic (the_line);
	sprintf (the_line, "%s\n", file_output);
	send_to_magic (the_line);
	if (strcmp (file_output, "none") != 0) {
		if (already_open) 
			send_to_magic ("y\n");
		sprintf (the_line, "%s\n", file_name);
		send_to_magic (the_line);
	}
	receive_all ();
}

void file_action (file_list, client_data, the_selected_output)

Widget			file_list;
caddr_t			client_data;
XtListReturnStruct	*the_selected_output;

{
	char	the_line [256];

	send_to_magic ("I\n");
	sprintf (the_line, "%s\n", tty_output);
	send_to_magic (the_line);
	strcpy (file_output, the_selected_output->string);
	sprintf (the_line, "%s\n", file_output);
	send_to_magic (the_line);
	if (the_selected_output->list_index > 0) {
		if (already_open)
			send_to_magic ("y\n");
		else
			already_open = True;
		XtSetSensitive (global_file_dialog, True);
		strcpy (file_name, 
			XtDialogGetValueString (global_file_dialog));
		sprintf (the_line, "%s\n", file_name);
		send_to_magic (the_line);
	}
	else
		XtSetSensitive (global_file_dialog, False);

	receive_all ();
}

void accept_action (accept_button, client_data, call_data)

Widget	accept_button;
caddr_t	client_data, call_data;

{
	char	the_line [256];

	send_to_magic ("I\n");
	sprintf (the_line, "%s\n", tty_output);
	send_to_magic (the_line);
	sprintf (the_line, "%s\n", file_output);
	send_to_magic (the_line);
	send_to_magic ("y\n");
	strcpy (file_name, XtDialogGetValueString (global_file_dialog));
	sprintf (the_line, "%s\n", file_name);
	send_to_magic (the_line);

	receive_all ();
}

void update_file_dialog ()
{
	extern String	global_file_string;
	Arg		args[3];

	XtSetArg (args[0], XtNvalue, global_file_string);
	XtSetValues (global_file_dialog, args, ONE);
}

void getfile (widget, event, params, num_params)
Widget		widget;
XEvent		*event;
String		*params;
Cardinal	*num_params;
{
	accept_action(widget, NULL, NULL);
}

void reset_action (the_button, client_data, call_data)

Widget	the_button;
caddr_t	client_data, call_data;

{
	extern	Widget	logic_list, order_list, tty_list, file_list,
			global_file_dialog;
	extern	void	reset_jump (), reset_axioms (), reset_conns ();
	extern 	Boolean	already_open, axioms_up, conns_up;
/*
**	Tell MaGIC to "kill" the job
*/
	send_to_magic ("K\n");
	receive_all ();
/*
**	Now MaGIC asks for "your favourite logic"
*/
	send_to_magic ("FD\n");
	receive_all ();
/*
**	Ask MaGIC to set up I/O defaults
*/
	send_to_magic ("I\nnone\nnone\n");
	already_open = False;
	strcpy (file_output, "none");
	strcpy (tty_output, "none");
	receive_all ();
/*
**	Cleanup things on the main control panel
*/
	XtListHighlight (logic_list, 0);
	XtListHighlight (order_list, 0);
	XtListHighlight (tty_list, 0);
	XtListHighlight (file_list, 0);
	XtSetSensitive (global_file_dialog, False);
/*
**	Now switch off the jump conditions
*/
	reset_jump ();
/*
**	Fix up the user-defined axioms
*/
	if (axioms_up)
		reset_axioms ();
/*
**	Fix up the connectives
*/
	if (conns_up)
		reset_conns (); 
}

void stop_action (the_button, client_data, call_data)

Widget	the_button;
caddr_t	client_data, call_data;

/*
**	This facility is not implemented yet, hence it does little.
*/

{
	/* do nothing */
}

void inform (scrollbar, xevent_ptr, string_ptr, cardinal_ptr)
Widget		scrollbar;
XEvent		*xevent_ptr;
String		*string_ptr;
Cardinal	*cardinal_ptr; /* none of the above are used */
{
	extern int	no_of_procs;
	char		a [10];
	void		update_scrollbar();
	
	sprintf (a, "#%d\n", no_of_procs);
	send_to_magic (a);
	if (receive_all () == ILLEGAL) {
		ring ();
		send_to_magic ("\n");
		receive_all ();
		update_scrollbar ();
	}
}

XtCallbackProc whereami (scrollbar, label, thumb_position)
Widget		scrollbar;
Widget		label;
float		*thumb_position;
{
	Arg	args[ONE];
	int	j;
	char	text [3];
	extern int
		no_of_procs, max_procs;

	j = (int) ((1.0 - (*thumb_position)) * (max_procs - 2.0 + 0.5) + 2.0);
	no_of_procs = j;
	sprintf (text, "%2d", j);
	XtSetArg (args[ZERO], XtNlabel, text);
	XtSetValues (label, args, ONE);
}

void update_pro_label ()
{
	Arg		args[ONE];
	extern	int	no_of_procs;
	extern	Widget	global_pro_label;
	char		text[3];

	sprintf (text, "%2d", no_of_procs);
	XtSetArg (args[ZERO], XtNlabel, text);
	XtSetValues (global_pro_label, args, ONE);
}

void update_scrollbar ()
{
	float		top;
	extern int	no_of_procs, max_procs;
	extern Widget	global_scrollbar;

	top = 1.0 - ((float)no_of_procs + 0.5 - 2.0) /
			((float)max_procs - 2.0 + 0.5);
	XawScrollbarSetThumb(global_scrollbar, top, 0.0);
}
