fixed plugin image size problem
[framework/uifw/elementary.git] / src / lib / elm_plug.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 typedef struct _Widget_Data Widget_Data;
5
6 struct _Widget_Data
7 {
8    Evas_Object *img;
9 };
10
11 static const char *widtype = NULL;
12 static void _del_hook(Evas_Object *obj);
13 static void _theme_hook(Evas_Object *obj);
14 static void _sizing_eval(Evas_Object *obj);
15 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
16 static void _mouse_up(void *data, Evas *e, Evas_Object *obj, void *event_info);
17
18 static const char SIG_CLICKED[] = "clicked";
19
20 static const Evas_Smart_Cb_Description _signals[] = {
21        {SIG_CLICKED, ""},
22        {NULL, NULL}
23 };
24
25
26 static void
27 _del_hook(Evas_Object *obj)
28 {
29    Widget_Data *wd = elm_widget_data_get(obj);
30
31    if (!wd) return;
32    free(wd);
33 }
34
35 static void
36 _del_pre_hook(Evas_Object *obj)
37 {
38    Widget_Data *wd = elm_widget_data_get(obj);
39
40    if (!wd) return;
41    if (!wd->img) return;
42    evas_object_event_callback_del_full(wd->img, EVAS_CALLBACK_CHANGED_SIZE_HINTS, _changed_size_hints, obj);
43    evas_object_del(wd->img);
44 }
45
46 static void
47 _theme_hook(Evas_Object *obj)
48 {
49    Widget_Data *wd = elm_widget_data_get(obj);
50
51    if (!wd) return;
52    _sizing_eval(obj);
53 }
54
55 static void
56 _sizing_eval(Evas_Object *obj)
57 {
58    Widget_Data *wd = elm_widget_data_get(obj);
59    Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
60
61    if (!wd) return;
62    if (!wd->img) return;
63    //TODO: get socket object size
64
65    evas_object_size_hint_min_get(wd->img, &minw, &minh);
66    evas_object_size_hint_max_get(wd->img, &maxw, &maxh);
67    evas_object_size_hint_min_set(obj, minw, minh);
68    evas_object_size_hint_max_set(obj, maxw, maxh);
69 }
70
71 static void
72 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
73 {
74    Widget_Data *wd = elm_widget_data_get(data);
75    if (!wd) return;
76    if (obj != wd->img) return;
77    _sizing_eval(data);
78 }
79
80 static void
81 _mouse_up(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
82 {
83    evas_object_smart_callback_call(data, SIG_CLICKED, NULL);
84 }
85
86
87 EAPI Evas_Object *
88 elm_plug_image_object_get(const Evas_Object *obj)
89 {
90    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
91    Widget_Data *wd = elm_widget_data_get(obj);
92    if (!wd) return NULL;
93    if (!wd->img) return NULL;
94    return wd->img;
95 }
96
97 EAPI Evas_Object *
98 elm_plug_add(Evas_Object *parent)
99 {
100    Evas_Object *obj;
101    Evas *e;
102    Widget_Data *wd;
103    Ecore_Evas *ee;
104
105    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
106
107    ELM_SET_WIDTYPE(widtype, "plug");
108    elm_widget_type_set(obj, "plug");
109    elm_widget_sub_object_add(parent, obj);
110    elm_widget_data_set(obj, wd);
111    elm_widget_del_hook_set(obj, _del_hook);
112    elm_widget_del_pre_hook_set(obj, _del_pre_hook);
113    elm_widget_theme_hook_set(obj, _theme_hook);
114    elm_widget_can_focus_set(obj, EINA_FALSE);
115
116    ee = ecore_evas_ecore_evas_get(evas_object_evas_get(obj));
117    if (!ee) return NULL;
118    wd->img = ecore_evas_extn_plug_new(ee);
119    if (!wd->img) return NULL;
120    evas_object_event_callback_add(wd->img, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
121                                   _changed_size_hints, obj);
122    evas_object_event_callback_add(wd->img, EVAS_CALLBACK_MOUSE_UP,
123                                   _mouse_up, obj);
124    elm_widget_resize_object_set(obj, wd->img);
125
126    evas_object_smart_callbacks_descriptions_set(obj, _signals);
127
128    _sizing_eval(obj);
129    return obj;
130 }
131
132 EAPI Eina_Bool
133 elm_plug_connect(Evas_Object *obj, const char *svcname, int svcnum, Eina_Bool svcsys)
134 {
135    Evas_Object *plug_img = NULL;
136
137    plug_img = elm_plug_image_object_get(obj);
138    if (!plug_img) return EINA_FALSE;
139
140    if (ecore_evas_extn_plug_connect(plug_img, svcname, svcnum, svcsys))
141      return EINA_TRUE;
142    else
143      return EINA_FALSE;
144 }
145