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