1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
|
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "chrome/browser/cocoa/bookmark_tree_controller.h"
#include "app/l10n_util_mac.h"
#include "base/logging.h"
#import "chrome/browser/cocoa/bookmark_item.h"
#import "chrome/browser/cocoa/bookmark_manager_controller.h"
#include "grit/generated_resources.h"
// Outline-view column identifiers.
static NSString* const kTitleColIdent = @"title";
static NSString* const kURLColIdent = @"url";
static NSString* const kFolderColIdent = @"folder";
@implementation BookmarkTreeController
@synthesize showsLeaves = showsLeaves_;
// Initialization after the nib is loaded.
- (void)awakeFromNib {
[outline_ setTarget:self];
[outline_ setDoubleAction:@selector(openItems:)];
[self registerDragTypes];
}
- (BookmarkItem*)group {
return group_;
}
- (void)setGroup:(BookmarkItem*)group {
if (group != group_) {
group_ = group;
[outline_ deselectAll:self];
[outline_ reloadData];
[outline_ noteNumberOfRowsChanged];
}
}
- (NSOutlineView*)outline {
return outline_;
}
- (BOOL)showsFolderColumn {
NSTableColumn* col = [outline_ tableColumnWithIdentifier:kFolderColIdent];
return ![col isHidden];
}
- (void)setShowsFolderColumn:(BOOL)show {
NSTableColumn* col = [outline_ tableColumnWithIdentifier:kFolderColIdent];
DCHECK(col);
if (show != ![col isHidden]) {
[col setHidden:!show];
[outline_ sizeToFit];
}
}
- (BOOL)flat {
return flat_;
}
- (void)setFlat:(BOOL)flat {
flat_ = flat;
[outline_ setIndentationPerLevel:flat ? 0.0 : 16.0];
}
#pragma mark -
#pragma mark SELECTION:
// Getter for the |selectedItems| property.
- (NSArray*)selectedItems {
NSMutableArray* items = [NSMutableArray array];
NSIndexSet* selectedRows = [outline_ selectedRowIndexes];
if (selectedRows != nil) {
for (NSInteger row = [selectedRows firstIndex]; row != NSNotFound;
row = [selectedRows indexGreaterThanIndex:row]) {
[items addObject:[outline_ itemAtRow:row]];
}
}
return items;
}
// Setter for the |selectedItems| property.
- (void)setSelectedItems:(NSArray*)items {
NSMutableIndexSet* newSelection = [NSMutableIndexSet indexSet];
for (NSUInteger i = 0; i < [items count]; i++) {
NSInteger row = [outline_ rowForItem:[items objectAtIndex:i]];
if (row >= 0) {
[newSelection addIndex:row];
}
}
[outline_ selectRowIndexes:newSelection byExtendingSelection:NO];
}
- (BookmarkItem*)selectedItem {
BookmarkItem* selected = nil;
if ([outline_ numberOfSelectedRows] == 1)
selected = [outline_ itemAtRow:[outline_ selectedRow]];
return selected;
}
- (void)setSelectedItem:(BookmarkItem*)item {
[self setSelectedItems:item ? [NSArray arrayWithObject:item] : nil];
}
+ (NSArray*)keyPathsForValuesAffectingSelectedItem {
return [NSArray arrayWithObject:@"selectedItems"];
}
- (BOOL)selectionShouldChangeInOutlineView:(NSOutlineView*)outlineView {
[self willChangeValueForKey:@"selectedItems"];
return YES;
}
- (void)outlineViewSelectionDidChange:(NSNotification*)notification {
[self didChangeValueForKey:@"selectedItems"];
}
// Returns the selected/right-clicked item(s) for a command to act on.
- (NSArray*)actionItems {
int row = [outline_ clickedRow];
if (row >= 0 && ![outline_ isRowSelected:row])
return [NSArray arrayWithObject:[outline_ itemAtRow:row]];
return [self selectedItems];
}
- (BOOL)expandItem:(BookmarkItem*)item {
if (!item)
return NO;
if (item == group_)
return YES;
if ([outline_ rowForItem:item] < 0) {
// If the item's not visible, expand its ancestors.
if (![self expandItem:[item parent]])
return NO;
}
if (![outline_ isExpandable:item])
return NO;
[outline_ expandItem:item];
DCHECK([outline_ isItemExpanded:item]);
return YES;
}
- (BOOL)revealItem:(BookmarkItem*)item {
if (![self expandItem:[item parent]])
return NO;
[outline_ scrollRowToVisible:[outline_ rowForItem:item]];
[self setSelectedItems:[NSArray arrayWithObject:item]];
return YES;
}
#pragma mark -
#pragma mark COMMANDS:
// Responds to a double-click by opening the selected URL(s).
- (IBAction)openItems:(id)sender {
for (BookmarkItem* item in [self actionItems]) {
if (![item isFolder]) {
[item open];
} else if (flat_) {
[manager_ showGroup:item];
} else {
if ([outline_ isItemExpanded:item])
[outline_ collapseItem:item];
else
[outline_ expandItem:item];
}
}
}
// The Delete command (also bound to the delete key.)
- (IBAction)delete:(id)sender {
NSArray* items = [self actionItems];
// Iterate backwards so that any selected children are deleted before
// selected parents (opposite order could cause double-free!)
bool any = false;
if ([items count]) {
for (NSInteger i = [items count] - 1; i >= 0; i--) {
BookmarkItem* item = [items objectAtIndex:i];
if ([[item parent] removeChild:item])
any = true;
}
}
if (any) {
[outline_ reloadData];
[outline_ deselectAll:self];
} else {
NSBeep();
}
}
- (void)editTitleOfItem:(BookmarkItem*)item {
int row = [outline_ rowForItem:item];
DCHECK(row >= 0);
[self setSelectedItem:item];
[outline_ editColumn:[outline_ columnWithIdentifier:@"title"]
row:row
withEvent:[NSApp currentEvent]
select:YES];
}
- (IBAction)editTitle:(id)sender {
NSArray* items = [self actionItems];
if ([items count] == 1)
[self editTitleOfItem:[items objectAtIndex:0]];
else
NSBeep();
}
- (IBAction)newFolder:(id)sender {
BookmarkItem* parent;
int index;
NSArray* selItems = [self actionItems];
if ([selItems count] > 0) {
// Insert at selected/clicked row.
BookmarkItem* selected = [selItems objectAtIndex:0];
parent = [selected parent];
index = [parent indexOfChild:selected];
} else {
// ...or at very end if there's no selection:
parent = group_;
index = [group_ numberOfChildren];
}
if (!parent) {
NSBeep();
return;
}
// Create the folder, then select it and make the title editable:
BookmarkItem* folder = [parent addFolderWithTitle:@"" atIndex:index];
[outline_ expandItem:folder];
[self editTitleOfItem:folder];
}
- (IBAction)revealSelectedItem:(id)sender {
NSArray* selItems = [self actionItems];
if ([selItems count] != 1 ||
![manager_ revealItem:[selItems objectAtIndex:0]])
NSBeep();
[[outline_ window] makeFirstResponder:outline_];
}
static void addItem(NSMenu* menu, int command, SEL action) {
[menu addItemWithTitle:l10n_util::GetNSStringWithFixup(command)
action:action
keyEquivalent:@""];
}
// Generates a context menu for the outline view.
- (NSMenu*)menu {
NSMenu* menu = [[[NSMenu alloc] initWithTitle:@""] autorelease];
addItem(menu, IDS_BOOMARK_BAR_OPEN_IN_NEW_TAB, @selector(openItems:));
if (showsLeaves_) {
addItem(menu, IDS_BOOKMARK_MANAGER_SHOW_IN_FOLDER,
@selector(revealSelectedItem:));
}
[menu addItem:[NSMenuItem separatorItem]];
addItem(menu, IDS_BOOKMARK_BAR_EDIT, @selector(editTitle:));
addItem(menu, IDS_BOOKMARK_BAR_REMOVE, @selector(delete:));
[menu addItem:[NSMenuItem separatorItem]];
addItem(menu, IDS_BOOMARK_BAR_NEW_FOLDER, @selector(newFolder:));
[menu addItem:[NSMenuItem separatorItem]];
addItem(menu, IDS_BOOKMARK_MANAGER_SORT, @selector(sortByTitle:));
for (NSMenuItem* item in [menu itemArray])
[item setTarget:self];
return menu;
}
// Selectively enables/disables menu commands.
- (BOOL)validateUserInterfaceItem:(id<NSValidatedUserInterfaceItem>)item {
SEL action = [item action];
NSArray* selected = [self actionItems];
NSUInteger selCount = [selected count];
if (action == @selector(copy:)) {
return selCount > 0;
} else if (action == @selector(cut:) || action == @selector(delete:)) {
if (selCount == 0)
return NO;
for(BookmarkItem* item in selected) {
if ([item isFixed])
return NO;
}
return YES;
} else if (action == @selector(paste:)) {
return ![group_ isFake] &&
[[NSPasteboard generalPasteboard] availableTypeFromArray:
[outline_ registeredDraggedTypes]] != nil;
} else if (action == @selector(openItems:)) {
if (selCount == 0)
return NO;
// Disable if folder selected (if only because title says "Open In Tab".)
for (BookmarkItem* item in selected) {
if (![item URLString])
return NO;
}
return YES;
} else if (action == @selector(editTitle:)) {
return selCount == 1 && ![[selected lastObject] isFixed];
} else if (action == @selector(newFolder:)) {
// Disable New Folder in Recents/Search list.
return !(flat_ && [group_ isFake]);
} else if (action == @selector(revealSelectedItem:)) {
// Enable Show In Folder only in the flat list when
// showing Recents or Search, and a URL is selected.
return flat_ && [group_ isFake] &&
selCount == 1 && [[selected lastObject] URLString];
} else {
return YES;
}
}
#pragma mark -
#pragma mark DATA SOURCE:
// Returns the number of children of an item (NSOutlineView data source)
- (NSInteger) outlineView:(NSOutlineView*)outlineView
numberOfChildrenOfItem:(id)item {
item = (item ? item : group_);
return showsLeaves_ ? [item numberOfChildren] : [item numberOfChildFolders];
}
// Returns a child of an item (NSOutlineView data source)
- (id)outlineView:(NSOutlineView*)outlineView
child:(NSInteger)index
ofItem:(id)item {
item = (item ? item : group_);
if (showsLeaves_)
return [item childAtIndex:index];
else
return [item childFolderAtIndex:index];
}
// Returns whether an item is expandable (NSOutlineView data source)
- (BOOL)outlineView:(NSOutlineView*)outlineView isItemExpandable:(id)item {
if (flat_ || ![(item ? item : group_) isFolder])
return NO;
// In leafless mode, a folder with no subfolders isn't expandable.
if (!showsLeaves_ && [item numberOfChildFolders] == 0)
return NO;
return YES;
}
- (id) outlineView:(NSOutlineView*)outlineView
itemForPersistentObject:(id)persistentID {
return [group_ itemWithPersistentID:persistentID];
}
- (id) outlineView:(NSOutlineView*)outlineView
persistentObjectForItem:(id)item {
item = (item ? item : group_);
return [item persistentID];
}
// Returns the value to display in a cell (NSOutlineView data source)
- (id) outlineView:(NSOutlineView*)outlineView
objectValueForTableColumn:(NSTableColumn*)tableColumn
byItem:(id)item {
item = (item ? item : group_);
NSString* ident = [tableColumn identifier];
if ([ident isEqualToString:kTitleColIdent]) {
return [item title];
} else if ([ident isEqualToString:kURLColIdent]) {
return [item URLString];
} else if ([ident isEqualToString:kFolderColIdent]) {
return [item folderPath];
} else {
NOTREACHED();
return nil;
}
}
// Stores the edited value of a cell (NSOutlineView data source)
- (void)outlineView:(NSOutlineView*)outlineView
setObjectValue:(id)value
forTableColumn:(NSTableColumn*)tableColumn
byItem:(id)item
{
item = (item ? item : group_);
NSString* ident = [tableColumn identifier];
if ([ident isEqualToString:kTitleColIdent]) {
[item setTitle:value];
} else if ([ident isEqualToString:kURLColIdent]) {
if ([value length])
[item setURLString:value];
}
}
// Returns whether a cell is editable (NSOutlineView data source)
- (BOOL) outlineView:(NSOutlineView*)outlineView
shouldEditTableColumn:(NSTableColumn*)tableColumn
item:(id)item {
NSString* ident = [tableColumn identifier];
if ([item isFixed])
return NO;
if ([ident isEqualToString:kURLColIdent] && [item isFolder])
return NO;
return YES;
}
// Sets a cell's icon before it's drawn (NSOutlineView data source)
- (void)outlineView:(NSOutlineView*)outlineView
willDisplayCell:(id)cell
forTableColumn:(NSTableColumn*)tableColumn
item:(id)item
{
// Use the bookmark/folder's icon.
if ([[tableColumn identifier] isEqualToString:kTitleColIdent]) {
item = (item ? item : group_);
[cell setImage:[item icon]];
}
// Show special folders (Bookmarks Bar, Others, Recents, Search) in bold.
static NSFont* sBoldFont = [[NSFont boldSystemFontOfSize:
[NSFont smallSystemFontSize]] retain];
static NSFont* sPlainFont = [[NSFont systemFontOfSize:
[NSFont smallSystemFontSize]] retain];
[cell setFont:[item isFixed] ? sBoldFont : sPlainFont];
}
// Updates the tree after the data model has changed.
- (void)itemChanged:(id)nodeItem childrenChanged:(BOOL)childrenChanged {
if (nodeItem == group_)
nodeItem = nil;
[outline_ reloadItem:nodeItem reloadChildren:childrenChanged];
}
@end
@implementation BookmarksOutlineView
- (BookmarkTreeController*)bookmarkController {
return (BookmarkTreeController*)[self delegate];
}
- (NSRect)frameOfOutlineCellAtRow:(NSInteger)row {
// If the controller is in flat view, don't reserve space for the triangles.
BookmarkTreeController* controller = [self delegate];
if ([controller flat])
return NSZeroRect;
return [super frameOfOutlineCellAtRow:row];
}
- (IBAction)cut:(id)sender {
[[self bookmarkController] cut:sender];
}
- (IBAction)copy:(id)sender {
[[self bookmarkController] copy:sender];
}
- (IBAction)paste:(id)sender {
[[self bookmarkController] paste:sender];
}
- (IBAction)delete:(id)sender {
[[self bookmarkController] delete:sender];
}
- (BOOL)validateUserInterfaceItem:(id<NSValidatedUserInterfaceItem>)item {
return [[self bookmarkController] validateUserInterfaceItem:item];
}
- (void)keyDown:(NSEvent*)event {
NSString* chars = [event charactersIgnoringModifiers];
if ([chars length] == 1) {
switch ([chars characterAtIndex:0]) {
case NSDeleteCharacter:
case NSDeleteFunctionKey:
[self delete:self];
return;
case NSCarriageReturnCharacter:
case NSEnterCharacter:
[[self bookmarkController] editTitle:self];
return;
case NSTabCharacter:
// For some reason NSTableView responds to the tab key by editing
// the selected row. Override this with the normal behavior.
[[self window] selectNextKeyView:self];
return;
}
}
[super keyDown:event];
}
- (NSMenu*)menu {
return [[self bookmarkController] menu];
}
@end
|