testOobeLocalization implementation
[chromium-blink-merge.git] / ui / aura / window.h
blob630becbae7bd5beaebfc6b584deedf5549967aa6
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_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"
32 namespace gfx {
33 class Display;
34 class Transform;
35 class Vector2d;
38 namespace ui {
39 class EventHandler;
40 class Layer;
41 class Texture;
44 namespace aura {
46 class LayoutManager;
47 class RootWindow;
48 class WindowDelegate;
49 class WindowObserver;
51 // TODO(beng): remove once RootWindow is renamed.
52 typedef RootWindow WindowEventDispatcher;
54 // Defined in window_property.h (which we do not include)
55 template<typename T>
56 struct WindowProperty;
58 namespace test {
59 class WindowTestApi;
62 // Aura window implementation. Interesting events are sent to the
63 // WindowDelegate.
64 // TODO(beng): resolve ownership.
65 class AURA_EXPORT Window : public ui::LayerDelegate,
66 public ui::LayerOwner,
67 public ui::EventTarget,
68 public ui::GestureConsumer {
69 public:
70 // Used when stacking windows.
71 enum StackDirection {
72 STACK_ABOVE,
73 STACK_BELOW
76 typedef std::vector<Window*> Windows;
78 explicit Window(WindowDelegate* delegate);
79 virtual ~Window();
81 // Initializes the window. This creates the window's layer.
82 void Init(WindowLayerType layer_type);
84 // Creates a new layer for the window. Erases the layer-owned bounds, so the
85 // caller may wish to set new bounds and other state on the window/layer.
86 // Returns the old layer, which can be used for animations. Caller owns the
87 // memory for the returned layer and must delete it when animation completes.
88 // Returns NULL and does not recreate layer if window does not own its layer.
89 ui::Layer* RecreateLayer() WARN_UNUSED_RESULT;
91 void set_owned_by_parent(bool owned_by_parent) {
92 owned_by_parent_ = owned_by_parent;
94 bool owned_by_parent() const { return owned_by_parent_; }
96 // A type is used to identify a class of Windows and customize behavior such
97 // as event handling and parenting. This field should only be consumed by the
98 // shell -- Aura itself shouldn't contain type-specific logic.
99 ui::wm::WindowType type() const { return type_; }
100 void SetType(ui::wm::WindowType type);
102 int id() const { return id_; }
103 void set_id(int id) { id_ = id; }
105 const std::string& name() const { return name_; }
106 void SetName(const std::string& name);
108 const base::string16 title() const { return title_; }
109 void set_title(const base::string16& title) { title_ = title; }
111 bool transparent() const { return transparent_; }
112 void SetTransparent(bool transparent);
114 WindowDelegate* delegate() { return delegate_; }
115 const WindowDelegate* delegate() const { return delegate_; }
117 const gfx::Rect& bounds() const { return bounds_; }
119 Window* parent() { return parent_; }
120 const Window* parent() const { return parent_; }
122 // Returns the root Window that contains this Window. The root Window is
123 // defined as the Window that has a dispatcher. These functions return NULL if
124 // the Window is contained in a hierarchy that does not have a dispatcher at
125 // its root.
126 virtual Window* GetRootWindow();
127 virtual const Window* GetRootWindow() const;
129 WindowEventDispatcher* GetDispatcher();
130 const WindowEventDispatcher* GetDispatcher() const;
131 void set_dispatcher(WindowEventDispatcher* dispatcher) {
132 dispatcher_ = dispatcher;
134 bool HasDispatcher() const { return !!dispatcher_; }
136 // The Window does not own this object.
137 void set_user_data(void* user_data) { user_data_ = user_data; }
138 void* user_data() const { return user_data_; }
140 // Changes the visibility of the window.
141 void Show();
142 void Hide();
143 // Returns true if this window and all its ancestors are visible.
144 bool IsVisible() const;
145 // Returns the visibility requested by this window. IsVisible() takes into
146 // account the visibility of the layer and ancestors, where as this tracks
147 // whether Show() without a Hide() has been invoked.
148 bool TargetVisibility() const { return visible_; }
150 // Returns the window's bounds in root window's coordinates.
151 gfx::Rect GetBoundsInRootWindow() const;
153 // Returns the window's bounds in screen coordinates.
154 // How the root window's coordinates is mapped to screen's coordinates
155 // is platform dependent and defined in the implementation of the
156 // |aura::client::ScreenPositionClient| interface.
157 gfx::Rect GetBoundsInScreen() const;
159 virtual void SetTransform(const gfx::Transform& transform);
161 // Assigns a LayoutManager to size and place child windows.
162 // The Window takes ownership of the LayoutManager.
163 void SetLayoutManager(LayoutManager* layout_manager);
164 LayoutManager* layout_manager() { return layout_manager_.get(); }
166 void set_event_targeter(scoped_ptr<ui::EventTargeter> targeter) {
167 targeter_ = targeter.Pass();
170 // Changes the bounds of the window. If present, the window's parent's
171 // LayoutManager may adjust the bounds.
172 void SetBounds(const gfx::Rect& new_bounds);
174 // Changes the bounds of the window in the screen coordintates.
175 // If present, the window's parent's LayoutManager may adjust the bounds.
176 void SetBoundsInScreen(const gfx::Rect& new_bounds_in_screen_coords,
177 const gfx::Display& dst_display);
179 // Returns the target bounds of the window. If the window's layer is
180 // not animating, it simply returns the current bounds.
181 gfx::Rect GetTargetBounds() const;
183 // Marks the a portion of window as needing to be painted.
184 void SchedulePaintInRect(const gfx::Rect& rect);
186 // Stacks the specified child of this Window at the front of the z-order.
187 void StackChildAtTop(Window* child);
189 // Stacks |child| above |target|. Does nothing if |child| is already above
190 // |target|. Does not stack on top of windows with NULL layer delegates,
191 // see WindowTest.StackingMadrigal for details.
192 void StackChildAbove(Window* child, Window* target);
194 // Stacks the specified child of this window at the bottom of the z-order.
195 void StackChildAtBottom(Window* child);
197 // Stacks |child| below |target|. Does nothing if |child| is already below
198 // |target|.
199 void StackChildBelow(Window* child, Window* target);
201 // Tree operations.
202 void AddChild(Window* child);
203 void RemoveChild(Window* child);
205 const Windows& children() const { return children_; }
207 // Returns true if this Window contains |other| somewhere in its children.
208 bool Contains(const Window* other) const;
210 // Retrieves the first-level child with the specified id, or NULL if no first-
211 // level child is found matching |id|.
212 Window* GetChildById(int id);
213 const Window* GetChildById(int id) const;
215 // Converts |point| from |source|'s coordinates to |target|'s. If |source| is
216 // NULL, the function returns without modifying |point|. |target| cannot be
217 // NULL.
218 static void ConvertPointToTarget(const Window* source,
219 const Window* target,
220 gfx::Point* point);
222 // Moves the cursor to the specified location relative to the window.
223 virtual void MoveCursorTo(const gfx::Point& point_in_window);
225 // Returns the cursor for the specified point, in window coordinates.
226 gfx::NativeCursor GetCursor(const gfx::Point& point) const;
228 // Sets an 'event filter' for the window. An 'event filter' for a Window is
229 // a pre-target event handler, where the window owns the handler. A window
230 // can have only one such event filter. Setting a new filter removes and
231 // destroys any previously installed filter.
232 void SetEventFilter(ui::EventHandler* event_filter);
234 // Add/remove observer.
235 void AddObserver(WindowObserver* observer);
236 void RemoveObserver(WindowObserver* observer);
237 bool HasObserver(WindowObserver* observer);
239 void set_ignore_events(bool ignore_events) { ignore_events_ = ignore_events; }
240 bool ignore_events() const { return ignore_events_; }
242 // Sets the window to grab hits for mouse and touch to an area extending
243 // -|mouse_insets| and -|touch_insets| pixels outside its bounds. This can be
244 // used to create an invisible non-client area, for example if your windows
245 // have no visible frames but still need to have resize edges.
246 void SetHitTestBoundsOverrideOuter(const gfx::Insets& mouse_insets,
247 const gfx::Insets& touch_insets) {
248 hit_test_bounds_override_outer_mouse_ = mouse_insets;
249 hit_test_bounds_override_outer_touch_ = touch_insets;
252 gfx::Insets hit_test_bounds_override_outer_touch() const {
253 return hit_test_bounds_override_outer_touch_;
256 gfx::Insets hit_test_bounds_override_outer_mouse() const {
257 return hit_test_bounds_override_outer_mouse_;
260 // Sets the window to grab hits for an area extending |insets| pixels inside
261 // its bounds (even if that inner region overlaps a child window). This can be
262 // used to create an invisible non-client area that overlaps the client area.
263 void set_hit_test_bounds_override_inner(const gfx::Insets& insets) {
264 hit_test_bounds_override_inner_ = insets;
266 gfx::Insets hit_test_bounds_override_inner() const {
267 return hit_test_bounds_override_inner_;
270 // Returns true if the |point_in_root| in root window's coordinate falls
271 // within this window's bounds. Returns false if the window is detached
272 // from root window.
273 bool ContainsPointInRoot(const gfx::Point& point_in_root) const;
275 // Returns true if relative-to-this-Window's-origin |local_point| falls
276 // within this Window's bounds.
277 bool ContainsPoint(const gfx::Point& local_point) const;
279 // Returns true if the mouse pointer at relative-to-this-Window's-origin
280 // |local_point| can trigger an event for this Window.
281 // TODO(beng): A Window can supply a hit-test mask to cause some portions of
282 // itself to not trigger events, causing the events to fall through to the
283 // Window behind.
284 bool HitTest(const gfx::Point& local_point);
286 // Returns the Window that most closely encloses |local_point| for the
287 // purposes of event targeting.
288 Window* GetEventHandlerForPoint(const gfx::Point& local_point);
290 // Returns the topmost Window with a delegate containing |local_point|.
291 Window* GetTopWindowContainingPoint(const gfx::Point& local_point);
293 // Returns this window's toplevel window (the highest-up-the-tree anscestor
294 // that has a delegate set). The toplevel window may be |this|.
295 Window* GetToplevelWindow();
297 // Claims or relinquishes the claim to focus.
298 void Focus();
299 void Blur();
301 // Returns true if the Window is currently the focused window.
302 bool HasFocus() const;
304 // Returns true if the Window can be focused.
305 virtual bool CanFocus() const;
307 // Returns true if the Window can receive events.
308 virtual bool CanReceiveEvents() const;
310 // Does a capture on the window. This does nothing if the window isn't showing
311 // (VISIBILITY_SHOWN) or isn't contained in a valid window hierarchy.
312 void SetCapture();
314 // Releases a capture.
315 void ReleaseCapture();
317 // Returns true if this window has capture.
318 bool HasCapture();
320 // Suppresses painting window content by disgarding damaged rect and ignoring
321 // new paint requests. This is a one way operation and there is no way to
322 // reenable painting.
323 void SuppressPaint();
325 // Sets the |value| of the given window |property|. Setting to the default
326 // value (e.g., NULL) removes the property. The caller is responsible for the
327 // lifetime of any object set as a property on the Window.
328 template<typename T>
329 void SetProperty(const WindowProperty<T>* property, T value);
331 // Returns the value of the given window |property|. Returns the
332 // property-specific default value if the property was not previously set.
333 template<typename T>
334 T GetProperty(const WindowProperty<T>* property) const;
336 // Sets the |property| to its default value. Useful for avoiding a cast when
337 // setting to NULL.
338 template<typename T>
339 void ClearProperty(const WindowProperty<T>* property);
341 // NativeWidget::[GS]etNativeWindowProperty use strings as keys, and this is
342 // difficult to change while retaining compatibility with other platforms.
343 // TODO(benrg): Find a better solution.
344 void SetNativeWindowProperty(const char* key, void* value);
345 void* GetNativeWindowProperty(const char* key) const;
347 // Type of a function to delete a property that this window owns.
348 typedef void (*PropertyDeallocator)(int64 value);
350 // Overridden from ui::LayerDelegate:
351 virtual void OnDeviceScaleFactorChanged(float device_scale_factor) OVERRIDE;
353 #if !defined(NDEBUG)
354 // These methods are useful when debugging.
355 std::string GetDebugInfo() const;
356 void PrintWindowHierarchy(int depth) const;
357 #endif
359 protected:
360 // Deletes (or removes if not owned by parent) all child windows. Intended for
361 // use from the destructor.
362 void RemoveOrDestroyChildren();
364 private:
365 friend class test::WindowTestApi;
366 friend class LayoutManager;
367 friend class RootWindow;
368 friend class WindowTargeter;
370 // Called by the public {Set,Get,Clear}Property functions.
371 int64 SetPropertyInternal(const void* key,
372 const char* name,
373 PropertyDeallocator deallocator,
374 int64 value,
375 int64 default_value);
376 int64 GetPropertyInternal(const void* key, int64 default_value) const;
378 // Changes the bounds of the window without condition.
379 void SetBoundsInternal(const gfx::Rect& new_bounds);
381 // Updates the visible state of the layer, but does not make visible-state
382 // specific changes. Called from Show()/Hide().
383 void SetVisible(bool visible);
385 // Schedules a paint for the Window's entire bounds.
386 void SchedulePaint();
388 // Asks the delegate to paint the window and invokes PaintLayerlessChildren()
389 // to paint any children with no layers.
390 void Paint(gfx::Canvas* canvas);
392 // Paints any layerless children to |canvas|.
393 void PaintLayerlessChildren(gfx::Canvas* canvas);
395 // Gets a Window (either this one or a subwindow) containing |local_point|.
396 // If |return_tightest| is true, returns the tightest-containing (i.e.
397 // furthest down the hierarchy) Window containing the point; otherwise,
398 // returns the loosest. If |for_event_handling| is true, then hit-test masks
399 // are honored; otherwise, only bounds checks are performed.
400 Window* GetWindowForPoint(const gfx::Point& local_point,
401 bool return_tightest,
402 bool for_event_handling);
404 // Implementation of RemoveChild(). If |child| is being removed as the result
405 // of an add, |new_parent| is the new parent |child| is going to be parented
406 // to.
407 void RemoveChildImpl(Window* child, Window* new_parent);
409 // If this Window has a layer the layer's parent is set to NULL, otherwise
410 // UnparentLayers() is invoked on all the children. |offset| is the offset
411 // relative to the nearest ancestor with a layer.
412 void UnparentLayers(bool has_layerless_ancestor,
413 const gfx::Vector2d& offset);
415 // If this Window has a layer it is added to |parent| and the origin set to
416 // |offset|. Otherwise this recurses through the children invoking
417 // ReparentLayers(). The net effect is both setting the parent of layers to
418 // |parent| as well as updating bounds of windows with a layerless ancestor.
419 void ReparentLayers(ui::Layer* parent, const gfx::Vector2d& offset);
421 // Offsets the first encountered Windows with layers by |offset|. This
422 // recurses through all layerless Windows, stopping at windows with layers.
423 void OffsetLayerBounds(const gfx::Vector2d& offset);
425 // Called when this window's parent has changed.
426 void OnParentChanged();
428 // The various stacking functions call into this to do the actual stacking.
429 void StackChildRelativeTo(Window* child,
430 Window* target,
431 StackDirection direction);
433 // Invoked from StackChildRelativeTo() to stack the layers appropriately
434 // when stacking |child| relative to |target|.
435 void StackChildLayerRelativeTo(Window* child,
436 Window* target,
437 StackDirection direction);
439 // Called when this window's stacking order among its siblings is changed.
440 void OnStackingChanged();
442 // Notifies observers registered with this Window (and its subtree) when the
443 // Window has been added or is about to be removed from a RootWindow.
444 void NotifyRemovingFromRootWindow();
445 void NotifyAddedToRootWindow();
447 // Methods implementing hierarchy change notifications. See WindowObserver for
448 // more details.
449 void NotifyWindowHierarchyChange(
450 const WindowObserver::HierarchyChangeParams& params);
451 // Notifies this window and its child hierarchy.
452 void NotifyWindowHierarchyChangeDown(
453 const WindowObserver::HierarchyChangeParams& params);
454 // Notifies this window and its parent hierarchy.
455 void NotifyWindowHierarchyChangeUp(
456 const WindowObserver::HierarchyChangeParams& params);
457 // Notifies this window's observers.
458 void NotifyWindowHierarchyChangeAtReceiver(
459 const WindowObserver::HierarchyChangeParams& params);
461 // Methods implementing visibility change notifications. See WindowObserver
462 // for more details.
463 void NotifyWindowVisibilityChanged(aura::Window* target, bool visible);
464 // Notifies this window's observers. Returns false if |this| was deleted
465 // during the call (by an observer), otherwise true.
466 bool NotifyWindowVisibilityChangedAtReceiver(aura::Window* target,
467 bool visible);
468 // Notifies this window and its child hierarchy. Returns false if
469 // |this| was deleted during the call (by an observer), otherwise
470 // true.
471 bool NotifyWindowVisibilityChangedDown(aura::Window* target, bool visible);
472 // Notifies this window and its parent hierarchy.
473 void NotifyWindowVisibilityChangedUp(aura::Window* target, bool visible);
475 // Invoked when the bounds of the window changes. This may be invoked directly
476 // by us, or from the closure returned by PrepareForLayerBoundsChange() after
477 // the bounds of the layer has changed. |old_bounds| is the previous bounds,
478 // and |contained_mouse| is true if the mouse was previously within the
479 // window's bounds.
480 void OnWindowBoundsChanged(const gfx::Rect& old_bounds, bool contained_mouse);
482 // Overridden from ui::LayerDelegate:
483 virtual void OnPaintLayer(gfx::Canvas* canvas) OVERRIDE;
484 virtual base::Closure PrepareForLayerBoundsChange() OVERRIDE;
486 // Overridden from ui::EventTarget:
487 virtual bool CanAcceptEvent(const ui::Event& event) OVERRIDE;
488 virtual EventTarget* GetParentTarget() OVERRIDE;
489 virtual scoped_ptr<ui::EventTargetIterator> GetChildIterator() const OVERRIDE;
490 virtual ui::EventTargeter* GetEventTargeter() OVERRIDE;
491 virtual void ConvertEventToTarget(ui::EventTarget* target,
492 ui::LocatedEvent* event) OVERRIDE;
494 // Updates the layer name with a name based on the window's name and id.
495 void UpdateLayerName(const std::string& name);
497 // Returns true if the mouse is currently within our bounds.
498 bool ContainsMouse();
500 // Returns the first ancestor (starting at |this|) with a layer. |offset| is
501 // set to the offset from |this| to the first ancestor with a layer. |offset|
502 // may be NULL.
503 Window* GetAncestorWithLayer(gfx::Vector2d* offset) {
504 return const_cast<Window*>(
505 const_cast<const Window*>(this)->GetAncestorWithLayer(offset));
507 const Window* GetAncestorWithLayer(gfx::Vector2d* offset) const;
509 // Bounds of this window relative to the parent. This is cached as the bounds
510 // of the Layer and Window are not necessarily the same. In particular bounds
511 // of the Layer are relative to the first ancestor with a Layer, where as this
512 // is relative to the parent Window.
513 gfx::Rect bounds_;
515 WindowEventDispatcher* dispatcher_;
517 ui::wm::WindowType type_;
519 // True if the Window is owned by its parent - i.e. it will be deleted by its
520 // parent during its parents destruction. True is the default.
521 bool owned_by_parent_;
523 WindowDelegate* delegate_;
525 // The Window's parent.
526 Window* parent_;
528 // Child windows. Topmost is last.
529 Windows children_;
531 // The visibility state of the window as set by Show()/Hide(). This may differ
532 // from the visibility of the underlying layer, which may remain visible after
533 // the window is hidden (e.g. to animate its disappearance).
534 bool visible_;
536 int id_;
537 std::string name_;
539 base::string16 title_;
541 // Whether layer is initialized as non-opaque.
542 bool transparent_;
544 scoped_ptr<ui::EventHandler> event_filter_;
545 scoped_ptr<LayoutManager> layout_manager_;
546 scoped_ptr<ui::EventTargeter> targeter_;
548 void* user_data_;
550 // Makes the window pass all events through to any windows behind it.
551 bool ignore_events_;
553 // See set_hit_test_outer_override().
554 gfx::Insets hit_test_bounds_override_outer_mouse_;
555 gfx::Insets hit_test_bounds_override_outer_touch_;
556 gfx::Insets hit_test_bounds_override_inner_;
558 ObserverList<WindowObserver, true> observers_;
560 // Value struct to keep the name and deallocator for this property.
561 // Key cannot be used for this purpose because it can be char* or
562 // WindowProperty<>.
563 struct Value {
564 const char* name;
565 int64 value;
566 PropertyDeallocator deallocator;
569 std::map<const void*, Value> prop_map_;
571 DISALLOW_COPY_AND_ASSIGN(Window);
574 } // namespace aura
576 #endif // UI_AURA_WINDOW_H_