Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / ash / system / tray / tray_bar_button_with_title.cc
1 // Copyright 2013 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/tray_bar_button_with_title.h"
6
7 #include "ash/system/tray/tray_constants.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "ui/base/resource/resource_bundle.h"
10 #include "ui/gfx/image/image_skia.h"
11 #include "ui/resources/grit/ui_resources.h"
12 #include "ui/views/controls/label.h"
13 #include "ui/views/painter.h"
14
15 namespace ash {
16 namespace {
17
18 const int kBarImagesActive[] = {
19     IDR_SLIDER_ACTIVE_LEFT,
20     IDR_SLIDER_ACTIVE_CENTER,
21     IDR_SLIDER_ACTIVE_RIGHT,
22 };
23
24 const int kBarImagesDisabled[] = {
25     IDR_SLIDER_DISABLED_LEFT,
26     IDR_SLIDER_DISABLED_CENTER,
27     IDR_SLIDER_DISABLED_RIGHT,
28 };
29
30 }  // namespace
31
32 class TrayBarButtonWithTitle::TrayBarButton : public views::View {
33  public:
34   TrayBarButton(const int bar_active_images[], const int bar_disabled_images[])
35     : views::View(),
36       bar_active_images_(bar_active_images),
37       bar_disabled_images_(bar_disabled_images),
38       painter_(new views::HorizontalPainter(bar_active_images_)){
39   }
40   virtual ~TrayBarButton() {}
41
42   // Overriden from views::View:
43   virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {
44     painter_->Paint(canvas, size());
45   }
46
47   void Update(bool control_on) {
48     painter_.reset(new views::HorizontalPainter(
49         control_on ? bar_active_images_ : bar_disabled_images_));
50     SchedulePaint();
51   }
52
53  private:
54   const int* bar_active_images_;
55   const int* bar_disabled_images_;
56   scoped_ptr<views::HorizontalPainter> painter_;
57
58   DISALLOW_COPY_AND_ASSIGN(TrayBarButton);
59 };
60
61 TrayBarButtonWithTitle::TrayBarButtonWithTitle(views::ButtonListener* listener,
62                                                int title_id,
63                                                int width)
64     : views::CustomButton(listener),
65       image_(new TrayBarButton(kBarImagesActive, kBarImagesDisabled)),
66       title_(NULL),
67       width_(width) {
68   AddChildView(image_);
69   if (title_id != -1) {
70     title_ = new views::Label;
71     ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
72     base::string16 text = rb.GetLocalizedString(title_id);
73     title_->SetText(text);
74     AddChildView(title_);
75   }
76
77   image_height_ = ui::ResourceBundle::GetSharedInstance().GetImageNamed(
78       kBarImagesActive[0]).ToImageSkia()->height();
79 }
80
81 TrayBarButtonWithTitle::~TrayBarButtonWithTitle() {}
82
83 void TrayBarButtonWithTitle::UpdateButton(bool control_on) {
84   image_->Update(control_on);
85 }
86
87 gfx::Size TrayBarButtonWithTitle::GetPreferredSize() const {
88   return gfx::Size(width_, kTrayPopupItemHeight);
89 }
90
91 void TrayBarButtonWithTitle::Layout() {
92   gfx::Rect rect(GetContentsBounds());
93   int bar_image_y = rect.height() / 2 - image_height_ / 2;
94   gfx::Rect bar_image_rect(rect.x(),
95                            bar_image_y,
96                            rect.width(),
97                            image_height_);
98   image_->SetBoundsRect(bar_image_rect);
99   if (title_) {
100     // The image_ has some empty space below the bar image, move the title
101     // a little bit up to look closer to the bar.
102     gfx::Size title_size = title_->GetPreferredSize();
103     title_->SetBounds(rect.x(),
104                       bar_image_y + image_height_ - 3,
105                       rect.width(),
106                       title_size.height());
107   }
108 }
109
110 }  // namespace ash