Merge "[elm] add shots feature from upstream"
[framework/uifw/elementary.git] / src / lib / elm_conform.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 /**
5  * @defgroup Conformant Conformant
6  *
7  * The aim is to provide a widget that can be used in elementary apps to 
8  * account for space taken up by the indicator, virtual keypad & softkey windows when running 
9  * the illume2 module of E17.
10  */
11
12 typedef struct _Widget_Data Widget_Data;
13 struct _Widget_Data
14 {
15    Evas_Object *base;
16    Evas_Object *shelf, *panel, *virtualkeypad;
17    Evas_Object *content;
18    Evas_Object *scroller;
19 #ifdef HAVE_ELEMENTARY_X
20    Ecore_Event_Handler *prop_hdl;
21    Ecore_X_Virtual_Keyboard_State vkb_state;
22 #endif
23    struct {
24         Ecore_Animator *animator; // animaton timer
25         double start; // time started
26         Evas_Coord auto_x, auto_y; // desired delta
27         Evas_Coord x, y; // current delta
28    } delta;
29 };
30
31 /* Enum to identify conformant swallow parts */
32 typedef enum _Conformant_Part_Type Conformant_Part_Type;
33 enum _Conformant_Part_Type
34 {
35    ELM_CONFORM_INDICATOR_PART      = 1,
36    ELM_CONFORM_VIRTUAL_KEYPAD_PART = 2,
37    ELM_CONFORM_SOFTKEY_PART        = 4
38 };
39 #define SUB_TYPE_COUNT 2
40 static char *sub_type[SUB_TYPE_COUNT] = { "scroller", "genlist" };
41
42 /* local function prototypes */
43 static const char *widtype = NULL;
44 static void _del_pre_hook(Evas_Object *obj);
45 static void _del_hook(Evas_Object *obj);
46 static void _mirrored_set(Evas_Object *obj, Eina_Bool rtl);
47 static void _theme_hook(Evas_Object *obj);
48 static void
49 _swallow_conformant_parts(Evas_Object *obj);
50 static void
51 _conformant_part_size_set(Evas_Object *obj, Evas_Object *sobj, Evas_Coord sx,
52                           Evas_Coord sy, Evas_Coord sw, Evas_Coord sh);
53 static void
54 _conformant_part_sizing_eval(Evas_Object *obj, Conformant_Part_Type part_type);
55 static void
56 _conformant_move_resize_event_cb(void *data, Evas *e, Evas_Object *obj,
57                                  void *event_info);
58 static void _sizing_eval(Evas_Object *obj);
59 static Eina_Bool _prop_change(void *data, int type, void *event);
60
61 /* local functions */
62 static void
63 _del_pre_hook(Evas_Object *obj)
64 {
65    Widget_Data *wd = elm_widget_data_get(obj);
66    if (!wd) return;
67 #ifdef HAVE_ELEMENTARY_X
68    if (wd->prop_hdl) ecore_event_handler_del(wd->prop_hdl);
69 #endif
70 }
71
72 static void
73 _del_hook(Evas_Object *obj)
74 {
75    Widget_Data *wd = elm_widget_data_get(obj);
76    if (!wd) return;
77    free(wd);
78 }
79
80 static void
81 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
82 {
83    Widget_Data *wd = elm_widget_data_get(obj);
84    if (!wd) return;
85    edje_object_mirrored_set(wd->base, rtl);
86 }
87
88 static void 
89 _theme_hook(Evas_Object *obj) 
90 {
91    Widget_Data *wd = elm_widget_data_get(obj);
92
93    if (!wd) return;
94    _elm_widget_mirrored_reload(obj);
95    _mirrored_set(obj, elm_widget_mirrored_get(obj));
96    _elm_theme_object_set(obj, wd->base, "conformant", "base", elm_widget_style_get(obj));
97    _swallow_conformant_parts(obj);
98
99    if (wd->content)
100      edje_object_part_swallow(wd->base, "elm.swallow.content", wd->content);
101    edje_object_scale_set(wd->base, elm_widget_scale_get(obj) * _elm_config->scale);
102    _sizing_eval(obj);
103 }
104
105 static void
106 _sizing_eval(Evas_Object *obj)
107 {
108    Widget_Data *wd = elm_widget_data_get(obj);
109    Evas_Coord mw = -1, mh = -1;
110    if (!wd) return;
111    edje_object_size_min_calc(wd->base, &mw, &mh);
112    evas_object_size_hint_min_set(obj, mw, mh);
113    evas_object_size_hint_max_set(obj, -1, -1);
114 }
115
116 static void
117 _conformant_part_size_set(Evas_Object *obj, Evas_Object *sobj, Evas_Coord sx,
118                           Evas_Coord sy, Evas_Coord sw, Evas_Coord sh)
119 {
120    Evas_Coord cx, cy, cw, ch;
121    Evas_Coord part_height = 0, part_width = 0;
122
123    evas_object_geometry_get(obj, &cx, &cy, &cw, &ch);
124
125    /* Part overlapping with conformant */
126    if ((cx < (sx+sw)) && ((cx+cw) > sx) && (cy < (sy+sh)) && ((cy+ch) > sy))
127      {
128         part_height = MIN((cy+ch), (sy+sh)) - MAX(cy, sy);
129         part_width = MIN((cx+cw), (sx+sw)) - MAX(cx, sx);
130      }
131
132    evas_object_size_hint_min_set(sobj, part_width, part_height);
133    evas_object_size_hint_max_set(sobj, part_width, part_height);
134 }
135
136 static void
137 _conformant_part_sizing_eval(Evas_Object *obj, Conformant_Part_Type part_type)
138 {
139 #ifdef HAVE_ELEMENTARY_X
140    Ecore_X_Window zone, xwin;
141    int sx = -1, sy = -1, sw = -1, sh = -1;
142    Widget_Data *wd = elm_widget_data_get(obj);
143
144    if (!wd) return;
145
146    xwin = elm_win_xwindow_get(obj);
147    zone = ecore_x_e_illume_zone_get(xwin);
148
149    if (part_type & ELM_CONFORM_INDICATOR_PART)
150      {
151         ecore_x_e_illume_indicator_geometry_get(zone, &sx, &sy, &sw, &sh);
152         _conformant_part_size_set(obj, wd->shelf, sx, sy, sw, sh);
153      }
154    if (part_type & ELM_CONFORM_VIRTUAL_KEYPAD_PART)
155      {
156         ecore_x_e_illume_keyboard_geometry_get(zone, &sx, &sy, &sw, &sh);
157         _conformant_part_size_set(obj,wd->virtualkeypad, sx, sy, sw, sh);
158      }
159    if (part_type & ELM_CONFORM_SOFTKEY_PART)
160      {
161         ecore_x_e_illume_softkey_geometry_get(zone, &sx, &sy, &sw, &sh);
162         _conformant_part_size_set(obj, wd->panel, sx, sy, sw, sh);
163      }
164 #endif
165 }
166
167 static void
168 _swallow_conformant_parts(Evas_Object *obj)
169 {
170    Widget_Data *wd = elm_widget_data_get(obj);
171
172    wd->scroller = NULL;
173    if (!wd->shelf)
174      {
175         wd->shelf = evas_object_rectangle_add(evas_object_evas_get(obj));
176         elm_widget_sub_object_add(obj, wd->shelf);
177         evas_object_size_hint_min_set(wd->shelf, -1, 0);
178         evas_object_size_hint_max_set(wd->shelf, -1, 0);
179      }
180    else
181      _conformant_part_sizing_eval(obj, ELM_CONFORM_INDICATOR_PART);
182
183    evas_object_color_set(wd->shelf, 0, 0, 0, 0);
184    edje_object_part_swallow(wd->base, "elm.swallow.shelf", wd->shelf);
185
186    if (!wd->virtualkeypad)
187      {
188         wd->virtualkeypad = evas_object_rectangle_add(evas_object_evas_get(obj));
189         elm_widget_sub_object_add(obj, wd->virtualkeypad);
190         evas_object_size_hint_min_set(wd->virtualkeypad, -1, 0);
191         evas_object_size_hint_max_set(wd->virtualkeypad, -1, 0);
192      }
193    else
194      _conformant_part_sizing_eval(obj, ELM_CONFORM_VIRTUAL_KEYPAD_PART);
195
196    evas_object_color_set(wd->virtualkeypad, 0, 0, 0, 0);
197    edje_object_part_swallow(wd->base, "elm.swallow.virtualkeypad",
198                             wd->virtualkeypad);
199
200    if (!wd->panel)
201      {
202         wd->panel = evas_object_rectangle_add(evas_object_evas_get(obj));
203         elm_widget_sub_object_add(obj, wd->panel);
204         evas_object_size_hint_min_set(wd->panel, -1, 0);
205         evas_object_size_hint_max_set(wd->panel, -1, 0);
206      }
207    else
208      _conformant_part_sizing_eval(obj, ELM_CONFORM_SOFTKEY_PART);
209
210    evas_object_color_set(wd->panel, 0, 0, 0, 0);
211    edje_object_part_swallow(wd->base, "elm.swallow.panel", wd->panel);
212 }
213
214 static void
215 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
216 {
217    Widget_Data *wd = elm_widget_data_get(data);
218    if (!wd) return;
219    _sizing_eval(data);
220 }
221
222 static void
223 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
224 {
225    Widget_Data *wd = elm_widget_data_get(obj);
226    Evas_Object *sub = event_info;
227    if (!wd) return;
228    if (sub == wd->content)
229      {
230         evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
231                                             _changed_size_hints, obj);
232         wd->content = NULL;
233         _sizing_eval(obj);
234      }
235 }
236
237 /* unused now - but meant to be for making sure the focused widget is always
238  * visible when the vkbd comes and goes by moving the conformant obj (and thus
239  * its children) to  show the focused widget (and if focus changes follow)
240
241 static Evas_Object *
242 _focus_object_get(const Evas_Object *obj)
243 {
244    Evas_Object *win, *foc;
245
246    win = elm_widget_top_get(obj);
247    if (!win) return NULL;
248    foc = elm_widget_top_get(win);
249 }
250
251 static void
252 _focus_object_region_get(const Evas_Object *obj, Evas_Coord *x, Evas_Coord *y, Evas_Coord *w, Evas_Coord *h)
253 {
254    evas_object_geometry_get(obj, x, y, w, h);
255 }
256
257 static void
258 _focus_change_del(void *data, Evas_Object *obj, void *event_info)
259 {
260    // called from toplevel when the focused window shanges
261 }
262
263 static void
264 _autoscroll_move(Evas_Object *obj)
265 {
266    // move conformant edje by delta to show focused widget
267 }
268
269 static void
270 _autoscroll_mode_enable(Evas_Object *obj)
271 {
272 // called when autoscroll mode should be on - content area smaller than
273 // its min size
274 // 1. get focused object
275 // 2. if not in visible conformant area calculate delta needed to
276 //    get it in
277 // 3. store delta and call _autoscroll_move() which either asanimates
278 //    or jumps right there
279 }
280
281 static void
282 _autoscroll_mode_disable(Evas_Object *obj)
283 {
284 // called when autoscroll mode should be off - set delta to 0 and
285 // call _autoscroll_move()
286 }
287  */
288
289 static void
290 _conformant_move_resize_event_cb(void *data __UNUSED__, Evas *e __UNUSED__,
291                                  Evas_Object *obj,
292                                  void *event_info __UNUSED__)
293 {
294    Conformant_Part_Type part_type;
295    Widget_Data *wd = elm_widget_data_get(obj);
296
297    if (!wd) return;
298    part_type =  (ELM_CONFORM_INDICATOR_PART |
299                  ELM_CONFORM_VIRTUAL_KEYPAD_PART |
300                  ELM_CONFORM_SOFTKEY_PART);
301    _conformant_part_sizing_eval(obj, part_type);
302 }
303
304 static void
305 _content_resize_event_cb(void *data, Evas *e __UNUSED__,
306                          Evas_Object *obj __UNUSED__,
307                          void *event_info __UNUSED__)
308 {
309    Evas_Object *focus_obj;
310    Evas_Object *conformant = (Evas_Object *) data;
311    Widget_Data *wd = elm_widget_data_get(conformant);
312    if (!wd) return;
313
314 #ifdef HAVE_ELEMENTARY_X
315    if (wd->vkb_state == ECORE_X_VIRTUAL_KEYBOARD_STATE_OFF) return;
316 #endif
317
318    focus_obj = elm_widget_focused_object_get(conformant);
319    if (focus_obj)
320    {
321       Evas_Coord x, y, w, h;
322
323       elm_widget_show_region_get(focus_obj, &x, &y, &w, &h);
324
325       if (h < _elm_config->finger_size)
326          h = _elm_config->finger_size;
327
328       elm_widget_show_region_set(focus_obj, x, y, w, h, EINA_TRUE);
329    }
330 }
331
332 static void
333 _update_autoscroll_objs(void *data)
334 {
335    const char *type;
336    int i;
337    Evas_Object *sub, *top_scroller = NULL;
338    Evas_Object *conformant = (Evas_Object *) data;
339    Widget_Data *wd = elm_widget_data_get(data);
340
341    if (!wd) return;
342
343    sub = elm_widget_focused_object_get(conformant);
344    //Look up for Top most scroller in the Focus Object hierarchy inside Conformant.
345
346    while (sub)
347      {
348         type = elm_widget_type_get(sub);
349         if (!strcmp(type, "conformant")) break;
350         for (i = 0; i < SUB_TYPE_COUNT; i++)
351           if (!strcmp(type, sub_type[i]))
352                {
353                   top_scroller = sub;
354                   break;
355                }
356         sub = elm_object_parent_widget_get(sub);
357      }
358
359    //If the scroller got changed by app, replace it.
360    if (top_scroller != wd->scroller)
361      {
362         if (wd->scroller) evas_object_event_callback_del(wd->scroller,
363                                                    EVAS_CALLBACK_RESIZE,
364                                                    _content_resize_event_cb);
365         wd->scroller = top_scroller;
366         if (wd->scroller) evas_object_event_callback_add(wd->scroller,
367                                                    EVAS_CALLBACK_RESIZE,
368                                                    _content_resize_event_cb,
369                                                    data);
370      }
371 }
372
373 static Eina_Bool
374 _prop_change(void *data, int type __UNUSED__, void *event)
375 {
376 #ifdef HAVE_ELEMENTARY_X
377    Ecore_X_Event_Window_Property *ev;
378    Widget_Data *wd = elm_widget_data_get(data);
379    if (!wd) return ECORE_CALLBACK_PASS_ON;
380    ev = event;
381    if (ev->atom == ECORE_X_ATOM_E_ILLUME_ZONE) 
382      {
383         Conformant_Part_Type part_type;
384
385         part_type =  (ELM_CONFORM_INDICATOR_PART |
386                       ELM_CONFORM_VIRTUAL_KEYPAD_PART |
387                       ELM_CONFORM_SOFTKEY_PART);
388         _conformant_part_sizing_eval(data, part_type);
389      }
390    else if (ev->atom == ECORE_X_ATOM_E_ILLUME_INDICATOR_GEOMETRY) 
391      _conformant_part_sizing_eval(data, ELM_CONFORM_INDICATOR_PART);
392    else if (ev->atom == ECORE_X_ATOM_E_ILLUME_SOFTKEY_GEOMETRY) 
393      _conformant_part_sizing_eval(data, ELM_CONFORM_SOFTKEY_PART);
394    else if (ev->atom == ECORE_X_ATOM_E_ILLUME_KEYBOARD_GEOMETRY) 
395      _conformant_part_sizing_eval(data, ELM_CONFORM_VIRTUAL_KEYPAD_PART);
396    else if (ev->atom == ECORE_X_ATOM_E_VIRTUAL_KEYBOARD_STATE)
397      {
398         Ecore_X_Window zone;
399
400         zone = ecore_x_e_illume_zone_get(ev->win);
401         wd->vkb_state = ecore_x_e_virtual_keyboard_state_get(zone);
402         if (wd->vkb_state == ECORE_X_VIRTUAL_KEYBOARD_STATE_OFF)
403           {
404              evas_object_size_hint_min_set(wd->virtualkeypad, -1, 0);
405              evas_object_size_hint_max_set(wd->virtualkeypad, -1, 0);
406           }
407         else
408           _update_autoscroll_objs(data);
409      }
410 #endif
411
412    return ECORE_CALLBACK_PASS_ON;
413 }
414
415 /**
416  * Add a new Conformant object
417  *
418  * @param parent The parent object
419  * @return The new conformant object or NULL if it cannot be created
420  *
421  * @ingroup Conformant
422  */
423 EAPI Evas_Object *
424 elm_conformant_add(Evas_Object *parent)
425 {
426    Evas_Object *obj;
427    Evas *e;
428    Widget_Data *wd;
429
430    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
431
432    ELM_SET_WIDTYPE(widtype, "conformant");
433    elm_widget_type_set(obj, "conformant");
434    elm_widget_sub_object_add(parent, obj);
435    elm_widget_data_set(obj, wd);
436    elm_widget_del_pre_hook_set(obj, _del_pre_hook);
437    elm_widget_del_hook_set(obj, _del_hook);
438    elm_widget_theme_hook_set(obj, _theme_hook);
439    elm_widget_can_focus_set(obj, EINA_FALSE);
440
441    wd->base = edje_object_add(e);
442    _elm_theme_object_set(obj, wd->base, "conformant", "base", "default");
443    elm_widget_resize_object_set(obj, wd->base);
444    _swallow_conformant_parts(obj);
445
446 #ifdef HAVE_ELEMENTARY_X
447    wd->prop_hdl = ecore_event_handler_add(ECORE_X_EVENT_WINDOW_PROPERTY,
448                                           _prop_change, obj);
449    wd->vkb_state = ECORE_X_VIRTUAL_KEYBOARD_STATE_OFF;
450 #endif
451
452    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
453    evas_object_event_callback_add(obj, EVAS_CALLBACK_RESIZE,
454                                        _conformant_move_resize_event_cb, obj);
455    evas_object_event_callback_add(obj, EVAS_CALLBACK_MOVE,
456                                        _conformant_move_resize_event_cb, obj);
457
458    _mirrored_set(obj, elm_widget_mirrored_get(obj));
459    _sizing_eval(obj);
460    return obj;
461 }
462
463 /**
464  * Set the content of the conformant widget
465  *
466  * Once the content object is set, a previously set one will be deleted.
467  * If you want to keep that old content object, use the
468  * elm_conformat_content_unset() function.
469  *
470  * @param obj The conformant object
471  * @return The content that was being used
472  *
473  * @ingroup Conformant
474  */
475 EAPI void
476 elm_conformant_content_set(Evas_Object *obj, Evas_Object *content)
477 {
478    ELM_CHECK_WIDTYPE(obj, widtype);
479    Widget_Data *wd = elm_widget_data_get(obj);
480    if (!wd) return;
481    if (wd->content == content) return;
482    if (wd->content) evas_object_del(wd->content);
483    wd->content = content;
484    if (content)
485      {
486         elm_widget_sub_object_add(obj, content);
487         evas_object_event_callback_add(content, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
488                                        _changed_size_hints, obj);
489         edje_object_part_swallow(wd->base, "elm.swallow.content", content);
490      }
491    _sizing_eval(obj);
492 }
493
494 /**
495  * Get the content of the conformant widget
496  *
497  * Return the content object which is set for this widget;
498  *
499  * @param obj The conformant object
500  * @return The content that is being used
501  *
502  * @ingroup Conformant
503  */
504 EAPI Evas_Object *
505 elm_conformant_content_get(const Evas_Object *obj)
506 {
507    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
508    Widget_Data *wd = elm_widget_data_get(obj);
509    if (!wd) return NULL;
510    return wd->content;
511 }
512
513 /**
514  * Unset the content of the conformant widget
515  *
516  * Unparent and return the content object which was set for this widget;
517  *
518  * @param obj The conformant object
519  * @return The content that was being used
520  *
521  * @ingroup Conformant
522  */
523 EAPI Evas_Object *
524 elm_conformant_content_unset(Evas_Object *obj)
525 {
526    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
527    Widget_Data *wd = elm_widget_data_get(obj);
528    Evas_Object *content;
529    if (!wd) return NULL;
530    if (!wd->content) return NULL;
531    content = wd->content;
532    elm_widget_sub_object_del(obj, wd->content);
533    edje_object_part_unswallow(wd->base, wd->content);
534    wd->content = NULL;
535    return content;
536 }
537
538 /**
539  * Returns the Evas_Object that represents the content area.
540  *
541  * @param obj The conformant object.
542  * @return The content area of the widget.
543  *
544  * @ingroup Conformant
545  */
546 EAPI Evas_Object*
547 elm_conformant_content_area_get(Evas_Object *obj)
548 {
549    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
550    Widget_Data *wd = elm_widget_data_get(obj);
551
552    if (!wd) return NULL;
553    /*Finger waggle warning*/
554    _elm_dangerous_call_check(__FUNCTION__);
555    return (Evas_Object*)edje_object_part_object_get(wd->base, "elm.swallow.content");
556 }
557