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