Permalink
Browse files

Merge pull request #59 from bww/master

Scroll view improvements + miscellany
  • Loading branch information...
2 parents adfcee6 + 381ec98 commit da9c067aa9d347a2eb5893b9e61a6c3f9bb17867 @atebits atebits committed Sep 6, 2011
View
@@ -18,14 +18,15 @@
@interface TUIImage : NSObject
{
- CGImageRef _imageRef;
+ CGImageRef _imageRef;
}
+ (TUIImage *)imageNamed:(NSString *)name;
+ (TUIImage *)imageNamed:(NSString *)name cache:(BOOL)shouldCache;
+ (TUIImage *)imageWithData:(NSData *)data;
+ (TUIImage *)imageWithCGImage:(CGImageRef)imageRef;
++ (TUIImage *)imageWithNSImage:(NSImage *)image;
+ (TUIImage *)_imageWithABImage:(id)abimage __attribute__((deprecated)); // don't use this
View
@@ -115,6 +115,66 @@ + (TUIImage *)imageWithCGImage:(CGImageRef)imageRef
return [[[self alloc] initWithCGImage:imageRef] autorelease];
}
+/**
+ * @brief Create a new TUIImage from an NSImage
+ *
+ * @note Don't use this method in -drawRect: if you use a NSGraphicsContext. This method may
+ * change the current context in order to convert the image and will not restore any previous
+ * context.
+ *
+ * @param image an NSImage
+ * @return TUIImage
+ */
++ (TUIImage *)imageWithNSImage:(NSImage *)image {
+
+#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
+ // first, attempt to find an NSBitmapImageRep representation, (the easy way)
+ for(NSImageRep *rep in [image representations]){
+ CGImageRef cgImage;
+ if([rep isKindOfClass:[NSBitmapImageRep class]] && (cgImage = [(NSBitmapImageRep *)rep CGImage]) != nil){
+ return [[[TUIImage alloc] initWithCGImage:cgImage] autorelease];
+ }
+ }
+#endif
+
+ // if that didn't work, we have to render the image to a context and create the CGImage
+ // from that (the hard way)
+ TUIImage *result = nil;
+
+ CGColorSpaceRef colorspace = NULL;
+ CGContextRef context = NULL;
+ CGBitmapInfo info = kCGImageAlphaPremultipliedLast;
+
+ size_t width = (size_t)ceil(image.size.width);
+ size_t height = (size_t)ceil(image.size.height);
+ size_t bytesPerPixel = 4;
+ size_t bitsPerComponent = 8;
+
+ // create a colorspace for our image
+ if((colorspace = CGColorSpaceCreateDeviceRGB()) != NULL){
+ // create a context for our image using premultiplied RGBA
+ if((context = CGBitmapContextCreate(NULL, width, height, bitsPerComponent, width * bytesPerPixel, colorspace, info)) != NULL){
+
+ // setup an NSGraphicsContext for our bitmap context and render our NSImage into it
+ [NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:FALSE]];
+ [image drawAtPoint:CGPointMake(0, 0) fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1];
+
+ // create an image from the context and use that to create our TUIImage
+ CGImageRef cgImage;
+ if((cgImage = CGBitmapContextCreateImage(context)) != NULL){
+ result = [[[TUIImage alloc] initWithCGImage:cgImage] autorelease];
+ CFRelease(cgImage);
+ }
+
+ CFRelease(context);
+ }
+ CFRelease(colorspace);
+ }
+
+ // return the (hopefully sucessfully initialized) TUIImage
+ return result;
+}
+
- (CGSize)size
{
return CGSizeMake(CGImageGetWidth(_imageRef), CGImageGetHeight(_imageRef));
View
@@ -22,7 +22,7 @@ @implementation TUIImageView
- (id)initWithImage:(TUIImage *)image
{
- if((self = [self initWithFrame:CGRectZero]))
+ if((self = [self initWithFrame:(image != nil) ? CGRectMake(0, 0, image.size.width, image.size.height) : CGRectZero]) != nil)
{
self.userInteractionEnabled = NO;
_image = [image retain];
View
@@ -251,6 +251,14 @@ - (void)mouseMoved:(NSEvent *)event
[self _updateHoverViewWithEvent:event];
}
+-(void)mouseEntered:(NSEvent *)event {
+ [self _updateHoverViewWithEvent:event];
+}
+
+-(void)mouseExited:(NSEvent *)event {
+ [self _updateHoverViewWithEvent:event];
+}
+
- (void)rightMouseDown:(NSEvent *)event
{
[_trackingView release];
@@ -29,11 +29,13 @@
unsigned int hover:1;
unsigned int active:1;
unsigned int trackingInsideKnob:1;
+ unsigned int scrollIndicatorStyle:2;
} _scrollKnobFlags;
}
-@property (nonatomic, assign) TUIScrollView *scrollView;
-@property (nonatomic, readonly) TUIView *knob;
+@property (nonatomic, assign) TUIScrollView * scrollView;
+@property (nonatomic, assign) unsigned int scrollIndicatorStyle;
+@property (nonatomic, readonly) TUIView * knob;
- (void)flash;
View
@@ -118,6 +118,23 @@ - (void)flash
[knob.layer addAnimation:animation forKey:@"opacity"];
}
+-(unsigned int)scrollIndicatorStyle {
+ return _scrollKnobFlags.scrollIndicatorStyle;
+}
+
+-(void)setScrollIndicatorStyle:(unsigned int)style {
+ _scrollKnobFlags.scrollIndicatorStyle = style;
+ switch(style){
+ case TUIScrollViewIndicatorStyleLight:
+ knob.backgroundColor = [TUIColor whiteColor];
+ break;
+ case TUIScrollViewIndicatorStyleDark:
+ default:
+ knob.backgroundColor = [TUIColor blackColor];
+ break;
+ }
+}
+
- (void)_updateKnobColor:(CGFloat)duration
{
[TUIView beginAnimations:nil context:NULL];
@@ -130,12 +147,16 @@ - (void)mouseEntered:(NSEvent *)event
{
_scrollKnobFlags.hover = 1;
[self _updateKnobColor:0.08];
+ // make sure we propagate mouse events
+ [super mouseEntered:event];
}
- (void)mouseExited:(NSEvent *)event
{
_scrollKnobFlags.hover = 0;
[self _updateKnobColor:0.25];
+ // make sure we propagate mouse events
+ [super mouseExited:event];
}
- (void)mouseDown:(NSEvent *)event
@@ -171,12 +192,15 @@ - (void)mouseDown:(NSEvent *)event
[scrollView setContentOffset:contentOffset animated:YES];
}
+
+ [super mouseDown:event];
}
- (void)mouseUp:(NSEvent *)event
{
_scrollKnobFlags.active = 0;
[self _updateKnobColor:0.08];
+ [super mouseUp:event];
}
#define KNOB_CALCULATIONS_REVERSE(OFFSET, LENGTH) \
View
@@ -18,9 +18,32 @@
#import "TUIGeometry.h"
typedef enum {
- TUIScrollViewIndicatorStyleDefault,
+ /** Dark scroll indicator style suitable for light background */
+ TUIScrollViewIndicatorStyleDark,
+ /** Light scroll indicator style suitable for dark backgrounds */
+ TUIScrollViewIndicatorStyleLight,
+ /** Default scroll indicator style (dark) */
+ TUIScrollViewIndicatorStyleDefault = TUIScrollViewIndicatorStyleDark
} TUIScrollViewIndicatorStyle;
+typedef enum {
+ /** Never show scrollers */
+ TUIScrollViewIndicatorVisibleNever,
+ /** Show scrollers only during an animated scroll (not particularly useful yet) */
+ TUIScrollViewIndicatorVisibleWhenScrolling,
+ /** Show scrollers only when the mouse is inside the scroll view */
+ TUIScrollViewIndicatorVisibleWhenMouseInside,
+ /** Always show scrollers */
+ TUIScrollViewIndicatorVisibleAlways,
+ /** Default scroller visibility (always) */
+ TUIScrollViewIndicatorVisibleDefault = TUIScrollViewIndicatorVisibleAlways
+} TUIScrollViewIndicatorVisibility;
+
+typedef enum {
+ TUIScrollViewIndicatorVertical,
+ TUIScrollViewIndicatorHorizontal,
+} TUIScrollViewIndicator;
+
@protocol TUIScrollViewDelegate;
@class TUIScrollKnob;
@@ -49,14 +72,15 @@ typedef enum {
@interface TUIScrollView : TUIView
{
- CGPoint _unroundedContentOffset;
- CGSize _contentSize;
- CGSize resizeKnobSize;
- TUIEdgeInsets _contentInset;
+ CGPoint _unroundedContentOffset;
+ CGSize _contentSize;
+ CGSize resizeKnobSize;
+ TUIEdgeInsets _contentInset;
id _delegate;
- TUIScrollKnob *_verticalScrollKnob;
- TUIScrollKnob *_horizontalScrollKnob;
+
+ TUIScrollKnob * _verticalScrollKnob;
+ TUIScrollKnob * _horizontalScrollKnob;
NSTimer *scrollTimer;
CGPoint destinationOffset;
@@ -92,23 +116,31 @@ typedef enum {
BOOL pulling; // horizontal pulling not done yet, this flag should be split
} _pull;
- CGPoint _dragScrollLocation;
+ CGPoint _dragScrollLocation;
BOOL x;
struct {
unsigned int didChangeContentInset:1;
unsigned int bounceEnabled:1;
+ unsigned int mouseInside:1;
+ unsigned int mouseDownInScrollKnob:1;
unsigned int ignoreNextScrollPhaseNormal_10_7:1;
unsigned int gestureBegan:1;
unsigned int animationMode:2;
unsigned int scrollDisabled:1;
- unsigned int indicatorStyle:2;
- unsigned int showsHorizontalScrollIndicator:1;
- unsigned int showsVerticalScrollIndicator:1;
+ unsigned int scrollIndicatorStyle:2;
+ unsigned int verticalScrollIndicatorVisibility:2;
+ unsigned int horizontalScrollIndicatorVisibility:2;
+ unsigned int verticalScrollIndicatorShowing:1;
+ unsigned int horizontalScrollIndicatorShowing:1;
unsigned int delegateScrollViewDidScroll:1;
unsigned int delegateScrollViewWillBeginDragging:1;
unsigned int delegateScrollViewDidEndDragging:1;
+ unsigned int delegateScrollViewWillShowScrollIndicator:1;
+ unsigned int delegateScrollViewDidShowScrollIndicator:1;
+ unsigned int delegateScrollViewWillHideScrollIndicator:1;
+ unsigned int delegateScrollViewDidHideScrollIndicator:1;
} _scrollViewFlags;
}
@@ -118,9 +150,11 @@ typedef enum {
@property (nonatomic) TUIEdgeInsets contentInset;
@property (nonatomic, assign) id<TUIScrollViewDelegate> delegate;
@property (nonatomic, getter=isScrollEnabled) BOOL scrollEnabled;
-@property (nonatomic) BOOL showsHorizontalScrollIndicator;
-@property (nonatomic) BOOL showsVerticalScrollIndicator;
-@property (nonatomic) TUIScrollViewIndicatorStyle indicatorStyle;
+@property (nonatomic) TUIScrollViewIndicatorVisibility horizontalScrollIndicatorVisibility;
+@property (nonatomic) TUIScrollViewIndicatorVisibility verticalScrollIndicatorVisibility;
+@property (readonly, nonatomic) BOOL verticalScrollIndicatorShowing;
+@property (readonly, nonatomic) BOOL horizontalScrollIndicatorShowing;
+@property (nonatomic) TUIScrollViewIndicatorStyle scrollIndicatorStyle;
@property (nonatomic) float decelerationRate;
- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated;
@@ -132,6 +166,7 @@ typedef enum {
- (void)endContinuousScrollAnimated:(BOOL)animated;
@property (nonatomic, readonly) CGRect visibleRect;
+@property (nonatomic, readonly) TUIEdgeInsets scrollIndicatorInsets;
- (void)flashScrollIndicators;
@@ -152,4 +187,9 @@ typedef enum {
- (void)scrollViewWillBeginDragging:(TUIScrollView *)scrollView;
- (void)scrollViewDidEndDragging:(TUIScrollView *)scrollView;
+- (void)scrollView:(TUIScrollView *)scrollView willShowScrollIndicator:(TUIScrollViewIndicator)indicator;
+- (void)scrollView:(TUIScrollView *)scrollView didShowScrollIndicator:(TUIScrollViewIndicator)indicator;
+- (void)scrollView:(TUIScrollView *)scrollView willHideScrollIndicator:(TUIScrollViewIndicator)indicator;
+- (void)scrollView:(TUIScrollView *)scrollView didHideScrollIndicator:(TUIScrollViewIndicator)indicator;
+
@end
Oops, something went wrong.

0 comments on commit da9c067

Please sign in to comment.