Elementary: Added ui-mirroring support for all the widgets.
[framework/uifw/elementary.git] / src / lib / elm_frame.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 /**
5  * @defgroup Frame Frame
6  *
7  * This holds some content and has a title. Looks like a frame, but
8  * supports styles so multple frames are avaible
9  */
10
11 typedef struct _Widget_Data Widget_Data;
12
13 struct _Widget_Data
14 {
15    Evas_Object *frm;
16    Evas_Object *content;
17    const char *label;
18 };
19
20 static const char *widtype = NULL;
21 static void _del_hook(Evas_Object *obj);
22 static void _mirrored_set(Evas_Object *obj, Eina_Bool rtl);
23 static void _theme_hook(Evas_Object *obj);
24 static void _sizing_eval(Evas_Object *obj);
25 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
26 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
27
28 static void
29 _del_hook(Evas_Object *obj)
30 {
31    Widget_Data *wd = elm_widget_data_get(obj);
32    if (!wd) return;
33    if (wd->label) eina_stringshare_del(wd->label);
34    free(wd);
35 }
36
37 static void
38 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
39 {
40    Widget_Data *wd = elm_widget_data_get(obj);
41    if (!wd) return;
42    edje_object_mirrored_set(wd->frm, rtl);
43 }
44
45 static void
46 _theme_hook(Evas_Object *obj)
47 {
48    Widget_Data *wd = elm_widget_data_get(obj);
49    if (!wd) return;
50    _mirrored_set(obj, elm_widget_mirrored_get(obj));
51    _elm_theme_object_set(obj, wd->frm, "frame", "base", elm_widget_style_get(obj));
52    edje_object_part_text_set(wd->frm, "elm.text", wd->label);
53    if (wd->content)
54      edje_object_part_swallow(wd->frm, "elm.swallow.content", wd->content);
55    edje_object_scale_set(wd->frm, elm_widget_scale_get(obj) * _elm_config->scale);
56    _sizing_eval(obj);
57 }
58
59 static Eina_Bool
60 _elm_frame_focus_next_hook(const Evas_Object *obj, Elm_Focus_Direction dir, Evas_Object **next)
61 {
62    Widget_Data *wd = elm_widget_data_get(obj);
63    Evas_Object *cur;
64
65    if ((!wd) || (!wd->content))
66      return EINA_FALSE;
67
68    cur = wd->content;
69
70    /* Try Focus cycle in subitem */
71    return elm_widget_focus_next_get(cur, dir, next);
72 }
73
74 static void
75 _sizing_eval(Evas_Object *obj)
76 {
77    Widget_Data *wd = elm_widget_data_get(obj);
78    Evas_Coord minw = -1, minh = -1;
79    if (!wd) return;
80    edje_object_size_min_calc(wd->frm, &minw, &minh);
81    evas_object_size_hint_min_set(obj, minw, minh);
82    evas_object_size_hint_max_set(obj, -1, -1);
83 }
84
85 static void
86 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
87 {
88    Widget_Data *wd = elm_widget_data_get(data);
89    // FIXME: why is this needed? how does edje get this unswallowed or
90    // lose its callbacks to edje
91    if (!wd) return;
92    edje_object_part_swallow(wd->frm, "elm.swallow.content", wd->content);
93    _sizing_eval(data);
94 }
95
96 static void
97 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
98 {
99    Widget_Data *wd = elm_widget_data_get(obj);
100    Evas_Object *sub = event_info;
101    if (!wd) return;
102    if (sub == wd->content)
103      {
104         evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
105                                             _changed_size_hints, obj);
106         wd->content = NULL;
107         _sizing_eval(obj);
108      }
109 }
110
111 /**
112  * Add a new frame to the parent
113  *
114  * @param parent The parent object
115  * @return The new object or NULL if it cannot be created
116  *
117  * @ingroup Frame
118  */
119 EAPI Evas_Object *
120 elm_frame_add(Evas_Object *parent)
121 {
122    Evas_Object *obj;
123    Evas *e;
124    Widget_Data *wd;
125
126    EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
127
128    wd = ELM_NEW(Widget_Data);
129    e = evas_object_evas_get(parent);
130    if (!e) return NULL;
131    obj = elm_widget_add(e);
132    ELM_SET_WIDTYPE(widtype, "frame");
133    elm_widget_type_set(obj, "frame");
134    elm_widget_sub_object_add(parent, obj);
135    elm_widget_data_set(obj, wd);
136    elm_widget_del_hook_set(obj, _del_hook);
137    elm_widget_theme_hook_set(obj, _theme_hook);
138    elm_widget_focus_next_hook_set(obj, _elm_frame_focus_next_hook);
139    elm_widget_can_focus_set(obj, EINA_FALSE);
140
141    wd->frm = edje_object_add(e);
142    _elm_theme_object_set(obj, wd->frm, "frame", "base", "default");
143    elm_widget_resize_object_set(obj, wd->frm);
144
145    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
146
147    _mirrored_set(obj, elm_widget_mirrored_get(obj));
148    _sizing_eval(obj);
149    return obj;
150 }
151
152 /**
153  * Set the frame label
154  *
155  * @param obj The frame object
156  * @param label The label of this frame object
157  *
158  * @ingroup Frame
159  */
160 EAPI void
161 elm_frame_label_set(Evas_Object *obj, const char *label)
162 {
163    ELM_CHECK_WIDTYPE(obj, widtype);
164    Widget_Data *wd = elm_widget_data_get(obj);
165    if (!wd) return;
166    eina_stringshare_replace(&(wd->label), label);
167    edje_object_part_text_set(wd->frm, "elm.text", wd->label);
168    _sizing_eval(obj);
169 }
170
171 /**
172  * Get the frame label
173  *
174  * @param obj The frame object
175  *
176  * @return The label of this frame objet or NULL if unable to get frame
177  *
178  * @ingroup Frame
179  */
180 EAPI const char *
181 elm_frame_label_get(const Evas_Object *obj)
182 {
183    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
184    Widget_Data *wd = elm_widget_data_get(obj);
185    if (!wd) return NULL;
186    return wd->label;
187 }
188
189 /**
190  * Set the content of the frame widget
191  *
192  * Once the content object is set, a previously set one will be deleted.
193  * If you want to keep that old content object, use the
194  * elm_frame_content_unset() function.
195  *
196  * @param obj The frame object
197  * @param content The content will be filled in this frame object
198  *
199  * @ingroup Frame
200  */
201 EAPI void
202 elm_frame_content_set(Evas_Object *obj, Evas_Object *content)
203 {
204    ELM_CHECK_WIDTYPE(obj, widtype);
205    Widget_Data *wd = elm_widget_data_get(obj);
206    if (!wd) return;
207    if (wd->content == content) return;
208    if (wd->content) evas_object_del(wd->content);
209    wd->content = content;
210    if (content)
211      {
212         elm_widget_sub_object_add(obj, content);
213         evas_object_event_callback_add(content, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
214                                        _changed_size_hints, obj);
215         edje_object_part_swallow(wd->frm, "elm.swallow.content", content);
216      }
217    _sizing_eval(obj);
218 }
219
220 /**
221  * Get the content of the frame widget
222  *
223  * Return the content object which is set for this widget
224  *
225  * @param obj The frame object
226  * @return The content that is being used
227  *
228  * @ingroup Frame
229  */
230 EAPI Evas_Object *
231 elm_frame_content_get(const Evas_Object *obj)
232 {
233    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
234    Widget_Data *wd = elm_widget_data_get(obj);
235    if (!wd) return NULL;
236    return wd->content;
237 }
238
239 /**
240  * Unset the content of the frame widget
241  *
242  * Unparent and return the content object which was set for this widget
243  *
244  * @param obj The frame object
245  * @return The content that was being used
246  *
247  * @ingroup Frame
248  */
249 EAPI Evas_Object *
250 elm_frame_content_unset(Evas_Object *obj)
251 {
252    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
253    Widget_Data *wd = elm_widget_data_get(obj);
254    Evas_Object *content;
255    if (!wd) return NULL;
256    if (!wd->content) return NULL;
257    content = wd->content;
258    elm_widget_sub_object_del(obj, wd->content);
259    edje_object_part_unswallow(wd->frm, wd->content);
260    wd->content = NULL;
261    return content;
262 }