1 // Copyright (c) 2011 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 "views/drag_utils.h"
7 #include "base/file_util.h"
8 #include "base/logging.h"
9 #include "base/utf_string_conversions.h"
10 #include "googleurl/src/gurl.h"
11 #include "grit/ui_resources.h"
12 #include "ui/base/dragdrop/os_exchange_data.h"
13 #include "ui/base/resource/resource_bundle.h"
14 #include "ui/gfx/canvas_skia.h"
15 #include "ui/gfx/font.h"
16 #include "views/controls/button/text_button.h"
18 using ui::OSExchangeData
;
20 namespace drag_utils
{
22 // Maximum width of the link drag image in pixels.
23 static const int kLinkDragImageMaxWidth
= 200;
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 void SetURLAndDragImage(const GURL
& url
,
31 const string16
& title
,
33 OSExchangeData
* data
) {
34 DCHECK(url
.is_valid() && data
);
36 data
->SetURL(url
, title
);
38 // Create a button to render the drag image for us.
39 views::TextButton
button(NULL
,
40 title
.empty() ? UTF8ToUTF16(url
.spec()) : title
);
41 button
.set_max_width(kLinkDragImageMaxWidth
);
43 button
.SetIcon(*ResourceBundle::GetSharedInstance().GetBitmapNamed(
44 IDR_DEFAULT_FAVICON
));
48 gfx::Size prefsize
= button
.GetPreferredSize();
49 button
.SetBounds(0, 0, prefsize
.width(), prefsize
.height());
52 gfx::CanvasSkia
canvas(prefsize
.width(), prefsize
.height(), false);
53 button
.PaintButton(&canvas
, views::TextButton::PB_FOR_DRAG
);
54 SetDragImageOnDataObject(canvas
, prefsize
,
55 gfx::Point(prefsize
.width() / 2, prefsize
.height() / 2), data
);
58 void CreateDragImageForFile(const FilePath
& file_name
,
60 OSExchangeData
* data_object
) {
64 // Set up our text portion
65 ResourceBundle
& rb
= ResourceBundle::GetSharedInstance();
66 gfx::Font font
= rb
.GetFont(ResourceBundle::BaseFont
);
68 const int width
= kFileDragImageMaxWidth
;
69 // Add +2 here to allow room for the halo.
70 const int height
= font
.GetHeight() + icon
->height() +
71 kLinkDragImageVPadding
+ 2;
72 gfx::CanvasSkia
canvas(width
, height
, false /* translucent */);
75 canvas
.DrawBitmapInt(*icon
, (width
- icon
->width()) / 2, 0);
77 std::wstring name
= UTF16ToWide(file_name
.BaseName().LossyDisplayName());
79 // Paint the file name. We inset it one pixel to allow room for the halo.
80 canvas
.DrawStringWithHalo(name
, font
, kFileDragImageTextColor
, SK_ColorWHITE
,
81 1, icon
->height() + kLinkDragImageVPadding
+ 1,
82 width
- 2, font
.GetHeight(),
83 gfx::Canvas::TEXT_ALIGN_CENTER
);
85 canvas
.DrawStringInt(WideToUTF16Hack(name
), font
, kFileDragImageTextColor
,
86 0, icon
->height() + kLinkDragImageVPadding
,
87 width
, font
.GetHeight(), gfx::Canvas::TEXT_ALIGN_CENTER
);
90 SetDragImageOnDataObject(canvas
, gfx::Size(width
, height
),
91 gfx::Point(width
/ 2, kLinkDragImageVPadding
),
95 void SetDragImageOnDataObject(const gfx::Canvas
& canvas
,
96 const gfx::Size
& size
,
97 const gfx::Point
& cursor_offset
,
98 OSExchangeData
* data_object
) {
99 SetDragImageOnDataObject(
100 canvas
.AsCanvasSkia()->ExtractBitmap(), size
, cursor_offset
, data_object
);
103 } // namespace drag_utils