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