tizen 2.4 release
[apps/home/attach-panel-gallery.git] / src / widget / ge-rotate-bg.c
1 /*
2 * Copyright (c) 2000-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
18 #ifdef _USE_ROTATE_BG_GE
19
20 #include "ge-rotate-bg.h"
21 #include "ge-exif.h"
22 #include "ge-debug.h"
23 #include "ge-icon.h"
24
25 #define GE_ROTATE_BG_DATA_KEY "ge_bg_data"
26 #define GE_ROTATE_BG_GROUP "ge_bg_layout"
27
28 typedef struct _ge_bg {
29         Evas_Object *base;
30         Evas_Object *img;
31         char *file;
32         bool b_preload;
33 } ge_bg;
34
35 static int __ge_rotate_bg_image_rotate_180(Evas_Object *obj)
36 {
37         GE_CHECK_VAL(obj, -1);
38         unsigned int *data = NULL;
39         unsigned int *data2 = NULL;
40         unsigned int *to = NULL;
41         unsigned int *from = NULL;
42         int x = 0;
43         int hw = 0;
44         int iw = 0;
45         int ih = 0;
46
47         evas_object_image_size_get(obj, &iw, &ih);
48         int size = iw * ih * sizeof(unsigned int);
49
50         /* EINA_FALSE for reading */
51         data = evas_object_image_data_get(obj, EINA_FALSE);
52         /* memcpy */
53         data2 = calloc(1, size);
54         GE_CHECK_VAL(data2, -1);
55         memcpy(data2, data, size);
56
57         data = evas_object_image_data_get(obj, EINA_TRUE);
58
59         hw = iw * ih;
60         x = hw;
61         to = data;
62         from = data2 + hw - 1;
63         for (; --x >= 0;) {
64                 *to = *from;
65                 to++;
66                 from--;
67         }
68
69
70         GE_FREE(data2);
71
72         evas_object_image_data_set(obj, data);
73         evas_object_image_data_update_add(obj, 0, 0, iw, ih);
74         return 0;
75 }
76
77 static int __ge_rotate_bg_image_rotate_90(Evas_Object *obj)
78 {
79         GE_CHECK_VAL(obj, -1);
80         unsigned int *data = NULL;
81         unsigned int *data2 = NULL;
82         unsigned int *to = NULL;
83         unsigned int *from = NULL;
84         int x = 0;
85         int y = 0;
86         int w = 0;
87         int hw = 0;
88         int iw = 0;
89         int ih = 0;
90
91         evas_object_image_size_get(obj, &iw, &ih);
92         int size = iw * ih * sizeof(unsigned int);
93
94         /* EINA_FALSE for reading */
95         data = evas_object_image_data_get(obj, EINA_FALSE);
96         /* memcpy */
97         data2 = calloc(1, size);
98         GE_CHECK_VAL(data2, -1);
99         memcpy(data2, data, size);
100
101         /* set width, height */
102         w = ih;
103         ih = iw;
104         iw = w;
105         hw = w * ih;
106
107         /* set width, height to image obj */
108         evas_object_image_size_set(obj, iw, ih);
109         data = evas_object_image_data_get(obj, EINA_TRUE);
110         to = data + w - 1;
111         hw = -hw - 1;
112         from = data2;
113
114         for (x = iw; --x >= 0;) {
115                 for (y = ih; --y >= 0;) {
116                         *to = *from;
117                         from++;
118                         to += w;
119                 }
120
121                 to += hw;
122         }
123
124         GE_FREE(data2);
125
126         evas_object_image_data_set(obj, data);
127         evas_object_image_data_update_add(obj, 0, 0, iw, ih);
128         return 0;
129 }
130
131 static int __ge_rotate_bg_image_rotate_270(Evas_Object *obj)
132 {
133         GE_CHECK_VAL(obj, -1);
134         unsigned int *data = NULL;
135         unsigned int *data2 = NULL;
136         unsigned int *to = NULL;
137         unsigned int *from = NULL;
138         int x = 0;
139         int y = 0;
140         int w = 0;
141         int hw = 0;
142         int iw = 0;
143         int ih = 0;
144
145         evas_object_image_size_get(obj, &iw, &ih);
146         int size = iw * ih * sizeof(unsigned int);
147
148         /* EINA_FALSE for reading */
149         data = evas_object_image_data_get(obj, EINA_FALSE);
150         /* memcpy */
151         data2 = calloc(1, size);
152         GE_CHECK_VAL(data2, -1);
153         memcpy(data2, data, size);
154
155         /* set width, height */
156         w = ih;
157         ih = iw;
158         iw = w;
159         hw = w * ih;
160
161         /* set width, height to image obj */
162         evas_object_image_size_set(obj, iw, ih);
163         data = evas_object_image_data_get(obj, EINA_TRUE);
164
165         to = data + hw - w;
166         w = -w;
167         hw = hw + 1;
168         from = data2;
169
170         for (x = iw; --x >= 0;) {
171                 for (y = ih; --y >= 0;) {
172                         *to = *from;
173                         from++;
174                         to += w;
175                 }
176
177                 to += hw;
178         }
179
180         GE_FREE(data2);
181
182         evas_object_image_data_set(obj, data);
183         evas_object_image_data_update_add(obj, 0, 0, iw, ih);
184         return 0;
185 }
186
187 /* check its orientation */
188 int __ge_rotate_bg_rotate_image(Evas_Object *obj, unsigned int orient)
189 {
190         switch (orient) {
191         case GE_ORIENTATION_ROT_90:
192                 __ge_rotate_bg_image_rotate_90(obj);
193                 break;
194         case GE_ORIENTATION_ROT_180:
195                 __ge_rotate_bg_image_rotate_180(obj);
196                 break;
197         case GE_ORIENTATION_ROT_270:
198                 __ge_rotate_bg_image_rotate_270(obj);
199                 break;
200         default:
201                 break;
202         }
203
204         return 0;
205 }
206
207 static void __ge_rotate_bg_delete_cb(void *data, Evas *e, Evas_Object *obj,
208                                      void *ei)
209 {
210         if (data) {
211                 ge_bg *bg_data = (ge_bg *)data;
212                 GE_FREEIF(bg_data->file);
213                 GE_FREE(data);
214         }
215 }
216
217 static void __ge_rotate_bg_custom_resize(void *data, Evas *e, Evas_Object *obj,
218                                          void *ei)
219 {
220         GE_CHECK(data);
221         ge_bg *bg_data = (ge_bg *)data;
222         Evas_Coord bx = 0;
223         Evas_Coord by = 0;
224         Evas_Coord bw = 0;
225         Evas_Coord bh = 0;
226         Evas_Coord iw = 0;
227         Evas_Coord ih = 0;
228         Evas_Coord fx = 0;
229         Evas_Coord fy = 0;
230         Evas_Coord fw = 0;
231         Evas_Coord fh = 0;
232         Evas_Coord nx = 0;
233         Evas_Coord ny = 0;
234         Evas_Coord nw = 0;
235         Evas_Coord nh = 0;
236
237         if ((!bg_data->img) || (!bg_data->base)) {
238                 ge_dbgE("Invalid object!");
239                 return;
240         }
241         /* grab image size */
242         evas_object_image_size_get(bg_data->img, &iw, &ih);
243         if ((iw < 1) || (ih < 1)) {
244                 ge_dbgE("(iw < 1) || (ih < 1)!");
245                 return;
246         }
247
248         /* grab base object dimensions */
249         evas_object_geometry_get(bg_data->base, &bx, &by, &bw, &bh);
250
251         /* set some defaults */
252         nx = bx;
253         ny = by;
254         nw = bw;
255         nh = bh;
256
257
258         fw = bw;
259         fh = ((ih * fw) / iw);
260         if (fh < bh) {
261                 fh = bh;
262                 fw = ((iw * fh) / ih);
263         }
264         fx = ((bw - fw) / 2);
265         fy = ((bh - fh) / 2);
266
267         evas_object_move(bg_data->img, nx, ny);
268         evas_object_resize(bg_data->img, nw, nh);
269         evas_object_image_fill_set(bg_data->img, fx, fy, fw, fh);
270 }
271
272 Evas_Object *_ge_rotate_bg_add_layout(Evas_Object *parent, const char *file,
273                                       const char *group)
274 {
275         Evas_Object *eo = NULL;
276         int r = 0;
277
278         eo = elm_layout_add(parent);
279         if (eo) {
280                 r = elm_layout_file_set(eo, file, group);
281                 if (!r) {
282                         evas_object_del(eo);
283                         return NULL;
284                 }
285
286                 evas_object_size_hint_weight_set(eo, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
287                 evas_object_size_hint_align_set(eo, EVAS_HINT_FILL, EVAS_HINT_FILL);
288         }
289
290         return eo;
291 }
292
293 Evas_Object *_ge_rotate_bg_add(Evas_Object *parent, bool b_preload)
294 {
295         GE_CHECK_NULL(parent);
296
297         ge_bg *bg_data = (ge_bg *)calloc(1, sizeof(ge_bg));
298         GE_CHECK_NULL(bg_data);
299
300         Evas_Object *base = NULL;
301         base = _ge_rotate_bg_add_layout(parent, GE_EDJ_FILE,
302                                         GE_ROTATE_BG_GROUP);
303         if (base == NULL) {
304                 GE_FREE(bg_data);
305                 return NULL;
306         }
307
308         bg_data->base= base;
309         bg_data->b_preload= b_preload;
310         if (!b_preload)
311                 evas_object_event_callback_add(base, EVAS_CALLBACK_RESIZE,
312                                                __ge_rotate_bg_custom_resize,
313                                                bg_data);
314         evas_object_event_callback_add(base, EVAS_CALLBACK_DEL,
315                                        __ge_rotate_bg_delete_cb, bg_data);
316
317         evas_object_data_set(base, GE_ROTATE_BG_DATA_KEY, bg_data);
318         return base;
319 }
320
321 int _ge_rotate_bg_set_file(Evas_Object *bg, const char *file, int w, int h)
322 {
323         GE_CHECK_VAL(file, -1);
324         GE_CHECK_VAL(bg, -1);
325
326         ge_bg *bg_data = evas_object_data_get(bg, GE_ROTATE_BG_DATA_KEY);
327         GE_CHECK_VAL(bg_data, -1);
328
329         Evas_Object *image_obj = NULL;
330         if (!bg_data->b_preload) {
331                 image_obj = evas_object_image_add(evas_object_evas_get(bg));
332                 evas_object_repeat_events_set(image_obj, EINA_TRUE);
333                 bg_data->img = image_obj;
334                 bg_data->file = strdup(file);
335                 elm_object_part_content_set(bg, "elm.swallow.image", image_obj);
336                 evas_object_image_load_size_set(image_obj, w, h);
337                 evas_object_image_file_set(image_obj, file, NULL);
338                 evas_object_image_preload(image_obj, EINA_TRUE);
339         } else {
340                 image_obj = elm_image_add(bg);
341                 evas_object_repeat_events_set(image_obj, EINA_TRUE);
342                 bg_data->img = image_obj;
343                 bg_data->file = strdup(file);
344                 elm_object_part_content_set(bg, "elm.swallow.image", image_obj);
345                 elm_image_fill_outside_set(image_obj, EINA_TRUE);
346                 elm_image_file_set(image_obj, file, NULL);
347                 elm_image_preload_disabled_set(image_obj, EINA_FALSE);
348                 elm_image_smooth_set(image_obj, EINA_FALSE);
349         }
350         return 0;
351 }
352
353 int _ge_rotate_bg_rotate_image(Evas_Object *bg, unsigned int orient)
354 {
355         GE_CHECK_VAL(bg, -1);
356
357         ge_bg *bg_data = evas_object_data_get(bg, GE_ROTATE_BG_DATA_KEY);
358         GE_CHECK_VAL(bg_data, -1);
359
360         if (bg_data->file && g_strcmp0(bg_data->file, GE_ICON_NO_THUMBNAIL) &&
361             g_strcmp0(bg_data->file, GE_ICON_CONTENTS_BROKEN))
362                 __ge_rotate_bg_rotate_image(bg_data->img, orient);
363         else
364                 __ge_rotate_bg_rotate_image(bg_data->img, GE_ORIENTATION_ROT_0);
365         if (!bg_data->b_preload)
366                 __ge_rotate_bg_custom_resize(bg_data, NULL, NULL, NULL);
367         return 0;
368 }
369
370 int _ge_rotate_bg_add_image(Evas_Object *bg, int w, int h)
371 {
372         GE_CHECK_VAL(bg, -1);
373
374         ge_bg *bg_data = evas_object_data_get(bg, GE_ROTATE_BG_DATA_KEY);
375         GE_CHECK_VAL(bg_data, -1);
376
377         Evas_Object *image_obj = NULL;
378         if (!bg_data->b_preload) {
379                 image_obj = evas_object_image_add(evas_object_evas_get(bg));
380                 evas_object_repeat_events_set(image_obj, EINA_TRUE);
381                 bg_data->img = image_obj;
382                 elm_object_part_content_set(bg, "elm.swallow.image", image_obj);
383                 evas_object_image_load_size_set(image_obj, w, h);
384         } else {
385                 image_obj = elm_image_add(bg);
386                 evas_object_repeat_events_set(image_obj, EINA_TRUE);
387                 bg_data->img = image_obj;
388                 elm_object_part_content_set(bg, "elm.swallow.image", image_obj);
389         }
390         return 0;
391 }
392
393 int _ge_rotate_bg_set_image_file(Evas_Object *bg, const char *file)
394 {
395         GE_CHECK_VAL(bg, -1);
396
397         ge_bg *bg_data = evas_object_data_get(bg, GE_ROTATE_BG_DATA_KEY);
398         GE_CHECK_VAL(bg_data, -1);
399         GE_CHECK_VAL(bg_data->img, -1);
400
401         if (!bg_data->b_preload) {
402                 evas_object_image_file_set(bg_data->img, file, NULL);
403                 evas_object_image_preload(bg_data->img, EINA_FALSE);
404                 __ge_rotate_bg_custom_resize(bg_data, NULL, NULL, NULL);
405         } else {
406                 elm_image_fill_outside_set(bg_data->img, EINA_TRUE);
407                 elm_image_file_set(bg_data->img, file, NULL);
408                 elm_image_preload_disabled_set(bg_data->img, EINA_FALSE);
409                 elm_image_smooth_set(bg_data->img, EINA_FALSE);
410         }
411         return 0;
412 }
413
414 #endif