| 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/base/dragdrop/drag_utils.h" | 5 #include "ui/base/dragdrop/drag_utils.h" |
| 6 | 6 |
| 7 #include "base/files/file_util.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/macros.h" |
| 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "build/build_config.h" |
| 7 #include "ui/base/dragdrop/os_exchange_data.h" | 12 #include "ui/base/dragdrop/os_exchange_data.h" |
| 8 #include "ui/gfx/canvas.h" | 13 #include "ui/gfx/canvas.h" |
| 9 #include "ui/gfx/image/image_skia.h" | 14 #include "ui/gfx/font_list.h" |
| 15 #include "ui/gfx/geometry/point.h" |
| 16 #include "ui/gfx/geometry/rect.h" |
| 17 #include "ui/gfx/geometry/size.h" |
| 18 #include "ui/gfx/image/canvas_image_source.h" |
| 19 #include "url/gurl.h" |
| 10 | 20 |
| 11 namespace drag_utils { | 21 namespace drag_utils { |
| 12 | 22 |
| 23 namespace { |
| 24 |
| 25 // Maximum width of the link drag image in pixels. |
| 26 static const int kLinkDragImageVPadding = 3; |
| 27 |
| 28 // File dragging pixel measurements |
| 29 static const int kFileDragImageMaxWidth = 200; |
| 30 static const SkColor kFileDragImageTextColor = SK_ColorBLACK; |
| 31 |
| 32 class FileDragImageSource : public gfx::CanvasImageSource { |
| 33 public: |
| 34 FileDragImageSource(const base::FilePath& file_name, |
| 35 const gfx::ImageSkia& icon) |
| 36 : CanvasImageSource(CalculateSize(icon), false), |
| 37 file_name_(file_name), |
| 38 icon_(icon) { |
| 39 } |
| 40 |
| 41 ~FileDragImageSource() override {} |
| 42 |
| 43 // Overridden from gfx::CanvasImageSource. |
| 44 void Draw(gfx::Canvas* canvas) override { |
| 45 if (!icon_.isNull()) { |
| 46 // Paint the icon. |
| 47 canvas->DrawImageInt(icon_, (size().width() - icon_.width()) / 2, 0); |
| 48 } |
| 49 |
| 50 base::string16 name = file_name_.BaseName().LossyDisplayName(); |
| 51 const int flags = gfx::Canvas::TEXT_ALIGN_CENTER; |
| 52 const gfx::FontList font_list; |
| 53 #if defined(OS_WIN) |
| 54 // Paint the file name. We inset it one pixel to allow room for the halo. |
| 55 const gfx::Rect rect(1, icon_.height() + kLinkDragImageVPadding + 1, |
| 56 size().width() - 2, font_list.GetHeight()); |
| 57 canvas->DrawStringRectWithHalo(name, font_list, kFileDragImageTextColor, |
| 58 SK_ColorWHITE, rect, flags); |
| 59 #else |
| 60 // NO_SUBPIXEL_RENDERING is required when drawing to a non-opaque canvas. |
| 61 const gfx::Rect rect(0, icon_.height() + kLinkDragImageVPadding, |
| 62 size().width(), font_list.GetHeight()); |
| 63 canvas->DrawStringRectWithFlags(name, font_list, kFileDragImageTextColor, |
| 64 rect, |
| 65 flags | gfx::Canvas::NO_SUBPIXEL_RENDERING); |
| 66 #endif |
| 67 } |
| 68 |
| 69 private: |
| 70 gfx::Size CalculateSize(const gfx::ImageSkia& icon) const { |
| 71 const int width = kFileDragImageMaxWidth; |
| 72 // Add +2 here to allow room for the halo. |
| 73 const int height = gfx::FontList().GetHeight() + icon.height() + |
| 74 kLinkDragImageVPadding + 2; |
| 75 return gfx::Size(width, height); |
| 76 } |
| 77 |
| 78 const base::FilePath file_name_; |
| 79 const gfx::ImageSkia icon_; |
| 80 |
| 81 DISALLOW_COPY_AND_ASSIGN(FileDragImageSource); |
| 82 }; |
| 83 |
| 84 } // namespace |
| 85 |
| 86 void CreateDragImageForFile(const base::FilePath& file_name, |
| 87 const gfx::ImageSkia& icon, |
| 88 ui::OSExchangeData* data_object) { |
| 89 DCHECK(data_object); |
| 90 gfx::CanvasImageSource* source = new FileDragImageSource(file_name, icon); |
| 91 gfx::Size size = source->size(); |
| 92 // ImageSkia takes ownership of |source|. |
| 93 gfx::ImageSkia image = gfx::ImageSkia(source, size); |
| 94 |
| 95 gfx::Vector2d cursor_offset(size.width() / 2, kLinkDragImageVPadding); |
| 96 SetDragImageOnDataObject(image, cursor_offset, data_object); |
| 97 } |
| 98 |
| 13 void SetDragImageOnDataObject(const gfx::Canvas& canvas, | 99 void SetDragImageOnDataObject(const gfx::Canvas& canvas, |
| 14 const gfx::Vector2d& cursor_offset, | 100 const gfx::Vector2d& cursor_offset, |
| 15 ui::OSExchangeData* data_object) { | 101 ui::OSExchangeData* data_object) { |
| 16 gfx::ImageSkia image = gfx::ImageSkia(canvas.ExtractImageRep()); | 102 gfx::ImageSkia image = gfx::ImageSkia(canvas.ExtractImageRep()); |
| 17 SetDragImageOnDataObject(image, cursor_offset, data_object); | 103 SetDragImageOnDataObject(image, cursor_offset, data_object); |
| 18 } | 104 } |
| 19 | 105 |
| 20 #if !defined(OS_WIN) | 106 #if !defined(OS_WIN) |
| 21 void SetDragImageOnDataObject(const gfx::ImageSkia& image_skia, | 107 void SetDragImageOnDataObject(const gfx::ImageSkia& image_skia, |
| 22 const gfx::Vector2d& cursor_offset, | 108 const gfx::Vector2d& cursor_offset, |
| 23 ui::OSExchangeData* data_object) { | 109 ui::OSExchangeData* data_object) { |
| 24 data_object->provider().SetDragImage(image_skia, cursor_offset); | 110 data_object->provider().SetDragImage(image_skia, cursor_offset); |
| 25 } | 111 } |
| 26 #endif | 112 #endif |
| 27 | 113 |
| 28 } // namespace drag_utils | 114 } // namespace drag_utils |
| OLD | NEW |