Merge "elementary_config is added as install file"
[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
299 #ifdef HAVE_ELEMENTARY_X
300    if (wd->vkb_state == ECORE_X_VIRTUAL_KEYBOARD_STATE_OFF) return;
301 #endif
302
303    focus_obj = elm_widget_focused_object_get(conformant);
304    if (focus_obj)
305    {
306       Evas_Coord x, y, w, h;
307
308       elm_widget_show_region_get(focus_obj, &x, &y, &w, &h);
309
310       if (h < _elm_config->finger_size)
311          h = _elm_config->finger_size;
312
313       elm_widget_show_region_set(focus_obj, x, y, w, h, EINA_TRUE);
314    }
315 }
316
317 static void
318 _update_autoscroll_objs(void *data)
319 {
320    const char *type;
321    int i;
322    Evas_Object *sub, *top_scroller = NULL;
323    Evas_Object *conformant = (Evas_Object *) data;
324    Widget_Data *wd = elm_widget_data_get(data);
325
326    if (!wd) return;
327
328    sub = elm_widget_focused_object_get(conformant);
329    //Look up for Top most scroller in the Focus Object hierarchy inside Conformant.
330
331    while (sub)
332      {
333         type = elm_widget_type_get(sub);
334         if (!strcmp(type, "conformant")) break;
335         for (i = 0; i < SUB_TYPE_COUNT; i++)
336           if (!strcmp(type, sub_type[i]))
337                {
338                   top_scroller = sub;
339                   break;
340                }
341         sub = elm_object_parent_widget_get(sub);
342      }
343
344    //If the scroller got changed by app, replace it.
345    if (top_scroller != wd->scroller)
346      {
347         if (wd->scroller) evas_object_event_callback_del(wd->scroller,
348                                                    EVAS_CALLBACK_RESIZE,
349                                                    _content_resize_event_cb);
350         wd->scroller = top_scroller;
351         if (wd->scroller) evas_object_event_callback_add(wd->scroller,
352                                                    EVAS_CALLBACK_RESIZE,
353                                                    _content_resize_event_cb,
354                                                    data);
355      }
356 }
357
358 static Eina_Bool
359 _prop_change(void *data, int type __UNUSED__, void *event)
360 {
361 #ifdef HAVE_ELEMENTARY_X
362    Ecore_X_Event_Window_Property *ev;
363    Widget_Data *wd = elm_widget_data_get(data);
364    if (!wd) return ECORE_CALLBACK_PASS_ON;
365    ev = event;
366    if (ev->atom == ECORE_X_ATOM_E_ILLUME_ZONE) 
367      {
368         Conformant_Part_Type part_type;
369
370         part_type =  (ELM_CONFORM_INDICATOR_PART |
371                       ELM_CONFORM_VIRTUAL_KEYPAD_PART |
372                       ELM_CONFORM_SOFTKEY_PART);
373         _conformant_part_sizing_eval(data, part_type);
374         evas_object_event_callback_add(data, EVAS_CALLBACK_RESIZE,
375                                        _conformant_move_resize_event_cb, data);
376         evas_object_event_callback_add(data, EVAS_CALLBACK_MOVE,
377                                        _conformant_move_resize_event_cb, data);
378      }
379    else if (ev->atom == ECORE_X_ATOM_E_ILLUME_INDICATOR_GEOMETRY) 
380      _conformant_part_sizing_eval(data, ELM_CONFORM_INDICATOR_PART);
381    else if (ev->atom == ECORE_X_ATOM_E_ILLUME_SOFTKEY_GEOMETRY) 
382      _conformant_part_sizing_eval(data, ELM_CONFORM_SOFTKEY_PART);
383    else if (ev->atom == ECORE_X_ATOM_E_ILLUME_KEYBOARD_GEOMETRY) 
384      _conformant_part_sizing_eval(data, ELM_CONFORM_VIRTUAL_KEYPAD_PART);
385    else if (ev->atom == ECORE_X_ATOM_E_VIRTUAL_KEYBOARD_STATE)
386      {
387         Ecore_X_Window zone;
388
389         zone = ecore_x_e_illume_zone_get(ev->win);
390         wd->vkb_state = ecore_x_e_virtual_keyboard_state_get(zone);
391         if (wd->vkb_state == ECORE_X_VIRTUAL_KEYBOARD_STATE_OFF)
392           {
393              evas_object_size_hint_min_set(wd->virtualkeypad, -1, 0);
394              evas_object_size_hint_max_set(wd->virtualkeypad, -1, 0);
395           }
396         else
397           _update_autoscroll_objs(data);
398      }
399 #endif
400
401    return ECORE_CALLBACK_PASS_ON;
402 }
403
404 /**
405  * Add a new Conformant object
406  *
407  * @param parent The parent object
408  * @return The new conformant object or NULL if it cannot be created
409  *
410  * @ingroup Conformant
411  */
412 EAPI Evas_Object *
413 elm_conformant_add(Evas_Object *parent)
414 {
415    Evas_Object *obj;
416    Evas *e;
417    Widget_Data *wd;
418
419    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
420
421    ELM_SET_WIDTYPE(widtype, "conformant");
422    elm_widget_type_set(obj, "conformant");
423    elm_widget_sub_object_add(parent, obj);
424    elm_widget_data_set(obj, wd);
425    elm_widget_del_hook_set(obj, _del_hook);
426    elm_widget_theme_hook_set(obj, _theme_hook);
427    elm_widget_can_focus_set(obj, EINA_FALSE);
428
429    wd->base = edje_object_add(e);
430    _elm_theme_object_set(obj, wd->base, "conformant", "base", "default");
431    elm_widget_resize_object_set(obj, wd->base);
432    _swallow_conformant_parts(obj);
433
434 #ifdef HAVE_ELEMENTARY_X
435    wd->prop_hdl = ecore_event_handler_add(ECORE_X_EVENT_WINDOW_PROPERTY,
436                                           _prop_change, obj);
437    wd->vkb_state = ECORE_X_VIRTUAL_KEYBOARD_STATE_OFF;
438 #endif
439
440    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
441
442    _mirrored_set(obj, elm_widget_mirrored_get(obj));
443    _sizing_eval(obj);
444    return obj;
445 }
446
447 /**
448  * Set the content of the conformant widget
449  *
450  * Once the content object is set, a previously set one will be deleted.
451  * If you want to keep that old content object, use the
452  * elm_conformat_content_unset() function.
453  *
454  * @param obj The conformant object
455  * @return The content that was being used
456  *
457  * @ingroup Conformant
458  */
459 EAPI void
460 elm_conformant_content_set(Evas_Object *obj, Evas_Object *content)
461 {
462    ELM_CHECK_WIDTYPE(obj, widtype);
463    Widget_Data *wd = elm_widget_data_get(obj);
464    if (!wd) return;
465    if (wd->content == content) return;
466    if (wd->content) evas_object_del(wd->content);
467    wd->content = content;
468    if (content)
469      {
470         elm_widget_sub_object_add(obj, content);
471         evas_object_event_callback_add(content, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
472                                        _changed_size_hints, obj);
473         edje_object_part_swallow(wd->base, "elm.swallow.content", content);
474      }
475    _sizing_eval(obj);
476 }
477
478 /**
479  * Get the content of the conformant widget
480  *
481  * Return the content object which is set for this widget;
482  *
483  * @param obj The conformant object
484  * @return The content that is being used
485  *
486  * @ingroup Conformant
487  */
488 EAPI Evas_Object *
489 elm_conformant_content_get(const Evas_Object *obj)
490 {
491    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
492    Widget_Data *wd = elm_widget_data_get(obj);
493    if (!wd) return NULL;
494    return wd->content;
495 }
496
497 /**
498  * Unset the content of the conformant widget
499  *
500  * Unparent and return the content object which was set for this widget;
501  *
502  * @param obj The conformant object
503  * @return The content that was being used
504  *
505  * @ingroup Conformant
506  */
507 EAPI Evas_Object *
508 elm_conformant_content_unset(Evas_Object *obj)
509 {
510    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
511    Widget_Data *wd = elm_widget_data_get(obj);
512    Evas_Object *content;
513    if (!wd) return NULL;
514    if (!wd->content) return NULL;
515    content = wd->content;
516    elm_widget_sub_object_del(obj, wd->content);
517    edje_object_part_unswallow(wd->base, wd->content);
518    wd->content = NULL;
519    return content;
520 }
521
522 /**
523  * Returns the Evas_Object that represents the content area.
524  *
525  * @param obj The conformant object.
526  * @return The content area of the widget.
527  *
528  * @ingroup Conformant
529  */
530 EAPI Evas_Object*
531 elm_conformant_content_area_get(Evas_Object *obj)
532 {
533    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
534    Widget_Data *wd = elm_widget_data_get(obj);
535
536    if (!wd) return NULL;
537    /*Finger waggle warning*/
538    _elm_dangerous_call_check(__FUNCTION__);
539    return (Evas_Object*)edje_object_part_object_get(wd->base, "elm.swallow.content");
540 }
541