OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 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/views/bookmark_bar_view.h" | 5 #include "chrome/browser/views/bookmark_bar_view.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <limits> | 8 #include <limits> |
9 #include <set> | 9 #include <set> |
10 #include <vector> | 10 #include <vector> |
(...skipping 20 matching lines...) Expand all Loading... |
31 #include "chrome/browser/views/frame/browser_view.h" | 31 #include "chrome/browser/views/frame/browser_view.h" |
32 #include "chrome/browser/views/location_bar_view.h" | 32 #include "chrome/browser/views/location_bar_view.h" |
33 #include "chrome/common/notification_service.h" | 33 #include "chrome/common/notification_service.h" |
34 #include "chrome/common/page_transition_types.h" | 34 #include "chrome/common/page_transition_types.h" |
35 #include "chrome/common/pref_names.h" | 35 #include "chrome/common/pref_names.h" |
36 #include "chrome/common/pref_service.h" | 36 #include "chrome/common/pref_service.h" |
37 #include "grit/app_resources.h" | 37 #include "grit/app_resources.h" |
38 #include "grit/generated_resources.h" | 38 #include "grit/generated_resources.h" |
39 #include "grit/theme_resources.h" | 39 #include "grit/theme_resources.h" |
40 #include "views/controls/button/menu_button.h" | 40 #include "views/controls/button/menu_button.h" |
| 41 #include "views/controls/label.h" |
| 42 #include "views/controls/button/menu_button.h" |
41 #include "views/controls/menu/menu_item_view.h" | 43 #include "views/controls/menu/menu_item_view.h" |
42 #include "views/drag_utils.h" | 44 #include "views/drag_utils.h" |
43 #include "views/view_constants.h" | 45 #include "views/view_constants.h" |
44 #include "views/widget/tooltip_manager.h" | 46 #include "views/widget/tooltip_manager.h" |
45 #include "views/widget/widget.h" | 47 #include "views/widget/widget.h" |
46 #include "views/window/window.h" | 48 #include "views/window/window.h" |
47 | 49 |
48 using views::CustomButton; | 50 using views::CustomButton; |
49 using views::DropTargetEvent; | 51 using views::DropTargetEvent; |
50 using views::MenuButton; | 52 using views::MenuButton; |
(...skipping 739 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
790 } | 792 } |
791 | 793 |
792 views::MenuItemView* BookmarkBarView::GetContextMenu() { | 794 views::MenuItemView* BookmarkBarView::GetContextMenu() { |
793 return bookmark_menu_ ? bookmark_menu_->context_menu() : NULL; | 795 return bookmark_menu_ ? bookmark_menu_->context_menu() : NULL; |
794 } | 796 } |
795 | 797 |
796 views::MenuItemView* BookmarkBarView::GetDropMenu() { | 798 views::MenuItemView* BookmarkBarView::GetDropMenu() { |
797 return bookmark_drop_menu_ ? bookmark_drop_menu_->menu() : NULL; | 799 return bookmark_drop_menu_ ? bookmark_drop_menu_->menu() : NULL; |
798 } | 800 } |
799 | 801 |
| 802 const BookmarkNode* BookmarkBarView::GetNodeForButtonAt(const gfx::Point& loc, |
| 803 int* start_index) { |
| 804 *start_index = 0; |
| 805 |
| 806 if (loc.x() < 0 || loc.x() >= width() || loc.y() < 0 || loc.y() >= height()) |
| 807 return NULL; |
| 808 |
| 809 // Check the buttons first. |
| 810 for (int i = 0; i < GetBookmarkButtonCount(); ++i) { |
| 811 views::View* child = GetChildViewAt(i); |
| 812 if (!child->IsVisible()) |
| 813 break; |
| 814 if (child->bounds().Contains(loc)) |
| 815 return model_->GetBookmarkBarNode()->GetChild(i); |
| 816 } |
| 817 |
| 818 // Then the overflow button. |
| 819 if (overflow_button_->IsVisible() && |
| 820 overflow_button_->bounds().Contains(loc)) { |
| 821 *start_index = GetFirstHiddenNodeIndex(); |
| 822 return model_->GetBookmarkBarNode(); |
| 823 } |
| 824 |
| 825 // And finally the other folder. |
| 826 if (other_bookmarked_button_->bounds().Contains(loc)) |
| 827 return model_->other_node(); |
| 828 |
| 829 return NULL; |
| 830 } |
| 831 |
| 832 views::MenuButton* BookmarkBarView::GetMenuButtonForNode( |
| 833 const BookmarkNode* node) { |
| 834 if (node == model_->other_node()) |
| 835 return other_bookmarked_button_; |
| 836 if (node == model_->GetBookmarkBarNode()) |
| 837 return overflow_button_; |
| 838 int index = model_->GetBookmarkBarNode()->IndexOfChild(node); |
| 839 if (index == -1 || !node->is_folder()) |
| 840 return NULL; |
| 841 return static_cast<views::MenuButton*>(GetChildViewAt(index)); |
| 842 } |
| 843 |
| 844 void BookmarkBarView::GetAnchorPositionAndStartIndexForButton( |
| 845 views::MenuButton* button, |
| 846 MenuItemView::AnchorPosition* anchor, |
| 847 int* start_index) { |
| 848 if (button == other_bookmarked_button_ || button == overflow_button_) |
| 849 *anchor = MenuItemView::TOPRIGHT; |
| 850 else |
| 851 *anchor = MenuItemView::TOPLEFT; |
| 852 |
| 853 // Invert orientation if right to left. |
| 854 if (UILayoutIsRightToLeft()) { |
| 855 if (*anchor == MenuItemView::TOPRIGHT) |
| 856 *anchor = MenuItemView::TOPLEFT; |
| 857 else |
| 858 *anchor = MenuItemView::TOPRIGHT; |
| 859 } |
| 860 |
| 861 if (button == overflow_button_) |
| 862 *start_index = GetFirstHiddenNodeIndex(); |
| 863 else |
| 864 *start_index = 0; |
| 865 } |
| 866 |
800 void BookmarkBarView::Init() { | 867 void BookmarkBarView::Init() { |
801 // Note that at this point we're not in a hierarchy so GetThemeProvider() will | 868 // Note that at this point we're not in a hierarchy so GetThemeProvider() will |
802 // return NULL. When we're inserted into a hierarchy, we'll call | 869 // return NULL. When we're inserted into a hierarchy, we'll call |
803 // UpdateColors(), which will set the appropriate colors for all the objects | 870 // UpdateColors(), which will set the appropriate colors for all the objects |
804 // added in this function. | 871 // added in this function. |
805 | 872 |
806 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 873 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
807 | 874 |
808 if (!kDefaultFavIcon) | 875 if (!kDefaultFavIcon) |
809 kDefaultFavIcon = rb.GetBitmapNamed(IDR_DEFAULT_FAVICON); | 876 kDefaultFavIcon = rb.GetBitmapNamed(IDR_DEFAULT_FAVICON); |
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1065 return bookmark_utils::BookmarkDragOperation( | 1132 return bookmark_utils::BookmarkDragOperation( |
1066 model_->GetBookmarkBarNode()->GetChild(i)); | 1133 model_->GetBookmarkBarNode()->GetChild(i)); |
1067 } | 1134 } |
1068 } | 1135 } |
1069 NOTREACHED(); | 1136 NOTREACHED(); |
1070 return DragDropTypes::DRAG_NONE; | 1137 return DragDropTypes::DRAG_NONE; |
1071 } | 1138 } |
1072 | 1139 |
1073 void BookmarkBarView::RunMenu(views::View* view, const gfx::Point& pt) { | 1140 void BookmarkBarView::RunMenu(views::View* view, const gfx::Point& pt) { |
1074 const BookmarkNode* node; | 1141 const BookmarkNode* node; |
1075 MenuItemView::AnchorPosition anchor_point = MenuItemView::TOPLEFT; | |
1076 | |
1077 // When we set the menu's position, we must take into account the mirrored | |
1078 // position of the View relative to its parent. This can be easily done by | |
1079 // passing the right flag to View::x(). | |
1080 int x = view->GetX(APPLY_MIRRORING_TRANSFORMATION); | |
1081 int bar_height = height() - kMenuOffset; | |
1082 | |
1083 if (IsDetached()) | |
1084 bar_height -= kNewtabVerticalPadding; | |
1085 | 1142 |
1086 int start_index = 0; | 1143 int start_index = 0; |
1087 if (view == other_bookmarked_button_) { | 1144 if (view == other_bookmarked_button_) { |
1088 UserMetrics::RecordAction(L"BookmarkBar_ShowOtherBookmarks", profile_); | |
1089 | |
1090 node = model_->other_node(); | 1145 node = model_->other_node(); |
1091 if (UILayoutIsRightToLeft()) | |
1092 anchor_point = MenuItemView::TOPLEFT; | |
1093 else | |
1094 anchor_point = MenuItemView::TOPRIGHT; | |
1095 } else if (view == overflow_button_) { | 1146 } else if (view == overflow_button_) { |
1096 node = model_->GetBookmarkBarNode(); | 1147 node = model_->GetBookmarkBarNode(); |
1097 start_index = GetFirstHiddenNodeIndex(); | 1148 start_index = GetFirstHiddenNodeIndex(); |
1098 if (UILayoutIsRightToLeft()) | |
1099 anchor_point = MenuItemView::TOPLEFT; | |
1100 else | |
1101 anchor_point = MenuItemView::TOPRIGHT; | |
1102 } else { | 1149 } else { |
1103 int button_index = GetChildIndex(view); | 1150 int button_index = GetChildIndex(view); |
1104 DCHECK_NE(-1, button_index); | 1151 DCHECK_NE(-1, button_index); |
1105 node = model_->GetBookmarkBarNode()->GetChild(button_index); | 1152 node = model_->GetBookmarkBarNode()->GetChild(button_index); |
| 1153 } |
1106 | 1154 |
1107 // When the UI layout is RTL, the bookmarks are laid out from right to left | |
1108 // and therefore when we display the menu we want it to be aligned with the | |
1109 // bottom right corner of the bookmark item. | |
1110 if (UILayoutIsRightToLeft()) | |
1111 anchor_point = MenuItemView::TOPRIGHT; | |
1112 else | |
1113 anchor_point = MenuItemView::TOPLEFT; | |
1114 } | |
1115 gfx::Point screen_loc(x, 0); | |
1116 View::ConvertPointToScreen(this, &screen_loc); | |
1117 bookmark_menu_ = new BookmarkMenuController( | 1155 bookmark_menu_ = new BookmarkMenuController( |
1118 browser_, profile_, page_navigator_, GetWindow()->GetNativeWindow(), | 1156 browser_, profile_, page_navigator_, GetWindow()->GetNativeWindow(), |
1119 node, start_index, false); | 1157 node, start_index, false); |
1120 bookmark_menu_->set_observer(this); | 1158 bookmark_menu_->set_observer(this); |
1121 bookmark_menu_->RunMenuAt(gfx::Rect(screen_loc.x(), screen_loc.y(), | 1159 bookmark_menu_->RunMenuAt(this, false); |
1122 view->width(), bar_height), | |
1123 anchor_point, false); | |
1124 } | 1160 } |
1125 | 1161 |
1126 void BookmarkBarView::ButtonPressed(views::Button* sender, | 1162 void BookmarkBarView::ButtonPressed(views::Button* sender, |
1127 const views::Event& event) { | 1163 const views::Event& event) { |
1128 #if defined(BROWSER_SYNC) | 1164 #if defined(BROWSER_SYNC) |
1129 // Show the login wizard if the user clicked the re-login button. | 1165 // Show the login wizard if the user clicked the re-login button. |
1130 if (sender->tag() == kSyncErrorButtonTag) { | 1166 if (sender->tag() == kSyncErrorButtonTag) { |
1131 DCHECK(sender == sync_error_button_); | 1167 DCHECK(sender == sync_error_button_); |
1132 DCHECK(sync_service_); | 1168 DCHECK(sync_service_); |
1133 sync_service_->ShowLoginDialog(); | 1169 sync_service_->ShowLoginDialog(); |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1291 | 1327 |
1292 void BookmarkBarView::ShowDropFolderForNode(const BookmarkNode* node) { | 1328 void BookmarkBarView::ShowDropFolderForNode(const BookmarkNode* node) { |
1293 if (bookmark_drop_menu_) { | 1329 if (bookmark_drop_menu_) { |
1294 if (bookmark_drop_menu_->node() == node) { | 1330 if (bookmark_drop_menu_->node() == node) { |
1295 // Already showing for the specified node. | 1331 // Already showing for the specified node. |
1296 return; | 1332 return; |
1297 } | 1333 } |
1298 bookmark_drop_menu_->Cancel(); | 1334 bookmark_drop_menu_->Cancel(); |
1299 } | 1335 } |
1300 | 1336 |
| 1337 views::MenuButton* menu_button = GetMenuButtonForNode(node); |
| 1338 if (!menu_button) |
| 1339 return; |
| 1340 |
1301 int start_index = 0; | 1341 int start_index = 0; |
1302 View* view_to_position_menu_from; | 1342 if (node == model_->GetBookmarkBarNode()) |
1303 | |
1304 // Note that both the anchor position and the position of the menu itself | |
1305 // change depending on the locale. Also note that we must apply the | |
1306 // mirroring transformation when querying for the child View bounds | |
1307 // (View::x(), specifically) so that we end up with the correct screen | |
1308 // coordinates if the View in question is mirrored. | |
1309 MenuItemView::AnchorPosition anchor = MenuItemView::TOPLEFT; | |
1310 if (node == model_->other_node()) { | |
1311 view_to_position_menu_from = other_bookmarked_button_; | |
1312 if (!UILayoutIsRightToLeft()) | |
1313 anchor = MenuItemView::TOPRIGHT; | |
1314 } else if (node == model_->GetBookmarkBarNode()) { | |
1315 DCHECK(overflow_button_->IsVisible()); | |
1316 view_to_position_menu_from = overflow_button_; | |
1317 start_index = GetFirstHiddenNodeIndex(); | 1343 start_index = GetFirstHiddenNodeIndex(); |
1318 if (!UILayoutIsRightToLeft()) | |
1319 anchor = MenuItemView::TOPRIGHT; | |
1320 } else { | |
1321 // Make sure node is still valid. | |
1322 int index = -1; | |
1323 const BookmarkNode* bb_node = model_->GetBookmarkBarNode(); | |
1324 for (int i = 0; i < GetBookmarkButtonCount(); ++i) { | |
1325 if (bb_node->GetChild(i) == node) { | |
1326 index = i; | |
1327 break; | |
1328 } | |
1329 } | |
1330 if (index == -1) | |
1331 return; | |
1332 view_to_position_menu_from = GetBookmarkButton(index); | |
1333 if (UILayoutIsRightToLeft()) | |
1334 anchor = MenuItemView::TOPRIGHT; | |
1335 } | |
1336 | 1344 |
1337 drop_info_->is_menu_showing = true; | 1345 drop_info_->is_menu_showing = true; |
1338 bookmark_drop_menu_ = new BookmarkMenuController( | 1346 bookmark_drop_menu_ = new BookmarkMenuController( |
1339 browser_, profile_, page_navigator_, GetWindow()->GetNativeWindow(), | 1347 browser_, profile_, page_navigator_, GetWindow()->GetNativeWindow(), |
1340 node, start_index, false); | 1348 node, start_index, false); |
1341 bookmark_drop_menu_->set_observer(this); | 1349 bookmark_drop_menu_->set_observer(this); |
1342 gfx::Point screen_loc; | 1350 bookmark_drop_menu_->RunMenuAt(this, true); |
1343 View::ConvertPointToScreen(view_to_position_menu_from, &screen_loc); | |
1344 bookmark_drop_menu_->RunMenuAt( | |
1345 gfx::Rect(screen_loc.x(), screen_loc.y(), | |
1346 view_to_position_menu_from->width(), | |
1347 view_to_position_menu_from->height()), | |
1348 anchor, true); | |
1349 } | 1351 } |
1350 | 1352 |
1351 void BookmarkBarView::StopShowFolderDropMenuTimer() { | 1353 void BookmarkBarView::StopShowFolderDropMenuTimer() { |
1352 if (show_folder_drop_menu_task_) | 1354 if (show_folder_drop_menu_task_) |
1353 show_folder_drop_menu_task_->Cancel(); | 1355 show_folder_drop_menu_task_->Cancel(); |
1354 } | 1356 } |
1355 | 1357 |
1356 void BookmarkBarView::StartShowFolderDropMenuTimer(const BookmarkNode* node) { | 1358 void BookmarkBarView::StartShowFolderDropMenuTimer(const BookmarkNode* node) { |
1357 if (testing_) { | 1359 if (testing_) { |
1358 // So that tests can run as fast as possible disable the delay during | 1360 // So that tests can run as fast as possible disable the delay during |
(...skipping 370 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1729 sync_error_button->SetTooltipText( | 1731 sync_error_button->SetTooltipText( |
1730 l10n_util::GetString(IDS_SYNC_BOOKMARK_BAR_ERROR_DESC)); | 1732 l10n_util::GetString(IDS_SYNC_BOOKMARK_BAR_ERROR_DESC)); |
1731 sync_error_button->SetAccessibleName( | 1733 sync_error_button->SetAccessibleName( |
1732 l10n_util::GetString(IDS_ACCNAME_SYNC_ERROR_BUTTON)); | 1734 l10n_util::GetString(IDS_ACCNAME_SYNC_ERROR_BUTTON)); |
1733 sync_error_button->SetIcon( | 1735 sync_error_button->SetIcon( |
1734 *ResourceBundle::GetSharedInstance().GetBitmapNamed(IDR_WARNING)); | 1736 *ResourceBundle::GetSharedInstance().GetBitmapNamed(IDR_WARNING)); |
1735 return sync_error_button; | 1737 return sync_error_button; |
1736 } | 1738 } |
1737 #endif // defined(BROWSER_SYNC) | 1739 #endif // defined(BROWSER_SYNC) |
1738 | 1740 |
OLD | NEW |