// 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(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(pickle.data()), pickle.size()); break; } default: { DLOG(ERROR) << "Unsupported drag get type!"; } } } std::vector 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(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(); } } // namespace bookmark_utils