Commits

Anonymous committed 58f10ba

Selectable toolbar - added ability to disable toolbar items

  • Participants
  • Parent commits 5fdd29a

Comments (0)

Files changed (2)

File BWSelectableToolbar.h

 {
 	BWSelectableToolbarHelper *helper;
 	NSMutableArray *itemIdentifiers;
-	NSMutableDictionary *itemsByIdentifier;
+	NSMutableDictionary *itemsByIdentifier, *enabledByIdentifier;
 	BOOL inIB;
 	
 	// For the IB inspector
 - (void)setSelectedItemIdentifier:(NSString *)itemIdentifier; // Use if you want an action in the tabbed window to change the tab.
 - (void)setSelectedItemIdentifierWithoutAnimation:(NSString *)itemIdentifier; // Use if you want to show the window with a certain item selected.
 
+// Programmatically disable or enable a toolbar item. 
+- (void)setEnabled:(BOOL)flag forIdentifier:(NSString *)itemIdentifier;
+
 @end

File BWSelectableToolbar.m

 @interface BWSelectableToolbar ()
 @property (retain) BWSelectableToolbarHelper *helper;
 @property (copy) NSMutableArray *labels;
+@property (copy) NSMutableDictionary *enabledByIdentifier;
 @property BOOL isPreferencesToolbar;
 @end
 
 @synthesize helper;
 @synthesize labels;
 @synthesize isPreferencesToolbar;
+@synthesize enabledByIdentifier;
 
 - (BWSelectableToolbar *)documentToolbar
 {
 		[self setDocumentToolbar:[decoder decodeObjectForKey:@"BWSTDocumentToolbar"]];
 		[self setHelper:[decoder decodeObjectForKey:@"BWSTHelper"]];
 		isPreferencesToolbar = [decoder decodeBoolForKey:@"BWSTIsPreferencesToolbar"];
+		[self setEnabledByIdentifier:[decoder decodeObjectForKey:@"BWSTEnabledByIdentifier"]];
 		
 //		NSLog(@"init with coder. helper decoded: %@", helper);
 	}
 	[coder encodeObject:[self documentToolbar] forKey:@"BWSTDocumentToolbar"];
 	[coder encodeObject:[self helper] forKey:@"BWSTHelper"];
 	[coder encodeBool:isPreferencesToolbar forKey:@"BWSTIsPreferencesToolbar"];
+	[coder encodeObject:[self enabledByIdentifier] forKey:@"BWSTEnabledByIdentifier"];
 	
 //	NSLog(@"encode with coder. helper encoded: %@",helper);
 }
 	if (self = [super initWithIdentifier:identifier])
 	{		
 		itemIdentifiers = [[NSMutableArray alloc] init];
-        itemsByIdentifier = [[NSMutableDictionary alloc] init];  
+        itemsByIdentifier = [[NSMutableDictionary alloc] init];
 		labels = [NSMutableArray array];
 		
 		selectedIndex = 0;
 	}
 }
 
-- (void)setSelectedItemIdentifier:(NSString *)itemIdentifier
-{
-	BOOL validIdentifier = NO;
-	
-	for (NSString *identifier in itemIdentifiers)
-	{
-		if ([identifier isEqualToString:itemIdentifier])
-			validIdentifier = YES;
-	}
-	
-	if (validIdentifier)
-		[self switchToItemAtIndex:[itemIdentifiers indexOfObject:itemIdentifier] animate:YES];
-}
-
-- (void)setSelectedItemIdentifierWithoutAnimation:(NSString *)itemIdentifier
-{
-	BOOL validIdentifier = NO;
-	
-	for (NSString *identifier in itemIdentifiers)
-	{
-		if ([identifier isEqualToString:itemIdentifier])
-			validIdentifier = YES;
-	}
-	
-	if (validIdentifier)
-		[self switchToItemAtIndex:[itemIdentifiers indexOfObject:itemIdentifier] animate:NO];
-}
-
 - (void)selectFirstItem
 {
 	int toolbarIndex = [self toolbarIndexFromSelectableIndex:0];
 												  object:[[self editableToolbar] _window]];
 	[itemIdentifiers release];
 	[itemsByIdentifier release];
+	[enabledByIdentifier release];
 	[labels release];
     [super dealloc];
 }
 
+#pragma mark Public Methods
+
+- (void)setSelectedItemIdentifier:(NSString *)itemIdentifier
+{
+	BOOL validIdentifier = NO;
+	
+	for (NSString *identifier in itemIdentifiers)
+	{
+		if ([identifier isEqualToString:itemIdentifier])
+			validIdentifier = YES;
+	}
+	
+	if (validIdentifier)
+		[self switchToItemAtIndex:[itemIdentifiers indexOfObject:itemIdentifier] animate:YES];
+}
+
+- (void)setSelectedItemIdentifierWithoutAnimation:(NSString *)itemIdentifier
+{
+	BOOL validIdentifier = NO;
+	
+	for (NSString *identifier in itemIdentifiers)
+	{
+		if ([identifier isEqualToString:itemIdentifier])
+			validIdentifier = YES;
+	}
+	
+	if (validIdentifier)
+		[self switchToItemAtIndex:[itemIdentifiers indexOfObject:itemIdentifier] animate:NO];
+}
+
+- (void)setEnabled:(BOOL)flag forIdentifier:(NSString *)itemIdentifier
+{
+	NSMutableDictionary *enabledDict = [[self enabledByIdentifier] mutableCopy];
+	
+	[enabledDict setObject:[NSNumber numberWithBool:flag] forKey:itemIdentifier];
+	
+	[self setEnabledByIdentifier:enabledDict];
+}
+
+#pragma mark Public Method Support Methods
+
+- (BOOL)validateToolbarItem:(NSToolbarItem *)theItem
+{
+	if ([self respondsToSelector:@selector(ibDidAddToDesignableDocument:)] == NO)
+	{
+		NSString *identifier = [theItem itemIdentifier];		
+		
+		if ([[self enabledByIdentifier] objectForKey:identifier] != nil)
+		{
+			if ([[[self enabledByIdentifier] objectForKey:identifier] boolValue] == NO)
+				return NO;
+		}
+	}
+	
+	return YES;
+}
+
+- (NSMutableDictionary *)enabledByIdentifier
+{
+	if (enabledByIdentifier == nil)
+		enabledByIdentifier = [NSMutableDictionary new];
+	
+    return [[enabledByIdentifier retain] autorelease]; 
+}
+
 #pragma mark NSWindow notifications
 
 - (void)windowDidResize:(NSNotification *)notification