OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "ui/views/controls/button/label_button.h" | 5 #include "ui/views/controls/button/label_button.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "grit/ui_resources.h" | 8 #include "grit/ui_resources.h" |
9 #include "ui/base/animation/throb_animation.h" | 9 #include "ui/base/animation/throb_animation.h" |
10 #include "ui/base/resource/resource_bundle.h" | 10 #include "ui/base/resource/resource_bundle.h" |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 LabelButton::~LabelButton() {} | 61 LabelButton::~LabelButton() {} |
62 | 62 |
63 const gfx::ImageSkia& LabelButton::GetImage(ButtonState for_state) { | 63 const gfx::ImageSkia& LabelButton::GetImage(ButtonState for_state) { |
64 if (for_state != STATE_NORMAL && button_state_images_[for_state].isNull()) | 64 if (for_state != STATE_NORMAL && button_state_images_[for_state].isNull()) |
65 return button_state_images_[STATE_NORMAL]; | 65 return button_state_images_[STATE_NORMAL]; |
66 return button_state_images_[for_state]; | 66 return button_state_images_[for_state]; |
67 } | 67 } |
68 | 68 |
69 void LabelButton::SetImage(ButtonState for_state, const gfx::ImageSkia& image) { | 69 void LabelButton::SetImage(ButtonState for_state, const gfx::ImageSkia& image) { |
70 button_state_images_[for_state] = image; | 70 button_state_images_[for_state] = image; |
71 image_->SetImage(GetImage(state())); | 71 UpdateImage(); |
72 } | 72 } |
73 | 73 |
74 const string16& LabelButton::GetText() const { | 74 const string16& LabelButton::GetText() const { |
75 return label_->text(); | 75 return label_->text(); |
76 } | 76 } |
77 | 77 |
78 void LabelButton::SetText(const string16& text) { | 78 void LabelButton::SetText(const string16& text) { |
79 SetAccessibleName(text); | 79 SetAccessibleName(text); |
80 label_->SetText(text); | 80 label_->SetText(text); |
81 } | 81 } |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 min_size_ = size; | 183 min_size_ = size; |
184 | 184 |
185 // Return the largest known size clamped to the maximum size (if valid). | 185 // Return the largest known size clamped to the maximum size (if valid). |
186 if (max_size_.width() > 0) | 186 if (max_size_.width() > 0) |
187 size.set_width(std::min(max_size_.width(), size.width())); | 187 size.set_width(std::min(max_size_.width(), size.width())); |
188 if (max_size_.height() > 0) | 188 if (max_size_.height() > 0) |
189 size.set_height(std::min(max_size_.height(), size.height())); | 189 size.set_height(std::min(max_size_.height(), size.height())); |
190 return size; | 190 return size; |
191 } | 191 } |
192 | 192 |
| 193 void LabelButton::Layout() { |
| 194 gfx::HorizontalAlignment adjusted_alignment = GetHorizontalAlignment(); |
| 195 if (base::i18n::IsRTL() && adjusted_alignment != gfx::ALIGN_CENTER) |
| 196 adjusted_alignment = (adjusted_alignment == gfx::ALIGN_LEFT) ? |
| 197 gfx::ALIGN_RIGHT : gfx::ALIGN_LEFT; |
| 198 |
| 199 gfx::Rect child_area(GetLocalBounds()); |
| 200 child_area.Inset(GetInsets()); |
| 201 |
| 202 gfx::Size image_size(image_->GetPreferredSize()); |
| 203 image_size.set_width(std::min(image_size.width(), child_area.width())); |
| 204 image_size.set_height(std::min(image_size.height(), child_area.height())); |
| 205 |
| 206 // The label takes any remaining width after sizing the image, unless both |
| 207 // views are centered. In that case, using the tighter preferred label width |
| 208 // avoids wasted space within the label that would look like awkward padding. |
| 209 gfx::Size label_size(child_area.size()); |
| 210 if (!image_size.IsEmpty() && !label_size.IsEmpty()) { |
| 211 label_size.set_width( |
| 212 std::max(child_area.width() - image_size.width() - kSpacing, 0)); |
| 213 if (adjusted_alignment == gfx::ALIGN_CENTER) { |
| 214 // Ensure multi-line labels paired with images use their available width. |
| 215 if (GetTextMultiLine()) |
| 216 label_->SizeToFit(label_size.width()); |
| 217 label_size.set_width( |
| 218 std::min(label_size.width(), label_->GetPreferredSize().width())); |
| 219 } |
| 220 } |
| 221 |
| 222 gfx::Point image_origin(child_area.origin()); |
| 223 image_origin.Offset(0, (child_area.height() - image_size.height()) / 2); |
| 224 if (adjusted_alignment == gfx::ALIGN_CENTER) { |
| 225 const int total_width = image_size.width() + label_size.width() + |
| 226 ((image_size.width() > 0 && label_size.width() > 0) ? kSpacing : 0); |
| 227 image_origin.Offset((child_area.width() - total_width) / 2, 0); |
| 228 } else if (adjusted_alignment == gfx::ALIGN_RIGHT) { |
| 229 image_origin.Offset(child_area.width() - image_size.width(), 0); |
| 230 } |
| 231 |
| 232 gfx::Point label_origin(child_area.origin()); |
| 233 if (!image_size.IsEmpty() &&adjusted_alignment != gfx::ALIGN_RIGHT) |
| 234 label_origin.set_x(image_origin.x() + image_size.width() + kSpacing); |
| 235 |
| 236 image_->SetBoundsRect(gfx::Rect(image_origin, image_size)); |
| 237 label_->SetBoundsRect(gfx::Rect(label_origin, label_size)); |
| 238 } |
| 239 |
193 const char* LabelButton::GetClassName() const { | 240 const char* LabelButton::GetClassName() const { |
194 return kViewClassName; | 241 return kViewClassName; |
195 } | 242 } |
196 | 243 |
| 244 void LabelButton::GetExtraParams(ui::NativeTheme::ExtraParams* params) const { |
| 245 params->button.checked = false; |
| 246 params->button.indeterminate = false; |
| 247 params->button.is_default = is_default_; |
| 248 params->button.is_focused = HasFocus() && IsAccessibilityFocusable(); |
| 249 params->button.has_border = style() == STYLE_NATIVE_TEXTBUTTON; |
| 250 params->button.classic_state = 0; |
| 251 params->button.background_color = GetNativeTheme()->GetSystemColor( |
| 252 ui::NativeTheme::kColorId_ButtonBackgroundColor); |
| 253 } |
| 254 |
| 255 void LabelButton::UpdateImage() { |
| 256 image_->SetImage(GetImage(state())); |
| 257 } |
| 258 |
197 void LabelButton::ResetColorsFromNativeTheme() { | 259 void LabelButton::ResetColorsFromNativeTheme() { |
198 const ui::NativeTheme* theme = GetNativeTheme(); | 260 const ui::NativeTheme* theme = GetNativeTheme(); |
199 SkColor colors[STATE_COUNT] = { | 261 SkColor colors[STATE_COUNT] = { |
200 theme->GetSystemColor(ui::NativeTheme::kColorId_ButtonEnabledColor), | 262 theme->GetSystemColor(ui::NativeTheme::kColorId_ButtonEnabledColor), |
201 theme->GetSystemColor(ui::NativeTheme::kColorId_ButtonHoverColor), | 263 theme->GetSystemColor(ui::NativeTheme::kColorId_ButtonHoverColor), |
202 theme->GetSystemColor(ui::NativeTheme::kColorId_ButtonHoverColor), | 264 theme->GetSystemColor(ui::NativeTheme::kColorId_ButtonHoverColor), |
203 theme->GetSystemColor(ui::NativeTheme::kColorId_ButtonDisabledColor), | 265 theme->GetSystemColor(ui::NativeTheme::kColorId_ButtonDisabledColor), |
204 }; | 266 }; |
205 | 267 |
206 // Certain styles do not change text color when hovered or pressed. | 268 // Certain styles do not change text color when hovered or pressed. |
207 bool constant_text_color = style() == STYLE_BUTTON; | 269 bool constant_text_color = style() == STYLE_BUTTON; |
208 #if defined(OS_WIN) | 270 #if defined(OS_WIN) |
209 constant_text_color |= (style() == STYLE_NATIVE_TEXTBUTTON && | 271 constant_text_color |= (style() == STYLE_NATIVE_TEXTBUTTON && |
210 theme == ui::NativeThemeWin::instance()); | 272 theme == ui::NativeThemeWin::instance()); |
211 #endif | 273 #endif |
212 if (constant_text_color) | 274 if (constant_text_color) |
213 colors[STATE_HOVERED] = colors[STATE_PRESSED] = colors[STATE_NORMAL]; | 275 colors[STATE_HOVERED] = colors[STATE_PRESSED] = colors[STATE_NORMAL]; |
214 | 276 |
215 for (size_t state = STATE_NORMAL; state < STATE_COUNT; ++state) { | 277 for (size_t state = STATE_NORMAL; state < STATE_COUNT; ++state) { |
216 if (!explicitly_set_colors_[state]) { | 278 if (!explicitly_set_colors_[state]) { |
217 SetTextColor(static_cast<ButtonState>(state), colors[state]); | 279 SetTextColor(static_cast<ButtonState>(state), colors[state]); |
218 explicitly_set_colors_[state] = false; | 280 explicitly_set_colors_[state] = false; |
219 } | 281 } |
220 } | 282 } |
221 } | 283 } |
222 | 284 |
223 void LabelButton::StateChanged() { | 285 void LabelButton::StateChanged() { |
224 const gfx::Size previous_image_size(image_->GetPreferredSize()); | 286 const gfx::Size previous_image_size(image_->GetPreferredSize()); |
225 image_->SetImage(GetImage(state())); | 287 UpdateImage(); |
226 const SkColor color = button_state_colors_[state()]; | 288 const SkColor color = button_state_colors_[state()]; |
227 if (state() != STATE_DISABLED && label_->enabled_color() != color) | 289 if (state() != STATE_DISABLED && label_->enabled_color() != color) |
228 label_->SetEnabledColor(color); | 290 label_->SetEnabledColor(color); |
229 label_->SetEnabled(state() != STATE_DISABLED); | 291 label_->SetEnabled(state() != STATE_DISABLED); |
230 if (image_->GetPreferredSize() != previous_image_size) | 292 if (image_->GetPreferredSize() != previous_image_size) |
231 Layout(); | 293 Layout(); |
232 } | 294 } |
233 | 295 |
234 void LabelButton::Layout() { | |
235 gfx::Rect child_area(GetLocalBounds()); | |
236 child_area.Inset(GetInsets()); | |
237 | |
238 gfx::Size image_size(image_->GetPreferredSize()); | |
239 image_size.set_width(std::min(image_size.width(), child_area.width())); | |
240 image_size.set_height(std::min(image_size.height(), child_area.height())); | |
241 | |
242 // The label takes any remaining width after sizing the image, unless both | |
243 // views are centered. In that case, using the tighter preferred label width | |
244 // avoids wasted space within the label that would look like awkward padding. | |
245 gfx::Size label_size(child_area.size()); | |
246 if (!image_size.IsEmpty() && !label_size.IsEmpty()) { | |
247 label_size.set_width( | |
248 std::max(child_area.width() - image_size.width() - kSpacing, 0)); | |
249 if (GetHorizontalAlignment() == gfx::ALIGN_CENTER) { | |
250 // Ensure multi-line labels paired with images use their available width. | |
251 if (GetTextMultiLine()) | |
252 label_->SizeToFit(label_size.width()); | |
253 label_size.set_width( | |
254 std::min(label_size.width(), label_->GetPreferredSize().width())); | |
255 } | |
256 } | |
257 | |
258 gfx::Point image_origin(child_area.origin()); | |
259 image_origin.Offset(0, (child_area.height() - image_size.height()) / 2); | |
260 if (GetHorizontalAlignment() == gfx::ALIGN_CENTER) { | |
261 const int total_width = image_size.width() + label_size.width() + | |
262 ((image_size.width() > 0 && label_size.width() > 0) ? kSpacing : 0); | |
263 image_origin.Offset((child_area.width() - total_width) / 2, 0); | |
264 } else if (GetHorizontalAlignment() == gfx::ALIGN_RIGHT) { | |
265 image_origin.Offset(child_area.width() - image_size.width(), 0); | |
266 } | |
267 | |
268 gfx::Point label_origin(child_area.origin()); | |
269 if (!image_size.IsEmpty() && GetHorizontalAlignment() != gfx::ALIGN_RIGHT) | |
270 label_origin.set_x(image_origin.x() + image_size.width() + kSpacing); | |
271 | |
272 image_->SetBoundsRect(gfx::Rect(image_origin, image_size)); | |
273 label_->SetBoundsRect(gfx::Rect(label_origin, label_size)); | |
274 } | |
275 | |
276 void LabelButton::ChildPreferredSizeChanged(View* child) { | 296 void LabelButton::ChildPreferredSizeChanged(View* child) { |
277 PreferredSizeChanged(); | 297 PreferredSizeChanged(); |
278 } | 298 } |
279 | 299 |
280 void LabelButton::OnNativeThemeChanged(const ui::NativeTheme* theme) { | 300 void LabelButton::OnNativeThemeChanged(const ui::NativeTheme* theme) { |
281 ResetColorsFromNativeTheme(); | 301 ResetColorsFromNativeTheme(); |
282 } | 302 } |
283 | 303 |
284 ui::NativeTheme::Part LabelButton::GetThemePart() const { | 304 ui::NativeTheme::Part LabelButton::GetThemePart() const { |
285 return ui::NativeTheme::kPushButton; | 305 return ui::NativeTheme::kPushButton; |
286 } | 306 } |
287 | 307 |
288 gfx::Rect LabelButton::GetThemePaintRect() const { | 308 gfx::Rect LabelButton::GetThemePaintRect() const { |
289 return GetLocalBounds(); | 309 return GetLocalBounds(); |
290 } | 310 } |
291 | 311 |
292 ui::NativeTheme::State LabelButton::GetThemeState( | 312 ui::NativeTheme::State LabelButton::GetThemeState( |
293 ui::NativeTheme::ExtraParams* params) const { | 313 ui::NativeTheme::ExtraParams* params) const { |
294 GetExtraParams(params); | 314 GetExtraParams(params); |
295 switch(state()) { | 315 switch (state()) { |
296 case STATE_NORMAL: return ui::NativeTheme::kNormal; | 316 case STATE_NORMAL: return ui::NativeTheme::kNormal; |
297 case STATE_HOVERED: return ui::NativeTheme::kHovered; | 317 case STATE_HOVERED: return ui::NativeTheme::kHovered; |
298 case STATE_PRESSED: return ui::NativeTheme::kPressed; | 318 case STATE_PRESSED: return ui::NativeTheme::kPressed; |
299 case STATE_DISABLED: return ui::NativeTheme::kDisabled; | 319 case STATE_DISABLED: return ui::NativeTheme::kDisabled; |
300 case STATE_COUNT: NOTREACHED() << "Unknown state: " << state(); | 320 case STATE_COUNT: NOTREACHED() << "Unknown state: " << state(); |
301 } | 321 } |
302 return ui::NativeTheme::kNormal; | 322 return ui::NativeTheme::kNormal; |
303 } | 323 } |
304 | 324 |
305 const ui::Animation* LabelButton::GetThemeAnimation() const { | 325 const ui::Animation* LabelButton::GetThemeAnimation() const { |
(...skipping 12 matching lines...) Expand all Loading... |
318 GetExtraParams(params); | 338 GetExtraParams(params); |
319 return ui::NativeTheme::kNormal; | 339 return ui::NativeTheme::kNormal; |
320 } | 340 } |
321 | 341 |
322 ui::NativeTheme::State LabelButton::GetForegroundThemeState( | 342 ui::NativeTheme::State LabelButton::GetForegroundThemeState( |
323 ui::NativeTheme::ExtraParams* params) const { | 343 ui::NativeTheme::ExtraParams* params) const { |
324 GetExtraParams(params); | 344 GetExtraParams(params); |
325 return ui::NativeTheme::kHovered; | 345 return ui::NativeTheme::kHovered; |
326 } | 346 } |
327 | 347 |
328 void LabelButton::GetExtraParams(ui::NativeTheme::ExtraParams* params) const { | |
329 params->button.checked = false; | |
330 params->button.indeterminate = false; | |
331 params->button.is_default = is_default_; | |
332 params->button.is_focused = HasFocus() && IsAccessibilityFocusable(); | |
333 params->button.has_border = style() == STYLE_NATIVE_TEXTBUTTON; | |
334 params->button.classic_state = 0; | |
335 params->button.background_color = GetNativeTheme()->GetSystemColor( | |
336 ui::NativeTheme::kColorId_ButtonBackgroundColor); | |
337 } | |
338 | |
339 } // namespace views | 348 } // namespace views |
OLD | NEW |