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