// This may look like C code, but it is really -*- C++ -*-

/***********************************************************
Copyright 1991 by the Massachusetts Institute of Technology

Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation, and that the name of M.I.T. not be used in advertising or
publicity pertaining to distribution of the software without specific,
written prior permission.  M.I.T. makes no representations about the
suitability of this software for any purpose.  It is provided "as is"
without express or implied warranty.

Author: Roman J. Budzianowski - Project Athena MIT

******************************************************************/

#include <xt++/Intrinsic.h>

#include <xt++/Shell.h>
#include <xt++/Xaw/Label.h>
#include <xt++/Xaw/Command.h>
#include <xt++/Xaw/Form.h>
#include <xt++/Xaw/Dialog.h>
#include <xt++/Xaw/Clock.h>
#include <xt++/Xaw/Panner.h>
#include <xt++/Xaw/Porthole.h>

#include <X11/StringDefs.h>
#include <libc.h>
#include <stream.h>

#include "ObjectEditor.h"

void QuitCB(Core* w , XtPointer, XtPointer)
{
   w->GetAppContext()->ExitLoop();
}

void AddIOObjectCB(Core*  , XtPointer w, XtPointer)
{
   static char name[] = "objectA";
   ObjectEditor * ws = (ObjectEditor*)w;

   name[6] = name[6]+1;
   ws->AddObjectView("io");
}

void AddConstantCB(Core*  , XtPointer w, XtPointer)
{
   static char name[] = "constantA";
   ObjectEditor * ws = (ObjectEditor*)w;

   name[8] = name[8]+1;
   ws->AddObjectView("constant",0,1);
}

void AddSplitterCB(Core*  , XtPointer w, XtPointer)
{
   static char name[] = "splitterA";
   ObjectEditor * ws = (ObjectEditor*)w;

   name[8] = name[8]+1;
   ws->AddObjectView("splitter",1,2);
}

void AddMultiplierCB(Core*  , XtPointer w, XtPointer)
{
   static char name[] = "multiplierA";
   ObjectEditor * ws = (ObjectEditor*)w;

   name[10] = name[10]+1;
   ws->AddObjectView("multiplier",2,1);
}

void  PortholeCallback(Core*, XtPointer panner_ptr, XtPointer report_ptr)
{
    XawPannerReport *report = (XawPannerReport *) report_ptr;
    Widget panner = (Widget) panner_ptr;

    panner->SetValues(XtNsliderX, (XtArgVal)report->slider_x,
		      XtNsliderY, (XtArgVal)report->slider_y);

    if (report->changed != (XawPRSliderX | XawPRSliderY)) {
       panner->SetValues(XtNsliderWidth, (XtArgVal)report->slider_width,
			 XtNsliderHeight, (XtArgVal)report->slider_height,
			 XtNcanvasWidth, (XtArgVal)report->canvas_width,
			 XtNcanvasHeight, (XtArgVal)report->canvas_height);
    }
}

void PannerCallback(Core*, XtPointer closure, XtPointer report_ptr)
{
   Core* editspace = (Core *) closure;
   XawPannerReport *report = (XawPannerReport *) report_ptr;

   editspace->SetValues(XtNx, -report->slider_x,
			XtNy, -report->slider_y);
}

main(unsigned int argc, char** argv)
{
   ApplicationContext appContext;

   Display *dpy =  appContext.OpenDisplay(NULL, argv[0], "Eureka",
                        NULL,0,
                        &argc, argv);

   ApplicationShellWidget aw( argv[0], "Eureka", dpy);

   FormWidget    form("form", aw);
   form.Manage();

   FormWidget  controls("controls",form);
   controls.Manage();

   CommandWidget quit("quit", controls);
   quit.AddCallback(XtNcallback, QuitCB, (XtPointer)NULL);
   quit.Manage();

   PannerWidget panner("panner", controls);
   panner.Manage();

   LabelWidget create("create", controls);
   create.Manage();

   CommandWidget io("create_io", controls);
   io.Manage();

   CommandWidget constant("create_constant", controls);
   constant.Manage();

   CommandWidget splitter("create_splitter", controls);
   splitter.Manage();

   CommandWidget multiplier("create_multiplier", controls);
   multiplier.Manage();

   PortholeWidget porthole("porthole", form);
   porthole.Manage();

   ObjectEditor work(&porthole);
   work.Manage();
   io.AddCallback(XtNcallback, AddIOObjectCB, (XtPointer)&work);
   constant.AddCallback(XtNcallback, AddConstantCB, (XtPointer)&work);
   splitter.AddCallback(XtNcallback, AddSplitterCB, (XtPointer)&work);
   multiplier.AddCallback(XtNcallback, AddMultiplierCB, (XtPointer)&work);

   porthole.AddCallback(XtNreportCallback, PortholeCallback, (XtPointer) &panner);
   panner.AddCallback(XtNreportCallback, PannerCallback, (XtPointer) &work);

   aw.Realize();

   appContext.MainLoop();

}
