| 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" | |
| 12 #include "ui/base/dragdrop/os_exchange_data.h" | 7 #include "ui/base/dragdrop/os_exchange_data.h" |
| 13 #include "ui/gfx/canvas.h" | 8 #include "ui/gfx/canvas.h" |
| 14 #include "ui/gfx/font_list.h" | 9 #include "ui/gfx/image/image_skia.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" | |
| 20 | 10 |
| 21 namespace drag_utils { | 11 namespace drag_utils { |
| 22 | 12 |
| 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 | |
| 99 void SetDragImageOnDataObject(const gfx::Canvas& canvas, | 13 void SetDragImageOnDataObject(const gfx::Canvas& canvas, |
| 100 const gfx::Vector2d& cursor_offset, | 14 const gfx::Vector2d& cursor_offset, |
| 101 ui::OSExchangeData* data_object) { | 15 ui::OSExchangeData* data_object) { |
| 102 gfx::ImageSkia image = gfx::ImageSkia(canvas.ExtractImageRep()); | 16 gfx::ImageSkia image = gfx::ImageSkia(canvas.ExtractImageRep()); |
| 103 SetDragImageOnDataObject(image, cursor_offset, data_object); | 17 SetDragImageOnDataObject(image, cursor_offset, data_object); |
| 104 } | 18 } |
| 105 | 19 |
| 106 #if !defined(OS_WIN) | 20 #if !defined(OS_WIN) |
| 107 void SetDragImageOnDataObject(const gfx::ImageSkia& image_skia, | 21 void SetDragImageOnDataObject(const gfx::ImageSkia& image_skia, |
| 108 const gfx::Vector2d& cursor_offset, | 22 const gfx::Vector2d& cursor_offset, |
| 109 ui::OSExchangeData* data_object) { | 23 ui::OSExchangeData* data_object) { |
| 110 data_object->provider().SetDragImage(image_skia, cursor_offset); | 24 data_object->provider().SetDragImage(image_skia, cursor_offset); |
| 111 } | 25 } |
| 112 #endif | 26 #endif |
| 113 | 27 |
| 114 } // namespace drag_utils | 28 } // namespace drag_utils |
| OLD | NEW |