| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 <limits> | 7 #include <limits> |
| 8 | 8 |
| 9 #include "base/base_drag_source.h" | 9 #include "base/base_drag_source.h" |
| 10 #include "base/gfx/skia_utils.h" | 10 #include "base/gfx/skia_utils.h" |
| 11 #include "chrome/app/theme/theme_resources.h" | 11 #include "chrome/app/theme/theme_resources.h" |
| 12 #include "chrome/browser/bookmark_bar_context_menu_controller.h" | 12 #include "chrome/browser/bookmark_bar_context_menu_controller.h" |
| 13 #include "chrome/browser/bookmarks/bookmark_drag_utils.h" |
| 13 #include "chrome/browser/browser.h" | 14 #include "chrome/browser/browser.h" |
| 14 #include "chrome/browser/browser_list.h" | 15 #include "chrome/browser/browser_list.h" |
| 15 #include "chrome/browser/browser_process.h" | 16 #include "chrome/browser/browser_process.h" |
| 16 #include "chrome/browser/browser_window.h" | 17 #include "chrome/browser/browser_window.h" |
| 17 #include "chrome/browser/drag_utils.h" | 18 #include "chrome/browser/drag_utils.h" |
| 18 #include "chrome/browser/download/download_util.h" | 19 #include "chrome/browser/download/download_util.h" |
| 19 #include "chrome/browser/history/history_backend.h" | 20 #include "chrome/browser/history/history_backend.h" |
| 20 #include "chrome/browser/history/history_database.h" | 21 #include "chrome/browser/history/history_database.h" |
| 21 #include "chrome/browser/history/history.h" | 22 #include "chrome/browser/history/history.h" |
| 22 #include "chrome/browser/page_navigator.h" | 23 #include "chrome/browser/page_navigator.h" |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 static const int kInstructionsPadding = 6; | 142 static const int kInstructionsPadding = 6; |
| 142 | 143 |
| 143 // Color of the instructional text. | 144 // Color of the instructional text. |
| 144 static const SkColor kInstructionsColor = SkColorSetRGB(128, 128, 142); | 145 static const SkColor kInstructionsColor = SkColorSetRGB(128, 128, 142); |
| 145 | 146 |
| 146 // Tag for the other button. | 147 // Tag for the other button. |
| 147 static const int kOtherFolderButtonTag = 1; | 148 static const int kOtherFolderButtonTag = 1; |
| 148 | 149 |
| 149 namespace { | 150 namespace { |
| 150 | 151 |
| 151 // Calculates the drop operation given the event and supported set of | |
| 152 // operations. | |
| 153 int PreferredDropOperation(const DropTargetEvent& event, int operation) { | |
| 154 int common_ops = (event.GetSourceOperations() & operation); | |
| 155 if (!common_ops) | |
| 156 return 0; | |
| 157 if (DragDropTypes::DRAG_COPY & common_ops) | |
| 158 return DragDropTypes::DRAG_COPY; | |
| 159 if (DragDropTypes::DRAG_LINK & common_ops) | |
| 160 return DragDropTypes::DRAG_LINK; | |
| 161 if (DragDropTypes::DRAG_MOVE & common_ops) | |
| 162 return DragDropTypes::DRAG_MOVE; | |
| 163 return DragDropTypes::DRAG_NONE; | |
| 164 } | |
| 165 | |
| 166 // Returns the tooltip text for the specified url and title. The returned | 152 // Returns the tooltip text for the specified url and title. The returned |
| 167 // text is clipped to fit within the bounds of the monitor. | 153 // text is clipped to fit within the bounds of the monitor. |
| 168 // | 154 // |
| 169 // Note that we adjust the direction of both the URL and the title based on the | 155 // Note that we adjust the direction of both the URL and the title based on the |
| 170 // locale so that pure LTR strings are displayed properly in RTL locales. | 156 // locale so that pure LTR strings are displayed properly in RTL locales. |
| 171 static std::wstring CreateToolTipForURLAndTitle(const gfx::Point& screen_loc, | 157 static std::wstring CreateToolTipForURLAndTitle(const gfx::Point& screen_loc, |
| 172 const GURL& url, | 158 const GURL& url, |
| 173 const std::wstring& title, | 159 const std::wstring& title, |
| 174 const std::wstring& languages) { | 160 const std::wstring& languages) { |
| 175 const gfx::Rect monitor_bounds = win_util::GetMonitorBoundsForRect( | 161 const gfx::Rect monitor_bounds = win_util::GetMonitorBoundsForRect( |
| (...skipping 1483 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1659 // Mouse is over 'other' folder. | 1645 // Mouse is over 'other' folder. |
| 1660 *is_over_other = true; | 1646 *is_over_other = true; |
| 1661 *drop_on = true; | 1647 *drop_on = true; |
| 1662 found = true; | 1648 found = true; |
| 1663 } else if (!GetBookmarkButtonCount()) { | 1649 } else if (!GetBookmarkButtonCount()) { |
| 1664 // No bookmarks, accept the drop. | 1650 // No bookmarks, accept the drop. |
| 1665 *index = 0; | 1651 *index = 0; |
| 1666 int ops = data.GetFirstNode(profile_) | 1652 int ops = data.GetFirstNode(profile_) |
| 1667 ? DragDropTypes::DRAG_MOVE | 1653 ? DragDropTypes::DRAG_MOVE |
| 1668 : DragDropTypes::DRAG_COPY | DragDropTypes::DRAG_LINK; | 1654 : DragDropTypes::DRAG_COPY | DragDropTypes::DRAG_LINK; |
| 1669 return PreferredDropOperation(event, ops); | 1655 return bookmark_drag_utils::PreferredDropOperation(event, ops); |
| 1670 } | 1656 } |
| 1671 | 1657 |
| 1672 for (int i = 0; i < GetBookmarkButtonCount() && | 1658 for (int i = 0; i < GetBookmarkButtonCount() && |
| 1673 GetBookmarkButton(i)->IsVisible() && !found; i++) { | 1659 GetBookmarkButton(i)->IsVisible() && !found; i++) { |
| 1674 views::TextButton* button = GetBookmarkButton(i); | 1660 views::TextButton* button = GetBookmarkButton(i); |
| 1675 int button_x = mirrored_x - button->x(); | 1661 int button_x = mirrored_x - button->x(); |
| 1676 int button_w = button->width(); | 1662 int button_w = button->width(); |
| 1677 if (button_x < button_w) { | 1663 if (button_x < button_w) { |
| 1678 found = true; | 1664 found = true; |
| 1679 BookmarkNode* node = model_->GetBookmarkBarNode()->GetChild(i); | 1665 BookmarkNode* node = model_->GetBookmarkBarNode()->GetChild(i); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1736 } else { | 1722 } else { |
| 1737 return CalculateDropOperation(event, data, model_->GetBookmarkBarNode(), | 1723 return CalculateDropOperation(event, data, model_->GetBookmarkBarNode(), |
| 1738 *index); | 1724 *index); |
| 1739 } | 1725 } |
| 1740 } | 1726 } |
| 1741 | 1727 |
| 1742 int BookmarkBarView::CalculateDropOperation(const DropTargetEvent& event, | 1728 int BookmarkBarView::CalculateDropOperation(const DropTargetEvent& event, |
| 1743 const BookmarkDragData& data, | 1729 const BookmarkDragData& data, |
| 1744 BookmarkNode* parent, | 1730 BookmarkNode* parent, |
| 1745 int index) { | 1731 int index) { |
| 1746 if (!CanDropAt(data, parent, index)) | 1732 if (data.IsFromProfile(profile_) && data.size() > 1) |
| 1733 // Currently only accept one dragged node at a time. |
| 1734 return DragDropTypes::DRAG_NONE; |
| 1735 |
| 1736 if (!bookmark_drag_utils::IsValidDropLocation(profile_, data, parent, index)) |
| 1747 return DragDropTypes::DRAG_NONE; | 1737 return DragDropTypes::DRAG_NONE; |
| 1748 | 1738 |
| 1749 if (data.GetFirstNode(profile_)) { | 1739 if (data.GetFirstNode(profile_)) { |
| 1750 // User is dragging from this profile: move. | 1740 // User is dragging from this profile: move. |
| 1751 return DragDropTypes::DRAG_MOVE; | 1741 return DragDropTypes::DRAG_MOVE; |
| 1752 } else { | 1742 } else { |
| 1753 // User is dragging from another app, copy. | 1743 // User is dragging from another app, copy. |
| 1754 return PreferredDropOperation( | 1744 return bookmark_drag_utils::PreferredDropOperation( |
| 1755 event, DragDropTypes::DRAG_COPY | DragDropTypes::DRAG_LINK); | 1745 event, DragDropTypes::DRAG_COPY | DragDropTypes::DRAG_LINK); |
| 1756 } | 1746 } |
| 1757 } | 1747 } |
| 1758 | 1748 |
| 1759 bool BookmarkBarView::CanDropAt(const BookmarkDragData& data, | |
| 1760 BookmarkNode* parent, | |
| 1761 int index) { | |
| 1762 DCHECK(data.is_valid()); | |
| 1763 BookmarkNode* dragged_node = data.GetFirstNode(profile_); | |
| 1764 if (dragged_node) { | |
| 1765 if (dragged_node->GetParent() == parent) { | |
| 1766 const int existing_index = parent->IndexOfChild(dragged_node); | |
| 1767 if (index == existing_index || existing_index + 1 == index) | |
| 1768 return false; | |
| 1769 } | |
| 1770 // Allow the drop only if the node we're going to drop on isn't a | |
| 1771 // descendant of the dragged node. | |
| 1772 BookmarkNode* test_node = parent; | |
| 1773 while (test_node && test_node != dragged_node) | |
| 1774 test_node = test_node->GetParent(); | |
| 1775 return (test_node == NULL); | |
| 1776 } // else case clones, always allow. | |
| 1777 return true; | |
| 1778 } | |
| 1779 | |
| 1780 | |
| 1781 int BookmarkBarView::PerformDropImpl(const BookmarkDragData& data, | 1749 int BookmarkBarView::PerformDropImpl(const BookmarkDragData& data, |
| 1782 BookmarkNode* parent_node, | 1750 BookmarkNode* parent_node, |
| 1783 int index) { | 1751 int index) { |
| 1784 BookmarkNode* dragged_node = data.GetFirstNode(profile_); | 1752 BookmarkNode* dragged_node = data.GetFirstNode(profile_); |
| 1785 if (dragged_node) { | 1753 if (dragged_node) { |
| 1786 // Drag from same profile, do a move. | 1754 // Drag from same profile, do a move. |
| 1787 model_->Move(dragged_node, parent_node, index); | 1755 model_->Move(dragged_node, parent_node, index); |
| 1788 return DragDropTypes::DRAG_MOVE; | 1756 return DragDropTypes::DRAG_MOVE; |
| 1789 } else if (data.has_single_url()) { | 1757 } else if (data.has_single_url()) { |
| 1790 // New URL, add it at the specified location. | 1758 // New URL, add it at the specified location. |
| 1791 std::wstring title = data.elements[0].title; | 1759 std::wstring title = data.elements[0].title; |
| 1792 if (title.empty()) { | 1760 if (title.empty()) { |
| 1793 // No title, use the host. | 1761 // No title, use the host. |
| 1794 title = UTF8ToWide(data.elements[0].url.host()); | 1762 title = UTF8ToWide(data.elements[0].url.host()); |
| 1795 if (title.empty()) | 1763 if (title.empty()) |
| 1796 title = l10n_util::GetString(IDS_BOOMARK_BAR_UNKNOWN_DRAG_TITLE); | 1764 title = l10n_util::GetString(IDS_BOOMARK_BAR_UNKNOWN_DRAG_TITLE); |
| 1797 } | 1765 } |
| 1798 model_->AddURL(parent_node, index, title, data.elements[0].url); | 1766 model_->AddURL(parent_node, index, title, data.elements[0].url); |
| 1799 return DragDropTypes::DRAG_COPY | DragDropTypes::DRAG_LINK; | 1767 return DragDropTypes::DRAG_COPY | DragDropTypes::DRAG_LINK; |
| 1800 } else { | 1768 } else { |
| 1801 // Dropping a group from different profile. Always accept. | 1769 // Dropping a group from different profile. Always accept. |
| 1802 CloneDragData(data.elements[0], parent_node, index); | 1770 bookmark_drag_utils::CloneDragData(model_, data.elements, parent_node, |
| 1771 index); |
| 1803 return DragDropTypes::DRAG_COPY; | 1772 return DragDropTypes::DRAG_COPY; |
| 1804 } | 1773 } |
| 1805 } | 1774 } |
| 1806 | 1775 |
| 1807 void BookmarkBarView::CloneDragData(const BookmarkDragData::Element& element, | |
| 1808 BookmarkNode* parent, | |
| 1809 int index_to_add_at) { | |
| 1810 DCHECK(model_); | |
| 1811 if (element.is_url) { | |
| 1812 model_->AddURL(parent, index_to_add_at, element.title, element.url); | |
| 1813 } else { | |
| 1814 BookmarkNode* new_folder = model_->AddGroup(parent, index_to_add_at, | |
| 1815 element.title); | |
| 1816 for (int i = 0; i < static_cast<int>(element.children.size()); ++i) | |
| 1817 CloneDragData(element.children[i], new_folder, i); | |
| 1818 } | |
| 1819 } | |
| 1820 | |
| 1821 int BookmarkBarView::GetFirstHiddenNodeIndex() { | 1776 int BookmarkBarView::GetFirstHiddenNodeIndex() { |
| 1822 const int bb_count = GetBookmarkButtonCount(); | 1777 const int bb_count = GetBookmarkButtonCount(); |
| 1823 for (int i = 0; i < bb_count; ++i) { | 1778 for (int i = 0; i < bb_count; ++i) { |
| 1824 if (!GetBookmarkButton(i)->IsVisible()) | 1779 if (!GetBookmarkButton(i)->IsVisible()) |
| 1825 return i; | 1780 return i; |
| 1826 } | 1781 } |
| 1827 return bb_count; | 1782 return bb_count; |
| 1828 } | 1783 } |
| 1829 | 1784 |
| 1830 void BookmarkBarView::StartThrobbing() { | 1785 void BookmarkBarView::StartThrobbing() { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1863 } | 1818 } |
| 1864 | 1819 |
| 1865 void BookmarkBarView::StopThrobbing(bool immediate) { | 1820 void BookmarkBarView::StopThrobbing(bool immediate) { |
| 1866 if (!throbbing_view_) | 1821 if (!throbbing_view_) |
| 1867 return; | 1822 return; |
| 1868 | 1823 |
| 1869 // If not immediate, cycle through 2 more complete cycles. | 1824 // If not immediate, cycle through 2 more complete cycles. |
| 1870 throbbing_view_->StartThrobbing(immediate ? 0 : 4); | 1825 throbbing_view_->StartThrobbing(immediate ? 0 : 4); |
| 1871 throbbing_view_ = NULL; | 1826 throbbing_view_ = NULL; |
| 1872 } | 1827 } |
| OLD | NEW |