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.
5 #ifndef UI_AURA_WINDOW_H_
6 #define UI_AURA_WINDOW_H_
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_observer.h"
19 #include "ui/compositor/layer_animator.h"
20 #include "ui/compositor/layer_delegate.h"
21 #include "ui/compositor/layer_owner.h"
22 #include "ui/events/event_constants.h"
23 #include "ui/events/event_target.h"
24 #include "ui/events/event_targeter.h"
25 #include "ui/events/gestures/gesture_types.h"
26 #include "ui/gfx/geometry/insets.h"
27 #include "ui/gfx/geometry/rect.h"
28 #include "ui/gfx/native_widget_types.h"
29 #include "ui/wm/public/window_types.h"
40 class TextInputClient
;
51 // Defined in window_property.h (which we do not include)
53 struct WindowProperty
;
63 // Aura window implementation. Interesting events are sent to the
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
{
71 // Used when stacking windows.
77 typedef std::vector
<Window
*> Windows
;
79 explicit Window(WindowDelegate
* delegate
);
82 // Initializes the window. This creates the window's layer.
83 void Init(ui::LayerType layer_type
);
85 void set_owned_by_parent(bool owned_by_parent
) {
86 owned_by_parent_
= owned_by_parent
;
88 bool owned_by_parent() const { return owned_by_parent_
; }
90 // A type is used to identify a class of Windows and customize behavior such
91 // as event handling and parenting. This field should only be consumed by the
92 // shell -- Aura itself shouldn't contain type-specific logic.
93 ui::wm::WindowType
type() const { return type_
; }
94 void SetType(ui::wm::WindowType type
);
96 int id() const { return id_
; }
97 void set_id(int id
) { id_
= id
; }
99 const std::string
& name() const { return name_
; }
100 void SetName(const std::string
& name
);
102 const base::string16
title() const { return title_
; }
103 void SetTitle(const base::string16
& title
);
105 bool transparent() const { return transparent_
; }
106 void SetTransparent(bool transparent
);
108 // See description in Layer::SetFillsBoundsCompletely.
109 void SetFillsBoundsCompletely(bool fills_bounds
);
111 WindowDelegate
* delegate() { return delegate_
; }
112 const WindowDelegate
* delegate() const { return delegate_
; }
114 const gfx::Rect
& bounds() const { return bounds_
; }
116 Window
* parent() { return parent_
; }
117 const Window
* parent() const { return parent_
; }
119 // Returns the root Window that contains this Window. The root Window is
120 // defined as the Window that has a dispatcher. These functions return NULL if
121 // the Window is contained in a hierarchy that does not have a dispatcher at
123 Window
* GetRootWindow();
124 const Window
* GetRootWindow() const;
126 WindowTreeHost
* GetHost();
127 const WindowTreeHost
* GetHost() const;
128 void set_host(WindowTreeHost
* host
) { host_
= host
; }
129 bool IsRootWindow() const { return !!host_
; }
131 // The Window does not own this object.
132 void set_user_data(void* user_data
) { user_data_
= user_data
; }
133 void* user_data() const { return user_data_
; }
135 // Changes the visibility of the window.
138 // Returns true if this window and all its ancestors are visible.
139 bool IsVisible() const;
140 // Returns the visibility requested by this window. IsVisible() takes into
141 // account the visibility of the layer and ancestors, where as this tracks
142 // whether Show() without a Hide() has been invoked.
143 bool TargetVisibility() const { return visible_
; }
145 // Returns the window's bounds in root window's coordinates.
146 gfx::Rect
GetBoundsInRootWindow() const;
148 // Returns the window's bounds in screen coordinates.
149 // How the root window's coordinates is mapped to screen's coordinates
150 // is platform dependent and defined in the implementation of the
151 // |aura::client::ScreenPositionClient| interface.
152 gfx::Rect
GetBoundsInScreen() const;
154 void SetTransform(const gfx::Transform
& transform
);
156 // Assigns a LayoutManager to size and place child windows.
157 // The Window takes ownership of the LayoutManager.
158 void SetLayoutManager(LayoutManager
* layout_manager
);
159 LayoutManager
* layout_manager() { return layout_manager_
.get(); }
161 // Sets a new event-targeter for the window, and returns the previous
163 scoped_ptr
<ui::EventTargeter
> SetEventTargeter(
164 scoped_ptr
<ui::EventTargeter
> targeter
);
166 // Changes the bounds of the window. If present, the window's parent's
167 // LayoutManager may adjust the bounds.
168 void SetBounds(const gfx::Rect
& new_bounds
);
170 // Changes the bounds of the window in the screen coordintates.
171 // If present, the window's parent's LayoutManager may adjust the bounds.
172 void SetBoundsInScreen(const gfx::Rect
& new_bounds_in_screen_coords
,
173 const gfx::Display
& dst_display
);
175 // Returns the target bounds of the window. If the window's layer is
176 // not animating, it simply returns the current bounds.
177 gfx::Rect
GetTargetBounds() const;
179 // Marks the a portion of window as needing to be painted.
180 void SchedulePaintInRect(const gfx::Rect
& rect
);
182 // Stacks the specified child of this Window at the front of the z-order.
183 void StackChildAtTop(Window
* child
);
185 // Stacks |child| above |target|. Does nothing if |child| is already above
186 // |target|. Does not stack on top of windows with NULL layer delegates,
187 // see WindowTest.StackingMadrigal for details.
188 void StackChildAbove(Window
* child
, Window
* target
);
190 // Stacks the specified child of this window at the bottom of the z-order.
191 void StackChildAtBottom(Window
* child
);
193 // Stacks |child| below |target|. Does nothing if |child| is already below
195 void StackChildBelow(Window
* child
, Window
* target
);
198 void AddChild(Window
* child
);
199 void RemoveChild(Window
* child
);
201 const Windows
& children() const { return children_
; }
203 // Returns true if this Window contains |other| somewhere in its children.
204 bool Contains(const Window
* other
) const;
206 // Retrieves the first-level child with the specified id, or NULL if no first-
207 // level child is found matching |id|.
208 Window
* GetChildById(int id
);
209 const Window
* GetChildById(int id
) const;
211 // Converts |point| from |source|'s coordinates to |target|'s. If |source| is
212 // NULL, the function returns without modifying |point|. |target| cannot be
214 static void ConvertPointToTarget(const Window
* source
,
215 const Window
* target
,
217 static void ConvertRectToTarget(const Window
* source
,
218 const Window
* target
,
221 // Returns the focused text input client within this window.
222 // This function does not look at child windows.
223 ui::TextInputClient
* GetFocusedTextInputClient();
225 // Moves the cursor to the specified location relative to the window.
226 void MoveCursorTo(const gfx::Point
& point_in_window
);
228 // Returns the cursor for the specified point, in window coordinates.
229 gfx::NativeCursor
GetCursor(const gfx::Point
& point
) const;
231 // Add/remove observer.
232 void AddObserver(WindowObserver
* observer
);
233 void RemoveObserver(WindowObserver
* observer
);
234 bool HasObserver(const WindowObserver
* observer
) const;
236 void set_ignore_events(bool ignore_events
) { ignore_events_
= ignore_events
; }
237 bool ignore_events() const { return ignore_events_
; }
239 // Sets the window to grab hits for an area extending |insets| pixels inside
240 // its bounds (even if that inner region overlaps a child window). This can be
241 // used to create an invisible non-client area that overlaps the client area.
242 void set_hit_test_bounds_override_inner(const gfx::Insets
& insets
) {
243 hit_test_bounds_override_inner_
= insets
;
245 gfx::Insets
hit_test_bounds_override_inner() const {
246 return hit_test_bounds_override_inner_
;
249 // Returns true if the |point_in_root| in root window's coordinate falls
250 // within this window's bounds. Returns false if the window is detached
252 bool ContainsPointInRoot(const gfx::Point
& point_in_root
) const;
254 // Returns true if relative-to-this-Window's-origin |local_point| falls
255 // within this Window's bounds.
256 bool ContainsPoint(const gfx::Point
& local_point
) const;
258 // Returns the Window that most closely encloses |local_point| for the
259 // purposes of event targeting.
260 Window
* GetEventHandlerForPoint(const gfx::Point
& local_point
);
262 // Returns the topmost Window with a delegate containing |local_point|.
263 Window
* GetTopWindowContainingPoint(const gfx::Point
& local_point
);
265 // Returns this window's toplevel window (the highest-up-the-tree anscestor
266 // that has a delegate set). The toplevel window may be |this|.
267 Window
* GetToplevelWindow();
272 // Returns true if the Window is currently the focused window.
273 bool HasFocus() const;
275 // Returns true if the Window can be focused.
276 bool CanFocus() const;
278 // Returns true if the Window can receive events.
279 bool CanReceiveEvents() const;
281 // Does a capture on the window. This does nothing if the window isn't showing
282 // (VISIBILITY_SHOWN) or isn't contained in a valid window hierarchy.
285 // Releases a capture.
286 void ReleaseCapture();
288 // Returns true if this window has capture.
291 // Suppresses painting window content by disgarding damaged rect and ignoring
292 // new paint requests. This is a one way operation and there is no way to
293 // reenable painting.
294 void SuppressPaint();
296 // Sets the |value| of the given window |property|. Setting to the default
297 // value (e.g., NULL) removes the property. The caller is responsible for the
298 // lifetime of any object set as a property on the Window.
300 void SetProperty(const WindowProperty
<T
>* property
, T value
);
302 // Returns the value of the given window |property|. Returns the
303 // property-specific default value if the property was not previously set.
305 T
GetProperty(const WindowProperty
<T
>* property
) const;
307 // Sets the |property| to its default value. Useful for avoiding a cast when
310 void ClearProperty(const WindowProperty
<T
>* property
);
312 // NativeWidget::[GS]etNativeWindowProperty use strings as keys, and this is
313 // difficult to change while retaining compatibility with other platforms.
314 // TODO(benrg): Find a better solution.
315 void SetNativeWindowProperty(const char* key
, void* value
);
316 void* GetNativeWindowProperty(const char* key
) const;
318 // Type of a function to delete a property that this window owns.
319 typedef void (*PropertyDeallocator
)(int64 value
);
321 // Overridden from ui::LayerDelegate:
322 void OnDeviceScaleFactorChanged(float device_scale_factor
) override
;
325 // These methods are useful when debugging.
326 std::string
GetDebugInfo() const;
327 void PrintWindowHierarchy(int depth
) const;
330 // Returns true if there was state needing to be cleaned up.
331 bool CleanupGestureState();
334 // Deletes (or removes if not owned by parent) all child windows. Intended for
335 // use from the destructor.
336 void RemoveOrDestroyChildren();
339 friend class test::WindowTestApi
;
340 friend class LayoutManager
;
341 friend class WindowTargeter
;
342 friend class subtle::PropertyHelper
;
343 // Called by the public {Set,Get,Clear}Property functions.
344 int64
SetPropertyInternal(const void* key
,
346 PropertyDeallocator deallocator
,
348 int64 default_value
);
349 int64
GetPropertyInternal(const void* key
, int64 default_value
) const;
351 // Returns true if the mouse pointer at relative-to-this-Window's-origin
352 // |local_point| can trigger an event for this Window.
353 // TODO(beng): A Window can supply a hit-test mask to cause some portions of
354 // itself to not trigger events, causing the events to fall through to the
356 bool HitTest(const gfx::Point
& local_point
);
358 // Changes the bounds of the window without condition.
359 void SetBoundsInternal(const gfx::Rect
& new_bounds
);
361 // Updates the visible state of the layer, but does not make visible-state
362 // specific changes. Called from Show()/Hide().
363 void SetVisible(bool visible
);
365 // Schedules a paint for the Window's entire bounds.
366 void SchedulePaint();
368 // Asks the delegate to paint the window and invokes PaintLayerlessChildren()
369 // to paint any children with no layers.
370 void Paint(const ui::PaintContext
& context
);
372 // Paints any layerless children to |canvas|.
373 void PaintLayerlessChildren(const ui::PaintContext
& context
);
375 // Gets a Window (either this one or a subwindow) containing |local_point|.
376 // If |return_tightest| is true, returns the tightest-containing (i.e.
377 // furthest down the hierarchy) Window containing the point; otherwise,
378 // returns the loosest. If |for_event_handling| is true, then hit-test masks
379 // are honored; otherwise, only bounds checks are performed.
380 Window
* GetWindowForPoint(const gfx::Point
& local_point
,
381 bool return_tightest
,
382 bool for_event_handling
);
384 // Implementation of RemoveChild(). If |child| is being removed as the result
385 // of an add, |new_parent| is the new parent |child| is going to be parented
387 void RemoveChildImpl(Window
* child
, Window
* new_parent
);
389 // If this Window has a layer the layer's parent is set to NULL, otherwise
390 // UnparentLayers() is invoked on all the children. |offset| is the offset
391 // relative to the nearest ancestor with a layer.
392 void UnparentLayers(bool has_layerless_ancestor
,
393 const gfx::Vector2d
& offset
);
395 // If this Window has a layer it is added to |parent| and the origin set to
396 // |offset|. Otherwise this recurses through the children invoking
397 // ReparentLayers(). The net effect is both setting the parent of layers to
398 // |parent| as well as updating bounds of windows with a layerless ancestor.
399 void ReparentLayers(ui::Layer
* parent
, const gfx::Vector2d
& offset
);
401 // Offsets the first encountered Windows with layers by |offset|. This
402 // recurses through all layerless Windows, stopping at windows with layers.
403 void OffsetLayerBounds(const gfx::Vector2d
& offset
);
405 // Called when this window's parent has changed.
406 void OnParentChanged();
408 // The various stacking functions call into this to do the actual stacking.
409 void StackChildRelativeTo(Window
* child
,
411 StackDirection direction
);
413 // Invoked from StackChildRelativeTo() to stack the layers appropriately
414 // when stacking |child| relative to |target|.
415 void StackChildLayerRelativeTo(Window
* child
,
417 StackDirection direction
);
419 // Called when this window's stacking order among its siblings is changed.
420 void OnStackingChanged();
422 // Notifies observers registered with this Window (and its subtree) when the
423 // Window has been added or is about to be removed from a RootWindow.
424 void NotifyRemovingFromRootWindow(Window
* new_root
);
425 void NotifyAddedToRootWindow();
427 // Methods implementing hierarchy change notifications. See WindowObserver for
429 void NotifyWindowHierarchyChange(
430 const WindowObserver::HierarchyChangeParams
& params
);
431 // Notifies this window and its child hierarchy.
432 void NotifyWindowHierarchyChangeDown(
433 const WindowObserver::HierarchyChangeParams
& params
);
434 // Notifies this window and its parent hierarchy.
435 void NotifyWindowHierarchyChangeUp(
436 const WindowObserver::HierarchyChangeParams
& params
);
437 // Notifies this window's observers.
438 void NotifyWindowHierarchyChangeAtReceiver(
439 const WindowObserver::HierarchyChangeParams
& params
);
441 // Methods implementing visibility change notifications. See WindowObserver
443 void NotifyWindowVisibilityChanged(aura::Window
* target
, bool visible
);
444 // Notifies this window's observers. Returns false if |this| was deleted
445 // during the call (by an observer), otherwise true.
446 bool NotifyWindowVisibilityChangedAtReceiver(aura::Window
* target
,
448 // Notifies this window and its child hierarchy. Returns false if
449 // |this| was deleted during the call (by an observer), otherwise
451 bool NotifyWindowVisibilityChangedDown(aura::Window
* target
, bool visible
);
452 // Notifies this window and its parent hierarchy.
453 void NotifyWindowVisibilityChangedUp(aura::Window
* target
, bool visible
);
455 // Notifies this window and its child hierarchy of a transform applied to
457 void NotifyAncestorWindowTransformed(Window
* source
);
459 // Invoked when the bounds of the window changes. This may be invoked directly
460 // by us, or from the closure returned by PrepareForLayerBoundsChange() after
461 // the bounds of the layer has changed. |old_bounds| is the previous bounds.
462 void OnWindowBoundsChanged(const gfx::Rect
& old_bounds
);
464 // Overridden from ui::LayerDelegate:
465 void OnPaintLayer(const ui::PaintContext
& context
) override
;
466 void OnDelegatedFrameDamage(const gfx::Rect
& damage_rect_in_dip
) override
;
467 base::Closure
PrepareForLayerBoundsChange() override
;
469 // Overridden from ui::EventTarget:
470 bool CanAcceptEvent(const ui::Event
& event
) override
;
471 EventTarget
* GetParentTarget() override
;
472 scoped_ptr
<ui::EventTargetIterator
> GetChildIterator() const override
;
473 ui::EventTargeter
* GetEventTargeter() override
;
474 void ConvertEventToTarget(ui::EventTarget
* target
,
475 ui::LocatedEvent
* event
) override
;
477 // Updates the layer name based on the window's name and id.
478 void UpdateLayerName();
480 // Returns the first ancestor (starting at |this|) with a layer. |offset| is
481 // set to the offset from |this| to the first ancestor with a layer. |offset|
483 Window
* GetAncestorWithLayer(gfx::Vector2d
* offset
) {
484 return const_cast<Window
*>(
485 const_cast<const Window
*>(this)->GetAncestorWithLayer(offset
));
487 const Window
* GetAncestorWithLayer(gfx::Vector2d
* offset
) const;
489 // Bounds of this window relative to the parent. This is cached as the bounds
490 // of the Layer and Window are not necessarily the same. In particular bounds
491 // of the Layer are relative to the first ancestor with a Layer, where as this
492 // is relative to the parent Window.
495 WindowTreeHost
* host_
;
497 ui::wm::WindowType type_
;
499 // True if the Window is owned by its parent - i.e. it will be deleted by its
500 // parent during its parents destruction. True is the default.
501 bool owned_by_parent_
;
503 WindowDelegate
* delegate_
;
505 // The Window's parent.
508 // Child windows. Topmost is last.
511 // The visibility state of the window as set by Show()/Hide(). This may differ
512 // from the visibility of the underlying layer, which may remain visible after
513 // the window is hidden (e.g. to animate its disappearance).
519 base::string16 title_
;
521 // Whether layer is initialized as non-opaque.
524 scoped_ptr
<LayoutManager
> layout_manager_
;
525 scoped_ptr
<ui::EventTargeter
> targeter_
;
529 // Makes the window pass all events through to any windows behind it.
532 // See set_hit_test_bounds_override_inner().
533 gfx::Insets hit_test_bounds_override_inner_
;
535 base::ObserverList
<WindowObserver
, true> observers_
;
537 // Value struct to keep the name and deallocator for this property.
538 // Key cannot be used for this purpose because it can be char* or
543 PropertyDeallocator deallocator
;
546 std::map
<const void*, Value
> prop_map_
;
548 DISALLOW_COPY_AND_ASSIGN(Window
);
553 #endif // UI_AURA_WINDOW_H_