summaryrefslogtreecommitdiffstats
path: root/chrome/browser/gtk/custom_drag.cc
blob: 9eb3b8566c1f1212a85cfabab5930c1995e48d41 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright (c) 2010 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 "chrome/browser/gtk/custom_drag.h"

#include "app/gtk_dnd_util.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/download/download_item.h"
#include "chrome/browser/gtk/bookmark_utils_gtk.h"
#include "gfx/gtk_util.h"
#include "googleurl/src/gurl.h"
#include "net/base/net_util.h"
#include "third_party/skia/include/core/SkBitmap.h"

namespace {

const int kDownloadItemCodeMask = gtk_dnd_util::TEXT_URI_LIST |
                                  gtk_dnd_util::CHROME_NAMED_URL;
const GdkDragAction kDownloadItemDragAction = GDK_ACTION_COPY;
const GdkDragAction kBookmarkDragAction =
    static_cast<GdkDragAction>(GDK_ACTION_COPY | GDK_ACTION_MOVE);

void OnDragDataGetForDownloadItem(GtkSelectionData* selection_data,
                                  guint target_type,
                                  const DownloadItem* download_item) {
  GURL url = net::FilePathToFileURL(download_item->full_path());
  gtk_dnd_util::WriteURLWithName(selection_data, url,
      UTF8ToUTF16(download_item->GetFileName().value()), target_type);
}

void OnDragDataGetStandalone(GtkWidget* widget, GdkDragContext* context,
                             GtkSelectionData* selection_data,
                             guint target_type, guint time,
                             const DownloadItem* item) {
  OnDragDataGetForDownloadItem(selection_data, target_type, item);
}

}  // namespace

// CustomDrag ------------------------------------------------------------------

CustomDrag::CustomDrag(SkBitmap* icon, int code_mask, GdkDragAction action)
    : drag_widget_(gtk_invisible_new()),
      pixbuf_(icon ? gfx::GdkPixbufFromSkBitmap(icon) : NULL) {
  g_object_ref_sink(drag_widget_);
  g_signal_connect(drag_widget_, "drag-data-get",
                   G_CALLBACK(OnDragDataGetThunk), this);
  g_signal_connect(drag_widget_, "drag-begin",
                   G_CALLBACK(OnDragBeginThunk), this);
  g_signal_connect(drag_widget_, "drag-end",
                   G_CALLBACK(OnDragEndThunk), this);

  GtkTargetList* list = gtk_dnd_util::GetTargetListFromCodeMask(code_mask);
  GdkEvent* event = gtk_get_current_event();
  gtk_drag_begin(drag_widget_, list, action, 1, event);
  if (event)
    gdk_event_free(event);
  gtk_target_list_unref(list);
}

CustomDrag::~CustomDrag() {
  if (pixbuf_)
    g_object_unref(pixbuf_);
  g_object_unref(drag_widget_);
}

void CustomDrag::OnDragBegin(GtkWidget* widget, GdkDragContext* drag_context) {
  if (pixbuf_)
    gtk_drag_set_icon_pixbuf(drag_context, pixbuf_, 0, 0);
}

void CustomDrag::OnDragEnd(GtkWidget* widget, GdkDragContext* drag_context) {
  delete this;
}

// DownloadItemDrag ------------------------------------------------------------

DownloadItemDrag::DownloadItemDrag(const DownloadItem* item,
                                   SkBitmap* icon)
    : CustomDrag(icon, kDownloadItemCodeMask, kDownloadItemDragAction),
      download_item_(item) {
}

DownloadItemDrag::~DownloadItemDrag() {
}

void DownloadItemDrag::OnDragDataGet(
    GtkWidget* widget, GdkDragContext* context,
    GtkSelectionData* selection_data,
    guint target_type, guint time) {
  OnDragDataGetForDownloadItem(selection_data, target_type, download_item_);
}

// static
void DownloadItemDrag::SetSource(GtkWidget* widget,
                                 DownloadItem* item,
                                 SkBitmap* icon) {
  gtk_drag_source_set(widget, GDK_BUTTON1_MASK, NULL, 0,
                      kDownloadItemDragAction);
  gtk_dnd_util::SetSourceTargetListFromCodeMask(widget, kDownloadItemCodeMask);

  // Disconnect previous signal handlers, if any.
  g_signal_handlers_disconnect_by_func(
      widget,
      reinterpret_cast<gpointer>(OnDragDataGetStandalone),
      item);
  // Connect new signal handlers.
  g_signal_connect(widget, "drag-data-get",
                   G_CALLBACK(OnDragDataGetStandalone), item);

  GdkPixbuf* pixbuf = icon ? gfx::GdkPixbufFromSkBitmap(icon) : NULL;
  if (pixbuf) {
    gtk_drag_source_set_icon_pixbuf(widget, pixbuf);
    g_object_unref(pixbuf);
  }
}

// static
void DownloadItemDrag::BeginDrag(const DownloadItem* item, SkBitmap* icon) {
  new DownloadItemDrag(item, icon);
}

// BookmarkDrag ----------------------------------------------------------------

BookmarkDrag::BookmarkDrag(Profile* profile,
                           const std::vector<const BookmarkNode*>& nodes)
    : CustomDrag(NULL,
                 bookmark_utils::GetCodeMask(false),
                 kBookmarkDragAction),
      profile_(profile),
      nodes_(nodes) {
}

BookmarkDrag::~BookmarkDrag() {
}

void BookmarkDrag::OnDragDataGet(GtkWidget* widget, GdkDragContext* context,
                                 GtkSelectionData* selection_data,
                                 guint target_type, guint time) {
  bookmark_utils::WriteBookmarksToSelection(nodes_, selection_data,
                                            target_type, profile_);
}

// static
void BookmarkDrag::BeginDrag(Profile* profile,
                             const std::vector<const BookmarkNode*>& nodes) {
  new BookmarkDrag(profile, nodes);
}