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