OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/views/bookmarks/bookmark_menu_delegate.h" | 5 #include "chrome/browser/ui/views/bookmarks/bookmark_menu_delegate.h" |
6 | 6 |
7 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
8 #include "chrome/browser/bookmarks/bookmark_model.h" | 8 #include "chrome/browser/bookmarks/bookmark_model.h" |
9 #include "chrome/browser/bookmarks/bookmark_node_data.h" | 9 #include "chrome/browser/bookmarks/bookmark_node_data.h" |
10 #include "chrome/browser/bookmarks/bookmark_utils.h" | 10 #include "chrome/browser/bookmarks/bookmark_utils.h" |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 real_delegate_ = real_delegate; | 62 real_delegate_ = real_delegate; |
63 if (parent) { | 63 if (parent) { |
64 parent_menu_item_ = parent; | 64 parent_menu_item_ = parent; |
65 int initial_count = parent->GetSubmenu() ? | 65 int initial_count = parent->GetSubmenu() ? |
66 parent->GetSubmenu()->GetMenuItemCount() : 0; | 66 parent->GetSubmenu()->GetMenuItemCount() : 0; |
67 if ((start_child_index < node->child_count()) && | 67 if ((start_child_index < node->child_count()) && |
68 (initial_count > 0)) { | 68 (initial_count > 0)) { |
69 parent->AppendSeparator(); | 69 parent->AppendSeparator(); |
70 } | 70 } |
71 BuildMenu(node, start_child_index, parent, &next_menu_id_); | 71 BuildMenu(node, start_child_index, parent, &next_menu_id_); |
72 if (show_options == SHOW_OTHER_FOLDER) { | 72 if (show_options == SHOW_PERMANENT_FOLDERS) |
73 const BookmarkNode* other_folder = | 73 BuildMenusForPermanentNodes(parent, &next_menu_id_); |
74 profile_->GetBookmarkModel()->other_node(); | |
75 if (!other_folder->empty()) { | |
76 parent->AppendSeparator(); | |
77 BuildOtherFolderMenu(parent, &next_menu_id_); | |
78 } | |
79 } | |
80 } else { | 74 } else { |
81 menu_ = CreateMenu(node, start_child_index, show_options); | 75 menu_ = CreateMenu(node, start_child_index, show_options); |
82 } | 76 } |
83 | 77 |
84 location_ = location; | 78 location_ = location; |
85 } | 79 } |
86 | 80 |
87 void BookmarkMenuDelegate::SetPageNavigator(PageNavigator* navigator) { | 81 void BookmarkMenuDelegate::SetPageNavigator(PageNavigator* navigator) { |
88 page_navigator_ = navigator; | 82 page_navigator_ = navigator; |
89 if (context_menu_.get()) | 83 if (context_menu_.get()) |
90 context_menu_->SetPageNavigator(navigator); | 84 context_menu_->SetPageNavigator(navigator); |
91 } | 85 } |
92 | 86 |
93 void BookmarkMenuDelegate::SetActiveMenu(const BookmarkNode* node, | 87 void BookmarkMenuDelegate::SetActiveMenu(const BookmarkNode* node, |
94 int start_index) { | 88 int start_index) { |
95 DCHECK(!parent_menu_item_); | 89 DCHECK(!parent_menu_item_); |
96 if (!node_to_menu_map_[node]) | 90 if (!node_to_menu_map_[node]) |
97 CreateMenu(node, start_index, HIDE_OTHER_FOLDER); | 91 CreateMenu(node, start_index, HIDE_PERMANENT_FOLDERS); |
98 menu_ = node_to_menu_map_[node]; | 92 menu_ = node_to_menu_map_[node]; |
99 } | 93 } |
100 | 94 |
101 string16 BookmarkMenuDelegate::GetTooltipText( | 95 string16 BookmarkMenuDelegate::GetTooltipText( |
102 int id, | 96 int id, |
103 const gfx::Point& screen_loc) const { | 97 const gfx::Point& screen_loc) const { |
104 DCHECK(menu_id_to_node_map_.find(id) != menu_id_to_node_map_.end()); | 98 DCHECK(menu_id_to_node_map_.find(id) != menu_id_to_node_map_.end()); |
105 | 99 |
106 MenuIDToNodeMap::const_iterator i = menu_id_to_node_map_.find(id); | 100 MenuIDToNodeMap::const_iterator i = menu_id_to_node_map_.find(id); |
107 DCHECK(i != menu_id_to_node_map_.end()); | 101 DCHECK(i != menu_id_to_node_map_.end()); |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 int BookmarkMenuDelegate::GetDropOperation( | 176 int BookmarkMenuDelegate::GetDropOperation( |
183 MenuItemView* item, | 177 MenuItemView* item, |
184 const views::DropTargetEvent& event, | 178 const views::DropTargetEvent& event, |
185 views::MenuDelegate::DropPosition* position) { | 179 views::MenuDelegate::DropPosition* position) { |
186 // Should only get here if we have drop data. | 180 // Should only get here if we have drop data. |
187 DCHECK(drop_data_.is_valid()); | 181 DCHECK(drop_data_.is_valid()); |
188 | 182 |
189 const BookmarkNode* node = menu_id_to_node_map_[item->GetCommand()]; | 183 const BookmarkNode* node = menu_id_to_node_map_[item->GetCommand()]; |
190 const BookmarkNode* drop_parent = node->parent(); | 184 const BookmarkNode* drop_parent = node->parent(); |
191 int index_to_drop_at = drop_parent->GetIndexOf(node); | 185 int index_to_drop_at = drop_parent->GetIndexOf(node); |
192 if (*position == views::MenuDelegate::DROP_AFTER) { | 186 switch (*position) { |
193 if (node == profile_->GetBookmarkModel()->other_node()) { | 187 case views::MenuDelegate::DROP_AFTER: |
194 // The other folder is shown after all bookmarks on the bookmark bar. | 188 if (node == profile_->GetBookmarkModel()->other_node() || |
195 // Dropping after the other folder makes no sense. | 189 node == profile_->GetBookmarkModel()->mobile_node()) { |
196 *position = views::MenuDelegate::DROP_NONE; | 190 // Dropping after these nodes makes no sense. |
197 } | 191 *position = views::MenuDelegate::DROP_NONE; |
198 index_to_drop_at++; | 192 } |
199 } else if (*position == views::MenuDelegate::DROP_ON) { | 193 index_to_drop_at++; |
200 drop_parent = node; | 194 break; |
201 index_to_drop_at = node->child_count(); | 195 |
| 196 case views::MenuDelegate::DROP_BEFORE: |
| 197 if (node == profile_->GetBookmarkModel()->mobile_node()) { |
| 198 // Dropping before this node makes no sense. |
| 199 *position = views::MenuDelegate::DROP_NONE; |
| 200 } |
| 201 index_to_drop_at++; |
| 202 break; |
| 203 |
| 204 case views::MenuDelegate::DROP_ON: |
| 205 drop_parent = node; |
| 206 index_to_drop_at = node->child_count(); |
| 207 break; |
| 208 |
| 209 default: |
| 210 break; |
202 } | 211 } |
203 DCHECK(drop_parent); | 212 DCHECK(drop_parent); |
204 return bookmark_utils::BookmarkDropOperation( | 213 return bookmark_utils::BookmarkDropOperation( |
205 profile_, event, drop_data_, drop_parent, index_to_drop_at); | 214 profile_, event, drop_data_, drop_parent, index_to_drop_at); |
206 } | 215 } |
207 | 216 |
208 int BookmarkMenuDelegate::OnPerformDrop( | 217 int BookmarkMenuDelegate::OnPerformDrop( |
209 MenuItemView* menu, | 218 MenuItemView* menu, |
210 views::MenuDelegate::DropPosition position, | 219 views::MenuDelegate::DropPosition position, |
211 const views::DropTargetEvent& event) { | 220 const views::DropTargetEvent& event) { |
212 const BookmarkNode* drop_node = menu_id_to_node_map_[menu->GetCommand()]; | 221 const BookmarkNode* drop_node = menu_id_to_node_map_[menu->GetCommand()]; |
213 DCHECK(drop_node); | 222 DCHECK(drop_node); |
214 BookmarkModel* model = profile_->GetBookmarkModel(); | 223 BookmarkModel* model = profile_->GetBookmarkModel(); |
215 DCHECK(model); | 224 DCHECK(model); |
216 const BookmarkNode* drop_parent = drop_node->parent(); | 225 const BookmarkNode* drop_parent = drop_node->parent(); |
217 DCHECK(drop_parent); | 226 DCHECK(drop_parent); |
218 int index_to_drop_at = drop_parent->GetIndexOf(drop_node); | 227 int index_to_drop_at = drop_parent->GetIndexOf(drop_node); |
219 switch (position) { | 228 switch (position) { |
220 case views::MenuDelegate::DROP_AFTER: | 229 case views::MenuDelegate::DROP_AFTER: |
221 index_to_drop_at++; | 230 index_to_drop_at++; |
222 break; | 231 break; |
223 | 232 |
224 case views::MenuDelegate::DROP_ON: | 233 case views::MenuDelegate::DROP_ON: |
225 DCHECK(drop_node->is_folder()); | 234 DCHECK(drop_node->is_folder()); |
226 drop_parent = drop_node; | 235 drop_parent = drop_node; |
227 index_to_drop_at = drop_node->child_count(); | 236 index_to_drop_at = drop_node->child_count(); |
228 break; | 237 break; |
229 | 238 |
230 case views::MenuDelegate::DROP_BEFORE: | 239 case views::MenuDelegate::DROP_BEFORE: |
231 if (drop_node == model->other_node()) { | 240 if (drop_node == model->other_node() || |
232 // This can happen with SHOW_OTHER_FOLDER. | 241 drop_node == model->mobile_node()) { |
| 242 // This can happen with SHOW_PERMANENT_FOLDERS. |
233 drop_parent = model->bookmark_bar_node(); | 243 drop_parent = model->bookmark_bar_node(); |
234 index_to_drop_at = drop_parent->child_count(); | 244 index_to_drop_at = drop_parent->child_count(); |
235 } | 245 } |
236 break; | 246 break; |
237 | 247 |
238 default: | 248 default: |
239 break; | 249 break; |
240 } | 250 } |
241 | 251 |
242 int result = bookmark_utils::PerformBookmarkDrop( | 252 int result = bookmark_utils::PerformBookmarkDrop( |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
384 } | 394 } |
385 | 395 |
386 MenuItemView* BookmarkMenuDelegate::CreateMenu(const BookmarkNode* parent, | 396 MenuItemView* BookmarkMenuDelegate::CreateMenu(const BookmarkNode* parent, |
387 int start_child_index, | 397 int start_child_index, |
388 ShowOptions show_options) { | 398 ShowOptions show_options) { |
389 MenuItemView* menu = new MenuItemView(real_delegate_); | 399 MenuItemView* menu = new MenuItemView(real_delegate_); |
390 menu->SetCommand(next_menu_id_++); | 400 menu->SetCommand(next_menu_id_++); |
391 menu_id_to_node_map_[menu->GetCommand()] = parent; | 401 menu_id_to_node_map_[menu->GetCommand()] = parent; |
392 menu->set_has_icons(true); | 402 menu->set_has_icons(true); |
393 BuildMenu(parent, start_child_index, menu, &next_menu_id_); | 403 BuildMenu(parent, start_child_index, menu, &next_menu_id_); |
394 if (show_options == SHOW_OTHER_FOLDER) | 404 if (show_options == SHOW_PERMANENT_FOLDERS) |
395 BuildOtherFolderMenu(menu, &next_menu_id_); | 405 BuildMenusForPermanentNodes(menu, &next_menu_id_); |
396 node_to_menu_map_[parent] = menu; | 406 node_to_menu_map_[parent] = menu; |
397 return menu; | 407 return menu; |
398 } | 408 } |
399 | 409 |
400 void BookmarkMenuDelegate::BuildOtherFolderMenu( | 410 void BookmarkMenuDelegate::BuildMenusForPermanentNodes( |
401 MenuItemView* menu, int* next_menu_id) { | 411 views::MenuItemView* menu, |
402 const BookmarkNode* other_folder = profile_->GetBookmarkModel()->other_node(); | 412 int* next_menu_id) { |
| 413 BookmarkModel* model = profile_->GetBookmarkModel(); |
| 414 bool added_separator = false; |
| 415 BuildMenuForPermanentNode(model->other_node(), menu, next_menu_id, |
| 416 &added_separator); |
| 417 BuildMenuForPermanentNode(model->mobile_node(), menu, next_menu_id, |
| 418 &added_separator); |
| 419 } |
| 420 |
| 421 void BookmarkMenuDelegate::BuildMenuForPermanentNode( |
| 422 const BookmarkNode* node, |
| 423 MenuItemView* menu, |
| 424 int* next_menu_id, |
| 425 bool* added_separator) { |
| 426 if (node->GetTotalNodeCount() == 1) |
| 427 return; // No children, don't create a menu. |
| 428 |
| 429 if (!*added_separator) { |
| 430 *added_separator = true; |
| 431 menu->AppendSeparator(); |
| 432 } |
403 int id = *next_menu_id; | 433 int id = *next_menu_id; |
404 (*next_menu_id)++; | 434 (*next_menu_id)++; |
405 SkBitmap* folder_icon = ResourceBundle::GetSharedInstance(). | 435 SkBitmap* folder_icon = ResourceBundle::GetSharedInstance(). |
406 GetBitmapNamed(IDR_BOOKMARK_BAR_FOLDER); | 436 GetBitmapNamed(IDR_BOOKMARK_BAR_FOLDER); |
407 MenuItemView* submenu = menu->AppendSubMenuWithIcon( | 437 MenuItemView* submenu = menu->AppendSubMenuWithIcon( |
408 id, | 438 id, node->GetTitle(), *folder_icon); |
409 l10n_util::GetStringUTF16(IDS_BOOKMARK_BAR_OTHER_BOOKMARKED), | 439 BuildMenu(node, 0, submenu, next_menu_id); |
410 *folder_icon); | 440 menu_id_to_node_map_[id] = node; |
411 BuildMenu(other_folder, 0, submenu, next_menu_id); | |
412 menu_id_to_node_map_[id] = other_folder; | |
413 } | 441 } |
414 | 442 |
415 void BookmarkMenuDelegate::BuildMenu(const BookmarkNode* parent, | 443 void BookmarkMenuDelegate::BuildMenu(const BookmarkNode* parent, |
416 int start_child_index, | 444 int start_child_index, |
417 MenuItemView* menu, | 445 MenuItemView* menu, |
418 int* next_menu_id) { | 446 int* next_menu_id) { |
419 DCHECK(parent->empty() || start_child_index < parent->child_count()); | 447 DCHECK(parent->empty() || start_child_index < parent->child_count()); |
420 for (int i = start_child_index; i < parent->child_count(); ++i) { | 448 for (int i = start_child_index; i < parent->child_count(); ++i) { |
421 const BookmarkNode* node = parent->GetChild(i); | 449 const BookmarkNode* node = parent->GetChild(i); |
422 int id = *next_menu_id; | 450 int id = *next_menu_id; |
(...skipping 24 matching lines...) Expand all Loading... |
447 MenuItemView* BookmarkMenuDelegate::GetMenuByID(int id) { | 475 MenuItemView* BookmarkMenuDelegate::GetMenuByID(int id) { |
448 for (NodeToMenuMap::const_iterator i = node_to_menu_map_.begin(); | 476 for (NodeToMenuMap::const_iterator i = node_to_menu_map_.begin(); |
449 i != node_to_menu_map_.end(); ++i) { | 477 i != node_to_menu_map_.end(); ++i) { |
450 MenuItemView* menu = i->second->GetMenuItemByID(id); | 478 MenuItemView* menu = i->second->GetMenuItemByID(id); |
451 if (menu) | 479 if (menu) |
452 return menu; | 480 return menu; |
453 } | 481 } |
454 | 482 |
455 return parent_menu_item_ ? parent_menu_item_->GetMenuItemByID(id) : NULL; | 483 return parent_menu_item_ ? parent_menu_item_->GetMenuItemByID(id) : NULL; |
456 } | 484 } |
OLD | NEW |