ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / ui / aura / window.h
blobd6d6f7a574a84c4d4be8c62f1174932091ae259b
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/geometry/insets.h"
28 #include "ui/gfx/geometry/rect.h"
29 #include "ui/gfx/native_widget_types.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 TextInputClient;
42 class Texture;
45 namespace aura {
47 class LayoutManager;
48 class WindowDelegate;
49 class WindowObserver;
50 class WindowTreeHost;
52 // Defined in window_property.h (which we do not include)
53 template<typename T>
54 struct WindowProperty;
56 namespace subtle {
57 class PropertyHelper;
60 namespace test {
61 class WindowTestApi;
64 // Aura window implementation. Interesting events are sent to the
65 // WindowDelegate.
66 // TODO(beng): resolve ownership.
67 class AURA_EXPORT Window : public ui::LayerDelegate,
68 public ui::LayerOwner,
69 public ui::EventTarget,
70 public ui::GestureConsumer {
71 public:
72 // Used when stacking windows.
73 enum StackDirection {
74 STACK_ABOVE,
75 STACK_BELOW
78 typedef std::vector<Window*> Windows;
80 explicit Window(WindowDelegate* delegate);
81 ~Window() override;
83 // Initializes the window. This creates the window's layer.
84 void Init(WindowLayerType layer_type);
86 void set_owned_by_parent(bool owned_by_parent) {
87 owned_by_parent_ = owned_by_parent;
89 bool owned_by_parent() const { return owned_by_parent_; }
91 // A type is used to identify a class of Windows and customize behavior such
92 // as event handling and parenting. This field should only be consumed by the
93 // shell -- Aura itself shouldn't contain type-specific logic.
94 ui::wm::WindowType type() const { return type_; }
95 void SetType(ui::wm::WindowType type);
97 int id() const { return id_; }
98 void set_id(int id) { id_ = id; }
100 const std::string& name() const { return name_; }
101 void SetName(const std::string& name);
103 const base::string16 title() const { return title_; }
104 void SetTitle(const base::string16& title);
106 bool transparent() const { return transparent_; }
107 void SetTransparent(bool transparent);
109 // See description in Layer::SetFillsBoundsCompletely.
110 void SetFillsBoundsCompletely(bool fills_bounds);
112 WindowDelegate* delegate() { return delegate_; }
113 const WindowDelegate* delegate() const { return delegate_; }
115 const gfx::Rect& bounds() const { return bounds_; }
117 Window* parent() { return parent_; }
118 const Window* parent() const { return parent_; }
120 // Returns the root Window that contains this Window. The root Window is
121 // defined as the Window that has a dispatcher. These functions return NULL if
122 // the Window is contained in a hierarchy that does not have a dispatcher at
123 // its root.
124 Window* GetRootWindow();
125 const Window* GetRootWindow() const;
127 WindowTreeHost* GetHost();
128 const WindowTreeHost* GetHost() const;
129 void set_host(WindowTreeHost* host) { host_ = host; }
130 bool IsRootWindow() const { return !!host_; }
132 // The Window does not own this object.
133 void set_user_data(void* user_data) { user_data_ = user_data; }
134 void* user_data() const { return user_data_; }
136 // Changes the visibility of the window.
137 void Show();
138 void Hide();
139 // Returns true if this window and all its ancestors are visible.
140 bool IsVisible() const;
141 // Returns the visibility requested by this window. IsVisible() takes into
142 // account the visibility of the layer and ancestors, where as this tracks
143 // whether Show() without a Hide() has been invoked.
144 bool TargetVisibility() const { return visible_; }
146 // Returns the window's bounds in root window's coordinates.
147 gfx::Rect GetBoundsInRootWindow() const;
149 // Returns the window's bounds in screen coordinates.
150 // How the root window's coordinates is mapped to screen's coordinates
151 // is platform dependent and defined in the implementation of the
152 // |aura::client::ScreenPositionClient| interface.
153 gfx::Rect GetBoundsInScreen() const;
155 void SetTransform(const gfx::Transform& transform);
157 // Assigns a LayoutManager to size and place child windows.
158 // The Window takes ownership of the LayoutManager.
159 void SetLayoutManager(LayoutManager* layout_manager);
160 LayoutManager* layout_manager() { return layout_manager_.get(); }
162 // Sets a new event-targeter for the window, and returns the previous
163 // event-targeter.
164 scoped_ptr<ui::EventTargeter> SetEventTargeter(
165 scoped_ptr<ui::EventTargeter> targeter);
167 // Changes the bounds of the window. If present, the window's parent's
168 // LayoutManager may adjust the bounds.
169 void SetBounds(const gfx::Rect& new_bounds);
171 // Changes the bounds of the window in the screen coordintates.
172 // If present, the window's parent's LayoutManager may adjust the bounds.
173 void SetBoundsInScreen(const gfx::Rect& new_bounds_in_screen_coords,
174 const gfx::Display& dst_display);
176 // Returns the target bounds of the window. If the window's layer is
177 // not animating, it simply returns the current bounds.
178 gfx::Rect GetTargetBounds() const;
180 // Marks the a portion of window as needing to be painted.
181 void SchedulePaintInRect(const gfx::Rect& rect);
183 // Stacks the specified child of this Window at the front of the z-order.
184 void StackChildAtTop(Window* child);
186 // Stacks |child| above |target|. Does nothing if |child| is already above
187 // |target|. Does not stack on top of windows with NULL layer delegates,
188 // see WindowTest.StackingMadrigal for details.
189 void StackChildAbove(Window* child, Window* target);
191 // Stacks the specified child of this window at the bottom of the z-order.
192 void StackChildAtBottom(Window* child);
194 // Stacks |child| below |target|. Does nothing if |child| is already below
195 // |target|.
196 void StackChildBelow(Window* child, Window* target);
198 // Tree operations.
199 void AddChild(Window* child);
200 void RemoveChild(Window* child);
202 const Windows& children() const { return children_; }
204 // Returns true if this Window contains |other| somewhere in its children.
205 bool Contains(const Window* other) const;
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);
218 static void ConvertRectToTarget(const Window* source,
219 const Window* target,
220 gfx::Rect* rect);
222 // Returns the focused text input client within this window.
223 // This function does not look at child windows.
224 ui::TextInputClient* GetFocusedTextInputClient();
226 // Moves the cursor to the specified location relative to the window.
227 void MoveCursorTo(const gfx::Point& point_in_window);
229 // Returns the cursor for the specified point, in window coordinates.
230 gfx::NativeCursor GetCursor(const gfx::Point& point) const;
232 // Add/remove observer.
233 void AddObserver(WindowObserver* observer);
234 void RemoveObserver(WindowObserver* observer);
235 bool HasObserver(const WindowObserver* observer) const;
237 void set_ignore_events(bool ignore_events) { ignore_events_ = ignore_events; }
238 bool ignore_events() const { return ignore_events_; }
240 // Sets the window to grab hits for an area extending |insets| pixels inside
241 // its bounds (even if that inner region overlaps a child window). This can be
242 // used to create an invisible non-client area that overlaps the client area.
243 void set_hit_test_bounds_override_inner(const gfx::Insets& insets) {
244 hit_test_bounds_override_inner_ = insets;
246 gfx::Insets hit_test_bounds_override_inner() const {
247 return hit_test_bounds_override_inner_;
250 // Returns true if the |point_in_root| in root window's coordinate falls
251 // within this window's bounds. Returns false if the window is detached
252 // from root window.
253 bool ContainsPointInRoot(const gfx::Point& point_in_root) const;
255 // Returns true if relative-to-this-Window's-origin |local_point| falls
256 // within this Window's bounds.
257 bool ContainsPoint(const gfx::Point& local_point) const;
259 // Returns the Window that most closely encloses |local_point| for the
260 // purposes of event targeting.
261 Window* GetEventHandlerForPoint(const gfx::Point& local_point);
263 // Returns the topmost Window with a delegate containing |local_point|.
264 Window* GetTopWindowContainingPoint(const gfx::Point& local_point);
266 // Returns this window's toplevel window (the highest-up-the-tree anscestor
267 // that has a delegate set). The toplevel window may be |this|.
268 Window* GetToplevelWindow();
270 // Claims or relinquishes the claim to focus.
271 void Focus();
272 void Blur();
274 // Returns true if the Window is currently the focused window.
275 bool HasFocus() const;
277 // Returns true if the Window can be focused.
278 bool CanFocus() const;
280 // Returns true if the Window can receive events.
281 bool CanReceiveEvents() const;
283 // Does a capture on the window. This does nothing if the window isn't showing
284 // (VISIBILITY_SHOWN) or isn't contained in a valid window hierarchy.
285 void SetCapture();
287 // Releases a capture.
288 void ReleaseCapture();
290 // Returns true if this window has capture.
291 bool HasCapture();
293 // Suppresses painting window content by disgarding damaged rect and ignoring
294 // new paint requests. This is a one way operation and there is no way to
295 // reenable painting.
296 void SuppressPaint();
298 // Sets the |value| of the given window |property|. Setting to the default
299 // value (e.g., NULL) removes the property. The caller is responsible for the
300 // lifetime of any object set as a property on the Window.
301 template<typename T>
302 void SetProperty(const WindowProperty<T>* property, T value);
304 // Returns the value of the given window |property|. Returns the
305 // property-specific default value if the property was not previously set.
306 template<typename T>
307 T GetProperty(const WindowProperty<T>* property) const;
309 // Sets the |property| to its default value. Useful for avoiding a cast when
310 // setting to NULL.
311 template<typename T>
312 void ClearProperty(const WindowProperty<T>* property);
314 // NativeWidget::[GS]etNativeWindowProperty use strings as keys, and this is
315 // difficult to change while retaining compatibility with other platforms.
316 // TODO(benrg): Find a better solution.
317 void SetNativeWindowProperty(const char* key, void* value);
318 void* GetNativeWindowProperty(const char* key) const;
320 // Type of a function to delete a property that this window owns.
321 typedef void (*PropertyDeallocator)(int64 value);
323 // Overridden from ui::LayerDelegate:
324 void OnDeviceScaleFactorChanged(float device_scale_factor) override;
326 #if !defined(NDEBUG)
327 // These methods are useful when debugging.
328 std::string GetDebugInfo() const;
329 void PrintWindowHierarchy(int depth) const;
330 #endif
332 // Returns true if there was state needing to be cleaned up.
333 bool CleanupGestureState();
335 protected:
336 // Deletes (or removes if not owned by parent) all child windows. Intended for
337 // use from the destructor.
338 void RemoveOrDestroyChildren();
340 private:
341 friend class test::WindowTestApi;
342 friend class LayoutManager;
343 friend class WindowTargeter;
344 friend class subtle::PropertyHelper;
345 // Called by the public {Set,Get,Clear}Property functions.
346 int64 SetPropertyInternal(const void* key,
347 const char* name,
348 PropertyDeallocator deallocator,
349 int64 value,
350 int64 default_value);
351 int64 GetPropertyInternal(const void* key, int64 default_value) const;
353 // Returns true if the mouse pointer at relative-to-this-Window's-origin
354 // |local_point| can trigger an event for this Window.
355 // TODO(beng): A Window can supply a hit-test mask to cause some portions of
356 // itself to not trigger events, causing the events to fall through to the
357 // Window behind.
358 bool HitTest(const gfx::Point& local_point);
360 // Changes the bounds of the window without condition.
361 void SetBoundsInternal(const gfx::Rect& new_bounds);
363 // Updates the visible state of the layer, but does not make visible-state
364 // specific changes. Called from Show()/Hide().
365 void SetVisible(bool visible);
367 // Schedules a paint for the Window's entire bounds.
368 void SchedulePaint();
370 // Asks the delegate to paint the window and invokes PaintLayerlessChildren()
371 // to paint any children with no layers.
372 void Paint(gfx::Canvas* canvas);
374 // Paints any layerless children to |canvas|.
375 void PaintLayerlessChildren(gfx::Canvas* canvas);
377 // Gets a Window (either this one or a subwindow) containing |local_point|.
378 // If |return_tightest| is true, returns the tightest-containing (i.e.
379 // furthest down the hierarchy) Window containing the point; otherwise,
380 // returns the loosest. If |for_event_handling| is true, then hit-test masks
381 // are honored; otherwise, only bounds checks are performed.
382 Window* GetWindowForPoint(const gfx::Point& local_point,
383 bool return_tightest,
384 bool for_event_handling);
386 // Implementation of RemoveChild(). If |child| is being removed as the result
387 // of an add, |new_parent| is the new parent |child| is going to be parented
388 // to.
389 void RemoveChildImpl(Window* child, Window* new_parent);
391 // If this Window has a layer the layer's parent is set to NULL, otherwise
392 // UnparentLayers() is invoked on all the children. |offset| is the offset
393 // relative to the nearest ancestor with a layer.
394 void UnparentLayers(bool has_layerless_ancestor,
395 const gfx::Vector2d& offset);
397 // If this Window has a layer it is added to |parent| and the origin set to
398 // |offset|. Otherwise this recurses through the children invoking
399 // ReparentLayers(). The net effect is both setting the parent of layers to
400 // |parent| as well as updating bounds of windows with a layerless ancestor.
401 void ReparentLayers(ui::Layer* parent, const gfx::Vector2d& offset);
403 // Offsets the first encountered Windows with layers by |offset|. This
404 // recurses through all layerless Windows, stopping at windows with layers.
405 void OffsetLayerBounds(const gfx::Vector2d& offset);
407 // Called when this window's parent has changed.
408 void OnParentChanged();
410 // The various stacking functions call into this to do the actual stacking.
411 void StackChildRelativeTo(Window* child,
412 Window* target,
413 StackDirection direction);
415 // Invoked from StackChildRelativeTo() to stack the layers appropriately
416 // when stacking |child| relative to |target|.
417 void StackChildLayerRelativeTo(Window* child,
418 Window* target,
419 StackDirection direction);
421 // Called when this window's stacking order among its siblings is changed.
422 void OnStackingChanged();
424 // Notifies observers registered with this Window (and its subtree) when the
425 // Window has been added or is about to be removed from a RootWindow.
426 void NotifyRemovingFromRootWindow(Window* new_root);
427 void NotifyAddedToRootWindow();
429 // Methods implementing hierarchy change notifications. See WindowObserver for
430 // more details.
431 void NotifyWindowHierarchyChange(
432 const WindowObserver::HierarchyChangeParams& params);
433 // Notifies this window and its child hierarchy.
434 void NotifyWindowHierarchyChangeDown(
435 const WindowObserver::HierarchyChangeParams& params);
436 // Notifies this window and its parent hierarchy.
437 void NotifyWindowHierarchyChangeUp(
438 const WindowObserver::HierarchyChangeParams& params);
439 // Notifies this window's observers.
440 void NotifyWindowHierarchyChangeAtReceiver(
441 const WindowObserver::HierarchyChangeParams& params);
443 // Methods implementing visibility change notifications. See WindowObserver
444 // for more details.
445 void NotifyWindowVisibilityChanged(aura::Window* target, bool visible);
446 // Notifies this window's observers. Returns false if |this| was deleted
447 // during the call (by an observer), otherwise true.
448 bool NotifyWindowVisibilityChangedAtReceiver(aura::Window* target,
449 bool visible);
450 // Notifies this window and its child hierarchy. Returns false if
451 // |this| was deleted during the call (by an observer), otherwise
452 // true.
453 bool NotifyWindowVisibilityChangedDown(aura::Window* target, bool visible);
454 // Notifies this window and its parent hierarchy.
455 void NotifyWindowVisibilityChangedUp(aura::Window* target, bool visible);
457 // Notifies this window and its child hierarchy of a transform applied to
458 // |source|.
459 void NotifyAncestorWindowTransformed(Window* source);
461 // Invoked when the bounds of the window changes. This may be invoked directly
462 // by us, or from the closure returned by PrepareForLayerBoundsChange() after
463 // the bounds of the layer has changed. |old_bounds| is the previous bounds.
464 void OnWindowBoundsChanged(const gfx::Rect& old_bounds);
466 // Overridden from ui::LayerDelegate:
467 void OnPaintLayer(gfx::Canvas* canvas) override;
468 void OnDelegatedFrameDamage(const gfx::Rect& damage_rect_in_dip) override;
469 base::Closure PrepareForLayerBoundsChange() override;
471 // Overridden from ui::EventTarget:
472 bool CanAcceptEvent(const ui::Event& event) override;
473 EventTarget* GetParentTarget() override;
474 scoped_ptr<ui::EventTargetIterator> GetChildIterator() const override;
475 ui::EventTargeter* GetEventTargeter() override;
476 void ConvertEventToTarget(ui::EventTarget* target,
477 ui::LocatedEvent* event) override;
479 // Updates the layer name based on the window's name and id.
480 void UpdateLayerName();
482 // Returns the first ancestor (starting at |this|) with a layer. |offset| is
483 // set to the offset from |this| to the first ancestor with a layer. |offset|
484 // may be NULL.
485 Window* GetAncestorWithLayer(gfx::Vector2d* offset) {
486 return const_cast<Window*>(
487 const_cast<const Window*>(this)->GetAncestorWithLayer(offset));
489 const Window* GetAncestorWithLayer(gfx::Vector2d* offset) const;
491 // Bounds of this window relative to the parent. This is cached as the bounds
492 // of the Layer and Window are not necessarily the same. In particular bounds
493 // of the Layer are relative to the first ancestor with a Layer, where as this
494 // is relative to the parent Window.
495 gfx::Rect bounds_;
497 WindowTreeHost* host_;
499 ui::wm::WindowType type_;
501 // True if the Window is owned by its parent - i.e. it will be deleted by its
502 // parent during its parents destruction. True is the default.
503 bool owned_by_parent_;
505 WindowDelegate* delegate_;
507 // The Window's parent.
508 Window* parent_;
510 // Child windows. Topmost is last.
511 Windows children_;
513 // The visibility state of the window as set by Show()/Hide(). This may differ
514 // from the visibility of the underlying layer, which may remain visible after
515 // the window is hidden (e.g. to animate its disappearance).
516 bool visible_;
518 int id_;
519 std::string name_;
521 base::string16 title_;
523 // Whether layer is initialized as non-opaque.
524 bool transparent_;
526 scoped_ptr<LayoutManager> layout_manager_;
527 scoped_ptr<ui::EventTargeter> targeter_;
529 void* user_data_;
531 // Makes the window pass all events through to any windows behind it.
532 bool ignore_events_;
534 // See set_hit_test_bounds_override_inner().
535 gfx::Insets hit_test_bounds_override_inner_;
537 ObserverList<WindowObserver, true> observers_;
539 // Value struct to keep the name and deallocator for this property.
540 // Key cannot be used for this purpose because it can be char* or
541 // WindowProperty<>.
542 struct Value {
543 const char* name;
544 int64 value;
545 PropertyDeallocator deallocator;
548 std::map<const void*, Value> prop_map_;
550 DISALLOW_COPY_AND_ASSIGN(Window);
553 } // namespace aura
555 #endif // UI_AURA_WINDOW_H_