Merge pull request #85 from JongHeonChoi/support_external_link
[platform/framework/web/crosswalk-tizen.git] / runtime / browser / native_window.cc
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16
17 #include "runtime/browser/native_window.h"
18
19 #include <Ecore_Wayland.h>
20 #include <cstdint>
21
22 #include "common/logger.h"
23
24 namespace runtime {
25
26 namespace {
27   const char* kEdjePath = "/usr/share/edje/xwalk/xwalk_tizen.edj";
28   const char* kWinowRotationEventKey = "wm,rotation,changed";
29 }  // namespace
30
31
32 NativeWindow::NativeWindow()
33     : initialized_(false),
34       window_(NULL),
35       layout_(NULL),
36       content_(NULL),
37       rotation_(0),
38       handler_id_(0) {
39 }
40
41 NativeWindow::~NativeWindow() {
42 }
43
44 void NativeWindow::Initialize() {
45   if (initialized_) {
46     LOGGER(DEBUG) << "already initialized";
47     return;
48   }
49   // window
50   window_ = CreateWindowInternal();
51   elm_win_conformant_set(window_, EINA_TRUE);
52   int w, h;
53   ecore_wl_screen_size_get(&w, &h);
54   evas_object_resize(window_, w, h);
55   elm_win_autodel_set(window_, EINA_TRUE);
56   evas_object_smart_callback_add(window_, "delete,request",
57                                  DidDeleteRequested, this);
58   evas_object_smart_callback_add(window_, "profile,changed",
59                                  DidProfileChanged, this);
60
61   #define EVAS_SIZE_EXPAND_FILL(obj) \
62     evas_object_size_hint_weight_set(obj, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND); \
63     evas_object_size_hint_align_set(obj, EVAS_HINT_FILL, EVAS_HINT_FILL);
64
65   // background
66   Evas_Object* bg = evas_object_rectangle_add(evas_object_evas_get(window_));
67   evas_object_color_set(bg, 0, 0, 0, 255);
68   EVAS_SIZE_EXPAND_FILL(bg);
69   elm_win_resize_object_add(window_, bg);
70   evas_object_render_op_set(bg, EVAS_RENDER_BLEND);
71   evas_object_show(bg);
72
73   // conformant
74   Evas_Object* conformant = elm_conformant_add(window_);
75   EVAS_SIZE_EXPAND_FILL(conformant);
76   elm_win_resize_object_add(window_, conformant);
77   evas_object_show(conformant);
78
79   // top layout
80   Evas_Object* top_layout = elm_layout_add(conformant);
81   elm_layout_file_set(top_layout, kEdjePath, "web-application");
82   EVAS_SIZE_EXPAND_FILL(top_layout);
83   elm_object_content_set(conformant, top_layout);
84   evas_object_show(top_layout);
85   layout_ = top_layout;
86
87   // Rotation
88   auto rotation_callback = [](void* user_data,
89                               Evas_Object* obj,
90                               void*) -> void {
91       NativeWindow* window = static_cast<NativeWindow*>(user_data);
92       int degree = elm_win_rotation_get(obj);
93       window->DidRotation(degree);
94   };
95   evas_object_smart_callback_add(window_,
96                                  kWinowRotationEventKey,
97                                  rotation_callback,
98                                  this);
99
100   if (w > h) {
101     natural_orientation_ = ScreenOrientation::LANDSCAPE_PRIMARY;
102   } else {
103     natural_orientation_ = ScreenOrientation::PORTRAIT_PRIMARY;
104   }
105
106   initialized_ = true;
107 }
108
109 void NativeWindow::DidDeleteRequested(void* /*data*/,
110     Evas_Object* /*obj*/, void* /*event_info*/) {
111   LOGGER(DEBUG) << "didDeleteRequested";
112   elm_exit();
113 }
114
115 void NativeWindow::DidProfileChanged(void* /*data*/,
116     Evas_Object* /*obj*/, void* /*event_info*/) {
117   LOGGER(DEBUG) << "didProfileChanged";
118 }
119
120 Evas_Object* NativeWindow::evas_object() const {
121   return window_;
122 }
123
124 void NativeWindow::SetContent(Evas_Object* content) {
125   // Remarks
126   // If any object was already set as a content object in the same part,
127   // the previous object will be deleted automatically with this call.
128   // If the content is NULL, this call will just delete the previous object.
129   // If the If you wish to preserve it,
130   // issue elm_object_part_content_unset() on it first.
131
132   evas_object_show(content);
133
134   // Hide unseted evas object to avoid focus and event callback problem.
135   evas_object_hide(
136     elm_object_part_content_unset(layout_, "elm.swallow.content"));
137   elm_object_part_content_set(layout_, "elm.swallow.content", content);
138   content_ = content;
139
140   // attached webview was resized by evas_norender API
141   evas_norender(evas_object_evas_get(window_));
142 }
143
144 void NativeWindow::DidRotation(int degree) {
145   rotation_ = degree;
146   auto it = handler_table_.begin();
147   for ( ; it != handler_table_.end(); ++it) {
148     it->second(degree);
149   }
150 }
151
152 int NativeWindow::AddRotationHandler(RotationHandler handler) {
153   int id = handler_id_++;
154   handler_table_[id] = handler;
155   return id;
156 }
157
158 void NativeWindow::RemoveRotationHandler(int id) {
159   handler_table_.erase(id);
160 }
161
162 void NativeWindow::SetRotationLock(int degree) {
163   if (degree != -1)
164     rotation_ = degree % 360;
165   elm_win_wm_rotation_preferred_rotation_set(window_, rotation_);
166 }
167
168 void NativeWindow::SetRotationLock(ScreenOrientation orientation) {
169   int portrait_natural_angle[] = {
170     0,  // PORTRAIT_PRIMARY
171     180,  // PORTRAIT_SECONDARY
172     270,  // LANDSCAPE_PRIMARY
173     90,  // LANDSCAPE_SECONDARY
174     0,  // NATURAL
175     -1  // ANY
176   };
177   int landscape_natural_angle[] = {
178     270,  // PORTRAIT_PRIMARY
179     90,  // PORTRAIT_SECONDARY
180     0,  // LANDSCAPE_PRIMARY
181     180,  // LANDSCAPE_SECONDARY
182     0,  // NATURAL
183     -1,  // ANY
184   };
185   auto& convert_table =
186       natural_orientation_ == ScreenOrientation::PORTRAIT_PRIMARY ?
187           portrait_natural_angle :
188           landscape_natural_angle;
189   SetRotationLock(convert_table[static_cast<int>(orientation)]);
190 }
191
192
193 void NativeWindow::SetAutoRotation() {
194   elm_win_wm_rotation_preferred_rotation_set(window_, -1);
195   if (elm_win_wm_rotation_supported_get(window_)) {
196     const int rotation[4] = {0, 90, 180, 270};
197     elm_win_wm_rotation_available_rotations_set(window_, rotation, 4);
198   }
199   rotation_ = elm_win_rotation_get(window_);
200 }
201
202 void NativeWindow::Show() {
203   evas_object_show(window_);
204 }
205
206 void NativeWindow::Active() {
207   elm_win_activate(window_);
208 }
209
210 void NativeWindow::InActive() {
211   elm_win_lower(window_);
212 }
213
214 void NativeWindow::FullScreen(bool enable) {
215   elm_win_indicator_opacity_set(window_,
216       enable ? ELM_WIN_INDICATOR_TRANSPARENT : ELM_WIN_INDICATOR_OPAQUE);
217 }
218
219 }  // namespace runtime