OLD | NEW |
| (Empty) |
1 // Copyright (c) 2010 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_menu_button.h" | |
6 | |
7 #include "build/build_config.h" | |
8 | |
9 #if defined(OS_GTK) | |
10 #include <gtk/gtk.h> | |
11 #endif | |
12 | |
13 #include "app/os_exchange_data.h" | |
14 #include "app/resource_bundle.h" | |
15 #include "app/theme_provider.h" | |
16 #include "chrome/browser/bookmarks/bookmark_model.h" | |
17 #include "chrome/browser/bookmarks/bookmark_utils.h" | |
18 #include "chrome/browser/browser.h" | |
19 #include "chrome/browser/browser_theme_provider.h" | |
20 #include "chrome/browser/metrics/user_metrics.h" | |
21 #include "chrome/browser/profile.h" | |
22 #include "chrome/browser/tab_contents/tab_contents.h" | |
23 #include "chrome/browser/view_ids.h" | |
24 #include "grit/theme_resources.h" | |
25 #include "views/window/window.h" | |
26 | |
27 BookmarkMenuButton::BookmarkMenuButton(Browser* browser) | |
28 : views::MenuButton(NULL, std::wstring(), NULL, false), | |
29 browser_(browser), | |
30 bookmark_drop_menu_(NULL), | |
31 drop_operation_(0) { | |
32 // TODO(sky): if we keep this code, we need real icons, a11y support, and a | |
33 // tooltip. | |
34 set_menu_delegate(this); | |
35 SetID(VIEW_ID_BOOKMARK_MENU); | |
36 | |
37 ThemeProvider* tp = browser_->profile()->GetThemeProvider(); | |
38 SetIcon(*tp->GetBitmapNamed(IDR_MENU_BOOKMARK)); | |
39 } | |
40 | |
41 BookmarkMenuButton::~BookmarkMenuButton() { | |
42 if (bookmark_drop_menu_) | |
43 bookmark_drop_menu_->set_observer(NULL); | |
44 } | |
45 | |
46 bool BookmarkMenuButton::GetDropFormats( | |
47 int* formats, | |
48 std::set<OSExchangeData::CustomFormat>* custom_formats) { | |
49 BookmarkModel* bookmark_model = GetBookmarkModel(); | |
50 if (!bookmark_model || !bookmark_model->IsLoaded()) | |
51 return false; | |
52 | |
53 *formats = OSExchangeData::URL; | |
54 custom_formats->insert(BookmarkDragData::GetBookmarkCustomFormat()); | |
55 return true; | |
56 } | |
57 | |
58 bool BookmarkMenuButton::AreDropTypesRequired() { | |
59 return true; | |
60 } | |
61 | |
62 bool BookmarkMenuButton::CanDrop(const OSExchangeData& data) { | |
63 BookmarkModel* bookmark_model = GetBookmarkModel(); | |
64 if (!bookmark_model || !bookmark_model->IsLoaded()) | |
65 return false; | |
66 | |
67 drag_data_ = BookmarkDragData(); | |
68 // Only accept drops of 1 node, which is the case for all data dragged from | |
69 // bookmark bar and menus. | |
70 return drag_data_.Read(data) && drag_data_.has_single_url(); | |
71 } | |
72 | |
73 int BookmarkMenuButton::OnDragUpdated(const views::DropTargetEvent& event) { | |
74 if (!drag_data_.is_valid()) | |
75 return 0; | |
76 | |
77 BookmarkModel* bookmark_model = GetBookmarkModel(); | |
78 drop_operation_ = bookmark_utils::BookmarkDropOperation( | |
79 browser_->profile(), event, drag_data_, | |
80 bookmark_model->GetBookmarkBarNode(), | |
81 bookmark_model->GetBookmarkBarNode()->GetChildCount()); | |
82 if (drop_operation_ != 0) | |
83 StartShowFolderDropMenuTimer(); | |
84 else | |
85 StopShowFolderDropMenuTimer(); | |
86 return drop_operation_; | |
87 } | |
88 | |
89 void BookmarkMenuButton::OnDragExited() { | |
90 StopShowFolderDropMenuTimer(); | |
91 drag_data_ = BookmarkDragData(); | |
92 } | |
93 | |
94 int BookmarkMenuButton::OnPerformDrop(const views::DropTargetEvent& event) { | |
95 StopShowFolderDropMenuTimer(); | |
96 | |
97 if (bookmark_drop_menu_) | |
98 bookmark_drop_menu_->Cancel(); | |
99 | |
100 // Reset the drag data to take as little memory as needed. | |
101 BookmarkDragData data = drag_data_; | |
102 drag_data_ = BookmarkDragData(); | |
103 | |
104 if (!drop_operation_) | |
105 return DragDropTypes::DRAG_NONE; | |
106 | |
107 BookmarkModel* model = GetBookmarkModel(); | |
108 if (!model) | |
109 return DragDropTypes::DRAG_NONE; | |
110 | |
111 const BookmarkNode* parent = model->GetBookmarkBarNode(); | |
112 return bookmark_utils::PerformBookmarkDrop( | |
113 browser_->profile(), data, parent, parent->GetChildCount()); | |
114 } | |
115 | |
116 void BookmarkMenuButton::BookmarkMenuDeleted( | |
117 BookmarkMenuController* controller) { | |
118 bookmark_drop_menu_ = NULL; | |
119 } | |
120 | |
121 void BookmarkMenuButton::RunMenu(views::View* source, | |
122 const gfx::Point& pt) { | |
123 RunMenu(source, pt, GetWindow()->GetNativeWindow(), false); | |
124 } | |
125 | |
126 void BookmarkMenuButton::RunMenu(views::View* source, | |
127 const gfx::Point& pt, | |
128 gfx::NativeWindow hwnd, | |
129 bool for_drop) { | |
130 Profile* profile = browser_->profile(); | |
131 | |
132 UserMetrics::RecordAction(UserMetricsAction("BookmarkMenu_clicked"), | |
133 profile); | |
134 | |
135 BookmarkMenuController* menu = new BookmarkMenuController( | |
136 browser_, profile, browser_->GetSelectedTabContents(), hwnd, | |
137 GetBookmarkModel()->GetBookmarkBarNode(), 0, true); | |
138 | |
139 views::MenuItemView::AnchorPosition anchor = views::MenuItemView::TOPRIGHT; | |
140 if (base::i18n::IsRTL()) | |
141 anchor = views::MenuItemView::TOPLEFT; | |
142 if (for_drop) { | |
143 bookmark_drop_menu_ = menu; | |
144 bookmark_drop_menu_->set_observer(this); | |
145 } | |
146 menu->RunMenuAt(this, views::MenuItemView::TOPRIGHT, for_drop); | |
147 } | |
148 | |
149 BookmarkModel* BookmarkMenuButton::GetBookmarkModel() { | |
150 return browser_->profile()->GetBookmarkModel(); | |
151 } | |
152 | |
153 void BookmarkMenuButton::StartShowFolderDropMenuTimer() { | |
154 if (show_drop_menu_timer_.IsRunning()) | |
155 return; | |
156 | |
157 #if defined(OS_WIN) | |
158 static DWORD delay = 0; | |
159 if (!delay && !SystemParametersInfo(SPI_GETMENUSHOWDELAY, 0, &delay, 0)) | |
160 delay = 400; | |
161 #else | |
162 static int delay = 400; | |
163 #endif | |
164 show_drop_menu_timer_.Start(base::TimeDelta::FromMilliseconds(delay), | |
165 this, &BookmarkMenuButton::ShowDropMenu); | |
166 } | |
167 | |
168 void BookmarkMenuButton::StopShowFolderDropMenuTimer() { | |
169 show_drop_menu_timer_.Stop(); | |
170 } | |
171 | |
172 void BookmarkMenuButton::ShowDropMenu() { | |
173 RunMenu(NULL, gfx::Point(), GetWindow()->GetNativeWindow(), true); | |
174 } | |
OLD | NEW |