This source file includes following definitions.
- icon_
- Draw
- CalculateSize
- CreateDragImageForFile
- SetDragImageOnDataObject
#include "ui/base/dragdrop/drag_utils.h"
#include "base/file_util.h"
#include "base/logging.h"
#include "base/strings/utf_string_conversions.h"
#include "ui/base/dragdrop/os_exchange_data.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/font_list.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/image/canvas_image_source.h"
#include "url/gurl.h"
namespace drag_utils {
namespace {
static const int kLinkDragImageVPadding = 3;
static const int kFileDragImageMaxWidth = 200;
static const SkColor kFileDragImageTextColor = SK_ColorBLACK;
class FileDragImageSource : public gfx::CanvasImageSource {
public:
FileDragImageSource(const base::FilePath& file_name,
const gfx::ImageSkia& icon)
: CanvasImageSource(CalculateSize(icon), false),
file_name_(file_name),
icon_(icon) {
}
virtual ~FileDragImageSource() {
}
virtual void Draw(gfx::Canvas* canvas) OVERRIDE {
if (!icon_.isNull()) {
canvas->DrawImageInt(icon_, (size().width() - icon_.width()) / 2, 0);
}
base::string16 name = file_name_.BaseName().LossyDisplayName();
const int flags = gfx::Canvas::TEXT_ALIGN_CENTER;
const gfx::FontList font_list;
#if defined(OS_WIN)
const gfx::Rect rect(1, icon_.height() + kLinkDragImageVPadding + 1,
size().width() - 2, font_list.GetHeight());
canvas->DrawStringRectWithHalo(name, font_list, kFileDragImageTextColor,
SK_ColorWHITE, rect, flags);
#else
const gfx::Rect rect(0, icon_.height() + kLinkDragImageVPadding,
size().width(), font_list.GetHeight());
canvas->DrawStringRectWithFlags(name, font_list, kFileDragImageTextColor,
rect,
flags | gfx::Canvas::NO_SUBPIXEL_RENDERING);
#endif
}
private:
gfx::Size CalculateSize(const gfx::ImageSkia& icon) const {
const int width = kFileDragImageMaxWidth;
const int height = gfx::FontList().GetHeight() + icon.height() +
kLinkDragImageVPadding + 2;
return gfx::Size(width, height);
}
const base::FilePath file_name_;
const gfx::ImageSkia icon_;
DISALLOW_COPY_AND_ASSIGN(FileDragImageSource);
};
}
void CreateDragImageForFile(const base::FilePath& file_name,
const gfx::ImageSkia& icon,
ui::OSExchangeData* data_object) {
DCHECK(data_object);
gfx::CanvasImageSource* source = new FileDragImageSource(file_name, icon);
gfx::Size size = source->size();
gfx::ImageSkia image = gfx::ImageSkia(source, size);
gfx::Vector2d cursor_offset(size.width() / 2, kLinkDragImageVPadding);
SetDragImageOnDataObject(image, size, cursor_offset, data_object);
}
void SetDragImageOnDataObject(const gfx::Canvas& canvas,
const gfx::Size& size,
const gfx::Vector2d& cursor_offset,
ui::OSExchangeData* data_object) {
gfx::ImageSkia image = gfx::ImageSkia(canvas.ExtractImageRep());
SetDragImageOnDataObject(image, size, cursor_offset, data_object);
}
}