1 // Copyright (c) 2011 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/infobars/infobar_button_border.h" 6 7 #include "grit/theme_resources.h" 8 #include "third_party/skia/include/core/SkBitmap.h" 9 #include "ui/base/resource/resource_bundle.h" 10 #include "ui/gfx/canvas.h" 11 #include "views/controls/button/text_button.h" 12 13 InfoBarButtonBorder::InfoBarButtonBorder() { 14 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); 15 normal_set_.top_left = rb.GetBitmapNamed(IDR_INFOBARBUTTON_TOP_LEFT_N); 16 normal_set_.top = rb.GetBitmapNamed(IDR_INFOBARBUTTON_TOP_N); 17 normal_set_.top_right = rb.GetBitmapNamed(IDR_INFOBARBUTTON_TOP_RIGHT_N); 18 normal_set_.left = rb.GetBitmapNamed(IDR_INFOBARBUTTON_LEFT_N); 19 normal_set_.center = rb.GetBitmapNamed(IDR_INFOBARBUTTON_CENTER_N); 20 normal_set_.right = rb.GetBitmapNamed(IDR_INFOBARBUTTON_RIGHT_N); 21 normal_set_.bottom_left = rb.GetBitmapNamed(IDR_INFOBARBUTTON_BOTTOM_LEFT_N); 22 normal_set_.bottom = rb.GetBitmapNamed(IDR_INFOBARBUTTON_BOTTOM_N); 23 normal_set_.bottom_right = 24 rb.GetBitmapNamed(IDR_INFOBARBUTTON_BOTTOM_RIGHT_N); 25 26 hot_set_.top_left = rb.GetBitmapNamed(IDR_INFOBARBUTTON_TOP_LEFT_H); 27 hot_set_.top = rb.GetBitmapNamed(IDR_INFOBARBUTTON_TOP_H); 28 hot_set_.top_right = rb.GetBitmapNamed(IDR_INFOBARBUTTON_TOP_RIGHT_H); 29 hot_set_.left = rb.GetBitmapNamed(IDR_INFOBARBUTTON_LEFT_H); 30 hot_set_.center = rb.GetBitmapNamed(IDR_INFOBARBUTTON_CENTER_H); 31 hot_set_.right = rb.GetBitmapNamed(IDR_INFOBARBUTTON_RIGHT_H); 32 hot_set_.bottom_left = rb.GetBitmapNamed(IDR_INFOBARBUTTON_BOTTOM_LEFT_H); 33 hot_set_.bottom = rb.GetBitmapNamed(IDR_INFOBARBUTTON_BOTTOM_H); 34 hot_set_.bottom_right = rb.GetBitmapNamed(IDR_INFOBARBUTTON_BOTTOM_RIGHT_H); 35 36 pushed_set_.top_left = rb.GetBitmapNamed(IDR_INFOBARBUTTON_TOP_LEFT_P); 37 pushed_set_.top = rb.GetBitmapNamed(IDR_INFOBARBUTTON_TOP_P); 38 pushed_set_.top_right = rb.GetBitmapNamed(IDR_INFOBARBUTTON_TOP_RIGHT_P); 39 pushed_set_.left = rb.GetBitmapNamed(IDR_INFOBARBUTTON_LEFT_P); 40 pushed_set_.center = rb.GetBitmapNamed(IDR_INFOBARBUTTON_CENTER_P); 41 pushed_set_.right = rb.GetBitmapNamed(IDR_INFOBARBUTTON_RIGHT_P); 42 pushed_set_.bottom_left = rb.GetBitmapNamed(IDR_INFOBARBUTTON_BOTTOM_LEFT_P); 43 pushed_set_.bottom = rb.GetBitmapNamed(IDR_INFOBARBUTTON_BOTTOM_P); 44 pushed_set_.bottom_right = 45 rb.GetBitmapNamed(IDR_INFOBARBUTTON_BOTTOM_RIGHT_P); 46 } 47 48 InfoBarButtonBorder::~InfoBarButtonBorder() { 49 } 50 51 void InfoBarButtonBorder::GetInsets(gfx::Insets* insets) const { 52 static const int kPreferredPaddingHorizontal = 6; 53 static const int kPreferredPaddingVertical = 5; 54 insets->Set(kPreferredPaddingVertical, kPreferredPaddingHorizontal, 55 kPreferredPaddingVertical, kPreferredPaddingHorizontal); 56 } 57 58 void InfoBarButtonBorder::Paint(const views::View& view, 59 gfx::Canvas* canvas) const { 60 const views::TextButton* mb = static_cast<const views::TextButton*>(&view); 61 int state = mb->state(); 62 const MBBImageSet* set = &normal_set_; 63 if (state == views::TextButton::BS_HOT) 64 set = &hot_set_; 65 else if (state == views::TextButton::BS_PUSHED) 66 set = &pushed_set_; 67 68 // Draw top left image. 69 canvas->DrawBitmapInt(*set->top_left, 0, 0); 70 71 // Stretch top image. 72 const gfx::Rect& bounds = view.bounds(); 73 canvas->DrawBitmapInt(*set->top, 0, 0, set->top->width(), set->top->height(), 74 set->top_left->width(), 0, 75 bounds.width() - set->top_right->width() - set->top_left->width(), 76 set->top->height(), false); 77 78 // Draw top right image. 79 canvas->DrawBitmapInt(*set->top_right, 80 bounds.width() - set->top_right->width(), 0); 81 82 // Stretch left image. 83 canvas->DrawBitmapInt(*set->left, 0, 0, set->left->width(), 84 set->left->height(), 0, set->top_left->height(), set->top_left->width(), 85 bounds.height() - set->top->height() - set->bottom_left->height(), false); 86 87 // Stretch center image. 88 canvas->DrawBitmapInt(*set->center, 0, 0, set->center->width(), 89 set->center->height(), set->left->width(), set->top->height(), 90 bounds.width() - set->right->width() - set->left->width(), 91 bounds.height() - set->bottom->height() - set->top->height(), false); 92 93 // Stretch right image. 94 canvas->DrawBitmapInt(*set->right, 0, 0, set->right->width(), 95 set->right->height(), bounds.width() - set->right->width(), 96 set->top_right->height(), set->right->width(), 97 bounds.height() - set->bottom_right->height() - set->top_right->height(), 98 false); 99 100 // Draw bottom left image. 101 canvas->DrawBitmapInt(*set->bottom_left, 0, 102 bounds.height() - set->bottom_left->height()); 103 104 // Stretch bottom image. 105 canvas->DrawBitmapInt(*set->bottom, 0, 0, set->bottom->width(), 106 set->bottom->height(), set->bottom_left->width(), 107 bounds.height() - set->bottom->height(), 108 bounds.width() - set->bottom_right->width() - set->bottom_left->width(), 109 set->bottom->height(), false); 110 111 // Draw bottom right image. 112 canvas->DrawBitmapInt(*set->bottom_right, 113 bounds.width() - set->bottom_right->width(), 114 bounds.height() - set->bottom_right->height()); 115 } 116