1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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
|