/*
 * This file contains routines to draw and update the screen.
 *
 * Copyright 1990 by the Massachusetts Institute of Technology.
 *
 * For copying and distribution information, please see the file
 * <mit-copyright.h>.
 *
 * Tom Coppeto
 * MIT Network Services
 * 8 August 1990
 *
 *    $Source: /afs/net.mit.edu/tools/src/xport/RCS/xstuff.c,v $
 *    $Author: tom $
 *    $Locker: tom $
 *    $Log:	xstuff.c,v $
 * Revision 1.2  90/08/19  16:19:18  tom
 * *** empty log message ***
 * 
 * Revision 1.1  90/08/15  01:13:17  tom
 * Initial revision
 * 
 */

#ifndef lint
static char *rcsid = "$Header: /afs/net.mit.edu/tools/src/xport/RCS/xstuff.c,v 1.2 90/08/19 16:19:18 tom Exp Locker: tom $";
#endif

#include <X11/Xatom.h>
#include <X11/IntrinsicP.h>
#include <X11/CompositeP.h>
#include <X11/StringDefs.h>
#include <X11/Shell.h>
#include <X11/Xaw/Paned.h>
#include <X11/Xaw/Form.h>
#include <X11/Xaw/Command.h>
#include <X11/Xaw/Toggle.h>
#include <X11/Xaw/Box.h>
#include <X11/Xaw/Viewport.h>
#include <X11/Xaw/Label.h>
#include <X11/Xaw/StripChart.h>
#include <X11/Xaw/Cardinals.h>
#include <X11/Xaw/AsciiText.h>
#include <X11/cursorfont.h>
#include <X11/Xaw/Dialog.h>
#include <X11/cursorfont.h>
#include <X11/Xaw/MenuButton.h>
#include <X11/Xaw/SimpleMenu.h>
#include <X11/Xaw/Sme.h>
#include <X11/Xaw/SmeBSB.h>
#include <X11/Xaw/Repeater.h>
#include <X11/Xaw/ScrollbarP.h>
#include <X11/Xaw/Viewport.h>
#include <X11/Xaw/ViewportP.h>
#include <X11/Xtw/Rolo.h>

#include "xrmonwatch.h"

XtAppContext app_con;
Widget toplevel;
Widget headerW;
Widget buffer;
Widget BufferPopup;

extern Agent agent;

draw_main(argc, argv)
     int argc;
     char **argv;
{
  Widget form;
  Widget view;
  Widget widget;
  Widget mainForm;
  Arg args[10];

  /*
   * initialize the application
   */

  toplevel    = XtAppInitialize(&app_con, "XNetwatch", NULL, 0, 
				&argc, argv, NULL, NULL, 0);

  display_init();

  mainForm    = XtCreateManagedWidget("form",  
				      formWidgetClass, 
				      toplevel, NULL, 0);

  form        = XtCreateManagedWidget("commandForm",
				      formWidgetClass,
				      mainForm, args, 0);

  widget      = XtCreateManagedWidget("quitButton", 
				      commandWidgetClass, 
				      form, args, 0);

/*  XtAddCallback(widget, XtNcallback, cb_filter, toplevel); */

  widget      = XtCreateManagedWidget("bufferButton", 
				      commandWidgetClass, 
				      form, args, 0);

  XtAddCallback(widget, XtNcallback, cb_buffer, toplevel);


  view        = XtCreateManagedWidget("viewport", 
				      viewportWidgetClass, 
				      mainForm, args, 0);

  form        = XtCreateManagedWidget("channelForm", 
				      formWidgetClass, 
				      view, args, 0);

  XtRealizeWidget(toplevel);
  XtAppMainLoop(app_con);
  
}

/*
 *   Function:    draw_screen()
 *   Description: draws the initial screen
 *   Returns:     none
 */



