
/* MenuController.m - written by Robert Vasvari 6/94
   this class is created solely for the purpose of dealing with
   menus */

#import "MenuController.h"

@implementation MenuController

	
- appDidInit:sender
{
id  matrix;

	[super appDidInit:sender];
	
	/* take care of the document menu */
	matrix=[documentMenu itemList];
	[[matrix cellAt:2 :0] //save
		setUpdateAction:@selector(updateDocMenus:) forMenu:documentMenu];
	[[matrix cellAt:3 :0] //saveAs
		setUpdateAction:@selector(updateDocMenus:) forMenu:documentMenu];
	[[matrix cellAt:4 :0] //SaveTo
		setUpdateAction:@selector(updateDocMenus:) forMenu:documentMenu];
	[[matrix cellAt:5 :0] //Revert
		setUpdateAction:@selector(updateDocMenus:) forMenu:documentMenu];
	[[matrix cellAt:6 :0] //Close
		setUpdateAction:@selector(updateDocMenus:) forMenu:documentMenu];

	/* take care of the window menu */
    [windowsMiniaturize setUpdateAction:
	     @selector(updateWindowsMiniaturize:) forMenu:[NXApp windowsMenu]];
    [windowsClose setUpdateAction:
		 @selector(updateWindowsClose:) forMenu:[NXApp windowsMenu]];

/*    matrix=[toolsMenu itemList];
	[[matrix cellAt:0 :0] setUpdateAction:
		 @selector(updateDocMenus:) forMenu:toolsMenu];
    [[matrix cellAt:1 :0] setUpdateAction:
		 @selector(updateDocMenus:) forMenu:toolsMenu];
    [[matrix cellAt:2 :0] setUpdateAction:
		 @selector(updateDocMenus:) forMenu:toolsMenu];
    [[matrix cellAt:3 :0] setUpdateAction:
		 @selector(updateDocMenus:) forMenu:toolsMenu];
    [[matrix cellAt:4 :0] setUpdateAction:
		 @selector(updateDocMenus:) forMenu:toolsMenu];
*/


    /* this will fix all of the edit menus */
    matrix=[editMenu itemList];
	[[matrix cellAt:0 :0] setUpdateAction:
		 @selector(updateEditMenus:) forMenu:editMenu];
	[[matrix cellAt:1 :0] setUpdateAction:
		 @selector(updateEditMenus:) forMenu:editMenu];
	[[matrix cellAt:2 :0] setUpdateAction:
		 @selector(updateEditMenus:) forMenu:editMenu];
	[[matrix cellAt:3 :0] setUpdateAction:
		 @selector(updateNoteEditMenus:) forMenu:editMenu];
	[[matrix cellAt:4 :0] setUpdateAction:
		 @selector(updateNoteEditMenus:) forMenu:editMenu];

	return self;
}


/* menu update methods */

- (BOOL)updateDocMenus:aMenuCell
{	if(currentDoc)
	 { 	if(![aMenuCell isEnabled])
	      { [aMenuCell setEnabled:YES];
		    return YES;
		  }
	 }
	else
	 {	if([aMenuCell isEnabled])
	      { [aMenuCell setEnabled:NO];
		    return YES;
		  }
	 }
	return NO;
}

- (BOOL)updateWindowsMiniaturize:aMenuCell
{
    id t;

    t = [NXApp calcTargetForAction:[aMenuCell action]];
    if (!t || ([t buttonMask]&NX_MINIATURIZEBUTTONMASK)==0) {
	if ([aMenuCell isEnabled]) {
	    [aMenuCell setEnabled:NO];
	    return YES;
	}
    }
    else {
	if (![aMenuCell isEnabled]) {
	    [aMenuCell setEnabled:YES];
	    return YES;
	}
    }
    return NO;
}

- (BOOL)updateWindowsClose:aMenuCell
{
    id t;

    t = [NXApp calcTargetForAction:[aMenuCell action]];
    if (!t || ([t buttonMask]&NX_CLOSEBUTTONMASK)==0) {
	if ([aMenuCell isEnabled]) {
	    [aMenuCell setEnabled:NO];
	    return YES;
	}
    }
    else {
	if (![aMenuCell isEnabled]) {
	    [aMenuCell setEnabled:YES];
	    return YES;
	}
    }
    return NO;
}

- (BOOL)updateEditMenus:aMenuCell
{	
	if([[[NXApp keyWindow] firstResponder] respondsTo:@selector(paste:)])
	     { if(![aMenuCell isEnabled]) 
		     { [aMenuCell setEnabled:YES];
		       return YES;
			 }
		 }
	else
		 { if([aMenuCell isEnabled]) 
		     { [aMenuCell setEnabled:NO];
		       return YES;
			 }
		 } 
	  
	return NO;
}

- (BOOL)isDeleteEnabled
{	return [editDelete isEnabled];
}


@end
