#if ( !defined(lint) && !defined(SABER))
  static char rcsid_module_c[] = "$Header: buttons.c,v 1.1 88/08/26 12:46:15 kit Exp $";
  static char rcs_version[] = "$Athena: buttons.c,v 1.1 88/08/26 12:46:15 kit Exp $";
#endif

/*
 *  This is the file buttons.c for xvdp.  It created all the buttons
 *  for player display.
 *
 *      Created:    8/25/88
 *      By:         Chris D. Peterson 
 *
 *      $Source: /u1/Video/tools/Xvdp/RCS/buttons.c,v $
 *      $Author: kit $
 *      $Header: buttons.c,v 1.1 88/08/26 12:46:15 kit Exp $
 *	
 *  	Copyright 1988 by the Massachusetts Institute of Technology.
 *
 *	For further information on copyright and distribution 
 *	see the file mit-copyright.h
 */

#include "mit-copyright.h"

#include <stdio.h>
#include <X11/Xos.h>
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>

#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/AsciiText.h>
#include <X11/Box.h>
#include <X11/Dialog.h>
#include <X11/Command.h>
#include <X11/Label.h>
#include <X11/Shell.h>
#include <X11/VPaned.h>

#include "xvdp.h"

static Widget top_label, popup, top;
static char * search_string;

Boolean DoVideoSearch();

void ForwardCallback(), ReverseCallback(), CommandCallback();
void DoSearch_part2(), MakePopup(), SetLocation(), ArgError();

static String model, tty;
static int baud;

static XtResource xvdp_resources[] = {
  {"model", "Model", XtRString, sizeof(String),
     (Cardinal) &(model), XtRString, "DEC_VDP50"},
  {"tty", "TTY", XtRString, sizeof(String),
     (Cardinal) &(tty), XtRString, "tty00"},
  {"baud", "Baud", XtRInt, sizeof(int),
     (Cardinal) &(baud), XtRString, "9600"},
};

static XrmOptionDescRec xvdp_options[] = {
{"-model", "model", XrmoptionSepArg, (caddr_t) NULL},
{"-tty",  "tty", XrmoptionSepArg, (caddr_t) NULL},
{"-baud", "baud", XrmoptionSepArg, (caddr_t) NULL},
};

/*	Function Name: InitWorld
 *	Description:   Initialized the world, and creates buttons.
 *	Arguments:     &argc, argv - arguments passed to the program.
 *	Returns:       none.
 */

Widget
InitWorld(argc, argv)
int * argc;
char ** argv;
{
  Widget pane, cbox, fbox, rbox;
  Arg arglist[10];
  Cardinal num_args;
  
  top = XtInitialize("xvdp","Xvdp", xvdp_options, XtNumber(xvdp_options),
		     (unsigned int*) argc, argv);

  if (*argc != 1) {
    ArgError(*argc, argv);
    exit(42);
  }

  XtGetApplicationResources( top, (caddr_t) NULL, xvdp_resources, 
			    XtNumber(xvdp_resources),NULL, (Cardinal) 0);

  StartDriver( model, tty, baud);

  num_args = (Cardinal) 0;
  XtSetArg(arglist[num_args], XtNwidth, 285);
  num_args++;
  XtSetArg(arglist[num_args], XtNheight, 125);
  num_args++;

  XtSetValues(top, arglist, num_args);

  pane = XtCreateManagedWidget("pane", vPanedWidgetClass, 
			       top, NULL, (Cardinal) 0);

  /* Create Panes. */

  top_label = XtCreateManagedWidget("topLabel", labelWidgetClass, 
			       pane, NULL, (Cardinal) 0);
  cbox = XtCreateManagedWidget("commandBox", boxWidgetClass, 
			       pane, NULL, (Cardinal) 0);
  fbox = XtCreateManagedWidget("forwardBox", boxWidgetClass, 
			       pane, NULL, (Cardinal) 0);
  rbox = XtCreateManagedWidget("reverseBox", boxWidgetClass, 
			       pane, NULL, (Cardinal) 0);
  
  /* Fill Command Box. */

  MakeButton(cbox, "quit", CommandCallback);
  MakeButton(cbox, "reset", CommandCallback);
  MakeButton(cbox, "still", CommandCallback);
  MakeButton(cbox, "index", CommandCallback);
  MakeButton(cbox, "where", CommandCallback);
  MakeButton(cbox, "goto", CommandCallback);

  /* Fill Forward Box. */

  num_args = (Cardinal) 0;
  XtSetArg(arglist[num_args], XtNborderWidth, 0);
  num_args++;

  (void) XtCreateManagedWidget("Forward", labelWidgetClass, 
				 fbox, arglist, num_args);

  MakeButton(fbox, "fast", ForwardCallback);
  MakeButton(fbox, "normal", ForwardCallback);
  MakeButton(fbox, "slow", ForwardCallback);
  MakeButton(fbox, "frame", ForwardCallback);

  /* Fill Reverse Box. */

  (void) XtCreateManagedWidget("Reverse", labelWidgetClass, 
				 rbox, arglist, num_args);

  MakeButton(rbox, "fast", ReverseCallback);
  MakeButton(rbox, "normal", ReverseCallback);
  MakeButton(rbox, "slow", ReverseCallback);
  MakeButton(rbox, "frame", ReverseCallback);

  MakePopup(top);		/* create dialog box popup. */

  return(top);
}

