| 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 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 alignment_(ALIGN_LEFT), | 181 alignment_(ALIGN_LEFT), |
| 182 icon_placement_(ICON_ON_LEFT), | 182 icon_placement_(ICON_ON_LEFT), |
| 183 font_(ResourceBundle::GetSharedInstance().GetFont( | 183 font_(ResourceBundle::GetSharedInstance().GetFont( |
| 184 ResourceBundle::BaseFont)), | 184 ResourceBundle::BaseFont)), |
| 185 color_(kEnabledColor), | 185 color_(kEnabledColor), |
| 186 color_enabled_(kEnabledColor), | 186 color_enabled_(kEnabledColor), |
| 187 color_disabled_(kDisabledColor), | 187 color_disabled_(kDisabledColor), |
| 188 color_highlight_(kHighlightColor), | 188 color_highlight_(kHighlightColor), |
| 189 color_hover_(kHoverColor), | 189 color_hover_(kHoverColor), |
| 190 has_hover_icon_(false), | 190 has_hover_icon_(false), |
| 191 has_pushed_icon_(false), |
| 191 max_width_(0), | 192 max_width_(0), |
| 192 normal_has_border_(false), | 193 normal_has_border_(false), |
| 193 show_highlighted_(true), | 194 show_multiple_states_(true), |
| 194 prefix_type_(PREFIX_NONE) { | 195 prefix_type_(PREFIX_NONE) { |
| 195 SetText(text); | 196 SetText(text); |
| 196 set_border(new TextButtonBorder); | 197 set_border(new TextButtonBorder); |
| 197 SetAnimationDuration(kHoverAnimationDurationMs); | 198 SetAnimationDuration(kHoverAnimationDurationMs); |
| 198 } | 199 } |
| 199 | 200 |
| 200 TextButton::~TextButton() { | 201 TextButton::~TextButton() { |
| 201 } | 202 } |
| 202 | 203 |
| 203 void TextButton::SetText(const std::wstring& text) { | 204 void TextButton::SetText(const std::wstring& text) { |
| 204 text_ = text; | 205 text_ = text; |
| 205 UpdateTextSize(); | 206 UpdateTextSize(); |
| 206 } | 207 } |
| 207 | 208 |
| 208 void TextButton::SetIcon(const SkBitmap& icon) { | 209 void TextButton::SetIcon(const SkBitmap& icon) { |
| 209 icon_ = icon; | 210 icon_ = icon; |
| 210 } | 211 } |
| 211 | 212 |
| 212 void TextButton::SetHoverIcon(const SkBitmap& icon) { | 213 void TextButton::SetHoverIcon(const SkBitmap& icon) { |
| 213 icon_hover_ = icon; | 214 icon_hover_ = icon; |
| 214 has_hover_icon_ = true; | 215 has_hover_icon_ = true; |
| 215 } | 216 } |
| 216 | 217 |
| 218 void TextButton::SetPushedIcon(const SkBitmap& icon) { |
| 219 icon_pushed_ = icon; |
| 220 has_pushed_icon_ = true; |
| 221 } |
| 222 |
| 217 void TextButton::SetFont(const gfx::Font& font) { | 223 void TextButton::SetFont(const gfx::Font& font) { |
| 218 font_ = font; | 224 font_ = font; |
| 219 UpdateTextSize(); | 225 UpdateTextSize(); |
| 220 } | 226 } |
| 221 | 227 |
| 222 void TextButton::SetEnabledColor(SkColor color) { | 228 void TextButton::SetEnabledColor(SkColor color) { |
| 223 color_enabled_ = color; | 229 color_enabled_ = color; |
| 224 UpdateColor(); | 230 UpdateColor(); |
| 225 } | 231 } |
| 226 | 232 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 238 } | 244 } |
| 239 | 245 |
| 240 void TextButton::ClearMaxTextSize() { | 246 void TextButton::ClearMaxTextSize() { |
| 241 max_text_size_ = text_size_; | 247 max_text_size_ = text_size_; |
| 242 } | 248 } |
| 243 | 249 |
| 244 void TextButton::SetNormalHasBorder(bool normal_has_border) { | 250 void TextButton::SetNormalHasBorder(bool normal_has_border) { |
| 245 normal_has_border_ = normal_has_border; | 251 normal_has_border_ = normal_has_border; |
| 246 } | 252 } |
| 247 | 253 |
| 248 void TextButton::SetShowHighlighted(bool show_highlighted) { | 254 void TextButton::SetShowMultipleStates(bool show_multiple_states) { |
| 249 show_highlighted_ = show_highlighted; | 255 show_multiple_states_ = show_multiple_states; |
| 250 } | 256 } |
| 251 | 257 |
| 252 void TextButton::Paint(gfx::Canvas* canvas, bool for_drag) { | 258 void TextButton::Paint(gfx::Canvas* canvas, bool for_drag) { |
| 253 if (!for_drag) { | 259 if (!for_drag) { |
| 254 PaintBackground(canvas); | 260 PaintBackground(canvas); |
| 255 | 261 |
| 256 if (show_highlighted_ && hover_animation_->is_animating()) { | 262 if (show_multiple_states_ && hover_animation_->is_animating()) { |
| 257 // Draw the hover bitmap into an offscreen buffer, then blend it | 263 // Draw the hover bitmap into an offscreen buffer, then blend it |
| 258 // back into the current canvas. | 264 // back into the current canvas. |
| 259 canvas->SaveLayerAlpha( | 265 canvas->SaveLayerAlpha( |
| 260 static_cast<int>(hover_animation_->GetCurrentValue() * 255)); | 266 static_cast<int>(hover_animation_->GetCurrentValue() * 255)); |
| 261 canvas->AsCanvasSkia()->drawARGB(0, 255, 255, 255, | 267 canvas->AsCanvasSkia()->drawARGB(0, 255, 255, 255, |
| 262 SkXfermode::kClear_Mode); | 268 SkXfermode::kClear_Mode); |
| 263 PaintBorder(canvas); | 269 PaintBorder(canvas); |
| 264 canvas->Restore(); | 270 canvas->Restore(); |
| 265 } else if ((show_highlighted_ && | 271 } else if ((show_multiple_states_ && |
| 266 (state_ == BS_HOT || state_ == BS_PUSHED)) || | 272 (state_ == BS_HOT || state_ == BS_PUSHED)) || |
| 267 (state_ == BS_NORMAL && normal_has_border_)) { | 273 (state_ == BS_NORMAL && normal_has_border_)) { |
| 268 PaintBorder(canvas); | 274 PaintBorder(canvas); |
| 269 } | 275 } |
| 270 | 276 |
| 271 PaintFocusBorder(canvas); | 277 PaintFocusBorder(canvas); |
| 272 } | 278 } |
| 273 | 279 |
| 274 SkBitmap icon; | 280 SkBitmap icon; |
| 275 if (has_hover_icon_ && show_highlighted_ && | 281 if (show_multiple_states_ && has_hover_icon_ && (state() == BS_HOT)) |
| 276 (state() == BS_HOT || state() == BS_PUSHED)) | |
| 277 icon = icon_hover_; | 282 icon = icon_hover_; |
| 283 else if (show_multiple_states_ && has_pushed_icon_ && (state() == BS_PUSHED)) |
| 284 icon = icon_pushed_; |
| 278 else | 285 else |
| 279 icon = icon_; | 286 icon = icon_; |
| 280 | 287 |
| 281 gfx::Insets insets = GetInsets(); | 288 gfx::Insets insets = GetInsets(); |
| 282 int available_width = width() - insets.width(); | 289 int available_width = width() - insets.width(); |
| 283 int available_height = height() - insets.height(); | 290 int available_height = height() - insets.height(); |
| 284 // Use the actual text (not max) size to properly center the text. | 291 // Use the actual text (not max) size to properly center the text. |
| 285 int content_width = text_size_.width(); | 292 int content_width = text_size_.width(); |
| 286 if (icon.width() > 0) { | 293 if (icon.width() > 0) { |
| 287 content_width += icon.width(); | 294 content_width += icon.width(); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 318 // doing this will mirror the text itself. Flipping the canvas will also | 325 // doing this will mirror the text itself. Flipping the canvas will also |
| 319 // make the icons look wrong because icons are almost always represented as | 326 // make the icons look wrong because icons are almost always represented as |
| 320 // direction insentisive bitmaps and such bitmaps should never be flipped | 327 // direction insentisive bitmaps and such bitmaps should never be flipped |
| 321 // horizontally. | 328 // horizontally. |
| 322 // | 329 // |
| 323 // Due to the above, we must perform the flipping manually for RTL UIs. | 330 // Due to the above, we must perform the flipping manually for RTL UIs. |
| 324 gfx::Rect text_bounds(text_x, text_y, text_width, text_size_.height()); | 331 gfx::Rect text_bounds(text_x, text_y, text_width, text_size_.height()); |
| 325 text_bounds.set_x(MirroredLeftPointForRect(text_bounds)); | 332 text_bounds.set_x(MirroredLeftPointForRect(text_bounds)); |
| 326 | 333 |
| 327 SkColor text_color; | 334 SkColor text_color; |
| 328 if (show_highlighted_ && (state() == BS_HOT || state() == BS_PUSHED)) | 335 if (show_multiple_states_ && (state() == BS_HOT || state() == BS_PUSHED)) |
| 329 text_color = color_hover_; | 336 text_color = color_hover_; |
| 330 else | 337 else |
| 331 text_color = color_; | 338 text_color = color_; |
| 332 | 339 |
| 333 int draw_string_flags = gfx::CanvasSkia::DefaultCanvasTextAlignment() | | 340 int draw_string_flags = gfx::CanvasSkia::DefaultCanvasTextAlignment() | |
| 334 PrefixTypeToCanvasType(prefix_type_); | 341 PrefixTypeToCanvasType(prefix_type_); |
| 335 | 342 |
| 336 if (for_drag) { | 343 if (for_drag) { |
| 337 #if defined(OS_WIN) | 344 #if defined(OS_WIN) |
| 338 // TODO(erg): Either port DrawStringWithHalo to linux or find an | 345 // TODO(erg): Either port DrawStringWithHalo to linux or find an |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 421 // changed by other functions like CustomButton::SetState(). | 428 // changed by other functions like CustomButton::SetState(). |
| 422 UpdateColor(); | 429 UpdateColor(); |
| 423 SchedulePaint(); | 430 SchedulePaint(); |
| 424 } | 431 } |
| 425 | 432 |
| 426 void TextButton::Paint(gfx::Canvas* canvas) { | 433 void TextButton::Paint(gfx::Canvas* canvas) { |
| 427 Paint(canvas, false); | 434 Paint(canvas, false); |
| 428 } | 435 } |
| 429 | 436 |
| 430 } // namespace views | 437 } // namespace views |
| OLD | NEW |