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