Revision: 2699
          http://skim-app.svn.sourceforge.net/skim-app/?rev=2699&view=rev
Author:   hofman
Date:     2007-08-22 07:21:38 -0700 (Wed, 22 Aug 2007)

Log Message:
-----------
Add a category with some convenience geometry functions.

Modified Paths:
--------------
    trunk/PDFPage_SKExtensions.m
    trunk/SKApplicationController.m
    trunk/SKMainWindowController.m
    trunk/SKPDFAnnotationNote.m
    trunk/SKPDFHoverWindow.m
    trunk/SKPDFView.m
    trunk/SKSnapshotWindowController.m
    trunk/Skim.xcodeproj/project.pbxproj

Added Paths:
-----------
    trunk/NSGeometry_SKExtensions.h
    trunk/NSGeometry_SKExtensions.m

Added: trunk/NSGeometry_SKExtensions.h
===================================================================
--- trunk/NSGeometry_SKExtensions.h                             (rev 0)
+++ trunk/NSGeometry_SKExtensions.h     2007-08-22 14:21:38 UTC (rev 2699)
@@ -0,0 +1,111 @@
+//
+//  NSGeometry_SKExtensions.h
+//  Skim
+//
+//  Created by Christiaan Hofman on 8/22/07.
+/*
+ This software is Copyright (c) 2007
+ Christiaan Hofman. All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+
+ - Redistributions of source code must retain the above copyright
+   notice, this list of conditions and the following disclaimer.
+
+ - Redistributions in binary form must reproduce the above copyright
+    notice, this list of conditions and the following disclaimer in
+    the documentation and/or other materials provided with the
+    distribution.
+
+ - Neither the name of Christiaan Hofman nor the names of any
+    contributors may be used to endorse or promote products derived
+    from this software without specific prior written permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#import <Cocoa/Cocoa.h>
+
+
+static inline NSPoint SKIntegralPoint(NSPoint point) {
+    return NSMakePoint(roundf(point.x), roundf(point.y));
+}
+
+static inline NSPoint SKAddPoints(NSPoint aPoint, NSPoint bPoint) {
+    return NSMakePoint(aPoint.x + bPoint.x, aPoint.y + bPoint.y);
+}
+
+static inline NSPoint SKSubstractPoints(NSPoint aPoint, NSPoint bPoint) {
+    return NSMakePoint(aPoint.x - bPoint.x, aPoint.y - bPoint.y);
+}
+
+static inline NSPoint SKBottomLeftPoint(NSRect rect) {
+    return NSMakePoint(NSMinX(rect), NSMinY(rect));
+}
+
+static inline NSPoint SKBottomRightPoint(NSRect rect) {
+    return NSMakePoint(NSMaxX(rect), NSMinY(rect));
+}
+
+static inline NSPoint SKTopLeftPoint(NSRect rect) {
+    return NSMakePoint(NSMinX(rect), NSMaxY(rect));
+}
+
+static inline NSPoint SKTopRightPoint(NSRect rect) {
+    return NSMakePoint(NSMaxX(rect), NSMaxY(rect));
+}
+
+static inline NSPoint SKCenterPoint(NSRect rect) {
+    return NSMakePoint(NSMidX(rect), NSMidY(rect));
+}
+
+static inline NSRect SKRectFromPoints(NSPoint aPoint, NSPoint bPoint) {
+    NSRect rect;
+    rect.origin.x = fmin(aPoint.x, bPoint.x);
+    rect.origin.y = fmin(aPoint.y, bPoint.y);
+    rect.size.width = fmax(aPoint.x, bPoint.x) - NSMinX(rect);
+    rect.size.height = fmax(aPoint.y, bPoint.y) - NSMinY(rect);
+    return rect;
+}
+
+static inline NSRect SKIntegralRectFromPoints(NSPoint aPoint, NSPoint bPoint) {
+    NSRect rect;
+    rect.origin.x = floorf(fmin(aPoint.x, bPoint.x));
+    rect.origin.y = floorf(fmin(aPoint.y, bPoint.y));
+    rect.size.width = ceilf(fmax(aPoint.x, bPoint.x) - NSMinX(rect));
+    rect.size.height = ceilf(fmax(aPoint.y, bPoint.y) - NSMinY(rect));
+    return rect;
+}
+
+static inline NSRect SKRectFromCenterAndPoint(NSPoint center, NSPoint point) {
+    NSRect rect;
+    rect.size.width = 2.0 * fabs(center.x - point.x);
+    rect.size.height = 2.0 * fabs(center.y - point.y);
+    rect.origin.x = center.x - 0.5 * NSWidth(rect);
+    rect.origin.y = center.y - 0.5 * NSHeight(rect);
+    return rect;
+}
+
+static inline NSRect SKRectFromCenterAndSize(NSPoint center, NSSize size) {
+    NSRect rect;
+    rect.origin.x = center.x - 0.5 * size.width;
+    rect.origin.y = center.y - 0.5 * size.height;
+    rect.size = size;
+    return rect;
+}
+
+extern NSPoint SKConstrainPointInRect(NSPoint point, NSRect boundary);
+extern NSRect SKConstrainRect(NSRect rect, NSRect boundary);
+extern NSRect SKIntersectionRect(NSRect rect, NSRect boundary);

Added: trunk/NSGeometry_SKExtensions.m
===================================================================
--- trunk/NSGeometry_SKExtensions.m                             (rev 0)
+++ trunk/NSGeometry_SKExtensions.m     2007-08-22 14:21:38 UTC (rev 2699)
@@ -0,0 +1,80 @@
+//
+//  NSGeometry_SKExtensions.m
+//  Skim
+//
+//  Created by Christiaan Hofman on 8/22/07.
+/*
+ This software is Copyright (c) 2007
+ Christiaan Hofman. All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+
+ - Redistributions of source code must retain the above copyright
+   notice, this list of conditions and the following disclaimer.
+
+ - Redistributions in binary form must reproduce the above copyright
+    notice, this list of conditions and the following disclaimer in
+    the documentation and/or other materials provided with the
+    distribution.
+
+ - Neither the name of Christiaan Hofman nor the names of any
+    contributors may be used to endorse or promote products derived
+    from this software without specific prior written permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#import "NSGeometry_SKExtensions.h"
+
+NSPoint SKConstrainPointInRect(NSPoint point, NSRect boundary){
+    if (point.x < NSMinX(boundary))
+        point.x = NSMinX(boundary);
+    else if (point.x > NSMaxX(boundary))
+        point.x = NSMaxX(boundary);
+    
+    if (point.y < NSMinY(boundary))
+        point.y = NSMinY(boundary);
+    else if (point.y > NSMaxY(boundary))
+        point.y = NSMaxY(boundary);
+    
+    return point;
+}
+
+NSRect SKConstrainRect(NSRect rect, NSRect boundary) {
+    if (NSWidth(rect) > NSWidth(boundary))
+        rect.size.width = NSWidth(boundary);
+    if (NSHeight(rect) > NSHeight(boundary))
+        rect.size.height = NSHeight(boundary);
+    
+    if (NSMinX(rect) < NSMinX(boundary))
+        rect.origin.x = NSMinX(boundary);
+    else if (NSMaxX(rect) > NSMaxX(boundary))
+        rect.origin.x = NSMaxX(boundary) - NSWidth(rect);
+    
+    if (NSMinY(rect) < NSMinY(boundary))
+        rect.origin.y = NSMinY(boundary);
+    else if (NSMaxY(rect) > NSMaxY(boundary))
+        rect.origin.y = NSMaxY(boundary) - NSHeight(rect);
+    
+    return rect;
+}
+
+NSRect SKIntersectionRect(NSRect rect, NSRect boundary) {
+    float minX = fmin(fmax(NSMinX(rect), NSMinX(boundary)), NSMaxX(boundary));
+    float maxX = fmax(fmin(NSMaxX(rect), NSMaxX(boundary)), NSMinX(boundary));
+    float minY = fmin(fmax(NSMinY(rect), NSMinY(boundary)), NSMaxY(boundary));
+    float maxY = fmax(fmin(NSMaxY(rect), NSMaxY(boundary)), NSMinY(boundary));
+    return NSMakeRect(minX, minY, maxX - minX, maxY - minY);
+}

Modified: trunk/PDFPage_SKExtensions.m
===================================================================
--- trunk/PDFPage_SKExtensions.m        2007-08-22 09:56:30 UTC (rev 2698)
+++ trunk/PDFPage_SKExtensions.m        2007-08-22 14:21:38 UTC (rev 2699)
@@ -44,6 +44,7 @@
 #import "OBUtilities.h"
 #import "NSBitmapImageRep_SKExtensions.h"
 #import "SKStringConstants.h"
+#import "NSGeometry_SKExtensions.h"
 
 NSString *SKPDFDocumentPageBoundsDidChangeNotification = 
@"SKPDFDocumentPageBoundsDidChangeNotification";
 
@@ -84,8 +85,7 @@
         } else if (NSEqualRects(NSZeroRect, r)) {
             r = NSMakeRect(NSMidX(b), NSMidY(b), 0.0, 0.0);
         } else {
-            r.origin.x += NSMinX(b);
-            r.origin.y += NSMinY(b);
+            r.origin = SKAddPoints(r.origin, b.origin);
         }
         [imageRep release];
         r = NSIntersectionRect(NSInsetRect(r, -marginWidth, -marginHeight), b);
@@ -165,8 +165,7 @@
         scaleX = scaleY = 1.0;
     }
     
-    readingBarRect.origin.x -= NSMinX(bounds);
-    readingBarRect.origin.y -= NSMinY(bounds);
+    readingBarRect.origin = SKSubstractPoints(readingBarRect.origin, 
bounds.origin);
     
     image = [[NSImage alloc] initWithSize:thumbnailSize];
     [image lockFocus];
@@ -233,7 +232,7 @@
                 continue;
             
             NSRect r = [self characterBoundsAtIndex:j];
-            PDFSelection *s = [self 
selectionForLineAtPoint:NSMakePoint(NSMidX(r), NSMidY(r))];
+            PDFSelection *s = [self selectionForLineAtPoint:SKCenterPoint(r)];
             unsigned k, kMax = [s safeNumberOfRangesOnPage:self];
             BOOL notEmpty = NO;
             

Modified: trunk/SKApplicationController.m
===================================================================
--- trunk/SKApplicationController.m     2007-08-22 09:56:30 UTC (rev 2698)
+++ trunk/SKApplicationController.m     2007-08-22 14:21:38 UTC (rev 2699)
@@ -56,6 +56,7 @@
 #import "NSURL_SKExtensions.h"
 #import "SKDocumentController.h"
 #import "Files_SKExtensions.h"
+#import "NSGeometry_SKExtensions.h"
 
 
 @implementation SKApplicationController
@@ -616,7 +617,7 @@
 
 - (void)drawRect:(NSRect)rect {
     NSRect bounds = [self bounds];
-    NSPoint center = NSMakePoint(NSMidX(bounds), NSMidY(bounds));
+    NSPoint center = SKCenterPoint(bounds);
     
     [[NSColor colorWithCalibratedWhite:0.0 alpha:0.5] setFill];
     [NSBezierPath fillRoundRectInRect:[self bounds] radius:10.0];

Modified: trunk/SKMainWindowController.m
===================================================================
--- trunk/SKMainWindowController.m      2007-08-22 09:56:30 UTC (rev 2698)
+++ trunk/SKMainWindowController.m      2007-08-22 14:21:38 UTC (rev 2699)
@@ -78,6 +78,7 @@
 #import "SKStatusBar.h"
 #import "SKTransitionController.h"
 #import "SKTypeSelectHelper.h"
+#import "NSGeometry_SKExtensions.h"
 
 #define SEGMENTED_CONTROL_HEIGHT    25.0
 #define WINDOW_X_DELTA              0.0
@@ -1568,14 +1569,7 @@
             rect.origin.x = NSMaxX(rect) - size.width;
         rect.origin.y = NSMaxY(rect) - size.height;
         rect.size = size;
-        if (NSMaxX(rect) > NSMaxX(bounds))
-            rect.origin.x = NSMaxX(bounds) - NSWidth(rect);
-        if (NSMinX(rect) < NSMinX(bounds))
-            rect.origin.x = NSMinX(bounds);
-        if (NSMaxY(rect) > NSMaxY(bounds))
-            rect.origin.y = NSMaxY(bounds) - NSHeight(rect);
-        if (NSMinY(rect) < NSMinY(bounds))
-            rect.origin.y = NSMinY(bounds);
+        rect = SKConstrainRect(rect, bounds);
         [rectArray addObject:[NSValue valueWithRect:rect]];
         if (i && i % 10 == 0) {
             [progressBar incrementBy:1.0];
@@ -1674,7 +1668,6 @@
     }
     
     PDFDisplayMode displayMode = [pdfView displayMode];
-    NSRect screenFrame = [[[self window] screen] visibleFrame];
     NSRect frame = [splitView frame];
     NSRect documentRect = [[[self pdfView] documentView] convertRect:[[[self 
pdfView] documentView] bounds] toView:nil];
     float bottomOffset = -1.0;
@@ -1704,14 +1697,7 @@
     
     frame.origin = [[self window] convertBaseToScreen:[[[self window] 
contentView] convertPoint:frame.origin toView:nil]];
     frame = [[self window] frameRectForContentRect:frame];
-    if (frame.size.width > NSWidth(screenFrame))
-        frame.size.width = NSWidth(screenFrame);
-    if (frame.size.height > NSHeight(screenFrame))
-        frame.size.height = NSHeight(screenFrame);
-    if (NSMaxX(frame) > NSMaxX(screenFrame))
-        frame.origin.x = NSMaxX(screenFrame) - NSWidth(frame);
-    if (NSMaxY(frame) > NSMaxY(screenFrame))
-        frame.origin.y = NSMaxY(screenFrame) - NSHeight(frame);
+    frame = SKConstrainRect(frame, [[[self window] screen] visibleFrame]);
     
     [[self window] setFrame:frame display:[[self window] isVisible]];
 }

Modified: trunk/SKPDFAnnotationNote.m
===================================================================
--- trunk/SKPDFAnnotationNote.m 2007-08-22 09:56:30 UTC (rev 2698)
+++ trunk/SKPDFAnnotationNote.m 2007-08-22 14:21:38 UTC (rev 2699)
@@ -43,6 +43,7 @@
 #import "SKPDFView.h"
 #import "OBUtilities.h"
 #import "NSUserDefaultsController_SKExtensions.h"
+#import "NSGeometry_SKExtensions.h"
 
 enum {
     SKASTextNote = 'NTxt',
@@ -223,7 +224,7 @@
 
 - (PDFDestination *)destination{
     NSRect bounds = [self bounds];
-    NSPoint point = NSMakePoint(NSMinX(bounds), NSMaxY(bounds));
+    NSPoint point = SKTopLeftPoint(bounds);
     return [[[PDFDestination alloc] initWithPage:[self page] atPoint:point] 
autorelease];
 }
 
@@ -742,10 +743,11 @@
 
 static NSArray *createQuadPointsWithBounds(const NSRect bounds, const NSPoint 
origin)
 {
-    NSPoint p0 = NSMakePoint(NSMinX(bounds) - origin.x, NSMaxY(bounds) - 
origin.y);
-    NSPoint p1 = NSMakePoint(NSMaxX(bounds) - origin.x, NSMaxY(bounds) - 
origin.y);
-    NSPoint p2 = NSMakePoint(NSMinX(bounds) - origin.x, NSMinY(bounds) - 
origin.y);
-    NSPoint p3 = NSMakePoint(NSMaxX(bounds) - origin.x, NSMinY(bounds) - 
origin.y);
+    NSRect r = NSOffsetRect(bounds, -origin.x, -origin.y);
+    NSPoint p0 = SKTopLeftPoint(r);
+    NSPoint p1 = SKTopRightPoint(r);
+    NSPoint p2 = SKBottomLeftPoint(r);
+    NSPoint p3 = SKBottomRightPoint(r);
     return [[NSArray alloc] initWithObjects:[NSValue valueWithPoint:p0], 
[NSValue valueWithPoint:p1], [NSValue valueWithPoint:p2], [NSValue 
valueWithPoint:p3], nil];
 }
 
@@ -928,9 +930,7 @@
         NSRect lineRect;
         lineRect.size.height = points[1].y - points[2].y;
         lineRect.size.width = points[1].x - points[2].x;
-        lineRect.origin = points[2];
-        lineRect.origin.x += [self bounds].origin.x;
-        lineRect.origin.y += [self bounds].origin.y;
+        lineRect.origin = SKAddPoints(points[2], [self bounds].origin);
         [self addLineRect:lineRect];
     }
 }
@@ -1415,20 +1415,20 @@
     NSPoint startPoint = [self startPoint];
     NSPoint endPoint = [self endPoint];
     
+    point = SKSubstractPoints(point, bounds.origin);
+    
     if ([super hitTest:point]) {
-        NSPoint relPoint = NSMakePoint(endPoint.x - startPoint.x, endPoint.y - 
startPoint.y);
+        NSPoint relPoint = SKSubstractPoints(endPoint, startPoint);
         float lengthSquared = relPoint.x * relPoint.x + relPoint.y * 
relPoint.y;
         float extProduct;
         
         if (lengthSquared < 16.0)
             return YES;
         
-        extProduct = (point.x - NSMinX(bounds) - startPoint.x) * relPoint.y - 
(point.y - NSMinY(bounds) - startPoint.y) * relPoint.x;
+        extProduct = (point.x - startPoint.x) * relPoint.y - (point.y - 
startPoint.y) * relPoint.x;
         
         return extProduct * extProduct < 16.0 * lengthSquared;
     } else {
-        point.x -= NSMinX(bounds);
-        point.y -= NSMinY(bounds);
         
         return (fabs(point.x - startPoint.x) < 3.5 && fabs(point.y - 
startPoint.y) < 3.5) ||
                (fabs(point.x - endPoint.x) < 3.5 && fabs(point.y - endPoint.y) 
< 3.5);
@@ -1464,14 +1464,9 @@
         NSPoint startPoint = NSPointFromPoint(*qdPoint);
         
         NSRect bounds = [self bounds];
-        NSPoint endPoint = [self endPoint];
-        endPoint.x = roundf(endPoint.x + NSMinX(bounds));
-        endPoint.y = roundf(endPoint.y + NSMinY(bounds));
+        NSPoint endPoint = SKIntegralPoint(SKAddPoints([self endPoint], 
bounds.origin));
         
-        bounds.origin.x = floorf(fmin(startPoint.x, endPoint.x));
-        bounds.size.width = ceilf(fmax(endPoint.x, startPoint.x)) - 
NSMinX(bounds);
-        bounds.origin.y = floorf(fmin(startPoint.y, endPoint.y));
-        bounds.size.height = ceilf(fmax(endPoint.y, startPoint.y)) - 
NSMinY(bounds);
+        bounds = SKIntegralRectFromPoints(startPoint, endPoint);
         
         if (NSWidth(bounds) < 8.0) {
             bounds.size.width = 8.0;
@@ -1482,10 +1477,8 @@
             bounds.origin.y = floorf(0.5 * (startPoint.y + endPoint.y) - 4.0);
         }
         
-        startPoint.x -= NSMinX(bounds);
-        startPoint.y -= NSMinY(bounds);
-        endPoint.x -= NSMinX(bounds);
-        endPoint.y -= NSMinY(bounds);
+        startPoint = SKSubstractPoints(startPoint, bounds.origin);
+        endPoint = SKSubstractPoints(endPoint, bounds.origin);
         
         [self setBounds:bounds];
         [self setStartPoint:startPoint];
@@ -1496,9 +1489,9 @@
 
 - (NSData *)startPointAsQDPoint {
     NSRect bounds = [self bounds];
-    NSPoint startPoint = [self startPoint];
-    startPoint.x = floorf(startPoint.x + NSMinX(bounds));
-    startPoint.y = floorf(startPoint.y + NSMinY(bounds));
+    NSPoint startPoint = SKAddPoints([self startPoint], bounds.origin);
+    startPoint.x = floorf(startPoint.x);
+    startPoint.y = floorf(startPoint.y);
     Point qdPoint = PointFromNSPoint(startPoint);
     return [NSData dataWithBytes:&qdPoint length:sizeof(Point)];
 }
@@ -1509,14 +1502,9 @@
         NSPoint endPoint = NSPointFromPoint(*qdPoint);
         
         NSRect bounds = [self bounds];
-        NSPoint startPoint = [self startPoint];
-        startPoint.x = roundf(startPoint.x + NSMinX(bounds));
-        startPoint.y = roundf(startPoint.y + NSMinY(bounds));
+        NSPoint startPoint = SKIntegralPoint(SKAddPoints([self startPoint], 
bounds.origin));
         
-        bounds.origin.x = floorf(fmin(startPoint.x, endPoint.x));
-        bounds.size.width = ceilf(fmax(endPoint.x, startPoint.x)) - 
NSMinX(bounds);
-        bounds.origin.y = floorf(fmin(startPoint.y, endPoint.y));
-        bounds.size.height = ceilf(fmax(endPoint.y, startPoint.y)) - 
NSMinY(bounds);
+        bounds = SKIntegralRectFromPoints(startPoint, endPoint);
         
         if (NSWidth(bounds) < 8.0) {
             bounds.size.width = 8.0;
@@ -1527,10 +1515,8 @@
             bounds.origin.y = floorf(0.5 * (startPoint.y + endPoint.y) - 4.0);
         }
         
-        startPoint.x -= NSMinX(bounds);
-        startPoint.y -= NSMinY(bounds);
-        endPoint.x -= NSMinX(bounds);
-        endPoint.y -= NSMinY(bounds);
+        startPoint = SKSubstractPoints(startPoint, bounds.origin);
+        endPoint = SKSubstractPoints(endPoint, bounds.origin);
         
         [self setBounds:bounds];
         [self setStartPoint:startPoint];
@@ -1541,9 +1527,9 @@
 
 - (NSData *)endPointAsQDPoint {
     NSRect bounds = [self bounds];
-    NSPoint endPoint = [self endPoint];
-    endPoint.x = floorf(endPoint.x + NSMinX(bounds));
-    endPoint.y = floorf(endPoint.y + NSMinY(bounds));
+    NSPoint endPoint = SKAddPoints([self endPoint], bounds.origin);
+    endPoint.x = floorf(endPoint.x);
+    endPoint.y = floorf(endPoint.y);
     Point qdPoint = PointFromNSPoint(endPoint);
     return [NSData dataWithBytes:&qdPoint length:sizeof(Point)];
 }

Modified: trunk/SKPDFHoverWindow.m
===================================================================
--- trunk/SKPDFHoverWindow.m    2007-08-22 09:56:30 UTC (rev 2698)
+++ trunk/SKPDFHoverWindow.m    2007-08-22 14:21:38 UTC (rev 2699)
@@ -41,6 +41,7 @@
 #import "SKPDFAnnotationNote.h"
 #import "NSBezierPath_BDSKExtensions.h"
 #import "NSParagraphStyle_SKExtensions.h"
+#import "NSGeometry_SKExtensions.h"
 
 #define WINDOW_WIDTH    400.0
 #define WINDOW_HEIGHT   80.0
@@ -172,22 +173,6 @@
     [super orderOut:sender];
 }
 
-static NSRect SKRectFittingRectInRect(NSRect inRect, NSRect outRect) {
-    if (NSWidth(inRect) > NSWidth(outRect))
-        inRect.size.width = NSWidth(outRect);
-    if (NSHeight(inRect) > NSHeight(outRect))
-        inRect.size.height = NSHeight(outRect);
-    if (NSMaxX(inRect) > NSMaxX(outRect) )
-        inRect.origin.x = NSMaxX(outRect) - NSWidth(inRect);
-    if (NSMinX(inRect) < NSMinX(outRect))
-        inRect.origin.x = NSMinX(outRect);
-    if (NSMaxY(inRect) > NSMaxY(outRect))
-        inRect.origin.y = NSMaxY(outRect) - NSHeight(inRect);
-    if (NSMinY(inRect) < NSMinY(outRect))
-        inRect.origin.y = NSMinY(outRect);
-    return inRect;
-}
-
 - (void)showWithTimer:(NSTimer *)aTimer {
     NSPoint thePoint = NSEqualPoints(point, NSZeroPoint) ? [NSEvent 
mouseLocation] : point;
     NSRect contentRect = NSMakeRect(thePoint.x, thePoint.y - WINDOW_OFFSET, 
WINDOW_WIDTH, WINDOW_HEIGHT);
@@ -227,7 +212,7 @@
             
             color = [NSColor controlBackgroundColor];
             
-            sourceRect = SKRectFittingRectInRect(sourceRect, pageImageRect);
+            sourceRect = SKConstrainRect(sourceRect, pageImageRect);
             
             NSDictionary *attrs = [[NSDictionary alloc] 
initWithObjectsAndKeys:labelFont, NSFontAttributeName, color, 
NSForegroundColorAttributeName, [NSParagraphStyle 
defaultClippingParagraphStyle], NSParagraphStyleAttributeName, nil];
             NSAttributedString *labelString = [[NSAttributedString alloc] 
initWithString:[NSString stringWithFormat:NSLocalizedString(@"Page %@", @"Tool 
tip label format"), [page label]] attributes:attrs];
@@ -308,7 +293,7 @@
         
         contentRect.size = [image size];
         contentRect.origin.y -= NSHeight(contentRect);
-        contentRect = SKRectFittingRectInRect(contentRect, [[NSScreen 
mainScreen] visibleFrame]);
+        contentRect = SKConstrainRect(contentRect, [[NSScreen mainScreen] 
visibleFrame]);
         [self setFrame:[self frameRectForContentRect:contentRect] display:NO];
         
         [[imageView enclosingScrollView] setBackgroundColor:color];

Modified: trunk/SKPDFView.m
===================================================================
--- trunk/SKPDFView.m   2007-08-22 09:56:30 UTC (rev 2698)
+++ trunk/SKPDFView.m   2007-08-22 14:21:38 UTC (rev 2699)
@@ -54,6 +54,7 @@
 #import "NSBezierPath_BDSKExtensions.h"
 #import "SKLineWell.h"
 #import <Carbon/Carbon.h>
+#import "NSGeometry_SKExtensions.h"
 
 NSString *SKPDFViewToolModeChangedNotification = 
@"SKPDFViewToolModeChangedNotification";
 NSString *SKPDFViewAnnotationModeChangedNotification = 
@"SKPDFViewAnnotationModeChangedNotification";
@@ -777,22 +778,13 @@
     
         NSData *data = [pboard dataForType:SKSkimNotePboardType];
         NSDictionary *note = [NSKeyedUnarchiver unarchiveObjectWithData:data];
-        NSRect bounds, pageBounds;
+        NSRect bounds;
         
         newAnnotation = [[[PDFAnnotation alloc] initWithDictionary:note] 
autorelease];
         bounds = [newAnnotation bounds];
         page = [self currentPage];
-        pageBounds = [page boundsForBox:[self displayBox]];
+        bounds = SKConstrainRect(bounds, [page boundsForBox:[self 
displayBox]]);
         
-        if (NSMaxX(bounds) > NSMaxX(pageBounds))
-            bounds.origin.x = NSMaxX(pageBounds) - NSWidth(bounds);
-        if (NSMinX(bounds) < NSMinX(pageBounds))
-            bounds.origin.x = NSMinX(pageBounds);
-        if (NSMaxY(bounds) > NSMaxY(pageBounds))
-            bounds.origin.y = NSMaxY(pageBounds) - NSHeight(bounds);
-        if (NSMinY(bounds) < NSMinY(pageBounds))
-            bounds.origin.y = NSMinY(pageBounds);
-        
         [newAnnotation setBounds:bounds];
         
     } else {
@@ -808,27 +800,17 @@
         if (page == nil) {
             // Get center of the PDFView.
             NSRect viewFrame = [self frame];
-            center = NSMakePoint(NSMidX(viewFrame), NSMidY(viewFrame));
+            center = SKCenterPoint(viewFrame);
             page = [self pageForPoint: center nearest: YES];
         }
                
                // Convert to "page space".
-               center = [self convertPoint: center toPage: page];
-        center.x = roundf(center.x);
-        center.y = roundf(center.y);
+               center = SKIntegralPoint([self convertPoint: center toPage: 
page]);
         
         NSSize defaultSize = isAlternate ? NSMakeSize(16.0, 16.0) : ([page 
rotation] % 180 == 90) ? NSMakeSize(64.0, 128.0) : NSMakeSize(128.0, 64.0);
-        NSRect bounds = NSMakeRect(center.x - 0.5 * defaultSize.width, 
center.y - 0.5 * defaultSize.height, defaultSize.width, defaultSize.height);
-        NSRect pageBounds = [page boundsForBox:[self displayBox]];
+        NSRect bounds = SKRectFromCenterAndSize(center, defaultSize);
         
-        if (NSMaxX(bounds) > NSMaxX(pageBounds))
-            bounds.origin.x = NSMaxX(pageBounds) - NSWidth(bounds);
-        if (NSMinX(bounds) < NSMinX(pageBounds))
-            bounds.origin.x = NSMinX(pageBounds);
-        if (NSMaxY(bounds) > NSMaxY(pageBounds))
-            bounds.origin.y = NSMaxY(pageBounds) - NSHeight(bounds);
-        if (NSMinY(bounds) < NSMinY(pageBounds))
-            bounds.origin.y = NSMinY(pageBounds);
+        bounds = SKConstrainRect(bounds, [page boundsForBox:[self 
displayBox]]);
         
         if (isAlternate)
             newAnnotation = [[SKPDFAnnotationNote alloc] 
initWithBounds:bounds];
@@ -1622,8 +1604,7 @@
             NSImage *pageImage = [[self currentPage] imageForBox:[self 
displayBox]];
             NSImage *image = nil;
             
-            sourceRect.origin.x -= NSMinX(bounds);
-            sourceRect.origin.y -= NSMinY(bounds);
+            sourceRect.origin = SKSubstractPoints(sourceRect.origin, 
bounds.origin);
             targetRect.origin = NSZeroPoint;
             targetRect.size = sourceRect.size;
             image = [[NSImage alloc] initWithSize:targetRect.size];
@@ -1684,15 +1665,7 @@
     }
     
     // Make sure it fits in the page
-    NSRect pageBounds = [page boundsForBox:[self displayBox]];
-    if (NSMaxX(bounds) > NSMaxX(pageBounds))
-        bounds.origin.x = NSMaxX(pageBounds) - NSWidth(bounds);
-    if (NSMinX(bounds) < NSMinX(pageBounds))
-        bounds.origin.x = NSMinX(pageBounds);
-    if (NSMaxY(bounds) > NSMaxY(pageBounds))
-        bounds.origin.y = NSMaxY(pageBounds) - NSHeight(bounds);
-    if (NSMinY(bounds) < NSMinY(pageBounds))
-        bounds.origin.y = NSMinY(pageBounds);
+    bounds = SKConstrainRect(bounds, [page boundsForBox:[self displayBox]]);
     
     [self addAnnotationWithType:annotationType contents:text page:page 
bounds:bounds];
 }
@@ -1727,28 +1700,18 @@
         if (page == nil) {
             // Get center of the PDFView.
             NSRect viewFrame = [self frame];
-            center = NSMakePoint(NSMidX(viewFrame), NSMidY(viewFrame));
+            center = SKCenterPoint(viewFrame);
             page = [self pageForPoint: center nearest: YES];
         }
                
                // Convert to "page space".
-               center = [self convertPoint: center toPage: page];
-        center.x = roundf(center.x);
-        center.y = roundf(center.y);
+               center = SKIntegralPoint([self convertPoint: center toPage: 
page]);
         if ([page rotation] % 180 == 90)
             defaultSize = NSMakeSize(defaultSize.height, defaultSize.width);
         bounds = NSMakeRect(center.x - 0.5 * defaultSize.width, center.y - 0.5 
* defaultSize.height, defaultSize.width, defaultSize.height);
         
         // Make sure it fits in the page
-        NSRect pageBounds = [page boundsForBox:[self displayBox]];
-        if (NSMaxX(bounds) > NSMaxX(pageBounds))
-            bounds.origin.x = NSMaxX(pageBounds) - NSWidth(bounds);
-        if (NSMinX(bounds) < NSMinX(pageBounds))
-            bounds.origin.x = NSMinX(pageBounds);
-        if (NSMaxY(bounds) > NSMaxY(pageBounds))
-            bounds.origin.y = NSMaxY(pageBounds) - NSHeight(bounds);
-        if (NSMinY(bounds) < NSMinY(pageBounds))
-            bounds.origin.y = NSMinY(pageBounds);
+        bounds = SKConstrainRect(bounds, [page boundsForBox:[self 
displayBox]]);
        }
     [self addAnnotationWithType:annotationType contents:text page:page 
bounds:bounds];
 }
@@ -1926,7 +1889,7 @@
         [[activeAnnotation page] addAnnotation:editAnnotation];
         
         // Start editing
-        NSPoint location = [self convertPoint:[self 
convertPoint:NSMakePoint(NSMidX(editBounds), NSMidY(editBounds)) 
fromPage:[activeAnnotation page]] toView:nil];
+        NSPoint location = [self convertPoint:[self 
convertPoint:SKCenterPoint(editBounds) fromPage:[activeAnnotation page]] 
toView:nil];
         NSEvent *theEvent = [NSEvent mouseEventWithType:NSLeftMouseDown 
location:location modifierFlags:0 timestamp:0 windowNumber:[[self window] 
windowNumber] context:nil eventNumber:0 clickCount:1 pressure:1.0];
         [super mouseDown:theEvent];
         
@@ -2095,7 +2058,7 @@
             if (page == nil) {
                 // Get the center
                 NSRect viewFrame = [self frame];
-                point = NSMakePoint(NSMidX(viewFrame), NSMidY(viewFrame));
+                point = SKCenterPoint(viewFrame);
                 page = [self pageForPoint:point nearest:YES];
             }
         }
@@ -2219,10 +2182,10 @@
 @implementation SKPDFView (Private)
 
 - (NSRect)resizeThumbForRect:(NSRect)rect rotation:(int)rotation {
+    NSSize size = NSMakeSize(8.0, 8.0);
        NSRect thumb = rect;
-    float size = 8.0;
     
-    thumb.size = NSMakeSize(size, size);
+    thumb.size = size;
        
        // Use rotation to determine thumb origin.
        switch (rotation) {
@@ -2244,14 +2207,9 @@
 }
 
 - (NSRect)resizeThumbForRect:(NSRect)rect point:(NSPoint)point {
-       NSRect thumb = rect;
-    float size = 8.0;
+    NSSize size = NSMakeSize(8.0, 8.0);
+       NSRect thumb = SKRectFromCenterAndSize(SKAddPoints(rect.origin, point), 
size);
     
-    thumb.size = NSMakeSize(size, size);
-       
-    thumb.origin.x = NSMinX(rect) + point.x - 0.5 * size;
-    thumb.origin.y = NSMinY(rect) + point.y - 0.5 * size;
-       
     thumb.origin.x =  point.x > 0.5 * NSWidth(rect) ? floorf(NSMinX(thumb)) : 
ceilf(NSMinX(thumb));
     thumb.origin.y =  point.y > 0.5 * NSHeight(rect) ? floorf(NSMinY(thumb)) : 
ceilf(NSMinY(thumb));
     
@@ -2300,9 +2258,9 @@
     PDFPage *page;
     unsigned first, last;
     
-    page = [self pageForPoint:NSMakePoint(NSMinX(visibleRect), 
NSMaxY(visibleRect)) nearest:YES];
+    page = [self pageForPoint:SKTopLeftPoint(visibleRect) nearest:YES];
     first = [[self document] indexForPage:page];
-    page = [self pageForPoint:NSMakePoint(NSMaxX(visibleRect), 
NSMinY(visibleRect)) nearest:YES];
+    page = [self pageForPoint:SKBottomRightPoint(visibleRect) nearest:YES];
     last = [[self document] indexForPage:page];
     
     return NSMakeRange(first, last - first + 1);
@@ -2438,14 +2396,9 @@
     if ([[activeAnnotation type] isEqualToString:@"Line"]) {
         
         SKPDFAnnotationLine *annotation = (SKPDFAnnotationLine 
*)activeAnnotation;
-        NSPoint oldEndPoint = [annotation endPoint];
-        NSPoint endPoint;
-        NSPoint startPoint = [annotation startPoint];
-        startPoint.x = roundf(startPoint.x + NSMinX(bounds));
-        startPoint.y = roundf(startPoint.y + NSMinY(bounds));
-        oldEndPoint.x = roundf(oldEndPoint.x + NSMinX(bounds));
-        oldEndPoint.y = roundf(oldEndPoint.y + NSMinY(bounds));
-        endPoint = oldEndPoint;
+        NSPoint startPoint = SKIntegralPoint(SKAddPoints([annotation 
startPoint], bounds.origin));
+        NSPoint endPoint = SKIntegralPoint(SKAddPoints([annotation endPoint], 
bounds.origin));
+        NSPoint oldEndPoint = endPoint;
         
         // Resize the annotation.
         switch ([page rotation]) {
@@ -2531,10 +2484,7 @@
         endPoint.y = floorf(endPoint.y);
         
         if (NSEqualPoints(endPoint, oldEndPoint) == NO) {
-            newBounds.origin.x = floorf(fmin(startPoint.x, endPoint.x));
-            newBounds.size.width = ceilf(fmax(endPoint.x, startPoint.x)) - 
NSMinX(newBounds);
-            newBounds.origin.y = floorf(fmin(startPoint.y, endPoint.y));
-            newBounds.size.height = ceilf(fmax(endPoint.y, startPoint.y)) - 
NSMinY(newBounds);
+            newBounds = SKIntegralRectFromPoints(startPoint, endPoint);
             
             if (NSWidth(newBounds) < 8.0) {
                 newBounds.size.width = 8.0;
@@ -2545,10 +2495,8 @@
                 newBounds.origin.y = floorf(0.5 * (startPoint.y + endPoint.y) 
- 4.0);
             }
             
-            startPoint.x -= NSMinX(newBounds);
-            startPoint.y -= NSMinY(newBounds);
-            endPoint.x -= NSMinX(newBounds);
-            endPoint.y -= NSMinY(newBounds);
+            startPoint = SKSubstractPoints(startPoint, newBounds.origin);
+            endPoint = SKSubstractPoints(endPoint, newBounds.origin);
             
             [annotation setBounds:newBounds];
             [annotation setStartPoint:startPoint];
@@ -2858,37 +2806,21 @@
         NSPoint mouseLoc = [self convertPoint:[theEvent locationInWindow] 
fromView:nil];
         NSPoint startPoint = [self convertPoint:[self 
convertPoint:mouseDownLoc fromView:nil] toPage:page];
         NSPoint endPt = [self convertPoint:mouseLoc toPage:page];
-        NSPoint relPoint = NSMakePoint(endPt.x - startPoint.x, endPt.y - 
startPoint.y);
+        NSPoint relPoint = SKSubstractPoints(endPt, startPoint);
         newBounds = wasBounds;
         
         if ([[activeAnnotation type] isEqualToString:@"Line"]) {
             
             SKPDFAnnotationLine *annotation = (SKPDFAnnotationLine 
*)activeAnnotation;
-            NSPoint endPoint = wasEndPoint;
-            endPoint.x = roundf(endPoint.x + NSMinX(wasBounds));
-            endPoint.y = roundf(endPoint.y + NSMinY(wasBounds));
-            startPoint = wasStartPoint;
-            startPoint.x = roundf(startPoint.x + NSMinX(wasBounds));
-            startPoint.y = roundf(startPoint.y + NSMinY(wasBounds));
+            NSPoint endPoint = SKIntegralPoint(SKSubstractPoints(wasEndPoint, 
wasBounds.origin));
+            startPoint = SKIntegralPoint(SKSubstractPoints(wasStartPoint, 
wasBounds.origin));
             NSPoint *draggedPoint = draggingStartPoint ? &startPoint : 
&endPoint;
             
-            draggedPoint->x += relPoint.x;
-            draggedPoint->y += relPoint.y;
-            if (draggedPoint->x > NSMaxX(pageBounds))
-                draggedPoint->x = NSMaxX(pageBounds);
-            else if (draggedPoint->x < NSMinX(pageBounds))
-                draggedPoint->x = NSMinX(pageBounds);
-            if (draggedPoint->y > NSMaxY(pageBounds))
-                draggedPoint->y = NSMaxY(pageBounds) ;
-            else if (draggedPoint->y < NSMinY(pageBounds))
-                draggedPoint->y = NSMinY(pageBounds);
+            *draggedPoint = SKConstrainPointInRect(SKAddPoints(*draggedPoint, 
relPoint), pageBounds);
             draggedPoint->x = floorf(draggedPoint->x);
             draggedPoint->y = floorf(draggedPoint->y);
             
-            newBounds.origin.x = floorf(fmin(startPoint.x, endPoint.x));
-            newBounds.size.width = ceilf(fmax(endPoint.x, startPoint.x)) - 
NSMinX(newBounds);
-            newBounds.origin.y = floorf(fmin(startPoint.y, endPoint.y));
-            newBounds.size.height = ceilf(fmax(endPoint.y, startPoint.y)) - 
NSMinY(newBounds);
+            newBounds = SKIntegralRectFromPoints(startPoint, endPoint);
             
             if (NSWidth(newBounds) < 8.0) {
                 newBounds.size.width = 8.0;
@@ -2899,10 +2831,8 @@
                 newBounds.origin.y = floorf(0.5 * (startPoint.y + endPoint.y) 
- 4.0);
             }
             
-            startPoint.x -= NSMinX(newBounds);
-            startPoint.y -= NSMinY(newBounds);
-            endPoint.x -= NSMinX(newBounds);
-            endPoint.y -= NSMinY(newBounds);
+            startPoint = SKSubstractPoints(startPoint, newBounds.origin);
+            endPoint = SKSubstractPoints(endPoint, newBounds.origin);
             
             [annotation setStartPoint:startPoint];
             [annotation setEndPoint:endPoint];
@@ -3011,19 +2941,10 @@
                 page = newActivePage;
             }
             
-            NSPoint endPt = [self convertPoint:mouseLoc toPage:page];
             newBounds = currentBounds;
-            newBounds.origin.x = roundf(endPt.x - clickDelta.x);
-            newBounds.origin.y = roundf(endPt.y - clickDelta.y);
+            newBounds.origin = SKIntegralPoint(SKSubstractPoints([self 
convertPoint:mouseLoc toPage:page], clickDelta));
             // constrain bounds inside page bounds
-            if (NSMaxX(newBounds) > NSMaxX(pageBounds))
-                newBounds.origin.x = NSMaxX(pageBounds) - NSWidth(newBounds);
-            if (NSMinX(newBounds) < NSMinX(pageBounds))
-                newBounds.origin.x = NSMinX(pageBounds);
-            if (NSMaxY(newBounds) > NSMaxY(pageBounds))
-                newBounds.origin.y = NSMaxY(pageBounds) - NSHeight(newBounds);
-            if (NSMinY(newBounds) < NSMinY(pageBounds))
-                newBounds.origin.y = NSMinY(pageBounds);
+            newBounds = SKConstrainRect(newBounds, pageBounds);
         }
     }
     
@@ -3044,17 +2965,14 @@
             break;
         
         // dragging
-        NSPoint        newLocation;
+        NSPoint        newLocation = [theEvent locationInWindow];
+        NSPoint        delta = SKSubstractPoints(initialLocation, newLocation);
         NSRect newVisibleRect;
-        float  xDelta, yDelta;
         
-        newLocation = [theEvent locationInWindow];
-        xDelta = initialLocation.x - newLocation.x;
-        yDelta = initialLocation.y - newLocation.y;
         if ([self isFlipped])
-            yDelta = -yDelta;
+            delta.y = -delta.y;
         
-        newVisibleRect = NSOffsetRect (visibleRect, xDelta, yDelta);
+        newVisibleRect = NSOffsetRect (visibleRect, delta.x, delta.y);
         [[self documentView] scrollRectToVisible: newVisibleRect];
        }
     
@@ -3119,25 +3037,23 @@
         // we must be dragging
         NSPoint        newPoint;
         NSRect newRect = initialRect;
-        float  xDelta, yDelta;
+        NSPoint delta;
         
         newPoint = [self convertPoint:[self convertPoint:[theEvent 
locationInWindow] fromView:nil] toPage:page];
-        xDelta = newPoint.x - initialPoint.x;
-        yDelta = newPoint.y - initialPoint.y;
+        delta = SKSubstractPoints(newPoint, initialPoint);
         
         if (xEdge == 0 && yEdge == 0) {
-            newRect.origin.x += xDelta;
-            newRect.origin.y += yDelta;
+            newRect.origin = SKAddPoints(newRect.origin, delta);
         } else {
             if (xEdge == 1) {
-                newRect.size.width += xDelta;
+                newRect.size.width += delta.x;
                 if (NSWidth(newRect) < 0.0) {
                     newRect.size.width *= -1.0;
                     newRect.origin.x -= NSWidth(newRect);
                 }
             } else if (xEdge == 2) {
-                newRect.origin.x += xDelta;
-                newRect.size.width -= xDelta;
+                newRect.origin.x += delta.x;
+                newRect.size.width -= delta.x;
                 if (NSWidth(newRect) < 0.0) {
                     newRect.size.width *= -1.0;
                     newRect.origin.x -= NSWidth(newRect);
@@ -3145,14 +3061,14 @@
             }
             
             if (yEdge == 1) {
-                newRect.size.height += yDelta;
+                newRect.size.height += delta.y;
                 if (NSHeight(newRect) < 0.0) {
                     newRect.size.height *= -1.0;
                     newRect.origin.y -= NSHeight(newRect);
                 }
             } else if (yEdge == 2) {
-                newRect.origin.y += yDelta;
-                newRect.size.height -= yDelta;
+                newRect.origin.y += delta.y;
+                newRect.size.height -= delta.y;
                 if (NSHeight(newRect) < 0.0) {
                     newRect.size.height *= -1.0;
                     newRect.origin.y -= NSHeight(newRect);
@@ -3161,11 +3077,7 @@
         }
         
         // don't use NSIntersectionRect, because we want to keep empty rects
-        float minX = fmin(fmax(NSMinX(newRect), NSMinX(pageBounds)), 
NSMaxX(pageBounds));
-        float maxX = fmax(fmin(NSMaxX(newRect), NSMaxX(pageBounds)), 
NSMinX(pageBounds));
-        float minY = fmin(fmax(NSMinY(newRect), NSMinY(pageBounds)), 
NSMaxY(pageBounds));
-        float maxY = fmax(fmin(NSMaxY(newRect), NSMaxY(pageBounds)), 
NSMinY(pageBounds));
-        newRect = NSMakeRect(minX, minY, maxX - minX, maxY - minY);
+        newRect = SKIntersectionRect(newRect, pageBounds);
         if (didDrag) {
             NSRect dirtyRect = NSUnionRect(NSInsetRect(selectionRect, -margin, 
-margin), NSInsetRect(newRect, -margin, -margin));
             NSRange r = [self visiblePageIndexRange];
@@ -3248,7 +3160,7 @@
         PDFSelection *sel = nil;
         if (rectSelection) {
             // how to handle multipage selection?  Preview.app's behavior is 
screwy as well, so we'll do the same thing
-            NSRect selRect = NSMakeRect(fmin(p2.x, p1.x), fmin(p2.y, p1.y), 
fabs(p2.x - p1.x), fabs(p2.y - p1.y));
+            NSRect selRect = SKRectFromPoints(p1, p2);
             sel = [page1 selectionForRect:selRect];
             if (NSIsEmptyRect(selectionRect) == NO)
                 [self setNeedsDisplayInRect:selectionRect];
@@ -3317,8 +3229,6 @@
        NSPoint startPoint = [[self documentView] convertPoint:mouseLoc 
fromView:nil];
        NSPoint currentPoint;
     NSRect selRect = {startPoint, NSZeroSize};
-    NSRect bounds;
-    float minX, maxX, minY, maxY;
     BOOL dragged = NO;
        
     [[self window] discardCachedImage];
@@ -3346,28 +3256,14 @@
         
         currentPoint = [[self documentView] convertPoint:mouseLoc 
fromView:nil];
         
-        minX = fmin(startPoint.x, currentPoint.x);
-        maxX = fmax(startPoint.x, currentPoint.x);
-        minY = fmin(startPoint.y, currentPoint.y);
-        maxY = fmax(startPoint.y, currentPoint.y);
         // center around startPoint when holding down the Shift key
-        if ([theEvent modifierFlags] & NSShiftKeyMask) {
-            if (currentPoint.x > startPoint.x)
-                minX -= maxX - minX;
-            else
-                maxX += maxX - minX;
-            if (currentPoint.y > startPoint.y)
-                minY -= maxY - minY;
-            else
-                maxY += maxY - minY;
-        }
+        if ([theEvent modifierFlags] & NSShiftKeyMask)
+            selRect = SKRectFromCenterAndPoint(startPoint, currentPoint);
+        else
+            selRect = SKRectFromPoints(startPoint, currentPoint);
+        
         // intersect with the bounds, project on the bounds if necessary and 
allow zero width or height
-        bounds = [[self documentView] bounds];
-        minX = fmin(fmax(minX, NSMinX(bounds)), NSMaxX(bounds));
-        maxX = fmax(fmin(maxX, NSMaxX(bounds)), NSMinX(bounds));
-        minY = fmin(fmax(minY, NSMinY(bounds)), NSMaxY(bounds));
-        maxY = fmax(fmin(maxY, NSMaxY(bounds)), NSMinY(bounds));
-        selRect = NSMakeRect(minX, minY, maxX - minX, maxY - minY);
+        selRect = SKIntersectionRect(selRect, [[self documentView] bounds]);
         
         [[self window] cacheImageInRect:NSInsetRect([[self documentView] 
convertRect:selRect toView:nil], -2.0, -2.0)];
         
@@ -3385,9 +3281,10 @@
     [[self window] discardCachedImage];
        [[self cursorForEvent:theEvent] set];
     
-    NSPoint point = [self convertPoint:NSMakePoint(NSMidX(selRect), 
NSMidY(selRect)) fromView:[self documentView]];
+    NSPoint point = [self convertPoint:SKCenterPoint(selRect) fromView:[self 
documentView]];
     PDFPage *page = [self pageForPoint:point nearest:YES];
     NSRect rect = [self convertRect:selRect fromView:[self documentView]];
+    NSRect bounds;
     int factor = 1;
     BOOL autoFits = NO;
     
@@ -3496,8 +3393,7 @@
                 magRect = visibleRect;
             } else {
                 magRect = currentLevel == 2 ? largeMagRect : smallMagRect;
-                magRect.origin.x += mouseLoc.x;
-                magRect.origin.y += mouseLoc.y;
+                magRect.origin = SKAddPoints(magRect.origin, mouseLoc);
                 // restore the cached image in order to clear the rect
                 [[self window] restoreCachedImage];
                 [[self window] 
cacheImageInRect:NSIntersectionRect(NSInsetRect(magRect, -2.0, -2.0), 
visibleRect)];

Modified: trunk/SKSnapshotWindowController.m
===================================================================
--- trunk/SKSnapshotWindowController.m  2007-08-22 09:56:30 UTC (rev 2698)
+++ trunk/SKSnapshotWindowController.m  2007-08-22 14:21:38 UTC (rev 2699)
@@ -47,6 +47,7 @@
 #import "NSWindowController_SKExtensions.h"
 #import "SKStringConstants.h"
 #import "NSUserDefaultsController_SKExtensions.h"
+#import "NSGeometry_SKExtensions.h"
 
 static NSString *SKSnapshotWindowFrameAutosaveName = @"SKSnapshotWindow";
 static NSString *SKSnapshotViewChangedNotification = 
@"SKSnapshotViewChangedNotification";
@@ -199,27 +200,12 @@
     NSRect contentRect = [pdfView convertRect:rect fromPage:page];
     contentRect = [pdfView convertRect:contentRect toView:nil];
     NSRect frame = [[self window] frame];
-    NSRect screenFrame = [[[self window] screen] visibleFrame];
     
     contentRect.size.width += [NSScroller scrollerWidth];
     contentRect.size.height += [NSScroller scrollerWidth];
     frame.size = [[self window] frameRectForContentRect:contentRect].size;
+    frame = SKConstrainRect(frame, [[[self window] screen] visibleFrame]);
     
-    if (NSMaxX(frame) > NSMaxX(screenFrame))
-        frame.origin.x = NSMaxX(screenFrame) - NSWidth(frame);
-    if (NSMinX(frame) < NSMinX(screenFrame)) {
-        frame.origin.x = NSMinX(screenFrame);
-        if (NSWidth(frame) > NSWidth(screenFrame))
-            frame.size.width = NSWidth(screenFrame);
-    }
-    if (NSMaxY(frame) > NSMaxY(screenFrame))
-        frame.origin.x = NSMaxY(screenFrame) - NSHeight(frame);
-    if (NSMinY(frame) < NSMinY(screenFrame)) {
-        frame.origin.x = NSMinY(screenFrame);
-        if (NSHeight(frame) > NSHeight(screenFrame))
-            frame.size.height = NSHeight(screenFrame);
-    }
-    
     [[self window] setFrame:NSIntegralRect(frame) display:NO animate:NO];
     
     [pdfView goToPage:page];
@@ -246,8 +232,8 @@
     NSRect visibleRect = [clipView convertRect:[clipView visibleRect] 
toView:pdfView];
     unsigned first, last, index = [[pdfView document] indexForPage:page];
     
-    first = [[pdfView document] indexForPage:[pdfView 
pageForPoint:NSMakePoint(NSMinX(visibleRect), NSMaxY(visibleRect)) 
nearest:YES]];
-    last = [[pdfView document] indexForPage:[pdfView 
pageForPoint:NSMakePoint(NSMaxX(visibleRect), NSMinY(visibleRect)) 
nearest:YES]];
+    first = [[pdfView document] indexForPage:[pdfView 
pageForPoint:SKTopLeftPoint(visibleRect) nearest:YES]];
+    last = [[pdfView document] indexForPage:[pdfView 
pageForPoint:SKBottomRightPoint(visibleRect) nearest:YES]];
     
     return index >= first && index <= last;
 }

Modified: trunk/Skim.xcodeproj/project.pbxproj
===================================================================
--- trunk/Skim.xcodeproj/project.pbxproj        2007-08-22 09:56:30 UTC (rev 
2698)
+++ trunk/Skim.xcodeproj/project.pbxproj        2007-08-22 14:21:38 UTC (rev 
2699)
@@ -129,6 +129,8 @@
                CE5BC5B10C79A27B00EBDCF7 /* NSTableView_SKExtensions.h in 
CopyFiles */ = {isa = PBXBuildFile; fileRef = CE5BC5AF0C79A27B00EBDCF7 /* 
NSTableView_SKExtensions.h */; };
                CE5BC5B20C79A27B00EBDCF7 /* NSTableView_SKExtensions.m in 
Sources */ = {isa = PBXBuildFile; fileRef = CE5BC5B00C79A27B00EBDCF7 /* 
NSTableView_SKExtensions.m */; };
                CE5BD6740C7ADF1500EBDCF7 /* SKTypeSelectHelper.m in Sources */ 
