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
|
// 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.
#include "app/l10n_util_mac.h"
#include "base/mac_util.h"
#include "base/sys_string_conversions.h"
#include "chrome/browser/bookmarks/bookmark_model.h"
#import "chrome/browser/cocoa/bookmark_bubble_controller.h"
#import "chrome/browser/cocoa/bookmark_bubble_window.h"
#include "grit/generated_resources.h"
@interface BookmarkBubbleController(PrivateAPI)
- (void)closeWindow;
@end
@implementation BookmarkBubbleController
@synthesize delegate = delegate_;
@synthesize folderComboBox = folderComboBox_;
- (id)initWithDelegate:(id<BookmarkBubbleControllerDelegate>)delegate
parentWindow:(NSWindow*)parentWindow
topLeftForBubble:(NSPoint)topLeftForBubble
model:(BookmarkModel*)model
node:(const BookmarkNode*)node
alreadyBookmarked:(BOOL)alreadyBookmarked {
if ((self = [super initWithNibName:@"BookmarkBubble"
bundle:mac_util::MainAppBundle()])) {
// all these are weak...
delegate_ = delegate;
parentWindow_ = parentWindow;
topLeftForBubble_ = topLeftForBubble;
model_ = model;
node_ = node;
alreadyBookmarked_ = alreadyBookmarked;
// But this is strong.
titleMapping_.reset([[NSMutableDictionary alloc] init]);
}
return self;
}
- (void)dealloc {
[self closeWindow];
[super dealloc];
}
- (void)showWindow {
[self view]; // force nib load and window_ allocation
[window_ makeKeyAndOrderFront:self];
}
// Actually close the window. Do nothing else.
- (void)closeWindow {
[parentWindow_ removeChildWindow:window_];
[window_ close];
}
- (void)awakeFromNib {
window_.reset([self createBubbleWindow]);
[parentWindow_ addChildWindow:window_ ordered:NSWindowAbove];
// Fill in inital values for text, controls, ...
// Default is IDS_BOOMARK_BUBBLE_PAGE_BOOKMARK; "Bookmark".
// If adding for the 1st time the string becomes "Bookmark Added!"
if (!alreadyBookmarked_) {
NSString* title =
l10n_util::GetNSString(IDS_BOOMARK_BUBBLE_PAGE_BOOKMARKED);
[bigTitle_ setStringValue:title];
}
[self fillInFolderList];
}
- (IBAction)edit:(id)sender {
[self updateBookmarkNode];
[self closeWindow];
[delegate_ editBookmarkNode:node_];
[delegate_ doneWithBubbleController:self];
}
- (IBAction)close:(id)sender {
if (node_) {
// no node_ if the bookmark was just removed
[self updateBookmarkNode];
}
[self closeWindow];
[delegate_ doneWithBubbleController:self];
}
// By implementing this, ESC causes the window to go away.
- (IBAction)cancel:(id)sender {
[self close:sender];
}
- (IBAction)remove:(id)sender {
model_->SetURLStarred(node_->GetURL(), node_->GetTitle(), false);
node_ = NULL; // no longer valid
[self close:self];
}
// We are the delegate of the combo box so we can tell when "choose
// another folder" was picked.
- (void)comboBoxSelectionDidChange:(NSNotification*)notification {
NSString* selected = [folderComboBox_ objectValueOfSelectedItem];
if ([selected isEqual:chooseAnotherFolder_.get()]) {
[self edit:self];
}
}
// We are the delegate of our own window so we know when we lose key.
// When we lose key status we close, mirroring Windows behaivor.
- (void)windowDidResignKey:(NSNotification*)notification {
// If we get here, we are done with this window and controller. The
// call of close: may destroy us which destroys the window. But the
// window is in the middle of processing resignKeyWindow. We
// retain/autorelease the window to insure it lasts until the end of
// this event.
[[window_ retain] autorelease];
if ([window_ isVisible])
[self close:self];
}
@end // BookmarkBubbleController
@implementation BookmarkBubbleController(ExposedForUnitTesting)
// Create and return a retained NSWindow for this bubble.
- (NSWindow*)createBubbleWindow {
NSRect contentRect = [[self view] frame];
NSPoint origin = topLeftForBubble_;
origin.y -= contentRect.size.height; // since it'll be our bottom-left
contentRect.origin = origin;
// Now convert to global coordinates since it'll be used for a window.
contentRect.origin = [parentWindow_ convertBaseToScreen:contentRect.origin];
NSWindow* window = [[BookmarkBubbleWindow alloc]
initWithContentRect:contentRect];
[window setDelegate:self];
[window setContentView:[self view]];
return window;
}
// Fill in all information related to the folder combo box.
//
// TODO(jrg): make sure nested folders that have the same name are
// handled properly.
// http://crbug.com/19408
- (void)fillInFolderList {
[nameTextField_ setStringValue:base::SysWideToNSString(node_->GetTitle())];
[self addFolderNodes:model_->root_node() toComboBox:folderComboBox_];
// Add "Choose another folder...". Remember it for later to compare against.
chooseAnotherFolder_.reset(
[l10n_util::GetNSString(IDS_BOOMARK_BUBBLE_CHOOSER_ANOTHER_FOLDER)
retain]);
[folderComboBox_ addItemWithObjectValue:chooseAnotherFolder_.get()];
// Finally, select the current parent.
NSString* parentTitle = base::SysWideToNSString(
node_->GetParent()->GetTitle());
[folderComboBox_ selectItemWithObjectValue:parentTitle];
}
- (BOOL)windowHasBeenClosed {
return ![window_ isVisible];
}
// For the given folder node, walk the tree and add folder names to
// the given combo box.
//
// TODO(jrg): no distinction is made among folders with the same name.
- (void)addFolderNodes:(const BookmarkNode*)parent toComboBox:(NSComboBox*)box {
NSString* title = base::SysWideToNSString(parent->GetTitle());
if ([title length]) { // no title if root
[box addItemWithObjectValue:title];
[titleMapping_ setValue:[NSValue valueWithPointer:parent] forKey:title];
}
for (int i = 0; i < parent->GetChildCount(); i++) {
const BookmarkNode* child = parent->GetChild(i);
if (child->is_folder())
[self addFolderNodes:child toComboBox:box];
}
}
// Look at the dialog; if the user has changed anything, update the
// bookmark node to reflect this.
- (void)updateBookmarkNode {
// First the title...
NSString* oldTitle = base::SysWideToNSString(node_->GetTitle());
NSString* newTitle = [nameTextField_ stringValue];
if (![oldTitle isEqual:newTitle]) {
model_->SetTitle(node_, base::SysNSStringToWide(newTitle));
}
// Then the parent folder.
NSString* oldParentTitle = base::SysWideToNSString(
node_->GetParent()->GetTitle());
NSString* newParentTitle = [folderComboBox_ objectValueOfSelectedItem];
if (![oldParentTitle isEqual:newParentTitle]) {
const BookmarkNode* newParent = static_cast<const BookmarkNode*>(
[[titleMapping_ objectForKey:newParentTitle] pointerValue]);
if (newParent) {
// newParent should only ever possibly be NULL in a unit test.
int index = newParent->GetChildCount();
model_->Move(node_, newParent, index);
}
}
}
- (void)setTitle:(NSString*)title parentFolder:(NSString*)folder {
[nameTextField_ setStringValue:title];
[folderComboBox_ selectItemWithObjectValue:folder];
}
- (NSString*)chooseAnotherFolderString {
return chooseAnotherFolder_.get();
}
@end // implementation BookmarkBubbleController(ExposedForUnitTesting)
|