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