/* Copyright (c) 2004-2007, Marcus Müller . 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 Mulle kybernetiK nor the names of its 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. */ #include "SelectiveView.h" #include "NSView+Extensions.h" @interface SelectiveView (PrivateAPI) - (void)setupSelectionRectAndMaskForFrame:(NSRect)_r; - (void)selectionRectDidChange; - (BOOL)shouldDisplaySelectionRect; - (void)setupTrackingRect; - (void)resetTrackingRect; - (BOOL)isMouseInTrackingRect; - (NSRect)resizeRect; - (NSPoint)resizeGripOrigin; @end @implementation SelectiveView static NSColor *borderColor = nil; static NSImage *resizeGrip = nil; + (void)initialize { static BOOL didInit = NO; if (didInit) return; didInit = YES; borderColor = [[NSColor whiteColor] retain]; resizeGrip = [[NSImage imageNamed:@"ResizeGrip"] retain]; } - (id)initWithFrame:(NSRect)frame { self = [super initWithFrame:frame]; if(self) { NSUserDefaults *ud; ud = [NSUserDefaults standardUserDefaults]; self->flags.useSelectionRect = [ud boolForKey:@"UseSelectionRect"]; [self setupSelectionRectAndMaskForFrame:frame]; } return self; } - (void)dealloc { [self->selectionMask release]; [super dealloc]; } /* Private API */ - (void)setupSelectionRectAndMaskForFrame:(NSRect)_r { float dw; dw = 180; self->selectionRect = NSMakeRect((_r.size.width - dw) / 2, (_r.size.height - dw) / 2, dw, dw); self->selectionMask = [[NSImage alloc] initWithSize:_r.size]; [self selectionRectDidChange]; } - (void)selectionRectDidChange { [self->selectionMask lockFocus]; [borderColor set]; NSRectFillUsingOperation([self bounds], NSCompositeCopy); NSRectFillUsingOperation(self->selectionRect, NSCompositeClear); [self->selectionMask unlockFocus]; #if 0 NSLog(@"%s rect:%@ frame:%@ mask:%@", __PRETTY_FUNCTION__, NSStringFromRect(self->selectionRect), NSStringFromRect([self frame]), NSStringFromSize([self->selectionMask size])); #endif [[self window] resetCursorRects]; if ([self shouldDisplaySelectionRect]) [self setNeedsDisplay:YES]; } - (BOOL)shouldDisplaySelectionRect { return self->flags.useSelectionRect ? YES : NO; } - (NSRect)resizeRect { NSRect r; float x, y; r = self->selectionRect; x = NSMaxX(r) - 16; y = r.origin.y; return NSMakeRect(x, y, 16, 16); } - (NSPoint)resizeGripOrigin { return NSMakePoint(NSMaxX(self->selectionRect) - 16 - 2, self->selectionRect.origin.y + 2); } - (void)setupTrackingRect { [self resetTrackingRect]; self->resizingRectTag = [self addTrackingRect:self->selectionRect owner:self userData:nil assumeInside:[self isMouseInTrackingRect]]; } - (void)resetTrackingRect { [self removeTrackingRect:self->resizingRectTag]; } - (BOOL)isMouseInTrackingRect { NSPoint p; p = [NSEvent mouseLocation]; p = [[self window] convertScreenToBase:p]; p = [self convertPoint:p fromView:nil]; return [self mouse:p inRect:self->selectionRect]; } /* Actions */ - (IBAction)toggleUseSelectionRect:(id)sender { self->flags.useSelectionRect = !self->flags.useSelectionRect; [[self window] resetCursorRects]; [self setNeedsDisplay:YES]; } /* Overriding NSView's behavior */ - (BOOL)isFlipped { return NO; /* stop the madness :-) */ } - (void)resetCursorRects { if (!self->flags.useSelectionRect) return; if (self->flags.isInMoveSession) { [self addCursorRect:self->selectionRect cursor:[NSCursor closedHandCursor]]; } else if(!self->flags.isInResizeSession) { NSCursor *c; NSRect r; NSSize rs; c = [NSCursor openHandCursor]; rs = [self resizeRect].size; r = NSMakeRect(self->selectionRect.origin.x, self->selectionRect.origin.y + rs.height, self->selectionRect.size.width, self->selectionRect.size.height - rs.height); [self addCursorRect:r cursor:c]; r = NSMakeRect(self->selectionRect.origin.x, self->selectionRect.origin.y, self->selectionRect.size.width - rs.width, rs.height); [self addCursorRect:r cursor:c]; [self addCursorRect:[self resizeRect] cursor:[NSCursor arrowCursor]]; } /* it's safe to do this here */ [self setupTrackingRect]; } - (NSImage *)image { if (!self->flags.useSelectionRect) return [super image]; return [self imageFromRect:self->selectionRect]; } -(void)drawRect:(NSRect)_r { [super drawRect:_r]; if ([self shouldDisplaySelectionRect]) { [self->selectionMask compositeToPoint:NSZeroPoint operation:NSCompositeSourceOver fraction:0.2f]; if (self->flags.shouldDrawResizeGrip) { [resizeGrip compositeToPoint:[self resizeGripOrigin] operation:NSCompositeSourceOver]; [[NSColor whiteColor] set]; NSFrameRect(self->selectionRect); } } } - (void)setFrameSize:(NSSize)_size { [super setFrameSize:_size]; [self->selectionMask release]; [self setupSelectionRectAndMaskForFrame:[self bounds]]; } - (void)mouseDragged:(NSEvent *)_ev { NSPoint loc; float dX, dY; if (!self->flags.useSelectionRect) { [super mouseDragged:_ev]; return; } else if (!self->flags.isInMoveSession && !self->flags.isInResizeSession) { loc = [self convertPoint:[_ev locationInWindow] fromView:nil]; /* did drag occur in resizeRect? (resize) */ if ([self mouse:loc inRect:[self resizeRect]]) { self->flags.isInResizeSession = YES; } /* or did drag occur in selectionRect? (move) */ else if ([self mouse:loc inRect:self->selectionRect]) { self->flags.isInMoveSession = YES; [[self window] resetCursorRects]; } } dX = [_ev deltaX]; dY = [_ev deltaY]; if(self->flags.isInMoveSession) { float maxX, maxY; self->selectionRect.origin.x += dX; self->selectionRect.origin.y -= dY; maxX = [self frame].size.width - self->selectionRect.size.width; maxY = [self frame].size.height - self->selectionRect.size.height; /* check bounds */ if (self->selectionRect.origin.x < 0) self->selectionRect.origin.x = 0; else if (self->selectionRect.origin.x > maxX) self->selectionRect.origin.x = maxX; if (self->selectionRect.origin.y < 0) self->selectionRect.origin.y = 0; else if (self->selectionRect.origin.y > maxY) self->selectionRect.origin.y = maxY; } else { float dXY; #if 0 if((dX < 0 && dY > 0) || (dX > 0 && dY < 0)) dXY = 0; else if(dX < 0 && dY < 0) dXY = MIN(dX, dY); else dXY = MAX(dX, dY); #else dXY = dY; #endif if (dXY > 0) { /* bigger */ float maxX, maxY, maxXY; maxX = [self frame].size.width - NSMaxX(self->selectionRect); maxY = self->selectionRect.origin.y; maxXY = MIN(maxX, maxY); if (dXY > maxXY) dXY = maxXY; self->selectionRect.size.width += dXY; self->selectionRect.size.height += dXY; self->selectionRect.origin.y -= dXY; } else { /* smaller */ float maxX, maxY, maxXY; maxX = self->selectionRect.size.width - 32; maxY = self->selectionRect.size.height - 32; maxXY = MIN(maxX, maxY); dXY = -dXY; if (dXY > maxXY) dXY = maxXY; self->selectionRect.size.width -= dXY; self->selectionRect.size.height -= dXY; self->selectionRect.origin.y += dXY; } } [self selectionRectDidChange]; } - (void)mouseUp:(NSEvent *)_ev { self->flags.isInMoveSession = NO; self->flags.isInResizeSession = NO; [[self window] resetCursorRects]; [super mouseUp:_ev]; } - (void)mouseEntered:(NSEvent *)_ev { if (!self->flags.shouldDrawResizeGrip) { if (![[self window] isKeyWindow]) return; self->flags.shouldDrawResizeGrip = YES; [self setNeedsDisplay:YES]; } } - (void)mouseExited:(NSEvent *)_ev { if (self->flags.shouldDrawResizeGrip) { self->flags.shouldDrawResizeGrip = NO; [self setNeedsDisplay:YES]; } } /* NSWindow delegate */ - (void)windowDidBecomeKey:(NSNotification *)_notif { if ([self isMouseInTrackingRect]) [self mouseEntered:nil]; } - (void)windowWillClose:(NSNotification *)_notif { [NSApp terminate:self]; } @end @implementation SelectiveView (AppleScriptConvenience) - (void)setUseSelectionRect:(BOOL)_yn { NSUserDefaults *ud; ud = [NSUserDefaults standardUserDefaults]; /* NOTE: "UseSelectionRect" is observed via KVO (Button uses Binding) We need to trigger KVO manually, so button's state gets changed accordingly */ [self willChangeValueForKey:@"UseSelectionRect"]; self->flags.useSelectionRect = _yn; [ud setBool:_yn forKey:@"UseSelectionRect"]; [self didChangeValueForKey:@"UseSelectionRect"]; [[self window] resetCursorRects]; [self setNeedsDisplay:YES]; } - (BOOL)useSelectionRect { return self->flags.useSelectionRect; } @end