OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ui/views/controls/button/label_button.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "grit/ui_resources.h" |
| 9 #include "ui/base/animation/throb_animation.h" |
| 10 #include "ui/base/resource/resource_bundle.h" |
| 11 #include "ui/views/controls/button/label_button_border.h" |
| 12 #include "ui/views/focus_border.h" |
| 13 |
| 14 #if defined(OS_WIN) |
| 15 #include "ui/gfx/color_utils.h" |
| 16 #include "ui/base/native_theme/native_theme_win.h" |
| 17 #endif |
| 18 |
| 19 namespace views { |
| 20 |
| 21 namespace { |
| 22 |
| 23 // The spacing between the icon and text. |
| 24 static const int kSpacing = 5; |
| 25 |
| 26 // The length of the hover fade animation. |
| 27 static const int kHoverAnimationDurationMs = 170; |
| 28 |
| 29 } // namespace |
| 30 |
| 31 LabelButton::LabelButton(ButtonListener* listener, const string16& text) |
| 32 : CustomButton(listener), |
| 33 image_(new ImageView()), |
| 34 label_(new Label(text)), |
| 35 default_button_(false), |
| 36 native_theme_(false) { |
| 37 set_border(new LabelButtonBorder(this)); |
| 38 // Inset the button focus rect from the actual border; roughly match Windows. |
| 39 set_focus_border(FocusBorder::CreateDashedFocusBorder(3, 3, 3, 3)); |
| 40 SetAnimationDuration(kHoverAnimationDurationMs); |
| 41 |
| 42 AddChildView(image_); |
| 43 image_->set_interactive(false); |
| 44 |
| 45 AddChildView(label_); |
| 46 label_->SetAutoColorReadabilityEnabled(false); |
| 47 label_->SetHorizontalAlignment(Label::ALIGN_LEFT); |
| 48 label_->SetElideBehavior(Label::ELIDE_AT_END); |
| 49 |
| 50 // Initialize the colors, border, and layout for a Views-themed button. |
| 51 SetNativeTheme(false); |
| 52 } |
| 53 |
| 54 LabelButton::~LabelButton() {} |
| 55 |
| 56 const gfx::ImageSkia& LabelButton::GetImage(ButtonState for_state) { |
| 57 if (for_state != BS_NORMAL && button_state_images_[for_state].isNull()) |
| 58 return button_state_images_[BS_NORMAL]; |
| 59 return button_state_images_[for_state]; |
| 60 } |
| 61 |
| 62 void LabelButton::SetImage(ButtonState for_state, const gfx::ImageSkia& image) { |
| 63 button_state_images_[for_state] = image; |
| 64 image_->SetImage(GetImage(state())); |
| 65 } |
| 66 |
| 67 const string16& LabelButton::GetText() const { |
| 68 return label_->text(); |
| 69 } |
| 70 |
| 71 void LabelButton::SetText(const string16& text) { |
| 72 label_->SetText(text); |
| 73 } |
| 74 |
| 75 void LabelButton::SetTextColor(ButtonState for_state, SkColor color) { |
| 76 button_state_colors_[for_state] = color; |
| 77 if (for_state == BS_DISABLED) |
| 78 label_->SetDisabledColor(color); |
| 79 else if (for_state == state()) |
| 80 label_->SetEnabledColor(color); |
| 81 } |
| 82 |
| 83 bool LabelButton::GetTextMultiLine() const { |
| 84 return label_->is_multi_line(); |
| 85 } |
| 86 |
| 87 void LabelButton::SetTextMultiLine(bool text_multi_line) { |
| 88 label_->SetMultiLine(text_multi_line); |
| 89 } |
| 90 |
| 91 Label::Alignment LabelButton::GetHorizontalAlignment() const { |
| 92 return label_->horizontal_alignment(); |
| 93 } |
| 94 |
| 95 void LabelButton::SetHorizontalAlignment(Label::Alignment alignment) { |
| 96 label_->SetHorizontalAlignment(alignment); |
| 97 InvalidateLayout(); |
| 98 } |
| 99 |
| 100 void LabelButton::SetDefaultButton(bool default_button) { |
| 101 if (default_button == default_button_) |
| 102 return; |
| 103 default_button_ = default_button; |
| 104 ui::Accelerator accel(ui::VKEY_RETURN, ui::EF_NONE); |
| 105 default_button_ ? AddAccelerator(accel) : RemoveAccelerator(accel); |
| 106 } |
| 107 |
| 108 void LabelButton::SetNativeTheme(bool native_theme) { |
| 109 native_theme_ = native_theme; |
| 110 static_cast<LabelButtonBorder*>(border())->set_native_theme(native_theme); |
| 111 |
| 112 const ui::NativeTheme* theme = ui::NativeTheme::instance(); |
| 113 SkColor colors[BS_COUNT] = { |
| 114 theme->GetSystemColor(ui::NativeTheme::kColorId_TextButtonEnabledColor), |
| 115 theme->GetSystemColor(ui::NativeTheme::kColorId_TextButtonHoverColor), |
| 116 theme->GetSystemColor(ui::NativeTheme::kColorId_TextButtonHoverColor), |
| 117 theme->GetSystemColor(ui::NativeTheme::kColorId_TextButtonDisabledColor), |
| 118 }; |
| 119 #if defined(OS_WIN) |
| 120 if (native_theme) { |
| 121 colors[BS_NORMAL] = color_utils::GetSysSkColor(COLOR_BTNTEXT); |
| 122 colors[BS_HOT] = color_utils::GetSysSkColor(COLOR_BTNTEXT); |
| 123 colors[BS_PUSHED] = color_utils::GetSysSkColor(COLOR_BTNTEXT); |
| 124 colors[BS_DISABLED] = color_utils::GetSysSkColor(COLOR_GRAYTEXT); |
| 125 } |
| 126 #endif |
| 127 for (size_t state = BS_NORMAL; state < BS_COUNT; ++state) |
| 128 SetTextColor(static_cast<ButtonState>(state), colors[state]); |
| 129 |
| 130 // Invalidate the layout to pickup the new insets from the border. |
| 131 InvalidateLayout(); |
| 132 } |
| 133 |
| 134 void LabelButton::StateChanged() { |
| 135 const gfx::Size previous_image_size(image_->GetPreferredSize()); |
| 136 image_->SetImage(GetImage(state())); |
| 137 const SkColor color = button_state_colors_[state()]; |
| 138 if (state() != BS_DISABLED && label_->enabled_color() != color) |
| 139 label_->SetEnabledColor(color); |
| 140 if (image_->GetPreferredSize() != previous_image_size) |
| 141 Layout(); |
| 142 } |
| 143 |
| 144 gfx::Size LabelButton::GetPreferredSize() { |
| 145 gfx::Size size(label_->GetPreferredSize()); |
| 146 // TODO(msw): Multi-line labels are cut off at their preferred bounds. |
| 147 // This apparent bug in CanvasSkia requires investigation. |
| 148 size.set_width(size.width() * (GetTextMultiLine() ? 2 : 1)); |
| 149 const gfx::Size image_size(image_->GetPreferredSize()); |
| 150 if (image_size.width() > 0 && size.width() > 0) |
| 151 size.Enlarge(kSpacing, 0); |
| 152 size.set_height(std::max(size.height(), image_size.height())); |
| 153 size.Enlarge(image_size.width() + GetInsets().width(), GetInsets().height()); |
| 154 |
| 155 // Increase the minimum size monotonically with the preferred size. |
| 156 size.SetSize(std::max(min_size_.width(), size.width()), |
| 157 std::max(min_size_.height(), size.height())); |
| 158 min_size_ = size; |
| 159 |
| 160 // Return the largest known size clamped to the maximum size (if valid). |
| 161 if (max_size_.width() > 0) |
| 162 size.set_width(std::min(max_size_.width(), size.width())); |
| 163 if (max_size_.height() > 0) |
| 164 size.set_height(std::min(max_size_.height(), size.height())); |
| 165 return size; |
| 166 } |
| 167 |
| 168 void LabelButton::Layout() { |
| 169 gfx::Rect child_area(GetLocalBounds()); |
| 170 child_area.Inset(GetInsets()); |
| 171 |
| 172 gfx::Size image_size(image_->GetPreferredSize()); |
| 173 image_size.set_width(std::min(image_size.width(), child_area.width())); |
| 174 image_size.set_height(std::min(image_size.height(), child_area.height())); |
| 175 |
| 176 // The label takes any remaining width after sizing the image. |
| 177 gfx::Size label_size(child_area.size()); |
| 178 if (!image_size.IsEmpty() && !label_size.IsEmpty()) { |
| 179 label_size.set_width(child_area.width() - image_size.width() - kSpacing); |
| 180 if (GetHorizontalAlignment() == Label::ALIGN_CENTER) { |
| 181 gfx::Size preferred(label_->GetPreferredSize()); |
| 182 // TODO(msw): Multi-line labels are cut off at their preferred bounds. |
| 183 // This apparent bug in CanvasSkia requires investigation. |
| 184 preferred.set_width(preferred.width() * (GetTextMultiLine() ? 2 : 1)); |
| 185 label_size.set_width(std::min(label_size.width(), preferred.width())); |
| 186 } |
| 187 } |
| 188 |
| 189 gfx::Point image_origin(child_area.origin()); |
| 190 image_origin.Offset(0, (child_area.height() - image_size.height()) / 2); |
| 191 if (GetHorizontalAlignment() == Label::ALIGN_CENTER) { |
| 192 const int total_width = image_size.width() + label_size.width() + |
| 193 ((image_size.width() > 0 && label_size.width() > 0) ? kSpacing : 0); |
| 194 image_origin.Offset((child_area.width() - total_width) / 2, 0); |
| 195 } else if (GetHorizontalAlignment() == Label::ALIGN_RIGHT) { |
| 196 image_origin.Offset(child_area.width() - image_size.width(), 0); |
| 197 } |
| 198 |
| 199 gfx::Point label_origin(child_area.origin()); |
| 200 if (!image_size.IsEmpty() && GetHorizontalAlignment() != Label::ALIGN_RIGHT) |
| 201 label_origin.set_x(image_origin.x() + image_size.width() + kSpacing); |
| 202 |
| 203 image_->SetBoundsRect(gfx::Rect(image_origin, image_size)); |
| 204 label_->SetBoundsRect(gfx::Rect(label_origin, label_size)); |
| 205 } |
| 206 |
| 207 std::string LabelButton::GetClassName() const { |
| 208 return "views/LabelButton"; |
| 209 } |
| 210 |
| 211 void LabelButton::ChildPreferredSizeChanged(View* child) { |
| 212 PreferredSizeChanged(); |
| 213 } |
| 214 |
| 215 ui::NativeTheme::Part LabelButton::GetThemePart() const { |
| 216 return ui::NativeTheme::kPushButton; |
| 217 } |
| 218 |
| 219 gfx::Rect LabelButton::GetThemePaintRect() const { |
| 220 return GetLocalBounds(); |
| 221 } |
| 222 |
| 223 ui::NativeTheme::State LabelButton::GetThemeState( |
| 224 ui::NativeTheme::ExtraParams* params) const { |
| 225 GetExtraParams(params); |
| 226 switch(state()) { |
| 227 case BS_NORMAL: return ui::NativeTheme::kNormal; |
| 228 case BS_HOT: return ui::NativeTheme::kHovered; |
| 229 case BS_PUSHED: return ui::NativeTheme::kPressed; |
| 230 case BS_DISABLED: return ui::NativeTheme::kDisabled; |
| 231 case BS_COUNT: NOTREACHED() << "Unknown state: " << state(); |
| 232 } |
| 233 return ui::NativeTheme::kNormal; |
| 234 } |
| 235 |
| 236 const ui::Animation* LabelButton::GetThemeAnimation() const { |
| 237 #if defined(OS_WIN) && !defined(USE_AURA) |
| 238 if (!ui::NativeThemeWin::instance()->IsThemingActive()) |
| 239 return NULL; |
| 240 #endif |
| 241 return hover_animation_.get(); |
| 242 } |
| 243 |
| 244 ui::NativeTheme::State LabelButton::GetBackgroundThemeState( |
| 245 ui::NativeTheme::ExtraParams* params) const { |
| 246 GetExtraParams(params); |
| 247 return ui::NativeTheme::kNormal; |
| 248 } |
| 249 |
| 250 ui::NativeTheme::State LabelButton::GetForegroundThemeState( |
| 251 ui::NativeTheme::ExtraParams* params) const { |
| 252 GetExtraParams(params); |
| 253 return ui::NativeTheme::kHovered; |
| 254 } |
| 255 |
| 256 void LabelButton::GetExtraParams(ui::NativeTheme::ExtraParams* params) const { |
| 257 params->button.checked = false; |
| 258 params->button.indeterminate = false; |
| 259 params->button.is_default = default_button(); |
| 260 params->button.is_focused = HasFocus() && IsAccessibilityFocusable(); |
| 261 params->button.has_border = native_theme(); |
| 262 params->button.classic_state = 0; |
| 263 params->button.background_color = ui::NativeTheme::instance()->GetSystemColor( |
| 264 ui::NativeTheme::kColorId_TextButtonBackgroundColor); |
| 265 } |
| 266 |
| 267 } // namespace views |
OLD | NEW |