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 26 matching lines...) Expand all Loading... |
37 set_border(new LabelButtonBorder()); | 37 set_border(new LabelButtonBorder()); |
38 // Inset the button focus rect from the actual border; roughly match Windows. | 38 // Inset the button focus rect from the actual border; roughly match Windows. |
39 set_focus_border(FocusBorder::CreateDashedFocusBorder(3, 3, 3, 3)); | 39 set_focus_border(FocusBorder::CreateDashedFocusBorder(3, 3, 3, 3)); |
40 SetAnimationDuration(kHoverAnimationDurationMs); | 40 SetAnimationDuration(kHoverAnimationDurationMs); |
41 | 41 |
42 AddChildView(image_); | 42 AddChildView(image_); |
43 image_->set_interactive(false); | 43 image_->set_interactive(false); |
44 | 44 |
45 AddChildView(label_); | 45 AddChildView(label_); |
46 label_->SetAutoColorReadabilityEnabled(false); | 46 label_->SetAutoColorReadabilityEnabled(false); |
47 label_->SetHorizontalAlignment(Label::ALIGN_LEFT); | 47 label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
48 | 48 |
49 // Initialize the colors, border, and layout for a Views-themed button. | 49 // Initialize the colors, border, and layout for a Views-themed button. |
50 SetNativeTheme(false); | 50 SetNativeTheme(false); |
51 } | 51 } |
52 | 52 |
53 LabelButton::~LabelButton() {} | 53 LabelButton::~LabelButton() {} |
54 | 54 |
55 const gfx::ImageSkia& LabelButton::GetImage(ButtonState for_state) { | 55 const gfx::ImageSkia& LabelButton::GetImage(ButtonState for_state) { |
56 if (for_state != BS_NORMAL && button_state_images_[for_state].isNull()) | 56 if (for_state != BS_NORMAL && button_state_images_[for_state].isNull()) |
57 return button_state_images_[BS_NORMAL]; | 57 return button_state_images_[BS_NORMAL]; |
(...skipping 30 matching lines...) Expand all Loading... |
88 } | 88 } |
89 | 89 |
90 const gfx::Font& LabelButton::GetFont() const { | 90 const gfx::Font& LabelButton::GetFont() const { |
91 return label_->font(); | 91 return label_->font(); |
92 } | 92 } |
93 | 93 |
94 void LabelButton::SetFont(const gfx::Font& font) { | 94 void LabelButton::SetFont(const gfx::Font& font) { |
95 label_->SetFont(font); | 95 label_->SetFont(font); |
96 } | 96 } |
97 | 97 |
98 Label::Alignment LabelButton::GetHorizontalAlignment() const { | 98 gfx::HorizontalAlignment LabelButton::GetHorizontalAlignment() const { |
99 return label_->horizontal_alignment(); | 99 return label_->horizontal_alignment(); |
100 } | 100 } |
101 | 101 |
102 void LabelButton::SetHorizontalAlignment(Label::Alignment alignment) { | 102 void LabelButton::SetHorizontalAlignment(gfx::HorizontalAlignment alignment) { |
103 label_->SetHorizontalAlignment(alignment); | 103 label_->SetHorizontalAlignment(alignment); |
104 InvalidateLayout(); | 104 InvalidateLayout(); |
105 } | 105 } |
106 | 106 |
107 void LabelButton::SetDefaultButton(bool default_button) { | 107 void LabelButton::SetDefaultButton(bool default_button) { |
108 if (default_button == default_button_) | 108 if (default_button == default_button_) |
109 return; | 109 return; |
110 default_button_ = default_button; | 110 default_button_ = default_button; |
111 ui::Accelerator accel(ui::VKEY_RETURN, ui::EF_NONE); | 111 ui::Accelerator accel(ui::VKEY_RETURN, ui::EF_NONE); |
112 default_button_ ? AddAccelerator(accel) : RemoveAccelerator(accel); | 112 default_button_ ? AddAccelerator(accel) : RemoveAccelerator(accel); |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 gfx::Size image_size(image_->GetPreferredSize()); | 176 gfx::Size image_size(image_->GetPreferredSize()); |
177 image_size.set_width(std::min(image_size.width(), child_area.width())); | 177 image_size.set_width(std::min(image_size.width(), child_area.width())); |
178 image_size.set_height(std::min(image_size.height(), child_area.height())); | 178 image_size.set_height(std::min(image_size.height(), child_area.height())); |
179 | 179 |
180 // The label takes any remaining width after sizing the image, unless both | 180 // The label takes any remaining width after sizing the image, unless both |
181 // views are centered. In that case, using the tighter preferred label width | 181 // views are centered. In that case, using the tighter preferred label width |
182 // avoids wasted space within the label that would look like awkward padding. | 182 // avoids wasted space within the label that would look like awkward padding. |
183 gfx::Size label_size(child_area.size()); | 183 gfx::Size label_size(child_area.size()); |
184 if (!image_size.IsEmpty() && !label_size.IsEmpty()) { | 184 if (!image_size.IsEmpty() && !label_size.IsEmpty()) { |
185 label_size.set_width(child_area.width() - image_size.width() - kSpacing); | 185 label_size.set_width(child_area.width() - image_size.width() - kSpacing); |
186 if (GetHorizontalAlignment() == Label::ALIGN_CENTER) { | 186 if (GetHorizontalAlignment() == gfx::ALIGN_CENTER) { |
187 label_size.set_width(std::min(label_size.width(), | 187 label_size.set_width(std::min(label_size.width(), |
188 label_->GetPreferredSize().width())); | 188 label_->GetPreferredSize().width())); |
189 } | 189 } |
190 } | 190 } |
191 | 191 |
192 gfx::Point image_origin(child_area.origin()); | 192 gfx::Point image_origin(child_area.origin()); |
193 image_origin.Offset(0, (child_area.height() - image_size.height()) / 2); | 193 image_origin.Offset(0, (child_area.height() - image_size.height()) / 2); |
194 if (GetHorizontalAlignment() == Label::ALIGN_CENTER) { | 194 if (GetHorizontalAlignment() == gfx::ALIGN_CENTER) { |
195 const int total_width = image_size.width() + label_size.width() + | 195 const int total_width = image_size.width() + label_size.width() + |
196 ((image_size.width() > 0 && label_size.width() > 0) ? kSpacing : 0); | 196 ((image_size.width() > 0 && label_size.width() > 0) ? kSpacing : 0); |
197 image_origin.Offset((child_area.width() - total_width) / 2, 0); | 197 image_origin.Offset((child_area.width() - total_width) / 2, 0); |
198 } else if (GetHorizontalAlignment() == Label::ALIGN_RIGHT) { | 198 } else if (GetHorizontalAlignment() == gfx::ALIGN_RIGHT) { |
199 image_origin.Offset(child_area.width() - image_size.width(), 0); | 199 image_origin.Offset(child_area.width() - image_size.width(), 0); |
200 } | 200 } |
201 | 201 |
202 gfx::Point label_origin(child_area.origin()); | 202 gfx::Point label_origin(child_area.origin()); |
203 if (!image_size.IsEmpty() && GetHorizontalAlignment() != Label::ALIGN_RIGHT) | 203 if (!image_size.IsEmpty() && GetHorizontalAlignment() != gfx::ALIGN_RIGHT) |
204 label_origin.set_x(image_origin.x() + image_size.width() + kSpacing); | 204 label_origin.set_x(image_origin.x() + image_size.width() + kSpacing); |
205 | 205 |
206 image_->SetBoundsRect(gfx::Rect(image_origin, image_size)); | 206 image_->SetBoundsRect(gfx::Rect(image_origin, image_size)); |
207 label_->SetBoundsRect(gfx::Rect(label_origin, label_size)); | 207 label_->SetBoundsRect(gfx::Rect(label_origin, label_size)); |
208 } | 208 } |
209 | 209 |
210 std::string LabelButton::GetClassName() const { | 210 std::string LabelButton::GetClassName() const { |
211 return "views/LabelButton"; | 211 return "views/LabelButton"; |
212 } | 212 } |
213 | 213 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 params->button.indeterminate = false; | 261 params->button.indeterminate = false; |
262 params->button.is_default = default_button(); | 262 params->button.is_default = default_button(); |
263 params->button.is_focused = HasFocus() && IsAccessibilityFocusable(); | 263 params->button.is_focused = HasFocus() && IsAccessibilityFocusable(); |
264 params->button.has_border = native_theme(); | 264 params->button.has_border = native_theme(); |
265 params->button.classic_state = 0; | 265 params->button.classic_state = 0; |
266 params->button.background_color = ui::NativeTheme::instance()->GetSystemColor( | 266 params->button.background_color = ui::NativeTheme::instance()->GetSystemColor( |
267 ui::NativeTheme::kColorId_TextButtonBackgroundColor); | 267 ui::NativeTheme::kColorId_TextButtonBackgroundColor); |
268 } | 268 } |
269 | 269 |
270 } // namespace views | 270 } // namespace views |
OLD | NEW |