
/* Generated by Interface Builder */

#import "IconView.h"

@implementation IconView

/* appDidInit is the first message sent after all objects, connections, and
   IDs have been initialized.  This method simply orders the window to the
   front and then registers the window with speaker/listener so it can
   receive icon related events.  The appDidInit message is only sent to this
   object because the File's Owner's delegate is connected to this
   viewObject within IB.
*/

  - appDidInit: sender
{
   [self registerWindow];
    
   return self;
}

/* basic view newFrame method-- nothing special here.  Just messages to self
   to initialize the various variables.
*/

+ newFrame: (NXRect *) theRect
{
   self = [super newFrame:theRect];
   
   return self;
}


/* drawSelf would normally update the view with whatever the application would
   display there.  In this case, it simply erases the view and draws a string
   art design so the window isn't completely boring.
*/
- drawSelf:(const NXRect *)rects :(int)rectCount
{
  int i;
    
  PSsetgray(0.333);
  NXRectFill(&bounds);

  PSsetgray(1.0);
  PSnewpath();
  for(i=0; i<100; i+=4) {
    PSmoveto(100-i,100);
    PSlineto(100, i);
    PSlineto(100+i, 100);
    PSlineto(100, 200-i);
    PSlineto(100-i, 100);
  }
  PSclosepath();
  PSstroke();

  return self;
}

/* register the window with the Workspace Manager so that icon messages
   will be sent this way as neccessary. First grab the application's
   speaker, then get a new Listener.  Once the listener is set up, the
   global window number is registered with the workspace manager.*/
- registerWindow
{
    unsigned int windowNum;
    id speaker = [NXApp appSpeaker];
    
    listener = [Listener new];
    if(!speaker || !listener) { /* if we failed to get the application's
				   speaker or an instance of a listener,
				   then warn user and die */
      NXRunAlertPanel("Application Death Warrant",
		      "Could not create new Speaker or Listener Object in
 registerWindow method.", "Quit", NULL, NULL);
      exit(1);
    }
    [listener setDelegate:self]; /* set listener's delegate to this view. */
    [listener privatePort]; /* This causes the listener to not be registered
			       publically-- no application can message to
			       this listener without specifically sending
			       the rights to do so */
    [listener addPort]; /* Adds the port to the list of ports that can
			   receive methods asynchronously */
    NXConvertWinNumToGlobal([window windowNum], &windowNum);
    [speaker setSendPort:NXPortFromName(NX_WORKSPACEREQUEST, NULL)];
    [speaker registerWindow:windowNum toPort:[listener listenPort]];

    return self;
}

/* unregister the window and free the listener object */
- unregisterWindow
{
    unsigned int windowNum;
    id speaker = [NXApp appSpeaker];

    if (listener) {
	[speaker setSendPort:NXPortFromName(NX_WORKSPACEREQUEST, NULL)];
	NXConvertWinNumToGlobal([window windowNum], &windowNum);
	[speaker unregisterWindow:windowNum];
	[listener free];
    }

    return self;
}

/* the following four methods deal with the various icon events.  All of
   these methods come from the listener that was set up and registered
   with the WorkSpace Manager in the -registerWindow method.  At the moment,
   nothing useful is done with the information passed back other than
   dumping the data to the various fields in the info window...

   Only the methods you are going to use need be implemented (except for the
   caveat mentioned below).

   Caveat:  If you are going to do anything useful with the file at the other
   end of the icon, you must trap for the -iconEntered message and store the
   pathList somewhere (instance variable, perhaps?)... even if you only care
   about the -iconReleasedAt stuff.
   */

/* handle iconEntered-- iconEntered is sent when an icon first
   enters the target area. */
- (int)iconEntered:(int)windowNum at:(double)x :(double)y
    iconWindow:(int)iconWindowNum iconX:(double)iconX iconY:(double)iconY
    iconWidth:(double)iconWidth iconHeight:(double)iconHeight
    pathList:(char *)pathList
{
  char s[100];

  [pathField setStringValue: pathList];
  sprintf(s, "%d", windowNum);
  [windowNumField setStringValue:s];
  sprintf(s, "%d", iconWindowNum);
  [iconWindowField setStringValue:s];
  sprintf(s, "%.1f", iconWidth);
  [iconWidthField setStringValue:s];
  sprintf(s, "%.1f", iconHeight);
  [iconHeightField setStringValue:s];
  sprintf(s, "(%.1f, %.1f)\n", x, y);
  [windowCoordField setStringValue:s];
  sprintf(s, "(%.1f, %.1f)\n", iconX, iconY);
  [iconCoordField setStringValue:s];

  return 0;
}

/* handle iconReleasedAt-- iconReleased is sent with the coordinates of
   where the icon was dropped within the target area. */
- (int)iconReleasedAt:(double)x :(double)y ok:(int *)flag
{
  char s[100];
  
  sprintf(s, "(%.1f, %.1f)\n", x, y);
  [iconReleasedField setStringValue:s];
  *flag = 0; /* since we don't do anything, have the Workspace animate
		or Icon back to it's source */
  return 0;
}

/* handle iconExitedAt-- iconExitedAt is sent when the icon leaves the
   targeted area */
- (int)iconExitedAt:(double)x :(double)y
{
  char s[100];
  
  sprintf(s, "(%.1f, %.1f)\n", x, y);
  [iconExitedField setStringValue:s];
  return 0;
}

/* handle iconMovedTo-- within the target area, this message is sent every
   time the icon moves. */
- (int)iconMovedTo:(double)x :(double)y
{
  char s[100];
  
  sprintf(s, "(%.1f, %.1f)\n", x, y);
  [iconMovedField setStringValue:s];
  return 0;
}

/* IB methods to set the various field ID's */

- setPathField:anObject { pathField = anObject; return self; }
- setWindowNumField:anObject { windowNumField = anObject; return self; }
- setIconWindowField:anObject { iconWindowField = anObject; return self;}
- setIconHeightField:anObject {  iconHeightField = anObject; return self;}
- setIconWidthField:anObject {  iconWidthField = anObject; return self;}
- setWindowCoordField:anObject { windowCoordField = anObject; return self;}
- setIconCoordField:anObject { iconCoordField = anObject; return self;}
- setIconReleasedField:anObject { iconReleasedField = anObject; return self;}
- setIconExitedField:anObject {  iconExitedField = anObject; return self;}
- setIconMovedField:anObject {  iconMovedField = anObject; return self;}
@end
