Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(657)

Side by Side Diff: ui/views/controls/button/label_button.cc

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

Powered by Google App Engine
This is Rietveld 408576698