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