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 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 } | 118 } |
119 | 119 |
120 void LabelButton::SetNativeTheme(bool native_theme) { | 120 void LabelButton::SetNativeTheme(bool native_theme) { |
121 native_theme_ = native_theme; | 121 native_theme_ = native_theme; |
122 static_cast<LabelButtonBorder*>(border())->set_native_theme(native_theme); | 122 static_cast<LabelButtonBorder*>(border())->set_native_theme(native_theme); |
123 // Invalidate the layout to pickup the new insets from the border. | 123 // Invalidate the layout to pickup the new insets from the border. |
124 InvalidateLayout(); | 124 InvalidateLayout(); |
125 ResetColorsFromNativeTheme(); | 125 ResetColorsFromNativeTheme(); |
126 } | 126 } |
127 | 127 |
| 128 gfx::Size LabelButton::GetPreferredSize() { |
| 129 // Resize multi-line labels paired with images to use their available width. |
| 130 const gfx::Size image_size(image_->GetPreferredSize()); |
| 131 if (GetTextMultiLine() && !image_size.IsEmpty() && !GetText().empty() && |
| 132 GetHorizontalAlignment() == gfx::ALIGN_CENTER) { |
| 133 label_->SizeToFit(GetLocalBounds().width() - image_size.width() - kSpacing); |
| 134 } |
| 135 |
| 136 // Calculate the required size. |
| 137 gfx::Size size(label_->GetPreferredSize()); |
| 138 if (image_size.width() > 0 && size.width() > 0) |
| 139 size.Enlarge(kSpacing, 0); |
| 140 size.set_height(std::max(size.height(), image_size.height())); |
| 141 size.Enlarge(image_size.width() + GetInsets().width(), GetInsets().height()); |
| 142 |
| 143 // Increase the minimum size monotonically with the preferred size. |
| 144 size.SetSize(std::max(min_size_.width(), size.width()), |
| 145 std::max(min_size_.height(), size.height())); |
| 146 min_size_ = size; |
| 147 |
| 148 // Return the largest known size clamped to the maximum size (if valid). |
| 149 if (max_size_.width() > 0) |
| 150 size.set_width(std::min(max_size_.width(), size.width())); |
| 151 if (max_size_.height() > 0) |
| 152 size.set_height(std::min(max_size_.height(), size.height())); |
| 153 return size; |
| 154 } |
| 155 |
128 void LabelButton::ResetColorsFromNativeTheme() { | 156 void LabelButton::ResetColorsFromNativeTheme() { |
129 const ui::NativeTheme* theme = GetNativeTheme(); | 157 const ui::NativeTheme* theme = GetNativeTheme(); |
130 SkColor colors[STATE_COUNT] = { | 158 SkColor colors[STATE_COUNT] = { |
131 theme->GetSystemColor(ui::NativeTheme::kColorId_TextButtonEnabledColor), | 159 theme->GetSystemColor(ui::NativeTheme::kColorId_TextButtonEnabledColor), |
132 theme->GetSystemColor(ui::NativeTheme::kColorId_TextButtonHoverColor), | 160 theme->GetSystemColor(ui::NativeTheme::kColorId_TextButtonHoverColor), |
133 theme->GetSystemColor(ui::NativeTheme::kColorId_TextButtonHoverColor), | 161 theme->GetSystemColor(ui::NativeTheme::kColorId_TextButtonHoverColor), |
134 theme->GetSystemColor(ui::NativeTheme::kColorId_TextButtonDisabledColor), | 162 theme->GetSystemColor(ui::NativeTheme::kColorId_TextButtonDisabledColor), |
135 }; | 163 }; |
136 #if defined(OS_WIN) | 164 #if defined(OS_WIN) |
137 // Native Windows buttons do not change color on hover or when pressed. | 165 // Native Windows buttons do not change color on hover or when pressed. |
(...skipping 12 matching lines...) Expand all Loading... |
150 const gfx::Size previous_image_size(image_->GetPreferredSize()); | 178 const gfx::Size previous_image_size(image_->GetPreferredSize()); |
151 image_->SetImage(GetImage(state())); | 179 image_->SetImage(GetImage(state())); |
152 const SkColor color = button_state_colors_[state()]; | 180 const SkColor color = button_state_colors_[state()]; |
153 if (state() != STATE_DISABLED && label_->enabled_color() != color) | 181 if (state() != STATE_DISABLED && label_->enabled_color() != color) |
154 label_->SetEnabledColor(color); | 182 label_->SetEnabledColor(color); |
155 label_->SetEnabled(state() != STATE_DISABLED); | 183 label_->SetEnabled(state() != STATE_DISABLED); |
156 if (image_->GetPreferredSize() != previous_image_size) | 184 if (image_->GetPreferredSize() != previous_image_size) |
157 Layout(); | 185 Layout(); |
158 } | 186 } |
159 | 187 |
160 gfx::Size LabelButton::GetPreferredSize() { | |
161 // Resize multi-line labels paired with images to use their available width. | |
162 const gfx::Size image_size(image_->GetPreferredSize()); | |
163 if (GetTextMultiLine() && !image_size.IsEmpty() && !GetText().empty() && | |
164 GetHorizontalAlignment() == gfx::ALIGN_CENTER) { | |
165 label_->SizeToFit(GetLocalBounds().width() - image_size.width() - kSpacing); | |
166 } | |
167 | |
168 // Calculate the required size. | |
169 gfx::Size size(label_->GetPreferredSize()); | |
170 if (image_size.width() > 0 && size.width() > 0) | |
171 size.Enlarge(kSpacing, 0); | |
172 size.set_height(std::max(size.height(), image_size.height())); | |
173 size.Enlarge(image_size.width() + GetInsets().width(), GetInsets().height()); | |
174 | |
175 // Increase the minimum size monotonically with the preferred size. | |
176 size.SetSize(std::max(min_size_.width(), size.width()), | |
177 std::max(min_size_.height(), size.height())); | |
178 min_size_ = size; | |
179 | |
180 // Return the largest known size clamped to the maximum size (if valid). | |
181 if (max_size_.width() > 0) | |
182 size.set_width(std::min(max_size_.width(), size.width())); | |
183 if (max_size_.height() > 0) | |
184 size.set_height(std::min(max_size_.height(), size.height())); | |
185 return size; | |
186 } | |
187 | |
188 void LabelButton::Layout() { | 188 void LabelButton::Layout() { |
189 gfx::Rect child_area(GetLocalBounds()); | 189 gfx::Rect child_area(GetLocalBounds()); |
190 child_area.Inset(GetInsets()); | 190 child_area.Inset(GetInsets()); |
191 | 191 |
192 gfx::Size image_size(image_->GetPreferredSize()); | 192 gfx::Size image_size(image_->GetPreferredSize()); |
193 image_size.set_width(std::min(image_size.width(), child_area.width())); | 193 image_size.set_width(std::min(image_size.width(), child_area.width())); |
194 image_size.set_height(std::min(image_size.height(), child_area.height())); | 194 image_size.set_height(std::min(image_size.height(), child_area.height())); |
195 | 195 |
196 // The label takes any remaining width after sizing the image, unless both | 196 // The label takes any remaining width after sizing the image, unless both |
197 // views are centered. In that case, using the tighter preferred label width | 197 // views are centered. In that case, using the tighter preferred label width |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
286 params->button.indeterminate = false; | 286 params->button.indeterminate = false; |
287 params->button.is_default = default_button(); | 287 params->button.is_default = default_button(); |
288 params->button.is_focused = HasFocus() && IsAccessibilityFocusable(); | 288 params->button.is_focused = HasFocus() && IsAccessibilityFocusable(); |
289 params->button.has_border = native_theme(); | 289 params->button.has_border = native_theme(); |
290 params->button.classic_state = 0; | 290 params->button.classic_state = 0; |
291 params->button.background_color = GetNativeTheme()->GetSystemColor( | 291 params->button.background_color = GetNativeTheme()->GetSystemColor( |
292 ui::NativeTheme::kColorId_TextButtonBackgroundColor); | 292 ui::NativeTheme::kColorId_TextButtonBackgroundColor); |
293 } | 293 } |
294 | 294 |
295 } // namespace views | 295 } // namespace views |
OLD | NEW |