Home | History | Annotate | Download | only in aura
      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_WINDOW_H_
      6 #define UI_AURA_WINDOW_H_
      7 
      8 #include <map>
      9 #include <string>
     10 #include <vector>
     11 
     12 #include "base/basictypes.h"
     13 #include "base/compiler_specific.h"
     14 #include "base/memory/scoped_ptr.h"
     15 #include "base/observer_list.h"
     16 #include "base/strings/string16.h"
     17 #include "ui/aura/aura_export.h"
     18 #include "ui/aura/window_layer_type.h"
     19 #include "ui/aura/window_observer.h"
     20 #include "ui/compositor/layer_animator.h"
     21 #include "ui/compositor/layer_delegate.h"
     22 #include "ui/compositor/layer_owner.h"
     23 #include "ui/events/event_constants.h"
     24 #include "ui/events/event_target.h"
     25 #include "ui/events/event_targeter.h"
     26 #include "ui/events/gestures/gesture_types.h"
     27 #include "ui/gfx/insets.h"
     28 #include "ui/gfx/native_widget_types.h"
     29 #include "ui/gfx/rect.h"
     30 #include "ui/wm/public/window_types.h"
     31 
     32 namespace gfx {
     33 class Display;
     34 class Transform;
     35 class Vector2d;
     36 }
     37 
     38 namespace ui {
     39 class EventHandler;
     40 class Layer;
     41 class Texture;
     42 }
     43 
     44 namespace aura {
     45 
     46 class LayoutManager;
     47 class WindowDelegate;
     48 class WindowObserver;
     49 class WindowTreeHost;
     50 
     51 // Defined in window_property.h (which we do not include)
     52 template<typename T>
     53 struct WindowProperty;
     54 
     55 namespace test {
     56 class WindowTestApi;
     57 }
     58 
     59 // Aura window implementation. Interesting events are sent to the
     60 // WindowDelegate.
     61 // TODO(beng): resolve ownership.
     62 class AURA_EXPORT Window : public ui::LayerDelegate,
     63                            public ui::LayerOwner,
     64                            public ui::EventTarget,
     65                            public ui::GestureConsumer {
     66  public:
     67   // Used when stacking windows.
     68   enum StackDirection {
     69     STACK_ABOVE,
     70     STACK_BELOW
     71   };
     72 
     73   typedef std::vector<Window*> Windows;
     74 
     75   explicit Window(WindowDelegate* delegate);
     76   virtual ~Window();
     77 
     78   // Initializes the window. This creates the window's layer.
     79   void Init(WindowLayerType layer_type);
     80 
     81   void set_owned_by_parent(bool owned_by_parent) {
     82     owned_by_parent_ = owned_by_parent;
     83   }
     84   bool owned_by_parent() const { return owned_by_parent_; }
     85 
     86   // A type is used to identify a class of Windows and customize behavior such
     87   // as event handling and parenting.  This field should only be consumed by the
     88   // shell -- Aura itself shouldn't contain type-specific logic.
     89   ui::wm::WindowType type() const { return type_; }
     90   void SetType(ui::wm::WindowType type);
     91 
     92   int id() const { return id_; }
     93   void set_id(int id) { id_ = id; }
     94 
     95   const std::string& name() const { return name_; }
     96   void SetName(const std::string& name);
     97 
     98   const base::string16 title() const { return title_; }
     99   void SetTitle(const base::string16& title);
    100 
    101   bool transparent() const { return transparent_; }
    102   void SetTransparent(bool transparent);
    103 
    104   // See description in Layer::SetFillsBoundsCompletely.
    105   void SetFillsBoundsCompletely(bool fills_bounds);
    106 
    107   WindowDelegate* delegate() { return delegate_; }
    108   const WindowDelegate* delegate() const { return delegate_; }
    109 
    110   const gfx::Rect& bounds() const { return bounds_; }
    111 
    112   Window* parent() { return parent_; }
    113   const Window* parent() const { return parent_; }
    114 
    115   // Returns the root Window that contains this Window. The root Window is
    116   // defined as the Window that has a dispatcher. These functions return NULL if
    117   // the Window is contained in a hierarchy that does not have a dispatcher at
    118   // its root.
    119   Window* GetRootWindow();
    120   const Window* GetRootWindow() const;
    121 
    122   WindowTreeHost* GetHost();
    123   const WindowTreeHost* GetHost() const;
    124   void set_host(WindowTreeHost* host) { host_ = host; }
    125   bool IsRootWindow() const { return !!host_; }
    126 
    127   // The Window does not own this object.
    128   void set_user_data(void* user_data) { user_data_ = user_data; }
    129   void* user_data() const { return user_data_; }
    130 
    131   // Changes the visibility of the window.
    132   void Show();
    133   void Hide();
    134   // Returns true if this window and all its ancestors are visible.
    135   bool IsVisible() const;
    136   // Returns the visibility requested by this window. IsVisible() takes into
    137   // account the visibility of the layer and ancestors, where as this tracks
    138   // whether Show() without a Hide() has been invoked.
    139   bool TargetVisibility() const { return visible_; }
    140 
    141   // Returns the window's bounds in root window's coordinates.
    142   gfx::Rect GetBoundsInRootWindow() const;
    143 
    144   // Returns the window's bounds in screen coordinates.
    145   // How the root window's coordinates is mapped to screen's coordinates
    146   // is platform dependent and defined in the implementation of the
    147   // |aura::client::ScreenPositionClient| interface.
    148   gfx::Rect GetBoundsInScreen() const;
    149 
    150   void SetTransform(const gfx::Transform& transform);
    151 
    152   // Assigns a LayoutManager to size and place child windows.
    153   // The Window takes ownership of the LayoutManager.
    154   void SetLayoutManager(LayoutManager* layout_manager);
    155   LayoutManager* layout_manager() { return layout_manager_.get(); }
    156 
    157   // Sets a new event-targeter for the window, and returns the previous
    158   // event-targeter.
    159   scoped_ptr<ui::EventTargeter> SetEventTargeter(
    160       scoped_ptr<ui::EventTargeter> targeter);
    161 
    162   // Changes the bounds of the window. If present, the window's parent's
    163   // LayoutManager may adjust the bounds.
    164   void SetBounds(const gfx::Rect& new_bounds);
    165 
    166   // Changes the bounds of the window in the screen coordintates.
    167   // If present, the window's parent's LayoutManager may adjust the bounds.
    168   void SetBoundsInScreen(const gfx::Rect& new_bounds_in_screen_coords,
    169                          const gfx::Display& dst_display);
    170 
    171   // Returns the target bounds of the window. If the window's layer is
    172   // not animating, it simply returns the current bounds.
    173   gfx::Rect GetTargetBounds() const;
    174 
    175   // Marks the a portion of window as needing to be painted.
    176   void SchedulePaintInRect(const gfx::Rect& rect);
    177 
    178   // Stacks the specified child of this Window at the front of the z-order.
    179   void StackChildAtTop(Window* child);
    180 
    181   // Stacks |child| above |target|.  Does nothing if |child| is already above
    182   // |target|.  Does not stack on top of windows with NULL layer delegates,
    183   // see WindowTest.StackingMadrigal for details.
    184   void StackChildAbove(Window* child, Window* target);
    185 
    186   // Stacks the specified child of this window at the bottom of the z-order.
    187   void StackChildAtBottom(Window* child);
    188 
    189   // Stacks |child| below |target|. Does nothing if |child| is already below
    190   // |target|.
    191   void StackChildBelow(Window* child, Window* target);
    192 
    193   // Tree operations.
    194   void AddChild(Window* child);
    195   void RemoveChild(Window* child);
    196 
    197   const Windows& children() const { return children_; }
    198 
    199   // Returns true if this Window contains |other| somewhere in its children.
    200   bool Contains(const Window* other) const;
    201 
    202   // Retrieves the first-level child with the specified id, or NULL if no first-
    203   // level child is found matching |id|.
    204   Window* GetChildById(int id);
    205   const Window* GetChildById(int id) const;
    206 
    207   // Converts |point| from |source|'s coordinates to |target|'s. If |source| is
    208   // NULL, the function returns without modifying |point|. |target| cannot be
    209   // NULL.
    210   static void ConvertPointToTarget(const Window* source,
    211                                    const Window* target,
    212                                    gfx::Point* point);
    213   static void ConvertRectToTarget(const Window* source,
    214                                   const Window* target,
    215                                   gfx::Rect* rect);
    216 
    217   // Moves the cursor to the specified location relative to the window.
    218   void MoveCursorTo(const gfx::Point& point_in_window);
    219 
    220   // Returns the cursor for the specified point, in window coordinates.
    221   gfx::NativeCursor GetCursor(const gfx::Point& point) const;
    222 
    223   // Add/remove observer.
    224   void AddObserver(WindowObserver* observer);
    225   void RemoveObserver(WindowObserver* observer);
    226   bool HasObserver(WindowObserver* observer);
    227 
    228   void set_ignore_events(bool ignore_events) { ignore_events_ = ignore_events; }
    229   bool ignore_events() const { return ignore_events_; }
    230 
    231   // Sets the window to grab hits for an area extending |insets| pixels inside
    232   // its bounds (even if that inner region overlaps a child window). This can be
    233   // used to create an invisible non-client area that overlaps the client area.
    234   void set_hit_test_bounds_override_inner(const gfx::Insets& insets) {
    235     hit_test_bounds_override_inner_ = insets;
    236   }
    237   gfx::Insets hit_test_bounds_override_inner() const {
    238     return hit_test_bounds_override_inner_;
    239   }
    240 
    241   // Returns true if the |point_in_root| in root window's coordinate falls
    242   // within this window's bounds. Returns false if the window is detached
    243   // from root window.
    244   bool ContainsPointInRoot(const gfx::Point& point_in_root) const;
    245 
    246   // Returns true if relative-to-this-Window's-origin |local_point| falls
    247   // within this Window's bounds.
    248   bool ContainsPoint(const gfx::Point& local_point) const;
    249 
    250   // Returns the Window that most closely encloses |local_point| for the
    251   // purposes of event targeting.
    252   Window* GetEventHandlerForPoint(const gfx::Point& local_point);
    253 
    254   // Returns the topmost Window with a delegate containing |local_point|.
    255   Window* GetTopWindowContainingPoint(const gfx::Point& local_point);
    256 
    257   // Returns this window's toplevel window (the highest-up-the-tree anscestor
    258   // that has a delegate set).  The toplevel window may be |this|.
    259   Window* GetToplevelWindow();
    260 
    261   // Claims or relinquishes the claim to focus.
    262   void Focus();
    263   void Blur();
    264 
    265   // Returns true if the Window is currently the focused window.
    266   bool HasFocus() const;
    267 
    268   // Returns true if the Window can be focused.
    269   bool CanFocus() const;
    270 
    271   // Returns true if the Window can receive events.
    272   bool CanReceiveEvents() const;
    273 
    274   // Does a capture on the window. This does nothing if the window isn't showing
    275   // (VISIBILITY_SHOWN) or isn't contained in a valid window hierarchy.
    276   void SetCapture();
    277 
    278   // Releases a capture.
    279   void ReleaseCapture();
    280 
    281   // Returns true if this window has capture.
    282   bool HasCapture();
    283 
    284   // Suppresses painting window content by disgarding damaged rect and ignoring
    285   // new paint requests. This is a one way operation and there is no way to
    286   // reenable painting.
    287   void SuppressPaint();
    288 
    289   // Sets the |value| of the given window |property|. Setting to the default
    290   // value (e.g., NULL) removes the property. The caller is responsible for the
    291   // lifetime of any object set as a property on the Window.
    292   template<typename T>
    293   void SetProperty(const WindowProperty<T>* property, T value);
    294 
    295   // Returns the value of the given window |property|.  Returns the
    296   // property-specific default value if the property was not previously set.
    297   template<typename T>
    298   T GetProperty(const WindowProperty<T>* property) const;
    299 
    300   // Sets the |property| to its default value. Useful for avoiding a cast when
    301   // setting to NULL.
    302   template<typename T>
    303   void ClearProperty(const WindowProperty<T>* property);
    304 
    305   // NativeWidget::[GS]etNativeWindowProperty use strings as keys, and this is
    306   // difficult to change while retaining compatibility with other platforms.
    307   // TODO(benrg): Find a better solution.
    308   void SetNativeWindowProperty(const char* key, void* value);
    309   void* GetNativeWindowProperty(const char* key) const;
    310 
    311   // Type of a function to delete a property that this window owns.
    312   typedef void (*PropertyDeallocator)(int64 value);
    313 
    314   // Overridden from ui::LayerDelegate:
    315   virtual void OnDeviceScaleFactorChanged(float device_scale_factor) OVERRIDE;
    316 
    317 #if !defined(NDEBUG)
    318   // These methods are useful when debugging.
    319   std::string GetDebugInfo() const;
    320   void PrintWindowHierarchy(int depth) const;
    321 #endif
    322 
    323   // Returns true if there was state needing to be cleaned up.
    324   bool CleanupGestureState();
    325 
    326  protected:
    327   // Deletes (or removes if not owned by parent) all child windows. Intended for
    328   // use from the destructor.
    329   void RemoveOrDestroyChildren();
    330 
    331  private:
    332   friend class test::WindowTestApi;
    333   friend class LayoutManager;
    334   friend class WindowTargeter;
    335 
    336   // Called by the public {Set,Get,Clear}Property functions.
    337   int64 SetPropertyInternal(const void* key,
    338                             const char* name,
    339                             PropertyDeallocator deallocator,
    340                             int64 value,
    341                             int64 default_value);
    342   int64 GetPropertyInternal(const void* key, int64 default_value) const;
    343 
    344   // Returns true if the mouse pointer at relative-to-this-Window's-origin
    345   // |local_point| can trigger an event for this Window.
    346   // TODO(beng): A Window can supply a hit-test mask to cause some portions of
    347   // itself to not trigger events, causing the events to fall through to the
    348   // Window behind.
    349   bool HitTest(const gfx::Point& local_point);
    350 
    351   // Changes the bounds of the window without condition.
    352   void SetBoundsInternal(const gfx::Rect& new_bounds);
    353 
    354   // Updates the visible state of the layer, but does not make visible-state
    355   // specific changes. Called from Show()/Hide().
    356   void SetVisible(bool visible);
    357 
    358   // Schedules a paint for the Window's entire bounds.
    359   void SchedulePaint();
    360 
    361   // Asks the delegate to paint the window and invokes PaintLayerlessChildren()
    362   // to paint any children with no layers.
    363   void Paint(gfx::Canvas* canvas);
    364 
    365   // Paints any layerless children to |canvas|.
    366   void PaintLayerlessChildren(gfx::Canvas* canvas);
    367 
    368   // Gets a Window (either this one or a subwindow) containing |local_point|.
    369   // If |return_tightest| is true, returns the tightest-containing (i.e.
    370   // furthest down the hierarchy) Window containing the point; otherwise,
    371   // returns the loosest.  If |for_event_handling| is true, then hit-test masks
    372   // are honored; otherwise, only bounds checks are performed.
    373   Window* GetWindowForPoint(const gfx::Point& local_point,
    374                             bool return_tightest,
    375                             bool for_event_handling);
    376 
    377   // Implementation of RemoveChild(). If |child| is being removed as the result
    378   // of an add, |new_parent| is the new parent |child| is going to be parented
    379   // to.
    380   void RemoveChildImpl(Window* child, Window* new_parent);
    381 
    382   // If this Window has a layer the layer's parent is set to NULL, otherwise
    383   // UnparentLayers() is invoked on all the children. |offset| is the offset
    384   // relative to the nearest ancestor with a layer.
    385   void UnparentLayers(bool has_layerless_ancestor,
    386                       const gfx::Vector2d& offset);
    387 
    388   // If this Window has a layer it is added to |parent| and the origin set to
    389   // |offset|. Otherwise this recurses through the children invoking
    390   // ReparentLayers(). The net effect is both setting the parent of layers to
    391   // |parent| as well as updating bounds of windows with a layerless ancestor.
    392   void ReparentLayers(ui::Layer* parent, const gfx::Vector2d& offset);
    393 
    394   // Offsets the first encountered Windows with layers by |offset|. This
    395   // recurses through all layerless Windows, stopping at windows with layers.
    396   void OffsetLayerBounds(const gfx::Vector2d& offset);
    397 
    398   // Called when this window's parent has changed.
    399   void OnParentChanged();
    400 
    401   // The various stacking functions call into this to do the actual stacking.
    402   void StackChildRelativeTo(Window* child,
    403                             Window* target,
    404                             StackDirection direction);
    405 
    406   // Invoked from StackChildRelativeTo() to stack the layers appropriately
    407   // when stacking |child| relative to |target|.
    408   void StackChildLayerRelativeTo(Window* child,
    409                                  Window* target,
    410                                  StackDirection direction);
    411 
    412   // Called when this window's stacking order among its siblings is changed.
    413   void OnStackingChanged();
    414 
    415   // Notifies observers registered with this Window (and its subtree) when the
    416   // Window has been added or is about to be removed from a RootWindow.
    417   void NotifyRemovingFromRootWindow(Window* new_root);
    418   void NotifyAddedToRootWindow();
    419 
    420   // Methods implementing hierarchy change notifications. See WindowObserver for
    421   // more details.
    422   void NotifyWindowHierarchyChange(
    423       const WindowObserver::HierarchyChangeParams& params);
    424   // Notifies this window and its child hierarchy.
    425   void NotifyWindowHierarchyChangeDown(
    426       const WindowObserver::HierarchyChangeParams& params);
    427   // Notifies this window and its parent hierarchy.
    428   void NotifyWindowHierarchyChangeUp(
    429       const WindowObserver::HierarchyChangeParams& params);
    430   // Notifies this window's observers.
    431   void NotifyWindowHierarchyChangeAtReceiver(
    432       const WindowObserver::HierarchyChangeParams& params);
    433 
    434   // Methods implementing visibility change notifications. See WindowObserver
    435   // for more details.
    436   void NotifyWindowVisibilityChanged(aura::Window* target, bool visible);
    437   // Notifies this window's observers. Returns false if |this| was deleted
    438   // during the call (by an observer), otherwise true.
    439   bool NotifyWindowVisibilityChangedAtReceiver(aura::Window* target,
    440                                                bool visible);
    441   // Notifies this window and its child hierarchy. Returns false if
    442   // |this| was deleted during the call (by an observer), otherwise
    443   // true.
    444   bool NotifyWindowVisibilityChangedDown(aura::Window* target, bool visible);
    445   // Notifies this window and its parent hierarchy.
    446   void NotifyWindowVisibilityChangedUp(aura::Window* target, bool visible);
    447 
    448   // Notifies this window and its child hierarchy of a transform applied to
    449   // |source|.
    450   void NotifyAncestorWindowTransformed(Window* source);
    451 
    452   // Invoked when the bounds of the window changes. This may be invoked directly
    453   // by us, or from the closure returned by PrepareForLayerBoundsChange() after
    454   // the bounds of the layer has changed. |old_bounds| is the previous bounds.
    455   void OnWindowBoundsChanged(const gfx::Rect& old_bounds);
    456 
    457   // Overridden from ui::LayerDelegate:
    458   virtual void OnPaintLayer(gfx::Canvas* canvas) OVERRIDE;
    459   virtual void OnDelegatedFrameDamage(
    460       const gfx::Rect& damage_rect_in_dip) OVERRIDE;
    461   virtual base::Closure PrepareForLayerBoundsChange() OVERRIDE;
    462 
    463   // Overridden from ui::EventTarget:
    464   virtual bool CanAcceptEvent(const ui::Event& event) OVERRIDE;
    465   virtual EventTarget* GetParentTarget() OVERRIDE;
    466   virtual scoped_ptr<ui::EventTargetIterator> GetChildIterator() const OVERRIDE;
    467   virtual ui::EventTargeter* GetEventTargeter() OVERRIDE;
    468   virtual void ConvertEventToTarget(ui::EventTarget* target,
    469                                     ui::LocatedEvent* event) OVERRIDE;
    470 
    471   // Updates the layer name based on the window's name and id.
    472   void UpdateLayerName();
    473 
    474   // Returns true if the mouse is currently within our bounds.
    475   bool ContainsMouse();
    476 
    477   // Returns the first ancestor (starting at |this|) with a layer. |offset| is
    478   // set to the offset from |this| to the first ancestor with a layer. |offset|
    479   // may be NULL.
    480   Window* GetAncestorWithLayer(gfx::Vector2d* offset) {
    481     return const_cast<Window*>(
    482         const_cast<const Window*>(this)->GetAncestorWithLayer(offset));
    483   }
    484   const Window* GetAncestorWithLayer(gfx::Vector2d* offset) const;
    485 
    486   // Bounds of this window relative to the parent. This is cached as the bounds
    487   // of the Layer and Window are not necessarily the same. In particular bounds
    488   // of the Layer are relative to the first ancestor with a Layer, where as this
    489   // is relative to the parent Window.
    490   gfx::Rect bounds_;
    491 
    492   WindowTreeHost* host_;
    493 
    494   ui::wm::WindowType type_;
    495 
    496   // True if the Window is owned by its parent - i.e. it will be deleted by its
    497   // parent during its parents destruction. True is the default.
    498   bool owned_by_parent_;
    499 
    500   WindowDelegate* delegate_;
    501 
    502   // The Window's parent.
    503   Window* parent_;
    504 
    505   // Child windows. Topmost is last.
    506   Windows children_;
    507 
    508   // The visibility state of the window as set by Show()/Hide(). This may differ
    509   // from the visibility of the underlying layer, which may remain visible after
    510   // the window is hidden (e.g. to animate its disappearance).
    511   bool visible_;
    512 
    513   int id_;
    514   std::string name_;
    515 
    516   base::string16 title_;
    517 
    518   // Whether layer is initialized as non-opaque.
    519   bool transparent_;
    520 
    521   scoped_ptr<LayoutManager> layout_manager_;
    522   scoped_ptr<ui::EventTargeter> targeter_;
    523 
    524   void* user_data_;
    525 
    526   // Makes the window pass all events through to any windows behind it.
    527   bool ignore_events_;
    528 
    529   // See set_hit_test_bounds_override_inner().
    530   gfx::Insets hit_test_bounds_override_inner_;
    531 
    532   ObserverList<WindowObserver, true> observers_;
    533 
    534   // Value struct to keep the name and deallocator for this property.
    535   // Key cannot be used for this purpose because it can be char* or
    536   // WindowProperty<>.
    537   struct Value {
    538     const char* name;
    539     int64 value;
    540     PropertyDeallocator deallocator;
    541   };
    542 
    543   std::map<const void*, Value> prop_map_;
    544 
    545   DISALLOW_COPY_AND_ASSIGN(Window);
    546 };
    547 
    548 }  // namespace aura
    549 
    550 #endif  // UI_AURA_WINDOW_H_
    551