Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(456)

Side by Side Diff: chrome/browser/views/bookmark_bar_view.cc

Issue 1912: Renames BoomarkBarModel to BookmarkModel. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 12 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/views/bookmark_bar_view.h" 5 #include "chrome/browser/views/bookmark_bar_view.h"
6 6
7 #include <limits> 7 #include <limits>
8 8
9 #include "base/base_drag_source.h" 9 #include "base/base_drag_source.h"
10 #include "base/gfx/skia_utils.h" 10 #include "base/gfx/skia_utils.h"
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 monitor_bounds.width(), 186 monitor_bounds.width(),
187 languages)); 187 languages));
188 if (l10n_util::GetTextDirection() == l10n_util::RIGHT_TO_LEFT) 188 if (l10n_util::GetTextDirection() == l10n_util::RIGHT_TO_LEFT)
189 l10n_util::WrapStringWithLTRFormatting(&elided_url); 189 l10n_util::WrapStringWithLTRFormatting(&elided_url);
190 result.append(elided_url); 190 result.append(elided_url);
191 } 191 }
192 return result; 192 return result;
193 } 193 }
194 194
195 // Returns the drag operations for the specified node. 195 // Returns the drag operations for the specified node.
196 static int GetDragOperationsForNode(BookmarkBarNode* node) { 196 static int GetDragOperationsForNode(BookmarkNode* node) {
197 if (node->GetType() == history::StarredEntry::URL) { 197 if (node->GetType() == history::StarredEntry::URL) {
198 return DragDropTypes::DRAG_COPY | DragDropTypes::DRAG_MOVE | 198 return DragDropTypes::DRAG_COPY | DragDropTypes::DRAG_MOVE |
199 DragDropTypes::DRAG_LINK; 199 DragDropTypes::DRAG_LINK;
200 } 200 }
201 return DragDropTypes::DRAG_COPY | DragDropTypes::DRAG_MOVE; 201 return DragDropTypes::DRAG_COPY | DragDropTypes::DRAG_MOVE;
202 } 202 }
203 203
204 // BookmarkButton ------------------------------------------------------------- 204 // BookmarkButton -------------------------------------------------------------
205 205
206 // Buttons used for the bookmarks on the bookmark bar. 206 // Buttons used for the bookmarks on the bookmark bar.
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
309 309
310 // MenuRunner manages creation and showing of a menu containing BookmarkNodes. 310 // MenuRunner manages creation and showing of a menu containing BookmarkNodes.
311 // MenuRunner is used to show the contents of bookmark folders on the 311 // MenuRunner is used to show the contents of bookmark folders on the
312 // bookmark bar, other folder, or overflow bookmarks. 312 // bookmark bar, other folder, or overflow bookmarks.
313 // 313 //
314 class MenuRunner : public ChromeViews::MenuDelegate, 314 class MenuRunner : public ChromeViews::MenuDelegate,
315 public BookmarkBarView::ModelChangedListener { 315 public BookmarkBarView::ModelChangedListener {
316 public: 316 public:
317 // start_child_index is the index of the first child in node to add to the 317 // start_child_index is the index of the first child in node to add to the
318 // menu. 318 // menu.
319 MenuRunner(BookmarkBarView* view, 319 MenuRunner(BookmarkBarView* view, BookmarkNode* node, int start_child_index)
320 BookmarkBarNode* node,
321 int start_child_index)
322 : view_(view), 320 : view_(view),
323 node_(node), 321 node_(node),
324 menu_(this) { 322 menu_(this) {
325 int next_menu_id = 1; 323 int next_menu_id = 1;
326 menu_id_to_node_map_[menu_.GetCommand()] = node; 324 menu_id_to_node_map_[menu_.GetCommand()] = node;
327 BuildMenu(node, start_child_index, &menu_, &next_menu_id); 325 BuildMenu(node, start_child_index, &menu_, &next_menu_id);
328 } 326 }
329 327
330 // Returns the node the menu is being run for. 328 // Returns the node the menu is being run for.
331 BookmarkBarNode* GetNode() { 329 BookmarkNode* GetNode() {
332 return node_; 330 return node_;
333 } 331 }
334 332
335 void RunMenuAt(HWND hwnd, 333 void RunMenuAt(HWND hwnd,
336 const gfx::Rect& bounds, 334 const gfx::Rect& bounds,
337 MenuItemView::AnchorPosition position, 335 MenuItemView::AnchorPosition position,
338 bool for_drop) { 336 bool for_drop) {
339 view_->SetModelChangedListener(this); 337 view_->SetModelChangedListener(this);
340 if (for_drop) 338 if (for_drop)
341 menu_.RunMenuForDropAt(hwnd, bounds, position); 339 menu_.RunMenuForDropAt(hwnd, bounds, position);
342 else 340 else
343 menu_.RunMenuAt(hwnd, bounds, position, false); 341 menu_.RunMenuAt(hwnd, bounds, position, false);
344 view_->ClearModelChangedListenerIfEquals(this); 342 view_->ClearModelChangedListenerIfEquals(this);
345 } 343 }
346 344
347 // Notification that the favicon has finished loading. Reset the icon 345 // Notification that the favicon has finished loading. Reset the icon
348 // of the menu item. 346 // of the menu item.
349 void FavIconLoaded(BookmarkBarNode* node) { 347 void FavIconLoaded(BookmarkNode* node) {
350 if (node_to_menu_id_map_.find(node) != 348 if (node_to_menu_id_map_.find(node) !=
351 node_to_menu_id_map_.end()) { 349 node_to_menu_id_map_.end()) {
352 menu_.SetIcon(node->GetFavIcon(), node_to_menu_id_map_[node]); 350 menu_.SetIcon(node->GetFavIcon(), node_to_menu_id_map_[node]);
353 } 351 }
354 } 352 }
355 353
356 virtual void ModelChanged() { 354 virtual void ModelChanged() {
357 if (context_menu_.get()) 355 if (context_menu_.get())
358 context_menu_->ModelChanged(); 356 context_menu_->ModelChanged();
359 menu_.Cancel(); 357 menu_.Cancel();
360 } 358 }
361 359
362 MenuItemView* menu() { return &menu_; } 360 MenuItemView* menu() { return &menu_; }
363 361
364 MenuItemView* context_menu() { 362 MenuItemView* context_menu() {
365 return context_menu_.get() ? context_menu_->menu() : NULL; 363 return context_menu_.get() ? context_menu_->menu() : NULL;
366 } 364 }
367 365
368 private: 366 private:
369 // Creates an entry in menu for each child node of parent starting at 367 // Creates an entry in menu for each child node of parent starting at
370 // start_child_index, recursively invoking this for any star groups. 368 // start_child_index, recursively invoking this for any star groups.
371 void BuildMenu(BookmarkBarNode* parent, 369 void BuildMenu(BookmarkNode* parent,
372 int start_child_index, 370 int start_child_index,
373 MenuItemView* menu, 371 MenuItemView* menu,
374 int* next_menu_id) { 372 int* next_menu_id) {
375 DCHECK(!parent->GetChildCount() || 373 DCHECK(!parent->GetChildCount() ||
376 start_child_index < parent->GetChildCount()); 374 start_child_index < parent->GetChildCount());
377 for (int i = start_child_index; i < parent->GetChildCount(); ++i) { 375 for (int i = start_child_index; i < parent->GetChildCount(); ++i) {
378 BookmarkBarNode* node = parent->GetChild(i); 376 BookmarkNode* node = parent->GetChild(i);
379 int id = *next_menu_id; 377 int id = *next_menu_id;
380 378
381 (*next_menu_id)++; 379 (*next_menu_id)++;
382 if (node->GetType() == history::StarredEntry::URL) { 380 if (node->GetType() == history::StarredEntry::URL) {
383 SkBitmap icon = node->GetFavIcon(); 381 SkBitmap icon = node->GetFavIcon();
384 if (icon.width() == 0) 382 if (icon.width() == 0)
385 icon = *kDefaultFavIcon; 383 icon = *kDefaultFavIcon;
386 menu->AppendMenuItemWithIcon(id, node->GetTitle(), icon); 384 menu->AppendMenuItemWithIcon(id, node->GetTitle(), icon);
387 node_to_menu_id_map_[node] = id; 385 node_to_menu_id_map_[node] = id;
388 } else { 386 } else {
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
421 419
422 if (drop_data_.is_url) 420 if (drop_data_.is_url)
423 return true; 421 return true;
424 422
425 if (drop_data_.profile_id != view_->GetProfile()->GetID()) { 423 if (drop_data_.profile_id != view_->GetProfile()->GetID()) {
426 // Always accept drags of bookmark groups from other profiles. 424 // Always accept drags of bookmark groups from other profiles.
427 return true; 425 return true;
428 } 426 }
429 // Drag originated from same profile and is not a URL. Only accept it if 427 // Drag originated from same profile and is not a URL. Only accept it if
430 // the dragged node is not a parent of the node menu represents. 428 // the dragged node is not a parent of the node menu represents.
431 BookmarkBarNode* drop_node = menu_id_to_node_map_[menu->GetCommand()]; 429 BookmarkNode* drop_node = menu_id_to_node_map_[menu->GetCommand()];
432 DCHECK(drop_node); 430 DCHECK(drop_node);
433 BookmarkBarNode* drag_node = drop_data_.GetNode(view_->GetProfile()-> 431 BookmarkNode* drag_node =
434 GetBookmarkBarModel()); 432 drop_data_.GetNode(view_->GetProfile()->GetBookmarkModel());
435 if (!drag_node) { 433 if (!drag_node) {
436 // Hmmm, can't find the dragged node. This is generally an error 434 // Hmmm, can't find the dragged node. This is generally an error
437 // condition and we won't try and do anything fancy. 435 // condition and we won't try and do anything fancy.
438 NOTREACHED(); 436 NOTREACHED();
439 return false; 437 return false;
440 } 438 }
441 BookmarkBarNode* node = drop_node; 439 BookmarkNode* node = drop_node;
442 while (drop_node && drop_node != drag_node) 440 while (drop_node && drop_node != drag_node)
443 drop_node = drop_node->GetParent(); 441 drop_node = drop_node->GetParent();
444 return (drop_node == NULL); 442 return (drop_node == NULL);
445 } 443 }
446 444
447 virtual int GetDropOperation(MenuItemView* item, 445 virtual int GetDropOperation(MenuItemView* item,
448 const ChromeViews::DropTargetEvent& event, 446 const ChromeViews::DropTargetEvent& event,
449 DropPosition* position) { 447 DropPosition* position) {
450 DCHECK(drop_data_.is_valid); 448 DCHECK(drop_data_.is_valid);
451 BookmarkBarNode* node = menu_id_to_node_map_[item->GetCommand()]; 449 BookmarkNode* node = menu_id_to_node_map_[item->GetCommand()];
452 BookmarkBarNode* drop_parent = node->GetParent(); 450 BookmarkNode* drop_parent = node->GetParent();
453 int index_to_drop_at = drop_parent->IndexOfChild(node); 451 int index_to_drop_at = drop_parent->IndexOfChild(node);
454 if (*position == DROP_AFTER) { 452 if (*position == DROP_AFTER) {
455 index_to_drop_at++; 453 index_to_drop_at++;
456 } else if (*position == DROP_ON) { 454 } else if (*position == DROP_ON) {
457 drop_parent = node; 455 drop_parent = node;
458 index_to_drop_at = node->GetChildCount(); 456 index_to_drop_at = node->GetChildCount();
459 } 457 }
460 DCHECK(drop_parent); 458 DCHECK(drop_parent);
461 return view_->CalculateDropOperation(drop_data_, drop_parent, 459 return view_->CalculateDropOperation(drop_data_, drop_parent,
462 index_to_drop_at); 460 index_to_drop_at);
463 } 461 }
464 462
465 virtual int OnPerformDrop(MenuItemView* menu, 463 virtual int OnPerformDrop(MenuItemView* menu,
466 DropPosition position, 464 DropPosition position,
467 const DropTargetEvent& event) { 465 const DropTargetEvent& event) {
468 BookmarkBarNode* drop_node = menu_id_to_node_map_[menu->GetCommand()]; 466 BookmarkNode* drop_node = menu_id_to_node_map_[menu->GetCommand()];
469 DCHECK(drop_node); 467 DCHECK(drop_node);
470 BookmarkBarModel* model = view_->GetModel(); 468 BookmarkModel* model = view_->GetModel();
471 DCHECK(model); 469 DCHECK(model);
472 BookmarkBarNode* drop_parent = drop_node->GetParent(); 470 BookmarkNode* drop_parent = drop_node->GetParent();
473 DCHECK(drop_parent); 471 DCHECK(drop_parent);
474 int index_to_drop_at = drop_parent->IndexOfChild(drop_node); 472 int index_to_drop_at = drop_parent->IndexOfChild(drop_node);
475 if (position == DROP_AFTER) { 473 if (position == DROP_AFTER) {
476 index_to_drop_at++; 474 index_to_drop_at++;
477 } else if (position == DROP_ON) { 475 } else if (position == DROP_ON) {
478 DCHECK(drop_node->GetType() != history::StarredEntry::URL); 476 DCHECK(drop_node->GetType() != history::StarredEntry::URL);
479 drop_parent = drop_node; 477 drop_parent = drop_node;
480 index_to_drop_at = drop_node->GetChildCount(); 478 index_to_drop_at = drop_node->GetChildCount();
481 } 479 }
482 480
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
519 517
520 view_->WriteDragData(menu_id_to_node_map_[sender->GetCommand()], data); 518 view_->WriteDragData(menu_id_to_node_map_[sender->GetCommand()], data);
521 } 519 }
522 520
523 virtual int GetDragOperations(MenuItemView* sender) { 521 virtual int GetDragOperations(MenuItemView* sender) {
524 return GetDragOperationsForNode( 522 return GetDragOperationsForNode(
525 menu_id_to_node_map_[sender->GetCommand()]); 523 menu_id_to_node_map_[sender->GetCommand()]);
526 } 524 }
527 525
528 // The node we're showing the contents of. 526 // The node we're showing the contents of.
529 BookmarkBarNode* node_; 527 BookmarkNode* node_;
530 528
531 // The view that created us. 529 // The view that created us.
532 BookmarkBarView* view_; 530 BookmarkBarView* view_;
533 531
534 // The menu. 532 // The menu.
535 MenuItemView menu_; 533 MenuItemView menu_;
536 534
537 // Mapping from menu id to the BookmarkBarNode. 535 // Mapping from menu id to the BookmarkNode.
538 std::map<int, BookmarkBarNode*> menu_id_to_node_map_; 536 std::map<int, BookmarkNode*> menu_id_to_node_map_;
539 537
540 // Mapping from node to menu id. This only contains entries for nodes of type 538 // Mapping from node to menu id. This only contains entries for nodes of type
541 // URL. 539 // URL.
542 std::map<BookmarkBarNode*, int> node_to_menu_id_map_; 540 std::map<BookmarkNode*, int> node_to_menu_id_map_;
543 541
544 // Data for the drop. 542 // Data for the drop.
545 BookmarkDragData drop_data_; 543 BookmarkDragData drop_data_;
546 544
547 scoped_ptr<BookmarkBarContextMenuController> context_menu_; 545 scoped_ptr<BookmarkBarContextMenuController> context_menu_;
548 546
549 DISALLOW_COPY_AND_ASSIGN(MenuRunner); 547 DISALLOW_COPY_AND_ASSIGN(MenuRunner);
550 }; 548 };
551 549
552 // ButtonSeparatorView -------------------------------------------------------- 550 // ButtonSeparatorView --------------------------------------------------------
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
671 // loaded. 669 // loaded.
672 other_bookmarked_button_->SetEnabled(false); 670 other_bookmarked_button_->SetEnabled(false);
673 671
674 NotificationService* ns = NotificationService::current(); 672 NotificationService* ns = NotificationService::current();
675 Source<Profile> ns_source(profile_->GetOriginalProfile()); 673 Source<Profile> ns_source(profile_->GetOriginalProfile());
676 ns->AddObserver(this, NOTIFY_BOOKMARK_BUBBLE_SHOWN, ns_source); 674 ns->AddObserver(this, NOTIFY_BOOKMARK_BUBBLE_SHOWN, ns_source);
677 ns->AddObserver(this, NOTIFY_BOOKMARK_BUBBLE_HIDDEN, ns_source); 675 ns->AddObserver(this, NOTIFY_BOOKMARK_BUBBLE_HIDDEN, ns_source);
678 ns->AddObserver(this, NOTIFY_BOOKMARK_BAR_VISIBILITY_PREF_CHANGED, 676 ns->AddObserver(this, NOTIFY_BOOKMARK_BAR_VISIBILITY_PREF_CHANGED,
679 NotificationService::AllSources()); 677 NotificationService::AllSources());
680 678
681 model_ = profile_->GetBookmarkBarModel(); 679 model_ = profile_->GetBookmarkModel();
682 model_->AddObserver(this); 680 model_->AddObserver(this);
683 if (model_->IsLoaded()) 681 if (model_->IsLoaded())
684 Loaded(model_); 682 Loaded(model_);
685 // else case: we'll receive notification back from the BookmarkBarModel when 683 // else case: we'll receive notification back from the BookmarkModel when done
686 // done loading, then we'll populate the bar. 684 // loading, then we'll populate the bar.
687 } 685 }
688 686
689 void BookmarkBarView::SetPageNavigator(PageNavigator* navigator) { 687 void BookmarkBarView::SetPageNavigator(PageNavigator* navigator) {
690 page_navigator_ = navigator; 688 page_navigator_ = navigator;
691 } 689 }
692 690
693 void BookmarkBarView::GetPreferredSize(CSize *out) { 691 void BookmarkBarView::GetPreferredSize(CSize *out) {
694 if (!prefButtonHeight) { 692 if (!prefButtonHeight) {
695 ChromeViews::TextButton text_button(L"X"); 693 ChromeViews::TextButton text_button(L"X");
696 CSize text_button_pref; 694 CSize text_button_pref;
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after
966 drop_info_->drop_on = drop_on; 964 drop_info_->drop_on = drop_on;
967 drop_info_->is_over_overflow = is_over_overflow; 965 drop_info_->is_over_overflow = is_over_overflow;
968 drop_info_->is_over_other = is_over_other; 966 drop_info_->is_over_other = is_over_other;
969 967
970 if (drop_info_->is_menu_showing) { 968 if (drop_info_->is_menu_showing) {
971 drop_menu_runner_.reset(); 969 drop_menu_runner_.reset();
972 drop_info_->is_menu_showing = false; 970 drop_info_->is_menu_showing = false;
973 } 971 }
974 972
975 if (drop_on || is_over_overflow || is_over_other) { 973 if (drop_on || is_over_overflow || is_over_other) {
976 BookmarkBarNode* node; 974 BookmarkNode* node;
977 if (is_over_other) 975 if (is_over_other)
978 node = model_->other_node(); 976 node = model_->other_node();
979 else if (is_over_overflow) 977 else if (is_over_overflow)
980 node = model_->GetBookmarkBarNode(); 978 node = model_->GetBookmarkBarNode();
981 else 979 else
982 node = model_->GetBookmarkBarNode()->GetChild(drop_index); 980 node = model_->GetBookmarkBarNode()->GetChild(drop_index);
983 StartShowFolderDropMenuTimer(node); 981 StartShowFolderDropMenuTimer(node);
984 } 982 }
985 983
986 return drop_info_->drag_operation; 984 return drop_info_->drag_operation;
(...skipping 15 matching lines...) Expand all
1002 } 1000 }
1003 1001
1004 int BookmarkBarView::OnPerformDrop(const DropTargetEvent& event) { 1002 int BookmarkBarView::OnPerformDrop(const DropTargetEvent& event) {
1005 StopShowFolderDropMenuTimer(); 1003 StopShowFolderDropMenuTimer();
1006 1004
1007 drop_menu_runner_.reset(); 1005 drop_menu_runner_.reset();
1008 1006
1009 if (!drop_info_.get() || !drop_info_->drag_operation) 1007 if (!drop_info_.get() || !drop_info_->drag_operation)
1010 return DragDropTypes::DRAG_NONE; 1008 return DragDropTypes::DRAG_NONE;
1011 1009
1012 BookmarkBarNode* root = 1010 BookmarkNode* root = drop_info_->is_over_other ? model_->other_node() :
1013 drop_info_->is_over_other ? model_->other_node() : 1011 model_->GetBookmarkBarNode();
1014 model_->GetBookmarkBarNode();
1015 int index = drop_info_->drop_index; 1012 int index = drop_info_->drop_index;
1016 const bool drop_on = drop_info_->drop_on; 1013 const bool drop_on = drop_info_->drop_on;
1017 const BookmarkDragData data = drop_info_->data; 1014 const BookmarkDragData data = drop_info_->data;
1018 const bool is_over_other = drop_info_->is_over_other; 1015 const bool is_over_other = drop_info_->is_over_other;
1019 DCHECK(data.is_valid); 1016 DCHECK(data.is_valid);
1020 1017
1021 if (drop_info_->drop_index != -1) { 1018 if (drop_info_->drop_index != -1) {
1022 // TODO(sky): optimize the SchedulePaint region. 1019 // TODO(sky): optimize the SchedulePaint region.
1023 SchedulePaint(); 1020 SchedulePaint();
1024 } 1021 }
1025 drop_info_.reset(); 1022 drop_info_.reset();
1026 1023
1027 BookmarkBarNode* parent_node; 1024 BookmarkNode* parent_node;
1028 if (is_over_other) { 1025 if (is_over_other) {
1029 parent_node = root; 1026 parent_node = root;
1030 index = parent_node->GetChildCount(); 1027 index = parent_node->GetChildCount();
1031 } else if (drop_on) { 1028 } else if (drop_on) {
1032 parent_node = root->GetChild(index); 1029 parent_node = root->GetChild(index);
1033 index = parent_node->GetChildCount(); 1030 index = parent_node->GetChildCount();
1034 } else { 1031 } else {
1035 parent_node = root; 1032 parent_node = root;
1036 } 1033 }
1037 return PerformDropImpl(data, parent_node, index); 1034 return PerformDropImpl(data, parent_node, index);
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
1148 return button; 1145 return button;
1149 } 1146 }
1150 1147
1151 int BookmarkBarView::GetBookmarkButtonCount() { 1148 int BookmarkBarView::GetBookmarkButtonCount() {
1152 // We contain four non-bookmark button views: recently bookmarked, 1149 // We contain four non-bookmark button views: recently bookmarked,
1153 // bookmarks separator, chevrons (for overflow) and the instruction 1150 // bookmarks separator, chevrons (for overflow) and the instruction
1154 // label. 1151 // label.
1155 return GetChildViewCount() - 4; 1152 return GetChildViewCount() - 4;
1156 } 1153 }
1157 1154
1158 void BookmarkBarView::Loaded(BookmarkBarModel* model) { 1155 void BookmarkBarView::Loaded(BookmarkModel* model) {
1159 BookmarkBarNode* node = model_->GetBookmarkBarNode(); 1156 BookmarkNode* node = model_->GetBookmarkBarNode();
1160 DCHECK(node && model_->other_node()); 1157 DCHECK(node && model_->other_node());
1161 // Create a button for each of the children on the bookmark bar. 1158 // Create a button for each of the children on the bookmark bar.
1162 for (int i = 0; i < node->GetChildCount(); ++i) 1159 for (int i = 0; i < node->GetChildCount(); ++i)
1163 AddChildView(i, CreateBookmarkButton(node->GetChild(i))); 1160 AddChildView(i, CreateBookmarkButton(node->GetChild(i)));
1164 other_bookmarked_button_->SetEnabled(true); 1161 other_bookmarked_button_->SetEnabled(true);
1165 Layout(); 1162 Layout();
1166 SchedulePaint(); 1163 SchedulePaint();
1167 } 1164 }
1168 1165
1169 void BookmarkBarView::BookmarkModelBeingDeleted(BookmarkBarModel* model) { 1166 void BookmarkBarView::BookmarkModelBeingDeleted(BookmarkModel* model) {
1170 // The bookmark model should never be deleted before us. This code exists 1167 // The bookmark model should never be deleted before us. This code exists
1171 // to check for regressions in shutdown code and not crash. 1168 // to check for regressions in shutdown code and not crash.
1172 NOTREACHED(); 1169 NOTREACHED();
1173 1170
1174 // Do minimal cleanup, presumably we'll be deleted shortly. 1171 // Do minimal cleanup, presumably we'll be deleted shortly.
1175 NotifyModelChanged(); 1172 NotifyModelChanged();
1176 model_->RemoveObserver(this); 1173 model_->RemoveObserver(this);
1177 model_ = NULL; 1174 model_ = NULL;
1178 } 1175 }
1179 1176
1180 void BookmarkBarView::BookmarkNodeMoved(BookmarkBarModel* model, 1177 void BookmarkBarView::BookmarkNodeMoved(BookmarkModel* model,
1181 BookmarkBarNode* old_parent, 1178 BookmarkNode* old_parent,
1182 int old_index, 1179 int old_index,
1183 BookmarkBarNode* new_parent, 1180 BookmarkNode* new_parent,
1184 int new_index) { 1181 int new_index) {
1185 StopThrobbing(true); 1182 StopThrobbing(true);
1186 BookmarkNodeRemovedImpl(model, old_parent, old_index); 1183 BookmarkNodeRemovedImpl(model, old_parent, old_index);
1187 BookmarkNodeAddedImpl(model, new_parent, new_index); 1184 BookmarkNodeAddedImpl(model, new_parent, new_index);
1188 StartThrobbing(); 1185 StartThrobbing();
1189 } 1186 }
1190 1187
1191 void BookmarkBarView::BookmarkNodeAdded(BookmarkBarModel* model, 1188 void BookmarkBarView::BookmarkNodeAdded(BookmarkModel* model,
1192 BookmarkBarNode* parent, 1189 BookmarkNode* parent,
1193 int index) { 1190 int index) {
1194 StopThrobbing(true); 1191 StopThrobbing(true);
1195 BookmarkNodeAddedImpl(model, parent, index); 1192 BookmarkNodeAddedImpl(model, parent, index);
1196 StartThrobbing(); 1193 StartThrobbing();
1197 } 1194 }
1198 1195
1199 void BookmarkBarView::BookmarkNodeAddedImpl(BookmarkBarModel* model, 1196 void BookmarkBarView::BookmarkNodeAddedImpl(BookmarkModel* model,
1200 BookmarkBarNode* parent, 1197 BookmarkNode* parent,
1201 int index) { 1198 int index) {
1202 NotifyModelChanged(); 1199 NotifyModelChanged();
1203 if (parent != model_->GetBookmarkBarNode()) { 1200 if (parent != model_->GetBookmarkBarNode()) {
1204 // We only care about nodes on the bookmark bar. 1201 // We only care about nodes on the bookmark bar.
1205 return; 1202 return;
1206 } 1203 }
1207 DCHECK(index >= 0 && index <= GetBookmarkButtonCount()); 1204 DCHECK(index >= 0 && index <= GetBookmarkButtonCount());
1208 AddChildView(index, CreateBookmarkButton(parent->GetChild(index))); 1205 AddChildView(index, CreateBookmarkButton(parent->GetChild(index)));
1209 Layout(); 1206 Layout();
1210 SchedulePaint(); 1207 SchedulePaint();
1211 } 1208 }
1212 1209
1213 void BookmarkBarView::BookmarkNodeRemoved(BookmarkBarModel* model, 1210 void BookmarkBarView::BookmarkNodeRemoved(BookmarkModel* model,
1214 BookmarkBarNode* parent, 1211 BookmarkNode* parent,
1215 int index) { 1212 int index) {
1216 StopThrobbing(true); 1213 StopThrobbing(true);
1217 BookmarkNodeRemovedImpl(model, parent, index); 1214 BookmarkNodeRemovedImpl(model, parent, index);
1218 StartThrobbing(); 1215 StartThrobbing();
1219 } 1216 }
1220 1217
1221 void BookmarkBarView::BookmarkNodeRemovedImpl(BookmarkBarModel* model, 1218 void BookmarkBarView::BookmarkNodeRemovedImpl(BookmarkModel* model,
1222 BookmarkBarNode* parent, 1219 BookmarkNode* parent,
1223 int index) { 1220 int index) {
1224 NotifyModelChanged(); 1221 NotifyModelChanged();
1225 if (parent != model_->GetBookmarkBarNode()) { 1222 if (parent != model_->GetBookmarkBarNode()) {
1226 // We only care about nodes on the bookmark bar. 1223 // We only care about nodes on the bookmark bar.
1227 return; 1224 return;
1228 } 1225 }
1229 DCHECK(index >= 0 && index < GetBookmarkButtonCount()); 1226 DCHECK(index >= 0 && index < GetBookmarkButtonCount());
1230 ChromeViews::View* button = GetChildViewAt(index); 1227 ChromeViews::View* button = GetChildViewAt(index);
1231 RemoveChildView(button); 1228 RemoveChildView(button);
1232 MessageLoop::current()->DeleteSoon(FROM_HERE, button); 1229 MessageLoop::current()->DeleteSoon(FROM_HERE, button);
1233 Layout(); 1230 Layout();
1234 SchedulePaint(); 1231 SchedulePaint();
1235 } 1232 }
1236 1233
1237 void BookmarkBarView::BookmarkNodeChanged(BookmarkBarModel* model, 1234 void BookmarkBarView::BookmarkNodeChanged(BookmarkModel* model,
1238 BookmarkBarNode* node) { 1235 BookmarkNode* node) {
1239 NotifyModelChanged(); 1236 NotifyModelChanged();
1240 BookmarkNodeChangedImpl(model, node); 1237 BookmarkNodeChangedImpl(model, node);
1241 } 1238 }
1242 1239
1243 void BookmarkBarView::BookmarkNodeChangedImpl(BookmarkBarModel* model, 1240 void BookmarkBarView::BookmarkNodeChangedImpl(BookmarkModel* model,
1244 BookmarkBarNode* node) { 1241 BookmarkNode* node) {
1245 if (node->GetParent() != model_->GetBookmarkBarNode()) { 1242 if (node->GetParent() != model_->GetBookmarkBarNode()) {
1246 // We only care about nodes on the bookmark bar. 1243 // We only care about nodes on the bookmark bar.
1247 return; 1244 return;
1248 } 1245 }
1249 int index = model_->GetBookmarkBarNode()->IndexOfChild(node); 1246 int index = model_->GetBookmarkBarNode()->IndexOfChild(node);
1250 DCHECK(index != -1); 1247 DCHECK(index != -1);
1251 ChromeViews::TextButton* button = GetBookmarkButton(index); 1248 ChromeViews::TextButton* button = GetBookmarkButton(index);
1252 CSize old_pref; 1249 CSize old_pref;
1253 button->GetPreferredSize(&old_pref); 1250 button->GetPreferredSize(&old_pref);
1254 ConfigureButton(node, button); 1251 ConfigureButton(node, button);
1255 CSize new_pref; 1252 CSize new_pref;
1256 button->GetPreferredSize(&new_pref); 1253 button->GetPreferredSize(&new_pref);
1257 if (old_pref.cx != new_pref.cx) { 1254 if (old_pref.cx != new_pref.cx) {
1258 Layout(); 1255 Layout();
1259 SchedulePaint(); 1256 SchedulePaint();
1260 } else if (button->IsVisible()) { 1257 } else if (button->IsVisible()) {
1261 button->SchedulePaint(); 1258 button->SchedulePaint();
1262 } 1259 }
1263 } 1260 }
1264 1261
1265 void BookmarkBarView::BookmarkNodeFavIconLoaded(BookmarkBarModel* model, 1262 void BookmarkBarView::BookmarkNodeFavIconLoaded(BookmarkModel* model,
1266 BookmarkBarNode* node) { 1263 BookmarkNode* node) {
1267 if (menu_runner_.get()) 1264 if (menu_runner_.get())
1268 menu_runner_->FavIconLoaded(node); 1265 menu_runner_->FavIconLoaded(node);
1269 if (drop_menu_runner_.get()) 1266 if (drop_menu_runner_.get())
1270 drop_menu_runner_->FavIconLoaded(node); 1267 drop_menu_runner_->FavIconLoaded(node);
1271 BookmarkNodeChangedImpl(model, node); 1268 BookmarkNodeChangedImpl(model, node);
1272 } 1269 }
1273 1270
1274 void BookmarkBarView::WriteDragData(View* sender, 1271 void BookmarkBarView::WriteDragData(View* sender,
1275 int press_x, 1272 int press_x,
1276 int press_y, 1273 int press_y,
1277 OSExchangeData* data) { 1274 OSExchangeData* data) {
1278 UserMetrics::RecordAction(L"BookmarkBar_DragButton", profile_); 1275 UserMetrics::RecordAction(L"BookmarkBar_DragButton", profile_);
1279 1276
1280 for (int i = 0; i < GetBookmarkButtonCount(); ++i) { 1277 for (int i = 0; i < GetBookmarkButtonCount(); ++i) {
1281 if (sender == GetBookmarkButton(i)) { 1278 if (sender == GetBookmarkButton(i)) {
1282 ChromeViews::TextButton* button = GetBookmarkButton(i); 1279 ChromeViews::TextButton* button = GetBookmarkButton(i);
1283 ChromeCanvas canvas(button->GetWidth(), button->GetHeight(), false); 1280 ChromeCanvas canvas(button->GetWidth(), button->GetHeight(), false);
1284 button->Paint(&canvas, true); 1281 button->Paint(&canvas, true);
1285 drag_utils::SetDragImageOnDataObject(canvas, button->GetWidth(), 1282 drag_utils::SetDragImageOnDataObject(canvas, button->GetWidth(),
1286 button->GetHeight(), press_x, 1283 button->GetHeight(), press_x,
1287 press_y, data); 1284 press_y, data);
1288 WriteDragData(model_->GetBookmarkBarNode()->GetChild(i), data); 1285 WriteDragData(model_->GetBookmarkBarNode()->GetChild(i), data);
1289 return; 1286 return;
1290 } 1287 }
1291 } 1288 }
1292 NOTREACHED(); 1289 NOTREACHED();
1293 } 1290 }
1294 1291
1295 void BookmarkBarView::WriteDragData(BookmarkBarNode* node, 1292 void BookmarkBarView::WriteDragData(BookmarkNode* node,
1296 OSExchangeData* data) { 1293 OSExchangeData* data) {
1297 DCHECK(node && data); 1294 DCHECK(node && data);
1298 BookmarkDragData drag_data(node); 1295 BookmarkDragData drag_data(node);
1299 drag_data.profile_id = GetProfile()->GetID(); 1296 drag_data.profile_id = GetProfile()->GetID();
1300 drag_data.Write(data); 1297 drag_data.Write(data);
1301 } 1298 }
1302 1299
1303 int BookmarkBarView::GetDragOperations(View* sender, int x, int y) { 1300 int BookmarkBarView::GetDragOperations(View* sender, int x, int y) {
1304 for (int i = 0; i < GetBookmarkButtonCount(); ++i) { 1301 for (int i = 0; i < GetBookmarkButtonCount(); ++i) {
1305 if (sender == GetBookmarkButton(i)) { 1302 if (sender == GetBookmarkButton(i)) {
1306 return GetDragOperationsForNode( 1303 return GetDragOperationsForNode(
1307 model_->GetBookmarkBarNode()->GetChild(i)); 1304 model_->GetBookmarkBarNode()->GetChild(i));
1308 } 1305 }
1309 } 1306 }
1310 NOTREACHED(); 1307 NOTREACHED();
1311 return DragDropTypes::DRAG_NONE; 1308 return DragDropTypes::DRAG_NONE;
1312 } 1309 }
1313 1310
1314 void BookmarkBarView::RunMenu(ChromeViews::View* view, 1311 void BookmarkBarView::RunMenu(ChromeViews::View* view,
1315 const CPoint& pt, 1312 const CPoint& pt,
1316 HWND hwnd) { 1313 HWND hwnd) {
1317 BookmarkBarNode* node; 1314 BookmarkNode* node;
1318 MenuItemView::AnchorPosition anchor_point = MenuItemView::TOPLEFT; 1315 MenuItemView::AnchorPosition anchor_point = MenuItemView::TOPLEFT;
1319 1316
1320 // When we set the menu's position, we must take into account the mirrored 1317 // When we set the menu's position, we must take into account the mirrored
1321 // position of the View relative to its parent. This can be easily done by 1318 // position of the View relative to its parent. This can be easily done by
1322 // passing the right flag to View::GetX(). 1319 // passing the right flag to View::GetX().
1323 int x = view->GetX(APPLY_MIRRORING_TRANSFORMATION); 1320 int x = view->GetX(APPLY_MIRRORING_TRANSFORMATION);
1324 int height = GetHeight() - kMenuOffset; 1321 int height = GetHeight() - kMenuOffset;
1325 1322
1326 if (IsNewTabPage() && !IsAlwaysShown()) 1323 if (IsNewTabPage() && !IsAlwaysShown())
1327 height -= kNewtabVerticalPadding; 1324 height -= kNewtabVerticalPadding;
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
1362 menu_runner_->RunMenuAt(parent_hwnd, 1359 menu_runner_->RunMenuAt(parent_hwnd,
1363 gfx::Rect(screen_loc.x, screen_loc.y, 1360 gfx::Rect(screen_loc.x, screen_loc.y,
1364 view->GetWidth(), height), 1361 view->GetWidth(), height),
1365 anchor_point, 1362 anchor_point,
1366 false); 1363 false);
1367 } 1364 }
1368 1365
1369 void BookmarkBarView::ButtonPressed(ChromeViews::BaseButton* sender) { 1366 void BookmarkBarView::ButtonPressed(ChromeViews::BaseButton* sender) {
1370 int index = GetChildIndex(sender); 1367 int index = GetChildIndex(sender);
1371 DCHECK(index != -1); 1368 DCHECK(index != -1);
1372 BookmarkBarNode* node = model_->GetBookmarkBarNode()->GetChild(index); 1369 BookmarkNode* node = model_->GetBookmarkBarNode()->GetChild(index);
1373 DCHECK(page_navigator_); 1370 DCHECK(page_navigator_);
1374 page_navigator_->OpenURL( 1371 page_navigator_->OpenURL(
1375 node->GetURL(), 1372 node->GetURL(),
1376 event_utils::DispositionFromEventFlags(sender->mouse_event_flags()), 1373 event_utils::DispositionFromEventFlags(sender->mouse_event_flags()),
1377 PageTransition::AUTO_BOOKMARK); 1374 PageTransition::AUTO_BOOKMARK);
1378 UserMetrics::RecordAction(L"ClickedBookmarkBarURLButton", profile_); 1375 UserMetrics::RecordAction(L"ClickedBookmarkBarURLButton", profile_);
1379 } 1376 }
1380 1377
1381 void BookmarkBarView::ShowContextMenu(View* source, 1378 void BookmarkBarView::ShowContextMenu(View* source,
1382 int x, 1379 int x,
1383 int y, 1380 int y,
1384 bool is_mouse_gesture) { 1381 bool is_mouse_gesture) {
1385 if (!model_->IsLoaded()) { 1382 if (!model_->IsLoaded()) {
1386 // Don't do anything if the model isn't loaded. 1383 // Don't do anything if the model isn't loaded.
1387 return; 1384 return;
1388 } 1385 }
1389 1386
1390 BookmarkBarNode* node = model_->GetBookmarkBarNode(); 1387 BookmarkNode* node = model_->GetBookmarkBarNode();
1391 if (source == other_bookmarked_button_) { 1388 if (source == other_bookmarked_button_) {
1392 node = model_->other_node(); 1389 node = model_->other_node();
1393 } else if (source != this) { 1390 } else if (source != this) {
1394 // User clicked on one of the bookmark buttons, find which one they 1391 // User clicked on one of the bookmark buttons, find which one they
1395 // clicked on. 1392 // clicked on.
1396 int bookmark_button_index = GetChildIndex(source); 1393 int bookmark_button_index = GetChildIndex(source);
1397 DCHECK(bookmark_button_index != -1 && 1394 DCHECK(bookmark_button_index != -1 &&
1398 bookmark_button_index < GetBookmarkButtonCount()); 1395 bookmark_button_index < GetBookmarkButtonCount());
1399 node = model_->GetBookmarkBarNode()->GetChild(bookmark_button_index); 1396 node = model_->GetBookmarkBarNode()->GetChild(bookmark_button_index);
1400 } 1397 }
1401 BookmarkBarContextMenuController controller(this, node); 1398 BookmarkBarContextMenuController controller(this, node);
1402 controller.RunMenuAt(x, y); 1399 controller.RunMenuAt(x, y);
1403 } 1400 }
1404 1401
1405 ChromeViews::View* BookmarkBarView::CreateBookmarkButton( 1402 ChromeViews::View* BookmarkBarView::CreateBookmarkButton(BookmarkNode* node) {
1406 BookmarkBarNode* node) {
1407 if (node->GetType() == history::StarredEntry::URL) { 1403 if (node->GetType() == history::StarredEntry::URL) {
1408 BookmarkButton* button = new BookmarkButton(node->GetURL(), 1404 BookmarkButton* button = new BookmarkButton(node->GetURL(),
1409 node->GetTitle(), 1405 node->GetTitle(),
1410 GetProfile()); 1406 GetProfile());
1411 button->SetListener(this, 0); 1407 button->SetListener(this, 0);
1412 ConfigureButton(node, button); 1408 ConfigureButton(node, button);
1413 return button; 1409 return button;
1414 } else { 1410 } else {
1415 ChromeViews::MenuButton* button = 1411 ChromeViews::MenuButton* button =
1416 new ChromeViews::MenuButton(node->GetTitle(), this, false); 1412 new ChromeViews::MenuButton(node->GetTitle(), this, false);
1417 button->SetIcon(GetGroupIcon()); 1413 button->SetIcon(GetGroupIcon());
1418 ConfigureButton(node, button); 1414 ConfigureButton(node, button);
1419 return button; 1415 return button;
1420 } 1416 }
1421 } 1417 }
1422 1418
1423 void BookmarkBarView::ConfigureButton(BookmarkBarNode* node, 1419 void BookmarkBarView::ConfigureButton(BookmarkNode* node,
1424 ChromeViews::TextButton* button) { 1420 ChromeViews::TextButton* button) {
1425 button->SetText(node->GetTitle()); 1421 button->SetText(node->GetTitle());
1426 button->ClearMaxTextSize(); 1422 button->ClearMaxTextSize();
1427 button->SetContextMenuController(this); 1423 button->SetContextMenuController(this);
1428 button->SetDragController(this); 1424 button->SetDragController(this);
1429 if (node->GetType() == history::StarredEntry::URL) { 1425 if (node->GetType() == history::StarredEntry::URL) {
1430 if (node->GetFavIcon().width() != 0) 1426 if (node->GetFavIcon().width() != 0)
1431 button->SetIcon(node->GetFavIcon()); 1427 button->SetIcon(node->GetFavIcon());
1432 else 1428 else
1433 button->SetIcon(*kDefaultFavIcon); 1429 button->SetIcon(*kDefaultFavIcon);
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
1477 ns->RemoveObserver(this, NOTIFY_BOOKMARK_BUBBLE_HIDDEN, ns_source); 1473 ns->RemoveObserver(this, NOTIFY_BOOKMARK_BUBBLE_HIDDEN, ns_source);
1478 ns->RemoveObserver(this, NOTIFY_BOOKMARK_BAR_VISIBILITY_PREF_CHANGED, 1474 ns->RemoveObserver(this, NOTIFY_BOOKMARK_BAR_VISIBILITY_PREF_CHANGED,
1479 NotificationService::AllSources()); 1475 NotificationService::AllSources());
1480 } 1476 }
1481 1477
1482 void BookmarkBarView::NotifyModelChanged() { 1478 void BookmarkBarView::NotifyModelChanged() {
1483 if (model_changed_listener_) 1479 if (model_changed_listener_)
1484 model_changed_listener_->ModelChanged(); 1480 model_changed_listener_->ModelChanged();
1485 } 1481 }
1486 1482
1487 void BookmarkBarView::ShowDropFolderForNode(BookmarkBarNode* node) { 1483 void BookmarkBarView::ShowDropFolderForNode(BookmarkNode* node) {
1488 if (drop_menu_runner_.get() && drop_menu_runner_->GetNode() == node) { 1484 if (drop_menu_runner_.get() && drop_menu_runner_->GetNode() == node) {
1489 // Already showing for the specified node. 1485 // Already showing for the specified node.
1490 return; 1486 return;
1491 } 1487 }
1492 1488
1493 int start_index = 0; 1489 int start_index = 0;
1494 View* view_to_position_menu_from; 1490 View* view_to_position_menu_from;
1495 1491
1496 // Note that both the anchor position and the position of the menu itself 1492 // Note that both the anchor position and the position of the menu itself
1497 // change depending on the locale. Also note that we must apply the 1493 // change depending on the locale. Also note that we must apply the
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
1535 view_to_position_menu_from->GetWidth(), 1531 view_to_position_menu_from->GetWidth(),
1536 view_to_position_menu_from->GetHeight()), 1532 view_to_position_menu_from->GetHeight()),
1537 anchor, true); 1533 anchor, true);
1538 } 1534 }
1539 1535
1540 void BookmarkBarView::StopShowFolderDropMenuTimer() { 1536 void BookmarkBarView::StopShowFolderDropMenuTimer() {
1541 if (show_folder_drop_menu_task_) 1537 if (show_folder_drop_menu_task_)
1542 show_folder_drop_menu_task_->Cancel(); 1538 show_folder_drop_menu_task_->Cancel();
1543 } 1539 }
1544 1540
1545 void BookmarkBarView::StartShowFolderDropMenuTimer(BookmarkBarNode* node) { 1541 void BookmarkBarView::StartShowFolderDropMenuTimer(BookmarkNode* node) {
1546 if (testing_) { 1542 if (testing_) {
1547 // So that tests can run as fast as possible disable the delay during 1543 // So that tests can run as fast as possible disable the delay during
1548 // testing. 1544 // testing.
1549 ShowDropFolderForNode(node); 1545 ShowDropFolderForNode(node);
1550 return; 1546 return;
1551 } 1547 }
1552 DCHECK(!show_folder_drop_menu_task_); 1548 DCHECK(!show_folder_drop_menu_task_);
1553 show_folder_drop_menu_task_ = new ShowFolderDropMenuTask(this, node); 1549 show_folder_drop_menu_task_ = new ShowFolderDropMenuTask(this, node);
1554 static DWORD delay = 0; 1550 static DWORD delay = 0;
1555 if (!delay && !SystemParametersInfo(SPI_GETMENUSHOWDELAY, 0, &delay, 0)) { 1551 if (!delay && !SystemParametersInfo(SPI_GETMENUSHOWDELAY, 0, &delay, 0)) {
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
1601 return DragDropTypes::DRAG_COPY; 1597 return DragDropTypes::DRAG_COPY;
1602 } 1598 }
1603 1599
1604 for (int i = 0; i < GetBookmarkButtonCount() && 1600 for (int i = 0; i < GetBookmarkButtonCount() &&
1605 GetBookmarkButton(i)->IsVisible() && !found; i++) { 1601 GetBookmarkButton(i)->IsVisible() && !found; i++) {
1606 ChromeViews::TextButton* button = GetBookmarkButton(i); 1602 ChromeViews::TextButton* button = GetBookmarkButton(i);
1607 int button_x = mirrored_x - button->GetX(); 1603 int button_x = mirrored_x - button->GetX();
1608 int button_w = button->GetWidth(); 1604 int button_w = button->GetWidth();
1609 if (button_x < button_w) { 1605 if (button_x < button_w) {
1610 found = true; 1606 found = true;
1611 BookmarkBarNode* node = model_->GetBookmarkBarNode()->GetChild(i); 1607 BookmarkNode* node = model_->GetBookmarkBarNode()->GetChild(i);
1612 if (node->GetType() != history::StarredEntry::URL) { 1608 if (node->GetType() != history::StarredEntry::URL) {
1613 if (button_x <= MenuItemView::kDropBetweenPixels) { 1609 if (button_x <= MenuItemView::kDropBetweenPixels) {
1614 *index = i; 1610 *index = i;
1615 } else if (button_x < button_w - MenuItemView::kDropBetweenPixels) { 1611 } else if (button_x < button_w - MenuItemView::kDropBetweenPixels) {
1616 *index = i; 1612 *index = i;
1617 *drop_on = true; 1613 *drop_on = true;
1618 } else { 1614 } else {
1619 *index = i + 1; 1615 *index = i + 1;
1620 } 1616 }
1621 } else if (button_x < button_w / 2) { 1617 } else if (button_x < button_w / 2) {
(...skipping 24 matching lines...) Expand all
1646 } else if (mirrored_x < other_bookmarked_button_->GetX()) { 1642 } else if (mirrored_x < other_bookmarked_button_->GetX()) {
1647 // Mouse is after the last visible button but before more recently 1643 // Mouse is after the last visible button but before more recently
1648 // bookmarked; use the last visible index. 1644 // bookmarked; use the last visible index.
1649 *index = GetFirstHiddenNodeIndex(); 1645 *index = GetFirstHiddenNodeIndex();
1650 } else { 1646 } else {
1651 return DragDropTypes::DRAG_NONE; 1647 return DragDropTypes::DRAG_NONE;
1652 } 1648 }
1653 } 1649 }
1654 1650
1655 if (*drop_on) { 1651 if (*drop_on) {
1656 BookmarkBarNode* parent = 1652 BookmarkNode* parent =
1657 *is_over_other ? model_->other_node() : 1653 *is_over_other ? model_->other_node() :
1658 model_->GetBookmarkBarNode()->GetChild(*index); 1654 model_->GetBookmarkBarNode()->GetChild(*index);
1659 int operation = 1655 int operation =
1660 CalculateDropOperation(data, parent, parent->GetChildCount()); 1656 CalculateDropOperation(data, parent, parent->GetChildCount());
1661 if (!operation && !data.is_url && 1657 if (!operation && !data.is_url &&
1662 data.profile_id == GetProfile()->GetID()) { 1658 data.profile_id == GetProfile()->GetID()) {
1663 if (data.GetNode(model_) == parent) { 1659 if (data.GetNode(model_) == parent) {
1664 // Don't open a menu if the node being dragged is the the menu to 1660 // Don't open a menu if the node being dragged is the the menu to
1665 // open. 1661 // open.
1666 *drop_on = false; 1662 *drop_on = false;
1667 } 1663 }
1668 } 1664 }
1669 return operation; 1665 return operation;
1670 } else { 1666 } else {
1671 return CalculateDropOperation(data, model_->GetBookmarkBarNode(), *index); 1667 return CalculateDropOperation(data, model_->GetBookmarkBarNode(), *index);
1672 } 1668 }
1673 } 1669 }
1674 1670
1675 int BookmarkBarView::CalculateDropOperation(const BookmarkDragData& data, 1671 int BookmarkBarView::CalculateDropOperation(const BookmarkDragData& data,
1676 BookmarkBarNode* parent, 1672 BookmarkNode* parent,
1677 int index) { 1673 int index) {
1678 if (!CanDropAt(data, parent, index)) 1674 if (!CanDropAt(data, parent, index))
1679 return DragDropTypes::DRAG_NONE; 1675 return DragDropTypes::DRAG_NONE;
1680 1676
1681 if (data.is_url) { 1677 if (data.is_url) {
1682 // User is dragging a URL. 1678 // User is dragging a URL.
1683 BookmarkBarNode* node = model_->GetNodeByURL(data.url); 1679 BookmarkNode* node = model_->GetNodeByURL(data.url);
1684 if (!node) { 1680 if (!node) {
1685 // We don't have a node with this url. 1681 // We don't have a node with this url.
1686 return DragDropTypes::DRAG_COPY; 1682 return DragDropTypes::DRAG_COPY;
1687 } 1683 }
1688 // Technically we're going to move, but most sources export as copy so that 1684 // Technically we're going to move, but most sources export as copy so that
1689 // if we don't accept copy we won't accept the drop. 1685 // if we don't accept copy we won't accept the drop.
1690 return DragDropTypes::DRAG_MOVE | DragDropTypes::DRAG_COPY; 1686 return DragDropTypes::DRAG_MOVE | DragDropTypes::DRAG_COPY;
1691 } else if (data.profile_id == GetProfile()->GetID()) { 1687 } else if (data.profile_id == GetProfile()->GetID()) {
1692 // Dropping a group from the same profile results in a move. 1688 // Dropping a group from the same profile results in a move.
1693 BookmarkBarNode* node = data.GetNode(model_); 1689 BookmarkNode* node = data.GetNode(model_);
1694 if (!node) { 1690 if (!node) {
1695 // Generally shouldn't get here, we originated the drag but couldn't 1691 // Generally shouldn't get here, we originated the drag but couldn't
1696 // find the node. 1692 // find the node.
1697 return DragDropTypes::DRAG_NONE; 1693 return DragDropTypes::DRAG_NONE;
1698 } 1694 }
1699 return DragDropTypes::DRAG_MOVE; 1695 return DragDropTypes::DRAG_MOVE;
1700 } else { 1696 } else {
1701 // Dropping a group from different profile. Always accept. 1697 // Dropping a group from different profile. Always accept.
1702 return DragDropTypes::DRAG_COPY; 1698 return DragDropTypes::DRAG_COPY;
1703 } 1699 }
1704 } 1700 }
1705 1701
1706 bool BookmarkBarView::CanDropAt(const BookmarkDragData& data, 1702 bool BookmarkBarView::CanDropAt(const BookmarkDragData& data,
1707 BookmarkBarNode* parent, 1703 BookmarkNode* parent,
1708 int index) { 1704 int index) {
1709 DCHECK(data.is_valid); 1705 DCHECK(data.is_valid);
1710 if (data.is_url) { 1706 if (data.is_url) {
1711 BookmarkBarNode* existing_node = model_->GetNodeByURL(data.url); 1707 BookmarkNode* existing_node = model_->GetNodeByURL(data.url);
1712 if (existing_node && existing_node->GetParent() == parent) { 1708 if (existing_node && existing_node->GetParent() == parent) {
1713 const int existing_index = parent->IndexOfChild(existing_node); 1709 const int existing_index = parent->IndexOfChild(existing_node);
1714 if (index == existing_index || existing_index + 1 == index) 1710 if (index == existing_index || existing_index + 1 == index)
1715 return false; 1711 return false;
1716 } 1712 }
1717 return true; 1713 return true;
1718 } else if (data.profile_id == profile_->GetID()) { 1714 } else if (data.profile_id == profile_->GetID()) {
1719 BookmarkBarNode* existing_node = data.GetNode(model_); 1715 BookmarkNode* existing_node = data.GetNode(model_);
1720 if (existing_node) { 1716 if (existing_node) {
1721 if (existing_node->GetParent() == parent) { 1717 if (existing_node->GetParent() == parent) {
1722 const int existing_index = parent->IndexOfChild(existing_node); 1718 const int existing_index = parent->IndexOfChild(existing_node);
1723 if (index == existing_index || existing_index + 1 == index) 1719 if (index == existing_index || existing_index + 1 == index)
1724 return false; 1720 return false;
1725 } 1721 }
1726 // Allow the drop only if the node we're going to drop on isn't a 1722 // Allow the drop only if the node we're going to drop on isn't a
1727 // descendant of the dragged node. 1723 // descendant of the dragged node.
1728 BookmarkBarNode* test_node = parent; 1724 BookmarkNode* test_node = parent;
1729 while (test_node && test_node != existing_node) 1725 while (test_node && test_node != existing_node)
1730 test_node = test_node->GetParent(); 1726 test_node = test_node->GetParent();
1731 return (test_node == NULL); 1727 return (test_node == NULL);
1732 } 1728 }
1733 } // else case clones, always allow. 1729 } // else case clones, always allow.
1734 return true; 1730 return true;
1735 } 1731 }
1736 1732
1737 1733
1738 int BookmarkBarView::PerformDropImpl(const BookmarkDragData& data, 1734 int BookmarkBarView::PerformDropImpl(const BookmarkDragData& data,
1739 BookmarkBarNode* parent_node, 1735 BookmarkNode* parent_node,
1740 int index) { 1736 int index) {
1741 if (data.is_url) { 1737 if (data.is_url) {
1742 // User is dragging a URL. 1738 // User is dragging a URL.
1743 BookmarkBarNode* node = model_->GetNodeByURL(data.url); 1739 BookmarkNode* node = model_->GetNodeByURL(data.url);
1744 if (!node) { 1740 if (!node) {
1745 std::wstring title = data.title; 1741 std::wstring title = data.title;
1746 if (title.empty()) { 1742 if (title.empty()) {
1747 // No title, use the host. 1743 // No title, use the host.
1748 title = UTF8ToWide(data.url.host()); 1744 title = UTF8ToWide(data.url.host());
1749 if (title.empty()) 1745 if (title.empty())
1750 title = l10n_util::GetString(IDS_BOOMARK_BAR_UNKNOWN_DRAG_TITLE); 1746 title = l10n_util::GetString(IDS_BOOMARK_BAR_UNKNOWN_DRAG_TITLE);
1751 } 1747 }
1752 model_->AddURL(parent_node, index, title, data.url); 1748 model_->AddURL(parent_node, index, title, data.url);
1753 return DragDropTypes::DRAG_COPY; 1749 return DragDropTypes::DRAG_COPY;
1754 } 1750 }
1755 model_->Move(node, parent_node, index); 1751 model_->Move(node, parent_node, index);
1756 return DragDropTypes::DRAG_MOVE; 1752 return DragDropTypes::DRAG_MOVE;
1757 } else if (data.profile_id == GetProfile()->GetID()) { 1753 } else if (data.profile_id == GetProfile()->GetID()) {
1758 BookmarkBarNode* node = data.GetNode(model_); 1754 BookmarkNode* node = data.GetNode(model_);
1759 if (!node) { 1755 if (!node) {
1760 // Generally shouldn't get here, we originated the drag but couldn't 1756 // Generally shouldn't get here, we originated the drag but couldn't
1761 // find the node. Do nothing. 1757 // find the node. Do nothing.
1762 return DragDropTypes::DRAG_COPY; 1758 return DragDropTypes::DRAG_COPY;
1763 } 1759 }
1764 model_->Move(node, parent_node, index); 1760 model_->Move(node, parent_node, index);
1765 return DragDropTypes::DRAG_MOVE; 1761 return DragDropTypes::DRAG_MOVE;
1766 } else { 1762 } else {
1767 // Dropping a group from different profile. Always accept. 1763 // Dropping a group from different profile. Always accept.
1768 CloneDragData(data, parent_node, index); 1764 CloneDragData(data, parent_node, index);
1769 return DragDropTypes::DRAG_COPY; 1765 return DragDropTypes::DRAG_COPY;
1770 } 1766 }
1771 } 1767 }
1772 1768
1773 void BookmarkBarView::CloneDragData(const BookmarkDragData& data, 1769 void BookmarkBarView::CloneDragData(const BookmarkDragData& data,
1774 BookmarkBarNode* parent, 1770 BookmarkNode* parent,
1775 int index_to_add_at) { 1771 int index_to_add_at) {
1776 DCHECK(data.is_valid && model_); 1772 DCHECK(data.is_valid && model_);
1777 if (data.is_url) { 1773 if (data.is_url) {
1778 BookmarkBarNode* node = model_->GetNodeByURL(data.url); 1774 BookmarkNode* node = model_->GetNodeByURL(data.url);
1779 if (node) { 1775 if (node) {
1780 model_->Move(node, parent, index_to_add_at); 1776 model_->Move(node, parent, index_to_add_at);
1781 } else { 1777 } else {
1782 model_->AddURL(parent, index_to_add_at, data.title, data.url); 1778 model_->AddURL(parent, index_to_add_at, data.title, data.url);
1783 } 1779 }
1784 } else { 1780 } else {
1785 BookmarkBarNode* new_folder = model_->AddGroup(parent, index_to_add_at, 1781 BookmarkNode* new_folder = model_->AddGroup(parent, index_to_add_at,
1786 data.title); 1782 data.title);
1787 for (int i = 0; i < static_cast<int>(data.children.size()); ++i) 1783 for (int i = 0; i < static_cast<int>(data.children.size()); ++i)
1788 CloneDragData(data.children[i], new_folder, i); 1784 CloneDragData(data.children[i], new_folder, i);
1789 } 1785 }
1790 } 1786 }
1791 1787
1792 int BookmarkBarView::GetFirstHiddenNodeIndex() { 1788 int BookmarkBarView::GetFirstHiddenNodeIndex() {
1793 const int bb_count = GetBookmarkButtonCount(); 1789 const int bb_count = GetBookmarkButtonCount();
1794 for (int i = 0; i < bb_count; ++i) { 1790 for (int i = 0; i < bb_count; ++i) {
1795 if (!GetBookmarkButton(i)->IsVisible()) 1791 if (!GetBookmarkButton(i)->IsVisible())
1796 return i; 1792 return i;
1797 } 1793 }
1798 return bb_count; 1794 return bb_count;
1799 } 1795 }
1800 1796
1801 void BookmarkBarView::StartThrobbing() { 1797 void BookmarkBarView::StartThrobbing() {
1802 DCHECK(!throbbing_view_); 1798 DCHECK(!throbbing_view_);
1803 1799
1804 if (bubble_url_.is_empty()) 1800 if (bubble_url_.is_empty())
1805 return; // Bubble isn't showing; nothing to throb. 1801 return; // Bubble isn't showing; nothing to throb.
1806 1802
1807 if (!GetViewContainer()) 1803 if (!GetViewContainer())
1808 return; // We're not showing, don't do anything. 1804 return; // We're not showing, don't do anything.
1809 1805
1810 BookmarkBarNode* node = model_->GetNodeByURL(bubble_url_); 1806 BookmarkNode* node = model_->GetNodeByURL(bubble_url_);
1811 if (!node) 1807 if (!node)
1812 return; // Generally shouldn't happen. 1808 return; // Generally shouldn't happen.
1813 1809
1814 // Determine which visible button is showing the url (or is an ancestor of 1810 // Determine which visible button is showing the url (or is an ancestor of
1815 // the url). 1811 // the url).
1816 if (node->HasAncestor(model_->GetBookmarkBarNode())) { 1812 if (node->HasAncestor(model_->GetBookmarkBarNode())) {
1817 BookmarkBarNode* bbn = model_->GetBookmarkBarNode(); 1813 BookmarkNode* bbn = model_->GetBookmarkBarNode();
1818 BookmarkBarNode* parent_on_bb = node; 1814 BookmarkNode* parent_on_bb = node;
1819 while (parent_on_bb->GetParent() != bbn) 1815 while (parent_on_bb->GetParent() != bbn)
1820 parent_on_bb = parent_on_bb->GetParent(); 1816 parent_on_bb = parent_on_bb->GetParent();
1821 int index = bbn->IndexOfChild(parent_on_bb); 1817 int index = bbn->IndexOfChild(parent_on_bb);
1822 if (index >= GetFirstHiddenNodeIndex()) { 1818 if (index >= GetFirstHiddenNodeIndex()) {
1823 // Node is hidden, animate the overflow button. 1819 // Node is hidden, animate the overflow button.
1824 throbbing_view_ = overflow_button_; 1820 throbbing_view_ = overflow_button_;
1825 } else { 1821 } else {
1826 throbbing_view_ = static_cast<BaseButton*>(GetChildViewAt(index)); 1822 throbbing_view_ = static_cast<BaseButton*>(GetChildViewAt(index));
1827 } 1823 }
1828 } else { 1824 } else {
1829 throbbing_view_ = other_bookmarked_button_; 1825 throbbing_view_ = other_bookmarked_button_;
1830 } 1826 }
1831 1827
1832 // Use a large number so that the button continues to throb. 1828 // Use a large number so that the button continues to throb.
1833 throbbing_view_->StartThrobbing(std::numeric_limits<int>::max()); 1829 throbbing_view_->StartThrobbing(std::numeric_limits<int>::max());
1834 } 1830 }
1835 1831
1836 void BookmarkBarView::StopThrobbing(bool immediate) { 1832 void BookmarkBarView::StopThrobbing(bool immediate) {
1837 if (!throbbing_view_) 1833 if (!throbbing_view_)
1838 return; 1834 return;
1839 1835
1840 // If not immediate, cycle through 2 more complete cycles. 1836 // If not immediate, cycle through 2 more complete cycles.
1841 throbbing_view_->StartThrobbing(immediate ? 0 : 4); 1837 throbbing_view_->StartThrobbing(immediate ? 0 : 4);
1842 throbbing_view_ = NULL; 1838 throbbing_view_ = NULL;
1843 } 1839 }
1844 1840
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698