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.
5 #include "ui/views/button_drag_utils.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "ui/base/dragdrop/drag_utils.h"
9 #include "ui/base/dragdrop/os_exchange_data.h"
10 #include "ui/base/resource/resource_bundle.h"
11 #include "ui/compositor/paint_context.h"
12 #include "ui/gfx/canvas.h"
13 #include "ui/gfx/geometry/point.h"
14 #include "ui/gfx/geometry/vector2d.h"
15 #include "ui/gfx/image/image.h"
16 #include "ui/resources/grit/ui_resources.h"
17 #include "ui/views/controls/button/label_button.h"
18 #include "ui/views/drag_utils.h"
19 #include "ui/views/widget/widget.h"
22 namespace button_drag_utils
{
24 // Maximum width of the link drag image in pixels.
25 static const int kLinkDragImageMaxWidth
= 150;
27 void SetURLAndDragImage(const GURL
& url
,
28 const base::string16
& title
,
29 const gfx::ImageSkia
& icon
,
30 const gfx::Point
* press_pt
,
31 ui::OSExchangeData
* data
,
32 views::Widget
* widget
) {
33 DCHECK(url
.is_valid() && data
);
34 data
->SetURL(url
, title
);
35 SetDragImage(url
, title
, icon
, press_pt
, data
, widget
);
38 void SetDragImage(const GURL
& url
,
39 const base::string16
& title
,
40 const gfx::ImageSkia
& icon
,
41 const gfx::Point
* press_pt
,
42 ui::OSExchangeData
* data
,
43 views::Widget
* widget
) {
44 // Create a button to render the drag image for us.
45 views::LabelButton
button(NULL
,
46 title
.empty() ? base::UTF8ToUTF16(url
.spec())
48 button
.SetTextSubpixelRenderingEnabled(false);
49 const ui::NativeTheme
* theme
=
50 widget
? widget
->GetNativeTheme() : ui::NativeTheme::instance();
51 button
.SetTextColor(views::Button::STATE_NORMAL
,
52 theme
->GetSystemColor(ui::NativeTheme::kColorId_LabelEnabledColor
));
53 gfx::ShadowValues
shadows(
55 gfx::ShadowValue(gfx::Vector2d(0, 0), 1.0f
,
56 theme
->GetSystemColor(
57 ui::NativeTheme::kColorId_LabelBackgroundColor
)));
58 button
.SetTextShadows(shadows
);
59 button
.SetMaxSize(gfx::Size(kLinkDragImageMaxWidth
, 0));
61 button
.SetImage(views::Button::STATE_NORMAL
,
62 *ui::ResourceBundle::GetSharedInstance().GetImageNamed(
63 IDR_DEFAULT_FAVICON
).ToImageSkia());
65 button
.SetImage(views::Button::STATE_NORMAL
, icon
);
67 gfx::Size prefsize
= button
.GetPreferredSize();
68 button
.SetBounds(0, 0, prefsize
.width(), prefsize
.height());
70 gfx::Vector2d press_point
;
72 press_point
= press_pt
->OffsetFromOrigin();
74 press_point
= gfx::Vector2d(prefsize
.width() / 2, prefsize
.height() / 2);
77 scoped_ptr
<gfx::Canvas
> canvas(
78 views::GetCanvasForDragImage(widget
, prefsize
));
79 button
.Paint(ui::PaintContext(canvas
.get()));
80 drag_utils::SetDragImageOnDataObject(*canvas
, press_point
, data
);
83 } // namespace button_drag_utils