//
//  PRWindowController.m
//  PRICE
//
//  Created by Riccardo Mottola on Thu Dec 12 2002.
//  Copyright (c) 2002-2005 Carduus. All rights reserved.
//
// This program is free software; you can redistribute it and/or modify it under the terms of the version 2 of the GNU General Public License as published by the Free Software Foundation.
// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

#import "PRWindowController.h"
#import "MyDocument.h"


@implementation PRWindowController

- (void)scaleFromMenu:(id)sender
{
    int    tag;
    NSSize currViewSize;
    NSSize imgSize;
    float  hscale, vscale;
    
    tag = [[sender selectedItem] tag];
    currViewSize = [[[view enclosingScrollView] contentView] visibleRect].size;
    imgSize = [[[self document] activeImage] size];
    switch (tag)
    {
        case -4:
            [scalePanel makeKeyAndOrderFront:nil];
            break;
        case -3:
            vscale = currViewSize.height / imgSize.height;
            hscale = currViewSize.width / imgSize.width;
            if (hscale > vscale)
                [self scaleImageTo:vscale];
            else
                [self scaleImageTo:hscale];
            break;
        case -2:
            hscale = currViewSize.width / imgSize.width;
            NSLog(@"%f %f", currViewSize.width, imgSize.width);
            [self scaleImageTo:hscale];
            break;
        case -1:
            vscale = currViewSize.height / imgSize.height;
            NSLog(@"%f %f", currViewSize.height, imgSize.height);
            [self scaleImageTo:vscale];
            break;
        case 12:
            [self scaleImageTo:0.125];
            break;
        case 25:
            [self scaleImageTo:0.25];
            break;
        case 50:
            [self scaleImageTo:0.5];
            break;
        case 75:
            [self scaleImageTo:0.75];
            break;
        case 100:
            [self scaleImageTo:1.0];
            break;
        case 150:
            [self scaleImageTo:1.55];
            break;
        case 200:
            [self scaleImageTo:2.0];
            break;
        case 400:
            [self scaleImageTo:4.0];
            break;
        default:
            NSLog(@"unexpected case in scale menu selection");
            break;
    }
}

- (void)scalePanelOk:(id)sender
{
    float percent;

    percent = [scalePanelScaleField floatValue];
    [scalePanel performClose:nil];
    [self scaleImageTo:percent/100.0];
}

- (void)scalePanelCancel:(id)sender
{
    [scalePanel performClose:nil];
}


/* stores the given value as a scale factor */
- (void)scaleImageTo:(float)internal_scale
{
    if (internal_scale > 0)
        scale = internal_scale;
    [self scaleImage];
}

/* scales the image by the current scale factor */
- (void)scaleImage
{
    [view scaleFrameBy:scale];
}

- (void)windowDidLoad
/* some initialization stuff */
{
    NSWindow *currWin;
    NSRect   currentWinContentRect;
    NSSize   currentWindowSize;
    NSSize   currViewSize;
    NSSize   currImageSize;
    NSSize   windowToViewBorder;
    NSSize   newWinSize;
    NSSize   minWinSize;
    NSSize   screenFrameSize;
    PRImage  *currImage;
    BOOL     shouldResize;

    shouldResize = NO;
    
    /* display the data by MyDocument's activeImage method */
    currImage = [[self document] activeImage];
    
    [view setImage:currImage];
    [self scaleImageTo:1.0];

    /* resize the window accordingly to the image's size */
    screenFrameSize = [[NSScreen mainScreen] visibleFrame].size;
    NSLog(@"current screen size: %f x  %f", screenFrameSize.width, screenFrameSize.height);

    /* get the current window size and limits */
    currWin = [self window];
    currentWinContentRect = [NSWindow contentRectForFrameRect:[currWin frame] styleMask:[currWin styleMask]];
    currentWindowSize = currentWinContentRect.size;
    minWinSize = [currWin minSize];
    NSLog(@"current window size: %f x  %f", currentWindowSize.width, currentWindowSize.height);
    NSLog(@"min window size: %f x  %f", minWinSize.width, minWinSize.height);
    newWinSize = currentWindowSize;

    /* get size of the visible part of the view */
    currViewSize = [[view enclosingScrollView] contentSize];

    /* calculate the difference that makes up the border */
    windowToViewBorder.width = currentWindowSize.width - currViewSize.width;
    windowToViewBorder.height = currentWindowSize.height - currViewSize.height;

    /* get the image size */
    currImageSize = [currImage size];
    NSLog(@"current image size: %f x  %f", currImageSize.width, currImageSize.height);

    /* resize if image is smaller */
    if (currImageSize.width < currViewSize.width)
    {
        float newViewWidth;

        shouldResize = YES;
        if (currImageSize.width > minWinSize.width)
            newViewWidth = currImageSize.width;
        else
            newViewWidth = minWinSize.width;
        newWinSize.width = newViewWidth + windowToViewBorder.width;
    }
    if (currImageSize.height < currViewSize.height)
    {
        float newViewHeight;

        shouldResize = YES;
        if (currImageSize.height > minWinSize.height)
            newViewHeight = currImageSize.height;
        else
            newViewHeight = minWinSize.height;
        newWinSize.height = newViewHeight + windowToViewBorder.height;
    }

    /* resize if image is larger */
    if ([[NSApp delegate] prefEnlargeWindows])
    {
        if (currImageSize.width > currViewSize.width)
        {
            float newViewWidth;

            shouldResize = YES;
            if (currImageSize.width < screenFrameSize.width)
                newViewWidth = currImageSize.width;
            else
                newViewWidth = screenFrameSize.width - currentWinContentRect.origin.x - windowToViewBorder.width;
            newWinSize.width = newViewWidth + windowToViewBorder.width;
        }
        if (currImageSize.height > currViewSize.height)
        {
            float newViewHeight;

            shouldResize = YES;
            if (currImageSize.height < screenFrameSize.height)
                newViewHeight = currImageSize.height;
            else
                newViewHeight = screenFrameSize.height - windowToViewBorder.height;
            newWinSize.height = newViewHeight + windowToViewBorder.height;
        }
    }

    if (shouldResize)
    {
        [currWin setContentSize:newWinSize];
    }
    
    /* update the information */
    [self setImageInfo:currImage];
}

- (NSView *)view

/* view accessor */
{
    return view;
}


- (void)setImageToDraw:(PRImage *)image
{    
    [view setImage:image];
    [self setImageInfo:image];
}

- (void)setImageInfo:(PRImage *)image
{   
    [imageInfoLine setStringValue: [NSString stringWithFormat:
            @"%d x %d, %d bpp",
            [image width], [image height], [image bitsPP]
        ]];
}

@end
