/*	MiscZoomScrollView.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 <Foundation/Foundation.h>
#import <AppKit/AppKit.h>

#import "MiscZoomScrollView.h"
#import "MiscZooming.h"

#define CLAMP(a,x,y) (MAX((x), MIN((y), (a))))

@implementation MiscZoomScrollView
/*"The MiscZoomScrollView class defines a scrollview that can zoom its document view if the document view implements
the MiscZooming protocol. It gives the user access to zooming with a zoom popup button.

You can use this class in IB by choosing "Group in ScrollView" and changing the class of the scrollview
to MiscZoomScrollView in the "Custom class" inspector."*/

- initWithFrame:(NSRect)frameRect
{
        [super initWithFrame:frameRect];
        zoomButton = nil;    	
    	return self;
}

- (void)awakeFromNib
{
    	NSString *filename;
    	NSBundle *bundle;
        
    	if(!zoomButton){
                bundle = [NSBundle bundleForClass:[self class]];
                filename = [bundle pathForResource:@"MiscZoomControls" ofType:@"nib"];
                [NSBundle loadNibFile:filename externalNameTable:
                    [NSDictionary dictionaryWithObjectsAndKeys:self, @"NSOwner", nil]
                                        withZone:[self zone]];
                [self addSubview:zoomButton];
                [self tile];
        }
}

- (void)zoom:sender
{
        NSString *title;
        float scale;
	NSWindow *window = [self window];
	BOOL updateControls = NO;
	NSView <MiscZooming> *docView = [self documentView];
        int zoom;
        NSArray *itemTitles;
        int index, numberOfTitles;

	if(![docView conformsToProtocol:@protocol(MiscZooming)])
		return;
	[window disableFlushWindow];
        title = [[zoomButton selectedCell] title];
        if([title isEqualToString:@"Fit"]){
            	[docView zoomToFit];
		updateControls = YES;
        } else if([title isEqualToString:@"Set..."]){
                [zoomText selectText:self];
                [zoomPanel makeKeyAndOrderFront:self];
                [NSApp runModalForWindow:zoomPanel];
                [zoomPanel close];
                scale = CLAMP([zoomText intValue],10,1600);
                [docView zoomToFactor:scale / 100.0];
		updateControls = YES;
        } else {
        	[docView zoomToFactor:[title intValue] / 100.0];
	}
	if(updateControls){
            	zoom = (int)([docView zoomFactor] * 100);
            	title = [NSString stringWithFormat:@"%d \%", zoom];
            	itemTitles = [zoomButton itemTitles];
            	index = 0;
            	numberOfTitles = [itemTitles count] - 2;
            	while(index < numberOfTitles && zoom > [[itemTitles objectAtIndex:index] intValue])
                	index++;
            	if(index == numberOfTitles)
                	[zoomButton insertItemWithTitle:title atIndex:index];
            	else if(zoom < [[itemTitles objectAtIndex:index] intValue])
                	[zoomButton insertItemWithTitle:title atIndex:index];
            	[zoomButton selectItemAtIndex:index];
        }
	[window enableFlushWindow];
        [window flushWindow];
}

- (void)stopZoomPanel:(id)sender
{
	[NSApp stopModal];    
}

- (NSPopUpButton *)zoomButton
{
	return (NSPopUpButton *)zoomButton;
}

- (void)tile
{
	NSRect zoomButtonRect, hRect;
	NSScroller *hScroller = [self horizontalScroller];

	[super tile];
	zoomButtonRect = [zoomButton frame];
	hRect = [hScroller frame];
	if(hRect.size.width - zoomButtonRect.size.width > 72){
		NSDivideRect(hRect,&zoomButtonRect,&hRect,zoomButtonRect.size.width,NSMaxXEdge);
		[hScroller setFrame:hRect];
		zoomButtonRect.origin.y += 1;
		zoomButtonRect.size.height -= 2;
		[zoomButton setFrame:zoomButtonRect];
	} else {
            	zoomButtonRect.origin.x = -1000;
                zoomButtonRect.origin.y = -1000;
		[zoomButton setFrame:zoomButtonRect];
        }
}		

@end

