SidebarDemoAppDelegate.m
/* |
File: SidebarDemoAppDelegate.m |
Abstract: |
Basic app delegate/controller for a View Based TableView that implements a standard source list sidebar. |
Version: 1.1 |
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple |
Inc. ("Apple") in consideration of your agreement to the following |
terms, and your use, installation, modification or redistribution of |
this Apple software constitutes acceptance of these terms. If you do |
not agree with these terms, please do not use, install, modify or |
redistribute this Apple software. |
In consideration of your agreement to abide by the following terms, and |
subject to these terms, Apple grants you a personal, non-exclusive |
license, under Apple's copyrights in this original Apple software (the |
"Apple Software"), to use, reproduce, modify and redistribute the Apple |
Software, with or without modifications, in source and/or binary forms; |
provided that if you redistribute the Apple Software in its entirety and |
without modifications, you must retain this notice and the following |
text and disclaimers in all such redistributions of the Apple Software. |
Neither the name, trademarks, service marks or logos of Apple Inc. may |
be used to endorse or promote products derived from the Apple Software |
without specific prior written permission from Apple. Except as |
expressly stated in this notice, no other rights or licenses, express or |
implied, are granted by Apple herein, including but not limited to any |
patent rights that may be infringed by your derivative works or by other |
works in which the Apple Software may be incorporated. |
The Apple Software is provided by Apple on an "AS IS" basis. APPLE |
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION |
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS |
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND |
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS. |
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL |
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, |
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED |
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), |
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE |
POSSIBILITY OF SUCH DAMAGE. |
Copyright (C) 2011 Apple Inc. All Rights Reserved. |
*/ |
#import "SidebarDemoAppDelegate.h" |
#import "SidebarTableCellView.h" |
@implementation SidebarDemoAppDelegate |
@synthesize window = _window; |
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification { |
// The array determines our order |
_topLevelItems = [[NSArray arrayWithObjects:@"Favorites", @"Content Views", @"Mailboxes", @"A Fourth Group", nil] retain]; |
// The data is stored ina dictionary. The objects are the nib names to load. |
_childrenDictionary = [NSMutableDictionary new]; |
[_childrenDictionary setObject:[NSArray arrayWithObjects:@"ContentView1", @"ContentView2", @"ContentView3", nil] forKey:@"Favorites"]; |
[_childrenDictionary setObject:[NSArray arrayWithObjects:@"ContentView1", @"ContentView2", @"ContentView3", nil] forKey:@"Content Views"]; |
[_childrenDictionary setObject:[NSArray arrayWithObjects:@"ContentView2", nil] forKey:@"Mailboxes"]; |
[_childrenDictionary setObject:[NSArray arrayWithObjects:@"ContentView1", @"ContentView1", @"ContentView1", @"ContentView1", @"ContentView2", nil] forKey:@"A Fourth Group"]; |
// The basic recipe for a sidebar. Note that the selectionHighlightStyle is set to NSTableViewSelectionHighlightStyleSourceList in the nib |
[_sidebarOutlineView sizeLastColumnToFit]; |
[_sidebarOutlineView reloadData]; |
[_sidebarOutlineView setFloatsGroupRows:NO]; |
// NSTableViewRowSizeStyleDefault should be used, unless the user has picked an explicit size. In that case, it should be stored out and re-used. |
[_sidebarOutlineView setRowSizeStyle:NSTableViewRowSizeStyleDefault]; |
// Expand all the root items; disable the expansion animation that normally happens |
[NSAnimationContext beginGrouping]; |
[[NSAnimationContext currentContext] setDuration:0]; |
[_sidebarOutlineView expandItem:nil expandChildren:YES]; |
[NSAnimationContext endGrouping]; |
} |
- (void)dealloc { |
[_currentContentViewController release]; |
[_topLevelItems release]; |
[_childrenDictionary release]; |
[super dealloc]; |
} |
- (void)_setContentViewToName:(NSString *)name { |
if (_currentContentViewController) { |
[[_currentContentViewController view] removeFromSuperview]; |
[_currentContentViewController release]; |
} |
_currentContentViewController = [[NSViewController alloc] initWithNibName:name bundle:nil]; // Retained |
NSView *view = [_currentContentViewController view]; |
view.frame = _mainContentView.bounds; |
[view setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; |
[_mainContentView addSubview:view]; |
} |
- (void)outlineViewSelectionDidChange:(NSNotification *)notification { |
if ([_sidebarOutlineView selectedRow] != -1) { |
NSString *item = [_sidebarOutlineView itemAtRow:[_sidebarOutlineView selectedRow]]; |
if ([_sidebarOutlineView parentForItem:item] != nil) { |
// Only change things for non-root items (root items can be selected, but are ignored) |
[self _setContentViewToName:item]; |
} |
} |
} |
- (NSArray *)_childrenForItem:(id)item { |
NSArray *children; |
if (item == nil) { |
children = _topLevelItems; |
} else { |
children = [_childrenDictionary objectForKey:item]; |
} |
return children; |
} |
- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item { |
return [[self _childrenForItem:item] objectAtIndex:index]; |
} |
- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item { |
if ([outlineView parentForItem:item] == nil) { |
return YES; |
} else { |
return NO; |
} |
} |
- (NSInteger) outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item { |
return [[self _childrenForItem:item] count]; |
} |
- (BOOL)outlineView:(NSOutlineView *)outlineView isGroupItem:(id)item { |
return [_topLevelItems containsObject:item]; |
} |
- (BOOL)outlineView:(NSOutlineView *)outlineView shouldShowOutlineCellForItem:(id)item { |
// As an example, hide the "outline disclosure button" for FAVORITES. This hides the "Show/Hide" button and disables the tracking area for that row. |
if ([item isEqualToString:@"Favorites"]) { |
return NO; |
} else { |
return YES; |
} |
} |
- (NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item { |
// For the groups, we just return a regular text view. |
if ([_topLevelItems containsObject:item]) { |
NSTextField *result = [outlineView makeViewWithIdentifier:@"HeaderTextField" owner:self]; |
// Uppercase the string value, but don't set anything else. NSOutlineView automatically applies attributes as necessary |
NSString *value = [item uppercaseString]; |
[result setStringValue:value]; |
return result; |
} else { |
// The cell is setup in IB. The textField and imageView outlets are properly setup. |
// Special attributes are automatically applied by NSTableView/NSOutlineView for the source list |
SidebarTableCellView *result = [outlineView makeViewWithIdentifier:@"MainCell" owner:self]; |
result.textField.stringValue = item; |
// Setup the icon based on our section |
id parent = [outlineView parentForItem:item]; |
NSInteger index = [_topLevelItems indexOfObject:parent]; |
NSInteger iconOffset = index % 4; |
switch (iconOffset) { |
case 0: { |
result.imageView.image = [NSImage imageNamed:NSImageNameIconViewTemplate]; |
break; |
} |
case 1: { |
result.imageView.image = [NSImage imageNamed:NSImageNameHomeTemplate]; |
break; |
} |
case 2: { |
result.imageView.image = [NSImage imageNamed:NSImageNameQuickLookTemplate]; |
break; |
} |
case 3: { |
result.imageView.image = [NSImage imageNamed:NSImageNameSlideshowTemplate]; |
break; |
} |
} |
BOOL hideUnreadIndicator = YES; |
// Setup the unread indicator to show in some cases. Layout is done in SidebarTableCellView's viewWillDraw |
if (index == 0) { |
// First row in the index |
hideUnreadIndicator = NO; |
[result.button setTitle:@"42"]; |
[result.button sizeToFit]; |
// Make it appear as a normal label and not a button |
[[result.button cell] setHighlightsBy:0]; |
} else if (index == 2) { |
// Example for a button |
hideUnreadIndicator = NO; |
result.button.target = self; |
result.button.action = @selector(buttonClicked:); |
[result.button setImage:[NSImage imageNamed:NSImageNameAddTemplate]]; |
// Make it appear as a button |
[[result.button cell] setHighlightsBy:NSPushInCellMask|NSChangeBackgroundCellMask]; |
} |
[result.button setHidden:hideUnreadIndicator]; |
return result; |
} |
} |
- (void)buttonClicked:(id)sender { |
// Example target action for the button |
NSInteger row = [_sidebarOutlineView rowForView:sender]; |
NSLog(@"row: %ld", row); |
} |
- (IBAction)sidebarMenuDidChange:(id)sender { |
// Allow the user to pick a sidebar style |
NSInteger rowSizeStyle = [sender tag]; |
[_sidebarOutlineView setRowSizeStyle:rowSizeStyle]; |
} |
- (void)menuNeedsUpdate:(NSMenu *)menu { |
for (NSInteger i = 0; i < [menu numberOfItems]; i++) { |
NSMenuItem *item = [menu itemAtIndex:i]; |
if (![item isSeparatorItem]) { |
// In IB, the tag was set to the appropriate rowSizeStyle. Read in that value. |
NSInteger state = ([item tag] == [_sidebarOutlineView rowSizeStyle]) ? 1 : 0; |
[item setState:state]; |
} |
} |
} |
- (BOOL)splitView:(NSSplitView *)splitView canCollapseSubview:(NSView *)subview { |
return NO; |
} |
- (CGFloat)splitView:(NSSplitView *)splitView constrainMinCoordinate:(CGFloat)proposedMinimumPosition ofSubviewAt:(NSInteger)dividerIndex { |
if (proposedMinimumPosition < 75) { |
proposedMinimumPosition = 75; |
} |
return proposedMinimumPosition; |
} |
@end |
Copyright © 2011 Apple Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2011-05-02