| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2010 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. | |
| 4 | |
| 5 #include "chrome/browser/gtk/custom_drag.h" | |
| 6 | |
| 7 #include "app/gtk_dnd_util.h" | |
| 8 #include "base/utf_string_conversions.h" | |
| 9 #include "chrome/browser/download/download_item.h" | |
| 10 #include "chrome/browser/gtk/bookmark_utils_gtk.h" | |
| 11 #include "gfx/gtk_util.h" | |
| 12 #include "googleurl/src/gurl.h" | |
| 13 #include "net/base/net_util.h" | |
| 14 #include "third_party/skia/include/core/SkBitmap.h" | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 const int kDownloadItemCodeMask = gtk_dnd_util::TEXT_URI_LIST | | |
| 19 gtk_dnd_util::CHROME_NAMED_URL; | |
| 20 const GdkDragAction kDownloadItemDragAction = GDK_ACTION_COPY; | |
| 21 const GdkDragAction kBookmarkDragAction = | |
| 22 static_cast<GdkDragAction>(GDK_ACTION_COPY | GDK_ACTION_MOVE); | |
| 23 | |
| 24 void OnDragDataGetForDownloadItem(GtkSelectionData* selection_data, | |
| 25 guint target_type, | |
| 26 const DownloadItem* download_item) { | |
| 27 GURL url = net::FilePathToFileURL(download_item->full_path()); | |
| 28 gtk_dnd_util::WriteURLWithName(selection_data, url, | |
| 29 UTF8ToUTF16(download_item->GetFileNameToReportUser().value()), | |
| 30 target_type); | |
| 31 } | |
| 32 | |
| 33 void OnDragDataGetStandalone(GtkWidget* widget, GdkDragContext* context, | |
| 34 GtkSelectionData* selection_data, | |
| 35 guint target_type, guint time, | |
| 36 const DownloadItem* item) { | |
| 37 OnDragDataGetForDownloadItem(selection_data, target_type, item); | |
| 38 } | |
| 39 | |
| 40 } // namespace | |
| 41 | |
| 42 // CustomDrag ------------------------------------------------------------------ | |
| 43 | |
| 44 CustomDrag::CustomDrag(SkBitmap* icon, int code_mask, GdkDragAction action) | |
| 45 : drag_widget_(gtk_invisible_new()), | |
| 46 pixbuf_(icon ? gfx::GdkPixbufFromSkBitmap(icon) : NULL) { | |
| 47 g_signal_connect(drag_widget_, "drag-data-get", | |
| 48 G_CALLBACK(OnDragDataGetThunk), this); | |
| 49 g_signal_connect(drag_widget_, "drag-begin", | |
| 50 G_CALLBACK(OnDragBeginThunk), this); | |
| 51 g_signal_connect(drag_widget_, "drag-end", | |
| 52 G_CALLBACK(OnDragEndThunk), this); | |
| 53 | |
| 54 GtkTargetList* list = gtk_dnd_util::GetTargetListFromCodeMask(code_mask); | |
| 55 GdkEvent* event = gtk_get_current_event(); | |
| 56 gtk_drag_begin(drag_widget_, list, action, 1, event); | |
| 57 if (event) | |
| 58 gdk_event_free(event); | |
| 59 gtk_target_list_unref(list); | |
| 60 } | |
| 61 | |
| 62 CustomDrag::~CustomDrag() { | |
| 63 if (pixbuf_) | |
| 64 g_object_unref(pixbuf_); | |
| 65 gtk_widget_destroy(drag_widget_); | |
| 66 } | |
| 67 | |
| 68 void CustomDrag::OnDragBegin(GtkWidget* widget, GdkDragContext* drag_context) { | |
| 69 if (pixbuf_) | |
| 70 gtk_drag_set_icon_pixbuf(drag_context, pixbuf_, 0, 0); | |
| 71 } | |
| 72 | |
| 73 void CustomDrag::OnDragEnd(GtkWidget* widget, GdkDragContext* drag_context) { | |
| 74 delete this; | |
| 75 } | |
| 76 | |
| 77 // DownloadItemDrag ------------------------------------------------------------ | |
| 78 | |
| 79 DownloadItemDrag::DownloadItemDrag(const DownloadItem* item, | |
| 80 SkBitmap* icon) | |
| 81 : CustomDrag(icon, kDownloadItemCodeMask, kDownloadItemDragAction), | |
| 82 download_item_(item) { | |
| 83 } | |
| 84 | |
| 85 DownloadItemDrag::~DownloadItemDrag() { | |
| 86 } | |
| 87 | |
| 88 void DownloadItemDrag::OnDragDataGet( | |
| 89 GtkWidget* widget, GdkDragContext* context, | |
| 90 GtkSelectionData* selection_data, | |
| 91 guint target_type, guint time) { | |
| 92 OnDragDataGetForDownloadItem(selection_data, target_type, download_item_); | |
| 93 } | |
| 94 | |
| 95 // static | |
| 96 void DownloadItemDrag::SetSource(GtkWidget* widget, | |
| 97 DownloadItem* item, | |
| 98 SkBitmap* icon) { | |
| 99 gtk_drag_source_set(widget, GDK_BUTTON1_MASK, NULL, 0, | |
| 100 kDownloadItemDragAction); | |
| 101 gtk_dnd_util::SetSourceTargetListFromCodeMask(widget, kDownloadItemCodeMask); | |
| 102 | |
| 103 // Disconnect previous signal handlers, if any. | |
| 104 g_signal_handlers_disconnect_by_func( | |
| 105 widget, | |
| 106 reinterpret_cast<gpointer>(OnDragDataGetStandalone), | |
| 107 item); | |
| 108 // Connect new signal handlers. | |
| 109 g_signal_connect(widget, "drag-data-get", | |
| 110 G_CALLBACK(OnDragDataGetStandalone), item); | |
| 111 | |
| 112 GdkPixbuf* pixbuf = icon ? gfx::GdkPixbufFromSkBitmap(icon) : NULL; | |
| 113 if (pixbuf) { | |
| 114 gtk_drag_source_set_icon_pixbuf(widget, pixbuf); | |
| 115 g_object_unref(pixbuf); | |
| 116 } | |
| 117 } | |
| 118 | |
| 119 // static | |
| 120 void DownloadItemDrag::BeginDrag(const DownloadItem* item, SkBitmap* icon) { | |
| 121 new DownloadItemDrag(item, icon); | |
| 122 } | |
| 123 | |
| 124 // BookmarkDrag ---------------------------------------------------------------- | |
| 125 | |
| 126 BookmarkDrag::BookmarkDrag(Profile* profile, | |
| 127 const std::vector<const BookmarkNode*>& nodes) | |
| 128 : CustomDrag(NULL, | |
| 129 bookmark_utils::GetCodeMask(false), | |
| 130 kBookmarkDragAction), | |
| 131 profile_(profile), | |
| 132 nodes_(nodes) { | |
| 133 } | |
| 134 | |
| 135 BookmarkDrag::~BookmarkDrag() { | |
| 136 } | |
| 137 | |
| 138 void BookmarkDrag::OnDragDataGet(GtkWidget* widget, GdkDragContext* context, | |
| 139 GtkSelectionData* selection_data, | |
| 140 guint target_type, guint time) { | |
| 141 bookmark_utils::WriteBookmarksToSelection(nodes_, selection_data, | |
| 142 target_type, profile_); | |
| 143 } | |
| 144 | |
| 145 // static | |
| 146 void BookmarkDrag::BeginDrag(Profile* profile, | |
| 147 const std::vector<const BookmarkNode*>& nodes) { | |
| 148 new BookmarkDrag(profile, nodes); | |
| 149 } | |
| OLD | NEW |