summaryrefslogtreecommitdiff
path: root/app-assist-efl/src/WControl.cpp
blob: ec786f8d55c446ca65c6bd5f164c8fbb0cfbffb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
 * Copyright (c) 2009-2015 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include "WControl.h"
#include "WView.h"
#include "WDebugInternal.h"
#include "WDefineInternal.h"

#include <Elementary.h>
#include <memory.h>

#define __CLASS_NAME	"WControl"


class __WControlImpl
{
public:
	__WControlImpl();
	~__WControlImpl();
public:
	//
	Evas_Object* __obj;
	char* __name;

	std::function<Evas_Object* (Evas_Object*, void*)> __createHandler;
	std::function<void ()> __destroyedHandler;

	std::shared_ptr<IWUiObject>* __selfPtr;
	std::weak_ptr<IWUiObject>* __popupMonitor;
	//
	static void __objDelCb( void* data, Evas* evas, Evas_Object* obj, void* event_info);
	void __attachPopup( WControl *popup );

};
__WControlImpl::__WControlImpl()
{
	__obj = NULL;
	__name = NULL;
	__createHandler = NULL;
	__destroyedHandler = NULL;
	__selfPtr = NULL;
	__popupMonitor = NULL;
}

__WControlImpl::~__WControlImpl()
{
	if(__name)
		free(__name);

	if( __popupMonitor )
	{
		if( auto p = __popupMonitor->lock() )
		{
			p->destroy();
		}
	}
	delete __popupMonitor;
	delete __selfPtr;
}

void __WControlImpl::__objDelCb( void* data, Evas* evas, Evas_Object* obj, void* event_info)
{
	auto ctrl = (WControl*)data;

	ctrl->onDestroy();

	if( ctrl->__pv->__destroyedHandler )
	{
		ctrl->__pv->__destroyedHandler();
	}

	delete ctrl;
}

void __WControlImpl::__attachPopup( WControl *popup )
{
	Evas_Object* parent = NULL;
	Evas_Object* obj = __obj;
	do
	{
		WView* view = WView_getInstanceFromEvasObj( obj );
		if( view != NULL )
		{
			parent = view->getWindow()->getBaseLayoutEvasObj();
			break;
		}
	}
	while( (obj = elm_object_parent_widget_get( obj )) != NULL );

	if( parent == NULL )
	{
		WERROR("Cannot find parent View!");
		// If the pop-up has been already created, the following statement will just return without creating pop-up.
		popup->create( elm_object_top_widget_get( __obj ), NULL);
	}
	else
	{
		// If the pop-up has been already created, the following statement will just return without creating pop-up.
		popup->create( parent, NULL );
	}
}

WControl::WControl()
{
	__pv = new __WControlImpl();
}

WControl::~WControl()
{
	if( __pv->__name)
	{
		WDEBUG( "name=%s", __pv->__name );
	}
	else
	{
		WHIT();
	}
	//
	delete __pv;
}
const char* WControl::getClassName()
{
	return __CLASS_NAME;
}

bool WControl::create( Evas_Object* parent, void* param )
{
	if( __pv->__name)
		WDEBUG( "name=%s", __pv->__name );
	else
		WHIT();

	if( __pv->__obj )
	{
		WDEBUG("Already created!");
		return true;
	}

	if( __pv->__createHandler )
		__pv->__obj = __pv->__createHandler( parent, param );
	else
		__pv->__obj = onCreate( parent, param );

	if( __pv->__obj == NULL)
		return false;

	if( __pv->__name )  // for easy debugging
	{
		evas_object_name_set( __pv->__obj, __pv->__name );
	}

	evas_object_data_set( __pv->__obj, WKEY_CONTROL_INSTANCE, this );
	evas_object_event_callback_add( __pv->__obj, EVAS_CALLBACK_DEL, __WControlImpl::__objDelCb, this);
	//
	// Do not place this before "evas_object_event_callback_add",
	// The reason is that if additional del callback is added at child class, it should be called first to keep state the class object is still alive.
	onCreated();
	return true;
}

void WControl::destroy()
{
	if(__pv->__obj)
	{
		evas_object_del(__pv->__obj);
		// Do not leave any code here.
		// After executing upper statement "evas_object_del", this object will be deleted at evas object deletion callback!
	}
	else
	{
		onDestroy();

		if( __pv->__destroyedHandler )
		{
			__pv->__destroyedHandler();
		}

		delete this;
	}
}

Evas_Object* WControl::getEvasObj()
{
	return __pv->__obj;
}

const Evas_Object* WControl::getConstEvasObj() const
{
	return __pv->__obj;
}

void WControl::setName(const char* name)
{
	if( __pv->__name)
	{
		free( __pv->__name);
		__pv->__name = NULL;
	}
	if( name )
	{
		__pv->__name = (char*)malloc( strlen(name)+1);
		strcpy( __pv->__name, name);
	}
}

const char* WControl::getName()
{
	return __pv->__name;
}

std::weak_ptr<IWUiObject> WControl::getWeakPtr()
{
	if( __pv->__selfPtr )
	{
		return std::weak_ptr<IWUiObject>(*__pv->__selfPtr);
	}
	__pv->__selfPtr = new std::shared_ptr<IWUiObject>( this,[](IWUiObject* p){} );
	return std::weak_ptr<IWUiObject>(*__pv->__selfPtr);
}

void WControl::attachPopup( WControl* popup )
{
	__pv->__attachPopup(popup);

	// destroy old popup after new popup created
	destroyPopup();

	if( __pv->__popupMonitor == NULL)
		__pv->__popupMonitor = new std::weak_ptr<IWUiObject>;
	*__pv->__popupMonitor = popup->getWeakPtr();
}

void WControl::attachHiddenPopup( WControl *popup )
{
	__pv->__attachPopup(popup);
	evas_object_hide(popup->getEvasObj());
}

void WControl::destroyPopup()
{
	if( __pv->__popupMonitor == NULL ) return;

	if( auto p = __pv->__popupMonitor->lock() )
	{
		p->destroy();
		__pv->__popupMonitor->reset();
	}
}

void WControl::setOnCreate( const std::function<Evas_Object* (Evas_Object*, void*)>& handlerFunc )
{
	__pv->__createHandler = handlerFunc;
}

void WControl::setOnDestroy( const std::function<void ()>& handlerFunc )
{
	__pv->__destroyedHandler = handlerFunc;
}

WControl* WControl_getInstanceFromEvasObj(Evas_Object* obj)
{
	return (WControl*)evas_object_data_get(obj, WKEY_CONTROL_INSTANCE);
}