Revision: 3907
http://skim-app.svn.sourceforge.net/skim-app/?rev=3907&view=rev
Author: hofman
Date: 2008-05-20 08:06:43 -0700 (Tue, 20 May 2008)
Log Message:
-----------
Add a root item for the bookmarks, so we don't have to manage a separate array.
Also collapses some code, as we don't need to special case the root items.
Modified Paths:
--------------
trunk/SKApplicationController.m
trunk/SKBookmark.h
trunk/SKBookmark.m
trunk/SKBookmarkController.h
trunk/SKBookmarkController.m
trunk/SKSheetController.m
Modified: trunk/SKApplicationController.m
===================================================================
--- trunk/SKApplicationController.m 2008-05-20 09:49:31 UTC (rev 3906)
+++ trunk/SKApplicationController.m 2008-05-20 15:06:43 UTC (rev 3907)
@@ -79,6 +79,7 @@
#define FILE_MENU_INDEX 1
#define VIEW_MENU_INDEX 4
+#define BOOKMARKS_MENU_INDEX 8
NSString *SKDocumentSetupAliasKey = @"_BDAlias";
NSString *SKDocumentSetupFileNameKey = @"fileName";
@@ -131,6 +132,8 @@
[fileMenu removeItemAtIndex:idx];
}
+ [[[NSApp mainMenu] itemAtIndex:BOOKMARKS_MENU_INDEX]
setRepresentedObject:[[SKBookmarkController sharedBookmarkController]
bookmarkRoot]];
+
[[NSColorPanel sharedColorPanel] setShowsAlpha:YES];
}
@@ -310,32 +313,34 @@
NSMenu *supermenu = [menu supermenu];
NSMenuItem *item = [supermenu itemAtIndex:[supermenu
indexOfItemWithSubmenu:menu]];
SKBookmark *bm = [item representedObject];
- if ([bm isKindOfClass:[SKBookmark class]] == NO) bm = nil;
- NSArray *bookmarks = bm ? [bm children] : [[SKBookmarkController
sharedBookmarkController] bookmarks];
- int i = [menu numberOfItems], numFixed = bm ? 0 : 2, numBookmarks =
[bookmarks count];
- while (i-- > numFixed)
- [menu removeItemAtIndex:i];
- if (numFixed > 0 && numBookmarks > 0)
- [menu addItem:[NSMenuItem separatorItem]];
- for (i = 0; i < numBookmarks; i++) {
- bm = [bookmarks objectAtIndex:i];
- switch ([bm bookmarkType]) {
- case SKBookmarkTypeFolder:
- item = [menu addItemWithTitle:[bm label] action:NULL
keyEquivalent:@""];
- [item setRepresentedObject:bm];
- [item setImage:[bm icon]];
- [item setSubmenu:[[[NSMenu allocWithZone:[NSMenu menuZone]]
initWithTitle:[bm label]] autorelease]];
- [[item submenu] setDelegate:self];
- break;
- case SKBookmarkTypeSeparator:
- [menu addItem:[NSMenuItem separatorItem]];
- break;
- default:
- item = [menu addItemWithTitle:[bm label]
action:@selector(openBookmark:) keyEquivalent:@""];
- [item setTarget:self];
- [item setRepresentedObject:bm];
- [item setImage:[bm icon]];
- break;
+
+ if ([bm isKindOfClass:[SKBookmark class]]) {
+ NSArray *bookmarks = [bm children];
+ int i = [menu numberOfItems], numBookmarks = [bookmarks count];
+ while (i-- > 0 && ([[menu itemAtIndex:i] isSeparatorItem] || [[menu
itemAtIndex:i] representedObject]))
+ [menu removeItemAtIndex:i];
+ if ([menu numberOfItems] > 0 && numBookmarks > 0)
+ [menu addItem:[NSMenuItem separatorItem]];
+ for (i = 0; i < numBookmarks; i++) {
+ bm = [bookmarks objectAtIndex:i];
+ switch ([bm bookmarkType]) {
+ case SKBookmarkTypeFolder:
+ item = [menu addItemWithTitle:[bm label] action:NULL
keyEquivalent:@""];
+ [item setRepresentedObject:bm];
+ [item setImage:[bm icon]];
+ [item setSubmenu:[[[NSMenu allocWithZone:[NSMenu
menuZone]] initWithTitle:[bm label]] autorelease]];
+ [[item submenu] setDelegate:self];
+ break;
+ case SKBookmarkTypeSeparator:
+ [menu addItem:[NSMenuItem separatorItem]];
+ break;
+ default:
+ item = [menu addItemWithTitle:[bm label]
action:@selector(openBookmark:) keyEquivalent:@""];
+ [item setTarget:self];
+ [item setRepresentedObject:bm];
+ [item setImage:[bm icon]];
+ break;
+ }
}
}
}
Modified: trunk/SKBookmark.h
===================================================================
--- trunk/SKBookmark.h 2008-05-20 09:49:31 UTC (rev 3906)
+++ trunk/SKBookmark.h 2008-05-20 15:06:43 UTC (rev 3907)
@@ -51,6 +51,7 @@
@interface SKBookmark : NSObject <NSCopying> {
SKBookmark *parent;
+ NSUndoManager *undoManager;
}
- (id)initWithAlias:(BDAlias *)anAlias pageIndex:(unsigned)aPageIndex
label:(NSString *)aLabel;
@@ -84,6 +85,9 @@
- (SKBookmark *)parent;
- (void)setParent:(SKBookmark *)newParent;
+- (NSUndoManager *)undoManager;
+- (void)setUndoManager:(NSUndoManager *)newUndoManager;
+
- (BOOL)isDescendantOf:(SKBookmark *)bookmark;
- (BOOL)isDescendantOfArray:(NSArray *)bookmarks;
Modified: trunk/SKBookmark.m
===================================================================
--- trunk/SKBookmark.m 2008-05-20 09:49:31 UTC (rev 3906)
+++ trunk/SKBookmark.m 2008-05-20 15:06:43 UTC (rev 3907)
@@ -37,7 +37,6 @@
*/
#import "SKBookmark.h"
-#import "SKBookmarkController.h"
#import "BDAlias.h"
#import "NSImage_SKExtensions.h"
#import "SKUtilities.h"
@@ -146,6 +145,7 @@
}
- (void)dealloc {
+ [undoManager release];
if (self != defaultPlaceholderBookmark)
[super dealloc];
}
@@ -199,6 +199,17 @@
return NO;
}
+- (NSUndoManager *)undoManager {
+ return undoManager ? undoManager : [parent undoManager];
+}
+
+- (void)setUndoManager:(NSUndoManager *)newUndoManager {
+ if (undoManager != newUndoManager) {
+ [undoManager release];
+ undoManager = [newUndoManager retain];
+ }
+}
+
@end
#pragma mark -
@@ -225,7 +236,7 @@
}
- (void)dealloc {
- [[[SKBookmarkController sharedBookmarkController] undoManager]
removeAllActionsWithTarget:self];
+ [[self undoManager] removeAllActionsWithTarget:self];
[alias release];
[aliasData release];
[label release];
@@ -300,8 +311,7 @@
- (void)setLabel:(NSString *)newLabel {
if (label != newLabel) {
- NSUndoManager *undoManager = [[SKBookmarkController
sharedBookmarkController] undoManager];
- [(SKBookmark *)[undoManager prepareWithInvocationTarget:self]
setLabel:label];
+ [(SKBookmark *)[[self undoManager] prepareWithInvocationTarget:self]
setLabel:label];
[label release];
label = [newLabel retain];
[[NSNotificationCenter defaultCenter]
postNotificationName:SKBookmarkChangedNotification object:self];
@@ -328,7 +338,7 @@
}
- (void)dealloc {
- [[[SKBookmarkController sharedBookmarkController] undoManager]
removeAllActionsWithTarget:self];
+ [[self undoManager] removeAllActionsWithTarget:self];
[label release];
[children release];
[super dealloc];
@@ -356,8 +366,7 @@
- (void)setLabel:(NSString *)newLabel {
if (label != newLabel) {
- NSUndoManager *undoManager = [[SKBookmarkController
sharedBookmarkController] undoManager];
- [(SKBookmark *)[undoManager prepareWithInvocationTarget:self]
setLabel:label];
+ [(SKBookmark *)[[self undoManager] prepareWithInvocationTarget:self]
setLabel:label];
[label release];
label = [newLabel retain];
[[NSNotificationCenter defaultCenter]
postNotificationName:SKBookmarkChangedNotification object:self];
@@ -369,8 +378,7 @@
}
- (void)insertChild:(SKBookmark *)child atIndex:(unsigned int)anIndex {
- NSUndoManager *undoManager = [[SKBookmarkController
sharedBookmarkController] undoManager];
- [(SKBookmark *)[undoManager prepareWithInvocationTarget:self]
removeChild:child];
+ [(SKBookmark *)[[self undoManager] prepareWithInvocationTarget:self]
removeChild:child];
[children insertObject:child atIndex:anIndex];
[child setParent:self];
[[NSNotificationCenter defaultCenter]
postNotificationName:SKBookmarkChangedNotification object:self];
@@ -381,8 +389,7 @@
}
- (void)removeChild:(SKBookmark *)child {
- NSUndoManager *undoManager = [[SKBookmarkController
sharedBookmarkController] undoManager];
- [(SKBookmark *)[undoManager prepareWithInvocationTarget:self]
insertChild:child atIndex:[[self children] indexOfObject:child]];
+ [(SKBookmark *)[[self undoManager] prepareWithInvocationTarget:self]
insertChild:child atIndex:[[self children] indexOfObject:child]];
[[NSNotificationCenter defaultCenter]
postNotificationName:SKBookmarkWillBeRemovedNotification object:self];
[child setParent:nil];
[children removeObject:child];
Modified: trunk/SKBookmarkController.h
===================================================================
--- trunk/SKBookmarkController.h 2008-05-20 09:49:31 UTC (rev 3906)
+++ trunk/SKBookmarkController.h 2008-05-20 15:06:43 UTC (rev 3907)
@@ -44,7 +44,7 @@
@interface SKBookmarkController : NSWindowController {
IBOutlet SKBookmarkOutlineView *outlineView;
IBOutlet SKStatusBar *statusBar;
- NSMutableArray *bookmarks;
+ SKBookmark *bookmarkRoot;
NSMutableArray *recentDocuments;
NSUndoManager *undoManager;
NSArray *draggedBookmarks;
@@ -53,12 +53,7 @@
+ (id)sharedBookmarkController;
-- (NSArray *)bookmarks;
-- (void)setBookmarks:(NSArray *)newBookmarks;
-- (unsigned)countOfBookmarks;
-- (id)objectInBookmarksAtIndex:(unsigned)index;
-- (void)insertObject:(id)obj inBookmarksAtIndex:(unsigned)index;
-- (void)removeObjectFromBookmarksAtIndex:(unsigned)index;
+- (SKBookmark *)bookmarkRoot;
- (void)addBookmarkForPath:(NSString *)path pageIndex:(unsigned)pageIndex
label:(NSString *)label toFolder:(SKBookmark *)folder;
- (void)handleApplicationWillTerminateNotification:(NSNotification
*)notification;
Modified: trunk/SKBookmarkController.m
===================================================================
--- trunk/SKBookmarkController.m 2008-05-20 09:49:31 UTC (rev 3906)
+++ trunk/SKBookmarkController.m 2008-05-20 15:06:43 UTC (rev 3907)
@@ -94,9 +94,9 @@
- (id)init {
if (self = [super init]) {
- bookmarks = [[NSMutableArray alloc] init];
recentDocuments = [[NSMutableArray alloc] init];
+ NSMutableArray *bookmarks = [NSMutableArray array];
NSData *data = [NSData dataWithContentsOfFile:[self
bookmarksFilePath]];
if (data) {
NSString *error = nil;
@@ -121,8 +121,12 @@
[bookmark release];
}
}
+
}
+ bookmarkRoot = [[SKBookmark alloc] initFolderWithChildren:bookmarks
label:nil];
+ [bookmarkRoot setUndoManager:[self undoManager]];
+
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleApplicationWillTerminateNotification:)
name:NSApplicationWillTerminateNotification
@@ -141,7 +145,7 @@
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
- [bookmarks release];
+ [bookmarkRoot release];
[recentDocuments release];
[draggedBookmarks release];
[toolbarItems release];
@@ -186,59 +190,10 @@
#pragma mark Bookmarks
-- (NSArray *)bookmarks {
- return bookmarks;
+- (SKBookmark *)bookmarkRoot {
+ return bookmarkRoot;
}
-- (void)setBookmarks:(NSArray *)newBookmarks {
- [[[self undoManager] prepareWithInvocationTarget:self]
setBookmarks:[[bookmarks copy] autorelease]];
- [bookmarks setArray:newBookmarks];
-}
-
-- (unsigned)countOfBookmarks {
- return [bookmarks count];
-}
-
-- (id)objectInBookmarksAtIndex:(unsigned)anIndex {
- return [bookmarks objectAtIndex:anIndex];
-}
-
-- (void)insertObject:(id)obj inBookmarksAtIndex:(unsigned)anIndex {
- [[[self undoManager] prepareWithInvocationTarget:self]
removeObjectFromBookmarksAtIndex:anIndex];
- [bookmarks insertObject:obj atIndex:anIndex];
- [self handleBookmarkChangedNotification:nil];
-}
-
-- (void)removeObjectFromBookmarksAtIndex:(unsigned)anIndex {
- [[[self undoManager] prepareWithInvocationTarget:self]
insertObject:[bookmarks objectAtIndex:anIndex] inBookmarksAtIndex:anIndex];
- [self handleBookmarkWillBeRemovedNotification:nil];
- [bookmarks removeObjectAtIndex:anIndex];
- [self handleBookmarkChangedNotification:nil];
-}
-
-- (NSArray *)childrenOfBookmark:(SKBookmark *)bookmark {
- return bookmark ? [bookmark children] : bookmarks;
-}
-
-- (unsigned int)indexOfChildBookmark:(SKBookmark *)bookmark {
- return [[self childrenOfBookmark:[bookmark parent]]
indexOfObject:bookmark];
-}
-
-- (void)bookmark:(SKBookmark *)bookmark insertChildBookmark:(SKBookmark
*)child atIndex:(unsigned int)anIndex {
- if (bookmark)
- [bookmark insertChild:child atIndex:anIndex];
- else
- [self insertObject:child inBookmarksAtIndex:anIndex];
-}
-
-- (void)removeChildBookmark:(SKBookmark *)bookmark {
- SKBookmark *parent = [bookmark parent];
- if (parent)
- [parent removeChild:bookmark];
- else
- [[self mutableArrayValueForKey:@"bookmarks"] removeObject:bookmark];
-}
-
- (NSArray *)minimumCoverForBookmarks:(NSArray *)items {
NSEnumerator *bmEnum = [items objectEnumerator];
SKBookmark *bm;
@@ -257,7 +212,7 @@
- (void)addBookmarkForPath:(NSString *)path pageIndex:(unsigned)pageIndex
label:(NSString *)label toFolder:(SKBookmark *)folder {
SKBookmark *bookmark = [[SKBookmark alloc] initWithPath:path
pageIndex:pageIndex label:label];
if (bookmark) {
- [self bookmark:folder insertChildBookmark:bookmark atIndex:[[self
childrenOfBookmark:folder] count]];
+ [(folder ? folder : bookmarkRoot) addChild:bookmark];
[bookmark release];
}
}
@@ -326,6 +281,8 @@
return [setups count] ? setups : nil;
}
+#pragma mark Bookmarks support
+
- (NSString *)bookmarksFilePath {
static NSString *bookmarksPath = nil;
@@ -386,8 +343,8 @@
- (IBAction)insertBookmarkFolder:(id)sender {
SKBookmark *folder = [[[SKBookmark alloc]
initFolderWithLabel:NSLocalizedString(@"Folder", @"default folder name")]
autorelease];
int rowIndex = [[outlineView selectedRowIndexes] lastIndex];
- SKBookmark *item = nil;
- unsigned int idx = [bookmarks count];
+ SKBookmark *item = bookmarkRoot;
+ unsigned int idx = [[bookmarkRoot children] count];
if (rowIndex != NSNotFound) {
SKBookmark *selectedItem = [outlineView itemAtRow:rowIndex];
@@ -396,10 +353,10 @@
idx = [[item children] count];
} else {
item = [selectedItem parent];
- idx = [self indexOfChildBookmark:selectedItem] + 1;
+ idx = [[item children] indexOfObject:selectedItem] + 1;
}
}
- [self bookmark:item insertChildBookmark:folder atIndex:idx];
+ [item insertChild:folder atIndex:idx];
int row = [outlineView rowForItem:folder];
[outlineView selectRowIndexes:[NSIndexSet indexSetWithIndex:row]
byExtendingSelection:NO];
@@ -409,8 +366,8 @@
- (IBAction)insertBookmarkSeparator:(id)sender {
SKBookmark *separator = [[[SKBookmark alloc] initSeparator] autorelease];
int rowIndex = [[outlineView selectedRowIndexes] lastIndex];
- SKBookmark *item = nil;
- unsigned int idx = [bookmarks count];
+ SKBookmark *item = bookmarkRoot;
+ unsigned int idx = [[bookmarkRoot children] count];
if (rowIndex != NSNotFound) {
SKBookmark *selectedItem = [outlineView itemAtRow:rowIndex];
@@ -419,10 +376,10 @@
idx = [[item children] count];
} else {
item = [selectedItem parent];
- idx = [self indexOfChildBookmark:selectedItem] + 1;
+ idx = [[item children] indexOfObject:selectedItem] + 1;
}
}
- [self bookmark:item insertChildBookmark:separator atIndex:idx];
+ [item insertChild:separator atIndex:idx];
int row = [outlineView rowForItem:separator];
[outlineView selectRowIndexes:[NSIndexSet indexSetWithIndex:row]
byExtendingSelection:NO];
@@ -453,7 +410,7 @@
- (void)handleApplicationWillTerminateNotification:(NSNotification
*)notification {
[recentDocuments makeObjectsPerformSelector:@selector(removeObjectForKey:)
withObject:SKRecentDocumentAliasKey];
- NSDictionary *bookmarksDictionary = [NSDictionary
dictionaryWithObjectsAndKeys:[bookmarks valueForKey:@"properties"],
SKBookmarkControllerBookmarksKey, recentDocuments,
SKBookmarkControllerRecentDocumentsKey, nil];
+ NSDictionary *bookmarksDictionary = [NSDictionary
dictionaryWithObjectsAndKeys:[[bookmarkRoot children]
valueForKey:@"properties"], SKBookmarkControllerBookmarksKey, recentDocuments,
SKBookmarkControllerRecentDocumentsKey, nil];
NSString *error = nil;
NSPropertyListFormat format = NSPropertyListBinaryFormat_v1_0;
NSData *data = [NSPropertyListSerialization
dataFromPropertyList:bookmarksDictionary format:format errorDescription:&error];
@@ -478,7 +435,8 @@
#pragma mark NSOutlineView datasource methods
- (int)outlineView:(NSOutlineView *)ov numberOfChildrenOfItem:(id)item {
- return [[self childrenOfBookmark:item] count];
+ if (item == nil) item = bookmarkRoot;
+ return [[item children] count];
}
- (BOOL)outlineView:(NSOutlineView *)ov isItemExpandable:(id)item {
@@ -486,7 +444,8 @@
}
- (id)outlineView:(NSOutlineView *)ov child:(int)anIndex ofItem:(id)item {
- return [[self childrenOfBookmark:item] objectAtIndex:anIndex];
+ if (item == nil) item = bookmarkRoot;
+ return [[item children] objectAtIndex:anIndex];
}
- (id)outlineView:(NSOutlineView *)ov objectValueForTableColumn:(NSTableColumn
*)tableColumn byItem:(id)item {
@@ -531,12 +490,10 @@
if (anIndex == NSOutlineViewDropOnItemIndex) {
if ([item bookmarkType] == SKBookmarkTypeFolder && [outlineView
isItemExpanded:item]) {
[ov setDropItem:item dropChildIndex:0];
- } else if ([item parent]) {
- [ov setDropItem:[item parent] dropChildIndex:[[[item parent]
children] indexOfObject:item] + 1];
} else if (item) {
- [ov setDropItem:nil dropChildIndex:[bookmarks
indexOfObject:item] + 1];
+ [ov setDropItem:(SKBookmark *)[item parent] == bookmarkRoot ?
nil : [item parent] dropChildIndex:[[[item parent] children]
indexOfObject:item] + 1];
} else {
- [ov setDropItem:nil dropChildIndex:[bookmarks count]];
+ [ov setDropItem:nil dropChildIndex:[[bookmarkRoot children]
count]];
}
}
return [item isDescendantOfArray:[self draggedBookmarks]] ?
NSDragOperationNone : NSDragOperationMove;
@@ -551,17 +508,19 @@
if (type) {
NSEnumerator *bmEnum = [[self draggedBookmarks] objectEnumerator];
SKBookmark *bookmark;
-
+
+ if (item == nil) item = bookmarkRoot;
+
while (bookmark = [bmEnum nextObject]) {
- int bookmarkIndex = [self indexOfChildBookmark:bookmark];
+ int bookmarkIndex = [[[bookmark parent] children]
indexOfObject:bookmark];
if (item == [bookmark parent]) {
if (anIndex > bookmarkIndex)
anIndex--;
if (anIndex == bookmarkIndex)
continue;
}
- [self removeChildBookmark:bookmark];
- [self bookmark:item insertChildBookmark:bookmark
atIndex:anIndex++];
+ [[bookmark parent] removeChild:bookmark];
+ [(SKBookmark *)item insertChild:bookmark atIndex:anIndex++];
}
return YES;
}
@@ -608,7 +567,7 @@
NSEnumerator *itemEnum = [[self minimumCoverForBookmarks:items]
reverseObjectEnumerator];
SKBookmark *item;
while (item = [itemEnum nextObject])
- [self removeChildBookmark:item];
+ [[item parent] removeChild:item];
}
- (BOOL)outlineView:(NSOutlineView *)ov canDeleteItems:(NSArray *)items {
Modified: trunk/SKSheetController.m
===================================================================
--- trunk/SKSheetController.m 2008-05-20 09:49:31 UTC (rev 3906)
+++ trunk/SKSheetController.m 2008-05-20 15:06:43 UTC (rev 3907)
@@ -145,11 +145,13 @@
}
- (void)prepare {
- NSArray *bookmarks = [[SKBookmarkController sharedBookmarkController]
bookmarks];
+ SKBookmarkController *bookmarkController = [SKBookmarkController
sharedBookmarkController];
+ SKBookmark *root = [bookmarkController bookmarkRoot];
[folderPopUp removeAllItems];
NSMenuItem *item = [[folderPopUp menu]
addItemWithTitle:NSLocalizedString(@"Bookmarks Menu", @"Menu item title")
action:NULL keyEquivalent:@""];
[item setImage:[NSImage imageNamed:@"SmallMenu"]];
- [self addMenuItemsForBookmarks:bookmarks level:1 toMenu:[folderPopUp
menu]];
+ [item setRepresentedObject:root];
+ [self addMenuItemsForBookmarks:[root children] level:1 toMenu:[folderPopUp
menu]];
[folderPopUp selectItemAtIndex:0];
}
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Skim-app-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/skim-app-commit