Home | History | Annotate | Download | only in dragdrop
      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/base/resource/resource_bundle.h"
     12 #include "ui/gfx/canvas.h"
     13 #include "ui/gfx/font.h"
     14 #include "ui/gfx/image/canvas_image_source.h"
     15 #include "ui/gfx/point.h"
     16 #include "ui/gfx/size.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   virtual ~FileDragImageSource() {
     40   }
     41 
     42   // Overridden from gfx::CanvasImageSource.
     43   virtual void Draw(gfx::Canvas* canvas) OVERRIDE {
     44     // Set up our text portion
     45     ResourceBundle& rb = ResourceBundle::GetSharedInstance();
     46     gfx::Font font = rb.GetFont(ResourceBundle::BaseFont);
     47 
     48     // Paint the icon.
     49     canvas->DrawImageInt(icon_, (size().width() - icon_.width()) / 2, 0);
     50 
     51     base::string16 name = file_name_.BaseName().LossyDisplayName();
     52     const int flags = gfx::Canvas::TEXT_ALIGN_CENTER;
     53 #if defined(OS_WIN)
     54     // Paint the file name. We inset it one pixel to allow room for the halo.
     55     canvas->DrawStringWithHalo(name, font, kFileDragImageTextColor,
     56                                SK_ColorWHITE, 1,
     57                                icon_.height() + kLinkDragImageVPadding + 1,
     58                                size().width() - 2, font.GetHeight(), flags);
     59 #else
     60     // NO_SUBPIXEL_RENDERING is required when drawing to a non-opaque canvas.
     61     canvas->DrawStringInt(name, font, kFileDragImageTextColor,
     62                           0, icon_.height() + kLinkDragImageVPadding,
     63                           size().width(), font.GetHeight(),
     64                           flags | gfx::Canvas::NO_SUBPIXEL_RENDERING);
     65 #endif
     66   }
     67 
     68  private:
     69   gfx::Size CalculateSize(const gfx::ImageSkia& icon) const {
     70     ResourceBundle& rb = ResourceBundle::GetSharedInstance();
     71     gfx::Font font = rb.GetFont(ResourceBundle::BaseFont);
     72     const int width = kFileDragImageMaxWidth;
     73     // Add +2 here to allow room for the halo.
     74     const int height = font.GetHeight() + icon.height() +
     75                        kLinkDragImageVPadding + 2;
     76     return gfx::Size(width, height);
     77   }
     78 
     79   const base::FilePath file_name_;
     80   const gfx::ImageSkia icon_;
     81 
     82   DISALLOW_COPY_AND_ASSIGN(FileDragImageSource);
     83 };
     84 
     85 }  // namespace
     86 
     87 void CreateDragImageForFile(const base::FilePath& file_name,
     88                             const gfx::ImageSkia* icon,
     89                             ui::OSExchangeData* data_object) {
     90   DCHECK(icon);
     91   DCHECK(data_object);
     92   gfx::CanvasImageSource* source = new FileDragImageSource(file_name, *icon);
     93   gfx::Size size = source->size();
     94   // ImageSkia takes ownership of |source|.
     95   gfx::ImageSkia image = gfx::ImageSkia(source, size);
     96 
     97   gfx::Vector2d cursor_offset(size.width() / 2, kLinkDragImageVPadding);
     98   SetDragImageOnDataObject(image, size, cursor_offset, data_object);
     99 }
    100 
    101 void SetDragImageOnDataObject(const gfx::Canvas& canvas,
    102                               const gfx::Size& size,
    103                               const gfx::Vector2d& cursor_offset,
    104                               ui::OSExchangeData* data_object) {
    105   gfx::ImageSkia image = gfx::ImageSkia(canvas.ExtractImageRep());
    106   SetDragImageOnDataObject(image, size, cursor_offset, data_object);
    107 }
    108 
    109 }  // namespace drag_utils
    110