draw_buffer()
{
  Widget mainForm;
  Widget form;
  Widget widget;
  Widget view;
  Widget menu;
  Arg args[5];

  if(BufferPopup)
    {
      XtMapWidget(BufferPopup);
      XRaiseWindow(XtDisplay(BufferPopup), XtWindow(BufferPopup));
      return;
    }
  
  BufferPopup = XtCreatePopupShell("dpyPopup", 
				   topLevelShellWidgetClass,
				   toplevel, NULL, 0);

  mainForm    = XtCreateManagedWidget("form",  
				      formWidgetClass, 
				      BufferPopup, NULL, 0);

  view        = XtCreateManagedWidget("outerViewport", 
				      viewportWidgetClass, 
				      mainForm, args, 0);

  form        = XtCreateManagedWidget("textForm", 
				      formWidgetClass, 
				      view, args, 0);

  XtSetArg(args[0], XtNlabel, (XtArgVal) agent.name);
  widget      = XtCreateManagedWidget("titleLabel", 
				      labelWidgetClass, 
				      form, args, 1);

  widget      = XtCreateManagedWidget("appleIcon", 
				      labelWidgetClass, 
				      form, args, 1);

  widget      = XtCreateManagedWidget("appleIcon", 
				      labelWidgetClass, 
				      form, args, 0);

  headerW     = XtCreateManagedWidget("textHeader", 
				      labelWidgetClass, 
				      form, args, 0);

  view        = XtCreateManagedWidget("innerViewport", 
				      viewportWidgetClass, 
				      form, args, 0); 

  buffer      = XtCreateManagedWidget("textBody", 
				      formWidgetClass, 
				      view, args, 0);

  form        = XtCreateManagedWidget("commandForm", 
				      formWidgetClass, 
				      mainForm, args, 0);

  widget      = XtCreateManagedWidget("fileButton", 
				      menuButtonWidgetClass, 
				      form, args, 0);

  menu        = XtCreatePopupShell("menu",
				   simpleMenuWidgetClass,
				   widget, args, 0);

  widget      = XtCreateManagedWidget("read",
				      smeBSBObjectClass, 
				      menu, args, 0);

  XtAddCallback(widget, XtNcallback, cb_readFile, toplevel);

  /*
   * filter button menu  
   */

  widget      = XtCreateManagedWidget("filterButton", 
				      commandWidgetClass, 
				      form, args, 0);

  XtAddCallback(widget, XtNcallback, cb_filter, toplevel);



  /*
   * display button menu 
   */

  widget      = XtCreateManagedWidget("displayButton", 
				      menuButtonWidgetClass, 
				      form, NULL, 0);

  menu        = XtCreatePopupShell("menu", 
				   simpleMenuWidgetClass, 
				   widget, args, 0);

  widget      = XtCreateManagedWidget("dpyMenuIP",
				      smeBSBObjectClass, 
				      menu, args, 0);
  
  XtAddCallback(widget, XtNcallback, cb_IPdisplay, toplevel);

  widget      = XtCreateManagedWidget("dpyMenuATalk",
				      smeBSBObjectClass, 
				      menu, args, 0);
  
  XtAddCallback(widget, XtNcallback, cb_ATdisplay, toplevel);
 
  widget      = XtCreateManagedWidget("captureButton", 
				      commandWidgetClass, 
				      form, NULL, 0);
  XtAddCallback(widget, XtNcallback, cb_capture, NULL);
  
  widget      = XtCreateManagedWidget("startButton", 
				      commandWidgetClass, 
				      form, NULL, 0);
  XtAddCallback(widget, XtNcallback, cb_start, buffer);

  widget      = XtCreateManagedWidget("quitButton", 
				      commandWidgetClass, 
				      form, NULL, 0);
  XtAddCallback(widget, XtNcallback, cb_quit, NULL);

  XtPopup(BufferPopup, XtGrabNone);
  return;
}




/*
 * Function:    void make_text_popup()
 * Arguments:   Widget w:    the parent of this popup
 *              char *title: the title of the window
 *              char *stuff: the stuff to display
 * Description: A simple text display window.
 */

