Home | History | Annotate | Download | only in widget
      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_dispatcher.h"
     12 #include "ui/views/focus/focus_manager.h"
     13 #include "ui/views/focus/focus_search.h"
     14 #include "ui/views/view.h"
     15 
     16 namespace views {
     17 
     18 namespace test {
     19 class RootViewTestHelper;
     20 class WidgetTest;
     21 }
     22 
     23 class Widget;
     24 
     25 // This is a views-internal API and should not be used externally.
     26 // Widget exposes this object as a View*.
     27 namespace internal {
     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 //
     46 class VIEWS_EXPORT RootView : public View,
     47                               public FocusTraversable,
     48                               public ui::EventDispatcherDelegate {
     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   // Input ---------------------------------------------------------------------
     67 
     68   // Process a key event. Send the event to the focused view and up the focus
     69   // path, and finally to the default keyboard handler, until someone consumes
     70   // it. Returns whether anyone consumed the event.
     71   void DispatchKeyEvent(ui::KeyEvent* event);
     72   void DispatchScrollEvent(ui::ScrollEvent* event);
     73   void DispatchTouchEvent(ui::TouchEvent* event);
     74   virtual void DispatchGestureEvent(ui::GestureEvent* event);
     75 
     76   // Focus ---------------------------------------------------------------------
     77 
     78   // Used to set the FocusTraversable parent after the view has been created
     79   // (typically when the hierarchy changes and this RootView is added/removed).
     80   virtual void SetFocusTraversableParent(FocusTraversable* focus_traversable);
     81 
     82   // Used to set the View parent after the view has been created.
     83   virtual void SetFocusTraversableParentView(View* view);
     84 
     85   // System events -------------------------------------------------------------
     86 
     87   // Public API for broadcasting theme change notifications to this View
     88   // hierarchy.
     89   void ThemeChanged();
     90 
     91   // Public API for broadcasting locale change notifications to this View
     92   // hierarchy.
     93   void LocaleChanged();
     94 
     95   // Overridden from FocusTraversable:
     96   virtual FocusSearch* GetFocusSearch() OVERRIDE;
     97   virtual FocusTraversable* GetFocusTraversableParent() OVERRIDE;
     98   virtual View* GetFocusTraversableParentView() OVERRIDE;
     99 
    100   // Overridden from View:
    101   virtual const Widget* GetWidget() const OVERRIDE;
    102   virtual Widget* GetWidget() OVERRIDE;
    103   virtual bool IsDrawn() const OVERRIDE;
    104   virtual void Layout() OVERRIDE;
    105   virtual const char* GetClassName() const OVERRIDE;
    106   virtual void SchedulePaintInRect(const gfx::Rect& rect) OVERRIDE;
    107   virtual bool OnMousePressed(const ui::MouseEvent& event) OVERRIDE;
    108   virtual bool OnMouseDragged(const ui::MouseEvent& event) OVERRIDE;
    109   virtual void OnMouseReleased(const ui::MouseEvent& event) OVERRIDE;
    110   virtual void OnMouseCaptureLost() OVERRIDE;
    111   virtual void OnMouseMoved(const ui::MouseEvent& event) OVERRIDE;
    112   virtual void OnMouseExited(const ui::MouseEvent& event) OVERRIDE;
    113   virtual bool OnMouseWheel(const ui::MouseWheelEvent& event) OVERRIDE;
    114   virtual void SetMouseHandler(View* new_mouse_handler) OVERRIDE;
    115   virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE;
    116   virtual void UpdateParentLayer() OVERRIDE;
    117 
    118  protected:
    119   // Overridden from View:
    120   virtual void ViewHierarchyChanged(
    121       const ViewHierarchyChangedDetails& details) OVERRIDE;
    122   virtual void VisibilityChanged(View* starting_from, bool is_visible) OVERRIDE;
    123   virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
    124   virtual gfx::Vector2d CalculateOffsetToAncestorWithLayer(
    125       ui::Layer** layer_parent) OVERRIDE;
    126   virtual View::DragInfo* GetDragInfo() OVERRIDE;
    127 
    128  private:
    129   friend class ::views::View;
    130   friend class ::views::Widget;
    131   friend class ::views::test::RootViewTestHelper;
    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   void DispatchEventToTarget(View* target, ui::Event* event);
    149 
    150   // |view| is the view receiving |event|. This function sends the event to all
    151   // the Views up the hierarchy that has |notify_enter_exit_on_child_| flag
    152   // turned on, but does not contain |sibling|.
    153   void NotifyEnterExitOfDescendant(const ui::MouseEvent& event,
    154                                    ui::EventType type,
    155                                    View* view,
    156                                    View* sibling);
    157 
    158   // Dispatches the KeyEvent to |view| and ancestors until the event is
    159   // handled.
    160   void DispatchKeyEventStartAt(View* view, ui::KeyEvent* event);
    161 
    162   // Overridden from ui::EventDispatcherDelegate:
    163   virtual bool CanDispatchToTarget(ui::EventTarget* target) OVERRIDE;
    164 
    165   //////////////////////////////////////////////////////////////////////////////
    166 
    167   // Tree operations -----------------------------------------------------------
    168 
    169   // The host Widget
    170   Widget* widget_;
    171 
    172   // Input ---------------------------------------------------------------------
    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 touch events.
    194   View* touch_pressed_handler_;
    195 
    196   // The view currently handling gesture events. When set, this handler receives
    197   // all gesture events, except when there is an event handler for the specific
    198   // gesture (e.g. scroll).
    199   View* gesture_handler_;
    200 
    201   // The view currently handling scroll gesture events.
    202   View* scroll_gesture_handler_;
    203 
    204   // Focus ---------------------------------------------------------------------
    205 
    206   // The focus search algorithm.
    207   FocusSearch focus_search_;
    208 
    209   // Whether this root view belongs to the current active window.
    210   // bool activated_;
    211 
    212   // The parent FocusTraversable, used for focus traversal.
    213   FocusTraversable* focus_traversable_parent_;
    214 
    215   // The View that contains this RootView. This is used when we have RootView
    216   // wrapped inside native components, and is used for the focus traversal.
    217   View* focus_traversable_parent_view_;
    218 
    219   View* event_dispatch_target_;
    220 
    221   // Drag and drop -------------------------------------------------------------
    222 
    223   // Tracks drag state for a view.
    224   View::DragInfo drag_info_;
    225 
    226   DISALLOW_IMPLICIT_CONSTRUCTORS(RootView);
    227 };
    228 
    229 }  // namespace internal
    230 }  // namespace views
    231 
    232 #endif  // UI_VIEWS_WIDGET_ROOT_VIEW_H_
    233