Frameworks/AIUtilities Framework/Source/JVMarkedScroller.m
branchfix-sFlags
changeset 5993 95e8102125d0
parent 4759 ab1d8f004486
equal deleted inserted replaced
5989:1b816aafafdd 5993:95e8102125d0
    51 	[super dealloc];
    51 	[super dealloc];
    52 }
    52 }
    53 
    53 
    54 #pragma mark -
    54 #pragma mark -
    55 
    55 
       
    56 - (BOOL)isHorizontalScroller {
       
    57     return self.enclosingScrollView.horizontalScroller == self;
       
    58 }
       
    59 
    56 + (BOOL)isCompatibleWithOverlayScrollers {
    60 + (BOOL)isCompatibleWithOverlayScrollers {
    57     return self == [JVMarkedScroller class];
    61     return self == [JVMarkedScroller class];
    58 }
    62 }
    59 
    63 
    60 - (void) drawRect:(NSRect) rect {
    64 - (void) drawRect:(NSRect) rect {
    63 	NSAffineTransform *transform = [NSAffineTransform transform];
    67 	NSAffineTransform *transform = [NSAffineTransform transform];
    64 	//Use -scrollerWidthForControlSize:scrollerStyle: on 10.7+
    68 	//Use -scrollerWidthForControlSize:scrollerStyle: on 10.7+
    65 	CGFloat width = [[self class] scrollerWidthForControlSize:[self controlSize]];
    69 	CGFloat width = [[self class] scrollerWidthForControlSize:[self controlSize]];
    66 	
    70 	
    67 	CGFloat scale = [self scaleToContentView];
    71 	CGFloat scale = [self scaleToContentView];
    68 	[transform scaleXBy:( sFlags.isHoriz ? scale : 1.f ) yBy:( sFlags.isHoriz ? 1.f : scale )];
    72 	[transform scaleXBy:( self.isHorizontalScroller ? scale : 1.f ) yBy:( self.isHorizontalScroller ? 1.f : scale )];
    69 	
    73 	
    70 	CGFloat offset = [self rectForPart:NSScrollerKnobSlot].origin.y;
    74 	CGFloat offset = [self rectForPart:NSScrollerKnobSlot].origin.y;
    71 	[transform translateXBy:( sFlags.isHoriz ? offset / scale : 0.f ) yBy:( sFlags.isHoriz ? 0.f : offset / scale )];
    75 	[transform translateXBy:( self.isHorizontalScroller ? offset / scale : 0.f ) yBy:( self.isHorizontalScroller ? 0.f : offset / scale )];
    72 	
    76 	
    73 	NSBezierPath *shades = [NSBezierPath bezierPath];
    77 	NSBezierPath *shades = [NSBezierPath bezierPath];
    74 	NSEnumerator *enumerator = [_shades objectEnumerator];
    78 	NSEnumerator *enumerator = [_shades objectEnumerator];
    75 	NSNumber *startNum = nil;
    79 	NSNumber *startNum = nil;
    76 	NSNumber *stopNum = nil;
    80 	NSNumber *stopNum = nil;
    78 	while( ( startNum = [enumerator nextObject] ) && ( stopNum = [enumerator nextObject] ) ) {
    82 	while( ( startNum = [enumerator nextObject] ) && ( stopNum = [enumerator nextObject] ) ) {
    79 		NSUInteger start = [startNum unsignedIntegerValue];
    83 		NSUInteger start = [startNum unsignedIntegerValue];
    80 		NSUInteger stop = [stopNum unsignedIntegerValue];
    84 		NSUInteger stop = [stopNum unsignedIntegerValue];
    81 		
    85 		
    82 		NSRect shadesRect = NSZeroRect;
    86 		NSRect shadesRect = NSZeroRect;
    83 		if( sFlags.isHoriz ) shadesRect = NSMakeRect( start, 0.f, ( stop - start ), width );
    87 		if( self.isHorizontalScroller ) shadesRect = NSMakeRect( start, 0.f, ( stop - start ), width );
    84 		else shadesRect = NSMakeRect( 0.f, start, width, ( stop - start ) );
    88 		else shadesRect = NSMakeRect( 0.f, start, width, ( stop - start ) );
    85 		
    89 		
    86 		shadesRect.origin = [transform transformPoint:shadesRect.origin];
    90 		shadesRect.origin = [transform transformPoint:shadesRect.origin];
    87 		shadesRect.size = [transform transformSize:shadesRect.size];
    91 		shadesRect.size = [transform transformSize:shadesRect.size];
    88 		
    92 		
    92 	if( ( [_shades count] % 2 ) == 1 ) {
    96 	if( ( [_shades count] % 2 ) == 1 ) {
    93 		NSRect shadesRect = NSZeroRect;
    97 		NSRect shadesRect = NSZeroRect;
    94 		NSUInteger start = [[_shades lastObject] unsignedIntegerValue];
    98 		NSUInteger start = [[_shades lastObject] unsignedIntegerValue];
    95 		CGFloat stop = [self contentViewLength];
    99 		CGFloat stop = [self contentViewLength];
    96 		
   100 		
    97 		if( sFlags.isHoriz ) shadesRect = NSMakeRect( start, 0.f, ( stop - start ), width );
   101 		if( self.isHorizontalScroller ) shadesRect = NSMakeRect( start, 0.f, ( stop - start ), width );
    98 		else shadesRect = NSMakeRect( 0.f, start, width, ( stop - start ) );
   102 		else shadesRect = NSMakeRect( 0.f, start, width, ( stop - start ) );
    99 		
   103 		
   100 		shadesRect.origin = [transform transformPoint:shadesRect.origin];
   104 		shadesRect.origin = [transform transformPoint:shadesRect.origin];
   101 		shadesRect.size = [transform transformSize:shadesRect.size];
   105 		shadesRect.size = [transform transformSize:shadesRect.size];
   102 		
   106 		
   132 		if( value > currentPosition && ( ! foundNext || value < _nearestNextMark ) ) {
   136 		if( value > currentPosition && ( ! foundNext || value < _nearestNextMark ) ) {
   133 			_nearestNextMark = value;
   137 			_nearestNextMark = value;
   134 			foundNext = YES;
   138 			foundNext = YES;
   135 		}
   139 		}
   136 		
   140 		
   137 		NSPoint point = NSMakePoint( ( sFlags.isHoriz ? value : 0 ), ( sFlags.isHoriz ? 0 : value ) );
   141 		NSPoint point = NSMakePoint( ( self.isHorizontalScroller ? value : 0 ), ( self.isHorizontalScroller ? 0 : value ) );
   138 		point = [transform transformPoint:point];
   142 		point = [transform transformPoint:point];
   139 		point.x = ( sFlags.isHoriz ? AIround( point.x ) + 0.5f : point.x );
   143 		point.x = ( self.isHorizontalScroller ? AIround( point.x ) + 0.5f : point.x );
   140 		point.y = ( sFlags.isHoriz ? point.y : AIround( point.y ) + 0.5f );
   144 		point.y = ( self.isHorizontalScroller ? point.y : AIround( point.y ) + 0.5f );
   141 		
   145 		
   142 		if( ! NSPointInRect( point, knobRect ) ) {
   146 		if( ! NSPointInRect( point, knobRect ) ) {
   143 			if( mark.color ) {
   147 			if( mark.color ) {
   144 				NSBezierPath *line = [NSBezierPath bezierPath];
   148 				NSBezierPath *line = [NSBezierPath bezierPath];
   145 				[line moveToPoint:point];
   149 				[line moveToPoint:point];
   146 				
   150 				
   147 				point = NSMakePoint( ( sFlags.isHoriz ? 0.f : width ), ( sFlags.isHoriz ? width : 0.f ) );
   151 				point = NSMakePoint( ( self.isHorizontalScroller ? 0.f : width ), ( self.isHorizontalScroller ? width : 0.f ) );
   148 				[line relativeLineToPoint:point];
   152 				[line relativeLineToPoint:point];
   149 				[line setLineWidth:2];
   153 				[line setLineWidth:2];
   150 				[lineArray addObject:mark.color];
   154 				[lineArray addObject:mark.color];
   151 				[lineArray addObject:line];
   155 				[lineArray addObject:line];
   152 			} else {
   156 			} else {
   153 				[lines moveToPoint:point];
   157 				[lines moveToPoint:point];
   154 				
   158 				
   155 				point = NSMakePoint( ( sFlags.isHoriz ? 0.f : width ), ( sFlags.isHoriz ? width : 0.f ) );
   159 				point = NSMakePoint( ( self.isHorizontalScroller ? 0.f : width ), ( self.isHorizontalScroller ? width : 0.f ) );
   156 				[lines relativeLineToPoint:point];
   160 				[lines relativeLineToPoint:point];
   157 			}
   161 			}
   158 		}
   162 		}
   159 	}
   163 	}
   160 	
   164 	
   202 									   action:@selector( removeAllMarks ) 
   206 									   action:@selector( removeAllMarks ) 
   203 								keyEquivalent:@""] autorelease];
   207 								keyEquivalent:@""] autorelease];
   204 	[item setTarget:self];
   208 	[item setTarget:self];
   205 	[menu addItem:item];
   209 	[menu addItem:item];
   206 	
   210 	
   207 	if( sFlags.isHoriz ) {
   211 	if( self.isHorizontalScroller ) {
   208 		item = [[[NSMenuItem alloc] initWithTitle:AILocalizedStringFromTableInBundle( @"Clear Marks from Here Left", nil, [NSBundle bundleWithIdentifier:AIUTILITIES_BUNDLE_ID], "clear marks from here left contextual menu") 
   212 		item = [[[NSMenuItem alloc] initWithTitle:AILocalizedStringFromTableInBundle( @"Clear Marks from Here Left", nil, [NSBundle bundleWithIdentifier:AIUTILITIES_BUNDLE_ID], "clear marks from here left contextual menu") 
   209 										   action:@selector( clearMarksHereLess: ) 
   213 										   action:@selector( clearMarksHereLess: ) 
   210 									keyEquivalent:@""] autorelease];
   214 									keyEquivalent:@""] autorelease];
   211 		[item setTarget:self];
   215 		[item setTarget:self];
   212 		[menu addItem:item];
   216 		[menu addItem:item];
   289 - (IBAction) clearMarksHereLess:(id) sender {
   293 - (IBAction) clearMarksHereLess:(id) sender {
   290 	NSEvent *event = [[NSApplication sharedApplication] currentEvent];
   294 	NSEvent *event = [[NSApplication sharedApplication] currentEvent];
   291 	NSPoint where = [self convertPoint:[event locationInWindow] fromView:nil];
   295 	NSPoint where = [self convertPoint:[event locationInWindow] fromView:nil];
   292 	NSRect slotRect = [self rectForPart:NSScrollerKnobSlot];
   296 	NSRect slotRect = [self rectForPart:NSScrollerKnobSlot];
   293 	CGFloat scale = [self scaleToContentView];
   297 	CGFloat scale = [self scaleToContentView];
   294 	[self removeMarksLessThan:( ( sFlags.isHoriz ? where.x - NSMinX( slotRect ) : where.y - NSMinY( slotRect ) ) / scale )];
   298 	[self removeMarksLessThan:( ( self.isHorizontalScroller ? where.x - NSMinX( slotRect ) : where.y - NSMinY( slotRect ) ) / scale )];
   295 }
   299 }
   296 
   300 
   297 - (IBAction) clearMarksHereGreater:(id) sender {
   301 - (IBAction) clearMarksHereGreater:(id) sender {
   298 	NSEvent *event = [[NSApplication sharedApplication] currentEvent];
   302 	NSEvent *event = [[NSApplication sharedApplication] currentEvent];
   299 	NSPoint where = [self convertPoint:[event locationInWindow] fromView:nil];
   303 	NSPoint where = [self convertPoint:[event locationInWindow] fromView:nil];
   300 	NSRect slotRect = [self rectForPart:NSScrollerKnobSlot];
   304 	NSRect slotRect = [self rectForPart:NSScrollerKnobSlot];
   301 	CGFloat scale = [self scaleToContentView];
   305 	CGFloat scale = [self scaleToContentView];
   302 	[self removeMarksGreaterThan:( ( sFlags.isHoriz ? where.x - NSMinX( slotRect ) : where.y - NSMinY( slotRect ) ) / scale )];
   306 	[self removeMarksGreaterThan:( ( self.isHorizontalScroller ? where.x - NSMinX( slotRect ) : where.y - NSMinY( slotRect ) ) / scale )];
   303 }
   307 }
   304 
   308 
   305 #pragma mark -
   309 #pragma mark -
   306 
   310 
   307 - (void) setLocationOfCurrentMark:(NSUInteger) location {
   311 - (void) setLocationOfCurrentMark:(NSUInteger) location {
   587 }
   591 }
   588 
   592 
   589 #pragma mark -
   593 #pragma mark -
   590 
   594 
   591 - (CGFloat) contentViewLength {
   595 - (CGFloat) contentViewLength {
   592 	if( sFlags.isHoriz ) return ( NSWidth( [self frame] ) / [self knobProportion] );
   596 	if( self.isHorizontalScroller ) return ( NSWidth( [self frame] ) / [self knobProportion] );
   593 	else return ( NSHeight( [self frame] ) / [self knobProportion] );
   597 	else return ( NSHeight( [self frame] ) / [self knobProportion] );
   594 }
   598 }
   595 
   599 
   596 - (CGFloat) scaleToContentView {
   600 - (CGFloat) scaleToContentView {
   597 	if( sFlags.isHoriz ) return NSWidth( [self rectForPart:NSScrollerKnobSlot] ) / NSWidth( [[(NSScrollView *)[self superview] contentView] documentRect] );
   601 	if( self.isHorizontalScroller ) return NSWidth( [self rectForPart:NSScrollerKnobSlot] ) / NSWidth( [[(NSScrollView *)[self superview] contentView] documentRect] );
   598 	else return NSHeight( [self rectForPart:NSScrollerKnobSlot] ) / NSHeight( [[(NSScrollView *)[self superview] contentView] documentRect] );
   602 	else return NSHeight( [self rectForPart:NSScrollerKnobSlot] ) / NSHeight( [[(NSScrollView *)[self superview] contentView] documentRect] );
   599 }
   603 }
   600 
   604 
   601 - (CGFloat) shiftAmountToCenterAlign {
   605 - (CGFloat) shiftAmountToCenterAlign {
   602 	CGFloat scale = [self scaleToContentView];
   606 	CGFloat scale = [self scaleToContentView];
   603 	if( sFlags.isHoriz ) return ( ( NSWidth( [self rectForPart:NSScrollerKnobSlot] ) * [self knobProportion] ) / 2.f ) / scale;
   607 	if( self.isHorizontalScroller ) return ( ( NSWidth( [self rectForPart:NSScrollerKnobSlot] ) * [self knobProportion] ) / 2.f ) / scale;
   604 	else return ( ( NSHeight( [self rectForPart:NSScrollerKnobSlot] ) * [self knobProportion] ) / 2.f ) / scale;
   608 	else return ( ( NSHeight( [self rectForPart:NSScrollerKnobSlot] ) * [self knobProportion] ) / 2.f ) / scale;
   605 }
   609 }
   606 @end
   610 @end