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