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 "ui/message_center/views/notification_button.h" 6 7 #include "ui/gfx/canvas.h" 8 #include "ui/message_center/message_center_style.h" 9 #include "ui/message_center/views/constants.h" 10 #include "ui/views/background.h" 11 #include "ui/views/border.h" 12 #include "ui/views/controls/image_view.h" 13 #include "ui/views/controls/label.h" 14 #include "ui/views/layout/box_layout.h" 15 #include "ui/views/painter.h" 16 17 namespace message_center { 18 19 NotificationButton::NotificationButton(views::ButtonListener* listener) 20 : views::CustomButton(listener), 21 icon_(NULL), 22 title_(NULL), 23 focus_painter_(views::Painter::CreateSolidFocusPainter( 24 message_center::kFocusBorderColor, 25 gfx::Insets(1, 2, 2, 2))) { 26 SetFocusable(true); 27 set_request_focus_on_press(false); 28 set_notify_enter_exit_on_child(true); 29 SetLayoutManager( 30 new views::BoxLayout(views::BoxLayout::kHorizontal, 31 message_center::kButtonHorizontalPadding, 32 kButtonVecticalPadding, 33 message_center::kButtonIconToTitlePadding)); 34 } 35 36 NotificationButton::~NotificationButton() { 37 } 38 39 void NotificationButton::SetIcon(const gfx::ImageSkia& image) { 40 if (icon_ != NULL) 41 delete icon_; // This removes the icon from this view's children. 42 if (image.isNull()) { 43 icon_ = NULL; 44 } else { 45 icon_ = new views::ImageView(); 46 icon_->SetImageSize(gfx::Size(message_center::kNotificationButtonIconSize, 47 message_center::kNotificationButtonIconSize)); 48 icon_->SetImage(image); 49 icon_->SetHorizontalAlignment(views::ImageView::LEADING); 50 icon_->SetVerticalAlignment(views::ImageView::LEADING); 51 icon_->SetBorder(views::Border::CreateEmptyBorder( 52 message_center::kButtonIconTopPadding, 0, 0, 0)); 53 AddChildViewAt(icon_, 0); 54 } 55 } 56 57 void NotificationButton::SetTitle(const base::string16& title) { 58 if (title_ != NULL) 59 delete title_; // This removes the title from this view's children. 60 if (title.empty()) { 61 title_ = NULL; 62 } else { 63 title_ = new views::Label(title); 64 title_->SetHorizontalAlignment(gfx::ALIGN_LEFT); 65 title_->SetEnabledColor(message_center::kRegularTextColor); 66 title_->SetBackgroundColor(kRegularTextBackgroundColor); 67 title_->SetBorder( 68 views::Border::CreateEmptyBorder(kButtonTitleTopPadding, 0, 0, 0)); 69 AddChildView(title_); 70 } 71 SetAccessibleName(title); 72 } 73 74 gfx::Size NotificationButton::GetPreferredSize() const { 75 return gfx::Size(message_center::kNotificationWidth, 76 message_center::kButtonHeight); 77 } 78 79 int NotificationButton::GetHeightForWidth(int width) const { 80 return message_center::kButtonHeight; 81 } 82 83 void NotificationButton::OnPaint(gfx::Canvas* canvas) { 84 CustomButton::OnPaint(canvas); 85 views::Painter::PaintFocusPainter(this, canvas, focus_painter_.get()); 86 } 87 88 void NotificationButton::OnFocus() { 89 views::CustomButton::OnFocus(); 90 ScrollRectToVisible(GetLocalBounds()); 91 // We render differently when focused. 92 SchedulePaint(); 93 } 94 95 void NotificationButton::OnBlur() { 96 views::CustomButton::OnBlur(); 97 // We render differently when focused. 98 SchedulePaint(); 99 } 100 101 void NotificationButton::ViewHierarchyChanged( 102 const ViewHierarchyChangedDetails& details) { 103 // We disable view hierarchy change detection in the parent 104 // because it resets the hoverstate, which we do not want 105 // when we update the view to contain a new label or image. 106 views::View::ViewHierarchyChanged(details); 107 } 108 109 void NotificationButton::StateChanged() { 110 if (state() == STATE_HOVERED || state() == STATE_PRESSED) { 111 set_background(views::Background::CreateSolidBackground( 112 message_center::kHoveredButtonBackgroundColor)); 113 } else { 114 set_background(NULL); 115 } 116 } 117 118 } // namespace message_center 119