Merge "A simple control using visuals" into devel/master
[platform/core/uifw/dali-demo.git] / examples / simple-visuals-control / my-control-impl.cpp
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
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 // CLASS HEADER
18
19 #include "my-control-impl.h"
20
21 // EXTERNAL INCLUDES
22
23 #include <dali/public-api/object/type-registry-helper.h>
24 #include <dali/devel-api/scripting/enum-helper.h>
25 #include <dali-toolkit/dali-toolkit.h>
26 #include <dali-toolkit/devel-api/focus-manager/keyinput-focus-manager.h>
27 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
28 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
29
30 using namespace Dali;
31 using namespace Dali::Toolkit;
32
33 namespace Demo
34 {
35 namespace Internal
36 {
37
38 namespace
39 {
40
41
42 Dali::BaseHandle Create()
43 {
44   return Demo::MyControl::New();
45 }
46
47 // Required code for Property set up.
48
49 DALI_TYPE_REGISTRATION_BEGIN( MyControl, Dali::Toolkit::Control, Create );
50
51 DALI_PROPERTY_REGISTRATION( Demo, MyControl, "iconVisual", MAP, ICON_VISUAL )
52
53 DALI_TYPE_REGISTRATION_END();
54
55 // Add an enum to string conversion entry for the control's visuals.  In this case just the icon visual.
56 // Enables Setting of the property using enums or strings.
57 DALI_ENUM_TO_STRING_TABLE_BEGIN( VISUAL_PROPERTIES )
58 { "iconVisual", Demo::MyControl::Property::ICON_VISUAL },
59 DALI_ENUM_TO_STRING_TABLE_END( VISUAL_PROPERTIES )
60
61
62 } // anonymous namespace
63
64
65 Internal::MyControl::MyControl()
66 : Control( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) )
67 {
68 }
69
70 Demo::MyControl Internal::MyControl::New()
71 {
72   IntrusivePtr<Internal::MyControl> impl = new Internal::MyControl();
73   Demo::MyControl handle = Demo::MyControl( *impl );
74   impl->Initialize();
75   return handle;
76 }
77
78 void MyControl::OnInitialize()
79 {
80   Dali::Actor self = Self();
81   self.SetKeyboardFocusable( true );
82 }
83
84 void MyControl::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
85 {
86   Demo::MyControl myControl = Demo::MyControl::DownCast( Dali::BaseHandle( object ) );
87
88   if( myControl )
89   {
90     MyControl& impl = GetImpl( myControl );
91     switch ( index )
92     {
93       case Demo::MyControl::Property::ICON_VISUAL:
94       {
95         Toolkit::Visual::Base iconVisual;
96         Toolkit::VisualFactory visualFactory = Toolkit::VisualFactory::Get();
97         Property::Map *map = value.GetMap();
98         if( map && !map->Empty() )
99         {
100           iconVisual = visualFactory.CreateVisual( *map );
101         }
102
103         if ( iconVisual )
104         {
105           impl.RegisterVisual( index, iconVisual );
106         }
107         break;
108       }
109     }
110   }
111 }
112
113 Property::Value MyControl::GetProperty( BaseObject* object, Property::Index propertyIndex )
114 {
115   Property::Value value;
116
117   Demo::MyControl myControl = Demo::MyControl::DownCast( Dali::BaseHandle( object ) );
118
119   if ( myControl )
120   {
121     switch ( propertyIndex )
122     {
123       case Demo::MyControl::Property::ICON_VISUAL:
124       {
125         Property::Map map;
126         Toolkit::Visual::Base visual =  GetImpl( myControl ).GetVisual( propertyIndex );
127         if ( visual )
128         {
129           visual.CreatePropertyMap( map ); // Creates a Property map containing the Visual that ICON_VISUAL currently is. Can change if state changes.
130           value = map;
131         }
132         break;
133       }
134       default:
135         break;
136     }
137   }
138
139   return value;
140 }
141
142 } // Internal
143 } // Demo