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