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/canvas_painter.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/resources/grit/views_resources.h"
20 #include "ui/views/widget/widget.h"
23 namespace button_drag_utils
{
25 // Maximum width of the link drag image in pixels.
26 static const int kLinkDragImageMaxWidth
= 150;
28 void SetURLAndDragImage(const GURL
& url
,
29 const base::string16
& title
,
30 const gfx::ImageSkia
& icon
,
31 const gfx::Point
* press_pt
,
32 ui::OSExchangeData
* data
,
33 views::Widget
* widget
) {
34 DCHECK(url
.is_valid() && data
);
35 data
->SetURL(url
, title
);
36 SetDragImage(url
, title
, icon
, press_pt
, data
, widget
);
39 void SetDragImage(const GURL
& url
,
40 const base::string16
& title
,
41 const gfx::ImageSkia
& icon
,
42 const gfx::Point
* press_pt
,
43 ui::OSExchangeData
* data
,
44 views::Widget
* widget
) {
45 // Create a button to render the drag image for us.
46 views::LabelButton
button(NULL
,
47 title
.empty() ? base::UTF8ToUTF16(url
.spec())
49 button
.SetTextSubpixelRenderingEnabled(false);
50 const ui::NativeTheme
* theme
=
51 widget
? widget
->GetNativeTheme() : ui::NativeTheme::instance();
52 button
.SetTextColor(views::Button::STATE_NORMAL
,
53 theme
->GetSystemColor(ui::NativeTheme::kColorId_LabelEnabledColor
));
54 gfx::ShadowValues
shadows(
56 gfx::ShadowValue(gfx::Vector2d(0, 0), 1.0f
,
57 theme
->GetSystemColor(
58 ui::NativeTheme::kColorId_LabelBackgroundColor
)));
59 button
.SetTextShadows(shadows
);
60 button
.SetMaxSize(gfx::Size(kLinkDragImageMaxWidth
, 0));
62 button
.SetImage(views::Button::STATE_NORMAL
,
63 *ui::ResourceBundle::GetSharedInstance().GetImageNamed(
64 IDR_DEFAULT_FAVICON
).ToImageSkia());
66 button
.SetImage(views::Button::STATE_NORMAL
, icon
);
68 gfx::Size prefsize
= button
.GetPreferredSize();
69 button
.SetBounds(0, 0, prefsize
.width(), prefsize
.height());
71 gfx::Vector2d press_point
;
73 press_point
= press_pt
->OffsetFromOrigin();
75 press_point
= gfx::Vector2d(prefsize
.width() / 2, prefsize
.height() / 2);
78 scoped_ptr
<gfx::Canvas
> canvas(
79 views::GetCanvasForDragImage(widget
, prefsize
));
80 button
.Paint(ui::CanvasPainter(canvas
.get(), 1.f
).context());
81 drag_utils::SetDragImageOnDataObject(*canvas
, press_point
, data
);
84 } // namespace button_drag_utils