fix: use EINA_* booleans instread of TRUE/FALSE
[platform/framework/web/wrt.git] / src / wrt-client / splash_screen_support.cpp
1 /*
2  * Copyright (c) 2012 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        splash_screen_support.cpp
18  * @author      Andrzej Surdej (a.surdej@samsung.com)
19  * @brief       Implementation file for splash screen support
20  */
21
22 #include "splash_screen_support.h"
23 #include <dpl/log/log.h>
24
25 const short  SPLASH_SCREEN_LAYER = 10;
26 const double SPLASH_SCREEN_MAXIUM_TIMER = 5.0;   // sec
27 const double SPLASH_SCREEN_BUFFER_TIMER = 0.3;  //  sec
28 const int    INDICATOR_H = 60;
29
30 static void evas_object_reposition(Evas_Object* obj, int x, int y, int w, int h)
31 {
32     evas_object_resize(obj,w, h);
33     evas_object_move(obj, x, y);
34 }
35
36 SplashScreenSupport::SplashScreenSupport(Evas_Object* parent, const char* image_path, bool indicator, bool landscape) :
37     m_parent(parent),
38     m_splashScreen(NULL),
39     m_splashScreenBg(NULL),
40     m_timer(NULL),
41     m_deviceWidth(0),
42     m_deviceHeight(0),
43     m_imageWidth(0),
44     m_imageHeight(0),
45     m_initialized(false),
46     m_isShowing(false),
47     m_indicator(indicator)
48 {
49     LogDebug("enter");
50
51     if (!m_parent || !image_path)
52     {
53         LogError("Invalid parameter");
54         return;
55     }
56
57     // create evas object
58     m_splashScreenBg = evas_object_rectangle_add(evas_object_evas_get(m_parent));
59     evas_object_color_set(m_splashScreenBg, 255, 255, 255, 255);
60
61     m_splashScreen = elm_image_add(m_splashScreenBg);
62     elm_image_no_scale_set(m_splashScreen, EINA_FALSE);
63
64     if (elm_image_file_set(m_splashScreen, image_path, NULL))
65     {
66         // check animation
67         if (elm_image_animated_available_get(m_splashScreen))
68         {
69             elm_image_animated_set(m_splashScreen, EINA_TRUE);
70             elm_image_animated_play_set(m_splashScreen, EINA_FALSE);
71         }
72
73         // get contents area
74         Evas_Coord x, y, w, h;
75         evas_object_geometry_get(m_parent, &x, &y, &w, &h);
76
77         m_deviceWidth  = (w < h) ? w : h;
78         m_deviceHeight = (w > h) ? w : h;
79
80         if (!landscape)
81         {
82             x = 0;
83             y = 0;
84             w = m_deviceWidth;
85             h = m_deviceHeight;
86
87             if (m_indicator)
88             {
89                 int indicator_h = INDICATOR_H * elm_config_scale_get();
90
91                 y += indicator_h;
92                 h -= indicator_h;
93             }
94         }
95         else
96         {
97             x = 0;
98             y = 0;
99             w = m_deviceHeight;
100             h = m_deviceWidth;
101         }
102
103         // fit to width
104         elm_image_object_size_get(m_splashScreen, &m_imageWidth, &m_imageHeight);
105
106         if (m_imageWidth == 0 || m_imageHeight == 0)
107         {
108             LogError("Splash screen image size error!");
109
110             m_imageWidth = w;
111             m_imageHeight = h;
112         }
113
114         double ratio_win    = (double)w/h;
115         double ratio_image  = (double)m_imageWidth/m_imageHeight;
116
117         // set evas position
118         evas_object_reposition(m_splashScreenBg, x, y, w, h);
119         evas_object_reposition(m_splashScreen,   x, y, w, h);
120
121         double scaled_image_w = w;
122         double scaled_image_h = (w/ratio_image);
123
124         Evas_Object* imageObject = elm_image_object_get(m_splashScreen);
125
126         if (ratio_image <= ratio_win)
127         {
128             evas_object_image_fill_set(imageObject, 0, 0-((scaled_image_h-h)/2), scaled_image_w, scaled_image_h);
129             evas_object_reposition(imageObject, x, y, w, h);
130         }
131         else
132         {
133             evas_object_image_fill_set(imageObject, 0, 0, scaled_image_w, scaled_image_h);
134             evas_object_reposition(imageObject, 0, y+(h-scaled_image_h)/2, scaled_image_w, scaled_image_h);
135         }
136
137         m_initialized = true;
138     }
139 }
140
141 SplashScreenSupport::~SplashScreenSupport()
142 {
143     LogDebug("enter");
144
145     if (m_initialized)
146     {
147         evas_object_del(m_splashScreen);
148         evas_object_del(m_splashScreenBg);
149     }
150
151     if (m_timer)
152     {
153         ecore_timer_del(m_timer);
154         m_timer = NULL;
155     }
156 }
157
158 Eina_Bool SplashScreenSupport::timerCallback(void *data)
159 {
160     LogDebug("enter");
161
162     SplashScreenSupport* This = static_cast<SplashScreenSupport*>(data);
163
164     if (This->isShowing())
165     {
166         This->stopSplashScreen();
167     }
168
169     return ECORE_CALLBACK_CANCEL;
170 }
171
172 void SplashScreenSupport::startSplashScreen()
173 {
174     LogDebug("splashImageOn");
175
176     if (m_initialized)
177     {
178         if (elm_image_animated_get(m_splashScreen) == EINA_TRUE)
179         {
180             elm_image_animated_play_set(m_splashScreen, EINA_TRUE);
181         }
182
183         evas_object_layer_set(m_splashScreenBg, SPLASH_SCREEN_LAYER);
184         evas_object_layer_set(m_splashScreen,   SPLASH_SCREEN_LAYER);
185
186         evas_object_show(m_splashScreenBg);
187         evas_object_show(m_splashScreen);
188
189         m_isShowing = true;
190
191         if (m_timer)
192         {
193             ecore_timer_del(m_timer);
194         }
195
196         m_timer = ecore_timer_add(SPLASH_SCREEN_MAXIUM_TIMER, timerCallback, this);
197     }
198 }
199
200
201 void SplashScreenSupport::stopSplashScreenBuffered()
202 {
203     if (m_isShowing)
204     {
205         if (m_timer)
206         {
207             ecore_timer_del(m_timer);
208         }
209
210         m_timer = ecore_timer_add(SPLASH_SCREEN_BUFFER_TIMER, timerCallback, this);
211     }
212 }
213
214 void SplashScreenSupport::stopSplashScreen()
215 {
216     LogDebug("splashImageOff");
217
218     if (m_isShowing)
219     {
220         if (elm_image_animated_get(m_splashScreen) == EINA_TRUE)
221         {
222             elm_image_animated_play_set(m_splashScreen, EINA_FALSE);
223         }
224
225         evas_object_hide(m_splashScreen);
226         evas_object_hide(m_splashScreenBg);
227
228         evas_object_del(m_splashScreen);
229         evas_object_del(m_splashScreenBg);
230
231         m_initialized = false;
232         m_isShowing = false;
233
234         if (m_timer)
235         {
236             ecore_timer_del(m_timer);
237             m_timer = NULL;
238         }
239     }
240 }
241
242 bool SplashScreenSupport::isShowing()
243 {
244     return m_isShowing;
245 }
246
247 void SplashScreenSupport::resizeSplashScreen()
248 {
249     if (m_initialized && m_isShowing)
250     {
251         int x, y, w, h;
252         int angle = elm_win_rotation_get(m_parent);
253
254         if (angle == 0 || angle == 180)
255         {
256             x = 0;
257             y = 0;
258             w = m_deviceWidth;
259             h = m_deviceHeight;
260
261             if (m_indicator)
262             {
263                 int indicator_h = INDICATOR_H * elm_config_scale_get();
264
265                 y += indicator_h;
266                 h -= indicator_h;
267             }
268         }
269         else
270         {
271             x = 0;
272             y = 0;
273             w = m_deviceHeight;
274             h = m_deviceWidth;
275         }
276
277         // fit to width
278         double ratio_win    = (double)w/h;
279         double ratio_image  = (double)m_imageWidth/m_imageHeight;
280
281         evas_object_reposition(m_splashScreenBg, x, y, w, h);
282         evas_object_reposition(m_splashScreen,   x, y, w, h);
283
284         double scaled_image_w = w;
285         double scaled_image_h = (w/ratio_image);
286
287         Evas_Object* imageObject = elm_image_object_get(m_splashScreen);
288
289         if (ratio_image <= ratio_win)
290         {
291             evas_object_image_fill_set(imageObject, 0, 0-((scaled_image_h-h)/2), scaled_image_w, scaled_image_h);
292             evas_object_reposition(imageObject, x, y, w, h);
293         }
294         else
295         {
296             evas_object_image_fill_set(imageObject, 0, 0, scaled_image_w, scaled_image_h);
297             evas_object_reposition(imageObject, 0, y+(h-scaled_image_h)/2, scaled_image_w, scaled_image_h);
298         }
299
300     }
301 }