Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / ui / aura / root_window.h
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 #ifndef UI_AURA_ROOT_WINDOW_H_
6 #define UI_AURA_ROOT_WINDOW_H_
7
8 #include <vector>
9
10 #include "base/basictypes.h"
11 #include "base/gtest_prod_util.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/message_loop/message_loop.h"
16 #include "ui/aura/aura_export.h"
17 #include "ui/aura/client/capture_delegate.h"
18 #include "ui/aura/window_tree_host.h"
19 #include "ui/aura/window_tree_host_delegate.h"
20 #include "ui/base/cursor/cursor.h"
21 #include "ui/compositor/compositor.h"
22 #include "ui/compositor/layer_animation_observer.h"
23 #include "ui/events/event_constants.h"
24 #include "ui/events/event_processor.h"
25 #include "ui/events/event_targeter.h"
26 #include "ui/events/gestures/gesture_recognizer.h"
27 #include "ui/events/gestures/gesture_types.h"
28 #include "ui/gfx/native_widget_types.h"
29 #include "ui/gfx/point.h"
30
31 namespace gfx {
32 class Size;
33 class Transform;
34 }
35
36 namespace ui {
37 class GestureEvent;
38 class GestureRecognizer;
39 class KeyEvent;
40 class LayerAnimationSequence;
41 class MouseEvent;
42 class ScrollEvent;
43 class TouchEvent;
44 class ViewProp;
45 }
46
47 namespace aura {
48 class RootWindow;
49 class WindowTreeHost;
50 class RootWindowObserver;
51 class TestScreen;
52 class WindowTargeter;
53
54 // RootWindow is responsible for hosting a set of windows.
55 class AURA_EXPORT RootWindow : public ui::EventProcessor,
56                                public ui::GestureEventHelper,
57                                public ui::LayerAnimationObserver,
58                                public client::CaptureDelegate,
59                                public WindowTreeHostDelegate {
60  public:
61   struct AURA_EXPORT CreateParams {
62     // CreateParams with initial_bounds and default host in pixel.
63     explicit CreateParams(const gfx::Rect& initial_bounds);
64     ~CreateParams() {}
65
66     gfx::Rect initial_bounds;
67
68     // A host to use in place of the default one that RootWindow will create.
69     // NULL by default.
70     WindowTreeHost* host;
71   };
72
73   explicit RootWindow(const CreateParams& params);
74   virtual ~RootWindow();
75
76   // Returns the WindowTreeHost for the specified accelerated widget, or NULL
77   // if there is none associated.
78   static RootWindow* GetForAcceleratedWidget(gfx::AcceleratedWidget widget);
79
80   Window* window() {
81     return const_cast<Window*>(const_cast<const RootWindow*>(this)->window());
82   }
83   const Window* window() const { return window_.get(); }
84   WindowTreeHost* host() {
85     return const_cast<WindowTreeHost*>(
86         const_cast<const RootWindow*>(this)->host());
87   }
88   const WindowTreeHost* host() const { return host_.get(); }
89   Window* mouse_pressed_handler() { return mouse_pressed_handler_; }
90   Window* mouse_moved_handler() { return mouse_moved_handler_; }
91
92   // Stop listening events in preparation for shutdown.
93   void PrepareForShutdown();
94
95   // Repost event for re-processing. Used when exiting context menus.
96   // We only support the ET_MOUSE_PRESSED and ET_GESTURE_TAP_DOWN event
97   // types (although the latter is currently a no-op).
98   void RepostEvent(const ui::LocatedEvent& event);
99
100   WindowTreeHostDelegate* AsWindowTreeHostDelegate();
101
102   // Invoked when the mouse events get enabled or disabled.
103   void OnMouseEventsEnableStateChanged(bool enabled);
104
105   // Returns a target window for the given gesture event.
106   Window* GetGestureTarget(ui::GestureEvent* event);
107
108   // Handles a gesture event. Returns true if handled. Unlike the other
109   // event-dispatching function (e.g. for touch/mouse/keyboard events), gesture
110   // events are dispatched from GestureRecognizer instead of WindowTreeHost.
111   void DispatchGestureEvent(ui::GestureEvent* event);
112
113   // Invoked when |window| is being destroyed.
114   void OnWindowDestroying(Window* window);
115
116   // Invoked when |window|'s bounds have changed. |contained_mouse| indicates if
117   // the bounds before change contained the |last_moust_location()|.
118   void OnWindowBoundsChanged(Window* window, bool contained_mouse);
119
120   // Dispatches OnMouseExited to the |window| which is hiding if nessessary.
121   void DispatchMouseExitToHidingWindow(Window* window);
122
123   // Dispatches a ui::ET_MOUSE_EXITED event at |point|.
124   void DispatchMouseExitAtPoint(const gfx::Point& point);
125
126   // Invoked when |window|'s visibility has changed.
127   void OnWindowVisibilityChanged(Window* window, bool is_visible);
128
129   // Invoked when |window|'s tranfrom has changed. |contained_mouse|
130   // indicates if the bounds before change contained the
131   // |last_moust_location()|.
132   void OnWindowTransformed(Window* window, bool contained_mouse);
133
134   // Invoked when the keyboard mapping (in X11 terms: the mapping between
135   // keycodes and keysyms) has changed.
136   void OnKeyboardMappingChanged();
137
138   // The system windowing system has sent a request that we close our window.
139   void OnWindowTreeHostCloseRequested();
140
141   // Add/remove observer. There is no need to remove the observer if
142   // the root window is being deleted. In particular, you SHOULD NOT remove
143   // in |WindowObserver::OnWindowDestroying| of the observer observing
144   // the root window because it is too late to remove it.
145   void AddRootWindowObserver(RootWindowObserver* observer);
146   void RemoveRootWindowObserver(RootWindowObserver* observer);
147
148   // Gesture Recognition -------------------------------------------------------
149
150   // When a touch event is dispatched to a Window, it may want to process the
151   // touch event asynchronously. In such cases, the window should consume the
152   // event during the event dispatch. Once the event is properly processed, the
153   // window should let the RootWindow know about the result of the event
154   // processing, so that gesture events can be properly created and dispatched.
155   void ProcessedTouchEvent(ui::TouchEvent* event,
156                            Window* window,
157                            ui::EventResult result);
158
159   // These methods are used to defer the processing of mouse/touch events
160   // related to resize. A client (typically a RenderWidgetHostViewAura) can call
161   // HoldPointerMoves when an resize is initiated and then ReleasePointerMoves
162   // once the resize is completed.
163   //
164   // More than one hold can be invoked and each hold must be cancelled by a
165   // release before we resume normal operation.
166   void HoldPointerMoves();
167   void ReleasePointerMoves();
168
169   // Gets the last location seen in a mouse event in this root window's
170   // coordinates. This may return a point outside the root window's bounds.
171   gfx::Point GetLastMouseLocationInRoot() const;
172
173  private:
174   FRIEND_TEST_ALL_PREFIXES(RootWindowTest, KeepTranslatedEventInRoot);
175
176   friend class Window;
177   friend class TestScreen;
178
179   // The parameter for OnWindowHidden() to specify why window is hidden.
180   enum WindowHiddenReason {
181     WINDOW_DESTROYED,  // Window is destroyed.
182     WINDOW_HIDDEN,     // Window is hidden.
183     WINDOW_MOVING,     // Window is temporarily marked as hidden due to move
184                        // across root windows.
185   };
186
187   // Updates the event with the appropriate transform for the device scale
188   // factor. The WindowTreeHostDelegate dispatches events in the physical pixel
189   // coordinate. But the event processing from RootWindow onwards happen in
190   // device-independent pixel coordinate. So it is necessary to update the event
191   // received from the host.
192   void TransformEventForDeviceScaleFactor(ui::LocatedEvent* event);
193
194   // Dispatches the specified event type (intended for enter/exit) to the
195   // |mouse_moved_handler_|.
196   ui::EventDispatchDetails DispatchMouseEnterOrExit(
197       const ui::MouseEvent& event,
198       ui::EventType type) WARN_UNUSED_RESULT;
199   ui::EventDispatchDetails ProcessGestures(
200       ui::GestureRecognizer::Gestures* gestures) WARN_UNUSED_RESULT;
201
202   // Called when a Window is attached or detached from the RootWindow.
203   void OnWindowAddedToRootWindow(Window* window);
204   void OnWindowRemovedFromRootWindow(Window* window, Window* new_root);
205
206   // Called when a window becomes invisible, either by being removed
207   // from root window hierarchy, via SetVisible(false) or being destroyed.
208   // |reason| specifies what triggered the hiding. Note that becoming invisible
209   // will cause a window to lose capture and some windows may destroy themselves
210   // on capture (like DragDropTracker).
211   void OnWindowHidden(Window* invisible, WindowHiddenReason reason);
212
213   // Cleans up the state of gestures for all windows in |window| (including
214   // |window| itself). This includes cancelling active touch points.
215   void CleanupGestureState(Window* window);
216
217   // Overridden from aura::client::CaptureDelegate:
218   virtual void UpdateCapture(Window* old_capture, Window* new_capture) OVERRIDE;
219   virtual void OnOtherRootGotCapture() OVERRIDE;
220   virtual void SetNativeCapture() OVERRIDE;
221   virtual void ReleaseNativeCapture() OVERRIDE;
222
223   // Overridden from ui::EventProcessor:
224   virtual ui::EventTarget* GetRootTarget() OVERRIDE;
225   virtual void PrepareEventForDispatch(ui::Event* event) OVERRIDE;
226
227   // Overridden from ui::EventDispatcherDelegate.
228   virtual bool CanDispatchToTarget(ui::EventTarget* target) OVERRIDE;
229   virtual ui::EventDispatchDetails PreDispatchEvent(ui::EventTarget* target,
230                                                     ui::Event* event) OVERRIDE;
231   virtual ui::EventDispatchDetails PostDispatchEvent(
232       ui::EventTarget* target, const ui::Event& event) OVERRIDE;
233
234   // Overridden from ui::GestureEventHelper.
235   virtual bool CanDispatchToConsumer(ui::GestureConsumer* consumer) OVERRIDE;
236   virtual void DispatchPostponedGestureEvent(ui::GestureEvent* event) OVERRIDE;
237   virtual void DispatchCancelTouchEvent(ui::TouchEvent* event) OVERRIDE;
238
239   // Overridden from ui::LayerAnimationObserver:
240   virtual void OnLayerAnimationEnded(
241       ui::LayerAnimationSequence* animation) OVERRIDE;
242   virtual void OnLayerAnimationScheduled(
243       ui::LayerAnimationSequence* animation) OVERRIDE;
244   virtual void OnLayerAnimationAborted(
245       ui::LayerAnimationSequence* animation) OVERRIDE;
246
247   // Overridden from aura::WindowTreeHostDelegate:
248   virtual void OnHostCancelMode() OVERRIDE;
249   virtual void OnHostActivated() OVERRIDE;
250   virtual void OnHostLostWindowCapture() OVERRIDE;
251   virtual void OnHostLostMouseGrab() OVERRIDE;
252   virtual void OnHostMoved(const gfx::Point& origin) OVERRIDE;
253   virtual void OnHostResized(const gfx::Size& size) OVERRIDE;
254   virtual void OnCursorMovedToRootLocation(
255       const gfx::Point& root_location) OVERRIDE;
256   virtual RootWindow* AsRootWindow() OVERRIDE;
257   virtual const RootWindow* AsRootWindow() const OVERRIDE;
258   virtual ui::EventProcessor* GetEventProcessor() OVERRIDE;
259
260   // We hold and aggregate mouse drags and touch moves as a way of throttling
261   // resizes when HoldMouseMoves() is called. The following methods are used to
262   // dispatch held and newly incoming mouse and touch events, typically when an
263   // event other than one of these needs dispatching or a matching
264   // ReleaseMouseMoves()/ReleaseTouchMoves() is called.  NOTE: because these
265   // methods dispatch events from WindowTreeHost the coordinates are in terms of
266   // the root.
267
268   ui::EventDispatchDetails DispatchHeldEvents() WARN_UNUSED_RESULT;
269   // Creates and dispatches synthesized mouse move event using the
270   // current mouse location.
271   ui::EventDispatchDetails SynthesizeMouseMoveEvent() WARN_UNUSED_RESULT;
272
273   void SynthesizeMouseMoveEventAsync();
274
275   // Posts a task to send synthesized mouse move event if there
276   // is no a pending task.
277   void PostMouseMoveEventAfterWindowChange();
278
279   void PreDispatchLocatedEvent(Window* target, ui::LocatedEvent* event);
280   void PreDispatchMouseEvent(Window* target, ui::MouseEvent* event);
281   void PreDispatchTouchEvent(Window* target, ui::TouchEvent* event);
282
283   // TODO(beng): evaluate the ideal ownership model.
284   scoped_ptr<Window> window_;
285
286   scoped_ptr<WindowTreeHost> host_;
287
288   // Touch ids that are currently down.
289   uint32 touch_ids_down_;
290
291   ObserverList<RootWindowObserver> observers_;
292
293   Window* mouse_pressed_handler_;
294   Window* mouse_moved_handler_;
295   Window* event_dispatch_target_;
296   Window* old_dispatch_target_;
297
298   bool synthesize_mouse_move_;
299
300   // How many move holds are outstanding. We try to defer dispatching
301   // touch/mouse moves while the count is > 0.
302   int move_hold_count_;
303   // The location of |held_move_event_| is in |window_|'s coordinate.
304   scoped_ptr<ui::LocatedEvent> held_move_event_;
305
306   // Allowing for reposting of events. Used when exiting context menus.
307   scoped_ptr<ui::LocatedEvent> held_repostable_event_;
308
309   // Set when dispatching a held event.
310   bool dispatching_held_event_;
311
312   scoped_ptr<ui::ViewProp> prop_;
313
314   // Used to schedule reposting an event.
315   base::WeakPtrFactory<RootWindow> repost_event_factory_;
316
317   // Used to schedule DispatchHeldEvents() when |move_hold_count_| goes to 0.
318   base::WeakPtrFactory<RootWindow> held_event_factory_;
319
320   DISALLOW_COPY_AND_ASSIGN(RootWindow);
321 };
322
323 }  // namespace aura
324
325 #endif  // UI_AURA_ROOT_WINDOW_H_