Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / ui / views / widget / desktop_aura / desktop_screen_position_client.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 "ui/views/widget/desktop_aura/desktop_screen_position_client.h"
6
7 #include "ui/views/widget/desktop_aura/desktop_native_widget_aura.h"
8
9 namespace views {
10
11 namespace {
12
13 // Returns true if bounds passed to window in SetBounds should be treated as
14 // though they are in screen coordinates.
15 bool PositionWindowInScreenCoordinates(aura::Window* window) {
16   if (window->type() == ui::wm::WINDOW_TYPE_POPUP)
17     return true;
18
19   Widget* widget = Widget::GetWidgetForNativeView(window);
20   return widget && widget->is_top_level();
21 }
22
23 }  // namespace
24
25 DesktopScreenPositionClient::DesktopScreenPositionClient(
26     aura::Window* root_window)
27     : wm::DefaultScreenPositionClient(), root_window_(root_window) {
28   aura::client::SetScreenPositionClient(root_window_, this);
29 }
30
31 DesktopScreenPositionClient::~DesktopScreenPositionClient() {
32   aura::client::SetScreenPositionClient(root_window_, NULL);
33 }
34
35 void DesktopScreenPositionClient::SetBounds(aura::Window* window,
36                                             const gfx::Rect& bounds,
37                                             const gfx::Display& display) {
38   // TODO(jam): Use the 3rd parameter, |display|.
39   aura::Window* root = window->GetRootWindow();
40
41   // This method assumes that |window| does not have an associated
42   // DesktopNativeWidgetAura.
43   internal::NativeWidgetPrivate* desktop_native_widget =
44       DesktopNativeWidgetAura::ForWindow(root);
45   DCHECK(!desktop_native_widget ||
46          desktop_native_widget->GetNativeView() != window);
47
48   if (PositionWindowInScreenCoordinates(window)) {
49     // The caller expects windows we consider "embedded" to be placed in the
50     // screen coordinate system. So we need to offset the root window's
51     // position (which is in screen coordinates) from these bounds.
52
53     gfx::Point origin = bounds.origin();
54     aura::Window::ConvertPointToTarget(window->parent(), root, &origin);
55
56     gfx::Point host_origin = GetOriginInScreen(root);
57     origin.Offset(-host_origin.x(), -host_origin.y());
58     window->SetBounds(gfx::Rect(origin, bounds.size()));
59     return;
60   }
61
62   window->SetBounds(bounds);
63 }
64
65 }  // namespace views