/*	Function Name: MakePopup
 *	Description: Creates the popup dialog box.
 *	Arguments: top - the top widget.
 *	Returns: none.
 */

void
MakePopup(top)
Widget top;
{
  Widget dialog, text;
  char * name;
  Arg arglist[10];
  Cardinal num_args = 0;
  static char trans_table[] =
    "<Key>0xff0d:   beginning-of-file() \nCtrl<Key>m:    beginning-of-file()";


  search_string = "1";

  popup = XtCreatePopupShell("popup", transientShellWidgetClass,
			     top, NULL, (Cardinal) 0);

  num_args = 0;
  XtSetArg(arglist[num_args], XtNlabel, "Enter frame to goto");
  num_args++;
  XtSetArg(arglist[num_args], XtNvalue, search_string);
  num_args++;
  
  dialog = XtCreateManagedWidget("dialog", dialogWidgetClass, popup,
			  arglist, num_args);
  
  text = XtNameToWidget(dialog,"value");

  XtOverrideTranslations(text, XtParseTranslationTable(trans_table));
  XtSetKeyboardFocus(dialog, text);

  name = "cancel";
  (void) XtDialogAddButton(dialog, name, DoSearch_part2, name);
  name = "okay";
  (void) XtDialogAddButton(dialog, name, DoSearch_part2, name);

}  
  
/*	Function Name: MakeBox
 *	Description:   Creates a generic box.
 *	Arguments:     parent - the parent widget.
 *                     name - name of the widget.
 *                     callback - the callback function.
 *	Returns: none.
 */

MakeButton(parent, name, callback)
Widget parent;
char * name;
FVoid callback;
{
  Widget button;

  button = XtCreateManagedWidget(name, commandWidgetClass, 
				 parent, NULL, (Cardinal) 0);

  XtAddCallback(button, XtNcallback, callback, name);
}  

/*	Function Name: ChangeLabel
 *	Description: Changes the top label.
 *	Arguments: str - the new label.
 *	Returns: none
 */

void
ChangeLabel(str)
char * str;
{
  /* top_label is a global variable. */
  Arg arglist[1];             /* An argument list. */

  XtSetArg(arglist[0], XtNlabel, str);

  XtSetValues(top_label, arglist, (Cardinal) 1);
}

/*	Function Name: DoSearch
 *	Description: Pops up the dialog box to get frame number
 *	Arguments: none.
 *	Returns: none.
 */

void
DoSearch()
{
  /* popup is a global variable. */

  SetLocation();
  XtPopup(popup, XtGrabExclusive);
}

/*	Function Name: DoSearch_part2
 *	Description: This function actually does the search.
 *      Arguments: w - the widget we are calling back from. 
 *                 pointer- (closure data) contains the name of the widget.
 *                 junk - (call data) not used.
 *      Returns: none.
 */

/* ARGSUSED */

void
DoSearch_part2(w, pointer, junk)
Widget w;
caddr_t pointer, junk;
{
  char * name = (char *) pointer;
  char temp[BUFSIZ];
  int frame;

  XtPopdown(popup);
  if ( streq(name, "cancel") )
    return;

  frame = atoi( XtDialogGetValueString( XtParent(w) ) );
  if (frame != 0) {
    if ( DoVideoSearch(frame) ) {
      sprintf(temp, "Now at frame %d", frame);
      ChangeLabel(temp);
    }
  }
  else
    ChangeLabel("Search value must be a number");
}

/*	Function Name: SetLocation
 *	Description: set the location of the popup widget.
 *	Arguments: none
 *	Returns: none.
 */

void
SetLocation()
{
  Arg arglist[10];
  Cardinal num_args;
  int x, y;
  char temp[BUFSIZ];

  num_args = 0;
  XtSetArg(arglist[num_args], XtNx, &x);
  num_args++;
  XtSetArg(arglist[num_args], XtNy, &y);
  num_args++;

  XtGetValues(top, arglist, num_args);

  sprintf(temp, "+%d+%d", x, y);
  num_args = 0;
  XtSetArg(arglist[num_args], XtNgeometry, temp);
  num_args++;

  XtSetValues(popup, arglist, num_args);
}
  
