Temporary fix for asking geolocation permission popup crash.
[platform/framework/web/wrt.git] / src / view / common / 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
21  * Efl.
22  */
23
24 #include "evas_object.h"
25 #include <dpl/foreach.h>
26
27 Evas_Object* EvasObject::IConnection::GetEvasObject()
28 {
29     return m_object->GetObject();
30 }
31
32 void EvasObject::IConnection::Disconnect()
33 {
34     m_object->DisconnectCallback(this);
35 }
36
37 EvasObject::IConnection::IConnection(EvasObject::EvasObjectShared* object) :
38     m_object(object)
39 {}
40
41 void EvasObject::IConnection::SmartCallbackWrapper(void* data,
42                                                    Evas_Object* /*object*/,
43                                                    void* event_info)
44 {
45     Assert(data);
46     IConnection* Calle = static_cast<IConnection*>(data);
47     Calle->Call(event_info);
48 }
49
50 void EvasObject::IConnection::EvasCallbackWrapper(void* data,
51                                                   Evas* /*evas*/,
52                                                   Evas_Object* /*object*/,
53                                                   void* event_info)
54 {
55     Assert(data);
56     IConnection* Calle = static_cast<IConnection*>(data);
57     Calle->Call(event_info);
58 }
59
60 Evas_Object* EvasObject::EvasObjectShared::GetObject()
61 {
62     return m_object;
63 }
64
65 EvasObject::EvasObjectShared::SmartConnectionBase::SmartConnectionBase(
66     const std::string& name,
67     EvasObject::EvasObjectShared* object) :
68     IConnection(object),
69     m_callbackName(name)
70 {}
71
72 void EvasObject::EvasObjectShared::SmartConnectionBase::ConnectPrv()
73 {
74     evas_object_smart_callback_add(GetEvasObject(),
75                                    m_callbackName.c_str(),
76                                    &IConnection::SmartCallbackWrapper, this);
77 }
78
79 void EvasObject::EvasObjectShared::SmartConnectionBase::DisconnectPrv()
80 {
81     evas_object_smart_callback_del(GetEvasObject(),
82                                    m_callbackName.c_str(),
83                                    &IConnection::SmartCallbackWrapper);
84 }
85
86 EvasObject::EvasObjectShared::EvasConnectionBase::EvasConnectionBase(
87     Evas_Callback_Type type,
88     EvasObject::EvasObjectShared* object) :
89     IConnection(object),
90     m_callbackType(type)
91 {}
92
93 void EvasObject::EvasObjectShared::EvasConnectionBase::ConnectPrv()
94 {
95     evas_object_event_callback_add(
96         GetEvasObject(), m_callbackType, &IConnection::EvasCallbackWrapper,
97         this);
98 }
99
100 void EvasObject::EvasObjectShared::EvasConnectionBase::DisconnectPrv()
101 {
102     evas_object_event_callback_del_full(
103         GetEvasObject(), m_callbackType, &IConnection::EvasCallbackWrapper,
104         this);
105 }
106
107 EvasObject::EvasObjectShared::EvasObjectShared() :
108     m_object(NULL)
109 {}
110
111 EvasObject::EvasObjectShared::EvasObjectShared(Evas_Object* object) :
112     m_object(object)
113 {
114     Assert(m_object);
115     evas_object_event_callback_add(m_object,
116                                    EVAS_CALLBACK_DEL,
117                                    &StaticOnDelEvent,
118                                    this);
119 }
120
121 void EvasObject::EvasObjectShared::SetObject(Evas_Object* object)
122 {
123     Assert(m_object == NULL);
124     Assert(object != NULL);
125     m_object = object;
126     evas_object_event_callback_add(m_object,
127                                    EVAS_CALLBACK_DEL,
128                                    &StaticOnDelEvent,
129                                    this);
130 }
131
132 EvasObject::EvasObjectShared::~EvasObjectShared()
133 {
134     if (m_object) {
135         DisconnectAll();
136         evas_object_event_callback_del(m_object,
137                                        EVAS_CALLBACK_DEL,
138                                        &StaticOnDelEvent);
139         m_object = NULL;
140     }
141 }
142
143 bool EvasObject::EvasObjectShared::DisconnectCallback(IConnection* connection)
144 {
145     IConnectionsSet::iterator it = m_connections.find(connection);
146     if (it != m_connections.end()) {
147         (*it)->DisconnectPrv();
148         delete connection;
149         m_connections.erase(it);
150         return true;
151     }
152     return false;
153 }
154
155 void EvasObject::EvasObjectShared::DisconnectAll()
156 {
157     FOREACH(it, m_connections)
158     {
159         (*it)->DisconnectPrv();
160         delete *it;
161     }
162     m_connections.clear();
163 }
164
165 void EvasObject::EvasObjectShared::StaticOnDelEvent(void* data,
166                                                     Evas* /*e*/,
167                                                     Evas_Object* /*o*/,
168                                                     void* /*ev*/)
169 {
170     Assert(data);
171     EvasObjectShared* This = static_cast<EvasObjectShared*>(data);
172     if (This->m_object) {
173         evas_object_event_callback_del(This->m_object,
174                                        EVAS_CALLBACK_DEL,
175                                        &StaticOnDelEvent);
176         This->DisconnectAll();
177         This->m_object = NULL;
178     }
179 }
180
181 EvasObject::EvasObject() :
182     m_object(new EvasObjectShared())
183 {}
184
185 EvasObject::EvasObject(Evas_Object* object) :
186     m_object(new EvasObjectShared(object))
187 {}
188
189 EvasObject::EvasObject(const EvasObject& other) :
190     m_object(other.m_object)
191 {}
192
193 //this destructor must be here to let pimpl with shared_ptr work without warning
194 EvasObject::~EvasObject()
195 {}
196
197 EvasObject& EvasObject::operator=(const EvasObject& other)
198 {
199     Assert(m_object);
200     m_object = other.m_object;
201     return *this;
202 }
203
204 EvasObject* EvasObject::operator=(Evas_Object* object)
205 {
206     Assert(m_object);
207     m_object->SetObject(object);
208     return this;
209 }
210
211 bool EvasObject::DisconnectCallback(IConnection* connection)
212 {
213     Assert(m_object);
214     return m_object->DisconnectCallback(connection);
215 }
216
217 void EvasObject::DisconnectAll()
218 {
219     Assert(m_object);
220     m_object->DisconnectAll();
221 }
222
223 EvasObject::operator Evas_Object *()
224 {
225     Assert(m_object);
226     return m_object->GetObject();
227 }