• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/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:
FileDragImageSource(const base::FilePath & file_name,const gfx::ImageSkia & icon)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 
~FileDragImageSource()39   virtual ~FileDragImageSource() {
40   }
41 
42   // Overridden from gfx::CanvasImageSource.
Draw(gfx::Canvas * canvas)43   virtual void Draw(gfx::Canvas* canvas) OVERRIDE {
44     if (!icon_.isNull()) {
45       // Paint the icon.
46       canvas->DrawImageInt(icon_, (size().width() - icon_.width()) / 2, 0);
47     }
48 
49     base::string16 name = file_name_.BaseName().LossyDisplayName();
50     const int flags = gfx::Canvas::TEXT_ALIGN_CENTER;
51     const gfx::FontList font_list;
52 #if defined(OS_WIN)
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);
58 #else
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,
63                                     rect,
64                                     flags | gfx::Canvas::NO_SUBPIXEL_RENDERING);
65 #endif
66   }
67 
68  private:
CalculateSize(const gfx::ImageSkia & icon) const69   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);
75   }
76 
77   const base::FilePath file_name_;
78   const gfx::ImageSkia icon_;
79 
80   DISALLOW_COPY_AND_ASSIGN(FileDragImageSource);
81 };
82 
83 }  // namespace
84 
CreateDragImageForFile(const base::FilePath & file_name,const gfx::ImageSkia & icon,ui::OSExchangeData * data_object)85 void CreateDragImageForFile(const base::FilePath& file_name,
86                             const gfx::ImageSkia& icon,
87                             ui::OSExchangeData* data_object) {
88   DCHECK(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);
93 
94   gfx::Vector2d cursor_offset(size.width() / 2, kLinkDragImageVPadding);
95   SetDragImageOnDataObject(image, cursor_offset, data_object);
96 }
97 
SetDragImageOnDataObject(const gfx::Canvas & canvas,const gfx::Vector2d & cursor_offset,ui::OSExchangeData * data_object)98 void SetDragImageOnDataObject(const gfx::Canvas& canvas,
99                               const gfx::Vector2d& cursor_offset,
100                               ui::OSExchangeData* data_object) {
101   gfx::ImageSkia image = gfx::ImageSkia(canvas.ExtractImageRep());
102   SetDragImageOnDataObject(image, cursor_offset, data_object);
103 }
104 
105 }  // namespace drag_utils
106