OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "views/controls/button/text_button.h" | 5 #include "views/controls/button/text_button.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "app/throb_animation.h" | 9 #include "app/throb_animation.h" |
10 #include "app/resource_bundle.h" | 10 #include "app/resource_bundle.h" |
(...skipping 16 matching lines...) Expand all Loading... |
27 // static | 27 // static |
28 const SkColor TextButton::kHighlightColor = SkColorSetARGB(200, 255, 255, 255); | 28 const SkColor TextButton::kHighlightColor = SkColorSetARGB(200, 255, 255, 255); |
29 // static | 29 // static |
30 const SkColor TextButton::kDisabledColor = SkColorSetRGB(161, 161, 146); | 30 const SkColor TextButton::kDisabledColor = SkColorSetRGB(161, 161, 146); |
31 // static | 31 // static |
32 const SkColor TextButton::kHoverColor = TextButton::kEnabledColor; | 32 const SkColor TextButton::kHoverColor = TextButton::kEnabledColor; |
33 | 33 |
34 // How long the hover fade animation should last. | 34 // How long the hover fade animation should last. |
35 static const int kHoverAnimationDurationMs = 170; | 35 static const int kHoverAnimationDurationMs = 170; |
36 | 36 |
| 37 static int PrefixTypeToCanvasType(TextButton::PrefixType type) { |
| 38 switch (type) { |
| 39 case TextButton::PREFIX_HIDE: |
| 40 return gfx::Canvas::HIDE_PREFIX; |
| 41 case TextButton::PREFIX_SHOW: |
| 42 return gfx::Canvas::SHOW_PREFIX; |
| 43 case TextButton::PREFIX_NONE: |
| 44 return 0; |
| 45 default: |
| 46 NOTREACHED(); |
| 47 return 0; |
| 48 } |
| 49 } |
| 50 |
37 //////////////////////////////////////////////////////////////////////////////// | 51 //////////////////////////////////////////////////////////////////////////////// |
38 // | 52 // |
39 // TextButtonBorder - constructors, destructors, initialization | 53 // TextButtonBorder - constructors, destructors, initialization |
40 // | 54 // |
41 //////////////////////////////////////////////////////////////////////////////// | 55 //////////////////////////////////////////////////////////////////////////////// |
42 | 56 |
43 TextButtonBorder::TextButtonBorder() { | 57 TextButtonBorder::TextButtonBorder() { |
44 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 58 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
45 | 59 |
46 hot_set_.top_left = rb.GetBitmapNamed(IDR_TEXTBUTTON_TOP_LEFT_H); | 60 hot_set_.top_left = rb.GetBitmapNamed(IDR_TEXTBUTTON_TOP_LEFT_H); |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 font_(ResourceBundle::GetSharedInstance().GetFont( | 183 font_(ResourceBundle::GetSharedInstance().GetFont( |
170 ResourceBundle::BaseFont)), | 184 ResourceBundle::BaseFont)), |
171 color_(kEnabledColor), | 185 color_(kEnabledColor), |
172 color_enabled_(kEnabledColor), | 186 color_enabled_(kEnabledColor), |
173 color_disabled_(kDisabledColor), | 187 color_disabled_(kDisabledColor), |
174 color_highlight_(kHighlightColor), | 188 color_highlight_(kHighlightColor), |
175 color_hover_(kHoverColor), | 189 color_hover_(kHoverColor), |
176 has_hover_icon_(false), | 190 has_hover_icon_(false), |
177 max_width_(0), | 191 max_width_(0), |
178 normal_has_border_(false), | 192 normal_has_border_(false), |
179 show_highlighted_(true) { | 193 show_highlighted_(true), |
| 194 prefix_type_(PREFIX_NONE) { |
180 SetText(text); | 195 SetText(text); |
181 set_border(new TextButtonBorder); | 196 set_border(new TextButtonBorder); |
182 SetAnimationDuration(kHoverAnimationDurationMs); | 197 SetAnimationDuration(kHoverAnimationDurationMs); |
183 } | 198 } |
184 | 199 |
185 TextButton::~TextButton() { | 200 TextButton::~TextButton() { |
186 } | 201 } |
187 | 202 |
188 void TextButton::SetText(const std::wstring& text) { | 203 void TextButton::SetText(const std::wstring& text) { |
189 text_ = text; | 204 text_ = text; |
190 // Update our new current and max text size | 205 UpdateTextSize(); |
191 text_size_.SetSize(font_.GetStringWidth(text_), font_.height()); | |
192 max_text_size_.SetSize(std::max(max_text_size_.width(), text_size_.width()), | |
193 std::max(max_text_size_.height(), | |
194 text_size_.height())); | |
195 } | 206 } |
196 | 207 |
197 void TextButton::SetIcon(const SkBitmap& icon) { | 208 void TextButton::SetIcon(const SkBitmap& icon) { |
198 icon_ = icon; | 209 icon_ = icon; |
199 } | 210 } |
200 | 211 |
201 void TextButton::SetHoverIcon(const SkBitmap& icon) { | 212 void TextButton::SetHoverIcon(const SkBitmap& icon) { |
202 icon_hover_ = icon; | 213 icon_hover_ = icon; |
203 has_hover_icon_ = true; | 214 has_hover_icon_ = true; |
204 } | 215 } |
205 | 216 |
206 void TextButton::SetFont(const gfx::Font& font) { | 217 void TextButton::SetFont(const gfx::Font& font) { |
207 font_ = font; | 218 font_ = font; |
| 219 UpdateTextSize(); |
208 } | 220 } |
209 | 221 |
210 void TextButton::SetEnabledColor(SkColor color) { | 222 void TextButton::SetEnabledColor(SkColor color) { |
211 color_enabled_ = color; | 223 color_enabled_ = color; |
212 UpdateColor(); | 224 UpdateColor(); |
213 } | 225 } |
214 | 226 |
215 void TextButton::SetDisabledColor(SkColor color) { | 227 void TextButton::SetDisabledColor(SkColor color) { |
216 color_disabled_ = color; | 228 color_disabled_ = color; |
217 UpdateColor(); | 229 UpdateColor(); |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
311 // Due to the above, we must perform the flipping manually for RTL UIs. | 323 // Due to the above, we must perform the flipping manually for RTL UIs. |
312 gfx::Rect text_bounds(text_x, text_y, text_width, text_size_.height()); | 324 gfx::Rect text_bounds(text_x, text_y, text_width, text_size_.height()); |
313 text_bounds.set_x(MirroredLeftPointForRect(text_bounds)); | 325 text_bounds.set_x(MirroredLeftPointForRect(text_bounds)); |
314 | 326 |
315 SkColor text_color; | 327 SkColor text_color; |
316 if (show_highlighted_ && (state() == BS_HOT || state() == BS_PUSHED)) | 328 if (show_highlighted_ && (state() == BS_HOT || state() == BS_PUSHED)) |
317 text_color = color_hover_; | 329 text_color = color_hover_; |
318 else | 330 else |
319 text_color = color_; | 331 text_color = color_; |
320 | 332 |
| 333 int draw_string_flags = gfx::Canvas::DefaultCanvasTextAlignment() | |
| 334 PrefixTypeToCanvasType(prefix_type_); |
| 335 |
321 if (for_drag) { | 336 if (for_drag) { |
322 #if defined(OS_WIN) | 337 #if defined(OS_WIN) |
323 // TODO(erg): Either port DrawStringWithHalo to linux or find an | 338 // TODO(erg): Either port DrawStringWithHalo to linux or find an |
324 // alternative here. | 339 // alternative here. |
325 canvas->DrawStringWithHalo(text_, font_, text_color, color_highlight_, | 340 canvas->DrawStringWithHalo(text_, font_, text_color, color_highlight_, |
326 text_bounds.x(), | 341 text_bounds.x(), |
327 text_bounds.y(), | 342 text_bounds.y(), |
328 text_bounds.width(), | 343 text_bounds.width(), |
329 text_bounds.height(), | 344 text_bounds.height(), |
330 gfx::Canvas::DefaultCanvasTextAlignment()); | 345 draw_string_flags); |
331 #else | 346 #else |
332 canvas->DrawStringInt(text_, | 347 canvas->DrawStringInt(text_, |
333 font_, | 348 font_, |
334 text_color, | 349 text_color, |
335 text_bounds.x(), | 350 text_bounds.x(), |
336 text_bounds.y(), | 351 text_bounds.y(), |
337 text_bounds.width(), | 352 text_bounds.width(), |
338 text_bounds.height()); | 353 text_bounds.height(), |
| 354 draw_string_flags); |
339 #endif | 355 #endif |
340 } else { | 356 } else { |
341 canvas->DrawStringInt(text_, | 357 canvas->DrawStringInt(text_, |
342 font_, | 358 font_, |
343 text_color, | 359 text_color, |
344 text_bounds.x(), | 360 text_bounds.x(), |
345 text_bounds.y(), | 361 text_bounds.y(), |
346 text_bounds.width(), | 362 text_bounds.width(), |
347 text_bounds.height()); | 363 text_bounds.height(), |
| 364 draw_string_flags); |
348 } | 365 } |
349 } | 366 } |
350 | 367 |
351 if (icon.width() > 0) { | 368 if (icon.width() > 0) { |
352 int icon_y = (available_height - icon.height()) / 2 + insets.top(); | 369 int icon_y = (available_height - icon.height()) / 2 + insets.top(); |
353 | 370 |
354 // Mirroring the icon position if necessary. | 371 // Mirroring the icon position if necessary. |
355 gfx::Rect icon_bounds(icon_x, icon_y, icon.width(), icon.height()); | 372 gfx::Rect icon_bounds(icon_x, icon_y, icon.width(), icon.height()); |
356 icon_bounds.set_x(MirroredLeftPointForRect(icon_bounds)); | 373 icon_bounds.set_x(MirroredLeftPointForRect(icon_bounds)); |
357 canvas->DrawBitmapInt(icon, icon_bounds.x(), icon_bounds.y()); | 374 canvas->DrawBitmapInt(icon, icon_bounds.x(), icon_bounds.y()); |
358 } | 375 } |
359 } | 376 } |
360 | 377 |
361 void TextButton::UpdateColor() { | 378 void TextButton::UpdateColor() { |
362 color_ = IsEnabled() ? color_enabled_ : color_disabled_; | 379 color_ = IsEnabled() ? color_enabled_ : color_disabled_; |
363 } | 380 } |
364 | 381 |
| 382 void TextButton::UpdateTextSize() { |
| 383 int width = 0, height = 0; |
| 384 gfx::Canvas::SizeStringInt( |
| 385 text_, font_, &width, &height, |
| 386 gfx::Canvas::NO_ELLIPSIS | PrefixTypeToCanvasType(prefix_type_)); |
| 387 text_size_.SetSize(width, font_.height()); |
| 388 max_text_size_.SetSize(std::max(max_text_size_.width(), text_size_.width()), |
| 389 std::max(max_text_size_.height(), |
| 390 text_size_.height())); |
| 391 } |
| 392 |
365 //////////////////////////////////////////////////////////////////////////////// | 393 //////////////////////////////////////////////////////////////////////////////// |
366 // TextButton, View overrides: | 394 // TextButton, View overrides: |
367 | 395 |
368 gfx::Size TextButton::GetPreferredSize() { | 396 gfx::Size TextButton::GetPreferredSize() { |
369 gfx::Insets insets = GetInsets(); | 397 gfx::Insets insets = GetInsets(); |
370 | 398 |
371 // Use the max size to set the button boundaries. | 399 // Use the max size to set the button boundaries. |
372 gfx::Size prefsize(max_text_size_.width() + icon_.width() + insets.width(), | 400 gfx::Size prefsize(max_text_size_.width() + icon_.width() + insets.width(), |
373 std::max(max_text_size_.height(), icon_.height()) + | 401 std::max(max_text_size_.height(), icon_.height()) + |
374 insets.height()); | 402 insets.height()); |
(...skipping 14 matching lines...) Expand all Loading... |
389 void TextButton::SetEnabled(bool enabled) { | 417 void TextButton::SetEnabled(bool enabled) { |
390 if (enabled != IsEnabled()) { | 418 if (enabled != IsEnabled()) { |
391 CustomButton::SetEnabled(enabled); | 419 CustomButton::SetEnabled(enabled); |
392 } | 420 } |
393 // We should always call UpdateColor() since the state of the button might be | 421 // We should always call UpdateColor() since the state of the button might be |
394 // changed by other functions like CustomButton::SetState(). | 422 // changed by other functions like CustomButton::SetState(). |
395 UpdateColor(); | 423 UpdateColor(); |
396 SchedulePaint(); | 424 SchedulePaint(); |
397 } | 425 } |
398 | 426 |
399 bool TextButton::OnMousePressed(const MouseEvent& e) { | |
400 if (request_focus_on_press()) | |
401 RequestFocus(); | |
402 return true; | |
403 } | |
404 | |
405 void TextButton::Paint(gfx::Canvas* canvas) { | 427 void TextButton::Paint(gfx::Canvas* canvas) { |
406 Paint(canvas, false); | 428 Paint(canvas, false); |
407 } | 429 } |
408 | 430 |
409 } // namespace views | 431 } // namespace views |
OLD | NEW |