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