Upstream version 10.38.220.0
[platform/framework/web/crosswalk.git] / src / ozone / wayland / window.cc
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Copyright 2013 Intel Corporation. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5
6 #include "ozone/wayland/window.h"
7
8 #include "base/logging.h"
9 #include "ozone/wayland/display.h"
10 #include "ozone/wayland/egl/egl_window.h"
11 #include "ozone/wayland/input_device.h"
12 #include "ozone/wayland/shell/shell.h"
13 #include "ozone/wayland/shell/shell_surface.h"
14
15 namespace ozonewayland {
16
17 WaylandWindow::WaylandWindow(unsigned handle) : shell_surface_(NULL),
18     window_(NULL),
19     type_(None),
20     handle_(handle),
21     allocation_(gfx::Rect(0, 0, 1, 1)) {
22 }
23
24 WaylandWindow::~WaylandWindow() {
25   delete window_;
26   delete shell_surface_;
27 }
28
29 void WaylandWindow::SetShellAttributes(ShellType type) {
30   if (type_ == type)
31     return;
32
33   if (!shell_surface_) {
34     shell_surface_ =
35         WaylandDisplay::GetInstance()->GetShell()->CreateShellSurface(this, type);
36   }
37
38   type_ = type;
39   shell_surface_->UpdateShellSurface(type_, NULL, 0, 0);
40 }
41
42 void WaylandWindow::SetShellAttributes(ShellType type,
43                                        WaylandShellSurface* shell_parent,
44                                        unsigned x,
45                                        unsigned y) {
46   DCHECK(shell_parent && (type == POPUP));
47
48   if (!shell_surface_) {
49     shell_surface_ =
50         WaylandDisplay::GetInstance()->GetShell()->CreateShellSurface(this, type);
51     WaylandInputDevice* input = WaylandDisplay::GetInstance()->PrimaryInput();
52     input->SetGrabWindowHandle(handle_, 0);
53   }
54
55   type_ = type;
56   shell_surface_->UpdateShellSurface(type_, shell_parent, x, y);
57 }
58
59 void WaylandWindow::SetWindowTitle(const base::string16& title) {
60   shell_surface_->SetWindowTitle(title);
61 }
62
63 void WaylandWindow::Maximize() {
64   if (type_ != FULLSCREEN)
65     shell_surface_->Maximize();
66 }
67
68 void WaylandWindow::Minimize() {
69   shell_surface_->Minimize();
70 }
71
72 void WaylandWindow::Restore() {
73   // If window is created as fullscreen, we don't set/restore any window states
74   // like Maximize etc.
75   if (type_ != FULLSCREEN)
76     shell_surface_->UpdateShellSurface(type_, NULL, 0, 0);
77 }
78
79 void WaylandWindow::SetFullscreen() {
80   if (type_ != FULLSCREEN)
81     shell_surface_->UpdateShellSurface(FULLSCREEN, NULL, 0, 0);
82 }
83
84 void WaylandWindow::RealizeAcceleratedWidget() {
85   if (!shell_surface_) {
86     LOG(ERROR) << "Shell type not set. Setting it to TopLevel";
87     SetShellAttributes(TOPLEVEL);
88   }
89
90   if (!window_)
91     window_ = new EGLWindow(shell_surface_->GetWLSurface(),
92                             allocation_.width(),
93                             allocation_.height());
94 }
95
96 wl_egl_window* WaylandWindow::egl_window() const {
97   DCHECK(window_);
98   return window_->egl_window();
99 }
100
101 void WaylandWindow::Resize(unsigned width, unsigned height) {
102   if ((allocation_.width() == width) && (allocation_.height() == height))
103     return;
104
105   allocation_ = gfx::Rect(allocation_.x(), allocation_.y(), width, height);
106   if (!shell_surface_ || !window_)
107     return;
108
109   window_->Resize(width, height);
110   WaylandDisplay* display = WaylandDisplay::GetInstance();
111   DCHECK(display);
112   display->FlushDisplay();
113 }
114
115 }  // namespace ozonewayland