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