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