sync
[platform/framework/web/wrt-plugins-common.git] / src / wrt-popup / wrt / popup-bin / renderer / evas_object.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /*
17  * @file        evas_object.cpp
18  * @author      Lukasz Wrzosek (l.wrzosel@samsung.com)
19  * @version     1.0
20  * @brief       This file is the implementation for Evas_Object wrapper from Efl.
21  */
22
23 #include "evas_object.h"
24 #include <stddef.h>
25 #include <dpl/foreach.h>
26
27
28 namespace Wrt {
29 namespace Popup {
30 namespace Renderer {
31
32 Evas_Object* EvasObject::IConnection::GetEvasObject()
33 {
34     return m_object->GetObject();
35 }
36
37 void EvasObject::IConnection::Disconnect()
38 {
39     m_object->DisconnectCallback(this);
40 }
41
42 EvasObject::IConnection::IConnection(EvasObject::EvasObjectShared* object) :
43     m_object(object)
44 {
45 }
46
47 void EvasObject::IConnection::SmartCallbackWrapper(void* data,
48         Evas_Object* /*object*/,
49         void* event_info)
50 {
51     Assert(data);
52     IConnection* Calle = static_cast<IConnection*>(data);
53     Calle->Call(event_info);
54 }
55
56 void EvasObject::IConnection::EvasCallbackWrapper(void* data,
57         Evas* /*evas*/,
58         Evas_Object* /*object*/,
59         void* event_info)
60 {
61     Assert(data);
62     IConnection* Calle = static_cast<IConnection*>(data);
63     Calle->Call(event_info);
64 }
65
66 Evas_Object* EvasObject::EvasObjectShared::GetObject()
67 {
68     return m_object;
69 }
70
71 EvasObject::EvasObjectShared::SmartConnectionBase::SmartConnectionBase(
72         const std::string& name,
73         EvasObject::EvasObjectShared* object) :
74     IConnection(object),
75     m_callbackName(name)
76 {
77 }
78
79 void EvasObject::EvasObjectShared::SmartConnectionBase::ConnectPrv()
80 {
81     evas_object_smart_callback_add(GetEvasObject(),
82                                    m_callbackName.c_str(),
83                                    &IConnection::SmartCallbackWrapper, this);
84 }
85
86 void EvasObject::EvasObjectShared::SmartConnectionBase::DisconnectPrv()
87 {
88     evas_object_smart_callback_del(GetEvasObject(),
89                                    m_callbackName.c_str(),
90                                    &IConnection::SmartCallbackWrapper);
91 }
92
93 EvasObject::EvasObjectShared::EvasConnectionBase::EvasConnectionBase(
94         Evas_Callback_Type type,
95         EvasObject::EvasObjectShared* object) :
96     IConnection(object),
97     m_callbackType(type)
98 {
99 }
100
101 void EvasObject::EvasObjectShared::EvasConnectionBase::ConnectPrv()
102 {
103     evas_object_event_callback_add(
104         GetEvasObject(), m_callbackType, &IConnection::EvasCallbackWrapper,
105         this);
106 }
107
108 void EvasObject::EvasObjectShared::EvasConnectionBase::DisconnectPrv()
109 {
110     evas_object_event_callback_del_full(
111         GetEvasObject(), m_callbackType, &IConnection::EvasCallbackWrapper,
112         this);
113 }
114
115 EvasObject::EvasObjectShared::EvasObjectShared() :
116     m_object(NULL)
117 {
118 }
119
120 EvasObject::EvasObjectShared::EvasObjectShared(Evas_Object* object) :
121     m_object(object)
122 {
123     Assert(m_object);
124     evas_object_event_callback_add(m_object,
125                                    EVAS_CALLBACK_DEL,
126                                    &StaticOnDelEvent,
127                                    this);
128 }
129
130 void EvasObject::EvasObjectShared::SetObject(Evas_Object* object)
131 {
132     Assert(m_object == NULL);
133     Assert(object != NULL);
134     m_object = object;
135     evas_object_event_callback_add(m_object,
136                                    EVAS_CALLBACK_DEL,
137                                    &StaticOnDelEvent,
138                                    this);
139 }
140
141 EvasObject::EvasObjectShared::~EvasObjectShared()
142 {
143     if (m_object) {
144         DisconnectAll();
145         evas_object_event_callback_del(m_object,
146                                        EVAS_CALLBACK_DEL,
147                                        &StaticOnDelEvent);
148         m_object = NULL;
149     }
150 }
151
152 bool EvasObject::EvasObjectShared::DisconnectCallback(IConnection* connection)
153 {
154     IConnectionsSet::iterator it = m_connections.find(connection);
155     if (it != m_connections.end()) {
156         (*it)->DisconnectPrv();
157         delete connection;
158         m_connections.erase(it);
159         return true;
160     }
161     return false;
162 }
163
164 void EvasObject::EvasObjectShared::DisconnectAll()
165 {
166     FOREACH(it, m_connections)
167     {
168         (*it)->DisconnectPrv();
169         delete *it;
170     }
171     m_connections.clear();
172 }
173
174 void EvasObject::EvasObjectShared::StaticOnDelEvent(void* data,
175         Evas* /*e*/,
176         Evas_Object* /*o*/,
177         void* /*ev*/)
178 {
179     Assert(data);
180     EvasObjectShared* This = static_cast<EvasObjectShared*>(data);
181     if (This->m_object) {
182         evas_object_event_callback_del(This->m_object,
183                                        EVAS_CALLBACK_DEL,
184                                        &StaticOnDelEvent);
185         This->DisconnectAll();
186         This->m_object = NULL;
187     }
188 }
189
190 EvasObject::EvasObject() :
191     m_object(new EvasObjectShared())
192 {
193 }
194
195 EvasObject::EvasObject(Evas_Object* object) :
196     m_object(new EvasObjectShared(object))
197 {
198 }
199
200 EvasObject::EvasObject(const EvasObject& other) :
201     m_object(other.m_object)
202 {
203 }
204
205 //this destructor must be here to let pimpl with shared_ptr work without warning
206 EvasObject::~EvasObject()
207 {
208 }
209
210 EvasObject& EvasObject::operator=(const EvasObject& other)
211 {
212     Assert(m_object);
213     m_object = other.m_object;
214     return *this;
215 }
216
217 EvasObject* EvasObject::operator=(Evas_Object* object)
218 {
219     Assert(m_object);
220     m_object->SetObject(object);
221     return this;
222 }
223
224 bool EvasObject::DisconnectCallback(IConnection* connection)
225 {
226     Assert(m_object);
227     return m_object->DisconnectCallback(connection);
228 }
229
230 void EvasObject::DisconnectAll()
231 {
232     Assert(m_object);
233     m_object->DisconnectAll();
234 }
235
236 EvasObject::operator Evas_Object *()
237 {
238     Assert(m_object);
239     return m_object->GetObject();
240 }
241
242 }
243 } // namespace DPL
244 } // namespace Popup