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