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