/* -*-C-*-
*******************************************************************************
*
* File:         Info.m
* RCS:          /usr/local/sources/CVS/EnhanceMail/Info.m,v 1.7 1998/07/01 21:58:06 tom Exp
* Description:  
* Author:       Carl Edman
* Created:      Fri Oct 13 11:48:05 1995
* Modified:     14-Jun-98 (Tom Hageman) -- incorporate Don Yacktman's EasterEgg.
* Language:     C
* Package:      N/A
* Status:       Experimental (Do Not Distribute)
*
* (C) Copyright 1995, but otherwise this file is perfect freeware.
*
*******************************************************************************
*/

#import "EnhanceMail.h"
#import "Info.h"

#import "Info.subproj/Spewer.h"

static id mod=nil;

@implementation EnhanceInfo

+ finishLoading:(struct mach_header *)header
{
   [self poseAs:[self superclass]];
   return self;
}

- (void)enhanceSetupEasterEgg:(Button *)startButton
{
   char path[MAXPATHLEN+1];

   // Kludge: load image needed by easter egg from bundle.
   if ([EnhanceBundle getPath:path forResource:"minienvelope" ofType:"tiff"])
   {
      NXZone *z = [self zone];
      EnhanceSpewer *spewer;
      NXImage *image = [[NXImage allocFromZone:z] initFromFile:path];

      if (image == nil) return;
      [image setName:"minienvelope.tiff"];

      spewer = [[EnhanceSpewer allocFromZone:z] init];
      [startButton setTarget:spewer];
      [startButton setAction:@selector(start:)];
      [[startButton window] setDelegate:spewer];
   }
}

- (Button *)_enhanceLogo
{
   /* create a(n almost) ready-to-insert button containing our enhance logo.
      Caller should still set frame origin. */
   char path[MAXPATHLEN+1];
   Button *button = nil;

   if ([EnhanceBundle getPath:path forResource:"e" ofType:"tiff"])
   {
      NXZone *z = [self zone];
      NXImage *image = [[NXImage allocFromZone:z] initFromFile:path];
      NXRect frame = {0};

      [image getSize:&frame.size];
      if (NX_WIDTH(&frame) && NX_HEIGHT(&frame))
      {
	 button = [[Button allocFromZone:z] init];
	 [button setBordered:NO];
	 [button setType:NX_MOMENTARYCHANGE];
	 [button setIconPosition:NX_ICONONLY];
	 [button setImage:image];
	 [button sizeToFit];
	 [button setSound:[Sound findSoundFor:"Basso"]];
      }
   }
   return button;
}

- (View *)_enhanceFindTitleView
{
   /* Heuristically find Mail title field: assume it's a subview of
      infoPanel's contentview with -icon named "Mail". */
   List *views = [[infoPanel contentView] subviews];
   int i = [views count];

   while (--i >= 0)
   {
      id view = [views objectAt:i];

      if ([view respondsTo:@selector(icon)])
      {
	 const char *icon = [view icon];

	 if (icon && strcmp(icon, "Mail")==0) return view;
      }
   }
   return nil;
}

- (void)enhanceInsinuateVersion
{
   const char *old;
   char *new;
   NXRect or,nr;

   old=[versionField stringValue];
   [versionField getFrame:&or];
   new=alloca(strlen(old)+3+strlen(EnhanceVersion)+1+1);
   sprintf(new, "%s (e%s)", old, EnhanceVersion);
   [versionField setStringValue:new];
   [versionField sizeToFit];
   [versionField getFrame:&nr];
   [versionField moveBy:(NX_MAXX(&or) - NX_MAXX(&nr)):0];
}

- (void)enhanceInsinuateLogo
{
   /* release field may be before or after Mail title, depending on
      localization.  Insert our logo after the rightmost one. */
   NXRect logoFrame, releaseFrame, titleFrame = {0};
   Button *logo = [self _enhanceLogo];

   if (logo == nil) return;

   [logo getFrame:&logoFrame];

   [releaseField getFrame:&releaseFrame];
   [[self _enhanceFindTitleView] getFrame:&titleFrame];

   if (NX_X(&releaseFrame) > NX_X(&titleFrame))
   {
      // XXX quick-n-dirty hard constants here.
      logoFrame.origin.x = NX_X(&releaseFrame) + 63;
      logoFrame.origin.y = NX_Y(&releaseFrame) + 11;
   }
   else
   {
      logoFrame.origin.x = NX_MAXX(&titleFrame) - 3;
      logoFrame.origin.y = NX_Y(&titleFrame) + 7;
   }
   [logo setFrame:&logoFrame];
   [[infoPanel contentView] addSubview:logo];

   [self enhanceSetupEasterEgg:logo];
}

- enhance
{
   [self enhanceInsinuateVersion];
   [self enhanceInsinuateLogo];
   return self;
}

+ new
{
   id ret=[super new];
   if (mod!=ret) mod=[ret enhance];
   return ret;
}

@end // EnhanceInfo
