/*	MiscTDVTargetAction.m

	Copyright 1996 Uwe Hoffmann.

	This notice may not be removed from this source code.
	The use and distribution of this software is governed by the
	terms of the MiscKit license agreement.  Refer to the license
	document included with the MiscKit distribution for the terms.

	Version 2 (August 1996)
*/

#import "MiscTreeDiagramView.h"
#import "MiscDiagramTree.h"
#import "MiscTreeDiagram.h"
#import "MiscUserPath.h"
#import "MiscHitPath.h"
#import "MiscTDVPrivate.h"
#import "MiscTDViewUtils.h"
#import <MiscFoundation/MiscUndoManager.h>

@implementation MiscTreeDiagramView(TargetAction)

static id pageLayoutPanel = nil;
 
- (void)pageLayout:(id)sender
{
	NSPrintInfo *printInfo = [treeDiagram printInfo];
	NSWindow *window = [self window];

	if(!pageLayoutPanel)
		pageLayoutPanel = [NSPageLayout pageLayout];
	 if([pageLayoutPanel runModalWithPrintInfo:printInfo] == NSOKButton){
		[window disableFlushWindow];
		canvasRect = miscTD_calcCanvas(printInfo);
		[self _internalResizeAndPosTree:[treeDiagram root] force:YES];
		[self display];
		[window enableFlushWindow];
		[window flushWindow];
    	} 
}

- (void)cut:(id)sender
{
	if(!opFlags.changeable)
		return;
	[self _internalCopyToPasteboard:[NSPasteboard generalPasteboard]];
	[self delete:self];
}

- (void)paste:(id)sender
{
	if(!opFlags.changeable)
		return;
	[self _internalPasteFromPasteboard:[NSPasteboard generalPasteboard]];
}

- (void)delete:(id)sender
{
	if(!opFlags.changeable)
		return;
	[treeDiagram deleteSelected];
}

- (void)copy:(id)sender
{
	[self _internalCopyToPasteboard:[NSPasteboard generalPasteboard]];
}

- (void)undo:(id)sender
{
    	[[treeDiagram undoManager] undo];
	return; 
}

- (void)redo:(id)sender
{
    	[[treeDiagram undoManager] redo];
	return; 
}

- (void)freeUndoBuffer:(id)sender
{
	return;
}

- (void)copyStyle:(id)sender
{
	return;
}

- (void)pasteStyle:(id)sender
{
	if(!opFlags.editable)
		return;
	return; 
}

- (void)promoteStyle:(id)sender
{
	return;
}

- (void)toggleRuler:(id)sender
{
    	NSScrollView *sv;

        sv = [self enclosingScrollView];
        if(sv){
            [sv setHasHorizontalRuler:YES];
            [sv setHasVerticalRuler:YES];
            [sv setRulersVisible:![sv rulersVisible]];
            [[sv horizontalRulerView] setClientView:self];
            [[sv verticalRulerView] setClientView:self];
        }
        return;
}

- (void)toggleAttachments:(id)sender
{
	return;
}

- (void)collapseExpand:(id)sender
{
	
    	MiscDiagramTree *st = [treeDiagram selectedNode];

    	if(!opFlags.changeable)
		return;
        if(st)
            	[treeDiagram toggleCollapseNode:st];
        return;
}

- (void)findNext:(id)sender
{
	return;
}

- (void)findPrevious:(id)sender
{
	return;
}

- (void)findNextAndOrderFindPanelOut:(id)sender
{
	return;
}

- (void)replace:(id)sender
{
	return;
}

- (void)replaceAndFind:(id)sender
{
	return;
}

- (void)replaceAll:(id)sender
{
	return;
}

- (void)orderFrontFindPanel:(id)sender
{
	return;
}

- (BOOL)validateMenuItem:(NSMenuItem *)anItem
{
    	NSString *menuTitle = [anItem title];
    	MiscDiagramTree *st = [treeDiagram selectedNode];
    
    	if([menuTitle isEqualToString:@"Collapse"] || [menuTitle isEqualToString:@"Expand"]){
        	if(!st)
            		return NO;
        	if([st isCollapsed] && [menuTitle isEqualToString:@"Collapse"]){
            		[anItem setTitle:@"Expand"];
        		return YES;
        	}
        	if(![st isCollapsed] && [menuTitle isEqualToString:@"Expand"]){
            		[anItem setTitle:@"Collapse"];
                	return YES;
        	}
    	}
    	if([menuTitle hasPrefix:@"Undo"]){
        	MiscUndoManager *undoManager = [treeDiagram undoManager];

        	if([undoManager numberOfUndos] == 0){
            		[anItem setTitle:@"Undo"];
            		return NO;
    		}
        	[anItem setTitle:[undoManager undoName]];
        	return YES;
    	}
    	if([menuTitle hasPrefix:@"Redo"]){
        	MiscUndoManager *undoManager = [treeDiagram undoManager];

        	if([undoManager numberOfRedos] == 0){
            		[anItem setTitle:@"Redo"];
                	return NO;
        	}
        	[anItem setTitle:[undoManager redoName]];
        	return YES;
    	}
    	return YES;
}

@end

