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