= {isa = PBXBuildFile; fileRef = CE5BD6720C7ADF1500EBDCF7 /* 
SKTypeSelectHelper.m */; };
+               CE5BEA180C7C635400EBDCF7 /* NSGeometry_SKExtensions.h in 
CopyFiles */ = {isa = PBXBuildFile; fileRef = CE5BEA160C7C635400EBDCF7 /* 
NSGeometry_SKExtensions.h */; };
+               CE5BEA190C7C635400EBDCF7 /* NSGeometry_SKExtensions.m in 
Sources */ = {isa = PBXBuildFile; fileRef = CE5BEA170C7C635400EBDCF7 /* 
NSGeometry_SKExtensions.m */; };
                CE5F42AF0BF89FE00069D89C /* AppleRemote.m in Sources */ = {isa 
= PBXBuildFile; fileRef = CE5F42AD0BF89FE00069D89C /* AppleRemote.m */; };
                CE5F43730BF8A3410069D89C /* IOKit.framework in Frameworks */ = 
{isa = PBXBuildFile; fileRef = CE5F42D30BF8A3400069D89C /* IOKit.framework */; 
};
                CE67BB260BC44AC9007B6929 /* ZoomValues.strings in Resources */ 
= {isa = PBXBuildFile; fileRef = CE67BB240BC44AC9007B6929 /* ZoomValues.strings 
*/; };
@@ -259,6 +261,7 @@
                        files = (
                                CE2BD8450BD4135600A5F4DB /* Sparkle.framework 
in CopyFiles */,
                                CE5BC5B10C79A27B00EBDCF7 /* 
NSTableView_SKExtensions.h in CopyFiles */,
+                               CE5BEA180C7C635400EBDCF7 /* 
NSGeometry_SKExtensions.h in CopyFiles */,
                        );
                        runOnlyForDeploymentPostprocessing = 0;
                };
