1//
2//  PRImageView.m
3//  PRICE
4//
5//  Created by Riccardo Mottola on Thu Dec 12 2002.
6//  Copyright (c) 2002-2015 Carduus. All rights reserved.
7//
8// This application is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
9// 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.
10
11#import "PRImageView.h"
12#import "PRImage.h" // defines NSInteger
13
14
15@implementation PRImageView
16
17- (void)awakeFromNib
18{
19    NSMutableArray *dragTypes;
20
21    /* init copy&paste */
22    dragTypes = [NSMutableArray arrayWithObjects:NSColorPboardType, NSFilenamesPboardType, nil];
23    [dragTypes addObjectsFromArray:[NSImage imagePasteboardTypes]];
24    [self registerForDraggedTypes:dragTypes];
25}
26
27- (void)drawRect:(NSRect)rect
28{
29    if (![[NSGraphicsContext currentContext] isDrawingToScreen])
30    {
31        NSSize imageSize;
32        NSRect finalRect;
33        float scaleX, scaleY, scale;
34
35
36        imageSize = [[self image] size];
37
38        scaleX = rect.size.width / imageSize.width;
39        scaleY = rect.size.height / imageSize.height;
40
41
42        if (scaleX < scaleY)
43            scale = scaleX;
44        else
45            scale = scaleY;
46
47        finalRect = NSMakeRect(rect.origin.x, rect.origin.y, imageSize.width*scale, imageSize.height*scale);
48
49        [[self image] drawInRect:finalRect  fromRect:NSMakeRect(0, 0, imageSize.width, imageSize.height) operation:(NSCompositingOperation)NSCompositeCopy fraction:(float)1.0];
50    } else
51    {
52        [super drawRect:rect];
53    }
54}
55
56- (void)scaleFrameBy:(float)scale
57{
58    NSSize imageSize;
59    NSAffineTransform *at;
60
61    if (scaleFactor == scale)
62      return;
63
64    scaleFactor = scale;
65
66    imageSize = [[self image] size];
67    at = [NSAffineTransform transform];
68    [at scaleBy:scale];
69
70    [self setFrameSize:[at transformSize:imageSize]];
71    [self setNeedsDisplay:YES];
72}
73
74/* We override setImage so we can invalidate and recalculate scaling.
75   This is necesssary since scaleFrameBy is optimized not to recalculate if the same factor is given.
76*/
77- (void)setImage:(NSImage *)image
78{
79  float oldScaleFactor;
80
81  [super setImage:image];
82
83  oldScaleFactor = scaleFactor;
84  scaleFactor = 0;
85  [self scaleFrameBy:oldScaleFactor];
86}
87
88- (BOOL) knowsPageRange: (NSRangePointer) range
89{
90    range->location = 1;
91    range->length = 1;
92
93    return YES;
94}
95
96
97- (NSRect) rectForPage: (NSInteger) pageNumber
98{
99    NSPrintInfo *pi = [[[NSDocumentController sharedDocumentController] currentDocument] printInfo];
100    return [pi imageablePageBounds];
101}
102
103
104
105-(void) dealloc
106{
107    [super dealloc];
108}
109
110@end
111