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.
5 #include "chrome/browser/first_run/try_chrome_dialog_view.h"
9 #include "base/logging.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/strings/string16.h"
12 #include "chrome/browser/process_singleton.h"
13 #include "chrome/installer/util/browser_distribution.h"
14 #include "chrome/installer/util/user_experiment.h"
15 #include "grit/chromium_strings.h"
16 #include "grit/generated_resources.h"
17 #include "grit/theme_resources.h"
18 #include "grit/ui_resources.h"
19 #include "ui/base/l10n/l10n_util.h"
20 #include "ui/base/resource/resource_bundle.h"
21 #include "ui/gfx/image/image.h"
22 #include "ui/views/background.h"
23 #include "ui/views/controls/button/checkbox.h"
24 #include "ui/views/controls/button/image_button.h"
25 #include "ui/views/controls/button/label_button.h"
26 #include "ui/views/controls/button/radio_button.h"
27 #include "ui/views/controls/image_view.h"
28 #include "ui/views/controls/link.h"
29 #include "ui/views/controls/separator.h"
30 #include "ui/views/layout/grid_layout.h"
31 #include "ui/views/layout/layout_constants.h"
32 #include "ui/views/widget/widget.h"
35 #include "ui/aura/root_window.h"
36 #include "ui/aura/window.h"
41 const wchar_t kHelpCenterUrl
[] =
42 L
"https://www.google.com/support/chrome/bin/answer.py?answer=150752";
52 const int kRadioGroupID
= 1;
57 TryChromeDialogView::Result
TryChromeDialogView::Show(
59 const ActiveModalDialogListener
& listener
) {
61 // This is a test value. We want to make sure we exercise
62 // returning this early. See TryChromeDialogBrowserTest test.
65 TryChromeDialogView
dialog(flavor
);
66 return dialog
.ShowModal(listener
);
69 TryChromeDialogView::TryChromeDialogView(size_t flavor
)
74 dont_try_chrome_(NULL
),
79 TryChromeDialogView::~TryChromeDialogView() {
82 TryChromeDialogView::Result
TryChromeDialogView::ShowModal(
83 const ActiveModalDialogListener
& listener
) {
84 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
86 views::ImageView
* icon
= new views::ImageView();
87 icon
->SetImage(rb
.GetNativeImageNamed(IDR_PRODUCT_LOGO_32
).ToImageSkia());
88 gfx::Size icon_size
= icon
->GetPreferredSize();
90 // An approximate window size. After Layout() we'll get better bounds.
91 views::Widget::InitParams
params(views::Widget::InitParams::TYPE_POPUP
);
92 params
.can_activate
= true;
93 params
.bounds
= gfx::Rect(310, 200);
94 popup_
= new views::Widget
;
97 views::View
* root_view
= popup_
->GetRootView();
98 // The window color is a tiny bit off-white.
99 root_view
->set_background(
100 views::Background::CreateSolidBackground(0xfc, 0xfc, 0xfc));
102 views::GridLayout
* layout
= views::GridLayout::CreatePanel(root_view
);
103 root_view
->SetLayoutManager(layout
);
104 views::ColumnSet
* columns
;
106 // First row: [icon][pad][text][pad][button].
107 columns
= layout
->AddColumnSet(0);
108 columns
->AddColumn(views::GridLayout::LEADING
, views::GridLayout::LEADING
, 0,
109 views::GridLayout::FIXED
, icon_size
.width(),
111 columns
->AddPaddingColumn(0, views::kRelatedControlHorizontalSpacing
);
112 columns
->AddColumn(views::GridLayout::FILL
, views::GridLayout::FILL
, 1,
113 views::GridLayout::USE_PREF
, 0, 0);
114 columns
->AddPaddingColumn(0, views::kUnrelatedControlHorizontalSpacing
);
115 columns
->AddColumn(views::GridLayout::TRAILING
, views::GridLayout::FILL
, 1,
116 views::GridLayout::USE_PREF
, 0, 0);
118 // Optional second row: [pad][pad][radio 1].
119 columns
= layout
->AddColumnSet(1);
120 columns
->AddPaddingColumn(0, icon_size
.width());
121 columns
->AddPaddingColumn(0, views::kRelatedControlHorizontalSpacing
);
122 columns
->AddColumn(views::GridLayout::LEADING
, views::GridLayout::FILL
, 1,
123 views::GridLayout::USE_PREF
, 0, 0);
125 // Third row: [pad][pad][radio 2].
126 columns
= layout
->AddColumnSet(2);
127 columns
->AddPaddingColumn(0, icon_size
.width());
128 columns
->AddPaddingColumn(0, views::kRelatedControlHorizontalSpacing
);
129 columns
->AddColumn(views::GridLayout::LEADING
, views::GridLayout::FILL
, 1,
130 views::GridLayout::USE_PREF
, 0, 0);
132 // Fourth row: [pad][pad][button][pad][button].
133 columns
= layout
->AddColumnSet(3);
134 columns
->AddPaddingColumn(0, icon_size
.width());
135 columns
->AddColumn(views::GridLayout::LEADING
, views::GridLayout::FILL
, 0,
136 views::GridLayout::USE_PREF
, 0, 0);
137 columns
->AddPaddingColumn(0, views::kRelatedButtonHSpacing
);
138 columns
->AddColumn(views::GridLayout::LEADING
, views::GridLayout::FILL
, 0,
139 views::GridLayout::USE_PREF
, 0, 0);
141 // Fifth row: [pad][pad][link].
142 columns
= layout
->AddColumnSet(4);
143 columns
->AddPaddingColumn(0, icon_size
.width());
144 columns
->AddPaddingColumn(0, views::kRelatedControlHorizontalSpacing
);
145 columns
->AddColumn(views::GridLayout::LEADING
, views::GridLayout::FILL
, 1,
146 views::GridLayout::USE_PREF
, 0, 0);
148 // Optional fourth row: [button].
149 columns
= layout
->AddColumnSet(5);
150 columns
->AddColumn(views::GridLayout::CENTER
, views::GridLayout::FILL
, 1,
151 views::GridLayout::USE_PREF
, 0, 0);
153 // Optional fourth row: [divider]
154 columns
= layout
->AddColumnSet(6);
155 columns
->AddColumn(views::GridLayout::CENTER
, views::GridLayout::FILL
, 1,
156 views::GridLayout::USE_PREF
, 0, 0);
158 // Optional fifth row [checkbox][pad][button]
159 columns
= layout
->AddColumnSet(7);
160 columns
->AddColumn(views::GridLayout::LEADING
, views::GridLayout::FILL
, 0,
161 views::GridLayout::USE_PREF
, 0, 0);
162 columns
->AddPaddingColumn(0, views::kUnrelatedControlHorizontalSpacing
);
163 columns
->AddColumn(views::GridLayout::TRAILING
, views::GridLayout::FILL
, 1,
164 views::GridLayout::USE_PREF
, 0, 0);
167 layout
->StartRow(0, 0);
168 layout
->AddView(icon
);
170 // Find out what experiment we are conducting.
171 installer::ExperimentDetails experiment
;
172 if (!BrowserDistribution::GetDistribution()->HasUserExperiments() ||
173 !installer::CreateExperimentDetails(flavor_
, &experiment
) ||
174 !experiment
.heading
) {
175 NOTREACHED() << "Cannot determine which headline to show.";
178 views::Label
* label
= new views::Label(
179 l10n_util::GetStringUTF16(experiment
.heading
),
180 rb
.GetFontList(ui::ResourceBundle::MediumFont
));
181 label
->SetMultiLine(true);
182 label
->SizeToFit(200);
183 label
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
184 layout
->AddView(label
);
185 // The close button is custom.
186 views::ImageButton
* close_button
= new views::ImageButton(this);
187 close_button
->SetImage(views::CustomButton::STATE_NORMAL
,
188 rb
.GetNativeImageNamed(IDR_CLOSE_2
).ToImageSkia());
189 close_button
->SetImage(views::CustomButton::STATE_HOVERED
,
190 rb
.GetNativeImageNamed(IDR_CLOSE_2_H
).ToImageSkia());
191 close_button
->SetImage(views::CustomButton::STATE_PRESSED
,
192 rb
.GetNativeImageNamed(IDR_CLOSE_2_P
).ToImageSkia());
193 close_button
->set_tag(BT_CLOSE_BUTTON
);
194 layout
->AddView(close_button
);
197 layout
->StartRowWithPadding(0, 1, 0, 10);
198 try_chrome_
= new views::RadioButton(
199 l10n_util::GetStringUTF16(IDS_TRY_TOAST_TRY_OPT
), kRadioGroupID
);
200 try_chrome_
->SetChecked(true);
201 try_chrome_
->set_tag(BT_TRY_IT_RADIO
);
202 try_chrome_
->set_listener(this);
203 layout
->AddView(try_chrome_
);
205 // Decide if the don't bug me is a button or a radio button.
206 bool dont_bug_me_button
=
207 !!(experiment
.flags
& installer::kToastUiDontBugMeAsButton
);
209 // Optional third and fourth row.
210 if (!dont_bug_me_button
) {
211 layout
->StartRow(0, 1);
212 dont_try_chrome_
= new views::RadioButton(
213 l10n_util::GetStringUTF16(IDS_TRY_TOAST_CANCEL
), kRadioGroupID
);
214 dont_try_chrome_
->set_tag(BT_DONT_BUG_RADIO
);
215 dont_try_chrome_
->set_listener(this);
216 layout
->AddView(dont_try_chrome_
);
218 if (experiment
.flags
& installer::kToastUiUninstall
) {
219 layout
->StartRow(0, 2);
220 kill_chrome_
= new views::RadioButton(
221 l10n_util::GetStringUTF16(IDS_UNINSTALL_CHROME
), kRadioGroupID
);
222 layout
->AddView(kill_chrome_
);
225 views::LabelButton
* accept_button
= new views::LabelButton(
226 this, l10n_util::GetStringUTF16(IDS_OK
));
227 accept_button
->SetStyle(views::Button::STYLE_BUTTON
);
228 accept_button
->set_tag(BT_OK_BUTTON
);
230 views::Separator
* separator
= NULL
;
231 if (experiment
.flags
& installer::kToastUiMakeDefault
) {
232 // In this flavor we have some veritical space, then a separator line
233 // and the 'make default' checkbox and the OK button on the same row.
234 layout
->AddPaddingRow(0, views::kUnrelatedControlVerticalSpacing
);
235 layout
->StartRow(0, 6);
236 separator
= new views::Separator(views::Separator::HORIZONTAL
);
237 layout
->AddView(separator
);
238 layout
->AddPaddingRow(0, views::kUnrelatedControlVerticalSpacing
);
240 layout
->StartRow(0, 7);
241 make_default_
= new views::Checkbox(
242 l10n_util::GetStringUTF16(IDS_TRY_TOAST_SET_DEFAULT
));
243 make_default_
->SetChecked(true);
244 layout
->AddView(make_default_
);
245 layout
->AddView(accept_button
);
247 // On this other flavor there is no checkbox, the OK button and possibly
248 // the cancel button are in the same row.
249 layout
->StartRowWithPadding(0, dont_bug_me_button
? 3 : 5, 0, 10);
250 layout
->AddView(accept_button
);
251 if (dont_bug_me_button
) {
252 // The dialog needs a "Don't bug me" as a button or as a radio button,
253 // this the button case.
254 views::LabelButton
* cancel_button
= new views::LabelButton(
255 this, l10n_util::GetStringUTF16(IDS_TRY_TOAST_CANCEL
));
256 cancel_button
->SetStyle(views::Button::STYLE_BUTTON
);
257 cancel_button
->set_tag(BT_CLOSE_BUTTON
);
258 layout
->AddView(cancel_button
);
262 if (experiment
.flags
& installer::kToastUiWhyLink
) {
263 layout
->StartRowWithPadding(0, 4, 0, 10);
264 views::Link
* link
= new views::Link(
265 l10n_util::GetStringUTF16(IDS_TRY_TOAST_WHY
));
266 link
->set_listener(this);
267 layout
->AddView(link
);
270 // We resize the window according to the layout manager. This takes into
271 // account the differences between XP and Vista fonts and buttons.
272 layout
->Layout(root_view
);
273 gfx::Size preferred
= layout
->GetPreferredSize(root_view
);
275 int separator_height
= separator
->GetPreferredSize().height();
276 separator
->SetSize(gfx::Size(preferred
.width(), separator_height
));
279 gfx::Rect pos
= ComputeWindowPosition(preferred
.width(), preferred
.height(),
280 base::i18n::IsRTL());
281 popup_
->SetBounds(pos
);
283 // Carve the toast shape into the window.
285 #if defined(USE_AURA)
287 popup_
->GetNativeView()->GetDispatcher()->host()->GetAcceleratedWidget();
289 toast_window
= popup_
->GetNativeView();
291 SetToastRegion(toast_window
, preferred
.width(), preferred
.height());
293 // Time to show the window in a modal loop.
295 if (!listener
.is_null())
296 listener
.Run(popup_
->GetNativeView());
297 base::MessageLoop::current()->Run();
298 if (!listener
.is_null())
303 gfx::Rect
TryChromeDialogView::ComputeWindowPosition(int width
,
306 // The 'Shell_TrayWnd' is the taskbar. We like to show our window in that
307 // monitor if we can. This code works even if such window is not found.
308 HWND taskbar
= ::FindWindowW(L
"Shell_TrayWnd", NULL
);
309 HMONITOR monitor
= ::MonitorFromWindow(taskbar
, MONITOR_DEFAULTTOPRIMARY
);
310 MONITORINFO info
= {sizeof(info
)};
311 if (!GetMonitorInfoW(monitor
, &info
)) {
312 // Quite unexpected. Do a best guess at a visible rectangle.
313 return gfx::Rect(20, 20, width
+ 20, height
+ 20);
315 // The |rcWork| is the work area. It should account for the taskbars that
316 // are in the screen when we called the function.
317 int left
= is_RTL
? info
.rcWork
.left
: info
.rcWork
.right
- width
;
318 int top
= info
.rcWork
.bottom
- height
;
319 return gfx::Rect(left
, top
, width
, height
);
322 void TryChromeDialogView::SetToastRegion(HWND window
, int w
, int h
) {
323 static const POINT polygon
[] = {
324 {0, 4}, {1, 2}, {2, 1}, {4, 0}, // Left side.
325 {w
-4, 0}, {w
-2, 1}, {w
-1, 2}, {w
, 4}, // Right side.
328 HRGN region
= ::CreatePolygonRgn(polygon
, arraysize(polygon
), WINDING
);
329 ::SetWindowRgn(window
, region
, FALSE
);
332 void TryChromeDialogView::ButtonPressed(views::Button
* sender
,
333 const ui::Event
& event
) {
334 if (sender
->tag() == BT_DONT_BUG_RADIO
) {
336 make_default_
->SetChecked(false);
337 make_default_
->SetVisible(false);
340 } else if (sender
->tag() == BT_TRY_IT_RADIO
) {
342 make_default_
->SetVisible(true);
343 make_default_
->SetChecked(true);
346 } else if (sender
->tag() == BT_CLOSE_BUTTON
) {
347 // The user pressed cancel or the [x] button.
349 } else if (!try_chrome_
) {
350 // We don't have radio buttons, the user pressed ok.
351 result_
= TRY_CHROME
;
353 // The outcome is according to the selected radio button.
354 if (try_chrome_
->checked())
355 result_
= TRY_CHROME
;
356 else if (dont_try_chrome_
&& dont_try_chrome_
->checked())
358 else if (kill_chrome_
&& kill_chrome_
->checked())
359 result_
= UNINSTALL_CHROME
;
361 NOTREACHED() << "Unknown radio button selected";
365 if ((result_
== TRY_CHROME
) && make_default_
->checked())
366 result_
= TRY_CHROME_AS_DEFAULT
;
370 base::MessageLoop::current()->Quit();
373 void TryChromeDialogView::LinkClicked(views::Link
* source
, int event_flags
) {
374 ::ShellExecuteW(NULL
, L
"open", kHelpCenterUrl
, NULL
, NULL
, SW_SHOW
);