// 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++/Xaw/Layout.h>
#include <X11/StringDefs.h>

#include "ObjectView.h"

class ObjectEditor;

class Connection {
friend class ObjectEditor;
 private:
   ObjectView* output;
   Anchor*     output_anchor;
   ObjectView* input;
   Anchor*     input_anchor;
   Connection* next;
   XPoint      start;
   XPoint      end;
				// cache the line's coordinates for exposure processing
				// and for rubberbanding

   Connection* Push(Connection* c) { next=c; return this; }
   Connection* Connect(ObjectView* i, Anchor* i_anchor);

   Connection(ObjectView* output,
	      Anchor*     output_anchor)
     : output(output), output_anchor(output_anchor) , input(NULL), input_anchor(NULL) {} 

   ~Connection() { if(input_anchor)input_anchor->Disconnect(); output_anchor->Disconnect(); }
  
};

class NewObject {
friend class ObjectEditor;
 private:
   String name;
   ObjectView* object;
   Cardinal input_count;
   Cardinal output_count;
   Boolean  creating;
};

class ObjectEditor : public LayoutWidget {
 public:
   ObjectEditor(Widget parent);
//   ObjectEditor(Core& parent) {}

   void StartConnection(ObjectView* output, Anchor* output_anchor);
   void CommitConnection(ObjectView* input, Anchor* input_anchor);
   void RubberbandConnection(XPoint&);
   void RefreshConnections();
   void AbortConnection();

   void  AddObjectView(String name, Cardinal input_count = 1, Cardinal output_count = 1);
   ObjectView* CreateObjectView(Position xpos, Position ypos);

   void StartMovingObject(ObjectView* object);
   void PlaceObject(XPoint&);
   void RubberbandObject(XPoint&);
   void PlaceNewObject(XPoint&);
   void    AbortCreatingObject() { new_object.creating = False; XBell(XtDisplay(),50); }

   XPoint GetEventLocation(XEvent*);
   void GetLastEvent(XEvent*);

   void Initialize() { XDefineCursor(XtDisplay(), XtWindow(), normal_cursor); }

 private:
   static  Boolean class_inited;
   Connection* connection_list;
   Connection* connection_in_progress;

   ObjectView* moving_object;
   XPoint      current_position;
   NewObject   new_object;

   EventMask  grab_mask;


   GC          normal_gc;	// draw connections
   GC          invert_gc;	// erase connection
   GC          flip_gc;		// draw rubberband

// resources 
   Cursor      normal_cursor;
   Cursor      placement_cursor;
   Cursor      connection_cursor;
   Boolean     rubberband;

// private methods
   void InitializeClass();

   void InitializeConnection(Position xpos, Position ypos);

   void DrawConnectionLine(GC);	// default connection_in_progress
   void DrawConnectionLine(Connection*,GC);
   void UpdateObjectConnections();
   XPoint& RecalculateConnection(XPoint&, Anchor*);
   void ClearConnectionWindows(Connection*);

   Boolean CreatingObjectP() { return new_object.creating; }

   Boolean PointerInsideP(XPoint&);
   void DrawRubberbandOfObject();
   void EraseRubberbandOfObject() { DrawRubberbandOfObject(); }

};


