| 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 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 const char* LabelButton::GetClassName() const { | 193 const char* LabelButton::GetClassName() const { |
| 194 return kViewClassName; | 194 return kViewClassName; |
| 195 } | 195 } |
| 196 | 196 |
| 197 void LabelButton::GetExtraParams(ui::NativeTheme::ExtraParams* params) const { |
| 198 params->button.checked = false; |
| 199 params->button.indeterminate = false; |
| 200 params->button.is_default = is_default_; |
| 201 params->button.is_focused = HasFocus() && IsAccessibilityFocusable(); |
| 202 params->button.has_border = style() == STYLE_NATIVE_TEXTBUTTON; |
| 203 params->button.classic_state = 0; |
| 204 params->button.background_color = GetNativeTheme()->GetSystemColor( |
| 205 ui::NativeTheme::kColorId_ButtonBackgroundColor); |
| 206 } |
| 207 |
| 208 void LabelButton::UpdateImage() { |
| 209 image_->SetImage(GetImage(state())); |
| 210 } |
| 211 |
| 197 void LabelButton::ResetColorsFromNativeTheme() { | 212 void LabelButton::ResetColorsFromNativeTheme() { |
| 198 const ui::NativeTheme* theme = GetNativeTheme(); | 213 const ui::NativeTheme* theme = GetNativeTheme(); |
| 199 SkColor colors[STATE_COUNT] = { | 214 SkColor colors[STATE_COUNT] = { |
| 200 theme->GetSystemColor(ui::NativeTheme::kColorId_ButtonEnabledColor), | 215 theme->GetSystemColor(ui::NativeTheme::kColorId_ButtonEnabledColor), |
| 201 theme->GetSystemColor(ui::NativeTheme::kColorId_ButtonHoverColor), | 216 theme->GetSystemColor(ui::NativeTheme::kColorId_ButtonHoverColor), |
| 202 theme->GetSystemColor(ui::NativeTheme::kColorId_ButtonHoverColor), | 217 theme->GetSystemColor(ui::NativeTheme::kColorId_ButtonHoverColor), |
| 203 theme->GetSystemColor(ui::NativeTheme::kColorId_ButtonDisabledColor), | 218 theme->GetSystemColor(ui::NativeTheme::kColorId_ButtonDisabledColor), |
| 204 }; | 219 }; |
| 205 | 220 |
| 206 // Certain styles do not change text color when hovered or pressed. | 221 // Certain styles do not change text color when hovered or pressed. |
| 207 bool constant_text_color = style() == STYLE_BUTTON; | 222 bool constant_text_color = style() == STYLE_BUTTON; |
| 208 #if defined(OS_WIN) | 223 #if defined(OS_WIN) |
| 209 constant_text_color |= (style() == STYLE_NATIVE_TEXTBUTTON && | 224 constant_text_color |= (style() == STYLE_NATIVE_TEXTBUTTON && |
| 210 theme == ui::NativeThemeWin::instance()); | 225 theme == ui::NativeThemeWin::instance()); |
| 211 #endif | 226 #endif |
| 212 if (constant_text_color) | 227 if (constant_text_color) |
| 213 colors[STATE_HOVERED] = colors[STATE_PRESSED] = colors[STATE_NORMAL]; | 228 colors[STATE_HOVERED] = colors[STATE_PRESSED] = colors[STATE_NORMAL]; |
| 214 | 229 |
| 215 for (size_t state = STATE_NORMAL; state < STATE_COUNT; ++state) { | 230 for (size_t state = STATE_NORMAL; state < STATE_COUNT; ++state) { |
| 216 if (!explicitly_set_colors_[state]) { | 231 if (!explicitly_set_colors_[state]) { |
| 217 SetTextColor(static_cast<ButtonState>(state), colors[state]); | 232 SetTextColor(static_cast<ButtonState>(state), colors[state]); |
| 218 explicitly_set_colors_[state] = false; | 233 explicitly_set_colors_[state] = false; |
| 219 } | 234 } |
| 220 } | 235 } |
| 221 } | 236 } |
| 222 | 237 |
| 223 void LabelButton::StateChanged() { | 238 void LabelButton::StateChanged() { |
| 224 const gfx::Size previous_image_size(image_->GetPreferredSize()); | 239 const gfx::Size previous_image_size(image_->GetPreferredSize()); |
| 225 image_->SetImage(GetImage(state())); | 240 UpdateImage(); |
| 226 const SkColor color = button_state_colors_[state()]; | 241 const SkColor color = button_state_colors_[state()]; |
| 227 if (state() != STATE_DISABLED && label_->enabled_color() != color) | 242 if (state() != STATE_DISABLED && label_->enabled_color() != color) |
| 228 label_->SetEnabledColor(color); | 243 label_->SetEnabledColor(color); |
| 229 label_->SetEnabled(state() != STATE_DISABLED); | 244 label_->SetEnabled(state() != STATE_DISABLED); |
| 230 if (image_->GetPreferredSize() != previous_image_size) | 245 if (image_->GetPreferredSize() != previous_image_size) |
| 231 Layout(); | 246 Layout(); |
| 232 } | 247 } |
| 233 | 248 |
| 234 void LabelButton::Layout() { | 249 void LabelButton::Layout() { |
| 235 gfx::Rect child_area(GetLocalBounds()); | 250 gfx::Rect child_area(GetLocalBounds()); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 285 return ui::NativeTheme::kPushButton; | 300 return ui::NativeTheme::kPushButton; |
| 286 } | 301 } |
| 287 | 302 |
| 288 gfx::Rect LabelButton::GetThemePaintRect() const { | 303 gfx::Rect LabelButton::GetThemePaintRect() const { |
| 289 return GetLocalBounds(); | 304 return GetLocalBounds(); |
| 290 } | 305 } |
| 291 | 306 |
| 292 ui::NativeTheme::State LabelButton::GetThemeState( | 307 ui::NativeTheme::State LabelButton::GetThemeState( |
| 293 ui::NativeTheme::ExtraParams* params) const { | 308 ui::NativeTheme::ExtraParams* params) const { |
| 294 GetExtraParams(params); | 309 GetExtraParams(params); |
| 295 switch(state()) { | 310 switch (state()) { |
| 296 case STATE_NORMAL: return ui::NativeTheme::kNormal; | 311 case STATE_NORMAL: return ui::NativeTheme::kNormal; |
| 297 case STATE_HOVERED: return ui::NativeTheme::kHovered; | 312 case STATE_HOVERED: return ui::NativeTheme::kHovered; |
| 298 case STATE_PRESSED: return ui::NativeTheme::kPressed; | 313 case STATE_PRESSED: return ui::NativeTheme::kPressed; |
| 299 case STATE_DISABLED: return ui::NativeTheme::kDisabled; | 314 case STATE_DISABLED: return ui::NativeTheme::kDisabled; |
| 300 case STATE_COUNT: NOTREACHED() << "Unknown state: " << state(); | 315 case STATE_COUNT: NOTREACHED() << "Unknown state: " << state(); |
| 301 } | 316 } |
| 302 return ui::NativeTheme::kNormal; | 317 return ui::NativeTheme::kNormal; |
| 303 } | 318 } |
| 304 | 319 |
| 305 const ui::Animation* LabelButton::GetThemeAnimation() const { | 320 const ui::Animation* LabelButton::GetThemeAnimation() const { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 318 GetExtraParams(params); | 333 GetExtraParams(params); |
| 319 return ui::NativeTheme::kNormal; | 334 return ui::NativeTheme::kNormal; |
| 320 } | 335 } |
| 321 | 336 |
| 322 ui::NativeTheme::State LabelButton::GetForegroundThemeState( | 337 ui::NativeTheme::State LabelButton::GetForegroundThemeState( |
| 323 ui::NativeTheme::ExtraParams* params) const { | 338 ui::NativeTheme::ExtraParams* params) const { |
| 324 GetExtraParams(params); | 339 GetExtraParams(params); |
| 325 return ui::NativeTheme::kHovered; | 340 return ui::NativeTheme::kHovered; |
| 326 } | 341 } |
| 327 | 342 |
| 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 | 343 } // namespace views |
| OLD | NEW |