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