summaryrefslogtreecommitdiffstats
path: root/ui/views/button_drag_utils.cc
blob: 8bdfe941bb739f41d37f41674e14cab609da12e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/views/button_drag_utils.h"

#include "base/utf_string_conversions.h"
#include "googleurl/src/gurl.h"
#include "grit/ui_resources.h"
#include "ui/base/dragdrop/drag_utils.h"
#include "ui/base/dragdrop/os_exchange_data.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/image/image.h"
#include "ui/views/controls/button/text_button.h"
#include "ui/views/drag_utils.h"

namespace button_drag_utils {

// Maximum width of the link drag image in pixels.
static const int kLinkDragImageMaxWidth = 200;

void SetURLAndDragImage(const GURL& url,
                        const string16& title,
                        const gfx::ImageSkia& icon,
                        ui::OSExchangeData* data,
                        views::Widget* widget) {
  DCHECK(url.is_valid() && data);

  data->SetURL(url, title);

  // Create a button to render the drag image for us.
  views::TextButton button(NULL,
                           title.empty() ? UTF8ToUTF16(url.spec()) : title);
  button.set_max_width(kLinkDragImageMaxWidth);
  if (icon.isNull()) {
    button.SetIcon(*ui::ResourceBundle::GetSharedInstance().GetImageNamed(
                   IDR_DEFAULT_FAVICON).ToImageSkia());
  } else {
    button.SetIcon(icon);
  }
  gfx::Size prefsize = button.GetPreferredSize();
  button.SetBounds(0, 0, prefsize.width(), prefsize.height());

  // Render the image.
  scoped_ptr<gfx::Canvas> canvas(
      views::GetCanvasForDragImage(widget, prefsize));
  button.PaintButton(canvas.get(), views::TextButton::PB_FOR_DRAG);
  drag_utils::SetDragImageOnDataObject(*canvas, prefsize,
      gfx::Point(prefsize.width() / 2, prefsize.height() / 2), data);
}

}  // namespace button_drag_utils