| 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/button_drag_utils.h" | 5 #include "ui/views/button_drag_utils.h" |
| 6 | 6 |
| 7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
| 8 #include "ui/base/dragdrop/drag_utils.h" | 8 #include "ui/base/dragdrop/drag_utils.h" |
| 9 #include "ui/base/dragdrop/os_exchange_data.h" | 9 #include "ui/base/dragdrop/os_exchange_data.h" |
| 10 #include "ui/base/resource/resource_bundle.h" | 10 #include "ui/base/resource/resource_bundle.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 | 25 |
| 26 namespace button_drag_utils { | 26 namespace button_drag_utils { |
| 27 | 27 |
| 28 // Maximum width of the link drag image in pixels. | 28 // Maximum width of the link drag image in pixels. |
| 29 static const int kLinkDragImageMaxWidth = 150; | 29 static const int kLinkDragImageMaxWidth = 150; |
| 30 | 30 |
| 31 void SetURLAndDragImage(const GURL& url, | 31 void SetURLAndDragImage(const GURL& url, |
| 32 const base::string16& title, | 32 const base::string16& title, |
| 33 const gfx::ImageSkia& icon, | 33 const gfx::ImageSkia& icon, |
| 34 const gfx::Point* press_pt, | 34 const gfx::Point* press_pt, |
| 35 ui::OSExchangeData* data, | 35 const views::Widget& widget, |
| 36 views::Widget* widget) { | 36 ui::OSExchangeData* data) { |
| 37 DCHECK(url.is_valid() && data); | 37 DCHECK(url.is_valid()); |
| 38 DCHECK(data); |
| 38 data->SetURL(url, title); | 39 data->SetURL(url, title); |
| 39 SetDragImage(url, title, icon, press_pt, data, widget); | 40 SetDragImage(url, title, icon, press_pt, widget, data); |
| 40 } | 41 } |
| 41 | 42 |
| 42 void SetDragImage(const GURL& url, | 43 void SetDragImage(const GURL& url, |
| 43 const base::string16& title, | 44 const base::string16& title, |
| 44 const gfx::ImageSkia& icon, | 45 const gfx::ImageSkia& icon, |
| 45 const gfx::Point* press_pt, | 46 const gfx::Point* press_pt, |
| 46 ui::OSExchangeData* data, | 47 const views::Widget& widget, |
| 47 views::Widget* widget) { | 48 ui::OSExchangeData* data) { |
| 48 // Create a button to render the drag image for us. | 49 // Create a button to render the drag image for us. |
| 49 views::LabelButton button(NULL, | 50 views::LabelButton button(NULL, |
| 50 title.empty() ? base::UTF8ToUTF16(url.spec()) | 51 title.empty() ? base::UTF8ToUTF16(url.spec()) |
| 51 : title); | 52 : title); |
| 52 button.SetTextSubpixelRenderingEnabled(false); | 53 button.SetTextSubpixelRenderingEnabled(false); |
| 53 const ui::NativeTheme* theme = widget->GetNativeTheme(); | 54 const ui::NativeTheme* theme = widget.GetNativeTheme(); |
| 54 button.SetTextColor(views::Button::STATE_NORMAL, | 55 button.SetTextColor(views::Button::STATE_NORMAL, |
| 55 theme->GetSystemColor(ui::NativeTheme::kColorId_TextfieldDefaultColor)); | 56 theme->GetSystemColor(ui::NativeTheme::kColorId_TextfieldDefaultColor)); |
| 56 | 57 |
| 57 SkColor bg_color = theme->GetSystemColor( | 58 SkColor bg_color = theme->GetSystemColor( |
| 58 ui::NativeTheme::kColorId_TextfieldDefaultBackground); | 59 ui::NativeTheme::kColorId_TextfieldDefaultBackground); |
| 59 if (widget->IsTranslucentWindowOpacitySupported()) { | 60 if (widget.IsTranslucentWindowOpacitySupported()) { |
| 60 button.SetTextShadows(gfx::ShadowValues( | 61 button.SetTextShadows(gfx::ShadowValues( |
| 61 10, gfx::ShadowValue(gfx::Vector2d(0, 0), 2.0f, bg_color))); | 62 10, gfx::ShadowValue(gfx::Vector2d(0, 0), 2.0f, bg_color))); |
| 62 } else { | 63 } else { |
| 63 button.set_background(views::Background::CreateSolidBackground(bg_color)); | 64 button.set_background(views::Background::CreateSolidBackground(bg_color)); |
| 64 button.SetBorder(button.CreateDefaultBorder()); | 65 button.SetBorder(button.CreateDefaultBorder()); |
| 65 } | 66 } |
| 66 button.SetMaxSize(gfx::Size(kLinkDragImageMaxWidth, 0)); | 67 button.SetMaxSize(gfx::Size(kLinkDragImageMaxWidth, 0)); |
| 67 if (icon.isNull()) { | 68 if (icon.isNull()) { |
| 68 button.SetImage(views::Button::STATE_NORMAL, | 69 button.SetImage(views::Button::STATE_NORMAL, |
| 69 *ui::ResourceBundle::GetSharedInstance().GetImageNamed( | 70 *ui::ResourceBundle::GetSharedInstance().GetImageNamed( |
| 70 IDR_DEFAULT_FAVICON).ToImageSkia()); | 71 IDR_DEFAULT_FAVICON).ToImageSkia()); |
| 71 } else { | 72 } else { |
| 72 button.SetImage(views::Button::STATE_NORMAL, icon); | 73 button.SetImage(views::Button::STATE_NORMAL, icon); |
| 73 } | 74 } |
| 74 | 75 |
| 75 gfx::Size size(button.GetPreferredSize()); | 76 gfx::Size size(button.GetPreferredSize()); |
| 76 button.SetBoundsRect(gfx::Rect(size)); | 77 button.SetBoundsRect(gfx::Rect(size)); |
| 77 | 78 |
| 78 gfx::Vector2d press_point; | 79 gfx::Vector2d press_point; |
| 79 if (press_pt) | 80 if (press_pt) |
| 80 press_point = press_pt->OffsetFromOrigin(); | 81 press_point = press_pt->OffsetFromOrigin(); |
| 81 else | 82 else |
| 82 press_point = gfx::Vector2d(size.width() / 2, size.height() / 2); | 83 press_point = gfx::Vector2d(size.width() / 2, size.height() / 2); |
| 83 | 84 |
| 84 SkBitmap bitmap; | 85 SkBitmap bitmap; |
| 85 float raster_scale = ScaleFactorForDragFromWidget(widget); | 86 float raster_scale = ScaleFactorForDragFromWidget(&widget); |
| 86 SkColor color = SK_ColorTRANSPARENT; | 87 SkColor color = SK_ColorTRANSPARENT; |
| 87 button.Paint(ui::CanvasPainter(&bitmap, size, raster_scale, color).context()); | 88 button.Paint(ui::CanvasPainter(&bitmap, size, raster_scale, color).context()); |
| 88 gfx::ImageSkia image(gfx::ImageSkiaRep(bitmap, raster_scale)); | 89 gfx::ImageSkia image(gfx::ImageSkiaRep(bitmap, raster_scale)); |
| 89 drag_utils::SetDragImageOnDataObject(image, press_point, data); | 90 drag_utils::SetDragImageOnDataObject(image, press_point, data); |
| 90 } | 91 } |
| 91 | 92 |
| 92 } // namespace button_drag_utils | 93 } // namespace button_drag_utils |
| OLD | NEW |