Change next_proto member type.
[chromium-blink-merge.git] / content / browser / web_contents / touch_editable_impl_aura.cc
blob1fabce67c7aa91b0f12f5e1b209a784a0524495b
1 // Copyright (c) 2013 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.
5 #include "content/browser/web_contents/touch_editable_impl_aura.h"
7 #include "content/browser/renderer_host/render_widget_host_impl.h"
8 #include "content/browser/renderer_host/render_widget_host_view_aura.h"
9 #include "content/browser/web_contents/web_contents_impl.h"
10 #include "content/common/view_messages.h"
11 #include "content/public/browser/render_view_host.h"
12 #include "content/public/browser/render_widget_host.h"
13 #include "ui/aura/client/screen_position_client.h"
14 #include "ui/aura/window.h"
15 #include "ui/aura/window_tree_host.h"
16 #include "ui/base/clipboard/clipboard.h"
17 #include "ui/base/touch/selection_bound.h"
18 #include "ui/base/ui_base_switches_util.h"
19 #include "ui/gfx/range/range.h"
20 #include "ui/strings/grit/ui_strings.h"
21 #include "ui/wm/public/activation_client.h"
23 namespace content {
25 ////////////////////////////////////////////////////////////////////////////////
26 // TouchEditableImplAura, public:
28 TouchEditableImplAura::~TouchEditableImplAura() {
29 Cleanup();
32 // static
33 TouchEditableImplAura* TouchEditableImplAura::Create() {
34 if (switches::IsTouchEditingEnabled())
35 return new TouchEditableImplAura();
36 return NULL;
39 void TouchEditableImplAura::AttachToView(RenderWidgetHostViewAura* view) {
40 if (rwhva_ == view)
41 return;
43 Cleanup();
44 if (!view)
45 return;
47 rwhva_ = view;
48 rwhva_->set_touch_editing_client(this);
51 void TouchEditableImplAura::UpdateEditingController() {
52 if (!rwhva_ || !rwhva_->HasFocus())
53 return;
55 if (text_input_type_ != ui::TEXT_INPUT_TYPE_NONE ||
56 selection_anchor_ != selection_focus_) {
57 if (touch_selection_controller_)
58 touch_selection_controller_->SelectionChanged();
59 } else {
60 EndTouchEditing(false);
64 void TouchEditableImplAura::OverscrollStarted() {
65 scrolls_in_progress_++;
68 void TouchEditableImplAura::OverscrollCompleted() {
69 ScrollEnded();
72 ////////////////////////////////////////////////////////////////////////////////
73 // TouchEditableImplAura, RenderWidgetHostViewAura::TouchEditingClient
74 // implementation:
76 void TouchEditableImplAura::StartTouchEditing() {
77 if (!rwhva_ || !rwhva_->HasFocus())
78 return;
80 if (!touch_selection_controller_) {
81 touch_selection_controller_.reset(
82 ui::TouchSelectionController::create(this));
84 if (touch_selection_controller_)
85 touch_selection_controller_->SelectionChanged();
88 void TouchEditableImplAura::EndTouchEditing(bool quick) {
89 if (touch_selection_controller_) {
90 if (touch_selection_controller_->IsHandleDragInProgress()) {
91 touch_selection_controller_->SelectionChanged();
92 } else {
93 selection_gesture_in_process_ = false;
94 touch_selection_controller_->HideHandles(quick);
95 touch_selection_controller_.reset();
100 void TouchEditableImplAura::OnSelectionOrCursorChanged(
101 const ui::SelectionBound& anchor,
102 const ui::SelectionBound& focus) {
103 selection_anchor_ = anchor;
104 selection_focus_ = focus;
106 // If touch editing handles were not visible, we bring them up only if the
107 // current event is a gesture event, no scroll/fling/overscoll is in progress,
108 // and there is non-zero selection on the page
109 if (selection_gesture_in_process_ && !scrolls_in_progress_ &&
110 selection_anchor_ != selection_focus_) {
111 StartTouchEditing();
112 selection_gesture_in_process_ = false;
115 UpdateEditingController();
118 void TouchEditableImplAura::OnTextInputTypeChanged(ui::TextInputType type) {
119 text_input_type_ = type;
122 bool TouchEditableImplAura::HandleInputEvent(const ui::Event* event) {
123 DCHECK(rwhva_);
124 if (!event->IsGestureEvent()) {
125 // Ignore all non-gesture events. Non-gesture events that can deactivate
126 // touch editing are handled in TouchSelectionControllerImpl.
127 return false;
130 const ui::GestureEvent* gesture_event =
131 static_cast<const ui::GestureEvent*>(event);
132 switch (event->type()) {
133 case ui::ET_GESTURE_TAP:
134 // When the user taps, we want to show touch editing handles if user
135 // tapped on selected text.
136 if (gesture_event->details().tap_count() == 1 &&
137 selection_anchor_ != selection_focus_) {
138 gfx::Rect selection_rect =
139 ui::RectBetweenSelectionBounds(selection_anchor_, selection_focus_);
140 if (selection_rect.Contains(gesture_event->location())) {
141 StartTouchEditing();
142 return true;
145 // For single taps, not inside selected region, we want to show handles
146 // only when the tap is on an already focused textfield.
147 textfield_was_focused_on_tap_ =
148 gesture_event->details().tap_count() == 1 &&
149 text_input_type_ != ui::TEXT_INPUT_TYPE_NONE;
150 break;
151 case ui::ET_GESTURE_LONG_PRESS:
152 selection_gesture_in_process_ = true;
153 break;
154 case ui::ET_GESTURE_SCROLL_BEGIN:
155 scrolls_in_progress_++;
156 // We need to hide selection handles during scroll (including fling and
157 // overscroll), but they should be re-activated after scrolling if:
158 // - an existing scroll decided that handles should be shown after
159 // scrolling; or
160 // - the gesture in progress is going to end in selection; or
161 // - selection handles are currently active.
162 handles_hidden_due_to_scroll_ = handles_hidden_due_to_scroll_ ||
163 selection_gesture_in_process_ ||
164 touch_selection_controller_ != NULL;
165 selection_gesture_in_process_ = false;
166 EndTouchEditing(true);
167 break;
168 case ui::ET_GESTURE_SCROLL_END:
169 ScrollEnded();
170 break;
171 default:
172 break;
174 return false;
177 void TouchEditableImplAura::GestureEventAck(int gesture_event_type) {
178 DCHECK(rwhva_);
179 if (gesture_event_type == blink::WebInputEvent::GestureTap &&
180 text_input_type_ != ui::TEXT_INPUT_TYPE_NONE &&
181 textfield_was_focused_on_tap_) {
182 StartTouchEditing();
183 UpdateEditingController();
187 void TouchEditableImplAura::DidStopFlinging() {
188 ScrollEnded();
191 void TouchEditableImplAura::OnViewDestroyed() {
192 Cleanup();
195 ////////////////////////////////////////////////////////////////////////////////
196 // TouchEditableImplAura, ui::TouchEditable implementation:
198 void TouchEditableImplAura::SelectRect(const gfx::Point& start,
199 const gfx::Point& end) {
200 RenderWidgetHost* host = rwhva_->GetRenderWidgetHost();
201 RenderViewHost* rvh = RenderViewHost::From(host);
202 WebContentsImpl* wc =
203 static_cast<WebContentsImpl*>(WebContents::FromRenderViewHost(rvh));
204 wc->SelectRange(start, end);
207 void TouchEditableImplAura::MoveCaretTo(const gfx::Point& point) {
208 if (!rwhva_)
209 return;
211 RenderWidgetHostImpl* host = RenderWidgetHostImpl::From(
212 rwhva_->GetRenderWidgetHost());
213 host->MoveCaret(point);
216 void TouchEditableImplAura::GetSelectionEndPoints(ui::SelectionBound* anchor,
217 ui::SelectionBound* focus) {
218 *anchor = selection_anchor_;
219 *focus = selection_focus_;
222 gfx::Rect TouchEditableImplAura::GetBounds() {
223 return rwhva_ ? gfx::Rect(rwhva_->GetNativeView()->bounds().size()) :
224 gfx::Rect();
227 gfx::NativeView TouchEditableImplAura::GetNativeView() const {
228 return rwhva_ ? rwhva_->GetNativeView()->GetToplevelWindow() : NULL;
231 void TouchEditableImplAura::ConvertPointToScreen(gfx::Point* point) {
232 if (!rwhva_)
233 return;
234 aura::Window* window = rwhva_->GetNativeView();
235 aura::client::ScreenPositionClient* screen_position_client =
236 aura::client::GetScreenPositionClient(window->GetRootWindow());
237 if (screen_position_client)
238 screen_position_client->ConvertPointToScreen(window, point);
241 void TouchEditableImplAura::ConvertPointFromScreen(gfx::Point* point) {
242 if (!rwhva_)
243 return;
244 aura::Window* window = rwhva_->GetNativeView();
245 aura::client::ScreenPositionClient* screen_position_client =
246 aura::client::GetScreenPositionClient(window->GetRootWindow());
247 if (screen_position_client)
248 screen_position_client->ConvertPointFromScreen(window, point);
251 bool TouchEditableImplAura::DrawsHandles() {
252 return false;
255 void TouchEditableImplAura::OpenContextMenu(const gfx::Point& anchor) {
256 if (!rwhva_)
257 return;
258 gfx::Point point = anchor;
259 ConvertPointFromScreen(&point);
260 RenderWidgetHost* host = rwhva_->GetRenderWidgetHost();
261 host->Send(new ViewMsg_ShowContextMenu(
262 host->GetRoutingID(), ui::MENU_SOURCE_TOUCH_EDIT_MENU, point));
263 EndTouchEditing(false);
266 bool TouchEditableImplAura::IsCommandIdChecked(int command_id) const {
267 NOTREACHED();
268 return false;
271 bool TouchEditableImplAura::IsCommandIdEnabled(int command_id) const {
272 if (!rwhva_)
273 return false;
274 bool editable = rwhva_->GetTextInputType() != ui::TEXT_INPUT_TYPE_NONE;
275 bool readable = rwhva_->GetTextInputType() != ui::TEXT_INPUT_TYPE_PASSWORD;
276 gfx::Range selection_range;
277 rwhva_->GetSelectionRange(&selection_range);
278 bool has_selection = !selection_range.is_empty();
279 switch (command_id) {
280 case IDS_APP_CUT:
281 return editable && readable && has_selection;
282 case IDS_APP_COPY:
283 return readable && has_selection;
284 case IDS_APP_PASTE: {
285 base::string16 result;
286 ui::Clipboard::GetForCurrentThread()->ReadText(
287 ui::CLIPBOARD_TYPE_COPY_PASTE, &result);
288 return editable && !result.empty();
290 case IDS_APP_DELETE:
291 return editable && has_selection;
292 case IDS_APP_SELECT_ALL:
293 return true;
294 default:
295 return false;
299 bool TouchEditableImplAura::GetAcceleratorForCommandId(
300 int command_id,
301 ui::Accelerator* accelerator) {
302 return false;
305 void TouchEditableImplAura::ExecuteCommand(int command_id, int event_flags) {
306 RenderWidgetHost* host = rwhva_->GetRenderWidgetHost();
307 RenderViewHost* rvh = RenderViewHost::From(host);
308 WebContents* wc = WebContents::FromRenderViewHost(rvh);
310 switch (command_id) {
311 case IDS_APP_CUT:
312 wc->Cut();
313 break;
314 case IDS_APP_COPY:
315 wc->Copy();
316 break;
317 case IDS_APP_PASTE:
318 wc->Paste();
319 break;
320 case IDS_APP_DELETE:
321 wc->Delete();
322 break;
323 case IDS_APP_SELECT_ALL:
324 wc->SelectAll();
325 break;
326 default:
327 NOTREACHED();
328 break;
330 EndTouchEditing(false);
333 void TouchEditableImplAura::DestroyTouchSelection() {
334 EndTouchEditing(false);
337 ////////////////////////////////////////////////////////////////////////////////
338 // TouchEditableImplAura, private:
340 TouchEditableImplAura::TouchEditableImplAura()
341 : text_input_type_(ui::TEXT_INPUT_TYPE_NONE),
342 rwhva_(NULL),
343 selection_gesture_in_process_(false),
344 handles_hidden_due_to_scroll_(false),
345 scrolls_in_progress_(0),
346 textfield_was_focused_on_tap_(false) {
349 void TouchEditableImplAura::ScrollEnded() {
350 scrolls_in_progress_--;
351 // If there is no scrolling left in progress, show selection handles if they
352 // were hidden due to scroll and there is a selection.
353 if (!scrolls_in_progress_ && handles_hidden_due_to_scroll_ &&
354 (selection_anchor_ != selection_focus_ ||
355 text_input_type_ != ui::TEXT_INPUT_TYPE_NONE)) {
356 StartTouchEditing();
357 UpdateEditingController();
358 handles_hidden_due_to_scroll_ = false;
362 void TouchEditableImplAura::Cleanup() {
363 if (rwhva_) {
364 rwhva_->set_touch_editing_client(NULL);
365 rwhva_ = NULL;
367 text_input_type_ = ui::TEXT_INPUT_TYPE_NONE;
368 EndTouchEditing(true);
369 selection_gesture_in_process_ = false;
370 handles_hidden_due_to_scroll_ = false;
371 scrolls_in_progress_ = 0;
374 } // namespace content