Copyright (c) 2006, Keith Anderson Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of keeto.net or Keith Anderson nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * Modified to have a glass-style background; this requires the sourceListBackground image. #import "KNShelfSplitView.h" #import <AIUtilities/AIAttributedStringAdditions.h> #import <AIUtilities/AIParagraphStyleAdditions.h> #import <AIUtilities/AIImageAdditions.h> #import <AIUtilities/AIToolbarUtilities.h> #define DEFAULT_SHELF_WIDTH 200.0f #define CONTROL_HEIGHT 22.0f #define BUTTON_WIDTH 30.0f #define THUMB_LINE_SPACING 2.0f #define THUMB_LINE_COUNT 3 #define RESIZE_BAR_EFFECTIVE_WIDTH 0.0f #define CONTROL_PART_NONE 0 #define CONTROL_PART_ACTION_BUTTON 1 #define CONTROL_PART_CONTEXT_BUTTON 2 #define CONTROL_PART_RESIZE_THUMB 3 #define CONTROL_PART_RESIZE_BAR 4 #define TOOLBAR_TOGGLESHELF_IDENTIFIER @"Toggle Shelf" #define TOGGLESHELF @"Toggle Shelf" @implementation KNShelfSplitView -(IBAction)toggleShelf:(id)sender [self setShelfIsVisible: ![self isShelfVisible]]; [self setNeedsDisplay: YES]; - (id)initWithFrame:(NSRect)aFrame { return [self initWithFrame: aFrame shelfView: nil contentView: nil]; -(id)initWithFrame:(NSRect)aFrame shelfView:(NSView *)aShelfView contentView:(NSView *)aContentView{ self = [super initWithFrame: aFrame]; currentShelfWidth = DEFAULT_SHELF_WIDTH; //change this prevShelfWidthBeforeDoubleClick = currentShelfWidth; activeControlPart = CONTROL_PART_NONE; shelfBackgroundColor = nil; contextButtonImage = nil; background = [[NSImage imageNamed:@"sourceListBackground" forClass:[self class]] retain]; backgroundSize = [background size]; [self setShelfView: aShelfView]; [self setContentView: aContentView]; if( autosaveName ){ [autosaveName release]; } if( contextButtonImage ){ [contextButtonImage release]; } if( actionButtonImage ){ [actionButtonImage release]; } if( shelfBackgroundColor ){ [shelfBackgroundColor release]; } if( background ){ [background release]; } -(void)setDelegate:(id)aDelegate{ delegateHasValidateWidth = NO; delegateHasContextMenu = NO; if( [delegate respondsToSelector:@selector(shelfSplitView:validateWidth:)] ){ delegateHasValidateWidth = YES; if ([delegate respondsToSelector:@selector(contextMenuForShelfSplitView:)]) { delegateHasContextMenu = YES; -(void)setTarget:(id)aTarget{ -(void)setAction:(SEL)aSelector{ -(void)didEndContextMenuTracking:(NSNotification *)notification [self setNeedsDisplayInRect: controlRect]; [[NSNotificationCenter defaultCenter] removeObserver:self name:NSMenuDidEndTrackingNotification object:notification.object]; -(void)setShelfView:(NSView *)aView{ [shelfView removeFromSuperview]; [self addSubview: shelfView]; -(void)setContentView:(NSView *)aView{ [contentView removeFromSuperview]; [self addSubview: contentView]; -(void)setDrawShelfLine:(BOOL)inDraw - (void)setShelfOnRight:(BOOL)inRight -(void)setShelfWidthNoConstraints:(CGFloat)aWidth currentShelfWidth = aWidth; -(void)setShelfWidth:(CGFloat)aWidth{ CGFloat newWidth = aWidth; // The shelf can never be completely closed. We always have at least enough to show our resize thumb, otherwise // if the delegate responds to shelfSplitView:validateWidth:, we use that width as our minimum shelf size CGFloat minShelf = 100; /*NSWidth(resizeThumbRect) + (shouldDrawContextButton ? NSWidth(contextButtonRect) : 0) + (shouldDrawActionButton ? NSWidth(actionButtonRect) : 0);*/ if( delegateHasValidateWidth ){ CGFloat requestedWidth = [delegate shelfSplitView:self validateWidth: aWidth]; if( requestedWidth > minShelf ){ minShelf = requestedWidth; if( minShelf > newWidth ){ // The shelf can never be wider than half the entire view CGFloat maxShelf = [self frame].size.width / 2; if( newWidth > maxShelf ){ currentShelfWidth = newWidth; return currentShelfWidth; -(void)setAutosaveName:(NSString *)aName{ [autosaveName autorelease]; autosaveName = [aName retain]; -(NSString *)autosaveName{ CGFloat leftShelfX = shelfOnRight ? NSWidth(self.bounds) - currentShelfWidth : 0; controlRect = NSMakeRect(leftShelfX, 0, currentShelfWidth, CONTROL_HEIGHT); resizeThumbRect = NSMakeRect(leftShelfX, 0, THUMB_WIDTH, CONTROL_HEIGHT); resizeThumbRect = NSMakeRect(leftShelfX + currentShelfWidth - THUMB_WIDTH, 0, THUMB_WIDTH, CONTROL_HEIGHT); resizeBarRect = NSMakeRect(leftShelfX - RESIZE_BAR_EFFECTIVE_WIDTH/2.0f, 0, RESIZE_BAR_EFFECTIVE_WIDTH, self.frame.size.height); resizeBarRect = NSMakeRect(leftShelfX + currentShelfWidth - RESIZE_BAR_EFFECTIVE_WIDTH/2.0f, 0, RESIZE_BAR_EFFECTIVE_WIDTH, self.frame.size.height); if (target && action && controlRect.size.width) { shouldDrawActionButton = YES; actionButtonRect = NSMakeRect(leftShelfX + THUMB_WIDTH, 0, BUTTON_WIDTH, CONTROL_HEIGHT); actionButtonRect = NSMakeRect(leftShelfX, 0, BUTTON_WIDTH, CONTROL_HEIGHT); if (delegateHasContextMenu && [delegate contextMenuForShelfSplitView:self].numberOfItems) { shouldDrawContextButton = YES; contextButtonRect = NSMakeRect(leftShelfX + THUMB_WIDTH + 2, 0, BUTTON_WIDTH, CONTROL_HEIGHT); contextButtonRect = NSMakeRect(leftShelfX, 0, BUTTON_WIDTH, CONTROL_HEIGHT); [shelfView setFrame:NSMakeRect(leftShelfX, CONTROL_HEIGHT + 1, currentShelfWidth, self.bounds.size.height - (CONTROL_HEIGHT + 1))]; CGFloat leftContentX, width; leftContentX = shelfOnRight ? 0 : currentShelfWidth + (self.drawShelfLine ? 1 : 0); width = NSWidth(self.bounds) - currentShelfWidth - (self.drawShelfLine ? 1 : 0); width = NSWidth(self.bounds); NSRect newRect = NSMakeRect(leftContentX, 0, width, NSHeight(self.bounds)); if (!NSEqualRects(newRect, contentView.frame)){ [contentView setFrame:newRect]; [self setNeedsDisplay: YES]; [[self window] invalidateCursorRectsForView: self]; -(void)setShelfIsVisible:(BOOL)visible{ if( isShelfVisible && !visible ){ [shelfView removeFromSuperview]; } else if( !isShelfVisible && visible ){ [self addSubview: shelfView]; isShelfVisible = visible; -(void)setActionButtonImage:(NSImage *)anImage{ [actionButtonImage autorelease]; actionButtonImage = [anImage retain]; [self setNeedsDisplayInRect: controlRect]; -(NSImage *)actionButtonImage{ return actionButtonImage; -(void)setContextButtonImage:(NSImage *)anImage{ if( contextButtonImage ){ [contextButtonImage autorelease]; contextButtonImage = [anImage retain]; [self setNeedsDisplayInRect: controlRect]; -(NSImage *)contextButtonImage{ return contextButtonImage; -(void)setShelfBackgroundColor:(NSColor *)aColor{ if( shelfBackgroundColor ){ [shelfBackgroundColor autorelease]; shelfBackgroundColor = [aColor retain]; [self setNeedsDisplay: YES]; -(NSColor *)shelfBackgroundColor{ return shelfBackgroundColor; [super resetCursorRects]; [self addCursorRect: resizeThumbRect cursor: [NSCursor resizeLeftRightCursor]]; [self addCursorRect: resizeBarRect cursor: [NSCursor resizeLeftRightCursor]]; -(void)mouseDown:(NSEvent *)anEvent{ BOOL stillMouseDown = YES; // determine if we're in a control part we care about currentLocation = [self convertPoint: [anEvent locationInWindow] fromView: nil]; if( shouldDrawActionButton && NSPointInRect( currentLocation, actionButtonRect ) ){ activeControlPart = CONTROL_PART_ACTION_BUTTON; }else if( shouldDrawContextButton && NSPointInRect( currentLocation, contextButtonRect ) ){ activeControlPart = CONTROL_PART_CONTEXT_BUTTON; NSEvent * contextEvent = [NSEvent mouseEventWithType:[anEvent type] location:NSMakePoint( contextButtonRect.origin.x + (contextButtonRect.size.width / 2) , contextButtonRect.origin.y + (contextButtonRect.size.height / 2) ) modifierFlags:[anEvent modifierFlags] timestamp:[anEvent timestamp] windowNumber:[anEvent windowNumber] context:[anEvent context] eventNumber:[anEvent eventNumber] clickCount:[anEvent clickCount] pressure:[anEvent pressure] [self setNeedsDisplayInRect:controlRect]; NSMenu *contextMenu = [delegate contextMenuForShelfSplitView:self]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didEndContextMenuTracking:) name:NSMenuDidEndTrackingNotification [NSMenu popUpContextMenu:contextMenu withEvent: contextEvent forView: self]; [super mouseDown:contextEvent]; }else if( NSPointInRect( currentLocation, resizeThumbRect ) ){ activeControlPart = CONTROL_PART_RESIZE_THUMB; }else if( NSPointInRect( currentLocation, resizeBarRect ) ){ activeControlPart = CONTROL_PART_RESIZE_BAR; activeControlPart = CONTROL_PART_NONE; [self setNeedsDisplayInRect: controlRect]; if( activeControlPart != CONTROL_PART_NONE ){ if([anEvent clickCount] == 2){ if( (activeControlPart == CONTROL_PART_RESIZE_THUMB) || (activeControlPart == CONTROL_PART_RESIZE_BAR) ){ if([delegate respondsToSelector:@selector(splitViewDidHaveResizeDoubleClick:)]) { [delegate splitViewDidHaveResizeDoubleClick:self]; if (currentShelfWidth > NSWidth(resizeBarRect)) { prevShelfWidthBeforeDoubleClick = currentShelfWidth; [self setShelfWidthNoConstraints:NSWidth(resizeBarRect)]; [self setShelfWidth:prevShelfWidthBeforeDoubleClick]; NSPoint startLocation = currentLocation = [self convertPoint: [anEvent locationInWindow] fromView: nil]; CGFloat initialDifference; initialDifference = startLocation.x - NSMinX(controlRect); initialDifference = NSMaxX(controlRect) - startLocation.x; anEvent = [[self window] nextEventMatchingMask: NSLeftMouseUpMask | NSLeftMouseDraggedMask]; currentLocation = [self convertPoint: [anEvent locationInWindow] fromView: nil]; if( (activeControlPart == CONTROL_PART_ACTION_BUTTON) && NSPointInRect( currentLocation, actionButtonRect ) ){ }else if( (activeControlPart == CONTROL_PART_CONTEXT_BUTTON) && NSPointInRect( currentLocation, contextButtonRect ) ){ switch( [anEvent type] ){ if( (activeControlPart == CONTROL_PART_RESIZE_THUMB) || (activeControlPart == CONTROL_PART_RESIZE_BAR) ){ width = self.bounds.size.width - currentLocation.x + initialDifference; width = currentLocation.x + initialDifference; [self setShelfWidth:width]; [self setNeedsDisplayInRect: controlRect]; [self setNeedsDisplayInRect: controlRect]; if( (activeControlPart == CONTROL_PART_ACTION_BUTTON) && NSPointInRect( currentLocation, actionButtonRect ) ){ if( target && action && [target respondsToSelector:action]){ [target performSelector: action withObject: self]; [super mouseDown:anEvent]; - (void)drawRect:(NSRect)rect { CGFloat leftShelfX = shelfOnRight ? NSWidth(self.bounds) - currentShelfWidth : 0; [self drawControlBackgroundInRect:NSMakeRect(leftShelfX, 0, controlRect.size.width, controlRect.size.height) if(shouldDrawActionButton) { [self drawControlBackgroundInRect:actionButtonRect active:(activeControlPart == CONTROL_PART_ACTION_BUTTON) && shouldHilite]; [[NSColor windowFrameColor] set]; NSRect targetRect = NSMakeRect(actionButtonRect.origin.x, actionButtonRect.origin.y, [actionButtonImage size].width, [actionButtonImage size].height); if( targetRect.size.width > actionButtonRect.size.width ){ targetRect.size.width = actionButtonRect.size.width; if( targetRect.size.width < actionButtonRect.size.width ){ targetRect.origin.x += (actionButtonRect.size.width - targetRect.size.width) / 2.0f; if( targetRect.size.height > actionButtonRect.size.height ){ targetRect.size.height = actionButtonRect.size.height; if( targetRect.size.height < actionButtonRect.size.height ){ targetRect.origin.y += (actionButtonRect.size.height - targetRect.size.height) / 2.0f; [actionButtonImage compositeToPoint:NSMakePoint(actionButtonRect.origin.x, actionButtonRect.origin.y) operation:NSCompositeDestinationAtop]; if (shouldDrawContextButton) { [self drawControlBackgroundInRect:contextButtonRect active:(activeControlPart == CONTROL_PART_CONTEXT_BUTTON ) && shouldHilite]; [[NSColor windowFrameColor] set]; NSRectFill( NSMakeRect( (contextButtonRect.origin.x + contextButtonRect.size.width) - 1, 0, 1, controlRect.size.height ) ); NSRect targetRect = NSMakeRect(contextButtonRect.origin.x, contextButtonRect.origin.y, [contextButtonImage size].width, [contextButtonImage size].height); if( targetRect.size.width > contextButtonRect.size.width ){ targetRect.size.width = contextButtonRect.size.width; if( targetRect.size.width < contextButtonRect.size.width ){ targetRect.origin.x += (contextButtonRect.size.width - targetRect.size.width) / 2.0f; if( targetRect.size.height > contextButtonRect.size.height ){ targetRect.size.height = contextButtonRect.size.height; if( targetRect.size.height < contextButtonRect.size.height ){ targetRect.origin.y += (contextButtonRect.size.height - targetRect.size.height) / 2.0f; [contextButtonImage drawInRect:targetRect fromRect:NSMakeRect( 0, 0, [contextButtonImage size].width, [contextButtonImage size].height ) operation:NSCompositeSourceOver [[NSColor windowFrameColor] set]; NSRectFill( NSMakeRect( leftShelfX, CONTROL_HEIGHT, currentShelfWidth, 1 ) ); NSRectFill( NSMakeRect( leftShelfX, 0, currentShelfWidth, 1 ) ); if (self.drawShelfLine) { [[NSColor windowFrameColor] set]; NSRectFill( NSMakeRect( leftShelfX - 1, 0, 1, [self frame].size.height ) ); NSRectFill( NSMakeRect( leftShelfX + currentShelfWidth, 0, 1, [self frame].size.height ) ); [[NSColor disabledControlTextColor] set]; NSRect thumbLineRect = NSMakeRect( resizeThumbRect.origin.x + THUMB_LINE_SPACING*2, resizeThumbRect.size.height / 4.0f, resizeThumbRect.size.height / 2.0f for( i=0; i<THUMB_LINE_COUNT; i++ ){ NSRectFill( thumbLineRect ); thumbLineRect.origin.x += (1+THUMB_LINE_SPACING); if( shelfBackgroundColor ){ [shelfBackgroundColor set]; NSRectFill( NSMakeRect( leftShelfX, CONTROL_HEIGHT+1, currentShelfWidth, [self frame].size.height ) ); [[NSColor windowFrameColor] set]; NSRectFill( NSMakeRect( resizeThumbRect.origin.x + resizeThumbRect.size.width + 1, 0, 1, resizeThumbRect.size.height ) ); if (attributedStringValue) { CGFloat leftShiftX = leftShelfX; if (shouldDrawActionButton) { leftShiftX += NSWidth(actionButtonRect) + 2; if (shouldDrawContextButton) { leftShiftX += NSWidth(contextButtonRect) + 2; textPoint = NSMakePoint(leftShiftX + resizeThumbRect.size.width + 4, (NSHeight(controlRect) - stringHeight)/2); textPoint = NSMakePoint(leftShiftX + 4, (NSHeight(controlRect) - stringHeight)/2 + 1); [attributedStringValue drawAtPoint:textPoint]; -(void)drawControlBackgroundInRect:(NSRect)aRect active:(BOOL)isActive{ //Draw the background, tiling across NSRect sourceRect = NSMakeRect(0, 0, backgroundSize.width, backgroundSize.height); NSRect destRect = NSMakeRect(aRect.origin.x, aRect.origin.y, sourceRect.size.width, aRect.size.height); while ((destRect.origin.x < NSMaxX(aRect)) && destRect.size.width > 0) { if (NSMaxX(destRect) > NSMaxX(aRect)) { destRect.size.width = NSMaxX(aRect) - NSMinX(destRect); sourceRect.size.width = NSWidth(destRect); [background drawInRect:destRect operation:(isActive ? NSCompositeSourceIn : NSCompositeSourceOver) fraction:(isActive ? 0.75f : 1.0f)]; destRect.origin.x += destRect.size.width; -(void)setFrame:(NSRect)aRect{ #pragma mark Status string - (void)setResizeThumbStringValue:(NSString *)inString if (!(attributedStringValue && [inString isEqualToString:[attributedStringValue string]])) { if(attributedStringValue) [attributedStringValue release]; NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys: [NSParagraphStyle styleWithAlignment:NSLeftTextAlignment lineBreakMode:NSLineBreakByTruncatingTail], NSParagraphStyleAttributeName, [NSFont systemFontOfSize:[NSFont smallSystemFontSize]], NSFontAttributeName, stringHeight = [NSAttributedString stringHeightForAttributes:attributes]; attributedStringValue = [[NSAttributedString alloc] initWithString:inString attributedStringValue = nil; [self setNeedsDisplay:YES];