OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/ui/views/home_button.h" |
| 6 |
| 7 #include "chrome/browser/prefs/pref_service.h" |
| 8 #include "chrome/browser/profiles/profile.h" |
| 9 #include "chrome/browser/ui/browser.h" |
| 10 #include "chrome/common/pref_names.h" |
| 11 #include "grit/generated_resources.h" |
| 12 #include "ui/base/l10n/l10n_util.h" |
| 13 #include "ui/views/bubble/bubble_delegate.h" |
| 14 #include "ui/views/controls/label.h" |
| 15 #include "ui/views/controls/link.h" |
| 16 #include "ui/views/controls/link_listener.h" |
| 17 #include "ui/views/layout/grid_layout.h" |
| 18 #include "ui/views/layout/layout_constants.h" |
| 19 #include "ui/views/widget/widget.h" |
| 20 |
| 21 // HomePageUndoBubble -------------------------------------------------------- |
| 22 |
| 23 namespace { |
| 24 |
| 25 class HomePageUndoBubble : public views::BubbleDelegateView, |
| 26 public views::LinkListener { |
| 27 public: |
| 28 static void ShowBubble(Browser* browser, |
| 29 bool undo_value_is_ntp, |
| 30 const GURL& undo_url, |
| 31 views::View* anchor_view); |
| 32 static void HideBubble(); |
| 33 |
| 34 private: |
| 35 HomePageUndoBubble(Browser* browser, bool undo_value_is_ntp, |
| 36 const GURL& undo_url, views::View* anchor_view); |
| 37 virtual ~HomePageUndoBubble(); |
| 38 |
| 39 // views::BubbleDelegateView: |
| 40 virtual void Init() OVERRIDE; |
| 41 virtual void WindowClosing() OVERRIDE; |
| 42 |
| 43 // views::LinkListener: |
| 44 virtual void LinkClicked(views::Link* source, int event_flags) OVERRIDE; |
| 45 |
| 46 static HomePageUndoBubble* home_page_undo_bubble_; |
| 47 |
| 48 Browser* browser_; |
| 49 bool undo_value_is_ntp_; |
| 50 GURL undo_url_; |
| 51 |
| 52 DISALLOW_COPY_AND_ASSIGN(HomePageUndoBubble); |
| 53 }; |
| 54 |
| 55 // static |
| 56 HomePageUndoBubble* HomePageUndoBubble::home_page_undo_bubble_ = NULL; |
| 57 |
| 58 void HomePageUndoBubble::ShowBubble(Browser* browser, |
| 59 bool undo_value_is_ntp, |
| 60 const GURL& undo_url, |
| 61 views::View* anchor_view) { |
| 62 HideBubble(); |
| 63 home_page_undo_bubble_ = new HomePageUndoBubble(browser, |
| 64 undo_value_is_ntp, |
| 65 undo_url, |
| 66 anchor_view); |
| 67 views::BubbleDelegateView::CreateBubble(home_page_undo_bubble_); |
| 68 home_page_undo_bubble_->StartFade(true); |
| 69 } |
| 70 |
| 71 void HomePageUndoBubble::HideBubble() { |
| 72 if (home_page_undo_bubble_) |
| 73 home_page_undo_bubble_->GetWidget()->Close(); |
| 74 } |
| 75 |
| 76 HomePageUndoBubble::HomePageUndoBubble( |
| 77 Browser* browser, |
| 78 bool undo_value_is_ntp, |
| 79 const GURL& undo_url, |
| 80 views::View* anchor_view) |
| 81 : BubbleDelegateView(anchor_view, views::BubbleBorder::TOP_LEFT), |
| 82 browser_(browser), |
| 83 undo_value_is_ntp_(undo_value_is_ntp), |
| 84 undo_url_(undo_url) { |
| 85 } |
| 86 |
| 87 HomePageUndoBubble::~HomePageUndoBubble() { |
| 88 } |
| 89 |
| 90 void HomePageUndoBubble::Init() { |
| 91 views::GridLayout* layout = new views::GridLayout(this); |
| 92 SetLayoutManager(layout); |
| 93 |
| 94 // Create two columns for the message and the undo link. |
| 95 views::ColumnSet* cs = layout->AddColumnSet(0); |
| 96 cs = layout->AddColumnSet(1); |
| 97 cs->AddColumn(views::GridLayout::LEADING, views::GridLayout::BASELINE, 0, |
| 98 views::GridLayout::USE_PREF, 0, 0); |
| 99 cs->AddPaddingColumn(0, views::kRelatedControlHorizontalSpacing); |
| 100 cs->AddColumn(views::GridLayout::CENTER, views::GridLayout::BASELINE, 0, |
| 101 views::GridLayout::USE_PREF, 0, 0); |
| 102 |
| 103 views::Label* message_label = new views::Label( |
| 104 l10n_util::GetStringUTF16(IDS_TOOLBAR_INFORM_SET_HOME_PAGE)); |
| 105 message_label->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 106 layout->StartRow(0, 1); |
| 107 layout->AddView(message_label); |
| 108 |
| 109 views::Link* undo_link = new views::Link( |
| 110 l10n_util::GetStringUTF16(IDS_ONE_CLICK_BUBBLE_UNDO)); |
| 111 undo_link->set_listener(this); |
| 112 layout->AddView(undo_link); |
| 113 } |
| 114 |
| 115 void HomePageUndoBubble::LinkClicked(views::Link* source, int event_flags) { |
| 116 PrefServiceBase* prefs = |
| 117 PrefServiceBase::FromBrowserContext(browser_->profile()); |
| 118 prefs->SetBoolean(prefs::kHomePageIsNewTabPage, undo_value_is_ntp_); |
| 119 prefs->SetString(prefs::kHomePage, undo_url_.spec()); |
| 120 |
| 121 HideBubble(); |
| 122 } |
| 123 |
| 124 void HomePageUndoBubble::WindowClosing() { |
| 125 // We have to reset |home_page_undo_bubble_| here, not in our destructor, |
| 126 // because we'll be hidden first, then destroyed asynchronously. If we wait |
| 127 // to reset this, and the user triggers a call to ShowBubble() while the |
| 128 // window is hidden but not destroyed, GetWidget()->Close() would be |
| 129 // called twice. |
| 130 DCHECK_EQ(this, home_page_undo_bubble_); |
| 131 home_page_undo_bubble_ = NULL; |
| 132 } |
| 133 |
| 134 } // namespace |
| 135 |
| 136 |
| 137 // HomeImageButton ----------------------------------------------------------- |
| 138 |
| 139 HomeImageButton::HomeImageButton( |
| 140 views::ButtonListener* listener, |
| 141 Browser* browser) |
| 142 : views::ImageButton(listener), |
| 143 browser_(browser) { |
| 144 } |
| 145 |
| 146 HomeImageButton::~HomeImageButton() { |
| 147 } |
| 148 |
| 149 bool HomeImageButton::GetDropFormats( |
| 150 int* formats, |
| 151 std::set<OSExchangeData::CustomFormat>* custom_formats) { |
| 152 *formats = ui::OSExchangeData::URL; |
| 153 return true; |
| 154 } |
| 155 |
| 156 bool HomeImageButton::CanDrop(const OSExchangeData& data) { |
| 157 return data.HasURL(); |
| 158 } |
| 159 |
| 160 int HomeImageButton::OnDragUpdated(const ui::DropTargetEvent& event) { |
| 161 return (event.source_operations() & ui::DragDropTypes::DRAG_LINK) ? |
| 162 ui::DragDropTypes::DRAG_LINK : ui::DragDropTypes::DRAG_NONE; |
| 163 } |
| 164 |
| 165 int HomeImageButton::OnPerformDrop(const ui::DropTargetEvent& event) { |
| 166 GURL new_homepage_url; |
| 167 string16 title; |
| 168 if (event.data().GetURLAndTitle(&new_homepage_url, &title) && |
| 169 new_homepage_url.is_valid()) { |
| 170 PrefService* prefs = browser_->profile()->GetPrefs(); |
| 171 bool old_is_ntp = prefs->GetBoolean(prefs::kHomePageIsNewTabPage); |
| 172 GURL old_homepage(prefs->GetString(prefs::kHomePage)); |
| 173 |
| 174 prefs->SetBoolean(prefs::kHomePageIsNewTabPage, false); |
| 175 prefs->SetString(prefs::kHomePage, new_homepage_url.spec()); |
| 176 |
| 177 HomePageUndoBubble::ShowBubble(browser_, old_is_ntp, old_homepage, this); |
| 178 } |
| 179 return ui::DragDropTypes::DRAG_NONE; |
| 180 } |
OLD | NEW |