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