- add sources.
[platform/framework/web/crosswalk.git] / src / ash / wm / window_util.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/wm/window_util.h"
6
7 #include <vector>
8
9 #include "ash/ash_constants.h"
10 #include "ash/shell.h"
11 #include "ash/wm/window_properties.h"
12 #include "ui/aura/client/activation_client.h"
13 #include "ui/aura/client/aura_constants.h"
14 #include "ui/aura/root_window.h"
15 #include "ui/aura/window.h"
16 #include "ui/gfx/display.h"
17 #include "ui/gfx/rect.h"
18 #include "ui/gfx/screen.h"
19 #include "ui/views/corewm/window_util.h"
20 #include "ui/views/view.h"
21 #include "ui/views/widget/widget.h"
22
23 namespace ash {
24 namespace wm {
25
26 // TODO(beng): replace many of these functions with the corewm versions.
27 void ActivateWindow(aura::Window* window) {
28   views::corewm::ActivateWindow(window);
29 }
30
31 void DeactivateWindow(aura::Window* window) {
32   views::corewm::DeactivateWindow(window);
33 }
34
35 bool IsActiveWindow(aura::Window* window) {
36   return views::corewm::IsActiveWindow(window);
37 }
38
39 aura::Window* GetActiveWindow() {
40   return aura::client::GetActivationClient(Shell::GetPrimaryRootWindow())->
41       GetActiveWindow();
42 }
43
44 aura::Window* GetActivatableWindow(aura::Window* window) {
45   return views::corewm::GetActivatableWindow(window);
46 }
47
48 bool CanActivateWindow(aura::Window* window) {
49   return views::corewm::CanActivateWindow(window);
50 }
51
52 bool IsWindowMinimized(aura::Window* window) {
53   return window->GetProperty(aura::client::kShowStateKey) ==
54       ui::SHOW_STATE_MINIMIZED;
55 }
56
57 void CenterWindow(aura::Window* window) {
58   const gfx::Display display =
59       Shell::GetScreen()->GetDisplayNearestWindow(window);
60   gfx::Rect center = display.work_area();
61   center.ClampToCenteredSize(window->bounds().size());
62   window->SetBoundsInScreen(center, display);
63 }
64
65 void AdjustBoundsToEnsureMinimumWindowVisibility(const gfx::Rect& visible_area,
66                                                  gfx::Rect* bounds) {
67   AdjustBoundsToEnsureWindowVisibility(
68       visible_area, kMinimumOnScreenArea, kMinimumOnScreenArea, bounds);
69 }
70
71 void AdjustBoundsToEnsureWindowVisibility(const gfx::Rect& visible_area,
72                                           int min_width,
73                                           int min_height,
74                                           gfx::Rect* bounds) {
75   bounds->set_width(std::min(bounds->width(), visible_area.width()));
76   bounds->set_height(std::min(bounds->height(), visible_area.height()));
77
78   min_width = std::min(min_width, visible_area.width());
79   min_height = std::min(min_height, visible_area.height());
80
81   if (bounds->x() + min_width > visible_area.right()) {
82     bounds->set_x(visible_area.right() - min_width);
83   } else if (bounds->right() - min_width < 0) {
84     bounds->set_x(min_width - bounds->width());
85   }
86   if (bounds->y() + min_height > visible_area.bottom()) {
87     bounds->set_y(visible_area.bottom() - min_height);
88   } else if (bounds->bottom() - min_height < 0) {
89     bounds->set_y(min_height - bounds->height());
90   }
91   if (bounds->y() < 0)
92     bounds->set_y(0);
93 }
94
95 bool MoveWindowToEventRoot(aura::Window* window, const ui::Event& event) {
96   views::View* target = static_cast<views::View*>(event.target());
97   if (!target)
98     return false;
99   aura::Window* target_root =
100       target->GetWidget()->GetNativeView()->GetRootWindow();
101   if (!target_root || target_root == window->GetRootWindow())
102     return false;
103   aura::Window* window_container =
104       ash::Shell::GetContainer(target_root, window->parent()->id());
105   // Move the window to the target launcher.
106   window_container->AddChild(window);
107   return true;
108 }
109
110 void ReparentChildWithTransientChildren(aura::Window* window,
111                                         aura::Window* child) {
112   window->AddChild(child);
113   ReparentTransientChildrenOfChild(window, child);
114 }
115
116 void ReparentTransientChildrenOfChild(aura::Window* window,
117                                       aura::Window* child) {
118   for (size_t i = 0; i < child->transient_children().size(); ++i)
119     ReparentChildWithTransientChildren(window, child->transient_children()[i]);
120 }
121
122 }  // namespace wm
123 }  // namespace ash