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