~ubuntu-branches/ubuntu/quantal/viewpdf.app/quantal

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
 * Copyright (C) 2004  Stefan Kleine Stegemann
 *
 * This program 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.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#import "CenteringClipView.h"


@implementation CenteringClipView

- (id) initWithFrame: (NSRect)aFrame
{
   return [super initWithFrame: aFrame];
}

- (void) centerDocumentView
{
   NSRect docRect = [[self documentView] frame];
   NSRect clipRect = [self bounds];

   if(docRect.size.width < clipRect.size.width)
   {
      clipRect.origin.x = (docRect.size.width - clipRect.size.width) / 2.0;
   }

   if(docRect.size.height < clipRect.size.height)
   {
      clipRect.origin.y = (docRect.size.height - clipRect.size.height) / 2.0;
   }

   // Probably the most efficient way to move the bounds origin.
   [self scrollToPoint: clipRect.origin];

   // We could use this instead since it allows a scroll view to
   // coordinate scrolling between multiple clip views.
   //[[self superview] scrollClipView:self toPoint:clipRect.origin];
}

// We need to override this so that the superclass doesn't override our new
// origin point.
- (NSPoint) constrainScrollPoint: (NSPoint)proposedNewOrigin
{
   NSRect docRect = [[self documentView] frame];
   NSRect clipRect = [self bounds];
   NSPoint newScrollPoint = proposedNewOrigin;

   float maxX = docRect.size.width - clipRect.size.width;
   float maxY = docRect.size.height - clipRect.size.height;

   // If the clip view is wider than the doc, we can't scroll horizontally
   if(docRect.size.width < clipRect.size.width)
   {
      newScrollPoint.x = maxX / 2.0;
   }
   else
   {
      newScrollPoint.x = MAX(0, MIN(newScrollPoint.x,maxX));
   }

   // If the clip view is taller than the doc, we can't scroll vertically
   if(docRect.size.height < clipRect.size.height)
   {
      newScrollPoint.y = maxY / 2.0;
   }
   else
   {
      newScrollPoint.y = MAX(0, MIN(newScrollPoint.y,maxY));
   }

   return newScrollPoint;
}

- (void) viewBoundsChanged: (NSNotification*)aNotification
{
   [super viewBoundsChanged: aNotification];
   [self centerDocumentView];
}

- (void) viewFrameChanged: (NSNotification*)aNotification
{
   [super viewFrameChanged: aNotification];
   [self centerDocumentView];
}

- (void) setFrame: (NSRect)aFrame
{
   [super setFrame: aFrame];
   [self centerDocumentView];
}

- (void) setFrameOrigin: (NSPoint)aPoint
{
   [super setFrameOrigin: aPoint];
   [self centerDocumentView];
}

- (void) setFrameSize: (NSSize)aSize
{
   [super setFrameSize: aSize];
   [self centerDocumentView];
}

- (void) setFrameRotation: (float)anAngle
{
   [super setFrameRotation: anAngle];
   [self centerDocumentView];
}

@end