[AT-SPI] Fix role setting
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / popup / confirmation-popup-impl.cpp
1 /*
2  * Copyright (c) 2020 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
18 // CLASS HEADER
19 #include "confirmation-popup-impl.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/object/type-registry.h>
23 #include <dali/public-api/object/type-registry-helper.h>
24 #include <cstring>
25
26 // INTERNAL INCLUDES
27 #include <dali-toolkit/internal/controls/control/control-data-impl.h>
28
29 namespace Dali
30 {
31
32 namespace Toolkit
33 {
34
35 namespace Internal
36 {
37
38 namespace
39 {
40
41 /*
42  * This struct is used to define all details required about a dynamically created signal.
43  */
44 struct ControlDetailType
45 {
46   const char* signalName;
47   const char* controlName;
48   const char* connectSignalPropertyName;
49 };
50
51 /* A table of all control details. These details are kept in one place for maintainability.
52  *  Name of the signal     | Name of the control  | Name of the property which lets the
53  *  the app-developer      | which will provide   | app developer choose which signal
54  *  can connect to.        | the signal.          | within the control to connect to.    */
55 const ControlDetailType ControlDetails[] = {
56   { "controlSignalOk",       "controlOk",           "connectSignalOkSelected"     },
57   { "controlSignalCancel",   "controlCancel",       "connectSignalCancelSelected" },
58 };
59 const unsigned int ControlDetailsCount = sizeof( ControlDetails ) / sizeof( ControlDetails[0] );
60
61 // To give sensible default behaviour to save the connect signal properties being set.
62 const char* const DEFAULT_CONNECT_SIGNAL_NAME = "clicked";
63
64 BaseHandle Create()
65 {
66   return Toolkit::ConfirmationPopup::New();
67 }
68
69 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::ConfirmationPopup, Toolkit::Popup, Create )
70
71 DALI_PROPERTY_REGISTRATION( Toolkit, ConfirmationPopup, ControlDetails[0].connectSignalPropertyName, STRING, CONNECT_SIGNAL_OK_SELECTED     )
72 DALI_PROPERTY_REGISTRATION( Toolkit, ConfirmationPopup, ControlDetails[1].connectSignalPropertyName, STRING, CONNECT_SIGNAL_CANCEL_SELECTED )
73
74 // Note: We do not use the macros for signal registration as we do not want to redefine the signal name strings.
75 // We have predefined them for optimal signal name to control name lookup.
76 SignalConnectorType signalConnector1( typeRegistration, ControlDetails[0].signalName, &Toolkit::Internal::ConfirmationPopup::DoConnectSignal );
77 SignalConnectorType signalConnector2( typeRegistration, ControlDetails[1].signalName, &Toolkit::Internal::ConfirmationPopup::DoConnectSignal );
78
79 DALI_TYPE_REGISTRATION_END()
80
81 } // Unnamed namespace
82
83 Dali::Toolkit::ConfirmationPopup ConfirmationPopup::New()
84 {
85   // Create the implementation, temporarily owned on stack.
86   IntrusivePtr< ConfirmationPopup > internalConfirmationPopup = new ConfirmationPopup();
87
88   // Pass ownership to CustomActor
89   Dali::Toolkit::ConfirmationPopup confirmationPopup( *internalConfirmationPopup );
90
91   // Second-phase initialisation of the implementation.
92   // This can only be done after the CustomActor connection has been made...
93   internalConfirmationPopup->Initialize();
94
95   return confirmationPopup;
96 }
97
98 ConfirmationPopup::ConfirmationPopup()
99 : Toolkit::Internal::Popup()
100 {
101   mControlSignals.reserve( MAXIMUM_NUMBER_OF_CONTROLS );
102   mControlSignalNames[ Toolkit::ConfirmationPopup::CONTROL_OK ] = DEFAULT_CONNECT_SIGNAL_NAME;
103   mControlSignalNames[ Toolkit::ConfirmationPopup::CONTROL_CANCEL ] = DEFAULT_CONNECT_SIGNAL_NAME;
104 }
105
106 ConfirmationPopup::~ConfirmationPopup()
107 {
108   for( SignalContainerType::iterator i = mControlSignals.begin(); i != mControlSignals.end(); ++i )
109   {
110     delete ( i->second );
111   }
112   mControlSignals.clear();
113 }
114
115 void ConfirmationPopup::SetProperty( BaseObject* object, Property::Index propertyIndex, const Property::Value& value )
116 {
117   Toolkit::ConfirmationPopup popup = Toolkit::ConfirmationPopup::DownCast( Dali::BaseHandle( object ) );
118
119   if ( popup )
120   {
121     ConfirmationPopup& popupImpl( GetDerivedImplementation( popup ) );
122
123     switch ( propertyIndex )
124     {
125       case Toolkit::ConfirmationPopup::Property::CONNECT_SIGNAL_OK_SELECTED:
126       {
127         popupImpl.SetControlSignalName( Toolkit::ConfirmationPopup::CONTROL_OK, value.Get< std::string >() );
128         break;
129       }
130       case Toolkit::ConfirmationPopup::Property::CONNECT_SIGNAL_CANCEL_SELECTED:
131       {
132         popupImpl.SetControlSignalName( Toolkit::ConfirmationPopup::CONTROL_CANCEL, value.Get< std::string >() );
133         break;
134       }
135     }
136   }
137 }
138
139 Property::Value ConfirmationPopup::GetProperty( BaseObject* object, Property::Index propertyIndex )
140 {
141   Property::Value value;
142
143   Toolkit::ConfirmationPopup popup = Toolkit::ConfirmationPopup::DownCast( Dali::BaseHandle( object ) );
144
145   if ( popup )
146   {
147     ConfirmationPopup& popupImpl( GetDerivedImplementation( popup ) );
148
149     switch ( propertyIndex )
150     {
151       case Toolkit::ConfirmationPopup::Property::CONNECT_SIGNAL_OK_SELECTED:
152       {
153         value = popupImpl.GetControlSignalName( Toolkit::ConfirmationPopup::CONTROL_OK );
154         break;
155       }
156       case Toolkit::ConfirmationPopup::Property::CONNECT_SIGNAL_CANCEL_SELECTED:
157       {
158         value = popupImpl.GetControlSignalName( Toolkit::ConfirmationPopup::CONTROL_CANCEL );
159         break;
160       }
161     }
162   }
163
164   return value;
165 }
166
167 void ConfirmationPopup::SetControlSignalName( const unsigned int controlNumber, const std::string& signalName )
168 {
169   if( controlNumber < ControlDetailsCount )
170   {
171     mControlSignalNames[ controlNumber ] = signalName;
172   }
173 }
174
175 std::string ConfirmationPopup::GetControlSignalName( unsigned int controlNumber ) const
176 {
177   if( controlNumber < ControlDetailsCount )
178   {
179     return mControlSignalNames[ controlNumber ];
180   }
181
182   return "";
183 }
184
185 bool ConfirmationPopup::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
186 {
187   Dali::BaseHandle handle( object );
188   Toolkit::ConfirmationPopup popup = Toolkit::ConfirmationPopup::DownCast( handle );
189
190   // Look up the requested signal, attempting to create it dynamically if it doesn't exist.
191   SignalDelegate* signalDelegate = Dali::Toolkit::GetDerivedImplementation( popup ).GetControlSignal( signalName );
192   if( signalDelegate )
193   {
194     // The signal delegate was created successfully, attempt to connect it to a callback if specified.
195     // If none is specified, the creation is still successful as the signal delegate can connect at a later time.
196     if( functor )
197     {
198       signalDelegate->Connect( tracker, functor );
199     }
200     return true;
201   }
202
203   // The signal could not be created.
204   return false;
205 }
206
207 SignalDelegate* ConfirmationPopup::GetControlSignal( const std::string& signalName )
208 {
209   // Check if the specified signal name already exists.
210   SignalContainerType::iterator end = mControlSignals.end();
211   for( SignalContainerType::iterator iter = mControlSignals.begin(); iter != end; ++iter )
212   {
213     // Find the first non-connected signal by matching signal name.
214     if( ( signalName == iter->first ) && ( !iter->second->IsConnected() ) )
215     {
216       // The requested signal (delegate) already exists, just return it.
217       return iter->second;
218     }
219   }
220
221   // The signal doesn't exist, or it does but it's already connected to something else.
222   // To make a new connection to an existing signal, we need a new delegate,
223   // as delegates house a signal connection functor each.
224   // Check the signal name is valid and if so create the signal dynamically.
225   for( unsigned int i = 0; i < ControlDetailsCount; ++i )
226   {
227     if( 0 == strcmp( signalName.c_str(), ControlDetails[ i ].signalName ) )
228     {
229       // The signal name is valid, check the respective actor to connect to exists.
230       Actor connectActor = Self().FindChildByName( ControlDetails[ i ].controlName );
231       if( connectActor )
232       {
233         // The actor exists, set up a signal delegate that will allow the application developer
234         // to connect the actor signal directly to their callback.
235         // Note: We don't use the GetControlSignalName() here for speedup, as we know the array bound is capped.
236         SignalDelegate* signalDelegate = new SignalDelegate( connectActor, mControlSignalNames[ i ] );
237
238         // Store the delegate with the signal name so we know what signals have been dynamically created so far.
239         mControlSignals.push_back( std::make_pair( signalName, signalDelegate ) );
240
241         // Return the delegate to allow connection to the newly created signal.
242         return signalDelegate;
243       }
244
245       // Signal name valid but could not connect to the control,
246       return NULL;
247     }
248   }
249
250   // Signal name was not found (invalid).
251   return NULL;
252 }
253
254
255 } // namespace Internal
256
257 } // namespace Toolkit
258
259 } // namespace Dali