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