diff options
author | estade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-03-18 17:10:18 +0000 |
---|---|---|
committer | estade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-03-18 17:10:18 +0000 |
commit | 2ca8a064a206fba910dc0f50f8a2f2509828d8f1 (patch) | |
tree | 9e128f90d2392b901aca101baabe12dd36621943 /chrome/browser/gtk | |
parent | 911f815112d8d11778c771467fbef2c2b1f8e160 (diff) | |
download | chromium_src-2ca8a064a206fba910dc0f50f8a2f2509828d8f1.zip chromium_src-2ca8a064a206fba910dc0f50f8a2f2509828d8f1.tar.gz chromium_src-2ca8a064a206fba910dc0f50f8a2f2509828d8f1.tar.bz2 |
GTK: implement extension bookmark manager drag api.
Also, cross-platform: eschew RenderViewHostDelegate:BookmarkDrag::DragData type in favor of BookmarkDragData.
BUG=37702
TEST=manual
Review URL: http://codereview.chromium.org/1029004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@41959 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/gtk')
-rw-r--r-- | chrome/browser/gtk/bookmark_bar_gtk.cc | 7 | ||||
-rw-r--r-- | chrome/browser/gtk/bookmark_manager_gtk.cc | 10 | ||||
-rw-r--r-- | chrome/browser/gtk/bookmark_utils_gtk.cc | 22 | ||||
-rw-r--r-- | chrome/browser/gtk/bookmark_utils_gtk.h | 4 | ||||
-rw-r--r-- | chrome/browser/gtk/custom_drag.cc | 134 | ||||
-rw-r--r-- | chrome/browser/gtk/custom_drag.h | 95 | ||||
-rw-r--r-- | chrome/browser/gtk/download_item_drag.cc | 83 | ||||
-rw-r--r-- | chrome/browser/gtk/download_item_drag.h | 43 | ||||
-rw-r--r-- | chrome/browser/gtk/download_item_gtk.cc | 2 |
9 files changed, 254 insertions, 146 deletions
diff --git a/chrome/browser/gtk/bookmark_bar_gtk.cc b/chrome/browser/gtk/bookmark_bar_gtk.cc index 2ae1421..c1054ab 100644 --- a/chrome/browser/gtk/bookmark_bar_gtk.cc +++ b/chrome/browser/gtk/bookmark_bar_gtk.cc @@ -837,12 +837,7 @@ GtkWidget* BookmarkBarGtk::CreateBookmarkButton(const BookmarkNode* node) { // The tool item is also a source for dragging gtk_drag_source_set(button, GDK_BUTTON1_MASK, NULL, 0, static_cast<GdkDragAction>(GDK_ACTION_MOVE | GDK_ACTION_COPY)); - int target_mask = gtk_dnd_util::CHROME_BOOKMARK_ITEM; - if (node->is_url()) { - target_mask |= gtk_dnd_util::TEXT_URI_LIST | - gtk_dnd_util::TEXT_PLAIN | - gtk_dnd_util::NETSCAPE_URL; - } + int target_mask = bookmark_utils::GetCodeMask(node->is_folder()); gtk_dnd_util::SetSourceTargetListFromCodeMask(button, target_mask); g_signal_connect(button, "drag-begin", G_CALLBACK(&OnButtonDragBeginThunk), this); diff --git a/chrome/browser/gtk/bookmark_manager_gtk.cc b/chrome/browser/gtk/bookmark_manager_gtk.cc index b023a44c..848b306 100644 --- a/chrome/browser/gtk/bookmark_manager_gtk.cc +++ b/chrome/browser/gtk/bookmark_manager_gtk.cc @@ -60,12 +60,6 @@ const int kDefaultColumnWidth = 200; // The destination targets that the right tree view accepts for dragging. const int kDestTargetList[] = { gtk_dnd_util::CHROME_BOOKMARK_ITEM, -1 }; -// The source targets that the right tree view supports for dragging. -const int kSourceTargetMask = gtk_dnd_util::CHROME_BOOKMARK_ITEM | - gtk_dnd_util::TEXT_URI_LIST | - gtk_dnd_util::TEXT_PLAIN | - gtk_dnd_util::NETSCAPE_URL; - // We only have one manager open at a time. BookmarkManagerGtk* manager = NULL; @@ -559,7 +553,7 @@ GtkWidget* BookmarkManagerGtk::MakeRightPane() { static_cast<GdkDragAction>(GDK_ACTION_MOVE | GDK_ACTION_COPY | GDK_ACTION_LINK)); gtk_dnd_util::SetSourceTargetListFromCodeMask( - right_tree_view_, kSourceTargetMask); + right_tree_view_, bookmark_utils::GetCodeMask(false)); // We connect to drag dest signals, but we don't actually enable the widget // as a drag destination unless it corresponds to the contents of a folder. @@ -1301,7 +1295,7 @@ gboolean BookmarkManagerGtk::OnRightTreeViewMotion( static_cast<gint>(event->y))) { bm->delaying_mousedown_ = false; GtkTargetList* targets = gtk_dnd_util::GetTargetListFromCodeMask( - kSourceTargetMask); + bookmark_utils::GetCodeMask(false)); gtk_drag_begin(tree_view, targets, GDK_ACTION_MOVE, 1, reinterpret_cast<GdkEvent*>(event)); // The drag adds a ref; let it own the list. diff --git a/chrome/browser/gtk/bookmark_utils_gtk.cc b/chrome/browser/gtk/bookmark_utils_gtk.cc index 73b3feda..c15c9fb 100644 --- a/chrome/browser/gtk/bookmark_utils_gtk.cc +++ b/chrome/browser/gtk/bookmark_utils_gtk.cc @@ -198,6 +198,16 @@ void SetButtonTextColors(GtkWidget* label, GtkThemeProvider* provider) { // DnD-related ----------------------------------------------------------------- +int GetCodeMask(bool folder) { + int rv = gtk_dnd_util::CHROME_BOOKMARK_ITEM; + if (!folder) { + rv |= gtk_dnd_util::TEXT_URI_LIST | + gtk_dnd_util::TEXT_PLAIN | + gtk_dnd_util::NETSCAPE_URL; + } + return rv; +} + void WriteBookmarkToSelection(const BookmarkNode* node, GtkSelectionData* selection_data, guint target_type, @@ -272,17 +282,19 @@ std::vector<const BookmarkNode*> GetNodesFromSelection( Profile* profile, gboolean* delete_selection_data, gboolean* dnd_success) { - *delete_selection_data = FALSE; - *dnd_success = FALSE; + if (delete_selection_data) + *delete_selection_data = FALSE; + if (dnd_success) + *dnd_success = FALSE; if ((selection_data != NULL) && (selection_data->length >= 0)) { - if (context->action == GDK_ACTION_MOVE) { + if (context && delete_selection_data && context->action == GDK_ACTION_MOVE) *delete_selection_data = TRUE; - } switch (target_type) { case gtk_dnd_util::CHROME_BOOKMARK_ITEM: { - *dnd_success = TRUE; + if (dnd_success) + *dnd_success = TRUE; Pickle pickle(reinterpret_cast<char*>(selection_data->data), selection_data->length); BookmarkDragData drag_data; diff --git a/chrome/browser/gtk/bookmark_utils_gtk.h b/chrome/browser/gtk/bookmark_utils_gtk.h index de56039..012ce96 100644 --- a/chrome/browser/gtk/bookmark_utils_gtk.h +++ b/chrome/browser/gtk/bookmark_utils_gtk.h @@ -48,6 +48,10 @@ void SetButtonTextColors(GtkWidget* label, GtkThemeProvider* provider); // Drag and drop. -------------------------------------------------------------- +// Get the DnD target mask for a bookmark drag. This will vary based on whether +// the node in question is a folder. +int GetCodeMask(bool folder); + // Pickle a node into a GtkSelection. void WriteBookmarkToSelection(const BookmarkNode* node, GtkSelectionData* selection_data, diff --git a/chrome/browser/gtk/custom_drag.cc b/chrome/browser/gtk/custom_drag.cc new file mode 100644 index 0000000..7cff653 --- /dev/null +++ b/chrome/browser/gtk/custom_drag.cc @@ -0,0 +1,134 @@ +// 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_manager.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 = GDK_ACTION_COPY; + +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) { + gtk_drag_source_set(widget, GDK_BUTTON1_MASK, NULL, 0, + kDownloadItemDragAction); + gtk_dnd_util::SetSourceTargetListFromCodeMask(widget, kDownloadItemCodeMask); + g_signal_connect(widget, "drag-data-get", + G_CALLBACK(OnDragDataGetStandalone), item); +} + +// 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); +} + diff --git a/chrome/browser/gtk/custom_drag.h b/chrome/browser/gtk/custom_drag.h new file mode 100644 index 0000000..a43b0da --- /dev/null +++ b/chrome/browser/gtk/custom_drag.h @@ -0,0 +1,95 @@ +// 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. + +#ifndef CHROME_BROWSER_GTK_CUSTOM_DRAG_H_ +#define CHROME_BROWSER_GTK_CUSTOM_DRAG_H_ + +#include <gtk/gtk.h> +#include <vector> + +#include "app/gtk_signal.h" +#include "base/basictypes.h" + +class BookmarkNode; +class DownloadItem; +class Profile; +class SkBitmap; + +// Base class for programatically generated drags. +class CustomDrag { + protected: + explicit CustomDrag(SkBitmap* icon, int code_mask, GdkDragAction action); + virtual ~CustomDrag(); + + virtual void OnDragDataGet(GtkWidget* widget, GdkDragContext* context, + GtkSelectionData* selection_data, + guint target_type, guint time) = 0; + + private: + CHROMEGTK_CALLBACK_1(CustomDrag, void, OnDragBegin, GdkDragContext*); + CHROMEGTK_CALLBACK_1(CustomDrag, void, OnDragEnd, GdkDragContext*); + + // Since this uses a virtual function, we can't use a macro. + static void OnDragDataGetThunk(GtkWidget* widget, GdkDragContext* context, + GtkSelectionData* selection_data, + guint target_type, guint time, + CustomDrag* custom_drag) { + return custom_drag->OnDragDataGet(widget, context, selection_data, + target_type, time); + } + + GtkWidget* drag_widget_; + GdkPixbuf* pixbuf_; + + DISALLOW_COPY_AND_ASSIGN(CustomDrag); +}; + +// Encapsulates functionality for drags of download items. +class DownloadItemDrag : public CustomDrag { + public: + // Sets |widget| as a source for drags pertaining to |item|. No + // DownloadItemDrag object is created. + static void SetSource(GtkWidget* widget, DownloadItem* item); + + // Creates a new DownloadItemDrag, the lifetime of which is tied to the + // system drag. + static void BeginDrag(const DownloadItem* item, SkBitmap* icon); + + private: + DownloadItemDrag(const DownloadItem* item, SkBitmap* icon); + virtual ~DownloadItemDrag(); + + virtual void OnDragDataGet(GtkWidget* widget, GdkDragContext* context, + GtkSelectionData* selection_data, + guint target_type, guint time); + + const DownloadItem* download_item_; + + DISALLOW_COPY_AND_ASSIGN(DownloadItemDrag); +}; + +// Encapsulates functionality for drags of one or more bookmarks. +class BookmarkDrag : public CustomDrag { + public: + // Creates a new BookmarkDrag, the lifetime of which is tied to the + // system drag. + static void BeginDrag(Profile* profile, + const std::vector<const BookmarkNode*>& nodes); + + private: + BookmarkDrag(Profile* profile, + const std::vector<const BookmarkNode*>& nodes); + virtual ~BookmarkDrag(); + + virtual void OnDragDataGet(GtkWidget* widget, GdkDragContext* context, + GtkSelectionData* selection_data, + guint target_type, guint time); + + Profile* profile_; + std::vector<const BookmarkNode*> nodes_; + + DISALLOW_COPY_AND_ASSIGN(BookmarkDrag); +}; + +#endif // CHROME_BROWSER_GTK_CUSTOM_DRAG_H_ diff --git a/chrome/browser/gtk/download_item_drag.cc b/chrome/browser/gtk/download_item_drag.cc deleted file mode 100644 index 24c312e..0000000 --- a/chrome/browser/gtk/download_item_drag.cc +++ /dev/null @@ -1,83 +0,0 @@ -// 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/download_item_drag.h" - -#include "app/gtk_dnd_util.h" -#include "base/utf_string_conversions.h" -#include "chrome/browser/download/download_manager.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 kCodeMask = gtk_dnd_util::TEXT_URI_LIST | - gtk_dnd_util::CHROME_NAMED_URL; -const GdkDragAction kDragAction = GDK_ACTION_COPY; - -void OnDragDataGet(GtkWidget* widget, GdkDragContext* context, - GtkSelectionData* selection_data, - guint target_type, guint time, - 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); -} - -} // namespace - -// static -void DownloadItemDrag::SetSource(GtkWidget* widget, DownloadItem* item) { - gtk_drag_source_set(widget, GDK_BUTTON1_MASK, NULL, 0, - kDragAction); - gtk_dnd_util::SetSourceTargetListFromCodeMask(widget, kCodeMask); - g_signal_connect(widget, "drag-data-get", - G_CALLBACK(OnDragDataGet), item); -} - -// static -void DownloadItemDrag::BeginDrag(const DownloadItem* item, SkBitmap* icon) { - new DownloadItemDrag(item, icon); -} - -DownloadItemDrag::DownloadItemDrag(const DownloadItem* item, - SkBitmap* icon) - : drag_widget_(gtk_invisible_new()), - pixbuf_(gfx::GdkPixbufFromSkBitmap(icon)) { - g_object_ref_sink(drag_widget_); - g_signal_connect(drag_widget_, "drag-data-get", - G_CALLBACK(OnDragDataGet), const_cast<DownloadItem*>(item)); - g_signal_connect(drag_widget_, "drag-begin", - G_CALLBACK(OnDragBegin), this); - g_signal_connect(drag_widget_, "drag-end", - G_CALLBACK(OnDragEnd), this); - - GtkTargetList* list = gtk_dnd_util::GetTargetListFromCodeMask(kCodeMask); - GdkEvent* event = gtk_get_current_event(); - gtk_drag_begin(drag_widget_, list, kDragAction, 1, event); - if (event) - gdk_event_free(event); - gtk_target_list_unref(list); -} - -DownloadItemDrag::~DownloadItemDrag() { - g_object_unref(pixbuf_); - g_object_unref(drag_widget_); -} - -// static -void DownloadItemDrag::OnDragBegin(GtkWidget* widget, - GdkDragContext* drag_context, - DownloadItemDrag* drag) { - gtk_drag_set_icon_pixbuf(drag_context, drag->pixbuf_, 0, 0); -} - -// static -void DownloadItemDrag::OnDragEnd(GtkWidget* widget, - GdkDragContext* drag_context, - DownloadItemDrag* drag) { - delete drag; -} diff --git a/chrome/browser/gtk/download_item_drag.h b/chrome/browser/gtk/download_item_drag.h deleted file mode 100644 index 1f8df44..0000000 --- a/chrome/browser/gtk/download_item_drag.h +++ /dev/null @@ -1,43 +0,0 @@ -// 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. - -#ifndef CHROME_BROWSER_GTK_DOWNLOAD_ITEM_DRAG_H_ -#define CHROME_BROWSER_GTK_DOWNLOAD_ITEM_DRAG_H_ - -#include <gtk/gtk.h> - -#include "base/basictypes.h" - -class DownloadItem; -class SkBitmap; - -class DownloadItemDrag { - public: - // Sets |widget| as a source for drags pertaining to |item|. No - // DownloadItemDrag object is created. - static void SetSource(GtkWidget* widget, DownloadItem* item); - - // Creates a new DownloadItemDrag, the lifetime of which is tied to the - // system drag. - static void BeginDrag(const DownloadItem* item, SkBitmap* icon); - - private: - explicit DownloadItemDrag(const DownloadItem* item, SkBitmap* icon); - ~DownloadItemDrag(); - - static void OnDragBegin(GtkWidget* widget, - GdkDragContext* drag_context, - DownloadItemDrag* drag); - - static void OnDragEnd(GtkWidget* widget, - GdkDragContext* drag_context, - DownloadItemDrag* drag); - - GtkWidget* drag_widget_; - GdkPixbuf* pixbuf_; - - DISALLOW_COPY_AND_ASSIGN(DownloadItemDrag); -}; - -#endif // CHROME_BROWSER_GTK_DOWNLOAD_ITEM_DRAG_H_ diff --git a/chrome/browser/gtk/download_item_gtk.cc b/chrome/browser/gtk/download_item_gtk.cc index 264b003..d996bc5 100644 --- a/chrome/browser/gtk/download_item_gtk.cc +++ b/chrome/browser/gtk/download_item_gtk.cc @@ -24,7 +24,7 @@ #include "chrome/browser/download/download_manager.h" #include "chrome/browser/download/download_shelf.h" #include "chrome/browser/download/download_util.h" -#include "chrome/browser/gtk/download_item_drag.h" +#include "chrome/browser/gtk/custom_drag.h" #include "chrome/browser/gtk/download_shelf_gtk.h" #include "chrome/browser/gtk/gtk_theme_provider.h" #include "chrome/browser/gtk/gtk_util.h" |