| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/gtk/location_bar_view_gtk.h" | 5 #include "chrome/browser/ui/gtk/location_bar_view_gtk.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 1603 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1614 } | 1614 } |
| 1615 | 1615 |
| 1616 //////////////////////////////////////////////////////////////////////////////// | 1616 //////////////////////////////////////////////////////////////////////////////// |
| 1617 // LocationBarViewGtk::PageActionViewGtk | 1617 // LocationBarViewGtk::PageActionViewGtk |
| 1618 | 1618 |
| 1619 LocationBarViewGtk::PageActionViewGtk::PageActionViewGtk( | 1619 LocationBarViewGtk::PageActionViewGtk::PageActionViewGtk( |
| 1620 LocationBarViewGtk* owner, | 1620 LocationBarViewGtk* owner, |
| 1621 ExtensionAction* page_action) | 1621 ExtensionAction* page_action) |
| 1622 : owner_(NULL), | 1622 : owner_(NULL), |
| 1623 page_action_(page_action), | 1623 page_action_(page_action), |
| 1624 last_icon_pixbuf_(NULL), | |
| 1625 tracker_(this), | 1624 tracker_(this), |
| 1626 current_tab_id_(-1), | 1625 current_tab_id_(-1), |
| 1627 window_(NULL), | 1626 window_(NULL), |
| 1628 accel_group_(NULL), | 1627 accel_group_(NULL), |
| 1629 preview_enabled_(false), | 1628 preview_enabled_(false), |
| 1630 ALLOW_THIS_IN_INITIALIZER_LIST(scoped_icon_animation_observer_( | 1629 ALLOW_THIS_IN_INITIALIZER_LIST(scoped_icon_animation_observer_( |
| 1631 page_action->GetIconAnimation( | 1630 page_action->GetIconAnimation( |
| 1632 SessionID::IdForTab(owner->GetTabContents())), | 1631 SessionID::IdForTab(owner->GetTabContents())), |
| 1633 this)) { | 1632 this)) { |
| 1634 event_box_.Own(gtk_event_box_new()); | 1633 event_box_.Own(gtk_event_box_new()); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1670 // We set the owner last of all so that we can determine whether we are in | 1669 // We set the owner last of all so that we can determine whether we are in |
| 1671 // the process of initializing this class or not. | 1670 // the process of initializing this class or not. |
| 1672 owner_ = owner; | 1671 owner_ = owner; |
| 1673 } | 1672 } |
| 1674 | 1673 |
| 1675 LocationBarViewGtk::PageActionViewGtk::~PageActionViewGtk() { | 1674 LocationBarViewGtk::PageActionViewGtk::~PageActionViewGtk() { |
| 1676 DisconnectPageActionAccelerator(); | 1675 DisconnectPageActionAccelerator(); |
| 1677 | 1676 |
| 1678 image_.Destroy(); | 1677 image_.Destroy(); |
| 1679 event_box_.Destroy(); | 1678 event_box_.Destroy(); |
| 1680 for (PixbufMap::iterator iter = pixbufs_.begin(); iter != pixbufs_.end(); | |
| 1681 ++iter) { | |
| 1682 g_object_unref(iter->second); | |
| 1683 } | |
| 1684 if (last_icon_pixbuf_) | |
| 1685 g_object_unref(last_icon_pixbuf_); | |
| 1686 } | 1679 } |
| 1687 | 1680 |
| 1688 bool LocationBarViewGtk::PageActionViewGtk::IsVisible() { | 1681 bool LocationBarViewGtk::PageActionViewGtk::IsVisible() { |
| 1689 return gtk_widget_get_visible(widget()); | 1682 return gtk_widget_get_visible(widget()); |
| 1690 } | 1683 } |
| 1691 | 1684 |
| 1692 void LocationBarViewGtk::PageActionViewGtk::UpdateVisibility( | 1685 void LocationBarViewGtk::PageActionViewGtk::UpdateVisibility( |
| 1693 WebContents* contents, const GURL& url) { | 1686 WebContents* contents, const GURL& url) { |
| 1694 // Save this off so we can pass it back to the extension when the action gets | 1687 // Save this off so we can pass it back to the extension when the action gets |
| 1695 // executed. See PageActionImageView::OnMousePressed. | 1688 // executed. See PageActionImageView::OnMousePressed. |
| 1696 current_tab_id_ = contents ? ExtensionTabUtil::GetTabId(contents) : -1; | 1689 current_tab_id_ = contents ? ExtensionTabUtil::GetTabId(contents) : -1; |
| 1697 current_url_ = url; | 1690 current_url_ = url; |
| 1698 | 1691 |
| 1699 bool visible = contents && | 1692 bool visible = contents && |
| 1700 (preview_enabled_ || page_action_->GetIsVisible(current_tab_id_)); | 1693 (preview_enabled_ || page_action_->GetIsVisible(current_tab_id_)); |
| 1701 if (visible) { | 1694 if (visible) { |
| 1702 // Set the tooltip. | 1695 // Set the tooltip. |
| 1703 gtk_widget_set_tooltip_text(event_box_.get(), | 1696 gtk_widget_set_tooltip_text(event_box_.get(), |
| 1704 page_action_->GetTitle(current_tab_id_).c_str()); | 1697 page_action_->GetTitle(current_tab_id_).c_str()); |
| 1705 | 1698 |
| 1706 // Set the image. | 1699 // Set the image. |
| 1707 // It can come from three places. In descending order of priority: | 1700 gfx::Image icon = page_action_->GetIcon(current_tab_id_, loaded_icons_); |
| 1708 // - The developer can set it dynamically by path or bitmap. It will be in | 1701 if (!icon.IsEmpty()) { |
| 1709 // page_action_->GetIcon(). | 1702 GdkPixbuf* pixbuf = icon.ToGdkPixbuf(); |
| 1710 // - The developer can set it dyanmically by index. It will be in | 1703 DCHECK(pixbuf); |
| 1711 // page_action_->GetIconIndex(). | 1704 gtk_image_set_from_pixbuf(GTK_IMAGE(image_.get()), pixbuf); |
| 1712 // - It can be set in the manifest by path. It will be in page_action_-> | |
| 1713 // default_icon_path(). | |
| 1714 | |
| 1715 // First look for a dynamically set bitmap. | |
| 1716 SkBitmap icon = page_action_->GetIcon(current_tab_id_); | |
| 1717 GdkPixbuf* pixbuf = NULL; | |
| 1718 if (!icon.isNull()) { | |
| 1719 if (icon.pixelRef() != last_icon_skbitmap_.pixelRef()) { | |
| 1720 if (last_icon_pixbuf_) | |
| 1721 g_object_unref(last_icon_pixbuf_); | |
| 1722 last_icon_skbitmap_ = icon; | |
| 1723 last_icon_pixbuf_ = gfx::GdkPixbufFromSkBitmap(icon); | |
| 1724 } | |
| 1725 DCHECK(last_icon_pixbuf_); | |
| 1726 pixbuf = last_icon_pixbuf_; | |
| 1727 } else { | |
| 1728 // Otherwise look for a dynamically set index, or fall back to the | |
| 1729 // default path. | |
| 1730 int icon_index = page_action_->GetIconIndex(current_tab_id_); | |
| 1731 std::string icon_path = (icon_index < 0) ? | |
| 1732 page_action_->default_icon_path() : | |
| 1733 page_action_->icon_paths()->at(icon_index); | |
| 1734 if (!icon_path.empty()) { | |
| 1735 PixbufMap::iterator iter = pixbufs_.find(icon_path); | |
| 1736 if (iter != pixbufs_.end()) | |
| 1737 pixbuf = iter->second; | |
| 1738 } | |
| 1739 } | |
| 1740 // The pixbuf might not be loaded yet. | |
| 1741 if (pixbuf) { | |
| 1742 const ExtensionAction::IconAnimation* icon_animation = | |
| 1743 scoped_icon_animation_observer_.icon_animation(); | |
| 1744 if (icon_animation) { | |
| 1745 // Draw |pixbuf| with the fade-in |icon_animation_| applied to it. | |
| 1746 // Use a temporary gfx::Image to do the conversion to/from a SkBitmap. | |
| 1747 g_object_ref(pixbuf); // don't let gfx::Image take ownership. | |
| 1748 gfx::Image animated_image( | |
| 1749 icon_animation->Apply(*gfx::Image(pixbuf).ToSkBitmap())); | |
| 1750 gtk_image_set_from_pixbuf(GTK_IMAGE(image_.get()), | |
| 1751 animated_image.ToGdkPixbuf()); | |
| 1752 } else { | |
| 1753 gtk_image_set_from_pixbuf(GTK_IMAGE(image_.get()), pixbuf); | |
| 1754 } | |
| 1755 } | 1705 } |
| 1756 } | 1706 } |
| 1757 | 1707 |
| 1758 bool old_visible = IsVisible(); | 1708 bool old_visible = IsVisible(); |
| 1759 if (visible) | 1709 if (visible) |
| 1760 gtk_widget_show_all(event_box_.get()); | 1710 gtk_widget_show_all(event_box_.get()); |
| 1761 else | 1711 else |
| 1762 gtk_widget_hide_all(event_box_.get()); | 1712 gtk_widget_hide_all(event_box_.get()); |
| 1763 | 1713 |
| 1764 if (visible != old_visible) { | 1714 if (visible != old_visible) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 1776 // We loaded icons()->size() icons, plus one extra if the page action had | 1726 // We loaded icons()->size() icons, plus one extra if the page action had |
| 1777 // a default icon. | 1727 // a default icon. |
| 1778 int total_icons = static_cast<int>(page_action_->icon_paths()->size()); | 1728 int total_icons = static_cast<int>(page_action_->icon_paths()->size()); |
| 1779 if (!page_action_->default_icon_path().empty()) | 1729 if (!page_action_->default_icon_path().empty()) |
| 1780 total_icons++; | 1730 total_icons++; |
| 1781 DCHECK(index < total_icons); | 1731 DCHECK(index < total_icons); |
| 1782 | 1732 |
| 1783 // Map the index of the loaded image back to its name. If we ever get an | 1733 // Map the index of the loaded image back to its name. If we ever get an |
| 1784 // index greater than the number of icons, it must be the default icon. | 1734 // index greater than the number of icons, it must be the default icon. |
| 1785 if (!image.IsEmpty()) { | 1735 if (!image.IsEmpty()) { |
| 1786 GdkPixbuf* pixbuf = | |
| 1787 static_cast<GdkPixbuf*>(g_object_ref(image.ToGdkPixbuf())); | |
| 1788 if (index < static_cast<int>(page_action_->icon_paths()->size())) | 1736 if (index < static_cast<int>(page_action_->icon_paths()->size())) |
| 1789 pixbufs_[page_action_->icon_paths()->at(index)] = pixbuf; | 1737 loaded_icons_[page_action_->icon_paths()->at(index)] = image; |
| 1790 else | 1738 else |
| 1791 pixbufs_[page_action_->default_icon_path()] = pixbuf; | 1739 loaded_icons_[page_action_->default_icon_path()] = image; |
| 1792 } | 1740 } |
| 1793 | 1741 |
| 1794 // If we have no owner, that means this class is still being constructed. | 1742 // If we have no owner, that means this class is still being constructed. |
| 1795 TabContents* tab_contents = owner_ ? owner_->GetTabContents() : NULL; | 1743 TabContents* tab_contents = owner_ ? owner_->GetTabContents() : NULL; |
| 1796 if (tab_contents) | 1744 if (tab_contents) |
| 1797 UpdateVisibility(tab_contents->web_contents(), current_url_); | 1745 UpdateVisibility(tab_contents->web_contents(), current_url_); |
| 1798 } | 1746 } |
| 1799 | 1747 |
| 1800 void LocationBarViewGtk::PageActionViewGtk::TestActivatePageAction() { | 1748 void LocationBarViewGtk::PageActionViewGtk::TestActivatePageAction() { |
| 1801 GdkEventButton event = {}; | 1749 GdkEventButton event = {}; |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1972 } | 1920 } |
| 1973 | 1921 |
| 1974 void LocationBarViewGtk::PageActionViewGtk::InspectPopup( | 1922 void LocationBarViewGtk::PageActionViewGtk::InspectPopup( |
| 1975 ExtensionAction* action) { | 1923 ExtensionAction* action) { |
| 1976 ExtensionPopupGtk::Show( | 1924 ExtensionPopupGtk::Show( |
| 1977 action->GetPopupUrl(current_tab_id_), | 1925 action->GetPopupUrl(current_tab_id_), |
| 1978 owner_->browser_, | 1926 owner_->browser_, |
| 1979 event_box_.get(), | 1927 event_box_.get(), |
| 1980 ExtensionPopupGtk::SHOW_AND_INSPECT); | 1928 ExtensionPopupGtk::SHOW_AND_INSPECT); |
| 1981 } | 1929 } |
| OLD | NEW |