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