| OLD | NEW |
| (Empty) |
| 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 "views/controls/image_view.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "base/utf_string_conversions.h" | |
| 9 #include "third_party/skia/include/core/SkPaint.h" | |
| 10 #include "ui/base/accessibility/accessible_view_state.h" | |
| 11 #include "ui/gfx/canvas.h" | |
| 12 #include "ui/gfx/insets.h" | |
| 13 | |
| 14 namespace views { | |
| 15 | |
| 16 ImageView::ImageView() | |
| 17 : image_size_set_(false), | |
| 18 horiz_alignment_(CENTER), | |
| 19 vert_alignment_(CENTER) { | |
| 20 } | |
| 21 | |
| 22 ImageView::~ImageView() { | |
| 23 } | |
| 24 | |
| 25 void ImageView::SetImage(const SkBitmap& bm) { | |
| 26 image_ = bm; | |
| 27 PreferredSizeChanged(); | |
| 28 SchedulePaint(); | |
| 29 } | |
| 30 | |
| 31 void ImageView::SetImage(const SkBitmap* bm) { | |
| 32 if (bm) { | |
| 33 SetImage(*bm); | |
| 34 } else { | |
| 35 SkBitmap t; | |
| 36 SetImage(t); | |
| 37 } | |
| 38 } | |
| 39 | |
| 40 const SkBitmap& ImageView::GetImage() { | |
| 41 return image_; | |
| 42 } | |
| 43 | |
| 44 void ImageView::SetImageSize(const gfx::Size& image_size) { | |
| 45 image_size_set_ = true; | |
| 46 image_size_ = image_size; | |
| 47 PreferredSizeChanged(); | |
| 48 } | |
| 49 | |
| 50 bool ImageView::GetImageSize(gfx::Size* image_size) { | |
| 51 DCHECK(image_size); | |
| 52 if (image_size_set_) | |
| 53 *image_size = image_size_; | |
| 54 return image_size_set_; | |
| 55 } | |
| 56 | |
| 57 gfx::Rect ImageView::GetImageBounds() const { | |
| 58 gfx::Size image_size(image_size_set_ ? | |
| 59 image_size_ : gfx::Size(image_.width(), image_.height())); | |
| 60 return gfx::Rect(ComputeImageOrigin(image_size), image_size); | |
| 61 } | |
| 62 | |
| 63 void ImageView::ResetImageSize() { | |
| 64 image_size_set_ = false; | |
| 65 } | |
| 66 | |
| 67 gfx::Size ImageView::GetPreferredSize() { | |
| 68 gfx::Insets insets = GetInsets(); | |
| 69 if (image_size_set_) { | |
| 70 gfx::Size image_size; | |
| 71 GetImageSize(&image_size); | |
| 72 image_size.Enlarge(insets.width(), insets.height()); | |
| 73 return image_size; | |
| 74 } | |
| 75 return gfx::Size(image_.width() + insets.width(), | |
| 76 image_.height() + insets.height()); | |
| 77 } | |
| 78 | |
| 79 gfx::Point ImageView::ComputeImageOrigin(const gfx::Size& image_size) const { | |
| 80 gfx::Insets insets = GetInsets(); | |
| 81 | |
| 82 int x; | |
| 83 // In order to properly handle alignment of images in RTL locales, we need | |
| 84 // to flip the meaning of trailing and leading. For example, if the | |
| 85 // horizontal alignment is set to trailing, then we'll use left alignment for | |
| 86 // the image instead of right alignment if the UI layout is RTL. | |
| 87 Alignment actual_horiz_alignment = horiz_alignment_; | |
| 88 if (base::i18n::IsRTL() && (horiz_alignment_ != CENTER)) | |
| 89 actual_horiz_alignment = (horiz_alignment_ == LEADING) ? TRAILING : LEADING; | |
| 90 switch (actual_horiz_alignment) { | |
| 91 case LEADING: x = insets.left(); break; | |
| 92 case TRAILING: x = width() - insets.right() - image_size.width(); break; | |
| 93 case CENTER: x = (width() - image_size.width()) / 2; break; | |
| 94 default: NOTREACHED(); x = 0; break; | |
| 95 } | |
| 96 | |
| 97 int y; | |
| 98 switch (vert_alignment_) { | |
| 99 case LEADING: y = insets.top(); break; | |
| 100 case TRAILING: y = height() - insets.bottom() - image_size.height(); break; | |
| 101 case CENTER: y = (height() - image_size.height()) / 2; break; | |
| 102 default: NOTREACHED(); y = 0; break; | |
| 103 } | |
| 104 | |
| 105 return gfx::Point(x, y); | |
| 106 } | |
| 107 | |
| 108 void ImageView::OnPaint(gfx::Canvas* canvas) { | |
| 109 View::OnPaint(canvas); | |
| 110 | |
| 111 if (image_.empty()) | |
| 112 return; | |
| 113 | |
| 114 gfx::Rect image_bounds(GetImageBounds()); | |
| 115 if (image_bounds.IsEmpty()) | |
| 116 return; | |
| 117 | |
| 118 if (image_bounds.size() != gfx::Size(image_.width(), image_.height())) { | |
| 119 // Resize case | |
| 120 image_.buildMipMap(false); | |
| 121 SkPaint paint; | |
| 122 paint.setFilterBitmap(true); | |
| 123 canvas->DrawBitmapInt(image_, 0, 0, image_.width(), image_.height(), | |
| 124 image_bounds.x(), image_bounds.y(), image_bounds.width(), | |
| 125 image_bounds.height(), true, paint); | |
| 126 } else { | |
| 127 canvas->DrawBitmapInt(image_, image_bounds.x(), image_bounds.y()); | |
| 128 } | |
| 129 } | |
| 130 | |
| 131 void ImageView::GetAccessibleState(ui::AccessibleViewState* state) { | |
| 132 state->role = ui::AccessibilityTypes::ROLE_GRAPHIC; | |
| 133 state->name = tooltip_text_; | |
| 134 } | |
| 135 | |
| 136 void ImageView::SetHorizontalAlignment(Alignment ha) { | |
| 137 if (ha != horiz_alignment_) { | |
| 138 horiz_alignment_ = ha; | |
| 139 SchedulePaint(); | |
| 140 } | |
| 141 } | |
| 142 | |
| 143 ImageView::Alignment ImageView::GetHorizontalAlignment() const { | |
| 144 return horiz_alignment_; | |
| 145 } | |
| 146 | |
| 147 void ImageView::SetVerticalAlignment(Alignment va) { | |
| 148 if (va != vert_alignment_) { | |
| 149 vert_alignment_ = va; | |
| 150 SchedulePaint(); | |
| 151 } | |
| 152 } | |
| 153 | |
| 154 ImageView::Alignment ImageView::GetVerticalAlignment() const { | |
| 155 return vert_alignment_; | |
| 156 } | |
| 157 | |
| 158 void ImageView::SetTooltipText(const string16& tooltip) { | |
| 159 tooltip_text_ = tooltip; | |
| 160 } | |
| 161 | |
| 162 string16 ImageView::GetTooltipText() const { | |
| 163 return tooltip_text_; | |
| 164 } | |
| 165 | |
| 166 bool ImageView::GetTooltipText(const gfx::Point& p, string16* tooltip) const { | |
| 167 if (tooltip_text_.empty()) | |
| 168 return false; | |
| 169 | |
| 170 *tooltip = GetTooltipText(); | |
| 171 return true; | |
| 172 } | |
| 173 | |
| 174 } // namespace views | |
| OLD | NEW |