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