* Minor fixes to glitches detected by the clang static analyzer.
authorJens Alfke <jens@mooseyard.com>
Sun Jul 13 10:45:42 2008 -0700 (2008-07-13)
changeset 16ce9c83f7ec14
parent 15 06e7d03fd23e
child 17 a1044ae95953
* Minor fixes to glitches detected by the clang static analyzer.
* Added MYAnimatingSplitView class.
CollectionUtils.h
ExceptionUtils.m
FileAlias.m
MYAnimatingSplitView.h
MYAnimatingSplitView.m
Test.m
     1.1 --- a/CollectionUtils.h	Tue Jun 24 12:05:24 2008 -0700
     1.2 +++ b/CollectionUtils.h	Sun Jul 13 10:45:42 2008 -0700
     1.3 @@ -7,6 +7,7 @@
     1.4  //
     1.5  
     1.6  #import <Foundation/Foundation.h>
     1.7 +#define _MYUTILITIES_COLLECTIONUTILS_ 1
     1.8  
     1.9  // Collection creation conveniences:
    1.10  
     2.1 --- a/ExceptionUtils.m	Tue Jun 24 12:05:24 2008 -0700
     2.2 +++ b/ExceptionUtils.m	Sun Jul 13 10:45:42 2008 -0700
     2.3 @@ -166,7 +166,6 @@
     2.4      // From: <http://lists.apple.com/archives/Xcode-users/2004/Feb/msg00241.html>
     2.5      int mib[4];
     2.6      size_t bufSize = 0;
     2.7 -    int local_error = 0;
     2.8      struct kinfo_proc kp;
     2.9      
    2.10      mib[0] = CTL_KERN;
    2.11 @@ -175,8 +174,8 @@
    2.12      mib[3] = getpid();
    2.13      
    2.14      bufSize = sizeof (kp);
    2.15 -    if ((local_error = sysctl(mib, 4, &kp, &bufSize, NULL, 0)) < 0) {
    2.16 -        Warn(@"Failure calling sysctl");
    2.17 +    if (sysctl(mib, 4, &kp, &bufSize, NULL, 0) < 0) {
    2.18 +        Warn(@"Error %i calling sysctl",errno);
    2.19          return NO;
    2.20      }
    2.21      return (kp.kp_proc.p_flag & P_TRACED) != 0;
     3.1 --- a/FileAlias.m	Tue Jun 24 12:05:24 2008 -0700
     3.2 +++ b/FileAlias.m	Sun Jul 13 10:45:42 2008 -0700
     3.3 @@ -199,7 +199,7 @@
     3.4      FSRef matches[count];
     3.5      
     3.6  #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
     3.7 -    if( ! FSMatchAliasBulk(fromRefPtr, rules, _alias, &count, matches, &wasChanged, NULL, NULL), error ) {
     3.8 +    if( ! CheckOSErr( FSMatchAliasBulk(fromRefPtr, rules, _alias, &count, matches, &wasChanged, NULL, NULL), error) ) {
     3.9          NSLog(@"%@: FSMatchAliasBulk failed!",self);
    3.10          return nil;
    3.11      }
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/MYAnimatingSplitView.h	Sun Jul 13 10:45:42 2008 -0700
     4.3 @@ -0,0 +1,43 @@
     4.4 +//
     4.5 +//  MYAnimatingSplitView.h
     4.6 +//  Cloudy
     4.7 +//
     4.8 +//  Created by Jens Alfke on 7/10/08.
     4.9 +//  Copyright 2008 Jens Alfke. All rights reserved.
    4.10 +//
    4.11 +
    4.12 +#import <AppKit/NSSplitView.h>
    4.13 +
    4.14 +
    4.15 +@interface MYAnimatingSplitView : NSSplitView
    4.16 +{
    4.17 +    NSTimeInterval _animationTime;
    4.18 +    BOOL _isLiveResizing;
    4.19 +}
    4.20 +
    4.21 +
    4.22 +/** The maximum time it will take to animate the divider. (Actual time depends on distance moved.) */
    4.23 +@property NSTimeInterval animationTime;
    4.24 +
    4.25 +/** Pixel position of the divider (in the splitview's bounds' coordinate system.)
    4.26 +    Setting this property animates the divider to the new position. */
    4.27 +@property float dividerPosition;
    4.28 +
    4.29 +/** Position of the divider, scaled to the range [0..1]. */
    4.30 +@property float dividerFractionalPosition;
    4.31 +
    4.32 +- (void) collapseSubviewAtIndex: (int)index;
    4.33 +- (void) collapseSubviewAtIndex: (int)index animate: (BOOL)animate;
    4.34 +
    4.35 +/** Returns YES while the splitview itself is being resized (i.e. while the window
    4.36 +    is resizing, or a parent splitview is moving its divider.) */
    4.37 +@property (readonly) BOOL isLiveResizing;
    4.38 +
    4.39 +@end
    4.40 +
    4.41 +
    4.42 +@interface NSObject (MYAnimatingSplitViewDelegate)
    4.43 +/** If the delegate implements this method, it will be called when the splitview
    4.44 +    begins and ends live resizing. */
    4.45 +- (void)splitView: (NSSplitView*)splitView inLiveResize: (BOOL)inLiveResize;
    4.46 +@end
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/MYAnimatingSplitView.m	Sun Jul 13 10:45:42 2008 -0700
     5.3 @@ -0,0 +1,140 @@
     5.4 +//
     5.5 +//  MYAnimatingSplitView.m
     5.6 +//  Cloudy
     5.7 +//
     5.8 +//  Created by Jens Alfke on 7/10/08.
     5.9 +//  Copyright 2008 Jens Alfke. All rights reserved.
    5.10 +//
    5.11 +
    5.12 +#import "MYAnimatingSplitView.h"
    5.13 +#import "DateUtils.h"
    5.14 +
    5.15 +
    5.16 +#define kDefaultAnimationTime 0.3
    5.17 +
    5.18 +
    5.19 +@interface MYAnimatingSplitView ()
    5.20 +@property (readwrite) BOOL isLiveResizing;
    5.21 +@end
    5.22 +
    5.23 +
    5.24 +@implementation MYAnimatingSplitView
    5.25 +
    5.26 +
    5.27 +@synthesize animationTime=_animationTime;
    5.28 +
    5.29 +
    5.30 +- (void) my_animateDividerToPosition: (float)pos
    5.31 +{
    5.32 +    const NSTimeInterval maxTime = _animationTime ?: kDefaultAnimationTime;
    5.33 +    
    5.34 +    NSTimeInterval startTime = TimeIntervalSinceBoot();
    5.35 +    float startPos = self.dividerPosition;
    5.36 +    const NSTimeInterval animationTime = maxTime * fabs(pos-startPos)/self.frame.size.height;
    5.37 +    float fract;
    5.38 +    do {
    5.39 +        fract = (float) MIN(1.0, (TimeIntervalSinceBoot()-startTime)/animationTime);
    5.40 +        float curPos = startPos + fract*(pos-startPos);
    5.41 +        [self setPosition: curPos ofDividerAtIndex: 0];
    5.42 +        [self.window displayIfNeeded];
    5.43 +        [self.window update];
    5.44 +    }while( fract < 1.0 );
    5.45 +}
    5.46 +
    5.47 +
    5.48 +- (void) getDividerPositionMin: (float*)outMin max: (float*)outMax
    5.49 +{
    5.50 +    *outMin = [self.delegate splitView: self 
    5.51 +                    constrainMinCoordinate: [self minPossiblePositionOfDividerAtIndex: 0]
    5.52 +                    ofSubviewAt: 0];
    5.53 +    *outMax = [self.delegate splitView: self 
    5.54 +                    constrainMaxCoordinate: [self maxPossiblePositionOfDividerAtIndex: 0]
    5.55 +                    ofSubviewAt: 0];
    5.56 +}
    5.57 +
    5.58 +- (float) dividerPosition
    5.59 +{
    5.60 +    return NSMaxY([[self.subviews objectAtIndex: 0] frame]);
    5.61 +}
    5.62 +
    5.63 +- (void) setDividerPosition: (float)pos
    5.64 +{
    5.65 +    float minPos,maxPos;
    5.66 +    [self getDividerPositionMin: &minPos max: &maxPos];
    5.67 +    NSView *targetView = [[self subviews] objectAtIndex:0];
    5.68 +    NSRect startFrame = [targetView frame];
    5.69 +
    5.70 +    // First uncollapse any collapsed subview:
    5.71 +    if( [self isSubviewCollapsed: targetView] )
    5.72 +        [self setPosition: minPos ofDividerAtIndex: 0];
    5.73 +    else if( startFrame.size.height > maxPos )
    5.74 +        [self setPosition: maxPos ofDividerAtIndex: 0];
    5.75 +    // Now animate:
    5.76 +    [self my_animateDividerToPosition: pos];
    5.77 +}
    5.78 +
    5.79 +- (float) dividerFractionalPosition
    5.80 +{
    5.81 +    float minPos, maxPos, pos=self.dividerPosition;
    5.82 +    [self getDividerPositionMin: &minPos max: &maxPos];
    5.83 +    float denom = maxPos-minPos;
    5.84 +    if( denom<=0 )
    5.85 +        return 0.0;
    5.86 +    else
    5.87 +        return (pos-minPos)/denom;
    5.88 +}
    5.89 +
    5.90 +- (void) setDividerFractionalPosition: (float)fract
    5.91 +{
    5.92 +    float minPos, maxPos;
    5.93 +    [self getDividerPositionMin: &minPos max: &maxPos];
    5.94 +    self.dividerPosition = roundf( minPos + fract*(maxPos-minPos) );
    5.95 +}
    5.96 +
    5.97 +
    5.98 +- (void) collapseSubviewAtIndex: (int)index animate: (BOOL)animate
    5.99 +{
   5.100 +    if( ! [self isSubviewCollapsed: [self.subviews objectAtIndex: index]] ) {
   5.101 +        float pos = index==0 ?[self minPossiblePositionOfDividerAtIndex: 0]
   5.102 +                             :[self maxPossiblePositionOfDividerAtIndex: 0];
   5.103 +        if( animate )
   5.104 +            [self my_animateDividerToPosition: pos];
   5.105 +        else
   5.106 +            [self setPosition: pos ofDividerAtIndex: 0];
   5.107 +    }
   5.108 +}
   5.109 +
   5.110 +- (void) collapseSubviewAtIndex: (int)index
   5.111 +{
   5.112 +    [self collapseSubviewAtIndex: index animate: YES];
   5.113 +}
   5.114 +
   5.115 +
   5.116 +
   5.117 +- (BOOL) isLiveResizing
   5.118 +{
   5.119 +    return _isLiveResizing;
   5.120 +}
   5.121 +
   5.122 +- (void) setIsLiveResizing: (BOOL)inLiveResize
   5.123 +{
   5.124 +    _isLiveResizing = inLiveResize;
   5.125 +    id delegate = self.delegate;
   5.126 +    if( [delegate respondsToSelector: @selector(splitView:inLiveResize:)] )
   5.127 +        [delegate splitView: self inLiveResize: inLiveResize];
   5.128 +}
   5.129 +
   5.130 +
   5.131 +- (void)viewWillStartLiveResize
   5.132 +{
   5.133 +    [super viewWillStartLiveResize];
   5.134 +    self.isLiveResizing = YES;
   5.135 +}
   5.136 +
   5.137 +- (void) viewDidEndLiveResize
   5.138 +{
   5.139 +    [super viewDidEndLiveResize];
   5.140 +    self.isLiveResizing = NO;
   5.141 +}
   5.142 +
   5.143 +@end
     6.1 --- a/Test.m	Tue Jun 24 12:05:24 2008 -0700
     6.2 +++ b/Test.m	Sun Jul 13 10:45:42 2008 -0700
     6.3 @@ -132,6 +132,7 @@
     6.4          va_list args;
     6.5          va_start(args,message);
     6.6          message = [[[NSString alloc] initWithFormat: message arguments: args] autorelease];
     6.7 +        message = [@"Assertion failed: " stringByAppendingString: message];
     6.8          va_end(args);
     6.9      } else
    6.10          message = [NSString stringWithUTF8String: condString];