4ef75bd91459738de654c8e807abfd4a2ccb5c94
[platform/framework/web/crosswalk.git] / src / content / browser / renderer_host / input / touch_selection_controller.h
1 // Copyright 2014 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 #ifndef CONTENT_BROWSER_RENDERER_HOST_INPUT_TOUCH_SELECTION_CONTROLLER_H_
6 #define CONTENT_BROWSER_RENDERER_HOST_INPUT_TOUCH_SELECTION_CONTROLLER_H_
7
8 #include "content/browser/renderer_host/input/selection_event_type.h"
9 #include "content/browser/renderer_host/input/touch_handle.h"
10 #include "content/common/content_export.h"
11 #include "ui/gfx/geometry/point_f.h"
12 #include "ui/gfx/geometry/rect_f.h"
13
14 namespace blink {
15 class WebInputEvent;
16 }
17
18 namespace ui {
19 class MotionEvent;
20 }
21
22 namespace content {
23
24 // Interface through which |TouchSelectionController| issues selection-related
25 // commands, notifications and requests.
26 class CONTENT_EXPORT TouchSelectionControllerClient {
27  public:
28   virtual ~TouchSelectionControllerClient() {}
29
30   virtual bool SupportsAnimation() const = 0;
31   virtual void SetNeedsAnimate() = 0;
32   virtual void MoveCaret(const gfx::PointF& position) = 0;
33   virtual void SelectBetweenCoordinates(const gfx::PointF& start,
34                                         const gfx::PointF& end) = 0;
35   virtual void OnSelectionEvent(SelectionEventType event,
36                                 const gfx::PointF& position) = 0;
37   virtual scoped_ptr<TouchHandleDrawable> CreateDrawable() = 0;
38 };
39
40 // Controller for manipulating text selection via touch input.
41 class CONTENT_EXPORT TouchSelectionController : public TouchHandleClient {
42  public:
43   explicit TouchSelectionController(TouchSelectionControllerClient* client);
44   virtual ~TouchSelectionController();
45
46   // To be called when the selection bounds have changed.
47   // Note that such updates will trigger handle updates only if preceded
48   // by an appropriate call to allow automatic showing.
49   void OnSelectionBoundsChanged(const gfx::RectF& start_rect,
50                                 TouchHandleOrientation start_orientation,
51                                 bool start_visible,
52                                 const gfx::RectF& end_rect,
53                                 TouchHandleOrientation end_orientation,
54                                 bool end_visible);
55
56   // Allows touch-dragging of the handle.
57   // Returns true iff the event was consumed, in which case the caller should
58   // cease further handling of the event.
59   bool WillHandleTouchEvent(const ui::MotionEvent& event);
60
61   // To be called before forwarding a tap event. This allows automatically
62   // showing the insertion handle from subsequent bounds changes.
63   void OnTapEvent();
64
65   // To be called before forwarding a longpress event. This allows automatically
66   // showing the selection or insertion handles from subsequent bounds changes.
67   void OnLongPressEvent();
68
69   // Hide the handles and suppress bounds updates until the next explicit
70   // showing allowance.
71   void HideAndDisallowShowingAutomatically();
72
73   // Override the handle visibility according to |hidden|.
74   void SetTemporarilyHidden(bool hidden);
75
76   // To be called when the editability of the focused region changes.
77   void OnSelectionEditable(bool editable);
78
79   // To be called when the contents of the focused region changes.
80   void OnSelectionEmpty(bool empty);
81
82   // Ticks an active animation, as requested to the client by |SetNeedsAnimate|.
83   // Returns true if an animation is active and requires further ticking.
84   bool Animate(base::TimeTicks animate_time);
85
86  private:
87   enum InputEventType { TAP, LONG_PRESS, INPUT_EVENT_TYPE_NONE };
88
89   // TouchHandleClient implementation.
90   virtual void OnHandleDragBegin(const TouchHandle& handle) OVERRIDE;
91   virtual void OnHandleDragUpdate(const TouchHandle& handle,
92                                   const gfx::PointF& new_position) OVERRIDE;
93   virtual void OnHandleDragEnd(const TouchHandle& handle) OVERRIDE;
94   virtual void OnHandleTapped(const TouchHandle& handle) OVERRIDE;
95   virtual void SetNeedsAnimate() OVERRIDE;
96   virtual scoped_ptr<TouchHandleDrawable> CreateDrawable() OVERRIDE;
97
98   void ShowInsertionHandleAutomatically();
99   void ShowSelectionHandlesAutomatically();
100
101   void OnInsertionChanged();
102   void OnSelectionChanged();
103
104   void ActivateInsertion();
105   void DeactivateInsertion();
106   void ActivateSelection();
107   void DeactivateSelection();
108   void ResetCachedValuesIfInactive();
109
110   gfx::PointF GetStartPosition() const;
111   gfx::PointF GetEndPosition() const;
112   float GetStartLineHeight() const;
113   float GetEndLineHeight() const;
114   bool GetStartVisible() const;
115   bool GetEndVisible() const;
116   TouchHandle::AnimationStyle GetAnimationStyle(bool was_active) const;
117
118   TouchSelectionControllerClient* const client_;
119
120   InputEventType last_input_event_type_;
121
122   gfx::RectF start_rect_;
123   TouchHandleOrientation start_orientation_;
124   bool start_visible_;
125   gfx::RectF end_rect_;
126   TouchHandleOrientation end_orientation_;
127   bool end_visible_;
128
129   scoped_ptr<TouchHandle> insertion_handle_;
130   bool is_insertion_active_;
131   bool activate_insertion_automatically_;
132
133   scoped_ptr<TouchHandle> start_selection_handle_;
134   scoped_ptr<TouchHandle> end_selection_handle_;
135   gfx::PointF fixed_handle_position_;
136   bool is_selection_active_;
137   bool activate_selection_automatically_;
138
139   bool selection_empty_;
140   bool selection_editable_;
141
142   bool temporarily_hidden_;
143
144   DISALLOW_COPY_AND_ASSIGN(TouchSelectionController);
145 };
146
147 }  // namespace content
148
149 #endif  // CONTENT_BROWSER_RENDERER_HOST_INPUT_TOUCH_SELECTION_CONTROLLER_H_