Fix build break
[chromium-blink-merge.git] / ui / aura / window.h
blob1847d25fdb753476292652162b1d3253d65ddbb3
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/string16.h"
17 #include "ui/aura/aura_export.h"
18 #include "ui/aura/client/window_types.h"
19 #include "ui/aura/window_observer.h"
20 #include "ui/base/events/event_constants.h"
21 #include "ui/base/events/event_target.h"
22 #include "ui/base/gestures/gesture_types.h"
23 #include "ui/compositor/layer_animator.h"
24 #include "ui/compositor/layer_delegate.h"
25 #include "ui/compositor/layer_owner.h"
26 #include "ui/compositor/layer_type.h"
27 #include "ui/gfx/insets.h"
28 #include "ui/gfx/native_widget_types.h"
29 #include "ui/gfx/rect.h"
31 namespace gfx {
32 class Display;
33 class Transform;
36 namespace ui {
37 class EventHandler;
38 class Layer;
39 class Texture;
42 namespace aura {
44 class LayoutManager;
45 class RootWindow;
46 class WindowDelegate;
47 class WindowObserver;
49 // Defined in window_property.h (which we do not include)
50 template<typename T>
51 struct WindowProperty;
53 namespace test {
54 class WindowTestApi;
57 // Aura window implementation. Interesting events are sent to the
58 // WindowDelegate.
59 // TODO(beng): resolve ownership.
60 class AURA_EXPORT Window : public ui::LayerDelegate,
61 public ui::LayerOwner,
62 public ui::EventTarget,
63 public ui::GestureConsumer {
64 public:
65 typedef std::vector<Window*> Windows;
67 explicit Window(WindowDelegate* delegate);
68 virtual ~Window();
70 // Initializes the window. This creates the window's layer.
71 void Init(ui::LayerType layer_type);
73 // Creates a new layer for the window. Erases the layer-owned bounds, so the
74 // caller may wish to set new bounds and other state on the window/layer.
75 // Returns the old layer, which can be used for animations. Caller owns the
76 // memory for the returned layer and must delete it when animation completes.
77 // Returns NULL and does not recreate layer if window does not own its layer.
78 ui::Layer* RecreateLayer() WARN_UNUSED_RESULT;
80 void set_owned_by_parent(bool owned_by_parent) {
81 owned_by_parent_ = owned_by_parent;
84 // A type is used to identify a class of Windows and customize behavior such
85 // as event handling and parenting. This field should only be consumed by the
86 // shell -- Aura itself shouldn't contain type-specific logic.
87 client::WindowType type() const { return type_; }
88 void SetType(client::WindowType type);
90 int id() const { return id_; }
91 void set_id(int id) { id_ = id; }
93 const std::string& name() const { return name_; }
94 void SetName(const std::string& name);
96 const string16 title() const { return title_; }
97 void set_title(const string16& title) { title_ = title; }
99 bool transparent() const { return transparent_; }
100 void SetTransparent(bool transparent);
102 WindowDelegate* delegate() { return delegate_; }
104 const gfx::Rect& bounds() const;
106 Window* parent() { return parent_; }
107 const Window* parent() const { return parent_; }
109 // Returns the RootWindow that contains this Window or NULL if the Window is
110 // not contained by a RootWindow.
111 virtual RootWindow* GetRootWindow();
112 virtual const RootWindow* GetRootWindow() const;
114 // The Window does not own this object.
115 void set_user_data(void* user_data) { user_data_ = user_data; }
116 void* user_data() const { return user_data_; }
118 // Changes the visibility of the window.
119 void Show();
120 void Hide();
121 // Returns true if this window and all its ancestors are visible.
122 bool IsVisible() const;
123 // Returns the visibility requested by this window. IsVisible() takes into
124 // account the visibility of the layer and ancestors, where as this tracks
125 // whether Show() without a Hide() has been invoked.
126 bool TargetVisibility() const { return visible_; }
128 // Returns the window's bounds in root window's coordinates.
129 gfx::Rect GetBoundsInRootWindow() const;
131 // Returns the window's bounds in screen coordinates.
132 // How the root window's coordinates is mapped to screen's coordinates
133 // is platform dependent and defined in the implementation of the
134 // |aura::client::ScreenPositionClient| interface.
135 gfx::Rect GetBoundsInScreen() const;
137 virtual void SetTransform(const gfx::Transform& transform);
139 // Assigns a LayoutManager to size and place child windows.
140 // The Window takes ownership of the LayoutManager.
141 void SetLayoutManager(LayoutManager* layout_manager);
142 LayoutManager* layout_manager() { return layout_manager_.get(); }
144 // Changes the bounds of the window. If present, the window's parent's
145 // LayoutManager may adjust the bounds.
146 void SetBounds(const gfx::Rect& new_bounds);
148 // Changes the bounds of the window in the screen coordintates.
149 // If present, the window's parent's LayoutManager may adjust the bounds.
150 void SetBoundsInScreen(const gfx::Rect& new_bounds_in_screen_coords,
151 const gfx::Display& dst_display);
153 // Returns the target bounds of the window. If the window's layer is
154 // not animating, it simply returns the current bounds.
155 gfx::Rect GetTargetBounds() const;
157 // Marks the a portion of window as needing to be painted.
158 void SchedulePaintInRect(const gfx::Rect& rect);
160 // Assigns a new external texture to the window's layer.
161 void SetExternalTexture(ui::Texture* texture);
163 // Places this window per |root_window|'s stacking client. The final location
164 // may be a RootWindow other than the one passed in. |root_window| may not be
165 // NULL. |bounds_in_screen| may be empty; it is more optional context that
166 // may, but isn't necessarily used.
167 void SetDefaultParentByRootWindow(RootWindow* root_window,
168 const gfx::Rect& bounds_in_screen);
170 // Stacks the specified child of this Window at the front of the z-order.
171 void StackChildAtTop(Window* child);
173 // Stacks |child| above |target|. Does nothing if |child| is already above
174 // |target|. Does not stack on top of windows with NULL layer delegates,
175 // see WindowTest.StackingMadrigal for details.
176 void StackChildAbove(Window* child, Window* target);
178 // Stacks |child| below |target|. Does nothing if |child| is already below
179 // |target|.
180 void StackChildBelow(Window* child, Window* target);
182 // Tree operations.
183 void AddChild(Window* child);
184 void RemoveChild(Window* child);
186 const Windows& children() const { return children_; }
188 // Returns true if this Window contains |other| somewhere in its children.
189 bool Contains(const Window* other) const;
191 // Adds or removes |child| as a transient child of this window. Transient
192 // children get the following behavior:
193 // . The transient parent destroys any transient children when it is
194 // destroyed. This means a transient child is destroyed if either its parent
195 // or transient parent is destroyed.
196 // . If a transient child and its transient parent share the same parent, then
197 // transient children are always ordered above the transient parent.
198 // Transient windows are typically used for popups and menus.
199 void AddTransientChild(Window* child);
200 void RemoveTransientChild(Window* child);
202 const Windows& transient_children() const { return transient_children_; }
204 Window* transient_parent() { return transient_parent_; }
205 const Window* transient_parent() const { return transient_parent_; }
207 // Retrieves the first-level child with the specified id, or NULL if no first-
208 // level child is found matching |id|.
209 Window* GetChildById(int id);
210 const Window* GetChildById(int id) const;
212 // Converts |point| from |source|'s coordinates to |target|'s. If |source| is
213 // NULL, the function returns without modifying |point|. |target| cannot be
214 // NULL.
215 static void ConvertPointToTarget(const Window* source,
216 const Window* target,
217 gfx::Point* point);
219 // Moves the cursor to the specified location relative to the window.
220 virtual void MoveCursorTo(const gfx::Point& point_in_window);
222 // Returns the cursor for the specified point, in window coordinates.
223 gfx::NativeCursor GetCursor(const gfx::Point& point) const;
225 // Sets an 'event filter' for the window. An 'event filter' for a Window is
226 // a pre-target event handler, where the window owns the handler. A window
227 // can have only one such event filter. Setting a new filter removes and
228 // destroys any previously installed filter.
229 void SetEventFilter(ui::EventHandler* event_filter);
231 // Add/remove observer.
232 void AddObserver(WindowObserver* observer);
233 void RemoveObserver(WindowObserver* observer);
234 bool HasObserver(WindowObserver* observer);
236 void set_ignore_events(bool ignore_events) { ignore_events_ = ignore_events; }
238 // Sets the window to grab hits for an area extending -|insets| pixels outside
239 // its bounds. This can be used to create an invisible non-client area, for
240 // example if your windows have no visible frames but still need to have
241 // resize edges. It is possible to set a larger hit-region for touch-events.
242 void SetHitTestBoundsOverrideOuter(const gfx::Insets& mouse_insets,
243 int touch_scale) {
244 hit_test_bounds_override_outer_mouse_ = mouse_insets;
245 hit_test_bounds_override_outer_touch_ = mouse_insets.Scale(touch_scale);
248 gfx::Insets hit_test_bounds_override_outer_mouse() const {
249 return hit_test_bounds_override_outer_mouse_;
252 // Sets the window to grab hits for an area extending |insets| pixels inside
253 // its bounds (even if that inner region overlaps a child window). This can be
254 // used to create an invisible non-client area that overlaps the client area.
255 void set_hit_test_bounds_override_inner(const gfx::Insets& insets) {
256 hit_test_bounds_override_inner_ = insets;
258 gfx::Insets hit_test_bounds_override_inner() const {
259 return hit_test_bounds_override_inner_;
262 // Returns true if the |point_in_root| in root window's coordinate falls
263 // within this window's bounds. Returns false if the window is detached
264 // from root window.
265 bool ContainsPointInRoot(const gfx::Point& point_in_root) const;
267 // Returns true if relative-to-this-Window's-origin |local_point| falls
268 // within this Window's bounds.
269 bool ContainsPoint(const gfx::Point& local_point) const;
271 // Returns true if the mouse pointer at relative-to-this-Window's-origin
272 // |local_point| can trigger an event for this Window.
273 // TODO(beng): A Window can supply a hit-test mask to cause some portions of
274 // itself to not trigger events, causing the events to fall through to the
275 // Window behind.
276 bool HitTest(const gfx::Point& local_point);
278 // Returns the Window that most closely encloses |local_point| for the
279 // purposes of event targeting.
280 Window* GetEventHandlerForPoint(const gfx::Point& local_point);
282 // Returns the topmost Window with a delegate containing |local_point|.
283 Window* GetTopWindowContainingPoint(const gfx::Point& local_point);
285 // Returns this window's toplevel window (the highest-up-the-tree anscestor
286 // that has a delegate set). The toplevel window may be |this|.
287 Window* GetToplevelWindow();
289 // Claims or relinquishes the claim to focus.
290 void Focus();
291 void Blur();
293 // Returns true if the Window is currently the focused window.
294 bool HasFocus() const;
296 // Returns true if the Window can be focused.
297 virtual bool CanFocus() const;
299 // Returns true if the Window can receive events.
300 virtual bool CanReceiveEvents() const;
302 // Does a capture on the window. This does nothing if the window isn't showing
303 // (VISIBILITY_SHOWN) or isn't contained in a valid window hierarchy.
304 void SetCapture();
306 // Releases a capture.
307 void ReleaseCapture();
309 // Returns true if this window has capture.
310 bool HasCapture();
312 // Suppresses painting window content by disgarding damaged rect and ignoring
313 // new paint requests.
314 void SuppressPaint();
316 // Sets the |value| of the given window |property|. Setting to the default
317 // value (e.g., NULL) removes the property. The caller is responsible for the
318 // lifetime of any object set as a property on the Window.
319 template<typename T>
320 void SetProperty(const WindowProperty<T>* property, T value);
322 // Returns the value of the given window |property|. Returns the
323 // property-specific default value if the property was not previously set.
324 template<typename T>
325 T GetProperty(const WindowProperty<T>* property) const;
327 // Sets the |property| to its default value. Useful for avoiding a cast when
328 // setting to NULL.
329 template<typename T>
330 void ClearProperty(const WindowProperty<T>* property);
332 // NativeWidget::[GS]etNativeWindowProperty use strings as keys, and this is
333 // difficult to change while retaining compatibility with other platforms.
334 // TODO(benrg): Find a better solution.
335 void SetNativeWindowProperty(const char* key, void* value);
336 void* GetNativeWindowProperty(const char* key) const;
338 // Type of a function to delete a property that this window owns.
339 typedef void (*PropertyDeallocator)(int64 value);
341 // Overridden from ui::LayerDelegate:
342 virtual void OnDeviceScaleFactorChanged(float device_scale_factor) OVERRIDE;
344 #ifndef NDEBUG
345 // These methods are useful when debugging.
346 std::string GetDebugInfo() const;
347 void PrintWindowHierarchy(int depth) const;
348 #endif
350 private:
351 friend class test::WindowTestApi;
352 friend class LayoutManager;
354 // Used when stacking windows.
355 enum StackDirection {
356 STACK_ABOVE,
357 STACK_BELOW
360 // Called by the public {Set,Get,Clear}Property functions.
361 int64 SetPropertyInternal(const void* key,
362 const char* name,
363 PropertyDeallocator deallocator,
364 int64 value,
365 int64 default_value);
366 int64 GetPropertyInternal(const void* key, int64 default_value) const;
368 // Changes the bounds of the window without condition.
369 void SetBoundsInternal(const gfx::Rect& new_bounds);
371 // Updates the visible state of the layer, but does not make visible-state
372 // specific changes. Called from Show()/Hide().
373 void SetVisible(bool visible);
375 // Schedules a paint for the Window's entire bounds.
376 void SchedulePaint();
378 // Gets a Window (either this one or a subwindow) containing |local_point|.
379 // If |return_tightest| is true, returns the tightest-containing (i.e.
380 // furthest down the hierarchy) Window containing the point; otherwise,
381 // returns the loosest. If |for_event_handling| is true, then hit-test masks
382 // are honored; otherwise, only bounds checks are performed.
383 Window* GetWindowForPoint(const gfx::Point& local_point,
384 bool return_tightest,
385 bool for_event_handling);
387 // Implementation of RemoveChild(). If |child| is being removed as the result
388 // of an add, |new_parent| is the new parent |child| is going to be parented
389 // to.
390 void RemoveChildImpl(Window* child, Window* new_parent);
392 // Called when this window's parent has changed.
393 void OnParentChanged();
395 // Determines the real location for stacking |child| and invokes
396 // StackChildRelativeToImpl().
397 void StackChildRelativeTo(Window* child,
398 Window* target,
399 StackDirection direction);
401 // Implementation of StackChildRelativeTo().
402 void StackChildRelativeToImpl(Window* child,
403 Window* target,
404 StackDirection direction);
406 // Called when this window's stacking order among its siblings is changed.
407 void OnStackingChanged();
409 // Notifies observers registered with this Window (and its subtree) when the
410 // Window has been added or is about to be removed from a RootWindow.
411 void NotifyRemovingFromRootWindow();
412 void NotifyAddedToRootWindow();
414 // Methods implementing hierarchy change notifications. See WindowObserver for
415 // more details.
416 void NotifyWindowHierarchyChange(
417 const WindowObserver::HierarchyChangeParams& params);
418 // Notifies this window and its child hierarchy.
419 void NotifyWindowHierarchyChangeDown(
420 const WindowObserver::HierarchyChangeParams& params);
421 // Notifies this window and its parent hierarchy.
422 void NotifyWindowHierarchyChangeUp(
423 const WindowObserver::HierarchyChangeParams& params);
424 // Notifies this window's observers.
425 void NotifyWindowHierarchyChangeAtReceiver(
426 const WindowObserver::HierarchyChangeParams& params);
428 // Methods implementing visibility change notifications. See WindowObserver
429 // for more details.
430 void NotifyWindowVisibilityChanged(aura::Window* target, bool visible);
431 // Notifies this window's observers. Returns false if |this| was deleted
432 // during the call (by an observer), otherwise true.
433 bool NotifyWindowVisibilityChangedAtReceiver(aura::Window* target,
434 bool visible);
435 // Notifies this window and its child hierarchy. Returns false if
436 // |this| was deleted during the call (by an observer), otherwise
437 // true.
438 bool NotifyWindowVisibilityChangedDown(aura::Window* target, bool visible);
439 // Notifies this window and its parent hierarchy.
440 void NotifyWindowVisibilityChangedUp(aura::Window* target, bool visible);
442 // Invoked from the closure returned by PrepareForLayerBoundsChange() after
443 // the bounds of the layer has changed. |old_bounds| is the previous bounds of
444 // the layer, and |contained_mouse| is true if the mouse was previously within
445 // the window's bounds.
446 void OnLayerBoundsChanged(const gfx::Rect& old_bounds, bool contained_mouse);
448 // Overridden from ui::LayerDelegate:
449 virtual void OnPaintLayer(gfx::Canvas* canvas) OVERRIDE;
450 virtual base::Closure PrepareForLayerBoundsChange() OVERRIDE;
452 // Overridden from ui::EventTarget:
453 virtual bool CanAcceptEvent(const ui::Event& event) OVERRIDE;
454 virtual EventTarget* GetParentTarget() OVERRIDE;
456 // Updates the layer name with a name based on the window's name and id.
457 void UpdateLayerName(const std::string& name);
459 // Returns true if the mouse is currently within our bounds.
460 bool ContainsMouse();
462 client::WindowType type_;
464 // True if the Window is owned by its parent - i.e. it will be deleted by its
465 // parent during its parents destruction. True is the default.
466 bool owned_by_parent_;
468 WindowDelegate* delegate_;
470 // The Window's parent.
471 Window* parent_;
473 // Child windows. Topmost is last.
474 Windows children_;
476 // Transient windows.
477 Windows transient_children_;
479 Window* transient_parent_;
481 // The visibility state of the window as set by Show()/Hide(). This may differ
482 // from the visibility of the underlying layer, which may remain visible after
483 // the window is hidden (e.g. to animate its disappearance).
484 bool visible_;
486 int id_;
487 std::string name_;
489 string16 title_;
491 // Whether layer is initialized as non-opaque.
492 bool transparent_;
494 scoped_ptr<ui::EventHandler> event_filter_;
495 scoped_ptr<LayoutManager> layout_manager_;
497 void* user_data_;
499 // Makes the window pass all events through to any windows behind it.
500 bool ignore_events_;
502 // See set_hit_test_outer_override().
503 gfx::Insets hit_test_bounds_override_outer_mouse_;
504 gfx::Insets hit_test_bounds_override_outer_touch_;
505 gfx::Insets hit_test_bounds_override_inner_;
507 ObserverList<WindowObserver> observers_;
509 // Value struct to keep the name and deallocator for this property.
510 // Key cannot be used for this purpose because it can be char* or
511 // WindowProperty<>.
512 struct Value {
513 const char* name;
514 int64 value;
515 PropertyDeallocator deallocator;
518 std::map<const void*, Value> prop_map_;
520 DISALLOW_COPY_AND_ASSIGN(Window);
523 } // namespace aura
525 #endif // UI_AURA_WINDOW_H_