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_VIEWS_WIDGET_ROOT_VIEW_H_
6#define UI_VIEWS_WIDGET_ROOT_VIEW_H_
7
8#include <string>
9
10#include "base/memory/ref_counted.h"
11#include "ui/events/event_processor.h"
12#include "ui/views/focus/focus_manager.h"
13#include "ui/views/focus/focus_search.h"
14#include "ui/views/view.h"
15
16namespace views {
17
18namespace test {
19class WidgetTest;
20}
21
22class Widget;
23
24// This is a views-internal API and should not be used externally.
25// Widget exposes this object as a View*.
26namespace internal {
27class PreEventDispatchHandler;
28
29////////////////////////////////////////////////////////////////////////////////
30// RootView class
31//
32//  The RootView is the root of a View hierarchy. A RootView is attached to a
33//  Widget. The Widget is responsible for receiving events from the host
34//  environment, converting them to views-compatible events and then forwarding
35//  them to the RootView for propagation into the View hierarchy.
36//
37//  A RootView can have only one child, called its "Contents View" which is
38//  sized to fill the bounds of the RootView (and hence the client area of the
39//  Widget). Call SetContentsView() after the associated Widget has been
40//  initialized to attach the contents view to the RootView.
41//  TODO(beng): Enforce no other callers to AddChildView/tree functions by
42//              overriding those methods as private here.
43//  TODO(beng): Clean up API further, make Widget a friend.
44//  TODO(sky): We don't really want to export this class.
45//
46class VIEWS_EXPORT RootView : public View,
47                              public FocusTraversable,
48                              public ui::EventProcessor {
49 public:
50  static const char kViewClassName[];
51
52  // Creation and lifetime -----------------------------------------------------
53  explicit RootView(Widget* widget);
54  virtual ~RootView();
55
56  // Tree operations -----------------------------------------------------------
57
58  // Sets the "contents view" of the RootView. This is the single child view
59  // that is responsible for laying out the contents of the widget.
60  void SetContentsView(View* contents_view);
61  View* GetContentsView();
62
63  // Called when parent of the host changed.
64  void NotifyNativeViewHierarchyChanged();
65
66  // Focus ---------------------------------------------------------------------
67
68  // Used to set the FocusTraversable parent after the view has been created
69  // (typically when the hierarchy changes and this RootView is added/removed).
70  virtual void SetFocusTraversableParent(FocusTraversable* focus_traversable);
71
72  // Used to set the View parent after the view has been created.
73  virtual void SetFocusTraversableParentView(View* view);
74
75  // System events -------------------------------------------------------------
76
77  // Public API for broadcasting theme change notifications to this View
78  // hierarchy.
79  void ThemeChanged();
80
81  // Public API for broadcasting locale change notifications to this View
82  // hierarchy.
83  void LocaleChanged();
84
85  // Overridden from FocusTraversable:
86  virtual FocusSearch* GetFocusSearch() OVERRIDE;
87  virtual FocusTraversable* GetFocusTraversableParent() OVERRIDE;
88  virtual View* GetFocusTraversableParentView() OVERRIDE;
89
90  // Overridden from ui::EventProcessor:
91  virtual ui::EventTarget* GetRootTarget() OVERRIDE;
92  virtual ui::EventDispatchDetails OnEventFromSource(ui::Event* event) OVERRIDE;
93
94  // Overridden from View:
95  virtual const Widget* GetWidget() const OVERRIDE;
96  virtual Widget* GetWidget() OVERRIDE;
97  virtual bool IsDrawn() const OVERRIDE;
98  virtual void Layout() OVERRIDE;
99  virtual const char* GetClassName() const OVERRIDE;
100  virtual void SchedulePaintInRect(const gfx::Rect& rect) OVERRIDE;
101  virtual bool OnMousePressed(const ui::MouseEvent& event) OVERRIDE;
102  virtual bool OnMouseDragged(const ui::MouseEvent& event) OVERRIDE;
103  virtual void OnMouseReleased(const ui::MouseEvent& event) OVERRIDE;
104  virtual void OnMouseCaptureLost() OVERRIDE;
105  virtual void OnMouseMoved(const ui::MouseEvent& event) OVERRIDE;
106  virtual void OnMouseExited(const ui::MouseEvent& event) OVERRIDE;
107  virtual bool OnMouseWheel(const ui::MouseWheelEvent& event) OVERRIDE;
108  virtual void SetMouseHandler(View* new_mouse_handler) OVERRIDE;
109  virtual void GetAccessibleState(ui::AXViewState* state) OVERRIDE;
110  virtual void UpdateParentLayer() OVERRIDE;
111
112 protected:
113  // TODO(tdanderson): Remove RootView::DispatchGestureEvent() once
114  //                   its targeting and dispatch logic has been moved
115  //                   elsewhere. See crbug.com/348083.
116  virtual void DispatchGestureEvent(ui::GestureEvent* event);
117
118  // Overridden from View:
119  virtual void ViewHierarchyChanged(
120      const ViewHierarchyChangedDetails& details) OVERRIDE;
121  virtual void VisibilityChanged(View* starting_from, bool is_visible) OVERRIDE;
122  virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
123  virtual gfx::Vector2d CalculateOffsetToAncestorWithLayer(
124      ui::Layer** layer_parent) OVERRIDE;
125  virtual View::DragInfo* GetDragInfo() OVERRIDE;
126
127 private:
128  friend class ::views::View;
129  friend class ::views::Widget;
130  friend class ::views::test::WidgetTest;
131
132  // Input ---------------------------------------------------------------------
133
134  // Update the cursor given a mouse event. This is called by non mouse_move
135  // event handlers to honor the cursor desired by views located under the
136  // cursor during drag operations. The location of the mouse should be in the
137  // current coordinate system (i.e. any necessary transformation should be
138  // applied to the point prior to calling this).
139  void UpdateCursor(const ui::MouseEvent& event);
140
141  // Updates the last_mouse_* fields from e. The location of the mouse should be
142  // in the current coordinate system (i.e. any necessary transformation should
143  // be applied to the point prior to calling this).
144  void SetMouseLocationAndFlags(const ui::MouseEvent& event);
145
146  // |view| is the view receiving |event|. This function sends the event to all
147  // the Views up the hierarchy that has |notify_enter_exit_on_child_| flag
148  // turned on, but does not contain |sibling|.
149  void NotifyEnterExitOfDescendant(const ui::MouseEvent& event,
150                                   ui::EventType type,
151                                   View* view,
152                                   View* sibling);
153
154  // Overridden from ui::EventDispatcherDelegate:
155  virtual bool CanDispatchToTarget(ui::EventTarget* target) OVERRIDE;
156  virtual ui::EventDispatchDetails PreDispatchEvent(ui::EventTarget* target,
157                                                    ui::Event* event) OVERRIDE;
158  virtual ui::EventDispatchDetails PostDispatchEvent(
159      ui::EventTarget* target, const ui::Event& event) OVERRIDE;
160
161  //////////////////////////////////////////////////////////////////////////////
162  // Tree operations -----------------------------------------------------------
163
164  // The host Widget
165  Widget* widget_;
166
167  // Input ---------------------------------------------------------------------
168
169  // The view currently handing down - drag - up
170  View* mouse_pressed_handler_;
171
172  // The view currently handling enter / exit
173  View* mouse_move_handler_;
174
175  // The last view to handle a mouse click, so that we can determine if
176  // a double-click lands on the same view as its single-click part.
177  View* last_click_handler_;
178
179  // true if mouse_pressed_handler_ has been explicitly set
180  bool explicit_mouse_handler_;
181
182  // Last position/flag of a mouse press/drag. Used if capture stops and we need
183  // to synthesize a release.
184  int last_mouse_event_flags_;
185  int last_mouse_event_x_;
186  int last_mouse_event_y_;
187
188  // The view currently handling gesture events. When set, this handler receives
189  // all gesture events, except when there is an event handler for the specific
190  // gesture (e.g. scroll).
191  View* gesture_handler_;
192
193  // The view currently handling scroll gesture events.
194  View* scroll_gesture_handler_;
195
196  scoped_ptr<internal::PreEventDispatchHandler> pre_dispatch_handler_;
197  scoped_ptr<internal::PostEventDispatchHandler> post_dispatch_handler_;
198
199  // Focus ---------------------------------------------------------------------
200
201  // The focus search algorithm.
202  FocusSearch focus_search_;
203
204  // Whether this root view belongs to the current active window.
205  // bool activated_;
206
207  // The parent FocusTraversable, used for focus traversal.
208  FocusTraversable* focus_traversable_parent_;
209
210  // The View that contains this RootView. This is used when we have RootView
211  // wrapped inside native components, and is used for the focus traversal.
212  View* focus_traversable_parent_view_;
213
214  View* event_dispatch_target_;
215  View* old_dispatch_target_;
216
217  // Drag and drop -------------------------------------------------------------
218
219  // Tracks drag state for a view.
220  View::DragInfo drag_info_;
221
222  DISALLOW_IMPLICIT_CONSTRUCTORS(RootView);
223};
224
225}  // namespace internal
226}  // namespace views
227
228#endif  // UI_VIEWS_WIDGET_ROOT_VIEW_H_
229