Home | History | Annotate | Download | only in bookmarks
      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/bookmarks/bookmark_prompt_view.h"
      6 
      7 #include "chrome/browser/ui/bookmarks/bookmark_prompt_controller.h"
      8 #include "grit/generated_resources.h"
      9 #include "ui/base/l10n/l10n_util.h"
     10 #include "ui/base/resource/resource_bundle.h"
     11 #include "ui/views/controls/label.h"
     12 #include "ui/views/controls/link.h"
     13 #include "ui/views/layout/box_layout.h"
     14 #include "ui/views/layout/layout_constants.h"
     15 #include "ui/views/widget/widget.h"
     16 
     17 namespace {
     18 
     19 // Horizontal padding of bookmark prompt.
     20 const int kHorizontalPadding = 20;
     21 
     22 }
     23 
     24 // static
     25 BookmarkPromptView* BookmarkPromptView::bookmark_bubble_ = NULL;
     26 
     27 // static
     28 void BookmarkPromptView::ShowPrompt(views::View* anchor_view,
     29                                     PrefService* prefs) {
     30   if (bookmark_bubble_)
     31     return;
     32   bookmark_bubble_ = new BookmarkPromptView(anchor_view, prefs);
     33   views::BubbleDelegateView::CreateBubble(bookmark_bubble_)->Show();
     34 }
     35 
     36 BookmarkPromptView::BookmarkPromptView(views::View* anchor_view,
     37                                        PrefService* prefs)
     38     : BubbleDelegateView(anchor_view, views::BubbleBorder::TOP_RIGHT),
     39       dismiss_link_(NULL),
     40       prefs_(prefs) {
     41   // Compensate for built-in vertical padding in the anchor view's image.
     42   set_anchor_view_insets(gfx::Insets(5, 0, 5, 0));
     43 }
     44 
     45 BookmarkPromptView::~BookmarkPromptView() {
     46   DCHECK_NE(this, bookmark_bubble_);
     47 }
     48 
     49 void BookmarkPromptView::Init() {
     50   SetLayoutManager(new views::BoxLayout(views::BoxLayout::kVertical,
     51                                         kHorizontalPadding, 0, 0));
     52 
     53   views::Label* label = new views::Label(l10n_util::GetStringUTF16(
     54       IDS_BOOKMARK_PROMPT_MESSAGE));
     55   ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
     56   label->SetFont(rb.GetFont(ui::ResourceBundle::MediumFont)
     57                    .DeriveFont(0, gfx::Font::BOLD));
     58   AddChildView(label);
     59 
     60   dismiss_link_ = new views::Link(l10n_util::GetStringUTF16(
     61       IDS_BOOKMARK_PROMPT_DISMISS));
     62   dismiss_link_->SetHorizontalAlignment(gfx::ALIGN_RIGHT);
     63   dismiss_link_->set_listener(this);
     64   AddChildView(dismiss_link_);
     65 }
     66 
     67 void BookmarkPromptView::LinkClicked(views::Link* source, int event_flags) {
     68   DCHECK_EQ(source, dismiss_link_);
     69   BookmarkPromptController::DisableBookmarkPrompt(prefs_);
     70   StartFade(false);
     71 }
     72 
     73 void BookmarkPromptView::WindowClosing() {
     74   bookmark_bubble_ = NULL;
     75   BookmarkPromptController::ClosingBookmarkPrompt();
     76 }
     77