Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

NSButton subclass that can properly highlight an icon on a colored ba…

…ckground without drawing a grey box around it. Intended to be used from inside a NIB (does its work in awakeFromNib).
  • Loading branch information...
commit 0b8b7b86803aa3e648f2667e296e581ab2360c46 1 parent a0b8ef1
@uliwitness authored
Showing with 63 additions and 0 deletions.
  1. +19 −0 ULIHighlightingButton.h
  2. +44 −0 ULIHighlightingButton.m
View
19 ULIHighlightingButton.h
@@ -0,0 +1,19 @@
+//
+// ULIHighlightingButton.h
+// Stacksmith
+//
+// Created by Uli Kusterer on 15.07.11.
+// Copyright 2011 Uli Kusterer. All rights reserved.
+//
+
+/*
+ An NSButton subclass that automatically sets up a highlighted alternate
+ image so you can have a highlighting pushbutton that doesn't have a grey
+ background.
+*/
+
+#import <AppKit/AppKit.h>
+
+@interface ULIHighlightingButton : NSButton
+
+@end
View
44 ULIHighlightingButton.m
@@ -0,0 +1,44 @@
+//
+// ULIHighlightingButton.m
+// Stacksmith
+//
+// Created by Uli Kusterer on 15.07.11.
+// Copyright 2011 Uli Kusterer. All rights reserved.
+//
+
+#import "ULIHighlightingButton.h"
+
+@implementation ULIHighlightingButton
+
+-(void) awakeFromNib
+{
+ NSImage * img = [self image];
+
+ NSRect iBox = NSZeroRect;
+ iBox.size = [img size];
+ NSImage* hImg = [[[NSImage alloc] initWithSize: iBox.size] autorelease];
+ [hImg lockFocus];
+ CGContextRef theCtx = [[NSGraphicsContext currentContext] graphicsPort];
+ CGContextSaveGState( theCtx );
+ [img drawAtPoint: NSZeroPoint fromRect: NSZeroRect operation: NSCompositeCopy fraction: 1.0];
+
+ // Make sure we only touch opaque pixels:
+ CGContextClipToMask( theCtx, NSRectToCGRect(iBox), [img CGImageForProposedRect: nil context: [NSGraphicsContext currentContext] hints: nil] );
+
+ // Now draw a rectangle over the icon that flips all the pixels:
+ #if INVERT
+ CGContextSetBlendMode( theCtx, kCGBlendModeDifference );
+ CGContextSetRGBFillColor( theCtx, 1, 1, 1, 1.0 );
+ CGContextFillRect( theCtx, NSRectToCGRect( iBox ) );
+ CGContextSetBlendMode( theCtx, kCGBlendModeNormal );
+ #else
+ [[NSColor colorWithCalibratedWhite: 0.0 alpha: 0.4] set];
+ NSRectFillUsingOperation( iBox, NSCompositeSourceAtop );
+ #endif
+ CGContextRestoreGState( theCtx );
+ [hImg unlockFocus];
+
+ [self setAlternateImage: hImg];
+}
+
+@end
Please sign in to comment.
Something went wrong with that request. Please try again.