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/first_run_bubble.h"
6
7#include "chrome/browser/first_run/first_run.h"
8#include "chrome/browser/search_engines/template_url_service_factory.h"
9#include "chrome/browser/ui/browser.h"
10#include "chrome/browser/ui/chrome_pages.h"
11#include "chrome/grit/generated_resources.h"
12#include "components/search_engines/util.h"
13#include "ui/base/l10n/l10n_util.h"
14#include "ui/base/resource/resource_bundle.h"
15#include "ui/views/controls/label.h"
16#include "ui/views/controls/link.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
21namespace {
22const int kAnchorVerticalInset = 5;
23const int kTopInset = 1;
24const int kLeftInset = 2;
25const int kBottomInset = 7;
26const int kRightInset = 2;
27}  // namespace
28
29// static
30FirstRunBubble* FirstRunBubble::ShowBubble(Browser* browser,
31                                           views::View* anchor_view) {
32  first_run::LogFirstRunMetric(first_run::FIRST_RUN_BUBBLE_SHOWN);
33
34  FirstRunBubble* delegate = new FirstRunBubble(browser, anchor_view);
35  views::BubbleDelegateView::CreateBubble(delegate)->Show();
36  return delegate;
37}
38
39void FirstRunBubble::Init() {
40  ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
41  const gfx::FontList& original_font_list =
42      rb.GetFontList(ui::ResourceBundle::MediumFont);
43
44  const base::string16 search_engine_name = browser_ ?
45      GetDefaultSearchEngineName(
46          TemplateURLServiceFactory::GetForProfile(browser_->profile())) :
47      base::string16();
48  views::Label* title = new views::Label(
49      l10n_util::GetStringFUTF16(IDS_FR_BUBBLE_TITLE, search_engine_name),
50      original_font_list.Derive(2, gfx::Font::BOLD));
51
52  views::Link* change =
53      new views::Link(l10n_util::GetStringUTF16(IDS_FR_BUBBLE_CHANGE));
54  change->SetFontList(original_font_list);
55  change->set_listener(this);
56
57  views::Label* subtext =
58      new views::Label(l10n_util::GetStringUTF16(IDS_FR_BUBBLE_SUBTEXT),
59                       original_font_list);
60
61  views::GridLayout* layout = views::GridLayout::CreatePanel(this);
62  SetLayoutManager(layout);
63  layout->SetInsets(kTopInset, kLeftInset, kBottomInset, kRightInset);
64
65  views::ColumnSet* columns = layout->AddColumnSet(0);
66  columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::LEADING, 0,
67                     views::GridLayout::USE_PREF, 0, 0);
68  columns->AddPaddingColumn(0, views::kRelatedControlHorizontalSpacing);
69  columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::LEADING, 0,
70                     views::GridLayout::USE_PREF, 0, 0);
71  columns->AddPaddingColumn(1, 0);
72
73  layout->StartRow(0, 0);
74  layout->AddView(title);
75  layout->AddView(change);
76  layout->StartRowWithPadding(0, 0, 0,
77      views::kRelatedControlSmallVerticalSpacing);
78  layout->AddView(subtext, columns->num_columns(), 1);
79}
80
81FirstRunBubble::FirstRunBubble(Browser* browser, views::View* anchor_view)
82    : views::BubbleDelegateView(anchor_view, views::BubbleBorder::TOP_LEFT),
83      browser_(browser) {
84  // Compensate for built-in vertical padding in the anchor view's image.
85  set_anchor_view_insets(
86      gfx::Insets(kAnchorVerticalInset, 0, kAnchorVerticalInset, 0));
87}
88
89FirstRunBubble::~FirstRunBubble() {
90}
91
92void FirstRunBubble::LinkClicked(views::Link* source, int event_flags) {
93  first_run::LogFirstRunMetric(first_run::FIRST_RUN_BUBBLE_CHANGE_INVOKED);
94
95  GetWidget()->Close();
96  if (browser_)
97    chrome::ShowSearchEngineSettings(browser_);
98}
99