OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "views/controls/image_view.h" | 5 #include "views/controls/image_view.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "gfx/canvas.h" | 8 #include "gfx/canvas.h" |
9 #include "gfx/insets.h" | 9 #include "gfx/insets.h" |
10 | 10 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 image_size_ = image_size; | 42 image_size_ = image_size; |
43 } | 43 } |
44 | 44 |
45 bool ImageView::GetImageSize(gfx::Size* image_size) { | 45 bool ImageView::GetImageSize(gfx::Size* image_size) { |
46 DCHECK(image_size); | 46 DCHECK(image_size); |
47 if (image_size_set_) | 47 if (image_size_set_) |
48 *image_size = image_size_; | 48 *image_size = image_size_; |
49 return image_size_set_; | 49 return image_size_set_; |
50 } | 50 } |
51 | 51 |
| 52 gfx::Rect ImageView::GetImageBounds() const { |
| 53 gfx::Size image_size(image_size_set_ ? |
| 54 image_size_ : gfx::Size(image_.width(), image_.height())); |
| 55 return gfx::Rect(ComputeImageOrigin(image_size), image_size); |
| 56 } |
| 57 |
52 void ImageView::ResetImageSize() { | 58 void ImageView::ResetImageSize() { |
53 image_size_set_ = false; | 59 image_size_set_ = false; |
54 } | 60 } |
55 | 61 |
56 gfx::Size ImageView::GetPreferredSize() { | 62 gfx::Size ImageView::GetPreferredSize() { |
57 gfx::Insets insets = GetInsets(); | 63 gfx::Insets insets = GetInsets(); |
58 if (image_size_set_) { | 64 if (image_size_set_) { |
59 gfx::Size image_size; | 65 gfx::Size image_size; |
60 GetImageSize(&image_size); | 66 GetImageSize(&image_size); |
61 image_size.Enlarge(insets.width(), insets.height()); | 67 image_size.Enlarge(insets.width(), insets.height()); |
62 return image_size; | 68 return image_size; |
63 } | 69 } |
64 return gfx::Size(image_.width() + insets.width(), | 70 return gfx::Size(image_.width() + insets.width(), |
65 image_.height() + insets.height()); | 71 image_.height() + insets.height()); |
66 } | 72 } |
67 | 73 |
68 void ImageView::ComputeImageOrigin(int image_width, int image_height, | 74 gfx::Point ImageView::ComputeImageOrigin(const gfx::Size& image_size) const { |
69 int *x, int *y) { | 75 gfx::Insets insets = GetInsets(); |
| 76 |
| 77 int x; |
70 // In order to properly handle alignment of images in RTL locales, we need | 78 // In order to properly handle alignment of images in RTL locales, we need |
71 // to flip the meaning of trailing and leading. For example, if the | 79 // to flip the meaning of trailing and leading. For example, if the |
72 // horizontal alignment is set to trailing, then we'll use left alignment for | 80 // horizontal alignment is set to trailing, then we'll use left alignment for |
73 // the image instead of right alignment if the UI layout is RTL. | 81 // the image instead of right alignment if the UI layout is RTL. |
74 Alignment actual_horiz_alignment = horiz_alignment_; | 82 Alignment actual_horiz_alignment = horiz_alignment_; |
75 if (UILayoutIsRightToLeft()) { | 83 if (UILayoutIsRightToLeft() && (horiz_alignment_ != CENTER)) |
76 if (horiz_alignment_ == TRAILING) | 84 actual_horiz_alignment = (horiz_alignment_ == LEADING) ? TRAILING : LEADING; |
77 actual_horiz_alignment = LEADING; | 85 switch (actual_horiz_alignment) { |
78 if (horiz_alignment_ == LEADING) | 86 case LEADING: x = insets.left(); break; |
79 actual_horiz_alignment = TRAILING; | 87 case TRAILING: x = width() - insets.right() - image_size.width(); break; |
| 88 case CENTER: x = (width() - image_size.width()) / 2; break; |
| 89 default: NOTREACHED(); x = 0; break; |
80 } | 90 } |
81 | 91 |
82 gfx::Insets insets = GetInsets(); | 92 int y; |
83 | 93 switch (vert_alignment_) { |
84 switch (actual_horiz_alignment) { | 94 case LEADING: y = insets.top(); break; |
85 case LEADING: | 95 case TRAILING: y = height() - insets.bottom() - image_size.height(); break; |
86 *x = insets.left(); | 96 case CENTER: y = (height() - image_size.height()) / 2; break; |
87 break; | 97 default: NOTREACHED(); y = 0; break; |
88 case TRAILING: | |
89 *x = width() - insets.right() - image_width; | |
90 break; | |
91 case CENTER: | |
92 *x = (width() - image_width) / 2; | |
93 break; | |
94 default: | |
95 NOTREACHED(); | |
96 } | 98 } |
97 | 99 |
98 switch (vert_alignment_) { | 100 return gfx::Point(x, y); |
99 case LEADING: | |
100 *y = insets.top(); | |
101 break; | |
102 case TRAILING: | |
103 *y = height() - insets.bottom() - image_height; | |
104 break; | |
105 case CENTER: | |
106 *y = (height() - image_height) / 2; | |
107 break; | |
108 default: | |
109 NOTREACHED(); | |
110 } | |
111 } | 101 } |
112 | 102 |
113 void ImageView::Paint(gfx::Canvas* canvas) { | 103 void ImageView::Paint(gfx::Canvas* canvas) { |
114 View::Paint(canvas); | 104 View::Paint(canvas); |
115 int image_width = image_.width(); | |
116 int image_height = image_.height(); | |
117 | 105 |
118 if (image_width == 0 || image_height == 0) | 106 gfx::Rect image_bounds(GetImageBounds()); |
| 107 if (image_bounds.IsEmpty()) |
119 return; | 108 return; |
120 | 109 |
121 int x, y; | 110 if (image_bounds.size() != gfx::Size(image_.width(), image_.height())) { |
122 if (image_size_set_ && | |
123 (image_size_.width() != image_width || | |
124 image_size_.width() != image_height)) { | |
125 // Resize case | 111 // Resize case |
126 image_.buildMipMap(false); | 112 image_.buildMipMap(false); |
127 ComputeImageOrigin(image_size_.width(), image_size_.height(), &x, &y); | |
128 SkPaint paint; | 113 SkPaint paint; |
129 paint.setFilterBitmap(true); | 114 paint.setFilterBitmap(true); |
130 canvas->DrawBitmapInt(image_, 0, 0, image_width, image_height, | 115 canvas->DrawBitmapInt(image_, 0, 0, image_.width(), image_.height(), |
131 x, y, image_size_.width(), image_size_.height(), | 116 image_bounds.x(), image_bounds.y(), image_bounds.width(), |
132 true, paint); | 117 image_bounds.height(), true, paint); |
133 } else { | 118 } else { |
134 ComputeImageOrigin(image_width, image_height, &x, &y); | 119 canvas->DrawBitmapInt(image_, image_bounds.x(), image_bounds.y()); |
135 canvas->DrawBitmapInt(image_, x, y); | |
136 } | 120 } |
137 } | 121 } |
138 | 122 |
139 bool ImageView::GetAccessibleRole(AccessibilityTypes::Role* role) { | 123 bool ImageView::GetAccessibleRole(AccessibilityTypes::Role* role) { |
140 if (!role) | 124 if (!role) |
141 return false; | 125 return false; |
142 | 126 |
143 *role = AccessibilityTypes::ROLE_GRAPHIC; | 127 *role = AccessibilityTypes::ROLE_GRAPHIC; |
144 return true; | 128 return true; |
145 } | 129 } |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 | 161 |
178 bool ImageView::GetTooltipText(const gfx::Point& p, std::wstring* tooltip) { | 162 bool ImageView::GetTooltipText(const gfx::Point& p, std::wstring* tooltip) { |
179 if (tooltip_text_.empty()) | 163 if (tooltip_text_.empty()) |
180 return false; | 164 return false; |
181 | 165 |
182 *tooltip = GetTooltipText(); | 166 *tooltip = GetTooltipText(); |
183 return true; | 167 return true; |
184 } | 168 } |
185 | 169 |
186 } // namespace views | 170 } // namespace views |
OLD | NEW |