Revision: 5492
          http://skim-app.svn.sourceforge.net/skim-app/?rev=5492&view=rev
Author:   hofman
Date:     2009-07-17 09:06:37 +0000 (Fri, 17 Jul 2009)

Log Message:
-----------
no need to keep temporary ivars for dragging an annotation as we run it in a 
separate event loop

Modified Paths:
--------------
    trunk/SKPDFView.h
    trunk/SKPDFView.m

Modified: trunk/SKPDFView.h
===================================================================
--- trunk/SKPDFView.h   2009-07-17 00:01:32 UTC (rev 5491)
+++ trunk/SKPDFView.h   2009-07-17 09:06:37 UTC (rev 5492)
@@ -113,11 +113,7 @@
        PDFAnnotation *activeAnnotation;
        PDFAnnotation *highlightAnnotation;
     NSTextField *editField;
-       NSRect wasBounds;
-    NSPoint wasStartPoint;
-    NSPoint wasEndPoint;
        NSPoint mouseDownLoc;
-       NSPoint clickDelta;
     NSRect selectionRect;
     NSUInteger selectionPageIndex;
     CGFloat magnification;

Modified: trunk/SKPDFView.m
===================================================================
--- trunk/SKPDFView.m   2009-07-17 00:01:32 UTC (rev 5491)
+++ trunk/SKPDFView.m   2009-07-17 09:06:37 UTC (rev 5492)
@@ -156,7 +156,6 @@
 - (void)doResizeReadingBarForKey:(unichar)eventChar;
 
 - (BOOL)doSelectAnnotationWithEvent:(NSEvent *)theEvent;
-- (void)doDragAnnotationWithEvent:(NSEvent *)theEvent;
 - (void)doSelectSnapshotWithEvent:(NSEvent *)theEvent;
 - (void)doMagnifyWithEvent:(NSEvent *)theEvent;
 - (void)doDragWithEvent:(NSEvent *)theEvent;
@@ -246,11 +245,7 @@
     readingBar = nil;
     
     activeAnnotation = nil;
-    wasBounds = NSZeroRect;
-    wasStartPoint = NSZeroPoint;
-    wasEndPoint = NSZeroPoint;
     mouseDownLoc = NSZeroPoint;
-    clickDelta = NSZeroPoint;
     selectionRect = NSZeroRect;
     selectionPageIndex = NSNotFound;
     magnification = 0.0;
@@ -2996,71 +2991,188 @@
     }
 }
 
