Update wrt_0.8.89
[platform/framework/web/wrt.git] / src / wrt-client / window_data.cpp
1 /*
2  * Copyright (c) 2011 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  * @file        window_data.cpp
18  * @author      Jaroslaw Osmanski (j.osmanski@samsung.com)
19  * @version     1.0
20  * @brief       Window data class implementation
21  */
22 #include "window_data.h"
23
24 #include <ui-gadget.h>
25 #include <dpl/log/log.h>
26
27 namespace {
28 char const* const DAEMON_EDJ_PATH = "/usr/share/edje/wrt/Daemon.edj";
29 char const* const THEME_EDJ_PATH = "/usr/share/edje/wrt/wrt_theme.edj";
30 char const* const ELM_STATE_SHOW_CONTENT = "elm,state,show,content";
31 char const* const ELM_SWALLOW_CONTENT = "elm.swallow.content";
32 char const* const ELM_SWALLOW_BACKWARD = "elm.swallow.backward";
33 char const* const ELM = "elm";
34 char const* const LAYOUT = "layout";
35 char const* const APPLICATION = "application";
36 char const* const INDICATOR = "indicator";
37 char const* const NOINDICATOR = "noindicator";
38 char const* const LIST_PROCESS = "list_process";
39 char const* const INTERNAL_LAYOUT = "internal_layout";
40 char const* const FLOATBACKWARD_BUTTON_STYLE = "wrt/backward";
41 const double PROGRESS_POSITION_X = 0.5;
42 const double PROGRESS_POSITION_Y = 0.5;
43 const int PROGRESS_SIZE_RATIO = 12;     //ResolutionWidth / ProgressWidth = 12
44 const int PROGRESS_COLOR_R = 153;
45 const int PROGRESS_COLOR_G = 153;
46 const int PROGRESS_COLOR_B = 153;
47 const int PROGRESS_COLOR_A = 255;
48
49 } // anonymous namespace
50
51 WindowData::WindowData(unsigned long pid, bool manualInit) :
52     m_naviBackButton(NULL), m_win(NULL)
53 {
54     m_win = createWindow(pid);
55
56     if (!manualInit)
57     {
58         init();
59     }
60 }
61
62 WindowData::~WindowData()
63 {
64     LogDebug("");
65     evas_object_del(m_win);
66 }
67
68 void WindowData::init()
69 {
70     Assert(m_win != NULL && "m_win is null");
71
72     int result;
73     // import button theme
74     elm_theme_overlay_add(NULL, THEME_EDJ_PATH);
75
76     m_platform_layout = createPlatformLayout(m_win);
77     evas_object_show(m_platform_layout);
78     m_navigation = createNavigationBar(m_platform_layout);
79     evas_object_show(m_navigation);
80     m_user_layout = createUserLayout(m_navigation);
81     evas_object_show(m_user_layout);
82     m_conformant = createConformant(m_user_layout);
83     evas_object_show(m_conformant);
84     m_progress = createProgress(m_user_layout);
85
86     result = ug_init(
87         static_cast<Display*>(ecore_x_display_get()),
88         elm_win_xwindow_get(m_win),
89         m_win,
90         UG_OPT_INDICATOR_ENABLE);
91
92     if (result != 0) {
93         LogError("Gadget creation failed for window: " << m_win);
94         Assert(false && "UI Gadget creation failed");
95     }
96 }
97
98 void WindowData::setEvasObjectForLayout(Evas_Object* evas_object)
99 {
100     elm_object_content_set(m_conformant, evas_object);
101 }
102
103 void WindowData::unsetEvasObjectForLayout()
104 {
105     elm_object_content_unset(m_conformant);
106 }
107
108 void WindowData::loadingOn()
109 {
110     LogInfo("loadingOn");
111
112     alignProgressPosition();
113     elm_progressbar_pulse(m_progress, EINA_TRUE);
114     evas_object_show(m_progress);
115 }
116
117 void WindowData::loadingOff()
118 {
119     LogInfo("loadingOff");
120
121     elm_progressbar_pulse(m_progress, EINA_FALSE);
122     evas_object_hide(m_progress);
123 }
124
125 void WindowData::moveProgress()
126 {
127     LogInfo("moveProgress");
128
129     if (evas_object_visible_get(m_progress) == EINA_TRUE) {
130         alignProgressPosition();
131     } else {
132         LogInfo("Progress is hide");
133     }
134 }
135
136 void WindowData::setViewModeFullScreen(bool indicator, bool backbutton)
137 {
138     LogDebug("indicator: " << indicator << ", backbutton: " << backbutton);
139     if (indicator) {
140         elm_win_indicator_mode_set(m_win, ELM_WIN_INDICATOR_SHOW);
141         elm_layout_theme_set(m_platform_layout,
142                              LAYOUT,
143                              APPLICATION,
144                              INDICATOR);
145     } else {
146         elm_win_indicator_mode_set(m_win, ELM_WIN_INDICATOR_HIDE);
147         elm_layout_theme_set(m_platform_layout,
148                              LAYOUT,
149                              APPLICATION,
150                              NOINDICATOR);
151     }
152     if (backbutton) {
153         createFloatBackButton();
154     }
155 }
156
157 void WindowData::setViewModeMaximized(
158         const char *title,
159         bool indicator,
160         bool backbutton)
161 {
162     LogDebug("indicator: " << indicator << ", backbutton: " << backbutton);
163     if (indicator) {
164         elm_win_indicator_mode_set(m_win, ELM_WIN_INDICATOR_SHOW);
165         elm_layout_theme_set(m_platform_layout,
166                              LAYOUT,
167                              APPLICATION,
168                              INDICATOR);
169     } else {
170         elm_win_indicator_mode_set(m_win, ELM_WIN_INDICATOR_HIDE);
171         elm_layout_theme_set(m_platform_layout,
172                              LAYOUT,
173                              APPLICATION,
174                              NOINDICATOR);
175     }
176     createTitle(title);
177     if (backbutton) {
178         createFloatBackButton();
179     }
180 }
181
182 void WindowData::createTitle(const char* data)
183 {
184     LogDebug("createTitle");
185     Elm_Object_Item* naviIt = elm_naviframe_top_item_get(m_navigation);
186     elm_naviframe_item_title_visible_set(naviIt, EINA_TRUE);
187     elm_object_item_text_set(naviIt, data);
188 }
189
190 void WindowData::removeTitle()
191 {
192     Elm_Object_Item* naviIt = elm_naviframe_top_item_get(m_navigation);
193     elm_object_item_text_set(naviIt, "");
194     elm_naviframe_item_title_visible_set(naviIt, EINA_FALSE);
195 }
196
197 void WindowData::createTitleButton()
198 {
199     // Add left button for back action
200     m_naviBackButton = elm_button_add(m_navigation);
201     elm_object_style_set(m_naviBackButton, "navigationbar_backbutton/default");
202
203     Elm_Object_Item* naviIt = elm_naviframe_top_item_get(m_navigation);
204     elm_object_item_part_content_set(naviIt, "prev_btn",
205                                      m_naviBackButton);
206 }
207
208 void WindowData::createFloatBackButton()
209 {
210     // Add float backbutton on the left coner
211     m_floatBackButton = elm_button_add(m_user_layout);
212     elm_object_style_set(m_floatBackButton, FLOATBACKWARD_BUTTON_STYLE);
213     elm_object_part_content_set(m_user_layout,
214                                 ELM_SWALLOW_BACKWARD,
215                                 m_floatBackButton);
216     evas_object_show(m_floatBackButton);
217 }
218
219 void WindowData::updateTitleButton(const bool display)
220 {
221     if (display) {
222         evas_object_show(m_naviBackButton);
223     } else {
224         evas_object_hide(m_naviBackButton);
225     }
226 }
227
228 Evas_Object* WindowData::createWindow(unsigned long pid)
229 {
230     Evas_Object* window = elm_win_add(NULL, "wrt-widget", ELM_WIN_BASIC);
231     ecore_x_window_prop_property_set(
232         elm_win_xwindow_get(window),
233         ECORE_X_ATOM_NET_WM_PID,
234         ECORE_X_ATOM_CARDINAL, 32, &pid, 1);
235     elm_win_title_set(window, "wrt-widget");
236     elm_win_borderless_set(window, EINA_TRUE);
237     elm_win_conformant_set(window, EINA_TRUE);
238     int w, h;
239     ecore_x_window_size_get(ecore_x_window_root_first_get(), &w, &h);
240     evas_object_resize(window, w, h);
241     return window;
242 }
243
244 Evas_Object* WindowData::createPlatformLayout(Evas_Object* parent)
245 {
246     Evas_Object*  platform_layout = elm_layout_add(parent);
247     elm_layout_theme_set(platform_layout, LAYOUT, APPLICATION, NOINDICATOR);
248     evas_object_size_hint_expand_set(platform_layout,
249                                      EVAS_HINT_EXPAND,
250                                      EVAS_HINT_EXPAND);
251     elm_win_resize_object_add(m_win, platform_layout);
252     edje_object_signal_emit(
253             elm_layout_edje_get(platform_layout), ELM_STATE_SHOW_CONTENT, ELM);
254     return platform_layout;
255 }
256
257 Evas_Object* WindowData::createUserLayout(Evas_Object* parent)
258 {
259     Assert(parent != NULL && "Parent for User Layout is null");
260     Evas_Object* layout = elm_layout_add(parent);
261     elm_layout_file_set(layout, DAEMON_EDJ_PATH, "client");
262     evas_object_size_hint_weight_set(
263             layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
264     Elm_Object_Item* naviIt = elm_naviframe_item_push(
265         /* Evas_Object *obj */
266         parent,
267         /* const char *title_label  */
268         "",
269         /* Evas_Object *prev_btn */
270         NULL,
271         /* Evas_Object *next_btn */
272         NULL,
273         /* Evas_Object *content */
274         layout,
275         /* const char *item_style */
276         NULL);
277
278     elm_naviframe_item_title_visible_set(naviIt, EINA_FALSE);
279     return layout;
280 }
281
282 Evas_Object* WindowData::createConformant(Evas_Object* parent)
283 {
284     Assert(parent != NULL && "Parent is null");
285     Evas_Object* conformant = elm_conformant_add(parent);
286     elm_object_style_set(conformant, INTERNAL_LAYOUT);
287     evas_object_size_hint_weight_set(
288             conformant, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
289     evas_object_size_hint_align_set(conformant, EVAS_HINT_FILL, EVAS_HINT_FILL);
290     elm_object_part_content_set(parent, ELM_SWALLOW_CONTENT, conformant);
291     evas_object_show(conformant);
292     return conformant;
293 }
294
295 Evas_Object* WindowData::createNavigationBar(Evas_Object* parent)
296 {
297     Assert(parent != NULL && "Parent for naviframe is null");
298     Evas_Object* navigation = elm_naviframe_add(parent);
299     elm_object_part_content_set(parent, ELM_SWALLOW_CONTENT, navigation);
300     return navigation;
301 }
302
303 Evas_Object* WindowData::createProgress(Evas_Object* parent)
304 {
305     Assert(parent != NULL && "Parent for progress is null");
306     Evas_Object* progress = elm_progressbar_add(parent);
307     progress = elm_progressbar_add(parent);
308     elm_object_style_set(progress, LIST_PROCESS);
309     evas_object_color_set(
310         progress,
311         PROGRESS_COLOR_R,
312         PROGRESS_COLOR_G,
313         PROGRESS_COLOR_B,
314         PROGRESS_COLOR_A);
315     Evas_Coord x, y, w, h;
316     int progressW, progressH;
317     evas_object_geometry_get(m_win, &x, &y, &w, &h);
318     progressW = w / static_cast<Evas_Coord>(PROGRESS_SIZE_RATIO);
319     progressH = progressW;
320
321     evas_object_resize(progress, progressW, progressH);
322     return progress;
323 }
324
325 void WindowData::alignProgressPosition()
326 {
327     Evas_Coord layOutX, layOutY, layOutW, layOutH;
328     Evas_Coord progressX, progressY, progressW, progressH;
329     Evas_Coord alignedX, alignedY;
330
331     evas_object_geometry_get(
332         m_user_layout,
333         &layOutX,
334         &layOutY,
335         &layOutW,
336         &layOutH);
337     evas_object_geometry_get(
338         m_progress,
339         &progressX,
340         &progressY,
341         &progressW,
342         &progressH);
343     alignedX = (layOutW - progressW) * PROGRESS_POSITION_X + layOutX;
344     alignedY = (layOutH - progressH) * PROGRESS_POSITION_Y + layOutY;
345     evas_object_move(m_progress, alignedX, alignedY);
346 }
347
348 void WindowData::addNaviBackButtonCallback(
349         const char* event,
350         CallbackType callback,
351         const void* data)
352 {
353     Assert(m_naviBackButton != NULL && "m_naviBackButton is null");
354     evas_object_smart_callback_add(m_naviBackButton, event, callback, data);
355 }
356
357 void* WindowData::delNaviBackButtonCallback(
358         const char* event,
359         CallbackType callBack)
360 {
361     Assert(m_naviBackButton != NULL && "m_naviBackButton is null");
362     return evas_object_smart_callback_del(m_naviBackButton, event, callBack);
363 }
364
365 void WindowData::addFloatBackButtonCallback(
366         const char* event,
367         CallbackType callback,
368         const void* data)
369 {
370     Assert(m_floatBackButton != NULL && "m_floatBackButton is null");
371     evas_object_smart_callback_add(m_floatBackButton, event, callback, data);
372 }
373
374 void* WindowData::delFloatBackButtonCallback(
375         const char* event,
376         CallbackType callBack)
377 {
378     Assert(m_floatBackButton != NULL && "m_floatBackButton is null");
379     return evas_object_smart_callback_del(m_floatBackButton, event, callBack);
380 }
381
382 void WindowData::userlayoutCallbackAdd(
383         const Evas_Callback_Type event,
384         EvasCallbackType callback,
385         const void* data)
386 {
387     Assert(m_user_layout != NULL && "m_user_layout is null");
388     evas_object_event_callback_add(m_user_layout, event, callback, data);
389 }
390
391 void* WindowData::userlayoutCallbackDel(
392         const Evas_Callback_Type event,
393         EvasCallbackType callBack)
394 {
395     Assert(m_user_layout != NULL && "m_user_layout is null");
396     return evas_object_event_callback_del(m_user_layout, event, callBack);
397 }
398
399 void WindowData::emitSignalForUserLayout(
400         const char* emission, const char* source)
401 {
402     LogInfo("emitSignalForUserLayout called");
403     Assert(m_user_layout != NULL && "m_user_layout is null");
404     Assert(emission != NULL && "emission is null");
405     Assert(source != NULL && "source is null");
406
407     edje_object_signal_emit(
408             elm_layout_edje_get(m_user_layout), emission, source);
409 }