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/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 virtual ~FileDragImageSource() {
42 // Overridden from gfx::CanvasImageSource.
43 virtual void Draw(gfx::Canvas
* canvas
) OVERRIDE
{
44 if (!icon_
.isNull()) {
46 canvas
->DrawImageInt(icon_
, (size().width() - icon_
.width()) / 2, 0);
49 base::string16 name
= file_name_
.BaseName().LossyDisplayName();
50 const int flags
= gfx::Canvas::TEXT_ALIGN_CENTER
;
51 const gfx::FontList font_list
;
53 // Paint the file name. We inset it one pixel to allow room for the halo.
54 const gfx::Rect
rect(1, icon_
.height() + kLinkDragImageVPadding
+ 1,
55 size().width() - 2, font_list
.GetHeight());
56 canvas
->DrawStringRectWithHalo(name
, font_list
, kFileDragImageTextColor
,
57 SK_ColorWHITE
, rect
, flags
);
59 // NO_SUBPIXEL_RENDERING is required when drawing to a non-opaque canvas.
60 const gfx::Rect
rect(0, icon_
.height() + kLinkDragImageVPadding
,
61 size().width(), font_list
.GetHeight());
62 canvas
->DrawStringRectWithFlags(name
, font_list
, kFileDragImageTextColor
,
64 flags
| gfx::Canvas::NO_SUBPIXEL_RENDERING
);
69 gfx::Size
CalculateSize(const gfx::ImageSkia
& icon
) const {
70 const int width
= kFileDragImageMaxWidth
;
71 // Add +2 here to allow room for the halo.
72 const int height
= gfx::FontList().GetHeight() + icon
.height() +
73 kLinkDragImageVPadding
+ 2;
74 return gfx::Size(width
, height
);
77 const base::FilePath file_name_
;
78 const gfx::ImageSkia icon_
;
80 DISALLOW_COPY_AND_ASSIGN(FileDragImageSource
);
85 void CreateDragImageForFile(const base::FilePath
& file_name
,
86 const gfx::ImageSkia
& icon
,
87 ui::OSExchangeData
* data_object
) {
89 gfx::CanvasImageSource
* source
= new FileDragImageSource(file_name
, icon
);
90 gfx::Size size
= source
->size();
91 // ImageSkia takes ownership of |source|.
92 gfx::ImageSkia image
= gfx::ImageSkia(source
, size
);
94 gfx::Vector2d
cursor_offset(size
.width() / 2, kLinkDragImageVPadding
);
95 SetDragImageOnDataObject(image
, size
, cursor_offset
, data_object
);
98 void SetDragImageOnDataObject(const gfx::Canvas
& canvas
,
99 const gfx::Size
& size
,
100 const gfx::Vector2d
& cursor_offset
,
101 ui::OSExchangeData
* data_object
) {
102 gfx::ImageSkia image
= gfx::ImageSkia(canvas
.ExtractImageRep());
103 SetDragImageOnDataObject(image
, size
, cursor_offset
, data_object
);
106 } // namespace drag_utils