void
make_text_popup(w, title, stuff, color)
     Widget w;
     char   *title;
     char   *stuff;
     int    color;
{
  Widget      popup;
  Widget      form;
  Widget      label;
  Widget      text;
  Widget      box;
  Widget      button;
  Widget      update;
  Position    x, y;
  Dimension   width, height;
  Cardinal    n;
  Arg         args[10];

  /*
   * This will position the upper left hand corner of the popup at the
   * center of the widget which invoked this callback, which will also
   * become the parent of the popup.  I don't deal with the possibility
   * that the popup will be all or partially off the edge of the screen.
   */
  
  n = 0;
  XtSetArg(args[n], XtNwidth,  (XtArgVal) &width);     n++;
  XtSetArg(args[n], XtNheight, (XtArgVal) &height);    n++;
  XtGetValues(w, args, n);
  XtTranslateCoords(w, (Position) (width / 4), (Position) (height / 4),&x, &y);
  
  n = 0;
  XtSetArg(args[n], XtNx, (XtArgVal) x);               n++;
  XtSetArg(args[n], XtNy, (XtArgVal) y);               n++;
  popup = XtCreatePopupShell("statPopup", transientShellWidgetClass, w,
			    args, n);

  n = 0;
  form  = XtCreateManagedWidget("popupForm", formWidgetClass, popup, args, n);

  XtSetArg(args[n], XtNlabel, (XtArgVal) title);       n++;
  label = XtCreateManagedWidget("popupLabel", labelWidgetClass, form, 
				args, n);

  n = 0;
  XtSetArg(args[n], XtNstring,   stuff);               n++;
  XtSetArg(args[n], XtNfromVert, label);               n++;
  XtSetArg(args[n], XtNforeground,  (XtArgVal) color); n++;
  text = XtCreateManagedWidget("statText", asciiTextWidgetClass, form, 
			       args, n);

  n = 0;
  XtSetArg(args[n], XtNfromVert, (XtArgVal) text);     n++;
  XtSetArg(args[n], XtNforeground,  (XtArgVal) color); n++;
  XtSetArg(args[n], XtNborderColor, (XtArgVal) color); n++;
  box = XtCreateManagedWidget("popupBox", formWidgetClass, form, args, n);

  button = XtCreateManagedWidget("popupQuit", commandWidgetClass, box, 
				 NULL, 0);
  XtAddCallback(button, XtNcallback, cb_destroy_widget, popup);

  XtPopup(popup, XtGrabNone);
  return;
}







void
make_rolo_popup(w, title, stuff)
     Widget w;
     char   *title;
     char   *stuff;
{
  Widget      popup;
  Widget      rolo;
  Widget      form;
  Widget      label;
  Widget      box;
  Widget      button;
  Widget      update;
  Position    x, y;
  Dimension   width, height;
  Cardinal    n;
  Arg         args[10];

  /*
   * This will position the upper left hand corner of the popup at the
   * center of the widget which invoked this callback, which will also
   * become the parent of the popup.  I don't deal with the possibility
   * that the popup will be all or partially off the edge of the screen.
   */
  
  n = 0;
  XtSetArg(args[n], XtNwidth,  (XtArgVal) &width);     n++;
  XtSetArg(args[n], XtNheight, (XtArgVal) &height);    n++;
  XtGetValues(w, args, n);
  XtTranslateCoords(w, (Position) (width / 4), (Position) (height / 4),&x, &y);
  
  n = 0;
  XtSetArg(args[n], XtNx, (XtArgVal) x);               n++;
  XtSetArg(args[n], XtNy, (XtArgVal) y);               n++;
  popup = XtCreatePopupShell("roloPopup", transientShellWidgetClass, w,
			    args, n);

  n = 0;
  form  = XtCreateManagedWidget("roloForm", formWidgetClass, popup, args, n);

  width = 100;
  height = 100;
  n = 0;
  XtSetArg(args[n], XtNwidth,  (XtArgVal) width);     n++;
  XtSetArg(args[n], XtNheight, (XtArgVal) height);    n++;
  rolo = XtCreateManagedWidget("rolo", roloWidgetClass, form, args, n);
  XtCreateManagedWidget("foo", labelWidgetClass, rolo, args, 0);

  n = 0;
  XtSetArg(args[n], XtNfromVert, (XtArgVal) rolo);     n++;
  box = XtCreateManagedWidget("popupBox", formWidgetClass, form, args, n);

  button = XtCreateManagedWidget("popupQuit", commandWidgetClass, box, 
				 NULL, 0);
  XtAddCallback(button, XtNcallback, cb_destroy_widget, popup);

  XtPopup(popup, XtGrabNone);
  return;
}


