Added property map for loading text and changed default color and style
[platform/core/uifw/widget-viewer-dali.git] / internal / widget_view / widget_view_impl.cpp
1 /*
2  * Samsung API
3  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
4  *
5  * Licensed under the Flora License, Version 1.1 (the License);
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://floralicense.org/license/
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an AS IS BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 // CLASS HEADER
19 #include <internal/widget_view/widget_view_impl.h>
20
21 // INTERNAL INCLUDES
22
23 // EXTERNAL INCLUDES
24 #include <dali/public-api/common/stage.h>
25 #include <dali/public-api/events/touch-data.h>
26 #include <dali/public-api/events/wheel-event.h>
27 #include <dali/public-api/images/native-image.h>
28 #include <dali/public-api/object/type-registry.h>
29 #include <dali/public-api/object/type-registry-helper.h>
30 #include <dali-toolkit/public-api/visuals/visual-properties.h>
31 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
32 #include <dali/integration-api/debug.h>
33 #include <string.h>
34 #include <Ecore_Input.h>
35 #include <widget_service.h>
36 #include <widget_instance.h>
37 #include <tzplatform_config.h>
38 #include <wayland-extension/tizen-extension-client-protocol.h>
39 #include <unistd.h>
40
41 namespace Dali
42 {
43
44 namespace WidgetView
45 {
46
47 namespace Internal
48 {
49
50 namespace
51 {
52
53 #if defined(DEBUG_ENABLED)
54 Integration::Log::Filter* gWidgetViewLogging  = Integration::Log::Filter::New( Debug::Verbose, false, "LOG_WIDGET_VIEW" );
55 #endif
56
57 BaseHandle Create()
58 {
59   return Dali::BaseHandle();
60 }
61
62 // Setup properties, signals and actions using the type-registry.
63 DALI_TYPE_REGISTRATION_BEGIN( Dali::WidgetView::WidgetView, Toolkit::Control, Create );
64 DALI_PROPERTY_REGISTRATION( Dali::WidgetView, WidgetView, "widgetId", BOOLEAN, WIDGET_ID )
65 DALI_PROPERTY_REGISTRATION( Dali::WidgetView, WidgetView, "instanceId", BOOLEAN, INSTANCE_ID )
66 DALI_PROPERTY_REGISTRATION( Dali::WidgetView, WidgetView, "contentInfo", BOOLEAN, CONTENT_INFO )
67 DALI_PROPERTY_REGISTRATION( Dali::WidgetView, WidgetView, "title", BOOLEAN, TITLE )
68 DALI_PROPERTY_REGISTRATION( Dali::WidgetView, WidgetView, "updatePeriod", BOOLEAN, UPDATE_PERIOD )
69 DALI_PROPERTY_REGISTRATION( Dali::WidgetView, WidgetView, "preview", MAP, PREVIEW )
70 DALI_PROPERTY_REGISTRATION( Dali::WidgetView, WidgetView, "loadingText", MAP, LOADING_TEXT )
71 DALI_PROPERTY_REGISTRATION( Dali::WidgetView, WidgetView, "widgetStateFaulted", BOOLEAN, WIDGET_STATE_FAULTED )
72 DALI_PROPERTY_REGISTRATION( Dali::WidgetView, WidgetView, "permanentDelete", BOOLEAN, PERMANENT_DELETE )
73
74 // Signals
75 DALI_SIGNAL_REGISTRATION( Dali::WidgetView, WidgetView, "widgetAdded", SIGNAL_WIDGET_ADDED )
76 DALI_SIGNAL_REGISTRATION( Dali::WidgetView, WidgetView, "widgetDeleted", SIGNAL_WIDGET_DELETED )
77 DALI_SIGNAL_REGISTRATION( Dali::WidgetView, WidgetView, "widgetCreationAborted", SIGNAL_WIDGET_CREATION_ABORTED )
78 DALI_SIGNAL_REGISTRATION( Dali::WidgetView, WidgetView, "widgetContentUpdated", SIGNAL_WIDGET_CONTENT_UPDATED )
79 DALI_SIGNAL_REGISTRATION( Dali::WidgetView, WidgetView, "widgetUpdatePeriodChanged", SIGNAL_WIDGET_UPDATE_PERIOD_CHANGED )
80 DALI_SIGNAL_REGISTRATION( Dali::WidgetView, WidgetView, "widgetFaulted", SIGNAL_WIDGET_FAULTED )
81
82 // Actions
83 DALI_ACTION_REGISTRATION( Dali::WidgetView, WidgetView, "pauseWidget", ACTION_WIDGETVIEW_PAUSE_WIDGET );
84 DALI_ACTION_REGISTRATION( Dali::WidgetView, WidgetView, "resumeWidget", ACTION_WIDGETVIEW_RESUME_WIDGET );
85 DALI_ACTION_REGISTRATION( Dali::WidgetView, WidgetView, "cancelTouchEvent", ACTION_WIDGETVIEW_CANCEL_TOUCH_EVENT );
86 DALI_ACTION_REGISTRATION( Dali::WidgetView, WidgetView, "activateFaultedWidget", ACTION_WIDGETVIEW_ACTIVATE_FAULTED_WIDGET );
87
88 DALI_TYPE_REGISTRATION_END()
89
90 struct wl_buffer* preBuffer;
91
92 const char* const FONT_STYLE( "fontStyle" );
93 const char* const TEXT_POINT_SIZE( "textPointSize" );
94 const char* const TEXT_COLOR( "textColor" );
95
96 static void OnBufferUpdated( struct tizen_remote_surface *surface, uint32_t type, struct wl_buffer *buffer,
97                               int32_t img_file_fd, uint32_t img_file_size, uint32_t time, struct wl_array *keys,
98                               const char *appid, const char *instance_id, const int pid, void *data)
99 {
100   Dali::WidgetView::Internal::WidgetView* widgetView = static_cast< Dali::WidgetView::Internal::WidgetView* >( data );
101
102   if( widgetView )
103   {
104     if( !widgetView->IsWidgetImageView() )
105     {
106       tizen_remote_surface_transfer_visibility( surface, TIZEN_REMOTE_SURFACE_VISIBILITY_TYPE_VISIBLE);
107
108       widgetView->CreateWidgetImageView();
109       widgetView->ConnectSignal( surface );
110     }
111
112     //get tbm surface from buffer
113     tbm_surface_h tbmSurface = static_cast< tbm_surface_h >( wl_buffer_get_user_data( buffer ) );
114     widgetView->UpdateImageSource( tbmSurface );
115   }
116
117   if( preBuffer != NULL && tizen_remote_surface_get_version( surface ) >= TIZEN_REMOTE_SURFACE_RELEASE_SINCE_VERSION )
118   {
119     tizen_remote_surface_release( surface, preBuffer );
120   }
121
122   preBuffer = buffer;
123
124   close( img_file_fd );
125 }
126
127 static void OnBufferAdded( const char *appid, const char *instance_id, const int pid, void *data )
128 {
129 }
130
131 static void OnSurfaceRemoved( const char *appid, const char *instance_id, const int pid, void *data )
132 {
133   Dali::WidgetView::Internal::WidgetView* widgetView = static_cast< Dali::WidgetView::Internal::WidgetView* >( data );
134
135   if( widgetView )
136   {
137     widgetView->CloseRemoteSurface();
138   }
139 }
140
141 } // unnamed namespace
142
143 Dali::WidgetView::WidgetView WidgetView::New( const std::string& widgetId, const std::string& contentInfo, int width, int height, float updatePeriod )
144 {
145   // Create the implementation, temporarily owned on stack
146   IntrusivePtr< WidgetView > internalWidgetView = new WidgetView( widgetId, contentInfo, width, height, updatePeriod );
147
148   // Pass ownership to CustomActor
149   Dali::WidgetView::WidgetView widgetView( *internalWidgetView );
150
151   // Second-phase init of the implementation
152   // This can only be done after the CustomActor connection has been made...
153   internalWidgetView->Initialize();
154
155   return widgetView;
156 }
157
158 WidgetView::WidgetView()
159 : Control( ControlBehaviour( REQUIRES_TOUCH_EVENTS ) ),
160   mWidgetId(),
161   mInstanceId(),
162   mContentInfo(),
163   mTitle(),
164   mWidth( 0 ),
165   mHeight( 0 ),
166   mPid( 0 ),
167   mUpdatePeriod( 0.0 ),
168   mPreviewVisible( true ),
169   mStateTextVisible( true ),
170   mPermanentDelete( false ),
171   mWatcherHandle( NULL ),
172   mRemoteSurface( NULL )
173 {
174 }
175
176 WidgetView::WidgetView( const std::string& widgetId, const std::string& contentInfo, int width, int height, float updatePeriod )
177 : Control( ControlBehaviour( REQUIRES_TOUCH_EVENTS ) ),
178   mWidgetId( widgetId ),
179   mInstanceId(),
180   mContentInfo( contentInfo ),
181   mTitle(),
182   mWidth( width ),
183   mHeight( height ),
184   mPid( 0 ),
185   mUpdatePeriod( updatePeriod ),
186   mPreviewVisible( true ),
187   mStateTextVisible( true ),
188   mPermanentDelete( false ),
189   mWatcherHandle( NULL ),
190   mRemoteSurface( NULL )
191 {
192 }
193
194 WidgetView::~WidgetView()
195 {
196   if( !mWidgetId.empty() && !mInstanceId.empty() )
197   {
198     widget_instance_terminate( mInstanceId.c_str() );
199
200     if( mPermanentDelete )
201     {
202       widget_instance_destroy( mInstanceId.c_str() );
203     }
204   }
205 }
206
207 bool WidgetView::PauseWidget()
208 {
209   int ret = widget_instance_pause( mInstanceId.c_str() );
210   if( ret < 0 )
211   {
212     DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::PauseWidget: Fail to pause widget(%s, %s) [%d]\n", mWidgetId.c_str(), mInstanceId.c_str(), ret );
213     return false;
214   }
215
216   DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::PauseWidget: Widget is paused (%s, %s)\n", mWidgetId.c_str(), mInstanceId.c_str() );
217
218   return true;
219 }
220
221 bool WidgetView::ResumeWidget()
222 {
223   int ret = widget_instance_resume( mInstanceId.c_str() );
224   if( ret < 0 )
225   {
226     DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::ResumeWidget: Fail to resume widget(%s, %s) [%d]\n", mWidgetId.c_str(), mInstanceId.c_str(), ret );
227     return false;
228   }
229
230   DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::ResumeWidget: Widget is resumed (%s, %s)\n", mWidgetId.c_str(), mInstanceId.c_str() );
231
232   return true;
233 }
234
235 const std::string& WidgetView::GetWidgetId() const
236 {
237   return mWidgetId;
238 }
239
240 const std::string& WidgetView::GetInstanceId() const
241 {
242   return mInstanceId;
243 }
244
245 const std::string& WidgetView::GetContentInfo()
246 {
247   widget_instance_h instance;
248   char* contentInfo = NULL;
249
250   mContentInfo.clear();
251
252   if( mWidgetId.empty() || mInstanceId.empty() )
253   {
254     DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::GetContentInfo: Widget id (%s) or instance id (%s) is invalid.\n", mWidgetId.c_str(), mInstanceId.c_str() );
255     return mContentInfo;
256   }
257
258   instance = widget_instance_get_instance( mWidgetId.c_str(), mInstanceId.c_str() );
259   if( !instance )
260   {
261     DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::GetContentInfo: widget_instance_get_instance is failed. [%s]\n", mInstanceId.c_str() );
262     return mContentInfo;
263   }
264
265   if( widget_instance_get_content( instance, &contentInfo ) < 0 )
266   {
267     DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::GetContentInfo: Failed to get content of widget. [%s]\n", mInstanceId.c_str() );
268     return mContentInfo;
269   }
270
271   mContentInfo = reinterpret_cast< char* >( contentInfo );
272
273   return mContentInfo;
274 }
275
276 const std::string& WidgetView::GetTitle()
277 {
278   //ToDo: We should add some codes by considering widget_viewer_evas
279   if( mTitle.empty() )
280   {
281     mTitle = widget_service_get_name( mWidgetId.c_str(), NULL );
282   }
283
284   DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::GetTitle: title = %s\n", mTitle.c_str() );
285
286   return mTitle;
287 }
288
289 float WidgetView::GetUpdatePeriod() const
290 {
291   return mUpdatePeriod;
292 }
293
294 bool WidgetView::CancelTouchEvent()
295 {
296   if( mRemoteSurface )
297   {
298     tizen_remote_surface_transfer_touch_cancel( mRemoteSurface );
299     return true;
300   }
301
302   return false;
303 }
304
305 void WidgetView::ShowPreview( bool show )
306 {
307   if( mPreviewImage && show != mPreviewVisible )
308   {
309     mPreviewVisible = show;
310     mPreviewImage.SetVisible( show );
311   }
312 }
313
314 bool WidgetView::IsPreviewVisible()
315 {
316   return mPreviewVisible;
317 }
318
319 void WidgetView::ShowStateText( bool show )
320 {
321   if( mStateText && mStateTextVisible != show )
322   {
323     mStateTextVisible = show;
324     mStateText.SetVisible( show );
325   }
326 }
327
328 bool WidgetView::IsStateTextVisible()
329 {
330   return mStateTextVisible;
331 }
332
333 void WidgetView::ActivateFaultedWidget()
334 {
335   if( mPid < 0 )
336   {
337     // Esable preview and text
338     if( mPreviewVisible )
339     {
340       mPreviewImage.SetVisible( true );
341     }
342
343     if( mStateTextVisible )
344     {
345       mStateText.SetVisible( true );
346     }
347
348     // launch widget again
349     mPid = widget_instance_launch( mInstanceId.c_str(), (char *)mContentInfo.c_str(), mWidth, mHeight );
350     if( mPid < 0)
351     {
352       DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::ActivateFaultedWidget: widget_instance_launch is failed. [%s]\n", mWidgetId.c_str() );
353
354       // Emit signal
355       Dali::WidgetView::WidgetView handle( GetOwner() );
356       mWidgetCreationAbortedSignal.Emit( handle );
357
358       return;
359     }
360
361     DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::ActivateFaultedWidget: widget_instance_launch is called. [%s, mPid = %d]\n", mWidgetId.c_str(), mPid );
362   }
363 }
364
365 bool WidgetView::IsWidgetFaulted()
366 {
367   return mPid < 0 ? true : false;
368 }
369
370 void WidgetView::SetPermanentDelete( bool permanentDelete )
371 {
372   mPermanentDelete = permanentDelete;
373 }
374
375 bool WidgetView::IsPermanentDelete()
376 {
377   return mPermanentDelete;
378 }
379
380 void WidgetView::CreateWidgetImageView()
381 {
382   Any source;
383   mImageSource = Dali::NativeImageSource::New( source );
384   Dali::NativeImage image = Dali::NativeImage::New( *mImageSource );
385
386   mWidgetImageView = Dali::Toolkit::ImageView::New( image );
387
388   mWidgetImageView.SetParentOrigin( ParentOrigin::CENTER );
389   mWidgetImageView.SetAnchorPoint( AnchorPoint::CENTER );
390   mWidgetImageView.SetSize( mWidth, mHeight );
391
392   Self().Add( mWidgetImageView );
393
394   //EFL app should be pre multiplied image.
395   mWidgetImageView.SetProperty( Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA, true );
396
397   // Disable preview and text
398   if( mPreviewVisible )
399   {
400     mPreviewImage.SetVisible( false );
401   }
402
403   if( mStateTextVisible )
404   {
405     mStateText.SetVisible( false );
406   }
407
408   // Emit signal
409   Dali::WidgetView::WidgetView handle( GetOwner() );
410   mWidgetAddedSignal.Emit( handle );
411
412   DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::AddObjectView: ObjectView is added.\n" );
413 }
414
415 void WidgetView::RemoveObjectView()
416 {
417   // Enable preview and text
418   if( mPreviewVisible )
419   {
420     mPreviewImage.SetVisible( true );
421   }
422
423   if( mStateTextVisible )
424   {
425     mStateText.SetVisible( true );
426   }
427
428   // Emit signal
429   Dali::WidgetView::WidgetView handle( GetOwner() );
430   mWidgetDeletedSignal.Emit( handle );
431
432   mWidgetImageView.Reset();
433
434   DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::RemoveObjectView: ObjectView is removed.\n" );
435 }
436
437 void WidgetView::SendWidgetEvent( int event )
438 {
439   Dali::WidgetView::WidgetView handle( GetOwner() );
440
441   DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::SendWidgetEvent: event = %d widget = %s\n", event,  mWidgetId.c_str() );
442
443   // Emit signal
444   switch( event )
445   {
446     case WIDGET_INSTANCE_EVENT_UPDATE:
447     case WIDGET_INSTANCE_EVENT_EXTRA_UPDATED:
448     {
449       mWidgetContentUpdatedSignal.Emit( handle );
450       break;
451     }
452     case WIDGET_INSTANCE_EVENT_PERIOD_CHANGED:
453     {
454       mWidgetUpdatePeriodChangedSignal.Emit( handle );
455       break;
456     }
457     case WIDGET_INSTANCE_EVENT_FAULT:
458     {
459       mWidgetFaultedSignal.Emit( handle );
460       break;
461     }
462     default:
463     {
464       break;
465     }
466   }
467 }
468
469 bool WidgetView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
470 {
471   Dali::BaseHandle handle( object );
472
473   bool connected( true );
474   Dali::WidgetView::WidgetView widgetView = Dali::WidgetView::WidgetView::DownCast( handle );
475
476   if( strcmp( signalName.c_str(), SIGNAL_WIDGET_ADDED ) == 0 )
477   {
478     widgetView.WidgetAddedSignal().Connect( tracker, functor );
479   }
480   else if( strcmp( signalName.c_str(), SIGNAL_WIDGET_DELETED ) == 0 )
481   {
482     widgetView.WidgetDeletedSignal().Connect( tracker, functor );
483   }
484   else if( strcmp( signalName.c_str(), SIGNAL_WIDGET_CREATION_ABORTED ) == 0 )
485   {
486     widgetView.WidgetCreationAbortedSignal().Connect( tracker, functor );
487   }
488   else if( strcmp( signalName.c_str(), SIGNAL_WIDGET_CONTENT_UPDATED ) == 0 )
489   {
490     widgetView.WidgetContentUpdatedSignal().Connect( tracker, functor );
491   }
492   else if( strcmp( signalName.c_str(), SIGNAL_WIDGET_UPDATE_PERIOD_CHANGED ) == 0 )
493   {
494     widgetView.WidgetUpdatePeriodChangedSignal().Connect( tracker, functor );
495   }
496   else if( strcmp( signalName.c_str(), SIGNAL_WIDGET_FAULTED ) == 0 )
497   {
498     widgetView.WidgetFaultedSignal().Connect( tracker, functor );
499   }
500   else
501   {
502     // signalName does not match any signal
503     connected = false;
504   }
505
506   return connected;
507 }
508
509 bool WidgetView::DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes )
510 {
511   bool ret = true;
512
513   Dali::BaseHandle handle( object );
514   Dali::WidgetView::WidgetView widgetView = Dali::WidgetView::WidgetView::DownCast( handle );
515
516   if( !widgetView )
517   {
518     return false;
519   }
520
521   WidgetView& impl = GetImplementation( widgetView );
522
523   if( strcmp( actionName.c_str(), ACTION_WIDGETVIEW_PAUSE_WIDGET ) == 0 )
524   {
525     impl.PauseWidget();
526   }
527   else if( strcmp( actionName.c_str(), ACTION_WIDGETVIEW_RESUME_WIDGET ) == 0 )
528   {
529     impl.ResumeWidget();
530   }
531   else if( strcmp( actionName.c_str(), ACTION_WIDGETVIEW_CANCEL_TOUCH_EVENT ) == 0 )
532   {
533     impl.CancelTouchEvent();
534   }
535   else if( strcmp( actionName.c_str(), ACTION_WIDGETVIEW_ACTIVATE_FAULTED_WIDGET ) == 0 )
536   {
537     impl.ActivateFaultedWidget();
538   }
539   else
540   {
541     ret = false;
542   }
543
544   return ret;
545 }
546
547 void WidgetView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
548 {
549   Dali::WidgetView::WidgetView widgetView = Dali::WidgetView::WidgetView::DownCast( Dali::BaseHandle( object ) );
550
551   if( widgetView )
552   {
553     WidgetView& impl = GetImplementation( widgetView );
554
555     switch( index )
556     {
557       case Dali::WidgetView::WidgetView::Property::PREVIEW:
558       {
559         bool previewEnabled;
560
561         if( value.Get( previewEnabled ) )
562         {
563           impl.ShowPreview( previewEnabled );
564         }
565         break;
566       }
567       case Dali::WidgetView::WidgetView::Property::LOADING_TEXT:
568       {
569         bool textEnabled;
570         Property::Map map;
571
572         if( value.Get( textEnabled ) )
573         {
574           impl.ShowStateText( textEnabled );
575         }
576         else if( value.Get( map ) )
577         {
578           impl.SetLoadingTextPropertyMap( map );
579         }
580         break;
581       }
582       case Dali::WidgetView::WidgetView::Property::PERMANENT_DELETE:
583       {
584         bool del;
585         if( value.Get( del ) )
586         {
587           impl.SetPermanentDelete( del );
588         }
589         break;
590       }
591     }
592   }
593 }
594
595 Property::Value WidgetView::GetProperty( BaseObject* object, Property::Index index )
596 {
597   Property::Value value;
598   Dali::WidgetView::WidgetView widgetView = Dali::WidgetView::WidgetView::DownCast( Dali::BaseHandle( object ) );
599
600   if( widgetView )
601   {
602     WidgetView& impl = GetImplementation( widgetView );
603
604     switch( index )
605     {
606       case Dali::WidgetView::WidgetView::Property::WIDGET_ID:
607       {
608         value = impl.GetWidgetId();
609         break;
610       }
611       case Dali::WidgetView::WidgetView::Property::INSTANCE_ID:
612       {
613         value = impl.GetInstanceId();
614         break;
615       }
616       case Dali::WidgetView::WidgetView::Property::CONTENT_INFO:
617       {
618         value = impl.GetContentInfo();
619         break;
620       }
621       case Dali::WidgetView::WidgetView::Property::TITLE:
622       {
623         value = impl.GetTitle();
624         break;
625       }
626       case Dali::WidgetView::WidgetView::Property::UPDATE_PERIOD:
627       {
628         value = impl.GetUpdatePeriod();
629         break;
630       }
631       case Dali::WidgetView::WidgetView::Property::PREVIEW:
632       {
633         value = impl.IsPreviewVisible();
634         break;
635       }
636       case Dali::WidgetView::WidgetView::Property::LOADING_TEXT:
637       {
638         value = impl.IsStateTextVisible();
639         break;
640       }
641       case Dali::WidgetView::WidgetView::Property::WIDGET_STATE_FAULTED:
642       {
643         value = impl.IsWidgetFaulted();
644         break;
645       }
646       case Dali::WidgetView::WidgetView::Property::PERMANENT_DELETE:
647       {
648         value = impl.IsPermanentDelete();
649         break;
650       }
651     }
652   }
653
654   return value;
655 }
656
657 Dali::WidgetView::WidgetView::WidgetViewSignalType& WidgetView::WidgetAddedSignal()
658 {
659   return mWidgetAddedSignal;
660 }
661
662 Dali::WidgetView::WidgetView::WidgetViewSignalType& WidgetView::WidgetDeletedSignal()
663 {
664   return mWidgetDeletedSignal;
665 }
666
667 Dali::WidgetView::WidgetView::WidgetViewSignalType& WidgetView::WidgetCreationAbortedSignal()
668 {
669   return mWidgetCreationAbortedSignal;
670 }
671
672 Dali::WidgetView::WidgetView::WidgetViewSignalType& WidgetView::WidgetContentUpdatedSignal()
673 {
674   return mWidgetContentUpdatedSignal;
675 }
676
677 Dali::WidgetView::WidgetView::WidgetViewSignalType& WidgetView::WidgetUpdatePeriodChangedSignal()
678 {
679   return mWidgetUpdatePeriodChangedSignal;
680 }
681
682 Dali::WidgetView::WidgetView::WidgetViewSignalType& WidgetView::WidgetFaultedSignal()
683 {
684   return mWidgetFaultedSignal;
685 }
686
687 void WidgetView::OnInitialize()
688 {
689   char* instanceId = NULL;
690   char* previewPath = NULL;
691   std::string previewImage;
692   widget_size_type_e sizeType;
693   screen_connector_toolkit_ops ops;
694
695   int ret = widget_instance_create( mWidgetId.c_str(), &instanceId );
696   if( ret < 0 || !instanceId )
697   {
698     DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::OnInitialize: widget_instance_create is failed [%s].\n", mWidgetId.c_str() );
699     return;
700   }
701
702   DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::OnInitialize: widget_instance_create is called. [widget id = %s, instance id = %s]\n",
703                  mWidgetId.c_str(), instanceId );
704
705   mInstanceId = instanceId;
706
707   // Preview image
708   widget_service_get_size_type( mWidth, mHeight, &sizeType );
709
710   previewPath = widget_service_get_preview_image_path( mWidgetId.c_str(), sizeType );
711   if( previewPath )
712   {
713     previewImage = previewPath;
714     free( previewPath );
715   }
716   else
717   {
718     previewImage = WIDGET_VIEW_RESOURCE_DEFAULT_IMG;
719   }
720
721   DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::OnInitialize: preview image path = %s\n", previewImage.c_str() );
722
723
724   mPreviewActor = Dali::Actor::New();
725   mPreviewActor.SetParentOrigin( ParentOrigin::CENTER );
726   mPreviewActor.SetAnchorPoint( AnchorPoint::CENTER );
727   mPreviewActor.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
728
729   mPreviewImage = Toolkit::ImageView::New( previewImage );
730   mPreviewImage.SetParentOrigin( ParentOrigin::CENTER );
731   mPreviewImage.SetAnchorPoint( AnchorPoint::CENTER );
732   mPreviewImage.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
733
734   Self().SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
735   Self().SetSize( mWidth, mHeight );
736
737   Self().Add( mPreviewActor );
738   mPreviewActor.Add( mPreviewImage );
739
740   mStateTextActor = Dali::Actor::New();
741   mStateTextActor.SetParentOrigin( ParentOrigin::CENTER );
742   mStateTextActor.SetAnchorPoint( AnchorPoint::CENTER );
743   mStateTextActor.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
744
745   // State text
746   // TODO: use po files
747   mStateText = Toolkit::TextLabel::New( "Loading..." );
748   mStateText.SetParentOrigin( ParentOrigin::CENTER );
749   mStateText.SetAnchorPoint( AnchorPoint::CENTER );
750   mStateText.SetProperty( Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER" );
751   mStateText.SetProperty( Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT, "CENTER" );
752   mStateText.SetProperty( Toolkit::TextLabel::Property::TEXT_COLOR, Dali::Color::WHITE );
753   mStateText.SetProperty( Toolkit::TextLabel::Property::FONT_STYLE, "Bold" );
754
755   mPreviewActor.Add( mStateTextActor );
756   mStateTextActor.Add( mStateText );
757
758   // launch widget
759   mPid = widget_instance_launch( instanceId, (char *)mContentInfo.c_str(), mWidth, mHeight );
760   if( mPid < 0)
761   {
762     DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::OnInitialize: widget_instance_launch is failed. [%s]\n", mWidgetId.c_str() );
763
764     // Emit signal
765     Dali::WidgetView::WidgetView handle( GetOwner() );
766     mWidgetCreationAbortedSignal.Emit( handle );
767
768     return;
769   }
770
771   ops.updated_cb = OnBufferUpdated;
772   ops.removed_cb = OnSurfaceRemoved;
773   ops.added_cb = OnBufferAdded;
774   mWatcherHandle = screen_connector_toolkit_add(&ops, (char *)instanceId, SCREEN_CONNECTOR_SCREEEN_TYPE_WIDGET, this);
775   DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::OnInitialize: widget_instance_launch is called. [%s, mPid = %d]\n", mWidgetId.c_str(), mPid );
776
777 }
778
779 void WidgetView::OnStageConnection( int depth )
780 {
781   Control::OnStageConnection( depth );
782 }
783
784 void WidgetView::OnStageDisconnection()
785 {
786   Control::OnStageDisconnection();
787 }
788
789 void WidgetView::OnSizeSet( const Vector3& targetSize )
790 {
791   if( mWidgetImageView )
792   {
793     mWidgetImageView.SetSize( targetSize );
794   }
795 }
796
797 bool WidgetView::IsWidgetImageView()
798 {
799   return ( mWidgetImageView )? true: false;
800 }
801
802 void WidgetView::UpdateImageSource( tbm_surface_h source )
803 {
804   mImageSource->SetSource( source );
805   Dali::Stage::GetCurrent().KeepRendering( 0.0f );
806 }
807
808 void WidgetView::ConnectSignal( tizen_remote_surface* surface )
809 {
810   if( mWidgetImageView && surface )
811   {
812     mRemoteSurface = surface;
813
814     Self().TouchSignal().Connect( this, &WidgetView::OnTouch );
815     Self().WheelEventSignal().Connect( this, &WidgetView::OnWheelEvent );
816   }
817
818 }
819
820 bool WidgetView::OnTouch( Dali::Actor actor, const Dali::TouchData& event )
821 {
822   tizen_remote_surface_event_type type = TIZEN_REMOTE_SURFACE_EVENT_TYPE_NONE;
823
824   if( event.GetPointCount() == 0 || mRemoteSurface == NULL )
825   {
826     return false;
827   }
828
829   switch( event.GetState( 0 ) )
830   {
831     case Dali::PointState::UP:
832     {
833       type = TIZEN_REMOTE_SURFACE_EVENT_TYPE_MOUSE_UP;
834       break;
835     }
836     case Dali::PointState::DOWN:
837     {
838       type = TIZEN_REMOTE_SURFACE_EVENT_TYPE_MOUSE_DOWN;
839       break;
840     }
841     case Dali::PointState::MOTION:
842     {
843       type = TIZEN_REMOTE_SURFACE_EVENT_TYPE_MOUSE_MOVE;
844       break;
845     }
846     default:
847     {
848       return false;
849     }
850   }
851
852   int button = 1;
853
854   if( type == TIZEN_REMOTE_SURFACE_EVENT_TYPE_MOUSE_MOVE )
855   {
856     button = 0 ;
857   }
858
859   Vector2 localPos = event.GetLocalPosition( 0 );
860
861   tizen_remote_surface_transfer_mouse_event( mRemoteSurface,
862                                              type,
863                                              0,
864                                              button,
865                                              (int)localPos.x,
866                                              (int)localPos.y,
867                                              wl_fixed_from_double( event.GetEllipseRadius( 0 ).x ),
868                                              wl_fixed_from_double( event.GetEllipseRadius( 0 ).y ),
869                                              wl_fixed_from_double( event.GetPressure( 0 ) ),
870                                              wl_fixed_from_double( event.GetAngle( 0 ).degree ),
871                                              TIZEN_INPUT_DEVICE_CLAS_TOUCHSCREEN,
872                                              TIZEN_INPUT_DEVICE_SUBCLAS_NONE,
873                                              "",
874                                              event.GetTime()
875                                            );
876   return true;
877 }
878
879 bool WidgetView::OnWheelEvent( Dali::Actor actor, const Dali::WheelEvent& event )
880 {
881   if( mRemoteSurface == NULL )
882   {
883     return false;
884   }
885   //ToDo: We should check TIZEN_INPUT_DEVICE_CLAS_MOUSE
886   tizen_remote_surface_transfer_mouse_wheel( mRemoteSurface,
887                                              event.direction,
888                                              event.z,
889                                              TIZEN_INPUT_DEVICE_CLAS_MOUSE,
890                                              TIZEN_INPUT_DEVICE_SUBCLAS_NONE,
891                                              "",
892                                              event.timeStamp
893                                            );
894   return true;
895 }
896
897 Vector3 WidgetView::GetNaturalSize()
898 {
899   Vector3 size;
900   size.x = mWidth;
901   size.y = mHeight;
902
903   if( size.x > 0 && size.y > 0 )
904   {
905     size.z = std::min( size.x, size.y );
906     return size;
907   }
908   else
909   {
910     return Control::GetNaturalSize();
911   }
912 }
913
914 float WidgetView::GetHeightForWidth( float width )
915 {
916   if( mWidth > 0 && mHeight > 0 )
917   {
918     return GetHeightForWidthBase( width );
919   }
920   else
921   {
922     return Control::GetHeightForWidthBase( width );
923   }
924 }
925
926 float WidgetView::GetWidthForHeight( float height )
927 {
928   if( mWidth > 0 && mHeight > 0 )
929   {
930     return GetWidthForHeightBase( height );
931   }
932   else
933   {
934     return Control::GetWidthForHeightBase( height );
935   }
936 }
937
938 void WidgetView::CloseRemoteSurface()
939 {
940   screen_connector_toolkit_remove( mWatcherHandle );
941   mRemoteSurface = NULL;
942 }
943
944 void WidgetView::SetLoadingTextPropertyMap( Property::Map map )
945 {
946   if( mStateText )
947   {
948     Property::Value* fontStyle = map.Find( FONT_STYLE );
949     std::string style;
950
951     if( fontStyle && fontStyle->Get( style ) )
952     {
953       mStateText.SetProperty( Toolkit::TextLabel::Property::FONT_STYLE, style );
954     }
955
956     Property::Value* textPointSize = map.Find( TEXT_POINT_SIZE );
957     float size = 0.f;
958
959     if( textPointSize && textPointSize->Get( size ) )
960     {
961       mStateText.SetProperty( Toolkit::TextLabel::Property::POINT_SIZE, size );
962     }
963
964     Property::Value* textColor = map.Find( TEXT_COLOR );
965     Vector4 color;
966
967     if( textColor && textColor->Get( color ) )
968     {
969       mStateText.SetProperty( Toolkit::TextLabel::Property::TEXT_COLOR, color );
970     }
971   }
972 }
973
974 } // namespace Internal
975
976 } // namespace WidgetView
977
978 } // namespace Dali