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 SetTitle(const base::string16
& 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 Window
* GetRootWindow();
120 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 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 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 // Add/remove observer.
224 void AddObserver(WindowObserver
* observer
);
225 void RemoveObserver(WindowObserver
* observer
);
226 bool HasObserver(WindowObserver
* observer
);
228 void set_ignore_events(bool ignore_events
) { ignore_events_
= ignore_events
; }
229 bool ignore_events() const { return ignore_events_
; }
231 // Sets the window to grab hits for an area extending |insets| pixels inside
232 // its bounds (even if that inner region overlaps a child window). This can be
233 // used to create an invisible non-client area that overlaps the client area.
234 void set_hit_test_bounds_override_inner(const gfx::Insets
& insets
) {
235 hit_test_bounds_override_inner_
= insets
;
237 gfx::Insets
hit_test_bounds_override_inner() const {
238 return hit_test_bounds_override_inner_
;
241 // Returns true if the |point_in_root| in root window's coordinate falls
242 // within this window's bounds. Returns false if the window is detached
244 bool ContainsPointInRoot(const gfx::Point
& point_in_root
) const;
246 // Returns true if relative-to-this-Window's-origin |local_point| falls
247 // within this Window's bounds.
248 bool ContainsPoint(const gfx::Point
& local_point
) const;
250 // Returns the Window that most closely encloses |local_point| for the
251 // purposes of event targeting.
252 Window
* GetEventHandlerForPoint(const gfx::Point
& local_point
);
254 // Returns the topmost Window with a delegate containing |local_point|.
255 Window
* GetTopWindowContainingPoint(const gfx::Point
& local_point
);
257 // Returns this window's toplevel window (the highest-up-the-tree anscestor
258 // that has a delegate set). The toplevel window may be |this|.
259 Window
* GetToplevelWindow();
261 // Claims or relinquishes the claim to focus.
265 // Returns true if the Window is currently the focused window.
266 bool HasFocus() const;
268 // Returns true if the Window can be focused.
269 bool CanFocus() const;
271 // Returns true if the Window can receive events.
272 bool CanReceiveEvents() const;
274 // Does a capture on the window. This does nothing if the window isn't showing
275 // (VISIBILITY_SHOWN) or isn't contained in a valid window hierarchy.
278 // Releases a capture.
279 void ReleaseCapture();
281 // Returns true if this window has capture.
284 // Suppresses painting window content by disgarding damaged rect and ignoring
285 // new paint requests. This is a one way operation and there is no way to
286 // reenable painting.
287 void SuppressPaint();
289 // Sets the |value| of the given window |property|. Setting to the default
290 // value (e.g., NULL) removes the property. The caller is responsible for the
291 // lifetime of any object set as a property on the Window.
293 void SetProperty(const WindowProperty
<T
>* property
, T value
);
295 // Returns the value of the given window |property|. Returns the
296 // property-specific default value if the property was not previously set.
298 T
GetProperty(const WindowProperty
<T
>* property
) const;
300 // Sets the |property| to its default value. Useful for avoiding a cast when
303 void ClearProperty(const WindowProperty
<T
>* property
);
305 // NativeWidget::[GS]etNativeWindowProperty use strings as keys, and this is
306 // difficult to change while retaining compatibility with other platforms.
307 // TODO(benrg): Find a better solution.
308 void SetNativeWindowProperty(const char* key
, void* value
);
309 void* GetNativeWindowProperty(const char* key
) const;
311 // Type of a function to delete a property that this window owns.
312 typedef void (*PropertyDeallocator
)(int64 value
);
314 // Overridden from ui::LayerDelegate:
315 virtual void OnDeviceScaleFactorChanged(float device_scale_factor
) OVERRIDE
;
318 // These methods are useful when debugging.
319 std::string
GetDebugInfo() const;
320 void PrintWindowHierarchy(int depth
) const;
323 // Returns true if there was state needing to be cleaned up.
324 bool CleanupGestureState();
327 // Deletes (or removes if not owned by parent) all child windows. Intended for
328 // use from the destructor.
329 void RemoveOrDestroyChildren();
332 friend class test::WindowTestApi
;
333 friend class LayoutManager
;
334 friend class WindowTargeter
;
336 // Called by the public {Set,Get,Clear}Property functions.
337 int64
SetPropertyInternal(const void* key
,
339 PropertyDeallocator deallocator
,
341 int64 default_value
);
342 int64
GetPropertyInternal(const void* key
, int64 default_value
) const;
344 // Returns true if the mouse pointer at relative-to-this-Window's-origin
345 // |local_point| can trigger an event for this Window.
346 // TODO(beng): A Window can supply a hit-test mask to cause some portions of
347 // itself to not trigger events, causing the events to fall through to the
349 bool HitTest(const gfx::Point
& local_point
);
351 // Changes the bounds of the window without condition.
352 void SetBoundsInternal(const gfx::Rect
& new_bounds
);
354 // Updates the visible state of the layer, but does not make visible-state
355 // specific changes. Called from Show()/Hide().
356 void SetVisible(bool visible
);
358 // Schedules a paint for the Window's entire bounds.
359 void SchedulePaint();
361 // Asks the delegate to paint the window and invokes PaintLayerlessChildren()
362 // to paint any children with no layers.
363 void Paint(gfx::Canvas
* canvas
);
365 // Paints any layerless children to |canvas|.
366 void PaintLayerlessChildren(gfx::Canvas
* canvas
);
368 // Gets a Window (either this one or a subwindow) containing |local_point|.
369 // If |return_tightest| is true, returns the tightest-containing (i.e.
370 // furthest down the hierarchy) Window containing the point; otherwise,
371 // returns the loosest. If |for_event_handling| is true, then hit-test masks
372 // are honored; otherwise, only bounds checks are performed.
373 Window
* GetWindowForPoint(const gfx::Point
& local_point
,
374 bool return_tightest
,
375 bool for_event_handling
);
377 // Implementation of RemoveChild(). If |child| is being removed as the result
378 // of an add, |new_parent| is the new parent |child| is going to be parented
380 void RemoveChildImpl(Window
* child
, Window
* new_parent
);
382 // If this Window has a layer the layer's parent is set to NULL, otherwise
383 // UnparentLayers() is invoked on all the children. |offset| is the offset
384 // relative to the nearest ancestor with a layer.
385 void UnparentLayers(bool has_layerless_ancestor
,
386 const gfx::Vector2d
& offset
);
388 // If this Window has a layer it is added to |parent| and the origin set to
389 // |offset|. Otherwise this recurses through the children invoking
390 // ReparentLayers(). The net effect is both setting the parent of layers to
391 // |parent| as well as updating bounds of windows with a layerless ancestor.
392 void ReparentLayers(ui::Layer
* parent
, const gfx::Vector2d
& offset
);
394 // Offsets the first encountered Windows with layers by |offset|. This
395 // recurses through all layerless Windows, stopping at windows with layers.
396 void OffsetLayerBounds(const gfx::Vector2d
& offset
);
398 // Called when this window's parent has changed.
399 void OnParentChanged();
401 // The various stacking functions call into this to do the actual stacking.
402 void StackChildRelativeTo(Window
* child
,
404 StackDirection direction
);
406 // Invoked from StackChildRelativeTo() to stack the layers appropriately
407 // when stacking |child| relative to |target|.
408 void StackChildLayerRelativeTo(Window
* child
,
410 StackDirection direction
);
412 // Called when this window's stacking order among its siblings is changed.
413 void OnStackingChanged();
415 // Notifies observers registered with this Window (and its subtree) when the
416 // Window has been added or is about to be removed from a RootWindow.
417 void NotifyRemovingFromRootWindow(Window
* new_root
);
418 void NotifyAddedToRootWindow();
420 // Methods implementing hierarchy change notifications. See WindowObserver for
422 void NotifyWindowHierarchyChange(
423 const WindowObserver::HierarchyChangeParams
& params
);
424 // Notifies this window and its child hierarchy.
425 void NotifyWindowHierarchyChangeDown(
426 const WindowObserver::HierarchyChangeParams
& params
);
427 // Notifies this window and its parent hierarchy.
428 void NotifyWindowHierarchyChangeUp(
429 const WindowObserver::HierarchyChangeParams
& params
);
430 // Notifies this window's observers.
431 void NotifyWindowHierarchyChangeAtReceiver(
432 const WindowObserver::HierarchyChangeParams
& params
);
434 // Methods implementing visibility change notifications. See WindowObserver
436 void NotifyWindowVisibilityChanged(aura::Window
* target
, bool visible
);
437 // Notifies this window's observers. Returns false if |this| was deleted
438 // during the call (by an observer), otherwise true.
439 bool NotifyWindowVisibilityChangedAtReceiver(aura::Window
* target
,
441 // Notifies this window and its child hierarchy. Returns false if
442 // |this| was deleted during the call (by an observer), otherwise
444 bool NotifyWindowVisibilityChangedDown(aura::Window
* target
, bool visible
);
445 // Notifies this window and its parent hierarchy.
446 void NotifyWindowVisibilityChangedUp(aura::Window
* target
, bool visible
);
448 // Invoked when the bounds of the window changes. This may be invoked directly
449 // by us, or from the closure returned by PrepareForLayerBoundsChange() after
450 // the bounds of the layer has changed. |old_bounds| is the previous bounds.
451 void OnWindowBoundsChanged(const gfx::Rect
& old_bounds
);
453 // Overridden from ui::LayerDelegate:
454 virtual void OnPaintLayer(gfx::Canvas
* canvas
) OVERRIDE
;
455 virtual base::Closure
PrepareForLayerBoundsChange() OVERRIDE
;
457 // Overridden from ui::EventTarget:
458 virtual bool CanAcceptEvent(const ui::Event
& event
) OVERRIDE
;
459 virtual EventTarget
* GetParentTarget() OVERRIDE
;
460 virtual scoped_ptr
<ui::EventTargetIterator
> GetChildIterator() const OVERRIDE
;
461 virtual ui::EventTargeter
* GetEventTargeter() OVERRIDE
;
462 virtual void ConvertEventToTarget(ui::EventTarget
* target
,
463 ui::LocatedEvent
* event
) OVERRIDE
;
465 // Updates the layer name based on the window's name and id.
466 void UpdateLayerName();
468 // Returns true if the mouse is currently within our bounds.
469 bool ContainsMouse();
471 // Returns the first ancestor (starting at |this|) with a layer. |offset| is
472 // set to the offset from |this| to the first ancestor with a layer. |offset|
474 Window
* GetAncestorWithLayer(gfx::Vector2d
* offset
) {
475 return const_cast<Window
*>(
476 const_cast<const Window
*>(this)->GetAncestorWithLayer(offset
));
478 const Window
* GetAncestorWithLayer(gfx::Vector2d
* offset
) const;
480 // Bounds of this window relative to the parent. This is cached as the bounds
481 // of the Layer and Window are not necessarily the same. In particular bounds
482 // of the Layer are relative to the first ancestor with a Layer, where as this
483 // is relative to the parent Window.
486 WindowTreeHost
* host_
;
488 ui::wm::WindowType type_
;
490 // True if the Window is owned by its parent - i.e. it will be deleted by its
491 // parent during its parents destruction. True is the default.
492 bool owned_by_parent_
;
494 WindowDelegate
* delegate_
;
496 // The Window's parent.
499 // Child windows. Topmost is last.
502 // The visibility state of the window as set by Show()/Hide(). This may differ
503 // from the visibility of the underlying layer, which may remain visible after
504 // the window is hidden (e.g. to animate its disappearance).
510 base::string16 title_
;
512 // Whether layer is initialized as non-opaque.
515 scoped_ptr
<LayoutManager
> layout_manager_
;
516 scoped_ptr
<ui::EventTargeter
> targeter_
;
520 // Makes the window pass all events through to any windows behind it.
523 // See set_hit_test_bounds_override_inner().
524 gfx::Insets hit_test_bounds_override_inner_
;
526 ObserverList
<WindowObserver
, true> observers_
;
528 // Value struct to keep the name and deallocator for this property.
529 // Key cannot be used for this purpose because it can be char* or
534 PropertyDeallocator deallocator
;
537 std::map
<const void*, Value
> prop_map_
;
539 DISALLOW_COPY_AND_ASSIGN(Window
);
544 #endif // UI_AURA_WINDOW_H_