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/insets.h"
28 #include "ui/gfx/native_widget_types.h"
29 #include "ui/gfx/rect.h"
30 #include "ui/wm/public/window_types.h"
51 // Defined in window_property.h (which we do not include)
53 struct WindowProperty
;
59 // Aura window implementation. Interesting events are sent to the
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
{
67 // Used when stacking windows.
73 typedef std::vector
<Window
*> Windows
;
75 explicit Window(WindowDelegate
* delegate
);
78 // Initializes the window. This creates the window's layer.
79 void Init(WindowLayerType layer_type
);
81 void set_owned_by_parent(bool owned_by_parent
) {
82 owned_by_parent_
= owned_by_parent
;
84 bool owned_by_parent() const { return owned_by_parent_
; }
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
);
92 int id() const { return id_
; }
93 void set_id(int id
) { id_
= id
; }
95 const std::string
& name() const { return name_
; }
96 void SetName(const std::string
& name
);
98 const base::string16
title() const { return title_
; }
99 void set_title(const base::string16
& title
) { title_
= title
; }
101 bool transparent() const { return transparent_
; }
102 void SetTransparent(bool transparent
);
104 // See description in Layer::SetFillsBoundsCompletely.
105 void SetFillsBoundsCompletely(bool fills_bounds
);
107 WindowDelegate
* delegate() { return delegate_
; }
108 const WindowDelegate
* delegate() const { return delegate_
; }
110 const gfx::Rect
& bounds() const { return bounds_
; }
112 Window
* parent() { return parent_
; }
113 const Window
* parent() const { return parent_
; }
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
119 virtual Window
* GetRootWindow();
120 virtual const Window
* GetRootWindow() const;
122 WindowTreeHost
* GetHost();
123 const WindowTreeHost
* GetHost() const;
124 void set_host(WindowTreeHost
* host
) { host_
= host
; }
125 bool IsRootWindow() const { return !!host_
; }
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_
; }
131 // Changes the visibility of the window.
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_
; }
141 // Returns the window's bounds in root window's coordinates.
142 gfx::Rect
GetBoundsInRootWindow() const;
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;
150 virtual void SetTransform(const gfx::Transform
& transform
);
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(); }
157 // Sets a new event-targeter for the window, and returns the previous
159 scoped_ptr
<ui::EventTargeter
> SetEventTargeter(
160 scoped_ptr
<ui::EventTargeter
> targeter
);
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
);
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
);
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;
175 // Marks the a portion of window as needing to be painted.
176 void SchedulePaintInRect(const gfx::Rect
& rect
);
178 // Stacks the specified child of this Window at the front of the z-order.
179 void StackChildAtTop(Window
* child
);
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
);
186 // Stacks the specified child of this window at the bottom of the z-order.
187 void StackChildAtBottom(Window
* child
);
189 // Stacks |child| below |target|. Does nothing if |child| is already below
191 void StackChildBelow(Window
* child
, Window
* target
);
194 void AddChild(Window
* child
);
195 void RemoveChild(Window
* child
);
197 const Windows
& children() const { return children_
; }
199 // Returns true if this Window contains |other| somewhere in its children.
200 bool Contains(const Window
* other
) const;
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;
207 // Converts |point| from |source|'s coordinates to |target|'s. If |source| is
208 // NULL, the function returns without modifying |point|. |target| cannot be
210 static void ConvertPointToTarget(const Window
* source
,
211 const Window
* target
,
213 static void ConvertRectToTarget(const Window
* source
,
214 const Window
* target
,
217 // Moves the cursor to the specified location relative to the window.
218 virtual void MoveCursorTo(const gfx::Point
& point_in_window
);
220 // Returns the cursor for the specified point, in window coordinates.
221 gfx::NativeCursor
GetCursor(const gfx::Point
& point
) const;
223 // Sets an 'event filter' for the window. An 'event filter' for a Window is
224 // a pre-target event handler, where the window owns the handler. A window
225 // can have only one such event filter. Setting a new filter removes and
226 // destroys any previously installed filter.
227 void SetEventFilter(ui::EventHandler
* event_filter
);
229 // Add/remove observer.
230 void AddObserver(WindowObserver
* observer
);
231 void RemoveObserver(WindowObserver
* observer
);
232 bool HasObserver(WindowObserver
* observer
);
234 void set_ignore_events(bool ignore_events
) { ignore_events_
= ignore_events
; }
235 bool ignore_events() const { return ignore_events_
; }
237 // Sets the window to grab hits for an area extending |insets| pixels inside
238 // its bounds (even if that inner region overlaps a child window). This can be
239 // used to create an invisible non-client area that overlaps the client area.
240 void set_hit_test_bounds_override_inner(const gfx::Insets
& insets
) {
241 hit_test_bounds_override_inner_
= insets
;
243 gfx::Insets
hit_test_bounds_override_inner() const {
244 return hit_test_bounds_override_inner_
;
247 // Returns true if the |point_in_root| in root window's coordinate falls
248 // within this window's bounds. Returns false if the window is detached
250 bool ContainsPointInRoot(const gfx::Point
& point_in_root
) const;
252 // Returns true if relative-to-this-Window's-origin |local_point| falls
253 // within this Window's bounds.
254 bool ContainsPoint(const gfx::Point
& local_point
) const;
256 // Returns the Window that most closely encloses |local_point| for the
257 // purposes of event targeting.
258 Window
* GetEventHandlerForPoint(const gfx::Point
& local_point
);
260 // Returns the topmost Window with a delegate containing |local_point|.
261 Window
* GetTopWindowContainingPoint(const gfx::Point
& local_point
);
263 // Returns this window's toplevel window (the highest-up-the-tree anscestor
264 // that has a delegate set). The toplevel window may be |this|.
265 Window
* GetToplevelWindow();
267 // Claims or relinquishes the claim to focus.
271 // Returns true if the Window is currently the focused window.
272 bool HasFocus() const;
274 // Returns true if the Window can be focused.
275 virtual bool CanFocus() const;
277 // Returns true if the Window can receive events.
278 virtual bool CanReceiveEvents() const;
280 // Does a capture on the window. This does nothing if the window isn't showing
281 // (VISIBILITY_SHOWN) or isn't contained in a valid window hierarchy.
284 // Releases a capture.
285 void ReleaseCapture();
287 // Returns true if this window has capture.
290 // Suppresses painting window content by disgarding damaged rect and ignoring
291 // new paint requests. This is a one way operation and there is no way to
292 // reenable painting.
293 void SuppressPaint();
295 // Sets the |value| of the given window |property|. Setting to the default
296 // value (e.g., NULL) removes the property. The caller is responsible for the
297 // lifetime of any object set as a property on the Window.
299 void SetProperty(const WindowProperty
<T
>* property
, T value
);
301 // Returns the value of the given window |property|. Returns the
302 // property-specific default value if the property was not previously set.
304 T
GetProperty(const WindowProperty
<T
>* property
) const;
306 // Sets the |property| to its default value. Useful for avoiding a cast when
309 void ClearProperty(const WindowProperty
<T
>* property
);
311 // NativeWidget::[GS]etNativeWindowProperty use strings as keys, and this is
312 // difficult to change while retaining compatibility with other platforms.
313 // TODO(benrg): Find a better solution.
314 void SetNativeWindowProperty(const char* key
, void* value
);
315 void* GetNativeWindowProperty(const char* key
) const;
317 // Type of a function to delete a property that this window owns.
318 typedef void (*PropertyDeallocator
)(int64 value
);
320 // Overridden from ui::LayerDelegate:
321 virtual void OnDeviceScaleFactorChanged(float device_scale_factor
) OVERRIDE
;
324 // These methods are useful when debugging.
325 std::string
GetDebugInfo() const;
326 void PrintWindowHierarchy(int depth
) const;
329 // Returns true if there was state needing to be cleaned up.
330 bool CleanupGestureState();
333 // Deletes (or removes if not owned by parent) all child windows. Intended for
334 // use from the destructor.
335 void RemoveOrDestroyChildren();
338 friend class test::WindowTestApi
;
339 friend class LayoutManager
;
340 friend class WindowTargeter
;
342 // Called by the public {Set,Get,Clear}Property functions.
343 int64
SetPropertyInternal(const void* key
,
345 PropertyDeallocator deallocator
,
347 int64 default_value
);
348 int64
GetPropertyInternal(const void* key
, int64 default_value
) const;
350 // Returns true if the mouse pointer at relative-to-this-Window's-origin
351 // |local_point| can trigger an event for this Window.
352 // TODO(beng): A Window can supply a hit-test mask to cause some portions of
353 // itself to not trigger events, causing the events to fall through to the
355 bool HitTest(const gfx::Point
& local_point
);
357 // Changes the bounds of the window without condition.
358 void SetBoundsInternal(const gfx::Rect
& new_bounds
);
360 // Updates the visible state of the layer, but does not make visible-state
361 // specific changes. Called from Show()/Hide().
362 void SetVisible(bool visible
);
364 // Schedules a paint for the Window's entire bounds.
365 void SchedulePaint();
367 // Asks the delegate to paint the window and invokes PaintLayerlessChildren()
368 // to paint any children with no layers.
369 void Paint(gfx::Canvas
* canvas
);
371 // Paints any layerless children to |canvas|.
372 void PaintLayerlessChildren(gfx::Canvas
* canvas
);
374 // Gets a Window (either this one or a subwindow) containing |local_point|.
375 // If |return_tightest| is true, returns the tightest-containing (i.e.
376 // furthest down the hierarchy) Window containing the point; otherwise,
377 // returns the loosest. If |for_event_handling| is true, then hit-test masks
378 // are honored; otherwise, only bounds checks are performed.
379 Window
* GetWindowForPoint(const gfx::Point
& local_point
,
380 bool return_tightest
,
381 bool for_event_handling
);
383 // Implementation of RemoveChild(). If |child| is being removed as the result
384 // of an add, |new_parent| is the new parent |child| is going to be parented
386 void RemoveChildImpl(Window
* child
, Window
* new_parent
);
388 // If this Window has a layer the layer's parent is set to NULL, otherwise
389 // UnparentLayers() is invoked on all the children. |offset| is the offset
390 // relative to the nearest ancestor with a layer.
391 void UnparentLayers(bool has_layerless_ancestor
,
392 const gfx::Vector2d
& offset
);
394 // If this Window has a layer it is added to |parent| and the origin set to
395 // |offset|. Otherwise this recurses through the children invoking
396 // ReparentLayers(). The net effect is both setting the parent of layers to
397 // |parent| as well as updating bounds of windows with a layerless ancestor.
398 void ReparentLayers(ui::Layer
* parent
, const gfx::Vector2d
& offset
);
400 // Offsets the first encountered Windows with layers by |offset|. This
401 // recurses through all layerless Windows, stopping at windows with layers.
402 void OffsetLayerBounds(const gfx::Vector2d
& offset
);
404 // Called when this window's parent has changed.
405 void OnParentChanged();
407 // The various stacking functions call into this to do the actual stacking.
408 void StackChildRelativeTo(Window
* child
,
410 StackDirection direction
);
412 // Invoked from StackChildRelativeTo() to stack the layers appropriately
413 // when stacking |child| relative to |target|.
414 void StackChildLayerRelativeTo(Window
* child
,
416 StackDirection direction
);
418 // Called when this window's stacking order among its siblings is changed.
419 void OnStackingChanged();
421 // Notifies observers registered with this Window (and its subtree) when the
422 // Window has been added or is about to be removed from a RootWindow.
423 void NotifyRemovingFromRootWindow(Window
* new_root
);
424 void NotifyAddedToRootWindow();
426 // Methods implementing hierarchy change notifications. See WindowObserver for
428 void NotifyWindowHierarchyChange(
429 const WindowObserver::HierarchyChangeParams
& params
);
430 // Notifies this window and its child hierarchy.
431 void NotifyWindowHierarchyChangeDown(
432 const WindowObserver::HierarchyChangeParams
& params
);
433 // Notifies this window and its parent hierarchy.
434 void NotifyWindowHierarchyChangeUp(
435 const WindowObserver::HierarchyChangeParams
& params
);
436 // Notifies this window's observers.
437 void NotifyWindowHierarchyChangeAtReceiver(
438 const WindowObserver::HierarchyChangeParams
& params
);
440 // Methods implementing visibility change notifications. See WindowObserver
442 void NotifyWindowVisibilityChanged(aura::Window
* target
, bool visible
);
443 // Notifies this window's observers. Returns false if |this| was deleted
444 // during the call (by an observer), otherwise true.
445 bool NotifyWindowVisibilityChangedAtReceiver(aura::Window
* target
,
447 // Notifies this window and its child hierarchy. Returns false if
448 // |this| was deleted during the call (by an observer), otherwise
450 bool NotifyWindowVisibilityChangedDown(aura::Window
* target
, bool visible
);
451 // Notifies this window and its parent hierarchy.
452 void NotifyWindowVisibilityChangedUp(aura::Window
* target
, bool visible
);
454 // Invoked when the bounds of the window changes. This may be invoked directly
455 // by us, or from the closure returned by PrepareForLayerBoundsChange() after
456 // the bounds of the layer has changed. |old_bounds| is the previous bounds.
457 void OnWindowBoundsChanged(const gfx::Rect
& old_bounds
);
459 // Overridden from ui::LayerDelegate:
460 virtual void OnPaintLayer(gfx::Canvas
* canvas
) OVERRIDE
;
461 virtual base::Closure
PrepareForLayerBoundsChange() OVERRIDE
;
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
;
471 // Updates the layer name based on the window's name and id.
472 void UpdateLayerName();
474 // Returns true if the mouse is currently within our bounds.
475 bool ContainsMouse();
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
<ui::EventHandler
> event_filter_
;
522 scoped_ptr
<LayoutManager
> layout_manager_
;
523 scoped_ptr
<ui::EventTargeter
> targeter_
;
527 // Makes the window pass all events through to any windows behind it.
530 // See set_hit_test_bounds_override_inner().
531 gfx::Insets hit_test_bounds_override_inner_
;
533 ObserverList
<WindowObserver
, true> observers_
;
535 // Value struct to keep the name and deallocator for this property.
536 // Key cannot be used for this purpose because it can be char* or
541 PropertyDeallocator deallocator
;
544 std::map
<const void*, Value
> prop_map_
;
546 DISALLOW_COPY_AND_ASSIGN(Window
);
551 #endif // UI_AURA_WINDOW_H_