[SLP Merge] Thu Jul 7 13:20:56 2011 +0900
[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 static void
114 _elm_frame_label_set(Evas_Object *obj, const char *item, const char *label)
115 {
116    ELM_CHECK_WIDTYPE(obj, widtype);
117    Widget_Data *wd = elm_widget_data_get(obj);
118    if (item && strcmp(item, "default")) return;
119    if (!wd) return;
120    eina_stringshare_replace(&(wd->label), label);
121    edje_object_part_text_set(wd->frm, "elm.text", wd->label);
122    _sizing_eval(obj);
123 }
124
125 static const char *
126 _elm_frame_label_get(const Evas_Object *obj, const char *item)
127 {
128    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
129    Widget_Data *wd = elm_widget_data_get(obj);
130    if (item && strcmp(item, "default")) return NULL;
131    if (!wd) return NULL;
132    return wd->label;
133 }
134
135 /**
136  * Add a new frame to the parent
137  *
138  * @param parent The parent object
139  * @return The new object or NULL if it cannot be created
140  *
141  * @ingroup Frame
142  */
143 EAPI Evas_Object *
144 elm_frame_add(Evas_Object *parent)
145 {
146    Evas_Object *obj;
147    Evas *e;
148    Widget_Data *wd;
149
150    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
151
152    ELM_SET_WIDTYPE(widtype, "frame");
153    elm_widget_type_set(obj, "frame");
154    elm_widget_sub_object_add(parent, obj);
155    elm_widget_data_set(obj, wd);
156    elm_widget_del_hook_set(obj, _del_hook);
157    elm_widget_theme_hook_set(obj, _theme_hook);
158    elm_widget_focus_next_hook_set(obj, _elm_frame_focus_next_hook);
159    elm_widget_can_focus_set(obj, EINA_FALSE);
160    elm_widget_text_set_hook_set(obj, _elm_frame_label_set);
161    elm_widget_text_get_hook_set(obj, _elm_frame_label_get);
162
163    wd->frm = edje_object_add(e);
164    _elm_theme_object_set(obj, wd->frm, "frame", "base", "default");
165    elm_widget_resize_object_set(obj, wd->frm);
166
167    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
168
169    _mirrored_set(obj, elm_widget_mirrored_get(obj));
170    _sizing_eval(obj);
171    return obj;
172 }
173
174 /**
175  * Set the frame label
176  *
177  * @param obj The frame object
178  * @param label The label of this frame object
179  *
180  * @ingroup Frame
181  * @deprecate use elm_object_text_* instead.
182  */
183 EAPI void
184 elm_frame_label_set(Evas_Object *obj, const char *label)
185 {
186    _elm_frame_label_set(obj, NULL, label);
187 }
188
189 /**
190  * Get the frame label
191  *
192  * @param obj The frame object
193  *
194  * @return The label of this frame objet or NULL if unable to get frame
195  *
196  * @ingroup Frame
197  * @deprecate use elm_object_text_* instead.
198  */
199 EAPI const char *
200 elm_frame_label_get(const Evas_Object *obj)
201 {
202    return _elm_frame_label_get(obj, NULL);
203 }
204
205 /**
206  * Set the content of the frame widget
207  *
208  * Once the content object is set, a previously set one will be deleted.
209  * If you want to keep that old content object, use the
210  * elm_frame_content_unset() function.
211  *
212  * @param obj The frame object
213  * @param content The content will be filled in this frame object
214  *
215  * @ingroup Frame
216  */
217 EAPI void
218 elm_frame_content_set(Evas_Object *obj, Evas_Object *content)
219 {
220    ELM_CHECK_WIDTYPE(obj, widtype);
221    Widget_Data *wd = elm_widget_data_get(obj);
222    if (!wd) return;
223    if (wd->content == content) return;
224    if (wd->content) evas_object_del(wd->content);
225    wd->content = content;
226    if (content)
227      {
228         elm_widget_sub_object_add(obj, content);
229         evas_object_event_callback_add(content, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
230                                        _changed_size_hints, obj);
231         edje_object_part_swallow(wd->frm, "elm.swallow.content", content);
232      }
233    _sizing_eval(obj);
234 }
235
236 /**
237  * Get the content of the frame widget
238  *
239  * Return the content object which is set for this widget
240  *
241  * @param obj The frame object
242  * @return The content that is being used
243  *
244  * @ingroup Frame
245  */
246 EAPI Evas_Object *
247 elm_frame_content_get(const Evas_Object *obj)
248 {
249    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
250    Widget_Data *wd = elm_widget_data_get(obj);
251    if (!wd) return NULL;
252    return wd->content;
253 }
254
255 /**
256  * Unset the content of the frame widget
257  *
258  * Unparent and return the content object which was set for this widget
259  *
260  * @param obj The frame object
261  * @return The content that was being used
262  *
263  * @ingroup Frame
264  */
265 EAPI Evas_Object *
266 elm_frame_content_unset(Evas_Object *obj)
267 {
268    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
269    Widget_Data *wd = elm_widget_data_get(obj);
270    Evas_Object *content;
271    if (!wd) return NULL;
272    if (!wd->content) return NULL;
273    content = wd->content;
274    elm_widget_sub_object_del(obj, wd->content);
275    edje_object_part_unswallow(wd->frm, wd->content);
276    wd->content = NULL;
277    return content;
278 }