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