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