Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / views / bookmarks / bookmark_bar_instructions_view.cc
blob7c5787ba8f851eb515c1e5429bb98fd9bc9a8274
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/ui/views/bookmarks/bookmark_bar_instructions_view.h"
7 #include <algorithm>
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/defaults.h"
11 #include "chrome/browser/themes/theme_properties.h"
12 #include "chrome/browser/ui/bookmarks/bookmark_bar_instructions_delegate.h"
13 #include "chrome/grit/generated_resources.h"
14 #include "ui/accessibility/ax_view_state.h"
15 #include "ui/base/l10n/l10n_util.h"
16 #include "ui/base/theme_provider.h"
17 #include "ui/views/controls/label.h"
18 #include "ui/views/controls/link.h"
20 namespace {
22 // Horizontal padding, in pixels, between the link and label.
23 const int kViewPadding = 6;
25 } // namespace
27 BookmarkBarInstructionsView::BookmarkBarInstructionsView(
28 BookmarkBarInstructionsDelegate* delegate)
29 : delegate_(delegate),
30 instructions_(NULL),
31 import_link_(NULL),
32 baseline_(-1),
33 updated_colors_(false) {
34 instructions_ = new views::Label(
35 l10n_util::GetStringUTF16(IDS_BOOKMARKS_NO_ITEMS));
36 instructions_->SetAutoColorReadabilityEnabled(false);
37 AddChildView(instructions_);
39 if (browser_defaults::kShowImportOnBookmarkBar) {
40 import_link_ = new views::Link(
41 l10n_util::GetStringUTF16(IDS_BOOKMARK_BAR_IMPORT_LINK));
42 // We don't want the link to alter tab navigation.
43 import_link_->SetFocusable(false);
44 import_link_->set_listener(this);
45 import_link_->set_context_menu_controller(this);
46 import_link_->SetAutoColorReadabilityEnabled(false);
47 AddChildView(import_link_);
51 gfx::Size BookmarkBarInstructionsView::GetPreferredSize() const {
52 int ascent = 0, descent = 0, height = 0, width = 0;
53 for (int i = 0; i < child_count(); ++i) {
54 const views::View* view = child_at(i);
55 gfx::Size pref = view->GetPreferredSize();
56 int baseline = view->GetBaseline();
57 if (baseline != -1) {
58 ascent = std::max(ascent, baseline);
59 descent = std::max(descent, pref.height() - baseline);
60 } else {
61 height = std::max(pref.height(), height);
63 width += pref.width();
65 width += (child_count() - 1) * kViewPadding;
66 if (ascent != 0)
67 height = std::max(ascent + descent, height);
68 return gfx::Size(width, height);
71 void BookmarkBarInstructionsView::Layout() {
72 int remaining_width = width();
73 int x = 0;
74 for (int i = 0; i < child_count(); ++i) {
75 views::View* view = child_at(i);
76 gfx::Size pref = view->GetPreferredSize();
77 int baseline = view->GetBaseline();
78 int y;
79 if (baseline != -1 && baseline_ != -1)
80 y = baseline_ - baseline;
81 else
82 y = (height() - pref.height()) / 2;
83 int view_width = std::min(remaining_width, pref.width());
84 view->SetBounds(x, y, view_width, pref.height());
85 x += view_width + kViewPadding;
86 remaining_width = std::max(0, width() - x);
90 const char* BookmarkBarInstructionsView::GetClassName() const {
91 return "BookmarkBarInstructionsView";
94 void BookmarkBarInstructionsView::OnThemeChanged() {
95 UpdateColors();
98 void BookmarkBarInstructionsView::ViewHierarchyChanged(
99 const ViewHierarchyChangedDetails& details) {
100 if (!updated_colors_ && details.is_add && GetWidget())
101 UpdateColors();
104 void BookmarkBarInstructionsView::GetAccessibleState(
105 ui::AXViewState* state) {
106 instructions_->GetAccessibleState(state);
109 void BookmarkBarInstructionsView::LinkClicked(views::Link* source,
110 int event_flags) {
111 delegate_->OnImportBookmarks();
114 void BookmarkBarInstructionsView::ShowContextMenuForView(
115 views::View* source,
116 const gfx::Point& point,
117 ui::MenuSourceType source_type) {
118 // Do nothing here, we don't want to show the Bookmarks context menu when
119 // the user right clicks on the "Import bookmarks now" link.
122 void BookmarkBarInstructionsView::UpdateColors() {
123 // We don't always have a theme provider (ui tests, for example).
124 const ui::ThemeProvider* theme_provider = GetThemeProvider();
125 if (!theme_provider)
126 return;
127 updated_colors_ = true;
128 SkColor text_color =
129 theme_provider->GetColor(ThemeProperties::COLOR_BOOKMARK_TEXT);
130 instructions_->SetEnabledColor(text_color);
131 if (import_link_)
132 import_link_->SetEnabledColor(text_color);