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