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