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