OLD | NEW |
(Empty) | |
| 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 "chrome/browser/ui/views/new_avatar_button.h" |
| 6 |
| 7 #include "grit/generated_resources.h" |
| 8 #include "grit/theme_resources.h" |
| 9 #include "ui/base/l10n/l10n_util.h" |
| 10 #include "ui/base/resource/resource_bundle.h" |
| 11 #include "ui/gfx/canvas.h" |
| 12 #include "ui/gfx/color_utils.h" |
| 13 #include "ui/views/border.h" |
| 14 #include "ui/views/painter.h" |
| 15 |
| 16 namespace { |
| 17 |
| 18 // Text padding within the button border. |
| 19 const int kInset = 10; |
| 20 |
| 21 |
| 22 // AvatarGlassButtonBorder ---------------------------------------------------- |
| 23 class AvatarGlassButtonBorder : public views::TextButtonDefaultBorder { |
| 24 public: |
| 25 AvatarGlassButtonBorder() { |
| 26 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_NORMAL); |
| 27 const int kHotImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_HOVER); |
| 28 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_PRESSED); |
| 29 |
| 30 SetInsets(gfx::Insets(kInset, kInset, kInset, kInset)); |
| 31 set_normal_painter(views::Painter::CreateImageGridPainter(kNormalImageSet)); |
| 32 set_hot_painter(views::Painter::CreateImageGridPainter(kHotImageSet)); |
| 33 set_pushed_painter(views::Painter::CreateImageGridPainter(kPushedImageSet)); |
| 34 } |
| 35 |
| 36 private: |
| 37 virtual ~AvatarGlassButtonBorder() { |
| 38 } |
| 39 |
| 40 DISALLOW_COPY_AND_ASSIGN(AvatarGlassButtonBorder); |
| 41 }; |
| 42 |
| 43 |
| 44 // AvatarThemedButtonBorder --------------------------------------------------- |
| 45 |
| 46 class AvatarThemedButtonBorder : public views::TextButtonDefaultBorder { |
| 47 public: |
| 48 AvatarThemedButtonBorder() { |
| 49 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_NORMAL); |
| 50 const int kHotImageSet[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_HOVER); |
| 51 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_PRESSED); |
| 52 |
| 53 SetInsets(gfx::Insets(kInset, kInset, kInset, kInset)); |
| 54 set_normal_painter(views::Painter::CreateImageGridPainter(kNormalImageSet)); |
| 55 set_hot_painter(views::Painter::CreateImageGridPainter(kHotImageSet)); |
| 56 set_pushed_painter(views::Painter::CreateImageGridPainter(kPushedImageSet)); |
| 57 } |
| 58 |
| 59 private: |
| 60 virtual ~AvatarThemedButtonBorder() { |
| 61 } |
| 62 |
| 63 DISALLOW_COPY_AND_ASSIGN(AvatarThemedButtonBorder); |
| 64 }; |
| 65 |
| 66 } // namespace |
| 67 |
| 68 |
| 69 // NewAvatarButton ------------------------------------------------------------ |
| 70 NewAvatarButton::NewAvatarButton( |
| 71 views::ButtonListener* listener, |
| 72 const string16& profile_name, |
| 73 AvatarButtonStyle button_style) |
| 74 : MenuButton(listener, profile_name, NULL, true) { |
| 75 set_animate_on_state_change(false); |
| 76 |
| 77 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance(); |
| 78 SetFont(rb->GetFont(ui::ResourceBundle::BaseFont)); |
| 79 |
| 80 if (button_style == GLASS_BUTTON) { |
| 81 set_border(new AvatarGlassButtonBorder); |
| 82 set_menu_marker( |
| 83 rb->GetImageNamed(IDR_AVATAR_GLASS_BUTTON_DROPARROW).ToImageSkia()); |
| 84 } else { |
| 85 set_border(new AvatarThemedButtonBorder); |
| 86 set_menu_marker( |
| 87 rb->GetImageNamed(IDR_AVATAR_THEMED_BUTTON_DROPARROW).ToImageSkia()); |
| 88 } |
| 89 SchedulePaint(); |
| 90 } |
| 91 |
| 92 NewAvatarButton::~NewAvatarButton() { |
| 93 } |
| 94 |
| 95 void NewAvatarButton::OnPaint(gfx::Canvas* canvas) { |
| 96 // From TextButton::PaintButton, draw everything but the text. |
| 97 OnPaintBackground(canvas); |
| 98 OnPaintBorder(canvas); |
| 99 OnPaintFocusBorder(canvas); |
| 100 |
| 101 gfx::Rect rect; |
| 102 // In RTL languages the marker gets drawn leftmost, so account for its offset. |
| 103 if (base::i18n::IsRTL()) |
| 104 rect = gfx::Rect(-kInset, 0, size().width(), size().height()); |
| 105 else |
| 106 rect = gfx::Rect(kInset, 0, size().width(), size().height()); |
| 107 // TODO(noms): This should be DrawStringRectWithHalo but that function |
| 108 // has a bug at the moment and incorrectly draws the background. |
| 109 canvas->DrawStringRectWithFlags( |
| 110 text(), |
| 111 gfx::FontList(ui::ResourceBundle::GetSharedInstance().GetFont( |
| 112 ui::ResourceBundle::BaseFont)), |
| 113 SK_ColorBLACK, |
| 114 rect, |
| 115 gfx::Canvas::NO_SUBPIXEL_RENDERING); |
| 116 |
| 117 // From MenuButton::PaintButton, paint the marker |
| 118 PaintMenuMarker(canvas); |
| 119 } |
OLD | NEW |