@@ -488,6 +491,8 @@
                CE5BC5B00C79A27B00EBDCF7 /* NSTableView_SKExtensions.m */ = 
{isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = 
sourcecode.c.objc; path = NSTableView_SKExtensions.m; sourceTree = "<group>"; };
                CE5BD6710C7ADF1500EBDCF7 /* SKTypeSelectHelper.h */ = {isa = 
PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = 
SKTypeSelectHelper.h; sourceTree = "<group>"; };
                CE5BD6720C7ADF1500EBDCF7 /* SKTypeSelectHelper.m */ = {isa = 
PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path 
= SKTypeSelectHelper.m; sourceTree = "<group>"; };
+               CE5BEA160C7C635400EBDCF7 /* NSGeometry_SKExtensions.h */ = {isa 
= PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path 
= NSGeometry_SKExtensions.h; sourceTree = "<group>"; };
+               CE5BEA170C7C635400EBDCF7 /* NSGeometry_SKExtensions.m */ = {isa 
= PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; 
path = NSGeometry_SKExtensions.m; sourceTree = "<group>"; };
                CE5F42AC0BF89FE00069D89C /* AppleRemote.h */ = {isa = 
PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = 
AppleRemote.h; path = vendorsrc/martinkahr/AppleRemoteSource_R80/AppleRemote.h; 
sourceTree = "<group>"; };
                CE5F42AD0BF89FE00069D89C /* AppleRemote.m */ = {isa = 
PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.objc; 
name = AppleRemote.m; path = 
vendorsrc/martinkahr/AppleRemoteSource_R80/AppleRemote.m; sourceTree = 
"<group>"; };
                CE5F42D30BF8A3400069D89C /* IOKit.framework */ = {isa = 
PBXFileReference; lastKnownFileType = wrapper.framework; name = 
IOKit.framework; path = /System/Library/Frameworks/IOKit.framework; sourceTree 
= "<absolute>"; };
@@ -817,6 +822,8 @@
                                CE2DE50C0B85DC4000D0DA12 /* 
PDFPage_SKExtensions.m */,
                                CE49726A0BDE8A7400D7F1D2 /* 
PDFSelection_SKExtensions.h */,
                                CE49726B0BDE8A7400D7F1D2 /* 
PDFSelection_SKExtensions.m */,
+                               CE5BEA160C7C635400EBDCF7 /* 
NSGeometry_SKExtensions.h */,
+                               CE5BEA170C7C635400EBDCF7 /* 
NSGeometry_SKExtensions.m */,
                        );
                        name = Categories;
                        sourceTree = "<group>";
@@ -1474,6 +1481,7 @@
                                CEF839B30C77742A00A3AD51 /* 
Files_SKExtensions.m in Sources */,
                                CE5BC5B20C79A27B00EBDCF7 /* 
NSTableView_SKExtensions.m in Sources */,
                                CE5BD6740C7ADF1500EBDCF7 /* 
SKTypeSelectHelper.m in Sources */,
+                               CE5BEA190C7C635400EBDCF7 /* 
NSGeometry_SKExtensions.m in Sources */,
                        );
                        runOnlyForDeploymentPostprocessing = 0;
                };


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >>  http://get.splunk.com/
_______________________________________________
Skim-app-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/skim-app-commit

Reply via email to