Permalink
Browse files

Sub-tab view drawing correct, still needs mouseover drawing though

  • Loading branch information...
Panupan Sriautharawong
Panupan Sriautharawong committed Dec 17, 2011
1 parent 3f90aae commit 7499507bb5032bfba6f8c8397d63b9e4d00b6c0a
@@ -9,10 +9,6 @@
@implementation WILLTabCell
@synthesize highlightedSegment;
-- (void) awakeFromNib
-{
-
-}
// TODO: monitor this for clickable area and button image alignment.
- (void) drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
@@ -9,6 +9,7 @@
/* Begin PBXBuildFile section */
82803B77149A8E6800E01207 /* TestView.m in Sources */ = {isa = PBXBuildFile; fileRef = 82803B76149A8E6800E01207 /* TestView.m */; };
82D15D6F149C059F0089FF1E /* WILLSubTabView.m in Sources */ = {isa = PBXBuildFile; fileRef = 82D15D6E149C059F0089FF1E /* WILLSubTabView.m */; };
+ 82D15D80149C0D3C0089FF1E /* WILLSubTabCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 82D15D7F149C0D3C0089FF1E /* WILLSubTabCell.m */; };
97AD26CB1491758200CDC4DB /* WILLTabCellSelectedBG.pdf in Resources */ = {isa = PBXBuildFile; fileRef = 97AD26C81491758200CDC4DB /* WILLTabCellSelectedBG.pdf */; };
97AD26CC1491758200CDC4DB /* WILLTabCellSelectedBorder.pdf in Resources */ = {isa = PBXBuildFile; fileRef = 97AD26C91491758200CDC4DB /* WILLTabCellSelectedBorder.pdf */; };
97AD26CD1491758200CDC4DB /* WILLTabViewBG.pdf in Resources */ = {isa = PBXBuildFile; fileRef = 97AD26CA1491758200CDC4DB /* WILLTabViewBG.pdf */; };
@@ -33,6 +34,8 @@
82803B76149A8E6800E01207 /* TestView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TestView.m; sourceTree = "<group>"; };
82D15D6D149C059F0089FF1E /* WILLSubTabView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WILLSubTabView.h; sourceTree = "<group>"; };
82D15D6E149C059F0089FF1E /* WILLSubTabView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = WILLSubTabView.m; sourceTree = "<group>"; };
+ 82D15D7E149C0D3C0089FF1E /* WILLSubTabCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WILLSubTabCell.h; sourceTree = "<group>"; };
+ 82D15D7F149C0D3C0089FF1E /* WILLSubTabCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = WILLSubTabCell.m; sourceTree = "<group>"; };
97AD26C81491758200CDC4DB /* WILLTabCellSelectedBG.pdf */ = {isa = PBXFileReference; lastKnownFileType = image.pdf; path = WILLTabCellSelectedBG.pdf; sourceTree = "<group>"; };
97AD26C91491758200CDC4DB /* WILLTabCellSelectedBorder.pdf */ = {isa = PBXFileReference; lastKnownFileType = image.pdf; path = WILLTabCellSelectedBorder.pdf; sourceTree = "<group>"; };
97AD26CA1491758200CDC4DB /* WILLTabViewBG.pdf */ = {isa = PBXFileReference; lastKnownFileType = image.pdf; path = WILLTabViewBG.pdf; sourceTree = "<group>"; };
@@ -120,6 +123,8 @@
97FB5ABF14914DA600CBBA3A /* WILLTabCell.m */,
97E9B2F0148EA568001D1D01 /* WILLTabView.h */,
97E9B2F1148EA568001D1D01 /* WILLTabView.m */,
+ 82D15D7E149C0D3C0089FF1E /* WILLSubTabCell.h */,
+ 82D15D7F149C0D3C0089FF1E /* WILLSubTabCell.m */,
82D15D6D149C059F0089FF1E /* WILLSubTabView.h */,
82D15D6E149C059F0089FF1E /* WILLSubTabView.m */,
82803B75149A8E6800E01207 /* TestView.h */,
@@ -236,6 +241,7 @@
97FB5AC014914DA600CBBA3A /* WILLTabCell.m in Sources */,
82803B77149A8E6800E01207 /* TestView.m in Sources */,
82D15D6F149C059F0089FF1E /* WILLSubTabView.m in Sources */,
+ 82D15D80149C0D3C0089FF1E /* WILLSubTabCell.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -0,0 +1,16 @@
+//
+// WILLTabCell.h
+// WILLTabView
+//
+// Created by Aaron C on 12/8/11.
+// Copyright (c) 2011 __MyCompanyName__. All rights reserved.
+//
+
+#import <Cocoa/Cocoa.h>
+
+@interface WILLSubTabCell : NSSegmentedCell {
+ NSInteger highlightedSegment;
+}
+@property(assign) NSInteger highlightedSegment;
+
+@end
@@ -0,0 +1,121 @@
+#import "WILLSubTabCell.h"
+
+#define TAB_HIGHLIGHT "WILLTabCellSelectedBG"
+#define TAB_SELECTED "WILLTabCellSelectedBG"
+#define TAB_NORMAL "WILLTabViewBG"
+#define TAB_BORDER "WILLTabCellSelectedBorder"
+
+@implementation WILLSubTabCell
+
+@synthesize highlightedSegment;
+- (void) drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
+{
+ for (int i =0 ;i < [self segmentCount]; i++) {
+ [self drawSegment:i inFrame:cellFrame withView:controlView];
+ }
+}
+
+- (void) drawSegment:(NSInteger)seg inFrame:(NSRect)frame withView:(NSView *)controlView
+{
+ NSString *label = [self labelForSegment:seg];
+ NSSize textSize = [label sizeWithAttributes: [NSDictionary dictionary]];
+ float totalWidth = 0;
+ for (int i=0; i < seg; i++) {
+ totalWidth += [self widthForSegment:i];
+ }
+ frame.origin.x = totalWidth;
+ frame.origin.y = 0 ;
+ frame.size.width = textSize.width+20;
+ frame.size.height = controlView.frame.size.height;
+
+ [self setWidth:frame.size.width forSegment:seg];
+
+ NSBezierPath* drawingPath = [NSBezierPath bezierPath];
+ [drawingPath appendBezierPathWithRoundedRect:NSInsetRect(frame, 2, 2) xRadius:10 yRadius:10];
+ [[NSColor colorWithCalibratedRed:0 green:0 blue:0 alpha:.4] setFill];
+ if (seg == highlightedSegment || [self isSelectedForSegment:seg]) {
+ [drawingPath fill];
+ [drawingPath setLineWidth:1];
+ [[NSColor colorWithCalibratedRed:0 green:0 blue:0 alpha:.1] set];
+ [drawingPath stroke];
+ }
+
+ NSMutableParagraphStyle *paragraph;
+ paragraph = [[NSMutableParagraphStyle alloc] init];
+ [paragraph setLineBreakMode:NSLineBreakByTruncatingTail];
+ [paragraph setAlignment:NSCenterTextAlignment];
+
+ NSDictionary *labelAttr = [NSDictionary dictionaryWithObjectsAndKeys:
+ [NSFont systemFontOfSize:[NSFont smallSystemFontSize]], NSFontAttributeName,
+ [self isSelectedForSegment:seg] ? [NSColor whiteColor] : [NSColor blackColor], NSForegroundColorAttributeName,
+ //shadow, NSShadowAttributeName,
+ paragraph, NSParagraphStyleAttributeName, nil];
+ NSAttributedString *attrStr = [[NSAttributedString alloc] initWithString:label attributes:labelAttr];
+ [attrStr drawInRect:NSInsetRect(frame, 2, 3)];
+}
+
+- (id)initWithCoder:(NSCoder *)decoder;
+{
+ if (![super initWithCoder:decoder])
+ return nil;
+ [self setHighlightedSegment:-1];
+ return self;
+}
+
+- (void)_updateHighlightedSegment:(NSPoint)currentPoint
+ inView:(NSView *)controlView {
+ [self setHighlightedSegment:-1];
+ NSPoint loc = currentPoint;
+ NSRect frame = controlView.frame;
+ loc.x += frame.origin.x;
+ loc.y += frame.origin.y;
+ NSUInteger i = 0, count = [self segmentCount];
+ while(i < count && frame.origin.x < controlView.frame.size.width) {
+ frame.size.width = [self widthForSegment:i];
+ if(NSMouseInRect(loc, frame, NO))
+ {
+ [self setHighlightedSegment:i];
+ break;
+ }
+ frame.origin.x += frame.size.width;
+ i++;
+ }
+
+ [controlView setNeedsDisplay:YES];
+}
+
+- (BOOL)startTrackingAt:(NSPoint)startPoint
+ inView:(NSView *)controlView {
+ [self _updateHighlightedSegment:startPoint inView:controlView];
+ return [super startTrackingAt:startPoint inView:controlView];
+}
+
+- (BOOL)continueTracking:(NSPoint)lastPoint
+ at:(NSPoint)currentPoint
+ inView:(NSView *)controlView {
+ [self _updateHighlightedSegment:currentPoint inView:controlView];
+ return [super continueTracking:lastPoint at:currentPoint inView:controlView];
+}
+
+// TODO: fix this warning.
+- (void)stopTracking:(NSPoint)lastPoint
+ at:(NSPoint)stopPoint
+ inView:(NSView *)controlView
+ mouseIsUp:(BOOL)flag; {
+
+ [super stopTracking:lastPoint at:stopPoint inView:controlView mouseIsUp:flag];
+ if (highlightedSegment >= 0) {
+
+ [self setSelectedSegment:highlightedSegment];
+ if ([self.target respondsToSelector:self.action]) {
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
+ [self.target performSelector:self.action withObject:controlView];
+#pragma clang diagnostic pop
+ }
+ }
+
+ [self setHighlightedSegment:-1];
+}
+
+@end
@@ -1,4 +1,5 @@
#import "WILLSubTabView.h"
+#import "WILLSubTabCell.h"
@implementation WILLSubTabView
@@ -7,7 +8,7 @@ -(id)initWithCoder:(NSCoder *)aDecoder {
barImage = [NSImage imageNamed:@"WILLTabViewBG"];
// Setup segmented control
segmentedControl = [[NSSegmentedControl alloc] init];
- //[segmentedControl setCell:[[WILLTabCell alloc] init]];
+ [segmentedControl setCell:[[WILLSubTabCell alloc] init]];
[segmentedControl setSegmentCount:self.numberOfTabViewItems];
[segmentedControl setTarget:self];
[segmentedControl setAction:@selector(ctrlSelected:)];

0 comments on commit 7499507

Please sign in to comment.