- add sources.
[platform/framework/web/crosswalk.git] / src / ash / system / tray / hover_highlight_view.cc
1 // Copyright 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 "ash/system/tray/hover_highlight_view.h"
6
7 #include "ash/system/tray/fixed_sized_image_view.h"
8 #include "ash/system/tray/tray_constants.h"
9 #include "ash/system/tray/view_click_listener.h"
10 #include "grit/ui_resources.h"
11 #include "ui/base/resource/resource_bundle.h"
12 #include "ui/gfx/canvas.h"
13 #include "ui/views/controls/image_view.h"
14 #include "ui/views/controls/label.h"
15 #include "ui/views/layout/box_layout.h"
16 #include "ui/views/layout/fill_layout.h"
17
18 namespace {
19
20 const int kCheckLabelPadding = 4;
21
22 }  // namespace
23
24 namespace ash {
25 namespace internal {
26
27 HoverHighlightView::HoverHighlightView(ViewClickListener* listener)
28     : listener_(listener),
29       text_label_(NULL),
30       highlight_color_(kHoverBackgroundColor),
31       default_color_(0),
32       text_highlight_color_(0),
33       text_default_color_(0),
34       hover_(false),
35       expandable_(false) {
36   set_notify_enter_exit_on_child(true);
37 }
38
39 HoverHighlightView::~HoverHighlightView() {
40 }
41
42 void HoverHighlightView::AddIconAndLabel(const gfx::ImageSkia& image,
43                                          const base::string16& text,
44                                          gfx::Font::FontStyle style) {
45   SetLayoutManager(new views::BoxLayout(
46       views::BoxLayout::kHorizontal, 0, 3, kTrayPopupPaddingBetweenItems));
47   views::ImageView* image_view =
48       new FixedSizedImageView(kTrayPopupDetailsIconWidth, 0);
49   image_view->SetImage(image);
50   AddChildView(image_view);
51
52   text_label_ = new views::Label(text);
53   text_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
54   text_label_->SetFont(text_label_->font().DeriveFont(0, style));
55   if (text_default_color_)
56     text_label_->SetEnabledColor(text_default_color_);
57   AddChildView(text_label_);
58
59   SetAccessibleName(text);
60 }
61
62 views::Label* HoverHighlightView::AddLabel(const base::string16& text,
63                                            gfx::Font::FontStyle style) {
64   SetLayoutManager(new views::FillLayout());
65   text_label_ = new views::Label(text);
66   int margin = kTrayPopupPaddingHorizontal +
67       kTrayPopupDetailsLabelExtraLeftMargin;
68   int left_margin = 0;
69   int right_margin = 0;
70   if (base::i18n::IsRTL())
71     right_margin = margin;
72   else
73     left_margin = margin;
74   text_label_->set_border(
75       views::Border::CreateEmptyBorder(5, left_margin, 5, right_margin));
76   text_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
77   text_label_->SetFont(text_label_->font().DeriveFont(0, style));
78   // Do not set alpha value in disable color. It will have issue with elide
79   // blending filter in disabled state for rendering label text color.
80   text_label_->SetDisabledColor(SkColorSetARGB(255, 127, 127, 127));
81   if (text_default_color_)
82     text_label_->SetEnabledColor(text_default_color_);
83   AddChildView(text_label_);
84
85   SetAccessibleName(text);
86   return text_label_;
87 }
88
89 views::Label* HoverHighlightView::AddCheckableLabel(const base::string16& text,
90                                                     gfx::Font::FontStyle style,
91                                                     bool checked) {
92   if (checked) {
93     ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
94     const gfx::ImageSkia* check =
95         rb.GetImageNamed(IDR_MENU_CHECK).ToImageSkia();
96     int margin = kTrayPopupPaddingHorizontal +
97         kTrayPopupDetailsLabelExtraLeftMargin - kCheckLabelPadding;
98     SetLayoutManager(new views::BoxLayout(
99         views::BoxLayout::kHorizontal, 0, 3, kCheckLabelPadding));
100     views::ImageView* image_view = new FixedSizedImageView(margin, 0);
101     image_view->SetImage(check);
102     image_view->SetHorizontalAlignment(views::ImageView::TRAILING);
103     AddChildView(image_view);
104
105     text_label_ = new views::Label(text);
106     text_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
107     text_label_->SetFont(text_label_->font().DeriveFont(0, style));
108     text_label_->SetDisabledColor(SkColorSetARGB(127, 0, 0, 0));
109     if (text_default_color_)
110       text_label_->SetEnabledColor(text_default_color_);
111     AddChildView(text_label_);
112
113     SetAccessibleName(text);
114     return text_label_;
115   }
116   return AddLabel(text, style);
117 }
118
119 void HoverHighlightView::SetExpandable(bool expandable) {
120   if (expandable != expandable_) {
121     expandable_ = expandable;
122     InvalidateLayout();
123   }
124 }
125
126 bool HoverHighlightView::PerformAction(const ui::Event& event) {
127   if (!listener_)
128     return false;
129   listener_->OnViewClicked(this);
130   return true;
131 }
132
133 gfx::Size HoverHighlightView::GetPreferredSize() {
134   gfx::Size size = ActionableView::GetPreferredSize();
135   if (!expandable_ || size.height() < kTrayPopupItemHeight)
136     size.set_height(kTrayPopupItemHeight);
137   return size;
138 }
139
140 int HoverHighlightView::GetHeightForWidth(int width) {
141   return GetPreferredSize().height();
142 }
143
144 void HoverHighlightView::OnMouseEntered(const ui::MouseEvent& event) {
145   hover_ = true;
146   if (text_highlight_color_ && text_label_)
147     text_label_->SetEnabledColor(text_highlight_color_);
148   SchedulePaint();
149 }
150
151 void HoverHighlightView::OnMouseExited(const ui::MouseEvent& event) {
152   hover_ = false;
153   if (text_default_color_ && text_label_)
154     text_label_->SetEnabledColor(text_default_color_);
155   SchedulePaint();
156 }
157
158 void HoverHighlightView::OnEnabledChanged() {
159   for (int i = 0; i < child_count(); ++i)
160     child_at(i)->SetEnabled(enabled());
161 }
162
163 void HoverHighlightView::OnPaintBackground(gfx::Canvas* canvas) {
164   canvas->DrawColor(hover_ ? highlight_color_ : default_color_);
165 }
166
167 void HoverHighlightView::OnFocus() {
168   ScrollRectToVisible(gfx::Rect(gfx::Point(), size()));
169   ActionableView::OnFocus();
170 }
171
172 }  // namespace internal
173 }  // namespace ash