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_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/geometry/insets.h"
28 #include "ui/gfx/geometry/rect.h"
29 #include "ui/gfx/native_widget_types.h"
30 #include "ui/wm/public/window_types.h"
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(WindowLayerType 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 // Moves the cursor to the specified location relative to the window.
222 void MoveCursorTo(const gfx::Point
& point_in_window
);
224 // Returns the cursor for the specified point, in window coordinates.
225 gfx::NativeCursor
GetCursor(const gfx::Point
& point
) const;
227 // Add/remove observer.
228 void AddObserver(WindowObserver
* observer
);
229 void RemoveObserver(WindowObserver
* observer
);
230 bool HasObserver(const WindowObserver
* observer
) const;
232 void set_ignore_events(bool ignore_events
) { ignore_events_
= ignore_events
; }
233 bool ignore_events() const { return ignore_events_
; }
235 // Sets the window to grab hits for an area extending |insets| pixels inside
236 // its bounds (even if that inner region overlaps a child window). This can be
237 // used to create an invisible non-client area that overlaps the client area.
238 void set_hit_test_bounds_override_inner(const gfx::Insets
& insets
) {
239 hit_test_bounds_override_inner_
= insets
;
241 gfx::Insets
hit_test_bounds_override_inner() const {
242 return hit_test_bounds_override_inner_
;
245 // Returns true if the |point_in_root| in root window's coordinate falls
246 // within this window's bounds. Returns false if the window is detached
248 bool ContainsPointInRoot(const gfx::Point
& point_in_root
) const;
250 // Returns true if relative-to-this-Window's-origin |local_point| falls
251 // within this Window's bounds.
252 bool ContainsPoint(const gfx::Point
& local_point
) const;
254 // Returns the Window that most closely encloses |local_point| for the
255 // purposes of event targeting.
256 Window
* GetEventHandlerForPoint(const gfx::Point
& local_point
);
258 // Returns the topmost Window with a delegate containing |local_point|.
259 Window
* GetTopWindowContainingPoint(const gfx::Point
& local_point
);
261 // Returns this window's toplevel window (the highest-up-the-tree anscestor
262 // that has a delegate set). The toplevel window may be |this|.
263 Window
* GetToplevelWindow();
265 // Claims or relinquishes the claim to focus.
269 // Returns true if the Window is currently the focused window.
270 bool HasFocus() const;
272 // Returns true if the Window can be focused.
273 bool CanFocus() const;
275 // Returns true if the Window can receive events.
276 bool CanReceiveEvents() const;
278 // Does a capture on the window. This does nothing if the window isn't showing
279 // (VISIBILITY_SHOWN) or isn't contained in a valid window hierarchy.
282 // Releases a capture.
283 void ReleaseCapture();
285 // Returns true if this window has capture.
288 // Suppresses painting window content by disgarding damaged rect and ignoring
289 // new paint requests. This is a one way operation and there is no way to
290 // reenable painting.
291 void SuppressPaint();
293 // Sets the |value| of the given window |property|. Setting to the default
294 // value (e.g., NULL) removes the property. The caller is responsible for the
295 // lifetime of any object set as a property on the Window.
297 void SetProperty(const WindowProperty
<T
>* property
, T value
);
299 // Returns the value of the given window |property|. Returns the
300 // property-specific default value if the property was not previously set.
302 T
GetProperty(const WindowProperty
<T
>* property
) const;
304 // Sets the |property| to its default value. Useful for avoiding a cast when
307 void ClearProperty(const WindowProperty
<T
>* property
);
309 // NativeWidget::[GS]etNativeWindowProperty use strings as keys, and this is
310 // difficult to change while retaining compatibility with other platforms.
311 // TODO(benrg): Find a better solution.
312 void SetNativeWindowProperty(const char* key
, void* value
);
313 void* GetNativeWindowProperty(const char* key
) const;
315 // Type of a function to delete a property that this window owns.
316 typedef void (*PropertyDeallocator
)(int64 value
);
318 // Overridden from ui::LayerDelegate:
319 void OnDeviceScaleFactorChanged(float device_scale_factor
) override
;
322 // These methods are useful when debugging.
323 std::string
GetDebugInfo() const;
324 void PrintWindowHierarchy(int depth
) const;
327 // Returns true if there was state needing to be cleaned up.
328 bool CleanupGestureState();
331 // Deletes (or removes if not owned by parent) all child windows. Intended for
332 // use from the destructor.
333 void RemoveOrDestroyChildren();
336 friend class test::WindowTestApi
;
337 friend class LayoutManager
;
338 friend class WindowTargeter
;
339 friend class subtle::PropertyHelper
;
340 // Called by the public {Set,Get,Clear}Property functions.
341 int64
SetPropertyInternal(const void* key
,
343 PropertyDeallocator deallocator
,
345 int64 default_value
);
346 int64
GetPropertyInternal(const void* key
, int64 default_value
) const;
348 // Returns true if the mouse pointer at relative-to-this-Window's-origin
349 // |local_point| can trigger an event for this Window.
350 // TODO(beng): A Window can supply a hit-test mask to cause some portions of
351 // itself to not trigger events, causing the events to fall through to the
353 bool HitTest(const gfx::Point
& local_point
);
355 // Changes the bounds of the window without condition.
356 void SetBoundsInternal(const gfx::Rect
& new_bounds
);
358 // Updates the visible state of the layer, but does not make visible-state
359 // specific changes. Called from Show()/Hide().
360 void SetVisible(bool visible
);
362 // Schedules a paint for the Window's entire bounds.
363 void SchedulePaint();
365 // Asks the delegate to paint the window and invokes PaintLayerlessChildren()
366 // to paint any children with no layers.
367 void Paint(gfx::Canvas
* canvas
);
369 // Paints any layerless children to |canvas|.
370 void PaintLayerlessChildren(gfx::Canvas
* canvas
);
372 // Gets a Window (either this one or a subwindow) containing |local_point|.
373 // If |return_tightest| is true, returns the tightest-containing (i.e.
374 // furthest down the hierarchy) Window containing the point; otherwise,
375 // returns the loosest. If |for_event_handling| is true, then hit-test masks
376 // are honored; otherwise, only bounds checks are performed.
377 Window
* GetWindowForPoint(const gfx::Point
& local_point
,
378 bool return_tightest
,
379 bool for_event_handling
);
381 // Implementation of RemoveChild(). If |child| is being removed as the result
382 // of an add, |new_parent| is the new parent |child| is going to be parented
384 void RemoveChildImpl(Window
* child
, Window
* new_parent
);
386 // If this Window has a layer the layer's parent is set to NULL, otherwise
387 // UnparentLayers() is invoked on all the children. |offset| is the offset
388 // relative to the nearest ancestor with a layer.
389 void UnparentLayers(bool has_layerless_ancestor
,
390 const gfx::Vector2d
& offset
);
392 // If this Window has a layer it is added to |parent| and the origin set to
393 // |offset|. Otherwise this recurses through the children invoking
394 // ReparentLayers(). The net effect is both setting the parent of layers to
395 // |parent| as well as updating bounds of windows with a layerless ancestor.
396 void ReparentLayers(ui::Layer
* parent
, const gfx::Vector2d
& offset
);
398 // Offsets the first encountered Windows with layers by |offset|. This
399 // recurses through all layerless Windows, stopping at windows with layers.
400 void OffsetLayerBounds(const gfx::Vector2d
& offset
);
402 // Called when this window's parent has changed.
403 void OnParentChanged();
405 // The various stacking functions call into this to do the actual stacking.
406 void StackChildRelativeTo(Window
* child
,
408 StackDirection direction
);
410 // Invoked from StackChildRelativeTo() to stack the layers appropriately
411 // when stacking |child| relative to |target|.
412 void StackChildLayerRelativeTo(Window
* child
,
414 StackDirection direction
);
416 // Called when this window's stacking order among its siblings is changed.
417 void OnStackingChanged();
419 // Notifies observers registered with this Window (and its subtree) when the
420 // Window has been added or is about to be removed from a RootWindow.
421 void NotifyRemovingFromRootWindow(Window
* new_root
);
422 void NotifyAddedToRootWindow();
424 // Methods implementing hierarchy change notifications. See WindowObserver for
426 void NotifyWindowHierarchyChange(
427 const WindowObserver::HierarchyChangeParams
& params
);
428 // Notifies this window and its child hierarchy.
429 void NotifyWindowHierarchyChangeDown(
430 const WindowObserver::HierarchyChangeParams
& params
);
431 // Notifies this window and its parent hierarchy.
432 void NotifyWindowHierarchyChangeUp(
433 const WindowObserver::HierarchyChangeParams
& params
);
434 // Notifies this window's observers.
435 void NotifyWindowHierarchyChangeAtReceiver(
436 const WindowObserver::HierarchyChangeParams
& params
);
438 // Methods implementing visibility change notifications. See WindowObserver
440 void NotifyWindowVisibilityChanged(aura::Window
* target
, bool visible
);
441 // Notifies this window's observers. Returns false if |this| was deleted
442 // during the call (by an observer), otherwise true.
443 bool NotifyWindowVisibilityChangedAtReceiver(aura::Window
* target
,
445 // Notifies this window and its child hierarchy. Returns false if
446 // |this| was deleted during the call (by an observer), otherwise
448 bool NotifyWindowVisibilityChangedDown(aura::Window
* target
, bool visible
);
449 // Notifies this window and its parent hierarchy.
450 void NotifyWindowVisibilityChangedUp(aura::Window
* target
, bool visible
);
452 // Notifies this window and its child hierarchy of a transform applied to
454 void NotifyAncestorWindowTransformed(Window
* source
);
456 // Invoked when the bounds of the window changes. This may be invoked directly
457 // by us, or from the closure returned by PrepareForLayerBoundsChange() after
458 // the bounds of the layer has changed. |old_bounds| is the previous bounds.
459 void OnWindowBoundsChanged(const gfx::Rect
& old_bounds
);
461 // Overridden from ui::LayerDelegate:
462 void OnPaintLayer(gfx::Canvas
* canvas
) override
;
463 void OnDelegatedFrameDamage(const gfx::Rect
& damage_rect_in_dip
) override
;
464 base::Closure
PrepareForLayerBoundsChange() override
;
466 // Overridden from ui::EventTarget:
467 bool CanAcceptEvent(const ui::Event
& event
) override
;
468 EventTarget
* GetParentTarget() override
;
469 scoped_ptr
<ui::EventTargetIterator
> GetChildIterator() const override
;
470 ui::EventTargeter
* GetEventTargeter() override
;
471 void ConvertEventToTarget(ui::EventTarget
* target
,
472 ui::LocatedEvent
* event
) override
;
474 // Updates the layer name based on the window's name and id.
475 void UpdateLayerName();
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|
480 Window
* GetAncestorWithLayer(gfx::Vector2d
* offset
) {
481 return const_cast<Window
*>(
482 const_cast<const Window
*>(this)->GetAncestorWithLayer(offset
));
484 const Window
* GetAncestorWithLayer(gfx::Vector2d
* offset
) const;
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.
492 WindowTreeHost
* host_
;
494 ui::wm::WindowType type_
;
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_
;
500 WindowDelegate
* delegate_
;
502 // The Window's parent.
505 // Child windows. Topmost is last.
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).
516 base::string16 title_
;
518 // Whether layer is initialized as non-opaque.
521 scoped_ptr
<LayoutManager
> layout_manager_
;
522 scoped_ptr
<ui::EventTargeter
> targeter_
;
526 // Makes the window pass all events through to any windows behind it.
529 // See set_hit_test_bounds_override_inner().
530 gfx::Insets hit_test_bounds_override_inner_
;
532 ObserverList
<WindowObserver
, true> observers_
;
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
540 PropertyDeallocator deallocator
;
543 std::map
<const void*, Value
> prop_map_
;
545 DISALLOW_COPY_AND_ASSIGN(Window
);
550 #endif // UI_AURA_WINDOW_H_