Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / ui / base / dragdrop / drag_utils.cc
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.
4
5 #include "ui/base/dragdrop/drag_utils.h"
6
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"
17 #include "url/gurl.h"
18
19 namespace drag_utils {
20
21 namespace {
22
23 // Maximum width of the link drag image in pixels.
24 static const int kLinkDragImageVPadding = 3;
25
26 // File dragging pixel measurements
27 static const int kFileDragImageMaxWidth = 200;
28 static const SkColor kFileDragImageTextColor = SK_ColorBLACK;
29
30 class FileDragImageSource : public gfx::CanvasImageSource {
31  public:
32   FileDragImageSource(const base::FilePath& file_name,
33                       const gfx::ImageSkia& icon)
34       : CanvasImageSource(CalculateSize(icon), false),
35         file_name_(file_name),
36         icon_(icon) {
37   }
38
39   ~FileDragImageSource() override {}
40
41   // Overridden from gfx::CanvasImageSource.
42   void Draw(gfx::Canvas* canvas) override {
43     if (!icon_.isNull()) {
44       // Paint the icon.
45       canvas->DrawImageInt(icon_, (size().width() - icon_.width()) / 2, 0);
46     }
47
48     base::string16 name = file_name_.BaseName().LossyDisplayName();
49     const int flags = gfx::Canvas::TEXT_ALIGN_CENTER;
50     const gfx::FontList font_list;
51 #if defined(OS_WIN)
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);
57 #else
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,
62                                     rect,
63                                     flags | gfx::Canvas::NO_SUBPIXEL_RENDERING);
64 #endif
65   }
66
67  private:
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);
74   }
75
76   const base::FilePath file_name_;
77   const gfx::ImageSkia icon_;
78
79   DISALLOW_COPY_AND_ASSIGN(FileDragImageSource);
80 };
81
82 }  // namespace
83
84 void CreateDragImageForFile(const base::FilePath& file_name,
85                             const gfx::ImageSkia& icon,
86                             ui::OSExchangeData* data_object) {
87   DCHECK(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);
92
93   gfx::Vector2d cursor_offset(size.width() / 2, kLinkDragImageVPadding);
94   SetDragImageOnDataObject(image, cursor_offset, data_object);
95 }
96
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);
102 }
103
104 }  // namespace drag_utils