-- (void)doDrawFreehandNoteWithEvent:(NSEvent *)theEvent {
-    NSPoint mouseDownOnPage = [self convertPoint:[theEvent locationInWindow] 
fromView:nil];
-    PDFPage *page = [self pageForPoint:mouseDownOnPage nearest:YES];
-    NSPoint pagePoint = [self convertPoint:mouseDownOnPage toPage:page];
-    NSString *text = nil;
-    PDFBorder *border = nil;
-    NSBezierPath *path;
-    BOOL didDraw = NO;
+- (void)doDragAnnotationWithEvent:(NSEvent *)theEvent 
originalBounds:(NSRect)originalBounds 
originalStartPoint:(NSPoint)originalStartPoint 
originalEndPoint:(NSPoint)originalEndPoint offset:(NSPoint)offset {
+    PDFPage *page = [activeAnnotation page];
+    NSRect newBounds;
+    NSRect currentBounds = [activeAnnotation bounds];
+    NSRect pageBounds = [page  boundsForBox:[self displayBox]];
     
-    if (([theEvent modifierFlags] & NSShiftKeyMask) && [[activeAnnotation 
type] isEqualToString:SKNInkString] && [[activeAnnotation page] isEqual:page]) {
-        bezierPaths = [[NSArray alloc] initWithArray:[(PDFAnnotationInk 
*)activeAnnotation paths] copyItems:YES];
-        NSAffineTransform *transform = [NSAffineTransform transform];
-        NSRect bounds = [activeAnnotation bounds];
-        [transform translateXBy:NSMinX(bounds) yBy:NSMinY(bounds)];
-        [bezierPaths 
makeObjectsPerformSelector:@selector(transformUsingAffineTransform:) 
withObject:transform];
-        text = [[[activeAnnotation string] retain] autorelease];
-        border = [[[activeAnnotation border] retain] autorelease];
-        pathColor = [[activeAnnotation color] retain];
-        NSEnumerator *pathEnum = [bezierPaths objectEnumerator];
-        while (path = [pathEnum nextObject]) {
-            [path setLineCapStyle:NSRoundLineCapStyle];
-            [path setLineJoinStyle:NSRoundLineJoinStyle];
-            [path setLineWidth:[border lineWidth]];
+    if (dragMask != 0) {
+        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 = SKSubstractPoints(endPt, startPoint);
+        newBounds = originalBounds;
+        
+        if ([[activeAnnotation type] isEqualToString:SKNLineString]) {
+            
+            PDFAnnotationLine *annotation = (PDFAnnotationLine 
*)activeAnnotation;
+            NSPoint endPoint = originalEndPoint;
+            startPoint = originalStartPoint;
+            NSPoint *draggedPoint = (dragMask & BDSKMinXEdgeMask) ? 
&startPoint : &endPoint;
+            
+            *draggedPoint = SKConstrainPointInRect(SKAddPoints(*draggedPoint, 
relPoint), pageBounds);
+            draggedPoint->x = SKFloor(draggedPoint->x);
+            draggedPoint->y = SKFloor(draggedPoint->y);
+            
+            newBounds = SKIntegralRectFromPoints(startPoint, endPoint);
+            
+            if (NSWidth(newBounds) < 8.0) {
+                newBounds.size.width = 8.0;
+                newBounds.origin.x = SKFloor(0.5 * (startPoint.x + endPoint.x) 
- 4.0);
+            }
+            if (NSHeight(newBounds) < 8.0) {
+                newBounds.size.height = 8.0;
+                newBounds.origin.y = SKFloor(0.5 * (startPoint.y + endPoint.y) 
- 4.0);
+            }
+            
+            if ([theEvent modifierFlags] & NSShiftKeyMask) {
+                NSPoint *fixedPoint = (dragMask & BDSKMinXEdgeMask) ? 
&endPoint : &startPoint;
+                NSPoint diffPoint = SKSubstractPoints(*draggedPoint, 
*fixedPoint);
+                CGFloat dx = SKAbs(diffPoint.x), dy = SKAbs(diffPoint.y);
+                
+                if (dx < 0.4 * dy) {
+                    diffPoint.x = 0.0;
+                } else if (dy < 0.4 * dx) {
+                    diffPoint.y = 0.0;
+                } else {
+                    dx = SKMin(dx, dy);
+                    diffPoint.x = diffPoint.x < 0.0 ? -dx : dx;
+                    diffPoint.y = diffPoint.y < 0.0 ? -dx : dx;
+                }
+                *draggedPoint = SKAddPoints(*fixedPoint, diffPoint);
+            }
+            
+            startPoint = SKSubstractPoints(startPoint, newBounds.origin);
+            endPoint = SKSubstractPoints(endPoint, newBounds.origin);
+            
+            [annotation setStartPoint:startPoint];
+            [annotation setEndPoint:endPoint];
+            
+        } else {
+            
+            if ((dragMask & BDSKMinXEdgeMask) && (dragMask & BDSKMaxXEdgeMask))
+                dragMask &= relPoint.x < 0.0 ? ~BDSKMaxXEdgeMask : 
~BDSKMinXEdgeMask;
+            if ((dragMask & BDSKMinYEdgeMask) && (dragMask & BDSKMaxYEdgeMask))
+                dragMask &= relPoint.y <= 0.0 ? ~BDSKMaxYEdgeMask : 
~BDSKMinYEdgeMask;
+            
+            if ([theEvent modifierFlags] & NSShiftKeyMask) {
+                CGFloat width = NSWidth(newBounds);
+                CGFloat height = NSHeight(newBounds);
+                
+                if (dragMask & BDSKMaxXEdgeMask)
+                    width = SKMax(8.0, width + relPoint.x);
+                else if (dragMask & BDSKMinXEdgeMask)
+                    width = SKMax(8.0, width - relPoint.x);
+                if (dragMask & BDSKMaxYEdgeMask)
+                    height = SKMax(8.0, height + relPoint.y);
+                else if (dragMask & BDSKMinYEdgeMask)
+                    height = SKMax(8.0, height - relPoint.y);
+                
+                if (dragMask & (BDSKMinXEdgeMask | BDSKMaxXEdgeMask)) {
+                    if (dragMask & (BDSKMinYEdgeMask | BDSKMaxYEdgeMask))
+                        width = height = SKMax(width, height);
+                    else
+                        height = width;
+                } else {
+                    width = height;
+                }
+                
+                if (dragMask & BDSKMinXEdgeMask) {
+                    if (NSMaxX(newBounds) - width < NSMinX(pageBounds))
+                        width = height = SKMax(8.0, NSMaxX(newBounds) - 
NSMinX(pageBounds));
+                } else {
+                    if (NSMinX(newBounds) + width > NSMaxX(pageBounds))
+                        width = height = SKMax(8.0, NSMaxX(pageBounds) - 
NSMinX(newBounds));
+                }
+                if (dragMask & BDSKMinYEdgeMask) {
+                    if (NSMaxY(newBounds) - height < NSMinY(pageBounds))
+                        width = height = SKMax(8.0, NSMaxY(newBounds) - 
NSMinY(pageBounds));
+                } else {
+                    if (NSMinY(newBounds) + height > NSMaxY(pageBounds))
+                        width = height = SKMax(8.0, NSMaxY(pageBounds) - 
NSMinY(newBounds));
+                }
+                
+                if (dragMask & BDSKMinXEdgeMask)
+                    newBounds.origin.x = NSMaxX(newBounds) - width;
+                if (dragMask & BDSKMinYEdgeMask)
+                    newBounds.origin.y = NSMaxY(newBounds) - height;
+                newBounds.size.width = width;
+                newBounds.size.height = height;
+               
+            } else {
+                if (dragMask & BDSKMaxXEdgeMask) {
+                    newBounds.size.width += relPoint.x;
+                    if (NSMaxX(newBounds) > NSMaxX(pageBounds))
+                        newBounds.size.width = NSMaxX(pageBounds) - 
NSMinX(newBounds);
+                    if (NSWidth(newBounds) < 8.0) {
+                        newBounds.size.width = 8.0;
+                    }
+                } else if (dragMask & BDSKMinXEdgeMask) {
+                    newBounds.origin.x += relPoint.x;
+                    newBounds.size.width -= relPoint.x;
+                    if (NSMinX(newBounds) < NSMinX(pageBounds)) {
+                        newBounds.size.width = NSMaxX(newBounds) - 
NSMinX(pageBounds);
+                        newBounds.origin.x = NSMinX(pageBounds);
+                    }
+                    if (NSWidth(newBounds) < 8.0) {
+                        newBounds.origin.x = NSMaxX(newBounds) - 8.0;
+                        newBounds.size.width = 8.0;
+                    }
+                }
+                if (dragMask & BDSKMaxYEdgeMask) {
+                    newBounds.size.height += relPoint.y;
+                    if (NSMaxY(newBounds) > NSMaxY(pageBounds)) {
+                        newBounds.size.height = NSMaxY(pageBounds) - 
NSMinY(newBounds);
+                    }
+                    if (NSHeight(newBounds) < 8.0) {
+                        newBounds.size.height = 8.0;
+                    }
+                } else if (dragMask & BDSKMinYEdgeMask) {
+                    newBounds.origin.y += relPoint.y;
+                    newBounds.size.height -= relPoint.y;
+                    if (NSMinY(newBounds) < NSMinY(pageBounds)) {
+                        newBounds.size.height = NSMaxY(newBounds) - 
NSMinY(pageBounds);
+                        newBounds.origin.y = NSMinY(pageBounds);
+                    }
+                    if (NSHeight(newBounds) < 8.0) {
+                        newBounds.origin.y = NSMaxY(newBounds) - 8.0;
+                        newBounds.size.height = 8.0;
+                    }
+                }
+            }
+            // Keep integer.
+            newBounds = NSIntegralRect(newBounds);
+            
         }
-        [self removeActiveAnnotation:nil];
-        path = [[bezierPaths lastObject] retain];
-        [path lineToPoint:pagePoint];
-        [self setNeedsDisplayInRect:[self convertRect:NSInsetRect([path 
nonEmptyBounds], -8.0, -8.0) fromPage:page]];
-        didDraw = YES;
     } else {
-        path = [NSBezierPath bezierPath];
-        [path setLineCapStyle:NSRoundLineCapStyle];
-        [path setLineJoinStyle:NSRoundLineJoinStyle];
-        [path setLineWidth:[[NSUserDefaults standardUserDefaults] 
floatForKey:SKInkNoteLineWidthKey]];
-        [path moveToPoint:pagePoint];
-        bezierPaths = [[NSArray alloc] initWithObjects:path, nil];
+        // Move annotation.
+        [[self documentView] autoscroll:theEvent];
+        
+        NSPoint mouseLoc = [self convertPoint:[theEvent locationInWindow] 
fromView:nil];
+        PDFPage *newActivePage = [self pageForPoint:mouseLoc nearest:YES];
+        
+        pageBounds = [newActivePage  boundsForBox:[self displayBox]];
+        
+        if (newActivePage == nil) {
+            // this should never happen, but just to be sure
+            newBounds = originalBounds;
+        } else {
+            if (newActivePage != page) {
+                // move the annotation to the new page
+                [self moveAnnotation:activeAnnotation toPage:newActivePage];
+                page = newActivePage;
+            }
+            
+            newBounds = currentBounds;
+            newBounds.origin = SKIntegralPoint(SKSubstractPoints([self 
convertPoint:mouseLoc toPage:page], offset));
+            // constrain bounds inside page bounds
+            newBounds = SKConstrainRect(newBounds, pageBounds);
+        }
     }
-    pathPageIndex = [page pageIndex];
     
-    [self setActiveAnnotation:nil];
-    
-    while (YES) {
-        theEvent = [[self window] nextEventMatchingMask: NSLeftMouseUpMask | 
NSLeftMouseDraggedMask];
-        if ([theEvent type] == NSLeftMouseUp)
-            break;
-        [path lineToPoint:[self convertPoint:[self convertPoint:[theEvent 
locationInWindow] fromView:nil] toPage:page]];
-        [self setNeedsDisplayInRect:[self convertRect:NSInsetRect([path 
nonEmptyBounds], -8.0, -8.0) fromPage:page]];
-        didDraw = YES;
-    }
-    
-    if (didDraw) {
-        [self addAnnotationWithType:SKInkNote contents:nil page:page 
bounds:NSZeroRect];
-        if (pathColor)
-            [activeAnnotation setColor:pathColor];
-        if (border)
-            [activeAnnotation setBorder:border];
-        if (text)
-            [activeAnnotation setString:text];
-        [[self undoManager] setActionName:NSLocalizedString(@"Add Note", 
@"Undo action name")];
-    }
-    
-    [bezierPaths release];
-    bezierPaths = nil;
-    [pathColor release];
-    pathColor = nil;
+    // Change annotation's location.
+    [activeAnnotation setBounds:newBounds];
 }
 
 - (BOOL)doSelectAnnotationWithEvent:(NSEvent *)theEvent {
@@ -3094,9 +3206,6 @@
             if ([annotation hitTest:pagePoint] && (editField == nil || 
annotation != activeAnnotation)) {
                 mouseDownInAnnotation = YES;
                 newActiveAnnotation = annotation;
-                // Remember click point relative to annotation origin.
-                clickDelta.x = pagePoint.x - NSMinX(bounds);
-                clickDelta.y = pagePoint.y - NSMinY(bounds);
                 break;
             } else if (NSPointInRect(pagePoint, bounds)) {
                 // register this, so we can do our own selection later
@@ -3132,8 +3241,6 @@
                 [self addAnnotationWithType:annotationMode contents:nil 
page:page bounds:bounds];
                 newActiveAnnotation = activeAnnotation;
                 mouseDownInAnnotation = YES;
-                clickDelta.x = pagePoint.x - NSMinX(bounds);
-                clickDelta.y = pagePoint.y - NSMinY(bounds);
             }
         } else if ([newActiveAnnotation isMarkup] && NSLeftMouseDragged == 
[[NSApp nextEventMatchingMask:(NSLeftMouseUpMask | NSLeftMouseDraggedMask) 
untilDate:[NSDate distantFuture] inMode:NSEventTrackingRunLoopMode dequeue:NO] 
type]) {
             newActiveAnnotation = nil;
@@ -3178,10 +3285,6 @@
                 [[self undoManager] setActionName:NSLocalizedString(@"Join 
Notes", @"Undo action name")];
                 
                 [paths release];
-                
-                bounds = [newActiveAnnotation bounds];
-                clickDelta.x = pagePoint.x - NSMinX(bounds);
-                clickDelta.y = pagePoint.y - NSMinY(bounds);
             }
         }
     }
@@ -3194,46 +3297,41 @@
         if ([theEvent clickCount] == 2 && [activeAnnotation isSkimNote]) {
             [self editActiveAnnotation:self];
         } else { 
-            // Old (current) annotation location.
-            wasBounds = [activeAnnotation bounds];
+            // Old (current) annotation location and click point relative to it
+            NSRect originalBounds = [activeAnnotation bounds];
             
-            if ([[activeAnnotation type] isEqualToString:SKNLineString]) {
-                wasStartPoint = [(PDFAnnotationLine *)activeAnnotation 
startPoint];
-                wasEndPoint = [(PDFAnnotationLine *)activeAnnotation endPoint];
-            }
-            
             // Hit-test for resize box.
             dragMask = 0;
             if ([[activeAnnotation type] isEqualToString:SKNLineString]) {
-                if (NSPointInRect(pagePoint, 
SKRectFromCenterAndSize(SKAddPoints(wasBounds.origin, [(PDFAnnotationLine 
*)activeAnnotation endPoint]), SKMakeSquareSize(8.0))))
+                if (NSPointInRect(pagePoint, 
SKRectFromCenterAndSize(SKAddPoints(originalBounds.origin, [(PDFAnnotationLine 
*)activeAnnotation endPoint]), SKMakeSquareSize(8.0))))
                     dragMask = BDSKMaxXEdgeMask;
-                else if (NSPointInRect(pagePoint, 
SKRectFromCenterAndSize(SKAddPoints(wasBounds.origin, [(PDFAnnotationLine 
*)activeAnnotation startPoint]), SKMakeSquareSize(8.0))))
+                else if (NSPointInRect(pagePoint, 
SKRectFromCenterAndSize(SKAddPoints(originalBounds.origin, [(PDFAnnotationLine 
*)activeAnnotation startPoint]), SKMakeSquareSize(8.0))))
                     dragMask = BDSKMinXEdgeMask;
             }  else if ([activeAnnotation isResizable]) {
-                if (NSWidth(wasBounds) < 2.0) {
+                if (NSWidth(originalBounds) < 2.0) {
                     dragMask |= BDSKMinXEdgeMask | BDSKMaxXEdgeMask;
                 } else if ([page rotation] < 180) {
-                    if (pagePoint.x >= NSMaxX(wasBounds) - 4.0)
+                    if (pagePoint.x >= NSMaxX(originalBounds) - 4.0)
                         dragMask |= BDSKMaxXEdgeMask;
-                    else if (pagePoint.x <= NSMinX(wasBounds) + 4.0)
+                    else if (pagePoint.x <= NSMinX(originalBounds) + 4.0)
                         dragMask |= BDSKMinXEdgeMask;
                 } else {
-                    if (pagePoint.x <= NSMinX(wasBounds) + 4.0)
+                    if (pagePoint.x <= NSMinX(originalBounds) + 4.0)
                         dragMask |= BDSKMinXEdgeMask;
-                    else if (pagePoint.x >= NSMaxX(wasBounds) - 4.0)
+                    else if (pagePoint.x >= NSMaxX(originalBounds) - 4.0)
                         dragMask |= BDSKMaxXEdgeMask;
                 }
-                if (NSHeight(wasBounds) < 2.0) {
+                if (NSHeight(originalBounds) < 2.0) {
                     dragMask |= BDSKMinYEdgeMask | BDSKMaxYEdgeMask;
                 } else if ([page rotation] % 270 != 0) {
-                    if (pagePoint.y >= NSMaxY(wasBounds) - 4.0)
+                    if (pagePoint.y >= NSMaxY(originalBounds) - 4.0)
                         dragMask |= BDSKMaxYEdgeMask;
-                    else if (pagePoint.y <= NSMinY(wasBounds) + 4.0)
+                    else if (pagePoint.y <= NSMinY(originalBounds) + 4.0)
                         dragMask |= BDSKMinYEdgeMask;
                 } else {
-                    if (pagePoint.y <= NSMinY(wasBounds) + 4.0)
+                    if (pagePoint.y <= NSMinY(originalBounds) + 4.0)
                         dragMask |= BDSKMinYEdgeMask;
-                    else if (pagePoint.y >= NSMaxY(wasBounds) - 4.0)
+                    else if (pagePoint.y >= NSMaxY(originalBounds) - 4.0)
                         dragMask |= BDSKMaxYEdgeMask;
                 }
             }
@@ -3244,6 +3342,16 @@
                 // we move or resize the annotation in an event loop, which 
ensures it's enclosed in a single undo group
                 BOOL draggedAnnotation = NO;
                 NSEvent *lastMouseEvent = theEvent;
+                NSRect originalBounds = [activeAnnotation bounds];
+                NSPoint offset = SKSubstractPoints(pagePoint, 
originalBounds.origin);
+                NSPoint originalStartPoint = NSZeroPoint;
+                NSPoint originalEndPoint = NSZeroPoint;
+                
+                if ([[activeAnnotation type] isEqualToString:SKNLineString]) {
+                    originalStartPoint = 
SKIntegralPoint(SKAddPoints([(PDFAnnotationLine *)activeAnnotation startPoint], 
originalBounds.origin));
+                    originalEndPoint = 
SKIntegralPoint(SKAddPoints([(PDFAnnotationLine *)activeAnnotation endPoint], 
originalBounds.origin));
+                }
+                
                 [NSEvent startPeriodicEventsAfterDelay:0.1 withPeriod:0.1];
                 while (YES) {
                     theEvent = [[self window] nextEventMatchingMask: 
NSLeftMouseUpMask | NSLeftMouseDraggedMask | NSPeriodicMask];
@@ -3253,10 +3361,10 @@
                         lastMouseEvent = theEvent;
                         draggedAnnotation = YES;
                     }
-                    [self doDragAnnotationWithEvent:lastMouseEvent];
+                    [self doDragAnnotationWithEvent:lastMouseEvent 
originalBounds:originalBounds originalStartPoint:originalStartPoint 
originalEndPoint:originalEndPoint offset:offset];
                 }
                 [NSEvent stopPeriodicEvents];
-                if (toolMode == SKNoteToolMode && NSEqualSizes(wasBounds.size, 
NSZeroSize) && [[activeAnnotation type] isEqualToString:SKNFreeTextString])
+                if (toolMode == SKNoteToolMode && 
NSEqualSizes(originalBounds.size, NSZeroSize) && [[activeAnnotation type] 
isEqualToString:SKNFreeTextString])
                     [self editActiveAnnotation:self];   
                 if (draggedAnnotation && 
                     [[NSUserDefaults standardUserDefaults] 
boolForKey:SKDisableUpdateContentsFromEnclosedTextKey] == NO &&
@@ -3286,188 +3394,71 @@
     }
 }
 
-- (void)doDragAnnotationWithEvent:(NSEvent *)theEvent {
-    PDFPage *page = [activeAnnotation page];
-    NSRect newBounds;
-    NSRect currentBounds = [activeAnnotation bounds];
-    NSRect pageBounds = [page  boundsForBox:[self displayBox]];
+- (void)doDrawFreehandNoteWithEvent:(NSEvent *)theEvent {
+    NSPoint mouseDownOnPage = [self convertPoint:[theEvent locationInWindow] 
fromView:nil];
+    PDFPage *page = [self pageForPoint:mouseDownOnPage nearest:YES];
+    NSPoint pagePoint = [self convertPoint:mouseDownOnPage toPage:page];
+    NSString *text = nil;
+    PDFBorder *border = nil;
+    NSBezierPath *path;
+    BOOL didDraw = NO;
     
-    if (dragMask != 0) {
-        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 = SKSubstractPoints(endPt, startPoint);
-        newBounds = wasBounds;
-        
-        if ([[activeAnnotation type] isEqualToString:SKNLineString]) {
-            
-            PDFAnnotationLine *annotation = (PDFAnnotationLine 
*)activeAnnotation;
-            NSPoint endPoint = SKIntegralPoint(SKAddPoints(wasEndPoint, 
wasBounds.origin));
-            startPoint = SKIntegralPoint(SKAddPoints(wasStartPoint, 
wasBounds.origin));
-            NSPoint *draggedPoint = (dragMask & BDSKMinXEdgeMask) ? 
&startPoint : &endPoint;
-            
-            *draggedPoint = SKConstrainPointInRect(SKAddPoints(*draggedPoint, 
relPoint), pageBounds);
-            draggedPoint->x = SKFloor(draggedPoint->x);
-            draggedPoint->y = SKFloor(draggedPoint->y);
-            
-            newBounds = SKIntegralRectFromPoints(startPoint, endPoint);
-            
-            if (NSWidth(newBounds) < 8.0) {
-                newBounds.size.width = 8.0;
-                newBounds.origin.x = SKFloor(0.5 * (startPoint.x + endPoint.x) 
- 4.0);
-            }
-            if (NSHeight(newBounds) < 8.0) {
-                newBounds.size.height = 8.0;
-                newBounds.origin.y = SKFloor(0.5 * (startPoint.y + endPoint.y) 
- 4.0);
-            }
-            
-            if ([theEvent modifierFlags] & NSShiftKeyMask) {
-                NSPoint *fixedPoint = (dragMask & BDSKMinXEdgeMask) ? 
&endPoint : &startPoint;
-                NSPoint diffPoint = SKSubstractPoints(*draggedPoint, 
*fixedPoint);
-                CGFloat dx = SKAbs(diffPoint.x), dy = SKAbs(diffPoint.y);
-                
-                if (dx < 0.4 * dy) {
-                    diffPoint.x = 0.0;
-                } else if (dy < 0.4 * dx) {
-                    diffPoint.y = 0.0;
-                } else {
-                    dx = SKMin(dx, dy);
-                    diffPoint.x = diffPoint.x < 0.0 ? -dx : dx;
-                    diffPoint.y = diffPoint.y < 0.0 ? -dx : dx;
-                }
-                *draggedPoint = SKAddPoints(*fixedPoint, diffPoint);
-            }
-            
-            startPoint = SKSubstractPoints(startPoint, newBounds.origin);
-            endPoint = SKSubstractPoints(endPoint, newBounds.origin);
-            
-            [annotation setStartPoint:startPoint];
-            [annotation setEndPoint:endPoint];
-            
-        } else {
-            
-            if ((dragMask & BDSKMinXEdgeMask) && (dragMask & BDSKMaxXEdgeMask))
-                dragMask &= relPoint.x < 0.0 ? ~BDSKMaxXEdgeMask : 
~BDSKMinXEdgeMask;
-            if ((dragMask & BDSKMinYEdgeMask) && (dragMask & BDSKMaxYEdgeMask))
-                dragMask &= relPoint.y <= 0.0 ? ~BDSKMaxYEdgeMask : 
~BDSKMinYEdgeMask;
-            
-            if ([theEvent modifierFlags] & NSShiftKeyMask) {
-                CGFloat width = NSWidth(newBounds);
-                CGFloat height = NSHeight(newBounds);
-                
-                if (dragMask & BDSKMaxXEdgeMask)
-                    width = SKMax(8.0, width + relPoint.x);
-                else if (dragMask & BDSKMinXEdgeMask)
-                    width = SKMax(8.0, width - relPoint.x);
-                if (dragMask & BDSKMaxYEdgeMask)
-                    height = SKMax(8.0, height + relPoint.y);
-                else if (dragMask & BDSKMinYEdgeMask)
-                    height = SKMax(8.0, height - relPoint.y);
-                
-                if (dragMask & (BDSKMinXEdgeMask | BDSKMaxXEdgeMask)) {
-                    if (dragMask & (BDSKMinYEdgeMask | BDSKMaxYEdgeMask))
-                        width = height = SKMax(width, height);
-                    else
-                        height = width;
-                } else {
-                    width = height;
-                }
-                
-                if (dragMask & BDSKMinXEdgeMask) {
-                    if (NSMaxX(newBounds) - width < NSMinX(pageBounds))
-                        width = height = SKMax(8.0, NSMaxX(newBounds) - 
NSMinX(pageBounds));
-                } else {
-                    if (NSMinX(newBounds) + width > NSMaxX(pageBounds))
-                        width = height = SKMax(8.0, NSMaxX(pageBounds) - 
NSMinX(newBounds));
-                }
-                if (dragMask & BDSKMinYEdgeMask) {
-                    if (NSMaxY(newBounds) - height < NSMinY(pageBounds))
-                        width = height = SKMax(8.0, NSMaxY(newBounds) - 
NSMinY(pageBounds));
-                } else {
-                    if (NSMinY(newBounds) + height > NSMaxY(pageBounds))
-                        width = height = SKMax(8.0, NSMaxY(pageBounds) - 
NSMinY(newBounds));
-                }
-                
-                if (dragMask & BDSKMinXEdgeMask)
-                    newBounds.origin.x = NSMaxX(newBounds) - width;
-                if (dragMask & BDSKMinYEdgeMask)
-                    newBounds.origin.y = NSMaxY(newBounds) - height;
-                newBounds.size.width = width;
-                newBounds.size.height = height;
-               
-            } else {
-                if (dragMask & BDSKMaxXEdgeMask) {
-                    newBounds.size.width += relPoint.x;
-                    if (NSMaxX(newBounds) > NSMaxX(pageBounds))
-                        newBounds.size.width = NSMaxX(pageBounds) - 
NSMinX(newBounds);
-                    if (NSWidth(newBounds) < 8.0) {
-                        newBounds.size.width = 8.0;
-                    }
-                } else if (dragMask & BDSKMinXEdgeMask) {
-                    newBounds.origin.x += relPoint.x;
-                    newBounds.size.width -= relPoint.x;
-                    if (NSMinX(newBounds) < NSMinX(pageBounds)) {
-                        newBounds.size.width = NSMaxX(newBounds) - 
NSMinX(pageBounds);
-                        newBounds.origin.x = NSMinX(pageBounds);
-                    }
-                    if (NSWidth(newBounds) < 8.0) {
-                        newBounds.origin.x = NSMaxX(newBounds) - 8.0;
-                        newBounds.size.width = 8.0;
-                    }
-                }
-                if (dragMask & BDSKMaxYEdgeMask) {
-                    newBounds.size.height += relPoint.y;
-                    if (NSMaxY(newBounds) > NSMaxY(pageBounds)) {
-                        newBounds.size.height = NSMaxY(pageBounds) - 
NSMinY(newBounds);
-                    }
-                    if (NSHeight(newBounds) < 8.0) {
-                        newBounds.size.height = 8.0;
-                    }
-                } else if (dragMask & BDSKMinYEdgeMask) {
-                    newBounds.origin.y += relPoint.y;
-                    newBounds.size.height -= relPoint.y;
-                    if (NSMinY(newBounds) < NSMinY(pageBounds)) {
-                        newBounds.size.height = NSMaxY(newBounds) - 
NSMinY(pageBounds);
-                        newBounds.origin.y = NSMinY(pageBounds);
-                    }
-                    if (NSHeight(newBounds) < 8.0) {
-                        newBounds.origin.y = NSMaxY(newBounds) - 8.0;
-                        newBounds.size.height = 8.0;
-                    }
-                }
-            }
-            // Keep integer.
-            newBounds = NSIntegralRect(newBounds);
-            
+    if (([theEvent modifierFlags] & NSShiftKeyMask) && [[activeAnnotation 
type] isEqualToString:SKNInkString] && [[activeAnnotation page] isEqual:page]) {
+        bezierPaths = [[NSArray alloc] initWithArray:[(PDFAnnotationInk 
*)activeAnnotation paths] copyItems:YES];
+        NSAffineTransform *transform = [NSAffineTransform transform];
+        NSRect bounds = [activeAnnotation bounds];
+        [transform translateXBy:NSMinX(bounds) yBy:NSMinY(bounds)];
+        [bezierPaths 
makeObjectsPerformSelector:@selector(transformUsingAffineTransform:) 
withObject:transform];
+        text = [[[activeAnnotation string] retain] autorelease];
+        border = [[[activeAnnotation border] retain] autorelease];
+        pathColor = [[activeAnnotation color] retain];
+        NSEnumerator *pathEnum = [bezierPaths objectEnumerator];
+        while (path = [pathEnum nextObject]) {
+            [path setLineCapStyle:NSRoundLineCapStyle];
+            [path setLineJoinStyle:NSRoundLineJoinStyle];
+            [path setLineWidth:[border lineWidth]];
         }
+        [self removeActiveAnnotation:nil];
+        path = [[bezierPaths lastObject] retain];
+        [path lineToPoint:pagePoint];
+        [self setNeedsDisplayInRect:[self convertRect:NSInsetRect([path 
nonEmptyBounds], -8.0, -8.0) fromPage:page]];
+        didDraw = YES;
     } else {
-        // Move annotation.
-        [[self documentView] autoscroll:theEvent];
-        
-        NSPoint mouseLoc = [self convertPoint:[theEvent locationInWindow] 
fromView:nil];
-        PDFPage *newActivePage = [self pageForPoint:mouseLoc nearest:YES];
-        
-        pageBounds = [newActivePage  boundsForBox:[self displayBox]];
-        
-        if (newActivePage == nil) {
-            // this should never happen, but just to be sure
-            newBounds = wasBounds;
-        } else {
-            if (newActivePage != page) {
-                // move the annotation to the new page
-                [self moveAnnotation:activeAnnotation toPage:newActivePage];
-                page = newActivePage;
-            }
-            
-            newBounds = currentBounds;
-            newBounds.origin = SKIntegralPoint(SKSubstractPoints([self 
convertPoint:mouseLoc toPage:page], clickDelta));
-            // constrain bounds inside page bounds
-            newBounds = SKConstrainRect(newBounds, pageBounds);
-        }
+        path = [NSBezierPath bezierPath];
+        [path setLineCapStyle:NSRoundLineCapStyle];
+        [path setLineJoinStyle:NSRoundLineJoinStyle];
+        [path setLineWidth:[[NSUserDefaults standardUserDefaults] 
floatForKey:SKInkNoteLineWidthKey]];
+        [path moveToPoint:pagePoint];
+        bezierPaths = [[NSArray alloc] initWithObjects:path, nil];
     }
+    pathPageIndex = [page pageIndex];
     
-    // Change annotation's location.
-    [activeAnnotation setBounds:newBounds];
+    [self setActiveAnnotation:nil];
+    
+    while (YES) {
+        theEvent = [[self window] nextEventMatchingMask: NSLeftMouseUpMask | 
NSLeftMouseDraggedMask];
+        if ([theEvent type] == NSLeftMouseUp)
+            break;
+        [path lineToPoint:[self convertPoint:[self convertPoint:[theEvent 
locationInWindow] fromView:nil] toPage:page]];
+        [self setNeedsDisplayInRect:[self convertRect:NSInsetRect([path 
nonEmptyBounds], -8.0, -8.0) fromPage:page]];
+        didDraw = YES;
+    }
+    
+    if (didDraw) {
+        [self addAnnotationWithType:SKInkNote contents:nil page:page 
bounds:NSZeroRect];
+        if (pathColor)
+            [activeAnnotation setColor:pathColor];
+        if (border)
+            [activeAnnotation setBorder:border];
+        if (text)
+            [activeAnnotation setString:text];
+        [[self undoManager] setActionName:NSLocalizedString(@"Add Note", 
@"Undo action name")];
+    }
+    
+    [bezierPaths release];
+    bezierPaths = nil;
+    [pathColor release];
+    pathColor = nil;
 }
 
 - (void)doDragWithEvent:(NSEvent *)theEvent {


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

------------------------------------------------------------------------------
Enter the BlackBerry Developer Challenge  
This is your chance to win up to $100,000 in prizes! For a limited time, 
vendors submitting new applications to BlackBerry App World(TM) will have
the opportunity to enter the BlackBerry Developer Challenge. See full prize  
details at: http://p.sf.net/sfu/Challenge
_______________________________________________
Skim-app-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/skim-app-commit

Reply via email to