OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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/tab_contents/chrome_web_contents_view_win_delegate.h" | |
6 | |
7 #include "chrome/browser/browser_shutdown.h" | |
8 #include "chrome/browser/tab_contents/web_drag_bookmark_handler_win.h" | |
9 #include "chrome/browser/ui/constrained_window_tab_helper.h" | |
10 #include "chrome/browser/ui/sad_tab_helper.h" | |
11 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | |
12 #include "chrome/browser/ui/views/constrained_window_views.h" | |
13 #include "chrome/browser/ui/views/tab_contents/render_view_context_menu_views.h" | |
14 #include "content/public/browser/render_process_host.h" | |
15 #include "content/public/browser/render_view_host.h" | |
16 #include "content/public/browser/render_widget_host_view.h" | |
17 #include "content/public/browser/web_contents.h" | |
18 #include "content/public/browser/web_contents_view.h" | |
19 #include "ui/views/focus/focus_manager.h" | |
20 #include "ui/views/focus/view_storage.h" | |
21 #include "ui/views/widget/widget.h" | |
22 | |
23 ChromeWebContentsViewWinDelegate::ChromeWebContentsViewWinDelegate( | |
24 content::WebContents* web_contents) | |
25 : web_contents_(web_contents) { | |
26 last_focused_view_storage_id_ = | |
27 views::ViewStorage::GetInstance()->CreateStorageID(); | |
28 } | |
29 | |
30 ChromeWebContentsViewWinDelegate::~ChromeWebContentsViewWinDelegate() { | |
31 // Makes sure to remove any stored view we may still have in the ViewStorage. | |
32 // | |
33 // It is possible the view went away before us, so we only do this if the | |
34 // view is registered. | |
35 views::ViewStorage* view_storage = views::ViewStorage::GetInstance(); | |
36 if (view_storage->RetrieveView(last_focused_view_storage_id_) != NULL) | |
37 view_storage->RemoveView(last_focused_view_storage_id_); | |
38 | |
39 } | |
40 | |
41 content::WebDragDestDelegate* | |
42 ChromeWebContentsViewWinDelegate::GetDragDestDelegate() { | |
43 // We install a chrome specific handler to intercept bookmark drags for the | |
44 // bookmark manager/extension API. | |
45 bookmark_handler_.reset(new WebDragBookmarkHandlerWin); | |
46 return bookmark_handler_.get(); | |
47 } | |
48 | |
49 bool ChromeWebContentsViewWinDelegate::Focus() { | |
50 TabContentsWrapper* wrapper = | |
51 TabContentsWrapper::GetCurrentWrapperForContents(web_contents_); | |
52 if (wrapper) { | |
53 views::Widget* sad_tab = wrapper->sad_tab_helper()->sad_tab(); | |
54 if (sad_tab) { | |
55 sad_tab->GetContentsView()->RequestFocus(); | |
56 return true; | |
57 } | |
58 | |
59 // TODO(erg): TabContents used to own constrained windows, which is why | |
60 // this is here. Eventually this should be ported to a containing view | |
61 // specializing in constrained window management. | |
62 ConstrainedWindowTabHelper* helper = | |
63 wrapper->constrained_window_tab_helper(); | |
64 if (helper->constrained_window_count() > 0) { | |
65 ConstrainedWindow* window = *helper->constrained_window_begin(); | |
66 DCHECK(window); | |
67 window->FocusConstrainedWindow(); | |
68 return true; | |
69 } | |
70 } | |
71 return false; | |
72 } | |
73 | |
74 void ChromeWebContentsViewWinDelegate::TakeFocus(bool reverse) { | |
75 GetFocusManager()->AdvanceFocus(reverse); | |
76 } | |
77 | |
78 void ChromeWebContentsViewWinDelegate::StoreFocus() { | |
79 views::ViewStorage* view_storage = views::ViewStorage::GetInstance(); | |
80 | |
81 if (view_storage->RetrieveView(last_focused_view_storage_id_) != NULL) | |
82 view_storage->RemoveView(last_focused_view_storage_id_); | |
83 | |
84 views::View* focused_view = GetFocusManager()->GetFocusedView(); | |
85 if (focused_view) | |
86 view_storage->StoreView(last_focused_view_storage_id_, focused_view); | |
87 } | |
88 | |
89 void ChromeWebContentsViewWinDelegate::RestoreFocus() { | |
90 views::ViewStorage* view_storage = views::ViewStorage::GetInstance(); | |
91 views::View* last_focused_view = | |
92 view_storage->RetrieveView(last_focused_view_storage_id_); | |
93 | |
94 if (!last_focused_view) { | |
95 SetInitialFocus(); | |
96 } else { | |
97 if (last_focused_view->IsFocusable() && | |
98 GetFocusManager()->ContainsView(last_focused_view)) { | |
99 last_focused_view->RequestFocus(); | |
100 } else { | |
101 // The focused view may not belong to the same window hierarchy (e.g. | |
102 // if the location bar was focused and the tab is dragged out), or it may | |
103 // no longer be focusable (e.g. if the location bar was focused and then | |
104 // we switched to fullscreen mode). In that case we default to the | |
105 // default focus. | |
106 SetInitialFocus(); | |
107 } | |
108 view_storage->RemoveView(last_focused_view_storage_id_); | |
109 } | |
110 } | |
111 | |
112 void ChromeWebContentsViewWinDelegate::ShowContextMenu( | |
113 const content::ContextMenuParams& params) { | |
114 context_menu_.reset(new RenderViewContextMenuViews(web_contents_, params)); | |
115 context_menu_->Init(); | |
116 | |
117 // Don't show empty menus. | |
118 if (context_menu_->menu_model().GetItemCount() == 0) | |
119 return; | |
120 | |
121 gfx::Point screen_point(params.x, params.y); | |
122 | |
123 POINT temp = screen_point.ToPOINT(); | |
124 ClientToScreen(web_contents_->GetView()->GetNativeView(), &temp); | |
125 screen_point = temp; | |
126 | |
127 // Enable recursive tasks on the message loop so we can get updates while | |
128 // the context menu is being displayed. | |
129 MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); | |
130 context_menu_->RunMenuAt(GetTopLevelWidget(), screen_point); | |
131 } | |
132 | |
133 void ChromeWebContentsViewWinDelegate::SizeChanged(const gfx::Size& size) { | |
134 TabContentsWrapper* wrapper = | |
135 TabContentsWrapper::GetCurrentWrapperForContents(web_contents_); | |
136 if (!wrapper) | |
137 return; | |
138 views::Widget* sad_tab = wrapper->sad_tab_helper()->sad_tab(); | |
139 if (sad_tab) | |
140 sad_tab->SetBounds(gfx::Rect(size)); | |
141 } | |
142 | |
143 views::Widget* ChromeWebContentsViewWinDelegate::GetTopLevelWidget() { | |
144 HWND top_level_window = web_contents_->GetView()->GetTopLevelNativeWindow(); | |
145 if (!top_level_window) | |
146 return NULL; | |
147 return views::Widget::GetWidgetForNativeWindow(top_level_window); | |
148 } | |
149 | |
150 views::FocusManager* | |
151 ChromeWebContentsViewWinDelegate::GetFocusManager() { | |
152 views::Widget* toplevel_widget = GetTopLevelWidget(); | |
153 return toplevel_widget ? toplevel_widget->GetFocusManager() : NULL; | |
154 } | |
155 | |
156 void ChromeWebContentsViewWinDelegate::SetInitialFocus() { | |
157 if (web_contents_->FocusLocationBarByDefault()) { | |
158 web_contents_->SetFocusToLocationBar(false); | |
159 } else { | |
160 web_contents_->GetView()->Focus(); | |
161 } | |
162 } | |
OLD | NEW |