Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / ui / aura / window.h
bloba8163a5d6117d23c5a42f46e5f073ad5ef24e3e5
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_
8 #include <map>
9 #include <string>
10 #include <vector>
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"
31 namespace gfx {
32 class Display;
33 class Transform;
34 class Vector2d;
37 namespace ui {
38 class EventHandler;
39 class Layer;
40 class TextInputClient;
41 class Texture;
44 namespace aura {
46 class LayoutManager;
47 class WindowDelegate;
48 class WindowObserver;
49 class WindowTreeHost;
51 // Defined in window_property.h (which we do not include)
52 template<typename T>
53 struct WindowProperty;
55 namespace subtle {
56 class PropertyHelper;
59 namespace test {
60 class WindowTestApi;
63 // Aura window implementation. Interesting events are sent to the
64 // WindowDelegate.
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 {
70 public:
71 // Used when stacking windows.
72 enum StackDirection {
73 STACK_ABOVE,
74 STACK_BELOW
77 typedef std::vector<Window*> Windows;
79 explicit Window(WindowDelegate* delegate);
80 ~Window() override;
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
122 // its root.
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.
136 void Show();
137 void Hide();
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
162 // event-targeter.
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
194 // |target|.
195 void StackChildBelow(Window* child, Window* target);
197 // Tree operations.
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
213 // NULL.
214 static void ConvertPointToTarget(const Window* source,
215 const Window* target,
216 gfx::Point* point);
217 static void ConvertRectToTarget(const Window* source,
218 const Window* target,
219 gfx::Rect* rect);
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
251 // from root window.
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();
269 // Claims or relinquishes the claim to focus.
270 void Focus();
271 void Blur();
273 // Returns true if the Window is currently the focused window.
274 bool HasFocus() const;
276 // Returns true if the Window can be focused.
277 bool CanFocus() const;
279 // Returns true if the Window can receive events.
280 bool CanReceiveEvents() const;
282 // Does a capture on the window. This does nothing if the window isn't showing
283 // (VISIBILITY_SHOWN) or isn't contained in a valid window hierarchy.
284 void SetCapture();
286 // Releases a capture.
287 void ReleaseCapture();
289 // Returns true if this window has capture.
290 bool HasCapture();
292 // Suppresses painting window content by disgarding damaged rect and ignoring
293 // new paint requests. This is a one way operation and there is no way to
294 // reenable painting.
295 void SuppressPaint();
297 // Sets the |value| of the given window |property|. Setting to the default
298 // value (e.g., NULL) removes the property. The caller is responsible for the
299 // lifetime of any object set as a property on the Window.
300 template<typename T>
301 void SetProperty(const WindowProperty<T>* property, T value);
303 // Returns the value of the given window |property|. Returns the
304 // property-specific default value if the property was not previously set.
305 template<typename T>
306 T GetProperty(const WindowProperty<T>* property) const;
308 // Sets the |property| to its default value. Useful for avoiding a cast when
309 // setting to NULL.
310 template<typename T>
311 void ClearProperty(const WindowProperty<T>* property);
313 // NativeWidget::[GS]etNativeWindowProperty use strings as keys, and this is
314 // difficult to change while retaining compatibility with other platforms.
315 // TODO(benrg): Find a better solution.
316 void SetNativeWindowProperty(const char* key, void* value);
317 void* GetNativeWindowProperty(const char* key) const;
319 // Type of a function to delete a property that this window owns.
320 typedef void (*PropertyDeallocator)(int64 value);
322 // Overridden from ui::LayerDelegate:
323 void OnDeviceScaleFactorChanged(float device_scale_factor) override;
325 #if !defined(NDEBUG)
326 // These methods are useful when debugging.
327 std::string GetDebugInfo() const;
328 void PrintWindowHierarchy(int depth) const;
329 #endif
331 // Returns true if there was state needing to be cleaned up.
332 bool CleanupGestureState();
334 protected:
335 // Deletes (or removes if not owned by parent) all child windows. Intended for
336 // use from the destructor.
337 void RemoveOrDestroyChildren();
339 private:
340 friend class test::WindowTestApi;
341 friend class LayoutManager;
342 friend class WindowTargeter;
343 friend class subtle::PropertyHelper;
344 // Called by the public {Set,Get,Clear}Property functions.
345 int64 SetPropertyInternal(const void* key,
346 const char* name,
347 PropertyDeallocator deallocator,
348 int64 value,
349 int64 default_value);
350 int64 GetPropertyInternal(const void* key, int64 default_value) const;
352 // Returns true if the mouse pointer at relative-to-this-Window's-origin
353 // |local_point| can trigger an event for this Window.
354 // TODO(beng): A Window can supply a hit-test mask to cause some portions of
355 // itself to not trigger events, causing the events to fall through to the
356 // Window behind.
357 bool HitTest(const gfx::Point& local_point);
359 // Changes the bounds of the window without condition.
360 void SetBoundsInternal(const gfx::Rect& new_bounds);
362 // Updates the visible state of the layer, but does not make visible-state
363 // specific changes. Called from Show()/Hide().
364 void SetVisible(bool visible);
366 // Schedules a paint for the Window's entire bounds.
367 void SchedulePaint();
369 // Asks the delegate to paint the window and invokes PaintLayerlessChildren()
370 // to paint any children with no layers.
371 void Paint(const ui::PaintContext& context);
373 // Paints any layerless children to |canvas|.
374 void PaintLayerlessChildren(const ui::PaintContext& context);
376 // Gets a Window (either this one or a subwindow) containing |local_point|.
377 // If |return_tightest| is true, returns the tightest-containing (i.e.
378 // furthest down the hierarchy) Window containing the point; otherwise,
379 // returns the loosest. If |for_event_handling| is true, then hit-test masks
380 // are honored; otherwise, only bounds checks are performed.
381 Window* GetWindowForPoint(const gfx::Point& local_point,
382 bool return_tightest,
383 bool for_event_handling);
385 // Implementation of RemoveChild(). If |child| is being removed as the result
386 // of an add, |new_parent| is the new parent |child| is going to be parented
387 // to.
388 void RemoveChildImpl(Window* child, Window* new_parent);
390 // If this Window has a layer the layer's parent is set to NULL, otherwise
391 // UnparentLayers() is invoked on all the children. |offset| is the offset
392 // relative to the nearest ancestor with a layer.
393 void UnparentLayers(bool has_layerless_ancestor,
394 const gfx::Vector2d& offset);
396 // If this Window has a layer it is added to |parent| and the origin set to
397 // |offset|. Otherwise this recurses through the children invoking
398 // ReparentLayers(). The net effect is both setting the parent of layers to
399 // |parent| as well as updating bounds of windows with a layerless ancestor.
400 void ReparentLayers(ui::Layer* parent, const gfx::Vector2d& offset);
402 // Offsets the first encountered Windows with layers by |offset|. This
403 // recurses through all layerless Windows, stopping at windows with layers.
404 void OffsetLayerBounds(const gfx::Vector2d& offset);
406 // Called when this window's parent has changed.
407 void OnParentChanged();
409 // The various stacking functions call into this to do the actual stacking.
410 void StackChildRelativeTo(Window* child,
411 Window* target,
412 StackDirection direction);
414 // Invoked from StackChildRelativeTo() to stack the layers appropriately
415 // when stacking |child| relative to |target|.
416 void StackChildLayerRelativeTo(Window* child,
417 Window* target,
418 StackDirection direction);
420 // Called when this window's stacking order among its siblings is changed.
421 void OnStackingChanged();
423 // Notifies observers registered with this Window (and its subtree) when the
424 // Window has been added or is about to be removed from a RootWindow.
425 void NotifyRemovingFromRootWindow(Window* new_root);
426 void NotifyAddedToRootWindow();
428 // Methods implementing hierarchy change notifications. See WindowObserver for
429 // more details.
430 void NotifyWindowHierarchyChange(
431 const WindowObserver::HierarchyChangeParams& params);
432 // Notifies this window and its child hierarchy.
433 void NotifyWindowHierarchyChangeDown(
434 const WindowObserver::HierarchyChangeParams& params);
435 // Notifies this window and its parent hierarchy.
436 void NotifyWindowHierarchyChangeUp(
437 const WindowObserver::HierarchyChangeParams& params);
438 // Notifies this window's observers.
439 void NotifyWindowHierarchyChangeAtReceiver(
440 const WindowObserver::HierarchyChangeParams& params);
442 // Methods implementing visibility change notifications. See WindowObserver
443 // for more details.
444 void NotifyWindowVisibilityChanged(aura::Window* target, bool visible);
445 // Notifies this window's observers. Returns false if |this| was deleted
446 // during the call (by an observer), otherwise true.
447 bool NotifyWindowVisibilityChangedAtReceiver(aura::Window* target,
448 bool visible);
449 // Notifies this window and its child hierarchy. Returns false if
450 // |this| was deleted during the call (by an observer), otherwise
451 // true.
452 bool NotifyWindowVisibilityChangedDown(aura::Window* target, bool visible);
453 // Notifies this window and its parent hierarchy.
454 void NotifyWindowVisibilityChangedUp(aura::Window* target, bool visible);
456 // Notifies this window and its child hierarchy of a transform applied to
457 // |source|.
458 void NotifyAncestorWindowTransformed(Window* source);
460 // Invoked when the bounds of the window changes. This may be invoked directly
461 // by us, or from the closure returned by PrepareForLayerBoundsChange() after
462 // the bounds of the layer has changed. |old_bounds| is the previous bounds.
463 void OnWindowBoundsChanged(const gfx::Rect& old_bounds);
465 // Overridden from ui::LayerDelegate:
466 void OnPaintLayer(const ui::PaintContext& context) override;
467 void OnDelegatedFrameDamage(const gfx::Rect& damage_rect_in_dip) override;
468 base::Closure PrepareForLayerBoundsChange() override;
470 // Overridden from ui::EventTarget:
471 bool CanAcceptEvent(const ui::Event& event) override;
472 EventTarget* GetParentTarget() override;
473 scoped_ptr<ui::EventTargetIterator> GetChildIterator() const override;
474 ui::EventTargeter* GetEventTargeter() override;
475 void ConvertEventToTarget(ui::EventTarget* target,
476 ui::LocatedEvent* event) override;
478 // Updates the layer name based on the window's name and id.
479 void UpdateLayerName();
481 // Returns the first ancestor (starting at |this|) with a layer. |offset| is
482 // set to the offset from |this| to the first ancestor with a layer. |offset|
483 // may be NULL.
484 Window* GetAncestorWithLayer(gfx::Vector2d* offset) {
485 return const_cast<Window*>(
486 const_cast<const Window*>(this)->GetAncestorWithLayer(offset));
488 const Window* GetAncestorWithLayer(gfx::Vector2d* offset) const;
490 // Bounds of this window relative to the parent. This is cached as the bounds
491 // of the Layer and Window are not necessarily the same. In particular bounds
492 // of the Layer are relative to the first ancestor with a Layer, where as this
493 // is relative to the parent Window.
494 gfx::Rect bounds_;
496 WindowTreeHost* host_;
498 ui::wm::WindowType type_;
500 // True if the Window is owned by its parent - i.e. it will be deleted by its
501 // parent during its parents destruction. True is the default.
502 bool owned_by_parent_;
504 WindowDelegate* delegate_;
506 // The Window's parent.
507 Window* parent_;
509 // Child windows. Topmost is last.
510 Windows children_;
512 // The visibility state of the window as set by Show()/Hide(). This may differ
513 // from the visibility of the underlying layer, which may remain visible after
514 // the window is hidden (e.g. to animate its disappearance).
515 bool visible_;
517 int id_;
518 std::string name_;
520 base::string16 title_;
522 // Whether layer is initialized as non-opaque.
523 bool transparent_;
525 scoped_ptr<LayoutManager> layout_manager_;
526 scoped_ptr<ui::EventTargeter> targeter_;
528 void* user_data_;
530 // Makes the window pass all events through to any windows behind it.
531 bool ignore_events_;
533 // See set_hit_test_bounds_override_inner().
534 gfx::Insets hit_test_bounds_override_inner_;
536 ObserverList<WindowObserver, true> observers_;
538 // Value struct to keep the name and deallocator for this property.
539 // Key cannot be used for this purpose because it can be char* or
540 // WindowProperty<>.
541 struct Value {
542 const char* name;
543 int64 value;
544 PropertyDeallocator deallocator;
547 std::map<const void*, Value> prop_map_;
549 DISALLOW_COPY_AND_ASSIGN(Window);
552 } // namespace aura
554 #endif // UI_AURA_WINDOW_H_