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