Roll src/third_party/WebKit b944946:201fd41 (svn 192400:192407)
[chromium-blink-merge.git] / ui / views / button_drag_utils.cc
blob5dd2c2eb0ff6ff30d9769d0460156decd723fd17
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/gfx/canvas.h"
12 #include "ui/gfx/geometry/point.h"
13 #include "ui/gfx/geometry/vector2d.h"
14 #include "ui/gfx/image/image.h"
15 #include "ui/resources/grit/ui_resources.h"
16 #include "ui/views/controls/button/label_button.h"
17 #include "ui/views/drag_utils.h"
18 #include "ui/views/widget/widget.h"
19 #include "url/gurl.h"
21 namespace button_drag_utils {
23 // Maximum width of the link drag image in pixels.
24 static const int kLinkDragImageMaxWidth = 150;
26 void SetURLAndDragImage(const GURL& url,
27 const base::string16& title,
28 const gfx::ImageSkia& icon,
29 const gfx::Point* press_pt,
30 ui::OSExchangeData* data,
31 views::Widget* widget) {
32 DCHECK(url.is_valid() && data);
33 data->SetURL(url, title);
34 SetDragImage(url, title, icon, press_pt, data, widget);
37 void SetDragImage(const GURL& url,
38 const base::string16& title,
39 const gfx::ImageSkia& icon,
40 const gfx::Point* press_pt,
41 ui::OSExchangeData* data,
42 views::Widget* widget) {
43 // Create a button to render the drag image for us.
44 views::LabelButton button(NULL,
45 title.empty() ? base::UTF8ToUTF16(url.spec())
46 : title);
47 button.SetTextSubpixelRenderingEnabled(false);
48 const ui::NativeTheme* theme =
49 widget ? widget->GetNativeTheme() : ui::NativeTheme::instance();
50 button.SetTextColor(views::Button::STATE_NORMAL,
51 theme->GetSystemColor(ui::NativeTheme::kColorId_LabelEnabledColor));
52 gfx::ShadowValues shadows(
53 10,
54 gfx::ShadowValue(gfx::Vector2d(0, 0), 1.0f,
55 theme->GetSystemColor(
56 ui::NativeTheme::kColorId_LabelBackgroundColor)));
57 button.SetTextShadows(shadows);
58 button.SetMaxSize(gfx::Size(kLinkDragImageMaxWidth, 0));
59 if (icon.isNull()) {
60 button.SetImage(views::Button::STATE_NORMAL,
61 *ui::ResourceBundle::GetSharedInstance().GetImageNamed(
62 IDR_DEFAULT_FAVICON).ToImageSkia());
63 } else {
64 button.SetImage(views::Button::STATE_NORMAL, icon);
66 gfx::Size prefsize = button.GetPreferredSize();
67 button.SetBounds(0, 0, prefsize.width(), prefsize.height());
69 gfx::Vector2d press_point;
70 if (press_pt)
71 press_point = press_pt->OffsetFromOrigin();
72 else
73 press_point = gfx::Vector2d(prefsize.width() / 2, prefsize.height() / 2);
75 // Render the image.
76 scoped_ptr<gfx::Canvas> canvas(
77 views::GetCanvasForDragImage(widget, prefsize));
78 button.Paint(canvas.get(), views::CullSet());
79 drag_utils::SetDragImageOnDataObject(*canvas, press_point, data);
82 } // namespace button_drag_utils