summaryrefslogtreecommitdiffstats
path: root/chrome/browser/gtk/bookmark_utils_gtk.cc
diff options
context:
space:
mode:
authorestade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-06-05 19:15:33 +0000
committerestade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-06-05 19:15:33 +0000
commite5f72c76f42ca9163a6691191ae0b1f471e81952 (patch)
tree9c7b26bedda7d4eaa5fa2f645ddd177f83636e6e /chrome/browser/gtk/bookmark_utils_gtk.cc
parentb7007b63f6e05341f9bdaec7ff08611ed8da3375 (diff)
downloadchromium_src-e5f72c76f42ca9163a6691191ae0b1f471e81952.zip
chromium_src-e5f72c76f42ca9163a6691191ae0b1f471e81952.tar.gz
chromium_src-e5f72c76f42ca9163a6691191ae0b1f471e81952.tar.bz2
Implement some drag-n-drop for the bookmark manager.
Factor out shared code into bookmark_utils_gtk.*. Fix a leak with toolbar button dragging. BUG=13110 TEST=Added extra DCHECKs. Also, you can drag between the right half of the bookmark manager and the bookmark bar with no crashes. Review URL: http://codereview.chromium.org/119220 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@17754 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/gtk/bookmark_utils_gtk.cc')
-rw-r--r--chrome/browser/gtk/bookmark_utils_gtk.cc127
1 files changed, 127 insertions, 0 deletions
diff --git a/chrome/browser/gtk/bookmark_utils_gtk.cc b/chrome/browser/gtk/bookmark_utils_gtk.cc
new file mode 100644
index 0000000..9858f3e
--- /dev/null
+++ b/chrome/browser/gtk/bookmark_utils_gtk.cc
@@ -0,0 +1,127 @@
+// Copyright (c) 2009 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/bookmark_utils_gtk.h"
+
+#include "app/resource_bundle.h"
+#include "base/gfx/gtk_util.h"
+#include "base/pickle.h"
+#include "chrome/browser/bookmarks/bookmark_drag_data.h"
+#include "chrome/browser/bookmarks/bookmark_model.h"
+#include "chrome/browser/gtk/dnd_registry.h"
+#include "chrome/browser/profile.h"
+#include "grit/app_resources.h"
+#include "grit/generated_resources.h"
+#include "grit/theme_resources.h"
+
+// Used in gtk_selection_data_set(). (I assume from this parameter that gtk has
+// to some really exotic hardware...)
+const int kBitsInAByte = 8;
+
+namespace bookmark_utils {
+
+// Mime types for DnD. Used to synchronize across applications.
+const char kInternalURIType[] = "application/x-chrome-bookmark-item";
+
+// Table of the mime types that we accept with their options.
+const GtkTargetEntry kTargetTable[] = {
+ { const_cast<char*>(kInternalURIType),
+ GTK_TARGET_SAME_APP,
+ dnd::X_CHROME_BOOKMARK_ITEM }
+ // TODO(erg): Add "text/uri-list" support.
+};
+
+const int kTargetTableSize = G_N_ELEMENTS(kTargetTable);
+
+GdkPixbuf* GetFolderIcon() {
+ ResourceBundle& rb = ResourceBundle::GetSharedInstance();
+ static GdkPixbuf* default_folder_icon = rb.GetPixbufNamed(
+ IDR_BOOKMARK_BAR_FOLDER);
+ return default_folder_icon;
+}
+
+GdkPixbuf* GetDefaultFavicon() {
+ ResourceBundle& rb = ResourceBundle::GetSharedInstance();
+ static GdkPixbuf* default_bookmark_icon = rb.GetPixbufNamed(
+ IDR_DEFAULT_FAVICON);
+ return default_bookmark_icon;
+}
+
+GdkPixbuf* GetPixbufForNode(BookmarkNode* node, BookmarkModel* model) {
+ GdkPixbuf* pixbuf;
+
+ if (node->is_url()) {
+ if (model->GetFavIcon(node).width() != 0) {
+ pixbuf = gfx::GdkPixbufFromSkBitmap(&model->GetFavIcon(node));
+ } else {
+ pixbuf = GetDefaultFavicon();
+ g_object_ref(pixbuf);
+ }
+ } else {
+ pixbuf = GetFolderIcon();
+ g_object_ref(pixbuf);
+ }
+
+ return pixbuf;
+}
+
+// DnD-related -----------------------------------------------------------------
+
+void WriteBookmarkToSelection(BookmarkNode* node,
+ GtkSelectionData* selection_data,
+ guint target_type,
+ Profile* profile) {
+ DCHECK(node);
+ switch (target_type) {
+ case dnd::X_CHROME_BOOKMARK_ITEM: {
+ BookmarkDragData data(node);
+ Pickle pickle;
+ data.WriteToPickle(profile, &pickle);
+
+ gtk_selection_data_set(selection_data, selection_data->target,
+ kBitsInAByte,
+ static_cast<const guchar*>(pickle.data()),
+ pickle.size());
+ break;
+ }
+ default: {
+ DLOG(ERROR) << "Unsupported drag get type!";
+ }
+ }
+}
+
+std::vector<BookmarkNode*> GetNodesFromSelection(
+ GdkDragContext* context,
+ GtkSelectionData* selection_data,
+ guint target_type,
+ Profile* profile,
+ gboolean* delete_selection_data,
+ gboolean* dnd_success) {
+ *delete_selection_data = FALSE;
+ *dnd_success = FALSE;
+
+ if ((selection_data != NULL) && (selection_data->length >= 0)) {
+ if (context->action == GDK_ACTION_MOVE) {
+ *delete_selection_data = TRUE;
+ }
+
+ switch (target_type) {
+ case dnd::X_CHROME_BOOKMARK_ITEM: {
+ *dnd_success = TRUE;
+ Pickle pickle(reinterpret_cast<char*>(selection_data->data),
+ selection_data->length);
+ BookmarkDragData drag_data;
+ drag_data.ReadFromPickle(&pickle);
+ return drag_data.GetNodes(profile);
+ }
+ default: {
+ DLOG(ERROR) << "Unsupported drag received type: " << target_type;
+ }
+ }
+ }
+
+ return std::vector<BookmarkNode*>();
+}
+
+} // namespace bookmark_utils