1 // Copyright (c) 2011 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_bar_instructions_view.h"
6
7 #include "base/utf_string_conversions.h"
8 #include "chrome/browser/defaults.h"
9 #include "chrome/browser/themes/theme_service.h"
10 #include "grit/generated_resources.h"
11 #include "ui/base/l10n/l10n_util.h"
12 #include "views/controls/label.h"
13
14 using views::View;
15
16 // Horizontal padding, in pixels, between the link and label.
17 static const int kViewPadding = 6;
18
BookmarkBarInstructionsView(Delegate * delegate)19 BookmarkBarInstructionsView::BookmarkBarInstructionsView(Delegate* delegate)
20 : delegate_(delegate),
21 instructions_(NULL),
22 import_link_(NULL),
23 baseline_(-1),
24 updated_colors_(false) {
25 instructions_ = new views::Label(
26 UTF16ToWide(l10n_util::GetStringUTF16(IDS_BOOKMARKS_NO_ITEMS)));
27 AddChildView(instructions_);
28
29 if (browser_defaults::kShowImportOnBookmarkBar) {
30 import_link_ = new views::Link(
31 UTF16ToWide(l10n_util::GetStringUTF16(IDS_BOOKMARK_BAR_IMPORT_LINK)));
32 // We don't want the link to alter tab navigation.
33 import_link_->SetFocusable(false);
34 import_link_->SetController(this);
35 AddChildView(import_link_);
36 }
37 }
38
GetPreferredSize()39 gfx::Size BookmarkBarInstructionsView::GetPreferredSize() {
40 int ascent = 0, descent = 0, height = 0, width = 0;
41 for (int i = 0; i < child_count(); ++i) {
42 View* view = GetChildViewAt(i);
43 gfx::Size pref = view->GetPreferredSize();
44 int baseline = view->GetBaseline();
45 if (baseline != -1) {
46 ascent = std::max(ascent, baseline);
47 descent = std::max(descent, pref.height() - baseline);
48 } else {
49 height = std::max(pref.height(), height);
50 }
51 width += pref.width();
52 }
53 width += (child_count() - 1) * kViewPadding;
54 if (ascent != 0)
55 height = std::max(ascent + descent, height);
56 return gfx::Size(width, height);
57 }
58
Layout()59 void BookmarkBarInstructionsView::Layout() {
60 int remaining_width = width();
61 int x = 0;
62 for (int i = 0; i < child_count(); ++i) {
63 View* view = GetChildViewAt(i);
64 gfx::Size pref = view->GetPreferredSize();
65 int baseline = view->GetBaseline();
66 int y;
67 if (baseline != -1 && baseline_ != -1)
68 y = baseline_ - baseline;
69 else
70 y = (height() - pref.height()) / 2;
71 int view_width = std::min(remaining_width, pref.width());
72 view->SetBounds(x, y, view_width, pref.height());
73 x += view_width + kViewPadding;
74 remaining_width = std::max(0, width() - x);
75 }
76 }
77
OnThemeChanged()78 void BookmarkBarInstructionsView::OnThemeChanged() {
79 UpdateColors();
80 }
81
ViewHierarchyChanged(bool is_add,View * parent,View * child)82 void BookmarkBarInstructionsView::ViewHierarchyChanged(bool is_add,
83 View* parent,
84 View* child) {
85 if (!updated_colors_ && is_add && GetWidget())
86 UpdateColors();
87 }
88
GetAccessibleState(ui::AccessibleViewState * state)89 void BookmarkBarInstructionsView::GetAccessibleState(
90 ui::AccessibleViewState* state) {
91 state->role = ui::AccessibilityTypes::ROLE_GROUPING;
92 }
93
LinkActivated(views::Link * source,int event_flags)94 void BookmarkBarInstructionsView::LinkActivated(views::Link* source,
95 int event_flags) {
96 delegate_->ShowImportDialog();
97 }
98
UpdateColors()99 void BookmarkBarInstructionsView::UpdateColors() {
100 // We don't always have a theme provider (ui tests, for example).
101 const ui::ThemeProvider* theme_provider = GetThemeProvider();
102 if (!theme_provider)
103 return;
104 updated_colors_ = true;
105 SkColor text_color =
106 theme_provider->GetColor(ThemeService::COLOR_BOOKMARK_TEXT);
107 instructions_->SetColor(text_color);
108 if (import_link_)
109 import_link_->SetColor(text_color);
110 }
111