OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "app/l10n_util_mac.h" |
| 6 #include "base/mac_util.h" |
| 7 #include "base/sys_string_conversions.h" |
| 8 #include "chrome/browser/bookmarks/bookmark_model.h" |
| 9 #import "chrome/browser/cocoa/bookmark_bubble_controller.h" |
| 10 #import "chrome/browser/cocoa/bookmark_bubble_window.h" |
| 11 #include "grit/generated_resources.h" |
| 12 |
| 13 |
| 14 @interface BookmarkBubbleController(PrivateAPI) |
| 15 - (void)closeWindow; |
| 16 @end |
| 17 |
| 18 @implementation BookmarkBubbleController |
| 19 |
| 20 @synthesize delegate = delegate_; |
| 21 @synthesize folderComboBox = folderComboBox_; |
| 22 |
| 23 - (id)initWithDelegate:(id<BookmarkBubbleControllerDelegate>)delegate |
| 24 parentWindow:(NSWindow*)parentWindow |
| 25 topLeftForBubble:(NSPoint)topLeftForBubble |
| 26 model:(BookmarkModel*)model |
| 27 node:(const BookmarkNode*)node |
| 28 alreadyBookmarked:(BOOL)alreadyBookmarked { |
| 29 if ((self = [super initWithNibName:@"BookmarkBubble" |
| 30 bundle:mac_util::MainAppBundle()])) { |
| 31 // all these are weak... |
| 32 delegate_ = delegate; |
| 33 parentWindow_ = parentWindow; |
| 34 topLeftForBubble_ = topLeftForBubble; |
| 35 model_ = model; |
| 36 node_ = node; |
| 37 alreadyBookmarked_ = alreadyBookmarked; |
| 38 // But this is strong. |
| 39 titleMapping_.reset([[NSMutableDictionary alloc] init]); |
| 40 } |
| 41 return self; |
| 42 } |
| 43 |
| 44 - (void)dealloc { |
| 45 [self closeWindow]; |
| 46 [super dealloc]; |
| 47 } |
| 48 |
| 49 - (void)showWindow { |
| 50 [self view]; // force nib load and window_ allocation |
| 51 [window_ makeKeyAndOrderFront:self]; |
| 52 } |
| 53 |
| 54 // Actually close the window. Do nothing else. |
| 55 - (void)closeWindow { |
| 56 [parentWindow_ removeChildWindow:window_]; |
| 57 [window_ close]; |
| 58 } |
| 59 |
| 60 - (void)awakeFromNib { |
| 61 window_.reset([self createBubbleWindow]); |
| 62 [parentWindow_ addChildWindow:window_ ordered:NSWindowAbove]; |
| 63 |
| 64 // Fill in inital values for text, controls, ... |
| 65 |
| 66 // Default is IDS_BOOMARK_BUBBLE_PAGE_BOOKMARK; "Bookmark". |
| 67 // If adding for the 1st time the string becomes "Bookmark Added!" |
| 68 if (!alreadyBookmarked_) { |
| 69 NSString* title = |
| 70 l10n_util::GetNSString(IDS_BOOMARK_BUBBLE_PAGE_BOOKMARKED); |
| 71 [bigTitle_ setStringValue:title]; |
| 72 } |
| 73 |
| 74 [self fillInFolderList]; |
| 75 } |
| 76 |
| 77 - (IBAction)edit:(id)sender { |
| 78 [self updateBookmarkNode]; |
| 79 [self closeWindow]; |
| 80 [delegate_ editBookmarkNode:node_]; |
| 81 [delegate_ doneWithBubbleController:self]; |
| 82 } |
| 83 |
| 84 - (IBAction)close:(id)sender { |
| 85 if (node_) { |
| 86 // no node_ if the bookmark was just removed |
| 87 [self updateBookmarkNode]; |
| 88 } |
| 89 [self closeWindow]; |
| 90 [delegate_ doneWithBubbleController:self]; |
| 91 } |
| 92 |
| 93 // By implementing this, ESC causes the window to go away. |
| 94 - (IBAction)cancel:(id)sender { |
| 95 [self close:sender]; |
| 96 } |
| 97 |
| 98 - (IBAction)remove:(id)sender { |
| 99 model_->SetURLStarred(node_->GetURL(), node_->GetTitle(), false); |
| 100 node_ = NULL; // no longer valid |
| 101 [self close:self]; |
| 102 } |
| 103 |
| 104 // We are the delegate of the combo box so we can tell when "choose |
| 105 // another folder" was picked. |
| 106 - (void)comboBoxSelectionDidChange:(NSNotification*)notification { |
| 107 NSString* selected = [folderComboBox_ objectValueOfSelectedItem]; |
| 108 if ([selected isEqual:chooseAnotherFolder_.get()]) { |
| 109 [self edit:self]; |
| 110 } |
| 111 } |
| 112 |
| 113 // We are the delegate of our own window so we know when we lose key. |
| 114 // When we lose key status we close, mirroring Windows behaivor. |
| 115 - (void)windowDidResignKey:(NSNotification*)notification { |
| 116 if ([window_ isVisible]) |
| 117 [self close:self]; |
| 118 } |
| 119 |
| 120 @end // BookmarkBubbleController |
| 121 |
| 122 |
| 123 @implementation BookmarkBubbleController(ExposedForUnitTesting) |
| 124 |
| 125 // Create and return a retained NSWindow for this bubble. |
| 126 - (NSWindow*)createBubbleWindow { |
| 127 NSRect contentRect = [[self view] frame]; |
| 128 NSPoint origin = topLeftForBubble_; |
| 129 origin.y -= contentRect.size.height; // since it'll be our bottom-left |
| 130 contentRect.origin = origin; |
| 131 // Now convert to global coordinates since it'll be used for a window. |
| 132 contentRect.origin = [parentWindow_ convertBaseToScreen:contentRect.origin]; |
| 133 NSWindow* window = [[BookmarkBubbleWindow alloc] |
| 134 initWithContentRect:contentRect]; |
| 135 [window setDelegate:self]; |
| 136 [window setContentView:[self view]]; |
| 137 return window; |
| 138 } |
| 139 |
| 140 // Fill in all information related to the folder combo box. |
| 141 // |
| 142 // TODO(jrg): make sure nested folders that have the same name are |
| 143 // handled properly. |
| 144 // http://crbug.com/19408 |
| 145 - (void)fillInFolderList { |
| 146 [nameTextField_ setStringValue:base::SysWideToNSString(node_->GetTitle())]; |
| 147 [self addFolderNodes:model_->root_node() toComboBox:folderComboBox_]; |
| 148 |
| 149 // Add "Choose another folder...". Remember it for later to compare against. |
| 150 chooseAnotherFolder_.reset( |
| 151 [l10n_util::GetNSString(IDS_BOOMARK_BUBBLE_CHOOSER_ANOTHER_FOLDER) |
| 152 retain]); |
| 153 [folderComboBox_ addItemWithObjectValue:chooseAnotherFolder_.get()]; |
| 154 |
| 155 // Finally, select the current parent. |
| 156 NSString* parentTitle = base::SysWideToNSString( |
| 157 node_->GetParent()->GetTitle()); |
| 158 [folderComboBox_ selectItemWithObjectValue:parentTitle]; |
| 159 } |
| 160 |
| 161 - (BOOL)windowHasBeenClosed { |
| 162 return ![window_ isVisible]; |
| 163 } |
| 164 |
| 165 // For the given folder node, walk the tree and add folder names to |
| 166 // the given combo box. |
| 167 // |
| 168 // TODO(jrg): no distinction is made among folders with the same name. |
| 169 - (void)addFolderNodes:(const BookmarkNode*)parent toComboBox:(NSComboBox*)box { |
| 170 NSString* title = base::SysWideToNSString(parent->GetTitle()); |
| 171 if ([title length]) { // no title if root |
| 172 [box addItemWithObjectValue:title]; |
| 173 [titleMapping_ setValue:[NSValue valueWithPointer:parent] forKey:title]; |
| 174 } |
| 175 for (int i = 0; i < parent->GetChildCount(); i++) { |
| 176 const BookmarkNode* child = parent->GetChild(i); |
| 177 if (child->is_folder()) |
| 178 [self addFolderNodes:child toComboBox:box]; |
| 179 } |
| 180 } |
| 181 |
| 182 // Look at the dialog; if the user has changed anything, update the |
| 183 // bookmark node to reflect this. |
| 184 - (void)updateBookmarkNode { |
| 185 // First the title... |
| 186 NSString* oldTitle = base::SysWideToNSString(node_->GetTitle()); |
| 187 NSString* newTitle = [nameTextField_ stringValue]; |
| 188 if (![oldTitle isEqual:newTitle]) { |
| 189 model_->SetTitle(node_, base::SysNSStringToWide(newTitle)); |
| 190 } |
| 191 // Then the parent folder. |
| 192 NSString* oldParentTitle = base::SysWideToNSString( |
| 193 node_->GetParent()->GetTitle()); |
| 194 NSString* newParentTitle = [folderComboBox_ objectValueOfSelectedItem]; |
| 195 if (![oldParentTitle isEqual:newParentTitle]) { |
| 196 const BookmarkNode* newParent = static_cast<const BookmarkNode*>( |
| 197 [[titleMapping_ objectForKey:newParentTitle] pointerValue]); |
| 198 if (newParent) { |
| 199 // newParent should only ever possibly be NULL in a unit test. |
| 200 int index = newParent->GetChildCount(); |
| 201 model_->Move(node_, newParent, index); |
| 202 } |
| 203 } |
| 204 } |
| 205 |
| 206 - (void)setTitle:(NSString*)title parentFolder:(NSString*)folder { |
| 207 [nameTextField_ setStringValue:title]; |
| 208 [folderComboBox_ selectItemWithObjectValue:folder]; |
| 209 } |
| 210 |
| 211 - (NSString*)chooseAnotherFolderString { |
| 212 return chooseAnotherFolder_.get(); |
| 213 } |
| 214 |
| 215 @end // implementation BookmarkBubbleController(ExposedForUnitTesting) |
OLD | NEW |