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