OLD | NEW |
| (Empty) |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/views/bookmark_folder_tree_view.h" | |
6 | |
7 #include <commctrl.h> | |
8 | |
9 #include "app/drag_drop_types.h" | |
10 #include "app/os_exchange_data.h" | |
11 #include "app/os_exchange_data_provider_win.h" | |
12 #include "base/base_drag_source.h" | |
13 #include "chrome/browser/bookmarks/bookmark_utils.h" | |
14 #include "chrome/browser/bookmarks/bookmark_model.h" | |
15 #include "chrome/browser/bookmarks/bookmark_folder_tree_model.h" | |
16 #include "chrome/browser/profile.h" | |
17 #include "grit/generated_resources.h" | |
18 #include "views/view_constants.h" | |
19 | |
20 void BookmarkFolderTreeView::DropInfo::Scrolled() { | |
21 view_->UpdateDropInfo(); | |
22 } | |
23 | |
24 BookmarkFolderTreeView::BookmarkFolderTreeView(Profile* profile, | |
25 BookmarkFolderTreeModel* model) | |
26 : views::TreeView(), | |
27 profile_(profile), | |
28 is_dragging_(false) { | |
29 SetModel(model); | |
30 SetEditable(false); | |
31 SetRootShown(false); | |
32 set_drag_enabled(true); | |
33 } | |
34 | |
35 bool BookmarkFolderTreeView::CanDrop(const OSExchangeData& data) { | |
36 if (!profile_->GetBookmarkModel()->IsLoaded()) | |
37 return false; | |
38 | |
39 BookmarkDragData drag_data; | |
40 | |
41 if (!drag_data.Read(data)) | |
42 return false; | |
43 | |
44 drop_info_.reset(new DropInfo(this)); | |
45 drop_info_->SetData(drag_data); | |
46 | |
47 // See if there are any urls being dropped. | |
48 for (size_t i = 0; i < drop_info_->data().size(); ++i) { | |
49 if (drop_info_->data().elements[0].is_url) { | |
50 drop_info_->set_only_folders(false); | |
51 break; | |
52 } | |
53 } | |
54 | |
55 return true; | |
56 } | |
57 | |
58 void BookmarkFolderTreeView::OnDragEntered( | |
59 const views::DropTargetEvent& event) { | |
60 } | |
61 | |
62 int BookmarkFolderTreeView::OnDragUpdated(const views::DropTargetEvent& event) { | |
63 drop_info_->Update(event); | |
64 return UpdateDropInfo(); | |
65 } | |
66 | |
67 void BookmarkFolderTreeView::OnDragExited() { | |
68 SetDropPosition(DropPosition()); | |
69 | |
70 drop_info_.reset(); | |
71 } | |
72 | |
73 int BookmarkFolderTreeView::OnPerformDrop(const views::DropTargetEvent& event) { | |
74 OnPerformDropImpl(); | |
75 | |
76 int drop_operation = drop_info_->drop_operation(); | |
77 SetDropPosition(DropPosition()); | |
78 drop_info_.reset(); | |
79 return drop_operation; | |
80 } | |
81 | |
82 const BookmarkNode* BookmarkFolderTreeView::GetSelectedBookmarkNode() { | |
83 TreeModelNode* selected_node = GetSelectedNode(); | |
84 if (!selected_node) | |
85 return NULL; | |
86 return TreeNodeAsBookmarkNode(folder_model()->AsNode(selected_node)); | |
87 } | |
88 | |
89 LRESULT BookmarkFolderTreeView::OnNotify(int w_param, LPNMHDR l_param) { | |
90 switch (l_param->code) { | |
91 case TVN_BEGINDRAG: { | |
92 HTREEITEM tree_item = | |
93 reinterpret_cast<LPNMTREEVIEW>(l_param)->itemNew.hItem; | |
94 FolderNode* folder_node = | |
95 folder_model()->AsNode(GetNodeForTreeItem(tree_item)); | |
96 BeginDrag(TreeNodeAsBookmarkNode(folder_node)); | |
97 return 0; // Return value ignored. | |
98 } | |
99 } | |
100 | |
101 return TreeView::OnNotify(w_param, l_param); | |
102 } | |
103 | |
104 int BookmarkFolderTreeView::UpdateDropInfo() { | |
105 DropPosition position = | |
106 CalculateDropPosition(drop_info_->last_y(), drop_info_->only_folders()); | |
107 drop_info_->set_drop_operation(CalculateDropOperation(position)); | |
108 | |
109 if (drop_info_->drop_operation() == DragDropTypes::DRAG_NONE) | |
110 position = DropPosition(); | |
111 | |
112 SetDropPosition(position); | |
113 | |
114 return drop_info_->drop_operation(); | |
115 } | |
116 | |
117 void BookmarkFolderTreeView::BeginDrag(const BookmarkNode* node) { | |
118 BookmarkModel* model = profile_->GetBookmarkModel(); | |
119 // Only allow the drag if the user has selected a node of type bookmark and it | |
120 // isn't the bookmark bar or other bookmarks folders. | |
121 if (!node || node == model->other_node() || | |
122 node == model->GetBookmarkBarNode()) { | |
123 return; | |
124 } | |
125 | |
126 std::vector<const BookmarkNode*> nodes_to_drag; | |
127 nodes_to_drag.push_back(node); | |
128 | |
129 OSExchangeData data; | |
130 BookmarkDragData(nodes_to_drag).Write(profile_, &data); | |
131 scoped_refptr<BaseDragSource> drag_source(new BaseDragSource); | |
132 DWORD effects; | |
133 is_dragging_ = true; | |
134 DoDragDrop(OSExchangeDataProviderWin::GetIDataObject(data), drag_source, | |
135 DROPEFFECT_LINK | DROPEFFECT_COPY | DROPEFFECT_MOVE, &effects); | |
136 is_dragging_ = false; | |
137 } | |
138 | |
139 BookmarkFolderTreeView::DropPosition BookmarkFolderTreeView:: | |
140 CalculateDropPosition(int y, bool only_folders) { | |
141 HWND hwnd = GetNativeControlHWND(); | |
142 HTREEITEM item = TreeView_GetFirstVisible(hwnd); | |
143 while (item) { | |
144 RECT bounds; | |
145 TreeView_GetItemRect(hwnd, item, &bounds, TRUE); | |
146 if (y < bounds.bottom) { | |
147 TreeModelNode* model_node = GetNodeForTreeItem(item); | |
148 if (folder_model()->GetNodeType(model_node) != | |
149 BookmarkFolderTreeModel::BOOKMARK) { | |
150 // Only allow drops on bookmark nodes. | |
151 return DropPosition(); | |
152 } | |
153 | |
154 FolderNode* node = folder_model()->AsNode(model_node); | |
155 if (!only_folders || !node->GetParent() || | |
156 !node->GetParent()->GetParent()) { | |
157 // If some of the elements being dropped are urls, then we only allow | |
158 // dropping on a folder. Similarly you can't drop between the | |
159 // bookmark bar and other folder nodes. | |
160 return DropPosition(node, node->GetChildCount(), true); | |
161 } | |
162 | |
163 // Drop contains all folders, allow them to be dropped between | |
164 // folders. | |
165 if (y < bounds.top + views::kDropBetweenPixels) { | |
166 return DropPosition(node->GetParent(), | |
167 node->GetParent()->IndexOfChild(node), false); | |
168 } | |
169 if (y >= bounds.bottom - views::kDropBetweenPixels) { | |
170 if (IsExpanded(node) && folder_model()->GetChildCount(node) > 0) { | |
171 // The node is expanded and has children, treat the drop as occurring | |
172 // as the first child. This is done to avoid the selection highlight | |
173 // dancing around when dragging over expanded folders. Without this | |
174 // the highlight jumps past the last expanded child of node. | |
175 return DropPosition(node, 0, false); | |
176 } | |
177 return DropPosition(node->GetParent(), | |
178 node->GetParent()->IndexOfChild(node) + 1, false); | |
179 } | |
180 return DropPosition(node, node->GetChildCount(), true); | |
181 } | |
182 item = TreeView_GetNextVisible(hwnd, item); | |
183 } | |
184 return DropPosition(); | |
185 } | |
186 | |
187 int BookmarkFolderTreeView::CalculateDropOperation( | |
188 const DropPosition& position) { | |
189 if (!position.parent) | |
190 return DragDropTypes::DRAG_NONE; | |
191 | |
192 if (drop_info_->data().IsFromProfile(profile_)) { | |
193 int bookmark_model_drop_index = FolderIndexToBookmarkIndex(position); | |
194 if (!bookmark_utils::IsValidDropLocation( | |
195 profile_, drop_info_->data(), | |
196 TreeNodeAsBookmarkNode(position.parent), | |
197 bookmark_model_drop_index)) { | |
198 return DragDropTypes::DRAG_NONE; | |
199 } | |
200 | |
201 // Data from the same profile. Prefer move, but do copy if the user wants | |
202 // that. | |
203 if (drop_info_->is_control_down()) | |
204 return DragDropTypes::DRAG_COPY; | |
205 | |
206 return DragDropTypes::DRAG_MOVE; | |
207 } | |
208 // We're going to copy, but return an operation compatible with the source | |
209 // operations so that the user can drop. | |
210 return bookmark_utils::PreferredDropOperation( | |
211 drop_info_->source_operations(), | |
212 DragDropTypes::DRAG_COPY | DragDropTypes::DRAG_LINK); | |
213 } | |
214 | |
215 void BookmarkFolderTreeView::OnPerformDropImpl() { | |
216 const BookmarkNode* parent_node = | |
217 TreeNodeAsBookmarkNode(drop_info_->position().parent); | |
218 int drop_index = FolderIndexToBookmarkIndex(drop_info_->position()); | |
219 BookmarkModel* model = profile_->GetBookmarkModel(); | |
220 // If the data is not from this profile we return an operation compatible | |
221 // with the source. As such, we need to need to check the data here too. | |
222 if (!drop_info_->data().IsFromProfile(profile_) || | |
223 drop_info_->drop_operation() == DragDropTypes::DRAG_COPY) { | |
224 bookmark_utils::CloneDragData(model, drop_info_->data().elements, | |
225 parent_node, drop_index); | |
226 return; | |
227 } | |
228 | |
229 // else, move. | |
230 std::vector<const BookmarkNode*> nodes = | |
231 drop_info_->data().GetNodes(profile_); | |
232 if (nodes.empty()) | |
233 return; | |
234 | |
235 for (size_t i = 0; i < nodes.size(); ++i) { | |
236 model->Move(nodes[i], parent_node, drop_index); | |
237 // Reset the drop_index, just in case the index didn't really change. | |
238 drop_index = parent_node->IndexOfChild(nodes[i]) + 1; | |
239 if (nodes[i]->is_folder()) { | |
240 Expand(folder_model()->GetFolderNodeForBookmarkNode(nodes[i])); | |
241 } | |
242 } | |
243 | |
244 if (is_dragging_ && nodes[0]->is_folder()) { | |
245 // We're the drag source. Select the node that was moved. | |
246 SetSelectedNode(folder_model()->GetFolderNodeForBookmarkNode(nodes[0])); | |
247 } | |
248 } | |
249 | |
250 void BookmarkFolderTreeView::SetDropPosition(const DropPosition& position) { | |
251 if (drop_info_->position().equals(position)) | |
252 return; | |
253 | |
254 // Remove the indicator over the previous location. | |
255 if (drop_info_->position().on) { | |
256 HTREEITEM item = GetTreeItemForNode(drop_info_->position().parent); | |
257 if (item) | |
258 TreeView_SetItemState(GetNativeControlHWND(), item, 0, TVIS_DROPHILITED); | |
259 } else if (drop_info_->position().index != -1) { | |
260 TreeView_SetInsertMark(GetNativeControlHWND(), NULL, FALSE); | |
261 } | |
262 | |
263 drop_info_->set_position(position); | |
264 | |
265 // And show the new indicator. | |
266 if (position.on) { | |
267 HTREEITEM item = GetTreeItemForNode(position.parent); | |
268 if (item) { | |
269 TreeView_SetItemState(GetNativeControlHWND(), item, TVIS_DROPHILITED, | |
270 TVIS_DROPHILITED); | |
271 } | |
272 } else if (position.index != -1) { | |
273 BOOL after = FALSE; | |
274 FolderNode* node = position.parent; | |
275 if (folder_model()->GetChildCount(position.parent) == position.index) { | |
276 after = TRUE; | |
277 node = folder_model()->GetChild(position.parent, position.index - 1); | |
278 } else { | |
279 node = folder_model()->GetChild(position.parent, position.index); | |
280 } | |
281 HTREEITEM item = GetTreeItemForNode(node); | |
282 if (item) | |
283 TreeView_SetInsertMark(GetNativeControlHWND(), item, after); | |
284 } | |
285 } | |
286 | |
287 BookmarkFolderTreeModel* BookmarkFolderTreeView::folder_model() const { | |
288 return static_cast<BookmarkFolderTreeModel*>(model()); | |
289 } | |
290 | |
291 const BookmarkNode* BookmarkFolderTreeView::TreeNodeAsBookmarkNode( | |
292 FolderNode* node) { | |
293 return folder_model()->TreeNodeAsBookmarkNode(node); | |
294 } | |
295 | |
296 int BookmarkFolderTreeView::FolderIndexToBookmarkIndex( | |
297 const DropPosition& position) { | |
298 const BookmarkNode* parent_node = TreeNodeAsBookmarkNode(position.parent); | |
299 if (position.on || position.index == position.parent->GetChildCount()) | |
300 return parent_node->GetChildCount(); | |
301 | |
302 if (position.index != 0) { | |
303 return parent_node->IndexOfChild( | |
304 TreeNodeAsBookmarkNode(position.parent->GetChild(position.index))); | |
305 } | |
306 | |
307 return 0; | |
308 } | |
OLD | NEW |