make all widgets use a standard setup macro. cuts code down and
[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 & 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;
17    Evas_Object *content;
18    Ecore_Event_Handler *prop_hdl;
19    struct {
20       Ecore_Animator *animator; // animaton timer
21       double start; // time started
22       Evas_Coord auto_x, auto_y; // desired delta
23       Evas_Coord x, y; // current delta
24    } delta;
25 };
26
27 /* local function prototypes */
28 static const char *widtype = NULL;
29 static void _del_hook(Evas_Object *obj);
30 static void _mirrored_set(Evas_Object *obj, Eina_Bool rtl);
31 static void _theme_hook(Evas_Object *obj);
32 static void _sizing_eval(Evas_Object *obj);
33 static Eina_Bool _prop_change(void *data, int type, void *event);
34
35 /* local functions */
36 static void 
37 _del_hook(Evas_Object *obj) 
38 {
39    Widget_Data *wd = elm_widget_data_get(obj);
40    if (!wd) return;
41    if (wd->prop_hdl) ecore_event_handler_del(wd->prop_hdl);
42    free(wd);
43 }
44
45 static void 
46 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
47 {
48    Widget_Data *wd = elm_widget_data_get(obj);
49    if (!wd) return;
50    edje_object_mirrored_set(wd->base, rtl);
51 }
52
53 static void 
54 _theme_hook(Evas_Object *obj) 
55 {
56    Widget_Data *wd = elm_widget_data_get(obj);
57    if (!wd) return;
58    _elm_widget_mirrored_reload(obj);
59    _mirrored_set(obj, elm_widget_mirrored_get(obj));
60    _elm_theme_object_set(obj, wd->base, "conformant", "base", elm_widget_style_get(obj));
61    if (wd->content)
62      edje_object_part_swallow(wd->base, "elm.swallow.content", wd->content);
63    edje_object_scale_set(wd->base, elm_widget_scale_get(obj) * _elm_config->scale);
64    _sizing_eval(obj);
65 }
66
67 static void 
68 _sizing_eval(Evas_Object *obj) 
69 {
70    Widget_Data *wd = elm_widget_data_get(obj);
71    Evas_Coord mw = -1, mh = -1;
72    if (!wd) return;
73    edje_object_size_min_calc(wd->base, &mw, &mh);
74    evas_object_size_hint_min_set(obj, mw, mh);
75    evas_object_size_hint_max_set(obj, -1, -1);
76 }
77
78 static void
79 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
80 {
81    Widget_Data *wd = elm_widget_data_get(data);
82    if (!wd) return;
83    _sizing_eval(data);
84 }
85
86 static void
87 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
88 {
89    Widget_Data *wd = elm_widget_data_get(obj);
90    Evas_Object *sub = event_info;
91    if (!wd) return;
92    if (sub == wd->content)
93      {
94         evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
95                                             _changed_size_hints, obj);
96         wd->content = NULL;
97         _sizing_eval(obj);
98      }
99 }
100
101 /* unused now - but meant to be for making sure the focused widget is always
102  * visible when the vkbd comes and goes by moving the conformant obj (and thus
103  * its children) to  show the focused widget (and if focus changes follow)
104
105 static Evas_Object *
106 _focus_object_get(const Evas_Object *obj)
107 {
108    Evas_Object *win, *foc;
109    
110    win = elm_widget_top_get(obj);
111    if (!win) return NULL;
112    foc = elm_widget_top_get(win);
113 }
114
115 static void
116 _focus_object_region_get(const Evas_Object *obj, Evas_Coord *x, Evas_Coord *y, Evas_Coord *w, Evas_Coord *h)
117 {
118    evas_object_geometry_get(obj, x, y, w, h);
119 }
120
121 static void
122 _focus_change_del(void *data, Evas_Object *obj, void *event_info)
123 {
124    // called from toplevel when the focused window shanges
125 }
126
127 static void
128 _autoscroll_move(Evas_Object *obj)
129 {
130    // move conformant edje by delta to show focused widget
131 }
132
133 static void
134 _autoscroll_mode_enable(Evas_Object *obj)
135 {
136    // called when autoscroll mode should be on - content area smaller than
137    // its min size
138    // 1. get focused object
139    // 2. if not in visible conformant area calculate delta needed to
140    //    get it in
141    // 3. store delta and call _autoscroll_move() which either asanimates
142    //    or jumps right there
143 }
144
145 static void
146 _autoscroll_mode_disable(Evas_Object *obj)
147 {
148    // called when autoscroll mode should be off - set delta to 0 and
149    // call _autoscroll_move()
150 }
151 */
152
153 static Eina_Bool
154 _prop_change(void *data, int type __UNUSED__, void *event) 
155 {
156 #ifdef HAVE_ELEMENTARY_X
157    Ecore_X_Event_Window_Property *ev;
158    Widget_Data *wd = elm_widget_data_get(data);
159    if (!wd) return ECORE_CALLBACK_PASS_ON;
160    ev = event;
161    if (ev->atom == ECORE_X_ATOM_E_ILLUME_ZONE) 
162      {
163         Ecore_X_Window zone;
164         int sh = -1;
165
166         zone = ecore_x_e_illume_zone_get(ev->win);
167         ecore_x_e_illume_indicator_geometry_get(zone, NULL, NULL, NULL, &sh);
168         if (sh < 0) sh = 0;
169         evas_object_size_hint_min_set(wd->shelf, -1, sh);
170         evas_object_size_hint_max_set(wd->shelf, -1, sh);
171         sh = -1;
172         ecore_x_e_illume_softkey_geometry_get(zone, NULL, NULL, NULL, &sh);
173         if (sh < 0) sh = 0;
174         evas_object_size_hint_min_set(wd->panel, -1, sh);
175         evas_object_size_hint_max_set(wd->panel, -1, sh);
176      }
177    else if (ev->atom == ECORE_X_ATOM_E_ILLUME_INDICATOR_GEOMETRY) 
178      {
179         Ecore_X_Window zone;
180         int sh = -1;
181
182         zone = ecore_x_e_illume_zone_get(ev->win);
183         ecore_x_e_illume_indicator_geometry_get(zone, NULL, NULL, NULL, &sh);
184         if (sh < 0) sh = 0;
185         evas_object_size_hint_min_set(wd->shelf, -1, sh);
186         evas_object_size_hint_max_set(wd->shelf, -1, sh);
187      }
188    else if (ev->atom == ECORE_X_ATOM_E_ILLUME_SOFTKEY_GEOMETRY) 
189      {
190         Ecore_X_Window zone;
191         int sh = -1;
192
193         zone = ecore_x_e_illume_zone_get(ev->win);
194         ecore_x_e_illume_softkey_geometry_get(zone, NULL, NULL, NULL, &sh);
195         if (sh < 0) sh = 0;
196         evas_object_size_hint_min_set(wd->panel, -1, sh);
197         evas_object_size_hint_max_set(wd->panel, -1, sh);
198      }
199    else if (ev->atom == ECORE_X_ATOM_E_ILLUME_KEYBOARD_GEOMETRY) 
200      {
201         Ecore_X_Window zone;
202         int ky = -1;
203
204         printf("Keyboard Geometry Changed\n");
205         zone = ecore_x_e_illume_zone_get(ev->win);
206         ecore_x_e_illume_keyboard_geometry_get(zone, NULL, &ky, NULL, NULL);
207         printf("\tGeom: %d\n", ky);
208      }
209 #endif
210
211    return ECORE_CALLBACK_PASS_ON;
212 }
213
214 /**
215  * Add a new Conformant object
216  * 
217  * @param parent The parent object
218  * @return The new conformant object or NULL if it cannot be created
219  * 
220  * @ingroup Conformant
221  */
222 EAPI Evas_Object *
223 elm_conformant_add(Evas_Object *parent) 
224 {
225    Evas_Object *obj;
226    Evas *e;
227    Widget_Data *wd;
228
229    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
230    
231    ELM_SET_WIDTYPE(widtype, "conformant");
232    elm_widget_type_set(obj, "conformant");
233    elm_widget_sub_object_add(parent, obj);
234    elm_widget_data_set(obj, wd);
235    elm_widget_del_hook_set(obj, _del_hook);
236    elm_widget_theme_hook_set(obj, _theme_hook);
237    elm_widget_can_focus_set(obj, EINA_FALSE);
238
239    wd->base = edje_object_add(e);
240    _elm_theme_object_set(obj, wd->base, "conformant", "base", "default");
241    elm_widget_resize_object_set(obj, wd->base);
242
243 #ifdef HAVE_ELEMENTARY_X
244    Ecore_X_Window zone, xwin;
245    int sh = -1;
246
247    xwin = elm_win_xwindow_get(parent);
248    zone = ecore_x_e_illume_zone_get(xwin);
249
250    ecore_x_e_illume_indicator_geometry_get(zone, NULL, NULL, NULL, &sh);
251    if (sh < 0) sh = 0;
252    wd->shelf = evas_object_rectangle_add(e);
253    evas_object_color_set(wd->shelf, 0, 0, 0, 0);
254    evas_object_size_hint_min_set(wd->shelf, -1, sh);
255    evas_object_size_hint_max_set(wd->shelf, -1, sh);
256    edje_object_part_swallow(wd->base, "elm.swallow.shelf", wd->shelf);
257
258    sh = -1;
259    ecore_x_e_illume_softkey_geometry_get(zone, NULL, NULL, NULL, &sh);
260    if (sh < 0) sh = 0;
261    wd->panel = evas_object_rectangle_add(e);
262    evas_object_color_set(wd->panel, 0, 0, 0, 0);
263    evas_object_size_hint_min_set(wd->panel, -1, sh);
264    evas_object_size_hint_max_set(wd->panel, -1, sh);
265    edje_object_part_swallow(wd->base, "elm.swallow.panel", wd->panel);
266
267    wd->prop_hdl = ecore_event_handler_add(ECORE_X_EVENT_WINDOW_PROPERTY, 
268                                           _prop_change, obj);
269    // FIXME: get kbd region prop
270 #endif
271
272    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
273
274    _mirrored_set(obj, elm_widget_mirrored_get(obj));
275    _sizing_eval(obj);
276    return obj;
277 }
278
279 /**
280  * Set the content of the conformant widget
281  *
282  * Once the content object is set, a previously set one will be deleted.
283  * If you want to keep that old content object, use the
284  * elm_conformat_content_unset() function.
285  *
286  * @param obj The conformant object
287  * @return The content that was being used
288  *
289  * @ingroup Conformant
290  */
291 EAPI void
292 elm_conformant_content_set(Evas_Object *obj, Evas_Object *content)
293 {
294    ELM_CHECK_WIDTYPE(obj, widtype);
295    Widget_Data *wd = elm_widget_data_get(obj);
296    if (!wd) return;
297    if (wd->content == content) return;
298    if (wd->content) evas_object_del(wd->content);
299    wd->content = content;
300    if (content)
301      {
302         elm_widget_sub_object_add(obj, content);
303         evas_object_event_callback_add(content, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
304                                        _changed_size_hints, obj);
305         edje_object_part_swallow(wd->base, "elm.swallow.content", content);
306      }
307    _sizing_eval(obj);
308 }
309
310 /**
311  * Get the content of the conformant widget
312  *
313  * Return the content object which is set for this widget;
314  *
315  * @param obj The conformant object
316  * @return The content that is being used
317  *
318  * @ingroup Conformant
319  */
320 EAPI Evas_Object *
321 elm_conformant_content_get(const Evas_Object *obj)
322 {
323    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
324    Widget_Data *wd = elm_widget_data_get(obj);
325    if (!wd) return NULL;
326    return wd->content;
327 }
328
329 /**
330  * Unset the content of the conformant widget
331  *
332  * Unparent and return the content object which was set for this widget;
333  *
334  * @param obj The conformant object
335  * @return The content that was being used
336  *
337  * @ingroup Conformant
338  */
339 EAPI Evas_Object *
340 elm_conformant_content_unset(Evas_Object *obj)
341 {
342    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
343    Widget_Data *wd = elm_widget_data_get(obj);
344    Evas_Object *content;
345    if (!wd) return NULL;
346    if (!wd->content) return NULL;
347    content = wd->content;
348    elm_widget_sub_object_del(obj, wd->content);
349    edje_object_part_unswallow(wd->base, wd->content);
350    wd->content = NULL;
351    return content;
352 }