- add sources.
[platform/framework/web/crosswalk.git] / src / ash / display / screen_position_controller.cc
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.
4
5 #include "ash/display/screen_position_controller.h"
6
7 #include "ash/display/display_controller.h"
8 #include "ash/root_window_controller.h"
9 #include "ash/shell.h"
10 #include "ash/shell_window_ids.h"
11 #include "ash/wm/coordinate_conversion.h"
12 #include "ash/wm/system_modal_container_layout_manager.h"
13 #include "ash/wm/window_properties.h"
14 #include "ui/aura/client/activation_client.h"
15 #include "ui/aura/client/capture_client.h"
16 #include "ui/aura/client/focus_client.h"
17 #include "ui/aura/root_window.h"
18 #include "ui/aura/window_tracker.h"
19 #include "ui/compositor/dip_util.h"
20 #include "ui/gfx/display.h"
21 #include "ui/gfx/screen.h"
22
23 namespace ash {
24 namespace {
25
26 // Return true if the window or its ancestor has |kStayInSameRootWindowkey|
27 // property.
28 bool ShouldStayInSameRootWindow(const aura::Window* window) {
29   return window &&
30       (window->GetProperty(internal::kStayInSameRootWindowKey) ||
31        ShouldStayInSameRootWindow(window->parent()));
32 }
33
34 // Move all transient children to |dst_root|, including the ones in
35 // the child windows and transient children of the transient children.
36 void MoveAllTransientChildrenToNewRoot(const gfx::Display& display,
37                                        aura::Window* window) {
38   aura::Window* dst_root = Shell::GetInstance()->display_controller()->
39       GetRootWindowForDisplayId(display.id());
40   aura::Window::Windows transient_children = window->transient_children();
41   for (aura::Window::Windows::iterator iter = transient_children.begin();
42        iter != transient_children.end(); ++iter) {
43     aura::Window* transient_child = *iter;
44     int container_id = transient_child->parent()->id();
45     DCHECK_GE(container_id, 0);
46     aura::Window* container = Shell::GetContainer(dst_root, container_id);
47     gfx::Rect parent_bounds_in_screen = transient_child->GetBoundsInScreen();
48     container->AddChild(transient_child);
49     transient_child->SetBoundsInScreen(parent_bounds_in_screen, display);
50
51     // Transient children may have transient children.
52     MoveAllTransientChildrenToNewRoot(display, transient_child);
53   }
54   // Move transient children of the child windows if any.
55   aura::Window::Windows children = window->children();
56   for (aura::Window::Windows::iterator iter = children.begin();
57        iter != children.end(); ++iter)
58     MoveAllTransientChildrenToNewRoot(display, *iter);
59 }
60
61 // Finds the root window at |location| in |window|'s coordinates and returns a
62 // pair of root window and location in that root window's coordinates. The
63 // function usually returns |window->GetRootWindow()|, but if the mouse pointer
64 // is moved outside the |window|'s root while the mouse is captured, it returns
65 // the other root window.
66 std::pair<aura::RootWindow*, gfx::Point> GetRootWindowRelativeToWindow(
67     aura::Window* window,
68     const gfx::Point& location) {
69   aura::Window* root_window = window->GetRootWindow();
70   gfx::Point location_in_root(location);
71   aura::Window::ConvertPointToTarget(window, root_window, &location_in_root);
72
73 #if defined(USE_X11)
74   if (!root_window->ContainsPointInRoot(location_in_root)) {
75     // This conversion is necessary to deal with X's passive input
76     // grab while dragging window. For example, if we have two
77     // displays, say 1000x1000 (primary) and 500x500 (extended one
78     // on the right), and start dragging a window at (999, 123), and
79     // then move the pointer to the right, the pointer suddenly
80     // warps to the extended display. The destination is (0, 123) in
81     // the secondary root window's coordinates, or (1000, 123) in
82     // the screen coordinates. However, since the mouse is captured
83     // by X during drag, a weird LocatedEvent, something like (0, 1123)
84     // in the *primary* root window's coordinates, is sent to Chrome
85     // (Remember that in the native X11 world, the two root windows
86     // are always stacked vertically regardless of the display
87     // layout in Ash). We need to figure out that (0, 1123) in the
88     // primary root window's coordinates is actually (0, 123) in the
89     // extended root window's coordinates.
90
91     gfx::Point location_in_native(location_in_root);
92     root_window->GetDispatcher()->ConvertPointToNativeScreen(
93         &location_in_native);
94
95     Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
96     for (size_t i = 0; i < root_windows.size(); ++i) {
97       const gfx::Rect native_bounds(
98           root_windows[i]->GetHostOrigin(),
99           root_windows[i]->GetHostSize());  // in px.
100       if (native_bounds.Contains(location_in_native)) {
101         root_window = root_windows[i];
102         location_in_root = location_in_native;
103         root_window->GetDispatcher()->ConvertPointFromNativeScreen(
104             &location_in_root);
105         break;
106       }
107     }
108   }
109 #else
110   // TODO(yusukes): Support non-X11 platforms if necessary.
111 #endif
112
113   return std::make_pair(root_window->GetDispatcher(), location_in_root);
114 }
115
116 }  // namespace
117
118 namespace internal {
119
120 void ScreenPositionController::ConvertPointToScreen(
121     const aura::Window* window,
122     gfx::Point* point) {
123   const aura::Window* root = window->GetRootWindow();
124   aura::Window::ConvertPointToTarget(window, root, point);
125   const gfx::Point display_origin = Shell::GetScreen()->GetDisplayNearestWindow(
126       const_cast<aura::Window*>(root)).bounds().origin();
127   point->Offset(display_origin.x(), display_origin.y());
128 }
129
130 void ScreenPositionController::ConvertPointFromScreen(
131     const aura::Window* window,
132     gfx::Point* point) {
133   const aura::Window* root = window->GetRootWindow();
134   const gfx::Point display_origin = Shell::GetScreen()->GetDisplayNearestWindow(
135       const_cast<aura::Window*>(root)).bounds().origin();
136   point->Offset(-display_origin.x(), -display_origin.y());
137   aura::Window::ConvertPointToTarget(root, window, point);
138 }
139
140 void ScreenPositionController::ConvertHostPointToScreen(
141     aura::Window* root_window,
142     gfx::Point* point) {
143   aura::Window* root = root_window->GetRootWindow();
144   root->GetDispatcher()->ConvertPointFromHost(point);
145   std::pair<aura::RootWindow*, gfx::Point> pair =
146       GetRootWindowRelativeToWindow(root, *point);
147   *point = pair.second;
148   ConvertPointToScreen(pair.first, point);
149 }
150
151 void ScreenPositionController::SetBounds(aura::Window* window,
152                                          const gfx::Rect& bounds,
153                                          const gfx::Display& display) {
154   DCHECK_NE(-1, display.id());
155   if (!window->parent()->GetProperty(internal::kUsesScreenCoordinatesKey)) {
156     window->SetBounds(bounds);
157     return;
158   }
159
160   // Don't move a window to other root window if:
161   // a) the window is a transient window. It moves when its
162   //    transient_parent moves.
163   // b) if the window or its ancestor has kStayInSameRootWindowkey. It's
164   //    intentionally kept in the same root window even if the bounds is
165   //    outside of the display.
166   if (!window->transient_parent() &&
167       !ShouldStayInSameRootWindow(window)) {
168     aura::Window* dst_root =
169         Shell::GetInstance()->display_controller()->GetRootWindowForDisplayId(
170             display.id());
171     DCHECK(dst_root);
172     aura::Window* dst_container = NULL;
173     if (dst_root != window->GetRootWindow()) {
174       int container_id = window->parent()->id();
175       // All containers that uses screen coordinates must have valid window ids.
176       DCHECK_GE(container_id, 0);
177       // Don't move modal background.
178       if (!SystemModalContainerLayoutManager::IsModalBackground(window))
179         dst_container = Shell::GetContainer(dst_root, container_id);
180     }
181
182     if (dst_container && window->parent() != dst_container) {
183       aura::Window* focused = aura::client::GetFocusClient(window)->
184           GetFocusedWindow();
185       aura::client::ActivationClient* activation_client =
186           aura::client::GetActivationClient(window->GetRootWindow());
187       aura::Window* active = activation_client->GetActiveWindow();
188
189       aura::WindowTracker tracker;
190       if (focused)
191         tracker.Add(focused);
192       if (active && focused != active)
193         tracker.Add(active);
194
195       dst_container->AddChild(window);
196
197       MoveAllTransientChildrenToNewRoot(display, window);
198
199       // Restore focused/active window.
200       if (tracker.Contains(focused)) {
201         aura::client::GetFocusClient(window)->FocusWindow(focused);
202         // TODO(beng): replace with GetRootWindow().
203         ash::Shell::GetInstance()->set_target_root_window(
204             focused->GetDispatcher());
205       } else if (tracker.Contains(active)) {
206         activation_client->ActivateWindow(active);
207       }
208     }
209   }
210
211   gfx::Point origin(bounds.origin());
212   const gfx::Point display_origin = Shell::GetScreen()->GetDisplayNearestWindow(
213       window).bounds().origin();
214   origin.Offset(-display_origin.x(), -display_origin.y());
215   window->SetBounds(gfx::Rect(origin, bounds.size()));
216 }
217
218 }  // internal
219 }  // ash