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 // 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();
268 // Returns true if the Window is currently the focused window.
269 bool HasFocus() const;
271 // Returns true if the Window can be focused.
272 bool CanFocus() const;
274 // Returns true if the Window can receive events.
275 bool CanReceiveEvents() const;
277 // Does a capture on the window. This does nothing if the window isn't showing
278 // (VISIBILITY_SHOWN) or isn't contained in a valid window hierarchy.
281 // Releases a capture.
282 void ReleaseCapture();
284 // Returns true if this window has capture.
287 // Suppresses painting window content by disgarding damaged rect and ignoring
288 // new paint requests. This is a one way operation and there is no way to
289 // reenable painting.
290 void SuppressPaint();
292 // Sets the |value| of the given window |property|. Setting to the default
293 // value (e.g., NULL) removes the property. The caller is responsible for the
294 // lifetime of any object set as a property on the Window.
296 void SetProperty(const WindowProperty
<T
>* property
, T value
);
298 // Returns the value of the given window |property|. Returns the
299 // property-specific default value if the property was not previously set.
301 T
GetProperty(const WindowProperty
<T
>* property
) const;
303 // Sets the |property| to its default value. Useful for avoiding a cast when
306 void ClearProperty(const WindowProperty
<T
>* property
);
308 // NativeWidget::[GS]etNativeWindowProperty use strings as keys, and this is
309 // difficult to change while retaining compatibility with other platforms.
310 // TODO(benrg): Find a better solution.
311 void SetNativeWindowProperty(const char* key
, void* value
);
312 void* GetNativeWindowProperty(const char* key
) const;
314 // Type of a function to delete a property that this window owns.
315 typedef void (*PropertyDeallocator
)(int64 value
);
317 // Overridden from ui::LayerDelegate:
318 void OnDeviceScaleFactorChanged(float device_scale_factor
) override
;
321 // These methods are useful when debugging.
322 std::string
GetDebugInfo() const;
323 void PrintWindowHierarchy(int depth
) const;
326 // Returns true if there was state needing to be cleaned up.
327 bool CleanupGestureState();
330 // Deletes (or removes if not owned by parent) all child windows. Intended for
331 // use from the destructor.
332 void RemoveOrDestroyChildren();
335 friend class test::WindowTestApi
;
336 friend class LayoutManager
;
337 friend class WindowTargeter
;
338 friend class subtle::PropertyHelper
;
339 // Called by the public {Set,Get,Clear}Property functions.
340 int64
SetPropertyInternal(const void* key
,
342 PropertyDeallocator deallocator
,
344 int64 default_value
);
345 int64
GetPropertyInternal(const void* key
, int64 default_value
) const;
347 // Returns true if the mouse pointer at relative-to-this-Window's-origin
348 // |local_point| can trigger an event for this Window.
349 // TODO(beng): A Window can supply a hit-test mask to cause some portions of
350 // itself to not trigger events, causing the events to fall through to the
352 bool HitTest(const gfx::Point
& local_point
);
354 // Changes the bounds of the window without condition.
355 void SetBoundsInternal(const gfx::Rect
& new_bounds
);
357 // Updates the visible state of the layer, but does not make visible-state
358 // specific changes. Called from Show()/Hide().
359 void SetVisible(bool visible
);
361 // Schedules a paint for the Window's entire bounds.
362 void SchedulePaint();
364 // Asks the delegate to paint the window and invokes PaintLayerlessChildren()
365 // to paint any children with no layers.
366 void Paint(const ui::PaintContext
& context
);
368 // Paints any layerless children to |canvas|.
369 void PaintLayerlessChildren(const ui::PaintContext
& context
);
371 // Gets a Window (either this one or a subwindow) containing |local_point|.
372 // If |return_tightest| is true, returns the tightest-containing (i.e.
373 // furthest down the hierarchy) Window containing the point; otherwise,
374 // returns the loosest. If |for_event_handling| is true, then hit-test masks
375 // are honored; otherwise, only bounds checks are performed.
376 Window
* GetWindowForPoint(const gfx::Point
& local_point
,
377 bool return_tightest
,
378 bool for_event_handling
);
380 // Implementation of RemoveChild(). If |child| is being removed as the result
381 // of an add, |new_parent| is the new parent |child| is going to be parented
383 void RemoveChildImpl(Window
* child
, Window
* new_parent
);
385 // Called when this window's parent has changed.
386 void OnParentChanged();
388 // The various stacking functions call into this to do the actual stacking.
389 void StackChildRelativeTo(Window
* child
,
391 StackDirection direction
);
393 // Invoked from StackChildRelativeTo() to stack the layers appropriately
394 // when stacking |child| relative to |target|.
395 void StackChildLayerRelativeTo(Window
* child
,
397 StackDirection direction
);
399 // Called when this window's stacking order among its siblings is changed.
400 void OnStackingChanged();
402 // Notifies observers registered with this Window (and its subtree) when the
403 // Window has been added or is about to be removed from a RootWindow.
404 void NotifyRemovingFromRootWindow(Window
* new_root
);
405 void NotifyAddedToRootWindow();
407 // Methods implementing hierarchy change notifications. See WindowObserver for
409 void NotifyWindowHierarchyChange(
410 const WindowObserver::HierarchyChangeParams
& params
);
411 // Notifies this window and its child hierarchy.
412 void NotifyWindowHierarchyChangeDown(
413 const WindowObserver::HierarchyChangeParams
& params
);
414 // Notifies this window and its parent hierarchy.
415 void NotifyWindowHierarchyChangeUp(
416 const WindowObserver::HierarchyChangeParams
& params
);
417 // Notifies this window's observers.
418 void NotifyWindowHierarchyChangeAtReceiver(
419 const WindowObserver::HierarchyChangeParams
& params
);
421 // Methods implementing visibility change notifications. See WindowObserver
423 void NotifyWindowVisibilityChanged(aura::Window
* target
, bool visible
);
424 // Notifies this window's observers. Returns false if |this| was deleted
425 // during the call (by an observer), otherwise true.
426 bool NotifyWindowVisibilityChangedAtReceiver(aura::Window
* target
,
428 // Notifies this window and its child hierarchy. Returns false if
429 // |this| was deleted during the call (by an observer), otherwise
431 bool NotifyWindowVisibilityChangedDown(aura::Window
* target
, bool visible
);
432 // Notifies this window and its parent hierarchy.
433 void NotifyWindowVisibilityChangedUp(aura::Window
* target
, bool visible
);
435 // Notifies this window and its child hierarchy of a transform applied to
437 void NotifyAncestorWindowTransformed(Window
* source
);
439 // Invoked when the bounds of the window changes. This may be invoked directly
440 // by us, or from the closure returned by PrepareForLayerBoundsChange() after
441 // the bounds of the layer has changed. |old_bounds| is the previous bounds.
442 void OnWindowBoundsChanged(const gfx::Rect
& old_bounds
);
444 // Overridden from ui::LayerDelegate:
445 void OnPaintLayer(const ui::PaintContext
& context
) override
;
446 void OnDelegatedFrameDamage(const gfx::Rect
& damage_rect_in_dip
) override
;
447 base::Closure
PrepareForLayerBoundsChange() override
;
449 // Overridden from ui::EventTarget:
450 bool CanAcceptEvent(const ui::Event
& event
) override
;
451 EventTarget
* GetParentTarget() override
;
452 scoped_ptr
<ui::EventTargetIterator
> GetChildIterator() const override
;
453 ui::EventTargeter
* GetEventTargeter() override
;
454 void ConvertEventToTarget(ui::EventTarget
* target
,
455 ui::LocatedEvent
* event
) override
;
457 // Updates the layer name based on the window's name and id.
458 void UpdateLayerName();
460 // Bounds of this window relative to the parent. This is cached as the bounds
461 // of the Layer and Window are not necessarily the same. In particular bounds
462 // of the Layer are relative to the first ancestor with a Layer, where as this
463 // is relative to the parent Window.
466 WindowTreeHost
* host_
;
468 ui::wm::WindowType type_
;
470 // True if the Window is owned by its parent - i.e. it will be deleted by its
471 // parent during its parents destruction. True is the default.
472 bool owned_by_parent_
;
474 WindowDelegate
* delegate_
;
476 // The Window's parent.
479 // Child windows. Topmost is last.
482 // The visibility state of the window as set by Show()/Hide(). This may differ
483 // from the visibility of the underlying layer, which may remain visible after
484 // the window is hidden (e.g. to animate its disappearance).
490 base::string16 title_
;
492 // Whether layer is initialized as non-opaque.
495 scoped_ptr
<LayoutManager
> layout_manager_
;
496 scoped_ptr
<ui::EventTargeter
> targeter_
;
500 // Makes the window pass all events through to any windows behind it.
503 // See set_hit_test_bounds_override_inner().
504 gfx::Insets hit_test_bounds_override_inner_
;
506 base::ObserverList
<WindowObserver
, true> observers_
;
508 // Value struct to keep the name and deallocator for this property.
509 // Key cannot be used for this purpose because it can be char* or
514 PropertyDeallocator deallocator
;
517 std::map
<const void*, Value
> prop_map_
;
519 DISALLOW_COPY_AND_ASSIGN(Window
);
524 #endif // UI_AURA_WINDOW_H_