/*
**	help.c
**
*/

#include <stdio.h>
#include <X11/Intrinsic.h>
#include <X11/Xaw/Paned.h>
#include <X11/Box.h>
#include <X11/Text.h>
#include <X11/List.h>
#include <X11/StringDefs.h>
#include "xmagic.h"

static	Widget	text_widget, list_widget;

#ifndef HELPPATH
#define HELPPATH	"/usr/lib/X11/xmagic/"
#endif

void make_help (parent_widget)

Widget		parent_widget;

{
	int	i;
	Widget	the_box, the_button, vpane_widget, file1_button, file2_button;
	void	button_action (), change_action ();
	static XtCallbackRec
		button_calls []	=
			{{button_action, NULL},
			 {NULL, NULL},},
		list_calls []	=
			{{change_action, NULL},
			 {NULL, NULL},};
	static String	
		the_list []	=
			{"intro",
			 "logic",
			 "output",
			 "formulae",
			 "axioms",
			 "connectives",
			 NULL,};
	Arg	list_args [30], box_args [30];
	char	*default_help;

	default_help = strcat (HELPPATH, "intro.hlp");
	vpane_widget = XtCreateManagedWidget ("vpane_widget", panedWidgetClass,
			parent_widget, NULL, 0);

	i = 0;
	XtSetArg (box_args [i], XtNhSpace, (XtArgVal) 10);
	i++;
	the_box = XtCreateManagedWidget ("box_widget", boxWidgetClass,
			vpane_widget, box_args, i);

	i = 0;
	XtSetArg (list_args [i], XtNcallback, (XtArgVal) list_calls);
	i++;
	XtSetArg (list_args [i], XtNlist, (XtArgVal) the_list);
	i++;
	XtSetArg (list_args [i], XtNdefaultColumns, (XtArgVal) 6);
	i++;
	XtSetArg (list_args [i], XtNborderWidth, (XtArgVal) 0);
	i++;
	XtSetArg (list_args [i], XtNverticalList, (XtArgVal) False);
	i++;
	list_widget = XtCreateManagedWidget ("the_list", listWidgetClass, 
			the_box, list_args, i);

	make_button (the_box, &the_button, "close_button", "close window",
			button_calls, NULL, 0, NULL, 0);

	make_text (vpane_widget, &text_widget, "text_widget", 
		default_help, 13*24+4, 80*6+14+4,
		NULL, 0, NULL, 0);
}

void button_action (the_button, client_data, call_data)

Widget	the_button;
caddr_t	client_data, call_data;

{
	XtPopdown (the_global_help_pop_up);
	XtSetSensitive (global_help_button, True);
}

static void change_action (the_button, client_data, the_item)

Widget			the_button;
caddr_t			client_data;
XtListReturnStruct	*the_item;

{
	XtTextSource	the_source;
	Arg		change_args [30];
	char		file_name [256];
	int		i;

	XtTextEnableRedisplay (text_widget);
	sprintf (file_name, "%s%s.hlp\0", HELPPATH, the_item->string);
	i = 0; 
	XtSetArg (change_args [i], XtNfile, (XtArgVal) file_name); 
	i++;
	XtDiskSourceDestroy (XtTextGetSource(text_widget));
	the_source = XtDiskSourceCreate (text_widget, change_args, i);
	XtTextSetSource (text_widget, the_source, 0);
}

void help_action (the_button, client_data, call_data)

Widget the_button;
caddr_t client_data, call_data;

{
	static Boolean	first = True;

	XtPopup (the_global_help_pop_up, XtGrabNone);
	if (first) {
		XtListHighlight (list_widget, 0);
		first = False;
	}
	XtSetSensitive (the_button, False);
}