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/gfx/canvas.h" |
| 12 #include "ui/gfx/image/image.h" |
| 13 #include "ui/views/controls/button/button_border.h" |
| 14 #include "ui/views/controls/button/button.h" |
| 15 #include "ui/views/focus_border.h" |
| 16 #include "ui/views/layout/grid_layout.h" |
| 17 #include "ui/views/widget/widget.h" |
| 18 |
| 19 #if defined(OS_WIN) |
| 20 #include "ui/gfx/color_utils.h" |
| 21 #include "ui/base/native_theme/native_theme_win.h" |
| 22 #endif |
| 23 |
| 24 namespace views { |
| 25 |
| 26 namespace { |
| 27 |
| 28 // The spacing between the icon and text. |
| 29 static const int kSpacing = 5; |
| 30 |
| 31 // The length of the hover fade animation. |
| 32 static const int kHoverAnimationDurationMs = 170; |
| 33 |
| 34 } // namespace |
| 35 |
| 36 LabelButton::LabelButton(ButtonListener* listener, const string16& text) |
| 37 : CustomButton(listener), |
| 38 image_(new ImageView()), |
| 39 label_(new Label(text)), |
| 40 default_button_(false), |
| 41 native_theme_(false) { |
| 42 set_border(new ButtonBorder(this)); |
| 43 // Inset the button focus rect from the actual border; match Windows roughly. |
| 44 set_focus_border(FocusBorder::CreateDashedFocusBorder(3, 3, 3, 3)); |
| 45 SetAnimationDuration(kHoverAnimationDurationMs); |
| 46 |
| 47 image_->set_owned_by_client(); |
| 48 image_->set_interactive(false); |
| 49 |
| 50 label_->set_owned_by_client(); |
| 51 label_->SetAutoColorReadabilityEnabled(false); |
| 52 label_->SetHorizontalAlignment(Label::ALIGN_LEFT); |
| 53 label_->SetElideBehavior(Label::ELIDE_AT_END); |
| 54 |
| 55 // Initialize the colors and layout for a Views-themed button. |
| 56 SetNativeTheme(false); |
| 57 } |
| 58 |
| 59 LabelButton::~LabelButton() {} |
| 60 |
| 61 const gfx::ImageSkia& LabelButton::GetImage(ButtonState for_state) { |
| 62 if (for_state != BS_NORMAL && button_state_images_[for_state].isNull()) |
| 63 return button_state_images_[BS_NORMAL]; |
| 64 return button_state_images_[for_state]; |
| 65 } |
| 66 |
| 67 void LabelButton::SetImage(ButtonState for_state, const gfx::ImageSkia& image) { |
| 68 const bool was_empty = GetImage(state()).isNull(); |
| 69 button_state_images_[for_state] = image; |
| 70 if (was_empty != GetImage(state()).isNull()) |
| 71 UpdateLayout(); |
| 72 } |
| 73 |
| 74 Label* LabelButton::GetLabel() const { |
| 75 return label_.get(); |
| 76 } |
| 77 |
| 78 void LabelButton::SetText(const string16& text) { |
| 79 const bool was_empty = GetLabel()->text().empty(); |
| 80 GetLabel()->SetText(text); |
| 81 if (was_empty != GetLabel()->text().empty()) |
| 82 UpdateLayout(); |
| 83 } |
| 84 |
| 85 void LabelButton::SetTextColor(ButtonState for_state, SkColor color) { |
| 86 button_state_colors_[for_state] = color; |
| 87 if (for_state == BS_DISABLED) |
| 88 GetLabel()->SetDisabledColor(color); |
| 89 else if (for_state == state()) |
| 90 GetLabel()->SetEnabledColor(color); |
| 91 } |
| 92 |
| 93 Label::Alignment LabelButton::GetHorizontalAlignment() const { |
| 94 return GetLabel()->horizontal_alignment(); |
| 95 } |
| 96 |
| 97 void LabelButton::SetHorizontalAlignment(Label::Alignment alignment) { |
| 98 GetLabel()->SetHorizontalAlignment(alignment); |
| 99 UpdateLayout(); |
| 100 } |
| 101 |
| 102 void LabelButton::SetDefaultButton(bool default_button) { |
| 103 if (default_button == default_button_) |
| 104 return; |
| 105 default_button_ = default_button; |
| 106 ui::Accelerator accel(ui::VKEY_RETURN, ui::EF_NONE); |
| 107 default_button_ ? AddAccelerator(accel) : RemoveAccelerator(accel); |
| 108 } |
| 109 |
| 110 void LabelButton::SetNativeTheme(bool native_theme) { |
| 111 native_theme_ = native_theme; |
| 112 static_cast<ButtonBorder*>(border())->set_native_theme(native_theme); |
| 113 |
| 114 const ui::NativeTheme* theme = ui::NativeTheme::instance(); |
| 115 SkColor colors[BS_COUNT] = { |
| 116 theme->GetSystemColor(ui::NativeTheme::kColorId_TextButtonEnabledColor), |
| 117 theme->GetSystemColor(ui::NativeTheme::kColorId_TextButtonHoverColor), |
| 118 theme->GetSystemColor(ui::NativeTheme::kColorId_TextButtonHoverColor), |
| 119 theme->GetSystemColor(ui::NativeTheme::kColorId_TextButtonDisabledColor), |
| 120 }; |
| 121 #if defined(OS_WIN) |
| 122 if (native_theme) { |
| 123 colors[BS_NORMAL] = color_utils::GetSysSkColor(COLOR_BTNTEXT); |
| 124 colors[BS_HOT] = color_utils::GetSysSkColor(COLOR_BTNTEXT); |
| 125 colors[BS_PUSHED] = color_utils::GetSysSkColor(COLOR_BTNTEXT); |
| 126 colors[BS_DISABLED] = color_utils::GetSysSkColor(COLOR_GRAYTEXT); |
| 127 } |
| 128 #endif |
| 129 for (size_t state = BS_NORMAL; state < BS_COUNT; state++) |
| 130 SetTextColor(static_cast<ButtonState>(state), colors[state]); |
| 131 |
| 132 UpdateLayout(); |
| 133 } |
| 134 |
| 135 void LabelButton::StateChanged() { |
| 136 const bool was_empty = image_->GetImage().isNull(); |
| 137 image_->SetImage(GetImage(state())); |
| 138 if (was_empty != image_->GetImage().isNull()) |
| 139 UpdateLayout(); |
| 140 const SkColor color = button_state_colors_[state()]; |
| 141 if (state() != BS_DISABLED && GetLabel()->enabled_color() != color) |
| 142 GetLabel()->SetEnabledColor(color); |
| 143 } |
| 144 |
| 145 gfx::Size LabelButton::GetPreferredSize() { |
| 146 gfx::Size size = label_->GetPreferredSize(); |
| 147 size.set_height(std::max(size.height(), GetImage(state()).height())); |
| 148 size.Enlarge(GetInsets().width() + GetImage(state()).width() + kSpacing, |
| 149 GetInsets().height()); |
| 150 |
| 151 // Increase the minimum size monotonically with the preferred size. |
| 152 size.SetSize(std::max(min_size_.width(), size.width()), |
| 153 std::max(min_size_.height(), size.height())); |
| 154 min_size_ = size; |
| 155 |
| 156 // Return the largest known size clamped to the maximum size (if valid). |
| 157 if (max_size_.width() > 0) |
| 158 size.set_width(std::min(max_size_.width(), size.width())); |
| 159 if (max_size_.height() > 0) |
| 160 size.set_height(std::min(max_size_.height(), size.height())); |
| 161 return size; |
| 162 } |
| 163 |
| 164 std::string LabelButton::GetClassName() const { |
| 165 return "views/LabelButton"; |
| 166 } |
| 167 |
| 168 void LabelButton::ChildPreferredSizeChanged(View* child) { |
| 169 PreferredSizeChanged(); |
| 170 } |
| 171 |
| 172 ui::NativeTheme::Part LabelButton::GetThemePart() const { |
| 173 return ui::NativeTheme::kPushButton; |
| 174 } |
| 175 |
| 176 gfx::Rect LabelButton::GetThemePaintRect() const { |
| 177 return GetLocalBounds(); |
| 178 } |
| 179 |
| 180 ui::NativeTheme::State LabelButton::GetThemeState( |
| 181 ui::NativeTheme::ExtraParams* params) const { |
| 182 GetExtraParams(params); |
| 183 switch(state()) { |
| 184 case BS_NORMAL: |
| 185 return ui::NativeTheme::kNormal; |
| 186 case BS_HOT: |
| 187 return ui::NativeTheme::kHovered; |
| 188 case BS_PUSHED: |
| 189 return ui::NativeTheme::kPressed; |
| 190 case BS_DISABLED: |
| 191 return ui::NativeTheme::kDisabled; |
| 192 case BS_COUNT: |
| 193 NOTREACHED() << "Unknown state: " << state(); |
| 194 } |
| 195 return ui::NativeTheme::kNormal; |
| 196 } |
| 197 |
| 198 const ui::Animation* LabelButton::GetThemeAnimation() const { |
| 199 #if defined(OS_WIN) && !defined(USE_AURA) |
| 200 if (!ui::NativeThemeWin::instance()->IsThemingActive()) |
| 201 return NULL; |
| 202 #endif |
| 203 return hover_animation_.get(); |
| 204 } |
| 205 |
| 206 ui::NativeTheme::State LabelButton::GetBackgroundThemeState( |
| 207 ui::NativeTheme::ExtraParams* params) const { |
| 208 GetExtraParams(params); |
| 209 return ui::NativeTheme::kNormal; |
| 210 } |
| 211 |
| 212 ui::NativeTheme::State LabelButton::GetForegroundThemeState( |
| 213 ui::NativeTheme::ExtraParams* params) const { |
| 214 GetExtraParams(params); |
| 215 return ui::NativeTheme::kHovered; |
| 216 } |
| 217 |
| 218 void LabelButton::GetExtraParams(ui::NativeTheme::ExtraParams* params) const { |
| 219 params->button.checked = false; |
| 220 params->button.indeterminate = false; |
| 221 params->button.is_default = default_button_; |
| 222 params->button.is_focused = HasFocus() && IsAccessibilityFocusable(); |
| 223 params->button.has_border = native_theme(); |
| 224 params->button.classic_state = 0; |
| 225 params->button.background_color = ui::NativeTheme::instance()->GetSystemColor( |
| 226 ui::NativeTheme::kColorId_TextButtonBackgroundColor); |
| 227 } |
| 228 |
| 229 void LabelButton::UpdateLayout() { |
| 230 // Temporarily disregard state changes while updating layout. |
| 231 const ButtonState saved_state = state(); |
| 232 SetState(BS_DISABLED); |
| 233 |
| 234 RemoveAllChildViews(false); |
| 235 GridLayout* layout = GridLayout::CreatePanel(this); |
| 236 ColumnSet* columns = layout->AddColumnSet(0); |
| 237 layout->SetInsets(GetInsets()); |
| 238 SetLayoutManager(layout); |
| 239 |
| 240 const bool image = !image_->GetImage().isNull(); |
| 241 // Layout with the label unless it is empty and the alignment is centered. |
| 242 const bool label = !GetLabel()->text().empty() || |
| 243 (GetHorizontalAlignment() != Label::ALIGN_CENTER); |
| 244 |
| 245 if (image) { |
| 246 if (GetHorizontalAlignment() == Label::ALIGN_CENTER) |
| 247 columns->AddPaddingColumn(1, 0); |
| 248 if (GetHorizontalAlignment() != Label::ALIGN_RIGHT) |
| 249 columns->AddColumn(GridLayout::CENTER, GridLayout::CENTER, 0, |
| 250 GridLayout::USE_PREF, 0, 0); |
| 251 } |
| 252 if (label) { |
| 253 if (image && GetHorizontalAlignment() != Label::ALIGN_RIGHT) |
| 254 columns->AddPaddingColumn(0, kSpacing); |
| 255 // Caution, centering a wide image and label pair is not well supported. |
| 256 // Either view may appear truncated, and the label will not properly elide. |
| 257 if (image && GetHorizontalAlignment() == Label::ALIGN_CENTER) |
| 258 columns->AddColumn(GridLayout::CENTER, GridLayout::CENTER, 0, |
| 259 GridLayout::USE_PREF, 0, 0); |
| 260 else |
| 261 columns->AddColumn(GridLayout::FILL, GridLayout::CENTER, 1, |
| 262 GridLayout::FIXED, 0, 0); |
| 263 if (image && GetHorizontalAlignment() == Label::ALIGN_RIGHT) |
| 264 columns->AddPaddingColumn(0, kSpacing); |
| 265 } |
| 266 if (image) { |
| 267 if (GetHorizontalAlignment() == Label::ALIGN_RIGHT) |
| 268 columns->AddColumn(GridLayout::CENTER, GridLayout::CENTER, 0, |
| 269 GridLayout::USE_PREF, 0, 0); |
| 270 else if (GetHorizontalAlignment() == Label::ALIGN_CENTER) |
| 271 columns->AddPaddingColumn(1, 0); |
| 272 } |
| 273 |
| 274 layout->StartRow(1, 0); |
| 275 if (image && GetHorizontalAlignment() != Label::ALIGN_RIGHT) |
| 276 layout->AddView(image_.get()); |
| 277 if (label) |
| 278 layout->AddView(label_.get()); |
| 279 if (image && GetHorizontalAlignment() == Label::ALIGN_RIGHT) |
| 280 layout->AddView(image_.get()); |
| 281 |
| 282 SetState(saved_state); |
| 283 } |
| 284 |
| 285 } // namespace views |
OLD | NEW |