| 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 "chrome/browser/chromeos/login/username_view.h" | 5 #include "chrome/browser/chromeos/login/username_view.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
| 9 #include "chrome/browser/chromeos/login/rounded_view.h" | 9 #include "chrome/browser/chromeos/login/rounded_view.h" |
| 10 #include "gfx/canvas.h" | 10 #include "gfx/canvas.h" |
| 11 #include "gfx/canvas_skia.h" | 11 #include "gfx/canvas_skia.h" |
| 12 #include "gfx/gtk_util.h" |
| 12 #include "gfx/rect.h" | 13 #include "gfx/rect.h" |
| 13 #include "third_party/skia/include/core/SkColorShader.h" | 14 #include "third_party/skia/include/core/SkColorShader.h" |
| 14 #include "third_party/skia/include/core/SkComposeShader.h" | 15 #include "third_party/skia/include/core/SkComposeShader.h" |
| 15 #include "third_party/skia/include/effects/SkGradientShader.h" | 16 #include "third_party/skia/include/effects/SkGradientShader.h" |
| 16 | 17 |
| 17 namespace chromeos { | 18 namespace chromeos { |
| 18 | 19 |
| 19 namespace { | 20 namespace { |
| 20 // Username label background color. | 21 // Username label background color. |
| 21 const SkColor kLabelBackgoundColor = 0x55000000; | 22 const SkColor kLabelBackgoundColor = 0x55000000; |
| 22 // Holds margin to height ratio. | 23 // Holds margin to height ratio. |
| 23 const double kMarginRatio = 1.0 / 3.0; | 24 const double kMarginRatio = 1.0 / 3.0; |
| 24 // Holds the frame width for the small shaped username view. | 25 // Holds the frame width for the small shaped username view. |
| 25 const SkScalar kSmallShapeFrameWidth = SkIntToScalar(1); | 26 const SkScalar kSmallShapeFrameWidth = SkIntToScalar(1); |
| 26 | 27 |
| 27 // Class that sets up half rounded rectangle (only the bottom corners are | 28 // Class that sets up half rounded rectangle (only the bottom corners are |
| 28 // rounded) as a clip region of the view. | 29 // rounded) as a clip region of the view. |
| 29 // For more info see the file "chrome/browser/chromeos/login/rounded_view.h". | 30 // For more info see the file "chrome/browser/chromeos/login/rounded_view.h". |
| 30 template<typename C> | 31 template<typename C> |
| 31 class HalfRoundedView : public RoundedView<C> { | 32 class HalfRoundedView : public RoundedView<C> { |
| 32 public: | 33 public: |
| 33 HalfRoundedView(const std::wstring &text, bool use_small_shape) | 34 HalfRoundedView(const std::wstring &text, bool use_small_shape) |
| 34 : RoundedView<C>(text), use_small_shape_(use_small_shape) { | 35 : RoundedView<C>(text, use_small_shape) { |
| 35 } | 36 } |
| 36 | 37 |
| 37 protected: | 38 protected: |
| 38 // Overrides ViewFilter. | 39 // Overrides ViewFilter. |
| 39 virtual SkPath GetClipPath() const { | 40 virtual SkPath GetClipPath() const { |
| 40 if (!use_small_shape_) { | 41 if (!C::use_small_shape()) { |
| 41 return RoundedView<C>::GetClipPath(); | 42 return RoundedView<C>::GetClipPath(); |
| 42 } else { | 43 } else { |
| 43 SkPath path; | 44 SkPath path; |
| 44 gfx::Rect frame_bounds = this->bounds(); | 45 gfx::Rect frame_bounds = this->bounds(); |
| 45 frame_bounds.Inset(kSmallShapeFrameWidth, kSmallShapeFrameWidth, | 46 frame_bounds.Inset(kSmallShapeFrameWidth, kSmallShapeFrameWidth, |
| 46 kSmallShapeFrameWidth, kSmallShapeFrameWidth); | 47 kSmallShapeFrameWidth, kSmallShapeFrameWidth); |
| 47 path.addRect(SkIntToScalar(frame_bounds.x()), | 48 path.addRect(SkIntToScalar(frame_bounds.x()), |
| 48 SkIntToScalar(frame_bounds.y()), | 49 SkIntToScalar(frame_bounds.y()), |
| 49 SkIntToScalar(frame_bounds.x() + frame_bounds.width()), | 50 SkIntToScalar(frame_bounds.x() + frame_bounds.width()), |
| 50 SkIntToScalar(frame_bounds.y() + frame_bounds.height())); | 51 SkIntToScalar(frame_bounds.y() + frame_bounds.height())); |
| 51 return path; | 52 return path; |
| 52 } | 53 } |
| 53 } | 54 } |
| 54 | 55 |
| 55 virtual void DrawFrame(gfx::Canvas* canvas) { | 56 virtual void DrawFrame(gfx::Canvas* canvas) { |
| 56 // No frame is needed. | 57 // No frame is needed. |
| 57 } | 58 } |
| 58 | 59 |
| 59 virtual SkRect GetViewRect() const { | 60 virtual SkRect GetViewRect() const { |
| 60 SkRect view_rect; | 61 SkRect view_rect; |
| 61 // The rectangle will be intersected with the bounds, so the correct half | 62 // The rectangle will be intersected with the bounds, so the correct half |
| 62 // of the round rectangle will be obtained. | 63 // of the round rectangle will be obtained. |
| 63 view_rect.iset(this->x(), | 64 view_rect.iset(this->x(), |
| 64 this->y() - this->height(), | 65 this->y() - this->height(), |
| 65 this->x() + this->width(), | 66 this->x() + this->width(), |
| 66 this->y() + this->height()); | 67 this->y() + this->height()); |
| 67 return view_rect; | 68 return view_rect; |
| 68 } | 69 } |
| 69 | |
| 70 private: | |
| 71 // Whether the shape for the smaller view should be used. | |
| 72 bool use_small_shape_; | |
| 73 }; | 70 }; |
| 74 | 71 |
| 75 } // namespace | 72 } // namespace |
| 76 UsernameView::UsernameView(const std::wstring& username) | 73 UsernameView::UsernameView(const std::wstring& username, bool use_small_shape) |
| 77 : views::Label(username) { | 74 : views::Label(username), |
| 75 use_small_shape_(use_small_shape) { |
| 78 } | 76 } |
| 79 | 77 |
| 80 void UsernameView::Paint(gfx::Canvas* canvas) { | 78 void UsernameView::Paint(gfx::Canvas* canvas) { |
| 81 gfx::Rect bounds = GetLocalBounds(false); | 79 gfx::Rect bounds = GetLocalBounds(false); |
| 82 if (!text_image_.get()) | 80 if (!text_image_.get()) |
| 83 PaintUsername(bounds); | 81 PaintUsername(bounds); |
| 84 DCHECK(bounds.size() == | 82 DCHECK(bounds.size() == |
| 85 gfx::Size(text_image_->width(), text_image_->height())); | 83 gfx::Size(text_image_->width(), text_image_->height())); |
| 86 | 84 |
| 87 canvas->DrawBitmapInt(*text_image_, bounds.x(), bounds.y()); | 85 canvas->DrawBitmapInt(*text_image_, bounds.x(), bounds.y()); |
| 88 } | 86 } |
| 89 | 87 |
| 90 // static | 88 // static |
| 91 UsernameView* UsernameView::CreateShapedUsernameView( | 89 UsernameView* UsernameView::CreateShapedUsernameView( |
| 92 const std::wstring& username, | 90 const std::wstring& username, |
| 93 bool use_small_shape) { | 91 bool use_small_shape) { |
| 94 return new HalfRoundedView<UsernameView>(username, use_small_shape); | 92 return new HalfRoundedView<UsernameView>(username, use_small_shape); |
| 95 } | 93 } |
| 96 | 94 |
| 95 gfx::NativeCursor UsernameView::GetCursorForPoint( |
| 96 views::Event::EventType event_type, |
| 97 const gfx::Point& p) { |
| 98 |
| 99 return use_small_shape_ ? gfx::GetCursor(GDK_HAND2) : NULL; |
| 100 } |
| 101 |
| 97 void UsernameView::PaintUsername(const gfx::Rect& bounds) { | 102 void UsernameView::PaintUsername(const gfx::Rect& bounds) { |
| 98 margin_width_ = bounds.height() * kMarginRatio; | 103 margin_width_ = bounds.height() * kMarginRatio; |
| 99 gfx::CanvasSkia canvas(bounds.width(), bounds.height(), false); | 104 gfx::CanvasSkia canvas(bounds.width(), bounds.height(), false); |
| 100 // Draw transparent background. | 105 // Draw transparent background. |
| 101 canvas.drawColor(0); | 106 canvas.drawColor(0); |
| 102 | 107 |
| 103 // Calculate needed space. | 108 // Calculate needed space. |
| 104 int flags = gfx::Canvas::TEXT_ALIGN_LEFT | | 109 int flags = gfx::Canvas::TEXT_ALIGN_LEFT | |
| 105 gfx::Canvas::TEXT_VALIGN_MIDDLE | | 110 gfx::Canvas::TEXT_VALIGN_MIDDLE | |
| 106 gfx::Canvas::NO_ELLIPSIS; | 111 gfx::Canvas::NO_ELLIPSIS; |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 image_shader->unref(); | 176 image_shader->unref(); |
| 172 | 177 |
| 173 SkPaint paint; | 178 SkPaint paint; |
| 174 paint.setShader(composite_shader)->unref(); | 179 paint.setShader(composite_shader)->unref(); |
| 175 canvas.drawPaint(paint); | 180 canvas.drawPaint(paint); |
| 176 text_image_.reset(new SkBitmap(canvas.ExtractBitmap())); | 181 text_image_.reset(new SkBitmap(canvas.ExtractBitmap())); |
| 177 } | 182 } |
| 178 } | 183 } |
| 179 | 184 |
| 180 } // namespace chromeos | 185 } // namespace chromeos |
| OLD | NEW |