Merge branch 'master' into svn_merge
[framework/uifw/elementary.git] / src / lib / elm_photo.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 /**
5  * @defgroup Photo Photo
6  * @ingroup Elementary
7  *
8  * For displaying the photo of a person (contact). Simple yet
9  * with a very specific purpose.
10  *
11  * Signals that you can add callbacks for are:
12  *
13  * "clicked" - This is called when a user has clicked the photo
14  * "drag,start" - Someone started dragging the image out of the object
15  * "drag,end" - Dragged item was dropped (somewhere)
16  */
17
18 typedef struct _Widget_Data Widget_Data;
19
20 struct _Widget_Data
21 {
22    Evas_Object *frm;
23    Evas_Object *img;
24    int size;
25    Eina_Bool fill;
26    Ecore_Timer *longtimer;
27 };
28
29 static const char *widtype = NULL;
30 static void _del_hook(Evas_Object *obj);
31 static void _mirrored_set(Evas_Object *obj, Eina_Bool rtl);
32 static void _theme_hook(Evas_Object *obj);
33 static void _sizing_eval(Evas_Object *obj);
34 static void _mouse_up(void *data, Evas *e, Evas_Object *obj, void *event_info);
35 static void _mouse_move(void *data, Evas *e, Evas_Object *obj, void *event_info);
36
37 static const char SIG_CLICKED[] = "clicked";
38 static const char SIG_DRAG_START[] = "drag,start";
39 static const char SIG_DRAG_END[] = "drag,end";
40
41 static const Evas_Smart_Cb_Description _signals[] = {
42    {SIG_CLICKED, ""},
43    {SIG_DRAG_START, ""},
44    {SIG_DRAG_END, ""},
45    {NULL, NULL}
46 };
47
48
49 static void
50 _del_hook(Evas_Object *obj)
51 {
52    Widget_Data *wd = elm_widget_data_get(obj);
53    if (!wd) return;
54    free(wd);
55 }
56
57 static void
58 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
59 {
60    Widget_Data *wd = elm_widget_data_get(obj);
61    if (!wd) return;
62    edje_object_mirrored_set(wd->frm, rtl);
63 }
64
65 static void
66 _theme_hook(Evas_Object *obj)
67 {
68    Widget_Data *wd = elm_widget_data_get(obj);
69    if (!wd) return;
70    _elm_widget_mirrored_reload(obj);
71    _mirrored_set(wd->frm, elm_widget_mirrored_get(obj));
72    _elm_theme_object_set(obj, wd->frm, "photo", "base",
73                          elm_widget_style_get(obj));
74    edje_object_part_swallow(wd->frm, "elm.swallow.content", wd->img);
75    edje_object_scale_set(wd->frm, elm_widget_scale_get(obj) *
76                          _elm_config->scale);
77    _sizing_eval(obj);
78 }
79
80 static void
81 _sizing_eval(Evas_Object *obj)
82 {
83    Widget_Data *wd = elm_widget_data_get(obj);
84    Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
85
86    if (!wd) return;
87    if (wd->size > 0)
88      {
89         double scale = 0.0;
90
91         scale = (wd->size * elm_widget_scale_get(obj) * _elm_config->scale);
92         evas_object_size_hint_min_set(wd->img, scale, scale);
93         edje_object_part_swallow(wd->frm, "elm.swallow.content", wd->img);
94         elm_coords_finger_size_adjust(1, &minw, 1, &minh);
95         edje_object_size_min_restricted_calc(wd->frm, &minw, &minh, minw, minh);
96         elm_coords_finger_size_adjust(1, &minw, 1, &minh);
97         maxw = minw;
98         maxh = minh;
99         evas_object_size_hint_min_set(obj, minw, minh);
100         evas_object_size_hint_max_set(obj, maxw, maxh);
101      }
102 }
103
104 static void
105 _icon_move_resize(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
106 {
107    Evas_Coord w, h;
108    Widget_Data *wd = elm_widget_data_get(data);
109
110    if (!wd) return;
111    if (wd->fill)
112      {
113         Edje_Message_Int_Set *msg;
114         Evas_Object *icon = _els_smart_icon_object_get(wd->img);
115
116         evas_object_geometry_get(icon, NULL, NULL, &w, &h);
117         msg = alloca(sizeof(Edje_Message_Int_Set) + (sizeof(int)));
118         msg->count=2;
119         msg->val[0] = (int)w;
120         msg->val[1] = (int)h;
121
122         edje_object_message_send(wd->frm, EDJE_MESSAGE_INT_SET, 0, msg);
123      }
124 }
125
126
127 static void
128 _drag_done_cb(void *unused __UNUSED__, Evas_Object *obj)
129 {
130    elm_object_scroll_freeze_pop(obj);
131    evas_object_smart_callback_call(obj, SIG_DRAG_END, NULL);
132 }
133
134 static Eina_Bool
135 _longpress(void *objv)
136 {
137    Widget_Data *wd = elm_widget_data_get(objv);
138    Evas_Object *tmp;
139    const char *file;
140    char *buf;
141
142    DBG("Long press: start drag!");
143    wd->longtimer = NULL; /* clear: must return NULL now */
144    evas_object_event_callback_del(objv, EVAS_CALLBACK_MOUSE_MOVE, _mouse_move);
145
146    tmp = _els_smart_icon_object_get(wd->img);
147    file = NULL;
148    evas_object_image_file_get(tmp,&file,NULL);
149    if (file)
150      {
151         /* FIXME: Deal with relative paths */
152         buf = malloc(strlen(file) + strlen("file://") + 1);
153         sprintf(buf, "%s%s","file://",file);
154         elm_drag_start(objv, ELM_SEL_FORMAT_IMAGE, buf, _drag_done_cb, NULL);
155         free(buf);
156      }
157    elm_object_scroll_freeze_push(objv);
158
159    evas_object_smart_callback_call(objv, SIG_DRAG_START, NULL);
160
161    return 0; /* Don't call again */
162 }
163
164 static void
165 _mouse_move(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event)
166 {
167    Widget_Data *wd = elm_widget_data_get(data);
168    Evas_Event_Mouse_Move *move = event;
169
170    /* Sanity */
171    if (!wd->longtimer)
172      {
173         evas_object_event_callback_del(obj, EVAS_CALLBACK_MOUSE_MOVE, _mouse_move);
174         return;
175      }
176
177    /* if the event is held, stop waiting */
178    if (move->event_flags & EVAS_EVENT_FLAG_ON_HOLD)
179      {
180         /* Moved too far: No longpress for you! */
181         ecore_timer_del(wd->longtimer);
182         wd->longtimer = NULL;
183         evas_object_event_callback_del(obj, EVAS_CALLBACK_MOUSE_MOVE,
184                                        _mouse_move);
185      }
186 }
187
188 static void
189 _mouse_down(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
190 {
191    Widget_Data *wd = elm_widget_data_get(data);
192
193    if (wd->longtimer) ecore_timer_del(wd->longtimer);
194
195    /* FIXME: Hard coded timeout */
196    wd->longtimer = ecore_timer_add(0.7, _longpress, data);
197    evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_MOVE,
198                                   _mouse_move, data);
199 }
200
201 static void
202 _mouse_up(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
203 {
204    Widget_Data *wd = elm_widget_data_get(data);
205
206    if ((wd) && (wd->longtimer))
207      {
208         ecore_timer_del(wd->longtimer);
209         wd->longtimer = NULL;
210      }
211
212    evas_object_smart_callback_call(data, SIG_CLICKED, NULL);
213 }
214
215
216 /**
217  * Add a new photo to the parent
218  *
219  * @param parent The parent object
220  * @return The new object or NULL if it cannot be created
221  *
222  * @ingroup Photo
223  */
224 EAPI Evas_Object *
225 elm_photo_add(Evas_Object *parent)
226 {
227    Evas_Object *obj;
228    Evas *e;
229    Widget_Data *wd;
230    Evas_Object *icon;
231
232    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
233
234    ELM_SET_WIDTYPE(widtype, "photo");
235    elm_widget_type_set(obj, "photo");
236    elm_widget_sub_object_add(parent, obj);
237    elm_widget_data_set(obj, wd);
238    elm_widget_del_hook_set(obj, _del_hook);
239    elm_widget_theme_hook_set(obj, _theme_hook);
240    elm_widget_can_focus_set(obj, EINA_FALSE);
241
242    wd->frm = edje_object_add(e);
243    _elm_theme_object_set(obj, wd->frm, "photo", "base", "default");
244    elm_widget_resize_object_set(obj, wd->frm);
245
246    wd->img = _els_smart_icon_add(e);
247    _els_smart_icon_scale_up_set(wd->img, 1);
248    _els_smart_icon_scale_down_set(wd->img, 1);
249    _els_smart_icon_smooth_scale_set(wd->img, 1);
250    _els_smart_icon_fill_inside_set(wd->img, 0);
251    _els_smart_icon_scale_size_set(wd->img, 0);
252    wd->fill = EINA_FALSE;
253    _els_smart_icon_scale_set(wd->img,
254                              elm_widget_scale_get(obj) * _elm_config->scale);
255    evas_object_event_callback_add(wd->img, EVAS_CALLBACK_MOUSE_UP,
256                                   _mouse_up, obj);
257    evas_object_event_callback_add(wd->img, EVAS_CALLBACK_MOUSE_DOWN,
258                                   _mouse_down, obj);
259    evas_object_repeat_events_set(wd->img, 1);
260    edje_object_part_swallow(wd->frm, "elm.swallow.content", wd->img);
261    evas_object_show(wd->img);
262    elm_widget_sub_object_add(obj, wd->img);
263
264    wd->longtimer = NULL;
265
266    icon = _els_smart_icon_object_get(wd->img);
267    evas_object_event_callback_add(icon, EVAS_CALLBACK_MOVE,
268                                   _icon_move_resize, obj);
269    evas_object_event_callback_add(icon, EVAS_CALLBACK_RESIZE,
270                                   _icon_move_resize, obj);
271
272    evas_object_smart_callbacks_descriptions_set(obj, _signals);
273
274    _mirrored_set(obj, elm_widget_mirrored_get(obj));
275    _sizing_eval(obj);
276    return obj;
277 }
278
279 /**
280  * Set the file that will be used as photo
281  *
282  * @param obj The photo object
283  * @param file The path to file that will be used as photo
284  *
285  * @return (1 = success, 0 = error)
286  *
287  * @ingroup Photo
288  */
289 EAPI Eina_Bool
290 elm_photo_file_set(Evas_Object *obj, const char *file)
291 {
292    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
293    Widget_Data *wd = elm_widget_data_get(obj);
294
295    if (!wd) return EINA_FALSE;
296    if (!_els_smart_icon_file_key_set(wd->img, file, NULL))
297      return EINA_FALSE;
298
299    _sizing_eval(obj);
300    return EINA_TRUE;
301 }
302
303 /**
304  * Set the size that will be used on the photo
305  *
306  * @param obj The photo object
307  * @param size The size that the photo will be
308  *
309  * @ingroup Photo
310  */
311 EAPI void
312 elm_photo_size_set(Evas_Object *obj, int size)
313 {
314    ELM_CHECK_WIDTYPE(obj, widtype);
315    Widget_Data *wd = elm_widget_data_get(obj);
316
317    if (!wd) return;
318    wd->size = (size > 0) ? size : 0;
319
320    _els_smart_icon_scale_size_set(wd->img, wd->size);
321
322    _sizing_eval(obj);
323 }
324
325 /**
326  * Set if the photo should be completely visible or not.
327  *
328  * @param obj The photo object
329  * @param fill if true the photo will be completely visible
330  *
331  * @ingroup Photo
332  */
333 EAPI void
334 elm_photo_fill_inside_set(Evas_Object *obj, Eina_Bool fill)
335 {
336    ELM_CHECK_WIDTYPE(obj, widtype);
337    Widget_Data *wd = elm_widget_data_get(obj);
338
339    if (!wd) return;
340    _els_smart_icon_fill_inside_set(wd->img, fill);
341    wd->fill = fill;
342    _sizing_eval(obj);
343 }
344
345 /**
346  * Set editability of the photo.
347  *
348  * An editable photo can be dragged to or from, and can be cut or pasted too.
349  * Note that pasting an image or dropping an item on the image will delete the
350  * existing content.
351  *
352  * @param obj The photo object.
353  * @param set To set of clear editablity.
354  */
355 EAPI void
356 elm_photo_editable_set(Evas_Object *obj, Eina_Bool set)
357 {
358    ELM_CHECK_WIDTYPE(obj, widtype);
359    Widget_Data *wd = elm_widget_data_get(obj);
360
361    if (!wd) return;;
362    _els_smart_icon_edit_set(wd->img, set, obj);
363 }
364
365 /* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-3f0^-2{2(0W1st0 :*/