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
3 // found in the LICENSE file.
5 #include "ui/base/dragdrop/drag_utils.h"
7 #include "base/files/file_util.h"
8 #include "base/logging.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "ui/base/dragdrop/os_exchange_data.h"
11 #include "ui/gfx/canvas.h"
12 #include "ui/gfx/font_list.h"
13 #include "ui/gfx/geometry/point.h"
14 #include "ui/gfx/geometry/rect.h"
15 #include "ui/gfx/geometry/size.h"
16 #include "ui/gfx/image/canvas_image_source.h"
19 namespace drag_utils
{
23 // Maximum width of the link drag image in pixels.
24 static const int kLinkDragImageVPadding
= 3;
26 // File dragging pixel measurements
27 static const int kFileDragImageMaxWidth
= 200;
28 static const SkColor kFileDragImageTextColor
= SK_ColorBLACK
;
30 class FileDragImageSource
: public gfx::CanvasImageSource
{
32 FileDragImageSource(const base::FilePath
& file_name
,
33 const gfx::ImageSkia
& icon
)
34 : CanvasImageSource(CalculateSize(icon
), false),
35 file_name_(file_name
),
39 ~FileDragImageSource() override
{}
41 // Overridden from gfx::CanvasImageSource.
42 void Draw(gfx::Canvas
* canvas
) override
{
43 if (!icon_
.isNull()) {
45 canvas
->DrawImageInt(icon_
, (size().width() - icon_
.width()) / 2, 0);
48 base::string16 name
= file_name_
.BaseName().LossyDisplayName();
49 const int flags
= gfx::Canvas::TEXT_ALIGN_CENTER
;
50 const gfx::FontList font_list
;
52 // Paint the file name. We inset it one pixel to allow room for the halo.
53 const gfx::Rect
rect(1, icon_
.height() + kLinkDragImageVPadding
+ 1,
54 size().width() - 2, font_list
.GetHeight());
55 canvas
->DrawStringRectWithHalo(name
, font_list
, kFileDragImageTextColor
,
56 SK_ColorWHITE
, rect
, flags
);
58 // NO_SUBPIXEL_RENDERING is required when drawing to a non-opaque canvas.
59 const gfx::Rect
rect(0, icon_
.height() + kLinkDragImageVPadding
,
60 size().width(), font_list
.GetHeight());
61 canvas
->DrawStringRectWithFlags(name
, font_list
, kFileDragImageTextColor
,
63 flags
| gfx::Canvas::NO_SUBPIXEL_RENDERING
);
68 gfx::Size
CalculateSize(const gfx::ImageSkia
& icon
) const {
69 const int width
= kFileDragImageMaxWidth
;
70 // Add +2 here to allow room for the halo.
71 const int height
= gfx::FontList().GetHeight() + icon
.height() +
72 kLinkDragImageVPadding
+ 2;
73 return gfx::Size(width
, height
);
76 const base::FilePath file_name_
;
77 const gfx::ImageSkia icon_
;
79 DISALLOW_COPY_AND_ASSIGN(FileDragImageSource
);
84 void CreateDragImageForFile(const base::FilePath
& file_name
,
85 const gfx::ImageSkia
& icon
,
86 ui::OSExchangeData
* data_object
) {
88 gfx::CanvasImageSource
* source
= new FileDragImageSource(file_name
, icon
);
89 gfx::Size size
= source
->size();
90 // ImageSkia takes ownership of |source|.
91 gfx::ImageSkia image
= gfx::ImageSkia(source
, size
);
93 gfx::Vector2d
cursor_offset(size
.width() / 2, kLinkDragImageVPadding
);
94 SetDragImageOnDataObject(image
, cursor_offset
, data_object
);
97 void SetDragImageOnDataObject(const gfx::Canvas
& canvas
,
98 const gfx::Vector2d
& cursor_offset
,
99 ui::OSExchangeData
* data_object
) {
100 gfx::ImageSkia image
= gfx::ImageSkia(canvas
.ExtractImageRep());
101 SetDragImageOnDataObject(image
, cursor_offset
, data_object
);
104 } // namespace drag_utils